@@ -178,7 +178,7 @@ private void receiveData(MulticastSocket socket, DatagramPacket dgram) {
178
178
String sentence = new String (dgram .getData (), 0 ,
179
179
dgram .getLength ());
180
180
181
- if (sentence .contains ("\" voltage\" " ) || sentence .contains ("\" mid\" " ))
181
+ if (sentence .contains ("\" plug \" " ) || sentence . contains ( " \" voltage\" " ) || sentence .contains ("\" mid\" " ))
182
182
logger .info ("Received packet: " + sentence );
183
183
else
184
184
logger .debug ("Received packet: " + sentence );
@@ -238,20 +238,19 @@ private void processEvent(XiaomiGatewayBindingProvider provider, String itemName
238
238
String type = provider .getItemType (itemName );
239
239
String eventSid = jobject .get ("sid" ).getAsString ();
240
240
241
- if (!(type .startsWith (eventSid ) && type .contains ("." ) ))
241
+ if (!(type .startsWith (eventSid ) && type .contains ("." )))
242
242
return ;
243
243
244
244
String subType = type .split ("\\ ." )[1 ];
245
- switch (subType )
246
- {
245
+ switch (subType ) {
247
246
case "temperature" :
248
- if ( isTemperatureEvent (jobject ) ) {
247
+ if ( isTemperatureEvent (jobject )) {
249
248
logger .debug ("Processing temperature event" );
250
249
processTemperatureEvent (itemName , jobject );
251
250
}
252
251
break ;
253
252
case "humidity" :
254
- if (isHumidityEvent (jobject )) {
253
+ if (isHumidityEvent (jobject )) {
255
254
logger .debug ("Processing humidity event" );
256
255
processHumidityEvent (itemName , jobject );
257
256
}
@@ -265,47 +264,59 @@ private void processEvent(XiaomiGatewayBindingProvider provider, String itemName
265
264
processColorEvent (itemName , jobject );
266
265
break ;
267
266
case "virtual_switch" :
268
- if ( isButtonEvent (jobject , "click" ) ) {
267
+ if ( isButtonEvent (jobject , "click" )) {
269
268
logger .debug ("Processing virtual switch click event" );
270
269
processVirtualSwitchEvent (itemName );
271
270
}
272
271
break ;
273
272
case "click" :
274
- if (isButtonEvent (jobject , "click" )) {
273
+ if (isButtonEvent (jobject , "click" )) {
275
274
logger .debug ("Processing click event" );
276
275
eventPublisher .sendCommand (itemName , OnOffType .ON );
277
276
}
278
277
break ;
279
278
case "double_click" :
280
- if (isButtonEvent (jobject , "double_click" ) ) {
279
+ if (isButtonEvent (jobject , "double_click" )) {
281
280
logger .debug ("Processing double click event" );
282
281
eventPublisher .sendCommand (itemName , OnOffType .ON );
283
282
}
284
283
break ;
285
284
case "long_click" :
286
- if ( isButtonEvent (jobject , "long_click_press" ) ) {
285
+ if ( isButtonEvent (jobject , "long_click_press" )) {
287
286
logger .debug ("Processing long click event" );
288
287
eventPublisher .sendCommand (itemName , OnOffType .ON );
289
288
}
290
289
break ;
291
290
case "long_click_release" :
292
- if ( isButtonEvent (jobject , "long_click_release" )) {
291
+ if ( isButtonEvent (jobject , "long_click_release" )) {
293
292
logger .debug ("Processing long click release event" );
294
293
eventPublisher .sendCommand (itemName , OnOffType .ON );
295
294
}
296
295
break ;
297
296
case "magnet" :
298
- if (isMagnetEvent (jobject )) {
297
+ if (isMagnetEvent (jobject )) {
299
298
logger .debug ("Processing magnet event" );
300
299
processMagnetEvent (itemName , jobject );
301
300
}
302
301
break ;
303
302
case "motion" :
304
- if (isMotionEvent (jobject )) {
303
+ if (isMotionEvent (jobject )) {
305
304
logger .debug ("Processing motion event" );
306
305
processMotionEvent (itemName , jobject );
307
306
}
308
307
break ;
308
+ case "plug" :
309
+ if (isPlugEvent (jobject )) {
310
+ logger .debug ("Processing plug event" );
311
+ processPlugEvent (itemName , jobject );
312
+ }
313
+ break ;
314
+ case "inuse" :
315
+ if (isPlugEvent (jobject )) {
316
+ logger .debug ("Processing plug inuse event" );
317
+ processPlugInuseEvent (itemName , jobject );
318
+ }
319
+ break ;
309
320
default :
310
321
if (isCubeEvent (jobject )) {
311
322
processCubeEvent (itemName , type , jobject );
@@ -424,11 +435,15 @@ private String getStatusEvent(JsonObject jobject) {
424
435
}
425
436
426
437
private boolean isCubeEvent (JsonObject jobject ) {
427
- return jobject != null && ! jobject .isJsonNull ( ) && jobject .get ("model" ).getAsString ().equals ("cube" );
438
+ return jobject != null && jobject .has ( "model" ) && jobject .get ("model" ).getAsString ().equals ("cube" );
428
439
}
429
440
430
441
private boolean isMotionEvent (JsonObject jobject ) {
431
- return jobject != null && !jobject .isJsonNull () && jobject .get ("model" ).getAsString ().equals ("motion" );
442
+ return jobject != null && jobject .has ("model" ) && jobject .get ("model" ).getAsString ().equals ("motion" );
443
+ }
444
+
445
+ private boolean isPlugEvent (JsonObject jobject ) {
446
+ return jobject != null && jobject .has ("model" ) && jobject .get ("model" ).getAsString ().equals ("plug" );
432
447
}
433
448
434
449
private void getGatewayInfo (JsonObject jobject ) {
@@ -464,7 +479,7 @@ private void listDevice(JsonObject jobject) {
464
479
private void processMotionEvent (String itemName , JsonObject jobject ) {
465
480
String data = jobject .get ("data" ).getAsString ();
466
481
JsonObject jo = parser .parse (data ).getAsJsonObject ();
467
- String stat = (jo .get ("status" ) != null ) ? jo .get ("status" ).getAsString ().toLowerCase () : "no_motion" ;
482
+ String stat = (jo .has ("status" )) ? jo .get ("status" ).getAsString ().toLowerCase () : "no_motion" ;
468
483
State oldValue ;
469
484
try {
470
485
oldValue = itemRegistry .getItem (itemName ).getState ();
@@ -476,6 +491,46 @@ private void processMotionEvent(String itemName, JsonObject jobject) {
476
491
}
477
492
}
478
493
494
+ private void processPlugEvent (String itemName , JsonObject jobject ) {
495
+ String data = jobject .get ("data" ).getAsString ();
496
+ JsonObject jo = parser .parse (data ).getAsJsonObject ();
497
+ String stat = (jo .has ("status" )) ? jo .get ("status" ).getAsString ().toLowerCase () : "off" ;
498
+ State oldValue ;
499
+ try {
500
+ oldValue = itemRegistry .getItem (itemName ).getState ();
501
+ State newValue = stat .equals ("on" ) ? OnOffType .ON : OnOffType .OFF ;
502
+ if (!newValue .equals (oldValue ) || newValue .equals (OnOffType .ON ))
503
+ eventPublisher .postUpdate (itemName , newValue );
504
+ } catch (ItemNotFoundException e ) {
505
+ logger .error (e .toString ());
506
+ }
507
+ }
508
+
509
+ private void processPlugInuseEvent (String itemName , JsonObject jobject ) {
510
+ String data = jobject .get ("data" ).getAsString ();
511
+ JsonObject jo = parser .parse (data ).getAsJsonObject ();
512
+ State newValue ;
513
+ if (jo .has ("inuse" ) ) {
514
+ newValue = (jo .get ("inuse" ).getAsString ().equals ("1" )) ? OnOffType .ON : OnOffType .OFF ;
515
+ } else {
516
+ if (jo .has ("status" ) && jo .get ("status" ).getAsString ().toLowerCase ().equals ("off" )) {
517
+ //If power is off, in use is off too
518
+ newValue = OnOffType .OFF ;
519
+ }
520
+ else
521
+ return ;
522
+ }
523
+
524
+ State oldValue ;
525
+ try {
526
+ oldValue = itemRegistry .getItem (itemName ).getState ();
527
+ if (!newValue .equals (oldValue ) || newValue .equals (OnOffType .ON ))
528
+ eventPublisher .postUpdate (itemName , newValue );
529
+ } catch (ItemNotFoundException e ) {
530
+ logger .error (e .toString ());
531
+ }
532
+ }
533
+
479
534
private void processVirtualSwitchEvent (String itemName ) {
480
535
State oldValue ;
481
536
Command command = OnOffType .OFF ;
@@ -557,7 +612,7 @@ private boolean isHumidityEvent(JsonObject jobject) {
557
612
}
558
613
559
614
private Float formatValue (String value ) {
560
- return Float .parseFloat (value .substring (0 , value .length () - 2 ) + "." + value .substring (2 ));
615
+ return Float .parseFloat (value .substring (0 , value .length () - 2 ) + "." + value .substring (2 ));
561
616
}
562
617
563
618
/**
@@ -740,8 +795,8 @@ protected void internalReceiveCommand(String itemName, Command command) {
740
795
logger .error ("Only OnOff/HSB/Percent command types currently supported" );
741
796
return ;
742
797
}
743
- if (!(itemType .contains ("channel" ) || itemType .endsWith (".color" ) || itemType .endsWith (".brightness" ))) {
744
- //only channel items
798
+ if (!(itemType .contains ("channel" ) || itemType .endsWith (".color" ) || itemType .endsWith (".brightness" ) || itemType . endsWith ( ".plug" ) )) {
799
+ //only channel/plug items
745
800
return ;
746
801
}
747
802
@@ -765,7 +820,10 @@ protected void internalReceiveCommand(String itemName, Command command) {
765
820
return ;
766
821
}
767
822
768
- if (itemType .endsWith (".channel_0" ) || itemType .endsWith (".channel_1" )) {
823
+ if (itemType .endsWith (".plug" )) {
824
+ String sid = getItemSid (itemType );
825
+ requestWrite (sid , new String []{"status" }, new Object []{command .toString ().toLowerCase ()});
826
+ } else if (itemType .endsWith (".channel_0" ) || itemType .endsWith (".channel_1" )) {
769
827
//86ctrl_neutral1/2
770
828
String sid = getItemSid (itemType );
771
829
String channel = getItemChannel (itemType );
0 commit comments