Skip to content

Commit 2e77df9

Browse files
committed
2 parents 64c3e23 + 4b7836c commit 2e77df9

File tree

2 files changed

+74
-43
lines changed

2 files changed

+74
-43
lines changed

hardware/PhilipsHue.cpp

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
363363
sprintf(szSValue, "%d;%d", Sat, Hue);
364364
unsigned char unitcode = 1;
365365
int cmd = (bIsOn ? Limitless_LedOn : Limitless_LedOff);
366+
int nvalue = 0;
367+
bool tIsOn = false;
366368

367369
//Get current nValue if exist
368370
std::vector<std::vector<std::string> > result;
@@ -372,23 +374,36 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
372374
if (!result.empty())
373375
{
374376
//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);
382380
}
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);
390381

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, &ltime);
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
392407
{
393408
//Set Name/Parameters
394409
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
411426
//Already in the system
412427
int nvalue = atoi(result[0][0].c_str());
413428
bool tIsOn = (nvalue != 0);
414-
if (bIsOn == tIsOn) //Check if the light was switched
429+
if (bIsOn == tIsOn) //Check if the scene was switched
415430
return;
416431
}
417432
//Send as LimitlessLight
@@ -435,6 +450,8 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
435450
unsigned char unitcode = 1;
436451
int cmd = (bIsOn ? light2_sOn : light2_sOff);
437452
int level = 0;
453+
int nvalue = 0;
454+
bool tIsOn = false;
438455

439456
if (LType == HLTYPE_NORMAL)
440457
bIsOn ? level = 15 : level = 0;
@@ -444,6 +461,8 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
444461
level = round(flevel);
445462
if (level > 15)
446463
level = 15;
464+
if (level == 0)
465+
level += 1; //If brightnesslevel < 6, level = 0 even if light is on
447466
}
448467
char szLevel[20];
449468
sprintf(szLevel, "%d", level);
@@ -453,38 +472,50 @@ void CPhilipsHue::InsertUpdateSwitch(const int NodeID, const _eHueLightType LTyp
453472
result = m_sql.safe_query("SELECT nValue FROM DeviceStatus WHERE (HardwareID==%d) AND (Unit==%d) AND (Type==%d) AND (SubType==%d) AND (DeviceID=='%q')",
454473
m_HwdID, int(unitcode), pTypeLighting2, sTypeAC, szID);
455474
//_log.Log(LOG_STATUS, "HueBridge state change: Bri = %d, Level = %d", BrightnessLevel, level);
475+
456476
if (!result.empty())
457477
{
458478
//Already in the system
459479
//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);
466482
}
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);
485483

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, &ltime);
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
488519
{
489520
//Set Name/Parameters
490521
m_sql.safe_query("UPDATE DeviceStatus SET Name='%q', SwitchType=%d, LastLevel=%d, nValue=%d, sValue='%q' WHERE (HardwareID==%d) AND (DeviceID=='%q')",

updaterelease

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ then
1313
MACH="armv7l"
1414
fi
1515

16-
echo "Updating to latest beta version..."
16+
echo "Updating to latest release version..."
1717
echo "Please Standby..."
1818
wget -O domoticz_release.tgz "http://www.domoticz.com/download.php?channel=release&type=release&system=${OS}&machine=${MACH}"
1919
tar xvfz domoticz_release.tgz

0 commit comments

Comments
 (0)