Skip to content

Commit 473e458

Browse files
authored
Bugfix InfluxDB shifting times (jomjol#2785)
* Modify time_sntp * Update time_sntp.cpp * Update time_sntp.cpp * update * Update interface_influxdb.cpp * Update interface_influxdb.cpp * Update time_sntp.cpp * Upload * Update interface_influxdb.cpp * Update interface_influxdb.cpp * Update interface_influxdb.cpp * Remove Time Convert vom influx Interface
1 parent 4f3f3d9 commit 473e458

File tree

8 files changed

+65
-33
lines changed

8 files changed

+65
-33
lines changed

code/components/jomjol_flowcontroll/ClassFlowDefineTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct NumberPost {
3434
bool AllowNegativeRates;
3535
bool checkDigitIncreaseConsistency;
3636
time_t lastvalue;
37+
time_t timeStampTimeUTC;
3738
string timeStamp;
3839
double FlowRateAct; // m3 / min
3940
double PreValue; // last value that was read out well

code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime)
137137
std::string resultraw = "";
138138
std::string resultrate = "";
139139
std::string resulttimestamp = "";
140+
long int timeutc;
140141
string zw = "";
141142
string namenumber = "";
142143

@@ -152,6 +153,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime)
152153
resulterror = (*NUMBERS)[i]->ErrorMessageText;
153154
resultrate = (*NUMBERS)[i]->ReturnRateValue;
154155
resulttimestamp = (*NUMBERS)[i]->timeStamp;
156+
timeutc = (*NUMBERS)[i]->timeStampTimeUTC;
155157

156158
if ((*NUMBERS)[i]->FieldV1.length() > 0)
157159
{
@@ -167,7 +169,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime)
167169
}
168170

169171
if (result.length() > 0)
170-
InfluxDBPublish(measurement, namenumber, result, resulttimestamp);
172+
InfluxDBPublish(measurement, namenumber, result, timeutc);
171173
}
172174
}
173175

code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime)
196196
std::string resultraw = "";
197197
std::string resultrate = "";
198198
std::string resulttimestamp = "";
199+
long int resulttimeutc = 0;
199200
string zw = "";
200201
string namenumber = "";
201202

@@ -212,6 +213,8 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime)
212213
resulterror = (*NUMBERS)[i]->ErrorMessageText;
213214
resultrate = (*NUMBERS)[i]->ReturnRateValue;
214215
resulttimestamp = (*NUMBERS)[i]->timeStamp;
216+
resulttimeutc = (*NUMBERS)[i]->timeStampTimeUTC;
217+
215218

216219
if ((*NUMBERS)[i]->FieldV2.length() > 0)
217220
{
@@ -229,8 +232,7 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime)
229232
printf("vor sende Influx_DB_V2 - namenumber. %s, result: %s, timestampt: %s", namenumber.c_str(), result.c_str(), resulttimestamp.c_str());
230233

231234
if (result.length() > 0)
232-
InfluxDB_V2_Publish(measurement, namenumber, result, resulttimestamp);
233-
// InfluxDB_V2_Publish(namenumber, result, resulttimestamp);
235+
InfluxDB_V2_Publish(measurement, namenumber, result, resulttimeutc);
234236
}
235237
}
236238

code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ void ClassFlowPostProcessing::SavePreValue()
285285
struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue);
286286
strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
287287
NUMBERS[j]->timeStamp = std::string(buffer);
288+
NUMBERS[j]->timeStampTimeUTC = NUMBERS[j]->lastvalue;
288289
// ESP_LOGD(TAG, "SaverPreValue %d, Value: %f, Nachkomma %d", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
289290

290291
_zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n";

code/components/jomjol_influxdb/interface_influxdb.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <time.h>
66
#include "ClassLogFile.h"
77
#include "esp_http_client.h"
8+
#include "time_sntp.h"
89
#include "../../include/defines.h"
910

1011

@@ -30,7 +31,7 @@ void InfluxDB_V2_Init(std::string _uri, std::string _bucket, std::string _org, s
3031
_influxDB_V2_Token = _token;
3132
}
3233

33-
void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp)
34+
void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC)
3435
{
3536
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
3637
esp_http_client_config_t http_config = {
@@ -41,28 +42,20 @@ void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string
4142
.user_data = response_buffer
4243
};
4344

44-
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
45+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", timeUTC: " + std::to_string(_timeUTC));
4546

4647
std::string payload;
4748
char nowTimestamp[21];
4849

