snapcast_control/protocol/
group.rs

1use super::{client::Client, server::Server};
2use serde::{Deserialize, Serialize};
3
4// the group
5/// A group of clients maintained by the Snapcast server
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
7pub struct Group {
8  pub id: String,
9  pub name: String,
10  pub stream_id: String,
11  pub muted: bool,
12  pub clients: Vec<Client>,
13}
14
15// params and results
16#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
17pub struct GetStatusParams {
18  pub id: String,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
22pub struct GetStatusResult {
23  pub group: Group,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub struct SetMuteParams {
28  pub id: String,
29  pub mute: bool,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
33pub struct SetMuteResult {
34  pub mute: bool,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
38pub struct OnMuteParams {
39  pub id: String,
40  pub mute: bool,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
44pub struct SetStreamParams {
45  pub id: String,
46  pub stream_id: String,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
50pub struct SetStreamResult {
51  pub stream_id: String,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
55pub struct SetClientsParams {
56  pub id: String,
57  /// vec of client ids
58  pub clients: Vec<String>,
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
62pub struct SetClientsResult {
63  pub server: Server,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
67pub struct SetNameParams {
68  pub id: String,
69  pub name: String,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
73pub struct SetNameResult {
74  pub name: String,
75}
76
77// notifications
78#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
79pub struct OnStreamChangedParams {
80  pub id: String,
81  pub stream_id: String,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
85pub struct OnNameChangedParams {
86  pub id: String,
87  pub name: String,
88}
89
90#[cfg(test)]
91mod tests {
92  use crate::protocol::client::{ClientConfig, ClientVolume, Host, LastSeen, Snapclient};
93
94  use super::*;
95
96  #[test]
97  fn serialize_group() {
98    let group = Group {
99      id: "4dcc4e3b-c699-a04b-7f0c-8260d23c43e1".to_string(),
100      muted: false,
101      name: "".to_string(),
102      stream_id: "stream 2".to_string(),
103      clients: vec![
104        Client {
105          config: ClientConfig {
106            instance: 1,
107            latency: 0,
108            name: "".to_string(),
109            volume: ClientVolume {
110              muted: false,
111              percent: 100,
112            },
113          },
114          connected: true,
115          host: Host {
116            arch: "x86_64".to_string(),
117            ip: "127.0.0.1".to_string(),
118            mac: "00:21:6a:7d:74:fc".to_string(),
119            name: "T400".to_string(),
120            os: "Linux Mint 17.3 Rosa".to_string(),
121          },
122          id: "00:21:6a:7d:74:fc".to_string(),
123          last_seen: LastSeen {
124            sec: 1488025905,
125            usec: 45238,
126          },
127          snapclient: Snapclient {
128            name: "Snapclient".to_string(),
129            protocol_version: 2,
130            version: "0.10.0".to_string(),
131          },
132        },
133        Client {
134          config: ClientConfig {
135            instance: 2,
136            latency: 6,
137            name: "123 456".to_string(),
138            volume: ClientVolume {
139              muted: false,
140              percent: 48,
141            },
142          },
143          connected: true,
144          host: Host {
145            arch: "x86_64".to_string(),
146            ip: "127.0.0.1".to_string(),
147            mac: "00:21:6a:7d:74:fc".to_string(),
148            name: "T400".to_string(),
149            os: "Linux Mint 17.3 Rosa".to_string(),
150          },
151          id: "00:21:6a:7d:74:fc#2".to_string(),
152          last_seen: LastSeen {
153            sec: 1488025901,
154            usec: 864472,
155          },
156          snapclient: Snapclient {
157            name: "Snapclient".to_string(),
158            protocol_version: 2,
159            version: "0.10.0".to_string(),
160          },
161        },
162      ],
163    };
164
165    serde_json::to_string(&group).unwrap();
166  }
167
168  #[test]
169  fn deserialize_group() {
170    let json = r#"{"clients":[{"config":{"instance":2,"latency":6,"name":"123 456","volume":{"muted":false,"percent":48}},"connected":true,"host":{"arch":"x86_64","ip":"127.0.0.1","mac":"00:21:6a:7d:74:fc","name":"T400","os":"Linux Mint 17.3 Rosa"},"id":"00:21:6a:7d:74:fc#2","lastSeen":{"sec":1488025901,"usec":864472},"snapclient":{"name":"Snapclient","protocolVersion":2,"version":"0.10.0"}},{"config":{"instance":1,"latency":0,"name":"","volume":{"muted":false,"percent":100}},"connected":true,"host":{"arch":"x86_64","ip":"127.0.0.1","mac":"00:21:6a:7d:74:fc","name":"T400","os":"Linux Mint 17.3 Rosa"},"id":"00:21:6a:7d:74:fc","lastSeen":{"sec":1488025905,"usec":45238},"snapclient":{"name":"Snapclient","protocolVersion":2,"version":"0.10.0"}}],"id":"4dcc4e3b-c699-a04b-7f0c-8260d23c43e1","muted":false,"name":"","stream_id":"stream 2"}"#;
171    let group: Group = serde_json::from_str(json).unwrap();
172
173    assert_eq!(group.id, "4dcc4e3b-c699-a04b-7f0c-8260d23c43e1");
174  }
175}