Skip to content

Commit 76090a8

Browse files
committed
Better Nest Thermostat implementation (fix for multiple thermostats for multiple locations)
1 parent a55ce30 commit 76090a8

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

hardware/Nest.cpp

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ void CNest::GetMeterDetails()
348348
{
349349
std::string sResult;
350350
#ifdef DEBUG_NextThermostatR
351-
sResult = ReadFile("E:\\nest.json");
351+
sResult = ReadFile("E:\\Nest_DoubleTherm.json");
352352
#else
353353
if (m_UserName.size()==0)
354354
return;
@@ -528,29 +528,64 @@ void CNest::GetMeterDetails()
528528
return;
529529
}
530530

531-
Json::Value::Members members;
532-
533531
size_t iThermostat = 0;
534-
for (Json::Value::iterator itShared = root["shared"].begin(); itShared != root["shared"].end(); ++itShared)
532+
for (Json::Value::iterator ittStructure = root["structure"].begin(); ittStructure != root["structure"].end(); ++ittStructure)
535533
{
536-
Json::Value nshared = *itShared;
537-
if (nshared.isObject())
534+
Json::Value nstructure = *ittStructure;
535+
if (!nstructure.isObject())
536+
continue;
537+
std::string StructureID = ittStructure.key().asString();
538+
std::string StructureName = nstructure["name"].asString();
539+
540+
for (Json::Value::iterator ittDevice = nstructure["devices"].begin(); ittDevice != nstructure["devices"].end(); ++ittDevice)
538541
{
539-
std::string Serial = itShared.key().asString();
540-
members = root["structure"].getMemberNames();
541-
if (iThermostat>=members.size())
542+
std::string devID = (*ittDevice).asString();
543+
if (devID.find("device.")==std::string::npos)
544+
continue;
545+
std::string Serial = devID.substr(7);
546+
if (root["device"].empty())
542547
continue;
543-
std::string StructureID = *(members.begin()+iThermostat);
544-
if (root["structure"][StructureID].empty())
548+
if (root["device"][Serial].empty())
549+
continue; //not found !?
550+
if (root["shared"][Serial].empty())
551+
continue; //Nothing shared?
552+
553+
554+
Json::Value ndevice = root["device"][Serial];
555+
if (!ndevice.isObject())
545556
continue;
546-
std::string Name = root["structure"][StructureID]["name"].asString();
557+
558+
std::string Name = "Thermostat";
559+
if (!ndevice["where_id"].empty())
560+
{
561+
//Lookup our 'where' (for the Name of the thermostat)
562+
std::string where_id = ndevice["where_id"].asString();
563+
564+
if (!root["where"].empty())
565+
{
566+
if (!root["where"][StructureID].empty())
567+
{
568+
for (Json::Value::iterator ittWheres = root["where"][StructureID]["wheres"].begin(); ittWheres != root["where"][StructureID]["wheres"].end(); ++ittWheres)
569+
{
570+
Json::Value nwheres = *ittWheres;
571+
if (nwheres["where_id"] == where_id)
572+
{
573+
Name = StructureName + " " + nwheres["name"].asString();
574+
break;
575+
}
576+
}
577+
}
578+
}
579+
}
547580

548581
_tNestThemostat ntherm;
549582
ntherm.Serial = Serial;
550583
ntherm.StructureID = StructureID;
551584
ntherm.Name = Name;
552585
m_thermostats[iThermostat] = ntherm;
553586

587+
Json::Value nshared = root["shared"][Serial];
588+
554589
//Setpoint
555590
if (!nshared["target_temperature"].empty())
556591
{
@@ -569,20 +604,20 @@ void CNest::GetMeterDetails()
569604
if (nshared["can_heat"].asBool() && !nshared["hvac_heater_state"].empty())
570605
{
571606
bool bIsHeating = nshared["hvac_heater_state"].asBool();
572-
UpdateSwitch(113,bIsHeating,"HeatingOn");
607+
UpdateSwitch(113, bIsHeating, "HeatingOn");
573608
}
574609

575610
// Check if thermostat is currently Cooling
576611
if (nshared["can_cool"].asBool() && !nshared["hvac_ac_state"].empty())
577612
{
578613
bool bIsCooling = nshared["hvac_ac_state"].asBool();
579-
UpdateSwitch(114,bIsCooling,"CoolingOn");
614+
UpdateSwitch(114, bIsCooling, "CoolingOn");
580615
}
581616

582617
//Away
583-
if (!root["structure"][StructureID]["away"].empty())
618+
if (!nstructure["away"].empty())
584619
{
585-
bool bIsAway = root["structure"][StructureID]["away"].asBool();
620+
bool bIsAway = nstructure["away"].asBool();
586621
SendSwitch((iThermostat * 3) + 3, 1, 255, bIsAway, 0, Name + " Away");
587622
}
588623
iThermostat++;

main/SQLHelper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,11 @@ bool CSQLHelper::OpenDatabase()
17511751
}
17521752
if (dbversion < 96)
17531753
{
1754-
query("ALTER TABLE MobileDevices ADD COLUMN [Name] VARCHAR(100) DEFAULT ''");
1754+
if (!DoesColumnExistsInTable("Name", "MobileDevices"))
1755+
{
1756+
query("ALTER TABLE MobileDevices ADD COLUMN [Name] VARCHAR(100) DEFAULT ''");
1757+
}
1758+
17551759
}
17561760
}
17571761
else if (bNewInstall)

0 commit comments

Comments
 (0)