24
24
#include " ../json/json.h"
25
25
#include < string>
26
26
27
+
28
+ #define LOGONFAILTRESHOLD 3
29
+ #define MAXPOLINTERVAL 3600
30
+
27
31
extern std::string szUserDataFolder;
28
32
29
33
const uint8_t CEvohomeWeb::m_dczToEvoWebAPIMode[7 ]={0 ,2 ,3 ,4 ,6 ,1 ,5 };
@@ -44,6 +48,7 @@ CEvohomeWeb::CEvohomeWeb(const int ID, const std::string &Username, const std::s
44
48
m_loggedon=false ;
45
49
m_tzoffset=-1 ;
46
50
m_lastDST=-1 ;
51
+ m_logonfailures=0 ;
47
52
48
53
Init ();
49
54
}
@@ -67,16 +72,25 @@ void CEvohomeWeb::Init()
67
72
68
73
bool CEvohomeWeb::StartSession ()
69
74
{
75
+ _log.Log (LOG_NORM, " EvohomeWeb: connect to Evohome server" );
70
76
m_loggedon=false ;
71
77
if (!login (m_username,m_password))
78
+ {
79
+ m_logonfailures++;
80
+ if (m_logonfailures > LOGONFAILTRESHOLD)
81
+ _log.Log (LOG_STATUS, " EvohomeWeb: logon fail treshold reached - trottling" );
82
+ if ((m_logonfailures * m_refreshrate) > MAXPOLINTERVAL)
83
+ m_logonfailures--;
72
84
return false ;
85
+ }
73
86
full_installation ();
74
87
m_tcs = NULL ;
75
88
if (is_single_heating_system ())
76
89
m_tcs = &locations[0 ].gateways [0 ].temperatureControlSystems [0 ];
77
90
78
91
m_zones[0 ] = 0 ;
79
92
m_loggedon=true ;
93
+ m_logonfailures=0 ;
80
94
return true ;
81
95
}
82
96
@@ -110,6 +124,7 @@ bool CEvohomeWeb::StopHardware()
110
124
void CEvohomeWeb::Do_Work ()
111
125
{
112
126
int sec_counter = m_refreshrate - 10 ;
127
+ int pollcounter = LOGONFAILTRESHOLD;
113
128
_log.Log (LOG_STATUS, " EvohomeWeb: Worker started..." );
114
129
while (!m_stoprequested)
115
130
{
@@ -118,9 +133,10 @@ void CEvohomeWeb::Do_Work()
118
133
if (sec_counter % 10 == 0 ) {
119
134
m_LastHeartbeat=mytime (NULL );
120
135
}
121
- if (sec_counter % m_refreshrate == 0 )
136
+ if ( ( sec_counter % m_refreshrate == 0 ) && (pollcounter++ > m_logonfailures) )
122
137
{
123
- GetStatus ();
138
+ if (GetStatus ())
139
+ int pollcounter = LOGONFAILTRESHOLD;
124
140
}
125
141
}
126
142
_log.Log (LOG_STATUS," EvohomeWeb: Worker stopped..." );
@@ -157,16 +173,14 @@ bool CEvohomeWeb::WriteToHardware(const char *pdata, const unsigned char length)
157
173
}
158
174
159
175
160
- void CEvohomeWeb::GetStatus ()
176
+ bool CEvohomeWeb::GetStatus ()
161
177
{
162
178
if (!m_loggedon && !StartSession ())
163
- return ;
179
+ return false ;
164
180
if (!get_status (m_tcs->locationId ))
165
181
{
166
- // FIXME: should I distinguish between simple HTTP errors and session related errors?
167
- _log.Log (LOG_ERROR," Evohome: failed to retrieve status" );
168
182
m_loggedon = false ;
169
- return ;
183
+ return false ;
170
184
}
171
185
172
186
// system status
@@ -179,17 +193,19 @@ void CEvohomeWeb::GetStatus()
179
193
// hot water status
180
194
if (has_dhw (m_tcs))
181
195
DecodeDHWState (m_tcs);
196
+
197
+ return true ;
182
198
}
183
199
184
200
185
201
bool CEvohomeWeb::SetSystemMode (uint8_t sysmode)
186
202
{
187
203
if (set_system_mode (m_tcs->systemId , (int )(m_dczToEvoWebAPIMode[sysmode])))
188
204
{
189
- _log.Log (LOG_STATUS," Evohome : changed system status to %s" ,GetControllerModeName (sysmode));
205
+ _log.Log (LOG_STATUS," EvohomeWeb : changed system status to %s" ,GetControllerModeName (sysmode));
190
206
return true ;
191
207
}
192
- _log.Log (LOG_ERROR," Evohome : error changing system status" );
208
+ _log.Log (LOG_ERROR," EvohomeWeb : error changing system status" );
193
209
m_loggedon = false ;
194
210
return false ;
195
211
}
@@ -206,7 +222,7 @@ bool CEvohomeWeb::SetSetpoint(const char *pdata)
206
222
zone* hz=get_zone_by_ID (zoneId);
207
223
if (hz == NULL ) // zone number not known by installation (manually added?)
208
224
{
209
- _log.Log (LOG_ERROR," Evohome : attempt to change setpoint on unknown zone" );
225
+ _log.Log (LOG_ERROR," EvohomeWeb : attempt to change setpoint on unknown zone" );
210
226
return false ;
211
227
}
212
228
@@ -257,7 +273,7 @@ bool CEvohomeWeb::SetDHWState(const char *pdata)
257
273
{
258
274
if (!has_dhw (m_tcs)) // Installation has no Hot Water device
259
275
{
260
- _log.Log (LOG_ERROR," Evohome : attempt to set state on non existing Hot Water device" );
276
+ _log.Log (LOG_ERROR," EvohomeWeb : attempt to set state on non existing Hot Water device" );
261
277
return false ;
262
278
}
263
279
@@ -363,7 +379,7 @@ void CEvohomeWeb::DecodeZone(zone* hz)
363
379
{
364
380
m_sql.safe_query (" UPDATE DeviceStatus SET Name='%q' WHERE (ID == %" PRIu64 " )" , zonedata[" name" ].c_str (), DevRowIdx);
365
381
if (sdevname.find (" zone " )!=std::string::npos)
366
- _log.Log (LOG_STATUS," Evohome : register new zone '%c'" , zonedata[" name" ].c_str ());
382
+ _log.Log (LOG_STATUS," EvohomeWeb : register new zone '%c'" , zonedata[" name" ].c_str ());
367
383
}
368
384
}
369
385
@@ -470,7 +486,7 @@ uint8_t CEvohomeWeb::GetUnit_by_ID(unsigned long evoID)
470
486
return row;
471
487
}
472
488
}
473
- _log.Log (LOG_ERROR," Evohome : cannot add new zone because you have no free zones left" );
489
+ _log.Log (LOG_ERROR," EvohomeWeb : cannot add new zone because you have no free zones left" );
474
490
}
475
491
return -1 ;
476
492
}
@@ -512,7 +528,7 @@ std::string CEvohomeWeb::local_to_utc(std::string local_time)
512
528
513
529
514
530
/*
515
- * Evohome client API
531
+ * Evohome client API starts here
516
532
*/
517
533
518
534
/*
@@ -528,8 +544,8 @@ std::string CEvohomeWeb::local_to_utc(std::string local_time)
528
544
#define EVOHOME_HOST " https://tccna.honeywell.com"
529
545
530
546
531
- const std::string weekdays[7 ] = {" Sunday" , " Monday" , " Tuesday" , " Wednesday" , " Thursday" , " Friday" , " Saturday" };
532
- // const std::string evo_modes[7] = {"Auto", "HeatingOff", "AutoWithEco", "Away", "DayOff", "", "Custom"};
547
+ const std::string CEvohomeWeb:: weekdays[7 ] = {" Sunday" , " Monday" , " Tuesday" , " Wednesday" , " Thursday" , " Friday" , " Saturday" };
548
+ // const std::string CEvohomeWeb:: evo_modes[7] = {"Auto", "HeatingOff", "AutoWithEco", "Away", "DayOff", "", "Custom"};
533
549
534
550
535
551
/* ***********************************************************************
@@ -556,7 +572,7 @@ bool CEvohomeWeb::login(std::string user, std::string password)
556
572
std::string s_res;
557
573
if (!HTTPClient::POST (EVOHOME_HOST" /Auth/OAuth/Token" , pdata.str (), LoginHeaders, s_res))
558
574
{
559
- _log.Log (LOG_ERROR," Evohome : HTTP client error at login!" );
575
+ _log.Log (LOG_ERROR," EvohomeWeb : HTTP client error at login!" );
560
576
return false ;
561
577
}
562
578
@@ -573,15 +589,15 @@ bool CEvohomeWeb::login(std::string user, std::string password)
573
589
i++;
574
590
c = html[i];
575
591
}
576
- _log.Log (LOG_ERROR," Evohome : login failed with message: %s" , edata.str ().c_str ());
592
+ _log.Log (LOG_ERROR," EvohomeWeb : login failed with message: %s" , edata.str ().c_str ());
577
593
return false ;
578
594
}
579
595
580
596
json_object *j_ret = json_tokener_parse (s_res.c_str ());
581
597
json_object *j_msg;
582
598
if ( (json_object_object_get_ex (j_ret, " error" , &j_msg)) || (json_object_object_get_ex (j_ret, " message" , &j_msg)) )
583
599
{
584
- _log.Log (LOG_ERROR," Evohome : login failed with message: %s" , json_object_get_string (j_msg));
600
+ _log.Log (LOG_ERROR," EvohomeWeb : login failed with message: %s" , json_object_get_string (j_msg));
585
601
return false ;
586
602
}
587
603
@@ -612,7 +628,7 @@ bool CEvohomeWeb::user_account()
612
628
std::string s_res;
613
629
if (!HTTPClient::GET (EVOHOME_HOST" /WebAPI/emea/api/v1/userAccount" , SessionHeaders, s_res))
614
630
{
615
- _log.Log (LOG_ERROR," Evohome : HTTP client error at retrieve user account info!" );
631
+ _log.Log (LOG_ERROR," EvohomeWeb : HTTP client error at retrieve user account info!" );
616
632
return false ;
617
633
}
618
634
json_object *j_ret = json_tokener_parse (s_res.c_str ());
@@ -642,17 +658,15 @@ void CEvohomeWeb::get_zones(int location, int gateway, int temperatureControlSys
642
658
for (i=0 ; i<l; i++)
643
659
{
644
660
locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].installationInfo = json_object_array_get_idx (j_list, i);
645
-
646
- json_object *j_zoneId;
647
- json_object_object_get_ex (locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].installationInfo , " zoneId" , &j_zoneId);
648
- locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].zoneId = json_object_get_string (j_zoneId);
661
+ locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].zoneId = json_get_val (locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].installationInfo ," zoneId" );
649
662
locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].systemId = locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].systemId ;
650
663
locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].gatewayId = locations[location].gateways [gateway].gatewayId ;
651
664
locations[location].gateways [gateway].temperatureControlSystems [temperatureControlSystem].zones [i].locationId = locations[location].locationId ;
652
665
}
653
666
}
654
667
}
655
668
669
+
656
670
void CEvohomeWeb::get_temperatureControlSystems (int location, int gateway)
657
671
{
658
672
locations[location].gateways [gateway].temperatureControlSystems .clear ();
@@ -666,13 +680,9 @@ void CEvohomeWeb::get_temperatureControlSystems(int location, int gateway)
666
680
for (i = 0 ; i < l; i++)
667
681
{
668
682
locations[location].gateways [gateway].temperatureControlSystems [i].installationInfo = json_object_array_get_idx (j_list, i);
669
-
670
- json_object *j_tcsId;
671
- json_object_object_get_ex (locations[location].gateways [gateway].temperatureControlSystems [i].installationInfo , " systemId" , &j_tcsId);
672
- locations[location].gateways [gateway].temperatureControlSystems [i].systemId = json_object_get_string (j_tcsId);
683
+ locations[location].gateways [gateway].temperatureControlSystems [i].systemId = json_get_val (locations[location].gateways [gateway].temperatureControlSystems [i].installationInfo , " systemId" );
673
684
locations[location].gateways [gateway].temperatureControlSystems [i].gatewayId = locations[location].gateways [gateway].gatewayId ;
674
685
locations[location].gateways [gateway].temperatureControlSystems [i].locationId = locations[location].locationId ;
675
-
676
686
get_zones (location, gateway, i);
677
687
}
678
688
}
@@ -691,13 +701,8 @@ void CEvohomeWeb::get_gateways(int location)
691
701
for (i = 0 ; i < l; i++)
692
702
{
693
703
locations[location].gateways [i].installationInfo = json_object_array_get_idx (j_list, i);
694
-
695
- json_object *j_gwInfo, *j_gwId;
696
- json_object_object_get_ex (locations[location].gateways [i].installationInfo , " gatewayInfo" , &j_gwInfo);
697
- json_object_object_get_ex (j_gwInfo, " gatewayId" , &j_gwId);
698
- locations[location].gateways [i].gatewayId = json_object_get_string (j_gwId);
704
+ locations[location].gateways [i].gatewayId = json_get_val (locations[location].gateways [i].installationInfo , " gatewayInfo" , " gatewayId" );
699
705
locations[location].gateways [i].locationId = locations[location].locationId ;
700
-
701
706
get_temperatureControlSystems (location,i);
702
707
}
703
708
}
@@ -713,7 +718,7 @@ bool CEvohomeWeb::full_installation()
713
718
std::string s_res;
714
719
if (!HTTPClient::GET (url.str (), SessionHeaders, s_res))
715
720
{
716
- _log.Log (LOG_ERROR," Evohome : HTTP client error at retrieve installation!" );
721
+ _log.Log (LOG_ERROR," EvohomeWeb : HTTP client error at retrieve installation!" );
717
722
return false ;
718
723
}
719
724
@@ -730,12 +735,7 @@ bool CEvohomeWeb::full_installation()
730
735
for (i=0 ; i<l; i++)
731
736
{
732
737
locations[i].installationInfo = json_object_array_get_idx (j_list, i);
733
-
734
- json_object *j_locInfo, *j_locId;
735
- json_object_object_get_ex (locations[i].installationInfo , " locationInfo" , &j_locInfo);
736
- json_object_object_get_ex (j_locInfo, " locationId" , &j_locId);
737
- locations[i].locationId = json_object_get_string (j_locId);
738
-
738
+ locations[i].locationId = json_get_val (locations[i].installationInfo , " locationInfo" , " locationId" );
739
739
get_gateways (i);
740
740
}
741
741
return true ;
@@ -773,7 +773,7 @@ bool CEvohomeWeb::get_status(int location)
773
773
std::string s_res;
774
774
if (!HTTPClient::GET (url.str (), SessionHeaders, s_res))
775
775
{
776
- _log.Log (LOG_ERROR," Evohome : HTTP client error at get status!" );
776
+ _log.Log (LOG_ERROR," EvohomeWeb : HTTP client error at get status!" );
777
777
return false ;
778
778
}
779
779
locations[location].status = json_tokener_parse (s_res.c_str ());
@@ -817,6 +817,8 @@ bool CEvohomeWeb::get_status(int location)
817
817
else
818
818
valid_json = false ;
819
819
820
+ if (!valid_json)
821
+ _log.Log (LOG_ERROR," EvohomeWeb: status request did not return a valid response" );
820
822
return valid_json;
821
823
}
822
824
@@ -946,13 +948,14 @@ std::string CEvohomeWeb::get_next_switchpoint(CEvohomeWeb::temperatureControlSys
946
948
return " " ;
947
949
return get_next_switchpoint (tcs->zones [zone].schedule );
948
950
}
949
-
951
+ /*
950
952
std::string CEvohomeWeb::get_next_switchpoint(std::string zoneId)
951
953
{
952
954
if ( (get_zone_by_ID(zoneId)->schedule == NULL) && !get_schedule(zoneId) )
953
955
return "";
954
956
return get_next_switchpoint(get_zone_by_ID(zoneId)->schedule);
955
957
}
958
+ */
956
959
std::string CEvohomeWeb::get_next_switchpoint (zone* hz)
957
960
{
958
961
if ( (hz->schedule == NULL ) && !get_schedule (hz->zoneId ) )
@@ -961,10 +964,10 @@ std::string CEvohomeWeb::get_next_switchpoint(zone* hz)
961
964
}
962
965
std::string CEvohomeWeb::get_next_switchpoint (json_object *schedule)
963
966
{
964
- std::string current_temperature ;
965
- return get_next_switchpoint_ex (schedule, current_temperature );
967
+ std::string current_setpoint ;
968
+ return get_next_switchpoint_ex (schedule, current_setpoint );
966
969
}
967
- std::string CEvohomeWeb::get_next_switchpoint_ex (json_object *schedule, std::string ¤t_temperature )
970
+ std::string CEvohomeWeb::get_next_switchpoint_ex (json_object *schedule, std::string ¤t_setpoint )
968
971
{
969
972
if (schedule == NULL )
970
973
return " " ;
@@ -1019,7 +1022,7 @@ std::string CEvohomeWeb::get_next_switchpoint_ex(json_object *schedule, std::str
1019
1022
else
1020
1023
{
1021
1024
json_object_object_get_ex (j_sp, " temperature" , &j_temp);
1022
- current_temperature = json_object_get_string (j_temp);
1025
+ current_setpoint = json_object_get_string (j_temp);
1023
1026
}
1024
1027
}
1025
1028
}
@@ -1074,6 +1077,7 @@ std::string CEvohomeWeb::json_get_val(json_object *j_json, const char* key1, con
1074
1077
* *
1075
1078
************************************************************************/
1076
1079
1080
+
1077
1081
bool CEvohomeWeb::verify_date (std::string date)
1078
1082
{
1079
1083
if (date.length () < 10 )
@@ -1185,11 +1189,12 @@ bool CEvohomeWeb::set_temperature(std::string zoneId, std::string temperature, s
1185
1189
return true ;
1186
1190
return false ;
1187
1191
}
1192
+ /*
1188
1193
bool CEvohomeWeb::set_temperature(std::string zoneId, std::string temperature)
1189
1194
{
1190
1195
return set_temperature(zoneId, temperature, "");
1191
1196
}
1192
-
1197
+ */
1193
1198
1194
1199
bool CEvohomeWeb::cancel_temperature_override (std::string zoneId)
1195
1200
{
@@ -1202,12 +1207,13 @@ bool CEvohomeWeb::cancel_temperature_override(std::string zoneId)
1202
1207
return false ;
1203
1208
}
1204
1209
1205
-
1210
+ /*
1206
1211
bool CEvohomeWeb::has_dhw(int location, int gateway, int temperatureControlSystem)
1207
1212
{
1208
1213
CEvohomeWeb::temperatureControlSystem *tcs = &locations[location].gateways[gateway].temperatureControlSystems[temperatureControlSystem];
1209
1214
return has_dhw(tcs);
1210
1215
}
1216
+ */
1211
1217
bool CEvohomeWeb::has_dhw (CEvohomeWeb::temperatureControlSystem *tcs)
1212
1218
{
1213
1219
json_object *j_dhw;
@@ -1253,8 +1259,9 @@ bool CEvohomeWeb::set_dhw_mode(std::string dhwId, std::string mode, std::string
1253
1259
return true ;
1254
1260
return false ;
1255
1261
}
1262
+ /*
1256
1263
bool CEvohomeWeb::set_dhw_mode(std::string systemId, std::string mode)
1257
1264
{
1258
1265
return set_dhw_mode(systemId, mode, "");
1259
1266
}
1260
-
1267
+ */
0 commit comments