@@ -40,7 +40,7 @@ impl Timestamped for SwitchData {
40
40
41
41
impl Typenamed for SwitchData {
42
42
fn type_name ( & self ) -> & str {
43
- return "switch"
43
+ return "switch" ;
44
44
}
45
45
}
46
46
@@ -65,7 +65,7 @@ impl Timestamped for CoverData {
65
65
66
66
impl Typenamed for CoverData {
67
67
fn type_name ( & self ) -> & str {
68
- return "cover"
68
+ return "cover" ;
69
69
}
70
70
}
71
71
@@ -228,9 +228,78 @@ fn handle_message<'a, T: Deserialize<'a> + Clone + Debug + Timestamped + Typenam
228
228
229
229
#[ cfg( test) ]
230
230
mod tests {
231
+ use super :: * ;
232
+ use influxdb:: Query ;
231
233
use paho_mqtt:: QOS_1 ;
234
+ use std:: sync:: mpsc:: sync_channel;
235
+ use std:: time:: Duration ;
232
236
233
- use super :: * ;
237
+ #[ test]
238
+ fn test_handle_switch_message ( ) -> Result < ( ) > {
239
+ let ( tx, rx) = sync_channel ( 100 ) ;
240
+ let txs = vec ! [ tx] ;
241
+
242
+ let mut logger = ShellyLogger :: new ( txs) ;
243
+
244
+ let message = Message :: new ( "shellies/loo-fan/status/switch:1" , "{\" id\" :0, \" source\" :\" timer\" , \" output\" :false, \" apower\" :0.0, \" voltage\" :226.5, \" current\" :3.1, \" aenergy\" :{\" total\" :1094.865,\" by_minute\" :[0.000,0.000,0.000],\" minute_ts\" :1703415907},\" temperature\" :{\" tC\" :36.4, \" tF\" :97.5}}" , QOS_1 ) ;
245
+ logger. check_message ( & message) ;
246
+
247
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
248
+ assert ! ( result. starts_with( "output,location=loo-fan,channel=1,sensor=shelly,type=switch,unit=bool value=0i " ) ) ;
249
+
250
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
251
+ assert ! ( result. starts_with( "power,location=loo-fan,channel=1,sensor=shelly,type=switch,unit=W value=0 " ) ) ;
252
+
253
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
254
+ assert ! ( result. starts_with( "current,location=loo-fan,channel=1,sensor=shelly,type=switch,unit=A value=3.0999" ) ) ;
255
+
256
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
257
+ assert ! ( result. starts_with( "voltage,location=loo-fan,channel=1,sensor=shelly,type=switch,unit=V value=226.5 " ) ) ;
258
+
259
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
260
+ assert ! ( result. starts_with( "total_energy,location=loo-fan,channel=1,sensor=shelly,type=switch,unit=Wh value=1094.86" ) ) ;
261
+
262
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
263
+ assert ! ( result. starts_with( "temperature,location=loo-fan,channel=1,sensor=shelly,type=switch,unit=°C value=36.40" ) ) ;
264
+
265
+ let result = rx. recv_timeout ( Duration :: from_micros ( 100 ) ) ;
266
+ assert ! ( result. is_err( ) ) ;
267
+ Ok ( ( ) )
268
+ }
269
+
270
+ #[ test]
271
+ fn test_handle_curtain_message ( ) -> Result < ( ) > {
272
+ let ( tx, rx) = sync_channel ( 100 ) ;
273
+ let txs = vec ! [ tx] ;
274
+
275
+ let mut logger = ShellyLogger :: new ( txs) ;
276
+
277
+ let message = Message :: new ( "shellies/bedroom-curtain/status/cover:0" , "{\" id\" :0, \" source\" :\" limit_switch\" , \" state\" :\" open\" ,\" apower\" :0.0,\" voltage\" :231.7,\" current\" :0.500,\" pf\" :0.00,\" freq\" :50.0,\" aenergy\" :{\" total\" :3.143,\" by_minute\" :[0.000,0.000,97.712],\" minute_ts\" :1703414519},\" temperature\" :{\" tC\" :30.7, \" tF\" :87.3},\" pos_control\" :true,\" last_direction\" :\" open\" ,\" current_pos\" :100}" , QOS_1 ) ;
278
+ logger. check_message ( & message) ;
279
+
280
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
281
+ assert ! ( result. starts_with( "position,location=bedroom-curtain,channel=0,sensor=shelly,type=cover,unit=% value=100i " ) ) ;
282
+
283
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
284
+ assert ! ( result. starts_with( "power,location=bedroom-curtain,channel=0,sensor=shelly,type=cover,unit=W value=0 " ) ) ;
285
+
286
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
287
+ assert ! ( result. starts_with( "current,location=bedroom-curtain,channel=0,sensor=shelly,type=cover,unit=A value=0.5 " ) ) ;
288
+
289
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
290
+ assert ! ( result. starts_with( "voltage,location=bedroom-curtain,channel=0,sensor=shelly,type=cover,unit=V value=231.6999" ) ) ;
291
+
292
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
293
+ assert ! ( result. starts_with( "total_energy,location=bedroom-curtain,channel=0,sensor=shelly,type=cover,unit=Wh value=3.14" ) ) ;
294
+
295
+ let result = rx. recv ( ) ?. build ( ) ?. get ( ) ;
296
+ assert ! ( result. starts_with( "temperature,location=bedroom-curtain,channel=0,sensor=shelly,type=cover,unit=°C value=30.7" ) ) ;
297
+
298
+ let result = rx. recv_timeout ( Duration :: from_micros ( 100 ) ) ;
299
+ assert ! ( result. is_err( ) ) ;
300
+
301
+ Ok ( ( ) )
302
+ }
234
303
235
304
#[ test]
236
305
fn test_parse_switch_status ( ) -> Result < ( ) > {
0 commit comments