indi/serialization/
switch_vector.rs1use super::super::*;
2use super::*;
3
4impl CommandtoParam for DefSwitchVector {
5 fn get_name(&self) -> &String {
6 &self.name
7 }
8 fn get_group(&self) -> &Option<String> {
9 &self.group
10 }
11 fn to_param(self) -> Parameter {
12 Parameter::SwitchVector(SwitchVector {
13 name: self.name,
14 group: self.group,
15 label: self.label,
16 state: self.state,
17 perm: self.perm,
18 rule: self.rule,
19 timeout: self.timeout,
20 timestamp: self.timestamp.map(Timestamp::into_inner),
21 values: self
22 .switches
23 .into_iter()
24 .map(|i| {
25 (
26 i.name,
27 Switch {
28 label: i.label,
29 value: i.value,
30 },
31 )
32 })
33 .collect(),
34 })
35 }
36}
37
38impl CommandToUpdate for SetSwitchVector {
39 fn get_name(&self) -> &String {
40 &self.name
41 }
42
43 fn update_param(self, param: &mut Parameter) -> Result<String, UpdateError> {
44 match param {
45 Parameter::SwitchVector(switch_vector) => {
46 switch_vector.timestamp = self.timestamp.map(Timestamp::into_inner);
47 for switch in self.switches {
48 if let Some(existing) = switch_vector.values.get_mut(&switch.name) {
49 existing.value = switch.value;
50 }
51 }
52 Ok(self.name)
53 }
54 _ => Err(UpdateError::ParameterTypeMismatch(self.name.clone())),
55 }
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_parse_switch() {
65 let xml = r#"<defSwitch name="INDI_DISABLED" label="Disabled">On</defSwitch>"#;
66 let command: DefSwitch = quick_xml::de::from_str(xml).unwrap();
67
68 assert_eq!(
69 command,
70 DefSwitch {
71 name: "INDI_DISABLED".to_string(),
72 label: Some("Disabled".to_string()),
73 value: SwitchState::On
74 }
75 );
76
77 let xml = r#"
78 <defSwitch name="INDI_DISABLED" label="Disabled">
79Off
80 </defSwitch>
81"#;
82
83 let command: DefSwitch = quick_xml::de::from_str(xml).unwrap();
84 assert_eq!(
85 command,
86 DefSwitch {
87 name: "INDI_DISABLED".to_string(),
88 label: Some("Disabled".to_string()),
89 value: SwitchState::Off
90 }
91 );
92 }
93
94 #[test]
95 fn test_one_switch() {
96 let xml = r#"
97 <oneSwitch name="INDI_DISABLED">
98On
99 </oneSwitch>
100"#;
101
102 let command: OneSwitch = quick_xml::de::from_str(xml).unwrap();
103
104 assert_eq!(
105 command,
106 OneSwitch {
107 name: "INDI_DISABLED".to_string(),
108 value: SwitchState::On
109 }
110 );
111 }
112
113 #[test]
114 fn test_send_new_switch_vector() {
115 let timestamp = DateTime::from_str("2022-10-13T07:41:56.301Z")
117 .unwrap()
118 .into();
119
120 let command = NewSwitchVector {
121 device: String::from_str("CCD Simulator").unwrap(),
122 name: String::from_str("Exposure").unwrap(),
123 timestamp: Some(timestamp),
124 switches: vec![OneSwitch {
125 name: String::from_str("seconds").unwrap(),
126 value: SwitchState::On,
127 }],
128 };
129 let result = quick_xml::se::to_string(&command).unwrap();
130
131 assert_eq!(
132 result,
133 String::from_str("<newSwitchVector device=\"CCD Simulator\" name=\"Exposure\" timestamp=\"2022-10-13T07:41:56.301\"><oneSwitch name=\"seconds\">On</oneSwitch></newSwitchVector>").unwrap()
134 );
135 }
136
137 #[test]
138 fn test_def_switch_vector() {
139 let xml = r#"
140 <defSwitchVector device="CCD Simulator" name="SIMULATE_BAYER" label="Bayer" group="Simulator Config" state="Idle" perm="rw" rule="OneOfMany" timeout="60" timestamp="2022-09-06T01:41:22">
141 <defSwitch name="INDI_ENABLED" label="Enabled">
142 Off
143 </defSwitch>
144 <defSwitch name="INDI_DISABLED" label="Disabled">
145 On
146 </defSwitch>
147 </defSwitchVector>
148 "#;
149 let param: DefSwitchVector = quick_xml::de::from_str(xml).unwrap();
150
151 assert_eq!(param.device, "CCD Simulator");
152 assert_eq!(param.name, "SIMULATE_BAYER");
153 assert_eq!(param.switches.len(), 2)
154 }
155
156 #[test]
157 fn test_set_switch_vector() {
158 let xml = r#"
159 <setSwitchVector device="CCD Simulator" name="DEBUG" state="Ok" timeout="0" timestamp="2022-10-01T22:07:22">
160 <oneSwitch name="ENABLE">
161 On
162 </oneSwitch>
163 <oneSwitch name="DISABLE">
164 Off
165 </oneSwitch>
166 </setSwitchVector>
167 "#;
168 let param: SetSwitchVector = quick_xml::de::from_str(xml).unwrap();
169 assert_eq!(param.device, "CCD Simulator");
170 assert_eq!(param.name, "DEBUG");
171 assert_eq!(param.switches.len(), 2)
172 }
173
174 #[test]
175 fn test_new_switch_vector() {
176 let xml = r#"
177 <newSwitchVector device="CCD Simulator" name="DEBUG" timestamp="2022-10-01T22:07:22">
178 <oneSwitch name="ENABLE">
179 On
180 </oneSwitch>
181 <oneSwitch name="DISABLE">
182 Off
183 </oneSwitch>
184 </newSwitchVector>
185 "#;
186 let param: NewSwitchVector = quick_xml::de::from_str(xml).unwrap();
187
188 assert_eq!(param.device, "CCD Simulator");
189 assert_eq!(param.name, "DEBUG");
190 assert_eq!(param.switches.len(), 2)
191 }
192}