49-
if (_timestamp.length() > 0)
50+
if (_timeUTC > 0)
5051
{
51-
struct tm tm;
52-
53-
time_t t;
54-
time(&t);
55-
localtime_r(&t, &tm); // Extract DST setting from actual time to consider it for timestamp evaluation
56-
57-
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
58-
t = mktime(&tm);
59-
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp: " + _timestamp + ", Timestamp (UTC): " + std::to_string(t));
60-
61-
sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
52+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp (UTC): " + std::to_string(_timeUTC));
53+
sprintf(nowTimestamp,"%ld000000000", _timeUTC); // UTC
6254
payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
6355
}
6456
else
6557
{
58+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "no timestamp given");
6659
payload = _measurement + " " + _key + "=" + _content;
6760
}
6861

@@ -137,7 +130,7 @@ static esp_err_t http_event_handler(esp_http_client_event_t *evt)
137130
return ESP_OK;
138131
}
139132

140-
void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp) {
133+
void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC) {
141134
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
142135
esp_http_client_config_t http_config = {
143136
.user_agent = "ESP32 Meter reader",
@@ -156,25 +149,17 @@ void InfluxDBPublish(std::string _measurement, std::string _key, std::string _co
156149
std::string payload;
157150
char nowTimestamp[21];
158151

159-
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
152+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", timeUTC: " + std::to_string(_timeUTC));
160153

161-
if (_timestamp.length() > 0)
154+
if (_timeUTC > 0)
162155
{
163-
struct tm tm;
164-
165-
time_t t;
166-
time(&t);
167-
localtime_r(&t, &tm); // Extract DST setting from actual time to consider it for timestamp evaluation
168-
169-
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
170-
t = mktime(&tm);
171-
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp: " + _timestamp + ", Timestamp (UTC): " + std::to_string(t));
172-
173-
sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
156+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp (UTC): " + std::to_string(_timeUTC));
157+
sprintf(nowTimestamp,"%ld000000000", _timeUTC); // UTC
174158
payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
175159
}
176160
else
177161
{
162+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "no timestamp given");
178163
payload = _measurement + " " + _key + "=" + _content;
179164
}
180165

code/components/jomjol_influxdb/interface_influxdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
// Interface to InfluxDB v1.x
1212
void InfluxDBInit(std::string _influxDBURI, std::string _database, std::string _user, std::string _password);
13-
void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp);
13+
void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC);
1414

1515
// Interface to InfluxDB v2.x
1616
void InfluxDB_V2_Init(std::string _uri, std::string _bucket, std::string _org, std::string _token);
17-
void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp);
17+
void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC);
1818

1919

2020

code/components/jomjol_time_sntp/time_sntp.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ std::string getNtpStatusText(sntp_sync_status_t status);
3131
static void setTimeZone(std::string _tzstring);
3232
static std::string getServerName(void);
3333

34+
int LocalTimeToUTCOffsetSeconds;
35+
36+
3437

3538
std::string ConvertTimeToString(time_t _time, const char * frm)
3639
{
@@ -89,15 +92,51 @@ bool time_manual_reset_sync(void)
8992
}
9093

9194

95+
int getUTCOffsetSeconds(std::string &zeitzone)
96+
{
97+
int offset = 0;
98+
int vorzeichen = 1;
99+
int minuten = 0;
100+
int stunden = 0;
101+
time_t now;
102+
struct tm timeinfo;
103+
104+
time (&now);
105+
localtime_r(&now, &timeinfo);
106+
char buffer[80];
107+
strftime(buffer, 80, "%z", &timeinfo);
108+
zeitzone = std::string(buffer);
109+
110+
if (zeitzone.length() == 5)
111+
{
112+
if (zeitzone[0] == '-')
113+
vorzeichen = -1;
114+
115+
stunden = stoi(zeitzone.substr(1, 2));
116+
minuten = stoi(zeitzone.substr(3, 2));
117+
118+
offset = ((stunden * 60) + minuten) * 60;
119+
}
120+
return offset;
121+
}
122+
123+
92124
void setTimeZone(std::string _tzstring)
93125
{
94126
setenv("TZ", _tzstring.c_str(), 1);
95127
tzset();
128+
96129
_tzstring = "Time zone set to " + _tzstring;
97130
LogFile.WriteToFile(ESP_LOG_INFO, TAG, _tzstring);
131+
132+
std::string zeitzone;
133+
LocalTimeToUTCOffsetSeconds = getUTCOffsetSeconds(zeitzone);
134+
// std::string zw = std::to_string(LocalTimeToUTCOffsetSeconds);
135+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "time zone: " + zeitzone + " Delta to UTC: " + std::to_string(LocalTimeToUTCOffsetSeconds) + " seconds");
98136
}
99137

100138

139+
101140
std::string getNtpStatusText(sntp_sync_status_t status) {
102141
if (status == SNTP_SYNC_STATUS_COMPLETED) {
103142
return "Synchronized";

code/components/jomjol_time_sntp/time_sntp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ bool setupTime();
2828

2929
bool time_manual_reset_sync(void);
3030

31+
extern int LocalTimeToUTCOffsetSeconds;
32+
3133

3234
#endif //TIMESNTP_H

0 commit comments

Comments
 (0)