@@ -363,6 +363,8 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
363
363
sprintf (szSValue, " %d;%d" , Sat, Hue);
364
364
unsigned char unitcode = 1 ;
365
365
int cmd = (bIsOn ? Limitless_LedOn : Limitless_LedOff);
366
+ int nvalue = 0 ;
367
+ bool tIsOn = false ;
366
368
367
369
// Get current nValue if exist
368
370
std::vector<std::vector<std::string> > result;
@@ -372,23 +374,36 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
372
374
if (!result.empty ())
373
375
{
374
376
// Already in the system
375
- // Update state for sValue, use Limitless_SetRGBColour ?
376
- int nvalue = atoi (result[0 ][0 ].c_str ());
377
- bool tIsOn = (nvalue != 0 );
378
- m_sql.safe_query (" UPDATE DeviceStatus SET sValue='%q', LastLevel = %d WHERE(HardwareID == %d) AND (DeviceID == '%q')" ,
379
- szSValue, BrightnessLevel, m_HwdID, szID);
380
- if (bIsOn == tIsOn) // Check if the light was switched
381
- return ;
377
+ // Update state
378
+ nvalue = atoi (result[0 ][0 ].c_str ());
379
+ tIsOn = (nvalue != 0 );
382
380
}
383
-
384
- // Send as LimitlessLight
385
- _tLimitlessLights lcmd;
386
- lcmd.id = NodeID;
387
- lcmd.command = cmd;
388
- lcmd.value = 0 ;
389
- m_mainworker.PushAndWaitRxMessage (this , (const unsigned char *)&lcmd, Name.c_str (), 255 );
390
381
391
- if (result.empty ())
382
+ if (bIsOn != tIsOn) // light was switched, send on or off
383
+ {
384
+ // Send as LimitlessLight
385
+ _tLimitlessLights lcmd;
386
+ lcmd.id = NodeID;
387
+ lcmd.command = cmd;
388
+ lcmd.value = BrightnessLevel;
389
+ m_mainworker.PushAndWaitRxMessage (this , (const unsigned char *)&lcmd, Name.c_str (), 255 );
390
+ }
391
+
392
+ if (bIsOn && (BrightnessLevel != 100 ))
393
+ cmd = Limitless_SetBrightnessLevel;
394
+
395
+ if (!result.empty ())
396
+ {
397
+ time_t now = time (0 );
398
+ struct tm ltime;
399
+ localtime_r (&now, <ime);
400
+
401
+ char szLastUpdate[40 ];
402
+ sprintf (szLastUpdate, " %04d-%02d-%02d %02d:%02d:%02d" , ltime.tm_year + 1900 , ltime.tm_mon + 1 , ltime.tm_mday , ltime.tm_hour , ltime.tm_min , ltime.tm_sec );
403
+ m_sql.safe_query (" UPDATE DeviceStatus SET nValue=%d, sValue='%q', LastLevel = %d, LastUpdate='%q' WHERE(HardwareID == %d) AND (DeviceID == '%q')" ,
404
+ int (cmd), szSValue, BrightnessLevel, szLastUpdate, m_HwdID, szID);
405
+ }
406
+ else
392
407
{
393
408
// Set Name/Parameters
394
409
m_sql.safe_query (" UPDATE DeviceStatus SET Name='%q', SwitchType=%d, nValue=%d, sValue='%q', LastLevel=%d WHERE(HardwareID == %d) AND (DeviceID == '%q')" ,
@@ -411,7 +426,7 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
411
426
// Already in the system
412
427
int nvalue = atoi (result[0 ][0 ].c_str ());
413
428
bool tIsOn = (nvalue != 0 );
414
- if (bIsOn == tIsOn) // Check if the light was switched
429
+ if (bIsOn == tIsOn) // Check if the scene was switched
415
430
return ;
416
431
}
417
432
// Send as LimitlessLight
@@ -435,6 +450,8 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
435
450
unsigned char unitcode = 1 ;
436
451
int cmd = (bIsOn ? light2_sOn : light2_sOff);
437
452
int level = 0 ;
453
+ int nvalue = 0 ;
454
+ bool tIsOn = false ;
438
455
439
456
if (LType == HLTYPE_NORMAL)
440
457
bIsOn ? level = 15 : level = 0 ;
@@ -444,6 +461,8 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
444
461
level = round (flevel);
445
462
if (level > 15 )
446
463
level = 15 ;
464
+ if (level == 0 )
465
+ level += 1 ; // If brightnesslevel < 6, level = 0 even if light is on
447
466
}
448
467
char szLevel[20 ];
449
468
sprintf (szLevel, " %d" , level);
@@ -453,38 +472,50 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
453
472
result = m_sql.safe_query (" SELECT nValue FROM DeviceStatus WHERE (HardwareID==%d) AND (Unit==%d) AND (Type==%d) AND (SubType==%d) AND (DeviceID=='%q')" ,
454
473
m_HwdID, int (unitcode), pTypeLighting2, sTypeAC , szID);
455
474
// _log.Log(LOG_STATUS, "HueBridge state change: Bri = %d, Level = %d", BrightnessLevel, level);
475
+
456
476
if (!result.empty ())
457
477
{
458
478
// Already in the system
459
479
// Update state
460
- int nvalue = atoi (result[0 ][0 ].c_str ());
461
- bool tIsOn = (nvalue != 0 );
462
- m_sql.safe_query (" UPDATE DeviceStatus SET LastLevel=%d, sValue='%q' WHERE (HardwareID==%d) AND (DeviceID=='%q')" ,
463
- BrightnessLevel, szLevel, m_HwdID, szID);
464
- if (bIsOn == tIsOn) // Check if the light was switched
465
- return ;
480
+ nvalue = atoi (result[0 ][0 ].c_str ());
481
+ tIsOn = (nvalue != 0 );
466
482
}
467
-
468
- tRBUF lcmd;
469
- memset (&lcmd, 0 , sizeof (RBUF));
470
- lcmd.LIGHTING2 .packetlength = sizeof (lcmd.LIGHTING2 ) - 1 ;
471
- lcmd.LIGHTING2 .packettype = pTypeLighting2;
472
- lcmd.LIGHTING2 .subtype = sTypeAC ;
473
- lcmd.LIGHTING2 .seqnbr = 1 ;
474
- lcmd.LIGHTING2 .id1 = 0 ;
475
- lcmd.LIGHTING2 .id2 = 0 ;
476
- lcmd.LIGHTING2 .id3 = 0 ;
477
- lcmd.LIGHTING2 .id4 = NodeID;
478
- lcmd.LIGHTING2 .unitcode = unitcode;
479
- lcmd.LIGHTING2 .cmnd = cmd;
480
- lcmd.LIGHTING2 .level = level;
481
- lcmd.LIGHTING2 .filler = 0 ;
482
- lcmd.LIGHTING2 .rssi = 12 ;
483
-
484
- m_mainworker.PushAndWaitRxMessage (this , (const unsigned char *)&lcmd.LIGHTING2 , Name.c_str (), 255 );
485
483
486
-
487
- if (result.empty ())
484
+ if (bIsOn != tIsOn) // light was switched, send on or off
485
+ {
486
+ tRBUF lcmd;
487
+ memset (&lcmd, 0 , sizeof (RBUF));
488
+ lcmd.LIGHTING2 .packetlength = sizeof (lcmd.LIGHTING2 ) - 1 ;
489
+ lcmd.LIGHTING2 .packettype = pTypeLighting2;
490
+ lcmd.LIGHTING2 .subtype = sTypeAC ;
491
+ lcmd.LIGHTING2 .seqnbr = 1 ;
492
+ lcmd.LIGHTING2 .id1 = 0 ;
493
+ lcmd.LIGHTING2 .id2 = 0 ;
494
+ lcmd.LIGHTING2 .id3 = 0 ;
495
+ lcmd.LIGHTING2 .id4 = NodeID;
496
+ lcmd.LIGHTING2 .unitcode = unitcode;
497
+ lcmd.LIGHTING2 .cmnd = cmd;
498
+ lcmd.LIGHTING2 .level = level;
499
+ lcmd.LIGHTING2 .filler = 0 ;
500
+ lcmd.LIGHTING2 .rssi = 12 ;
501
+ m_mainworker.PushAndWaitRxMessage (this , (const unsigned char *)&lcmd.LIGHTING2 , Name.c_str (), 255 );
502
+ }
503
+
504
+ if (bIsOn && (level != 15 ))
505
+ cmd = light2_sSetLevel;
506
+
507
+ if (!result.empty ())
508
+ {
509
+ time_t now = time (0 );
510
+ struct tm ltime;
511
+ localtime_r (&now, <ime);
512
+
513
+ char szLastUpdate[40 ];
514
+ sprintf (szLastUpdate, " %04d-%02d-%02d %02d:%02d:%02d" , ltime.tm_year + 1900 , ltime.tm_mon + 1 , ltime.tm_mday , ltime.tm_hour , ltime.tm_min , ltime.tm_sec );
515
+ m_sql.safe_query (" UPDATE DeviceStatus SET LastLevel=%d, nValue=%d, sValue='%q', LastUpdate='%q' WHERE (HardwareID==%d) AND (DeviceID=='%q')" ,
516
+ BrightnessLevel, int (cmd), szLevel, szLastUpdate, m_HwdID, szID);
517
+ }
518
+ else
488
519
{
489
520
// Set Name/Parameters
490
521
m_sql.safe_query (" UPDATE DeviceStatus SET Name='%q', SwitchType=%d, LastLevel=%d, nValue=%d, sValue='%q' WHERE (HardwareID==%d) AND (DeviceID=='%q')" ,
0 commit comments