Skip to content

Commit 192b265

Browse files
committed
Refactored code and fixed issue on switches
1 parent 235042a commit 192b265

File tree

4 files changed

+69
-92
lines changed

4 files changed

+69
-92
lines changed

push/GooglePubSubPush.cpp

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "stdafx.h"
22
#include "GooglePubSubPush.h"
3-
#include <boost/date_time/c_local_time_adjustor.hpp>
43
#include "../hardware/hardwaretypes.h"
54
#include "../json/json.h"
65
#include "../main/Helper.h"
@@ -30,25 +29,6 @@ extern std::string szUserDataFolder;
3029
// this should be filled in by the preprocessor
3130
extern const char * Python_exe;
3231

33-
typedef struct _STR_TABLE_ID1_ID2 {
34-
unsigned long id1;
35-
unsigned long id2;
36-
const char *str1;
37-
} STR_TABLE_ID1_ID2;
38-
39-
static boost::posix_time::time_duration get_utc_offset() {
40-
using namespace boost::posix_time;
41-
42-
// boost::date_time::c_local_adjustor uses the C-API to adjust a
43-
// moment given in utc to the same moment in the local time zone.
44-
typedef boost::date_time::c_local_adjustor<ptime> local_adj;
45-
46-
const ptime utc_now = second_clock::universal_time();
47-
const ptime now = local_adj::utc_to_local(utc_now);
48-
49-
return now - utc_now;
50-
}
51-
5232
CGooglePubSubPush::CGooglePubSubPush()
5333
{
5434
m_bLinkActive = false;
@@ -151,8 +131,6 @@ void CGooglePubSubPush::DoGooglePubSubPush()
151131
int delpos = atoi(sd[1].c_str());
152132
int dType = atoi(sd[3].c_str());
153133
int dSubType = atoi(sd[4].c_str());
154-
std::string lType = sd[3].c_str();
155-
std::string lSubType = sd[4].c_str();
156134
int nValue = atoi(sd[5].c_str());
157135
std::string sValue = sd[6].c_str();
158136
int targetType = atoi(sd[7].c_str());
@@ -162,16 +140,12 @@ void CGooglePubSubPush::DoGooglePubSubPush()
162140
int includeUnit = atoi(sd[11].c_str());
163141
int metertype = atoi(sd[12].c_str());
164142
int lastUpdate = atoi(sd[13].c_str());
165-
std::string lstatus="";
166-
std::string lunit = "";
167143
std::string ltargetVariable = sd[8].c_str();
168144
std::string ltargetDeviceId = sd[9].c_str();
169145
std::string lname = sd[14].c_str();
170146
sendValue = sValue;
171147

172-
// Compute tz
173-
boost::posix_time::time_duration uoffset = get_utc_offset();
174-
unsigned long tzoffset = (int)((double)(uoffset.ticks() / 3600000000LL) * 3600);
148+
unsigned long tzoffset = get_tzoffset();
175149

176150
#ifdef WIN32
177151
unsigned __int64 localTime = lastUpdate;
@@ -190,18 +164,7 @@ void CGooglePubSubPush::DoGooglePubSubPush()
190164
char szLocalTimeUtcMs[16];
191165
sprintf(szLocalTimeUtcMs, "%llu", localTimeUtc * 1000);
192166

193-
// RFC3339 time format
194-
time_t tmpT = localTimeUtc;
195-
struct tm* timeinfo = gmtime(&tmpT);
196-
197-
char llastUpdate[255];
198-
#if !defined WIN32
199-
snprintf(llastUpdate, sizeof(llastUpdate), "%04d-%02d-%02dT%02d:%02d:%02dZ",
200-
timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
201-
#else
202-
sprintf_s(llastUpdate, sizeof(llastUpdate), "%04d-%02d-%02dT%02d:%02d:%02dZ",
203-
timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
204-
#endif
167+
std::string llastUpdate = get_lastUpdate(localTimeUtc);
205168

206169
// Replace keywords
207170
/*
@@ -220,9 +183,9 @@ void CGooglePubSubPush::DoGooglePubSubPush()
220183
%h : hostname
221184
*/
222185

223-
lunit = getUnit(delpos, metertype);
224-
lType = RFX_Type_Desc(dType,1);
225-
lSubType = RFX_Type_SubType_Desc(dType,dSubType);
186+
std::string lunit = getUnit(delpos, metertype);
187+
std::string lType = RFX_Type_Desc(dType,1);
188+
std::string lSubType = RFX_Type_SubType_Desc(dType,dSubType);
226189

227190
char hostname[256];
228191
gethostname(hostname, sizeof(hostname));
@@ -252,8 +215,8 @@ void CGooglePubSubPush::DoGooglePubSubPush()
252215
replaceAll(googlePubSubData, "%t1", std::string(szLocalTimeMs));
253216
replaceAll(googlePubSubData, "%t2", std::string(szLocalTimeUtc));
254217
replaceAll(googlePubSubData, "%t3", std::string(szLocalTimeUtcMs));
255-
replaceAll(googlePubSubData, "%t4", std::string(llastUpdate));
256-
replaceAll(googlePubSubData, "%n", std::string(lname));
218+
replaceAll(googlePubSubData, "%t4", llastUpdate);
219+
replaceAll(googlePubSubData, "%n", lname);
257220
replaceAll(googlePubSubData, "%T0", lType);
258221
replaceAll(googlePubSubData, "%T1", lSubType);
259222
replaceAll(googlePubSubData, "%h", std::string(hostname));

push/HttpPush.cpp

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "../hardware/hardwaretypes.h"
77
#include "../main/RFXtrx.h"
88
#include "../main/SQLHelper.h"
9-
#include <boost/date_time/c_local_time_adjustor.hpp>
109
#include "../webserver/Base64.h"
1110
#include "../main/localtime_r.h"
1211
#include "../main/WebServer.h"
@@ -18,25 +17,6 @@
1817
#include <unistd.h> //gethostbyname
1918
#endif
2019

21-
typedef struct _STR_TABLE_ID1_ID2 {
22-
unsigned long id1;
23-
unsigned long id2;
24-
const char *str1;
25-
} STR_TABLE_ID1_ID2;
26-
27-
static boost::posix_time::time_duration get_utc_offset() {
28-
using namespace boost::posix_time;
29-
30-
// boost::date_time::c_local_adjustor uses the C-API to adjust a
31-
// moment given in utc to the same moment in the local time zone.
32-
typedef boost::date_time::c_local_adjustor<ptime> local_adj;
33-
34-
const ptime utc_now = second_clock::universal_time();
35-
const ptime now = local_adj::utc_to_local(utc_now);
36-
37-
return now - utc_now;
38-
}
39-
4020
CHttpPush::CHttpPush()
4121
{
4222
m_bLinkActive = false;
@@ -113,8 +93,6 @@ void CHttpPush::DoHttpPush()
11393
int delpos = atoi(sd[1].c_str());
11494
int dType = atoi(sd[3].c_str());
11595
int dSubType = atoi(sd[4].c_str());
116-
std::string lType = sd[3].c_str();
117-
std::string lSubType = sd[4].c_str();
11896
int nValue = atoi(sd[5].c_str());
11997
std::string sValue = sd[6].c_str();
12098
int targetType = atoi(sd[7].c_str());
@@ -124,16 +102,12 @@ void CHttpPush::DoHttpPush()
124102
int includeUnit = atoi(sd[11].c_str());
125103
int metertype = atoi(sd[12].c_str());
126104
int lastUpdate = atoi(sd[13].c_str());
127-
std::string lstatus="";
128-
std::string lunit = "";
129105
std::string ltargetVariable = sd[8].c_str();
130106
std::string ltargetDeviceId = sd[9].c_str();
131107
std::string lname = sd[14].c_str();
132108
sendValue = sValue;
133109

134-
// Compute tz
135-
boost::posix_time::time_duration uoffset = get_utc_offset();
136-
unsigned long tzoffset = (int)((double)(uoffset.ticks() / 3600000000LL) * 3600);
110+
unsigned long tzoffset = get_tzoffset();
137111

138112
#ifdef WIN32
139113
unsigned __int64 localTime = lastUpdate;
@@ -152,18 +126,7 @@ void CHttpPush::DoHttpPush()
152126
char szLocalTimeUtcMs[16];
153127
sprintf(szLocalTimeUtcMs, "%llu", localTimeUtc * 1000);
154128

155-
// RFC3339 time format
156-
time_t tmpT = localTimeUtc;
157-
struct tm* timeinfo = gmtime(&tmpT);
158-
159-
char llastUpdate[255];
160-
#if !defined WIN32
161-
snprintf(llastUpdate, sizeof(llastUpdate), "%04d-%02d-%02dT%02d:%02d:%02dZ",
162-
timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
163-
#else
164-
sprintf_s(llastUpdate, sizeof(llastUpdate), "%04d-%02d-%02dT%02d:%02d:%02dZ",
165-
timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
166-
#endif
129+
std::string llastUpdate = get_lastUpdate(localTimeUtc);
167130

168131
// Replace keywords
169132
/*
@@ -182,9 +145,9 @@ void CHttpPush::DoHttpPush()
182145
%h : hostname
183146
*/
184147

185-
lunit = getUnit(delpos, metertype);
186-
lType = RFX_Type_Desc(dType,1);
187-
lSubType = RFX_Type_SubType_Desc(dType,dSubType);
148+
std::string lunit = getUnit(delpos, metertype);
149+
std::string lType = RFX_Type_Desc(dType,1);
150+
std::string lSubType = RFX_Type_SubType_Desc(dType,dSubType);
188151

189152
char hostname[256];
190153
gethostname(hostname, sizeof(hostname));
@@ -214,8 +177,8 @@ void CHttpPush::DoHttpPush()
214177
replaceAll(httpUrl, "%t1", std::string(szLocalTimeMs));
215178
replaceAll(httpUrl, "%t2", std::string(szLocalTimeUtc));
216179
replaceAll(httpUrl, "%t3", std::string(szLocalTimeUtcMs));
217-
replaceAll(httpUrl, "%t4", std::string(llastUpdate));
218-
replaceAll(httpUrl, "%n", std::string(lname));
180+
replaceAll(httpUrl, "%t4", llastUpdate);
181+
replaceAll(httpUrl, "%n", lname);
219182
replaceAll(httpUrl, "%T0", lType);
220183
replaceAll(httpUrl, "%T1", lSubType);
221184
replaceAll(httpUrl, "%h", std::string(hostname));
@@ -228,8 +191,8 @@ void CHttpPush::DoHttpPush()
228191
replaceAll(httpData, "%t1", std::string(szLocalTimeMs));
229192
replaceAll(httpData, "%t2", std::string(szLocalTimeUtc));
230193
replaceAll(httpData, "%t3", std::string(szLocalTimeUtcMs));
231-
replaceAll(httpData, "%t4", std::string(llastUpdate));
232-
replaceAll(httpData, "%n", std::string(lname));
194+
replaceAll(httpData, "%t4", llastUpdate);
195+
replaceAll(httpData, "%n", lname);
233196
replaceAll(httpData, "%T0", lType);
234197
replaceAll(httpData, "%T1", lSubType);
235198
replaceAll(httpData, "%h", std::string(hostname));

push/Push.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "stdafx.h"
22
#include "Push.h"
33
#include "../hardware/hardwaretypes.h"
4+
#include <boost/date_time/c_local_time_adjustor.hpp>
45
#include "../main/Logger.h"
56
#include "../main/RFXtrx.h"
67
#include "../main/SQLHelper.h"
@@ -12,6 +13,50 @@ CPush::CPush()
1213
m_DeviceRowIdx = -1;
1314
}
1415

16+
// STATIC
17+
static boost::posix_time::time_duration get_utc_offset() {
18+
using namespace boost::posix_time;
19+
20+
// boost::date_time::c_local_adjustor uses the C-API to adjust a
21+
// moment given in utc to the same moment in the local time zone.
22+
typedef boost::date_time::c_local_adjustor<ptime> local_adj;
23+
24+
const ptime utc_now = second_clock::universal_time();
25+
const ptime now = local_adj::utc_to_local(utc_now);
26+
27+
return now - utc_now;
28+
}
29+
30+
unsigned long CPush::get_tzoffset()
31+
{
32+
// Compute tz
33+
boost::posix_time::time_duration uoffset = get_utc_offset();
34+
unsigned long tzoffset = (int)((double)(uoffset.ticks() / 3600000000LL) * 3600);
35+
return tzoffset;
36+
}
37+
38+
#ifdef WIN32
39+
std::string CPush::get_lastUpdate(unsigned __int64 localTimeUtc)
40+
#else
41+
std::string CPush::get_lastUpdate(unsigned long long int localTimeUtc)
42+
#endif
43+
{
44+
// RFC3339 time format
45+
time_t tmpT = localTimeUtc;
46+
struct tm* timeinfo = gmtime(&tmpT);
47+
48+
char llastUpdate[255];
49+
#if !defined WIN32
50+
snprintf(llastUpdate, sizeof(llastUpdate), "%04d-%02d-%02dT%02d:%02d:%02dZ",
51+
timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
52+
#else
53+
sprintf_s(llastUpdate, sizeof(llastUpdate), "%04d-%02d-%02dT%02d:%02d:%02dZ",
54+
timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
55+
#endif
56+
57+
return std::string(llastUpdate);
58+
}
59+
1560
// STATIC
1661
void CPush::replaceAll(std::string& context, const std::string& from, const std::string& to)
1762
{
@@ -187,7 +232,7 @@ std::string CPush::ProcessSendValue(const std::string &rawsendValue, const int d
187232
}
188233
else if (vType == "Status")
189234
{
190-
sprintf(szData,"Not supported yet");
235+
sprintf(szData, "%d", nValue);
191236
}
192237
else if ((vType == "Current 1") || (vType == "Current 2") || (vType == "Current 3"))
193238
{

push/Push.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ class CPush
1515
unsigned long long m_DeviceRowIdx;
1616
boost::signals2::connection m_sConnection;
1717

18-
const char *RFX_Type_SubType_DropdownOptions(const unsigned char dType, const unsigned char sType);
1918
std::string ProcessSendValue(const std::string &rawsendValue, const int delpos, const int nValue, const int includeUnit, const int metertype);
2019
std::string getUnit(const int delpos, const int metertypein);
2120

21+
static unsigned long get_tzoffset();
22+
#ifdef WIN32
23+
static std::string get_lastUpdate(unsigned __int64);
24+
#else
25+
static std::string get_lastUpdate(unsigned long long int);
26+
#endif
27+
2228
static void replaceAll(std::string& context, const std::string& from, const std::string& to);
2329
};
2430

0 commit comments

Comments
 (0)