Bugfix InfluxDB shifting times (#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
This commit is contained in:
jomjol
2024-01-01 10:41:12 +01:00
committed by GitHub
parent 4f3f3d9af2
commit 473e458b85
8 changed files with 65 additions and 33 deletions

View File

@@ -34,6 +34,7 @@ struct NumberPost {
bool AllowNegativeRates; bool AllowNegativeRates;
bool checkDigitIncreaseConsistency; bool checkDigitIncreaseConsistency;
time_t lastvalue; time_t lastvalue;
time_t timeStampTimeUTC;
string timeStamp; string timeStamp;
double FlowRateAct; // m3 / min double FlowRateAct; // m3 / min
double PreValue; // last value that was read out well double PreValue; // last value that was read out well

View File

@@ -137,6 +137,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime)
std::string resultraw = ""; std::string resultraw = "";
std::string resultrate = ""; std::string resultrate = "";
std::string resulttimestamp = ""; std::string resulttimestamp = "";
long int timeutc;
string zw = ""; string zw = "";
string namenumber = ""; string namenumber = "";
@@ -152,6 +153,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime)
resulterror = (*NUMBERS)[i]->ErrorMessageText; resulterror = (*NUMBERS)[i]->ErrorMessageText;
resultrate = (*NUMBERS)[i]->ReturnRateValue; resultrate = (*NUMBERS)[i]->ReturnRateValue;
resulttimestamp = (*NUMBERS)[i]->timeStamp; resulttimestamp = (*NUMBERS)[i]->timeStamp;
timeutc = (*NUMBERS)[i]->timeStampTimeUTC;
if ((*NUMBERS)[i]->FieldV1.length() > 0) if ((*NUMBERS)[i]->FieldV1.length() > 0)
{ {
@@ -167,7 +169,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime)
} }
if (result.length() > 0) if (result.length() > 0)
InfluxDBPublish(measurement, namenumber, result, resulttimestamp); InfluxDBPublish(measurement, namenumber, result, timeutc);
} }
} }

View File

@@ -196,6 +196,7 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime)
std::string resultraw = ""; std::string resultraw = "";
std::string resultrate = ""; std::string resultrate = "";
std::string resulttimestamp = ""; std::string resulttimestamp = "";
long int resulttimeutc = 0;
string zw = ""; string zw = "";
string namenumber = ""; string namenumber = "";
@@ -212,6 +213,8 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime)
resulterror = (*NUMBERS)[i]->ErrorMessageText; resulterror = (*NUMBERS)[i]->ErrorMessageText;
resultrate = (*NUMBERS)[i]->ReturnRateValue; resultrate = (*NUMBERS)[i]->ReturnRateValue;
resulttimestamp = (*NUMBERS)[i]->timeStamp; resulttimestamp = (*NUMBERS)[i]->timeStamp;
resulttimeutc = (*NUMBERS)[i]->timeStampTimeUTC;
if ((*NUMBERS)[i]->FieldV2.length() > 0) if ((*NUMBERS)[i]->FieldV2.length() > 0)
{ {
@@ -229,8 +232,7 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime)
printf("vor sende Influx_DB_V2 - namenumber. %s, result: %s, timestampt: %s", namenumber.c_str(), result.c_str(), resulttimestamp.c_str()); printf("vor sende Influx_DB_V2 - namenumber. %s, result: %s, timestampt: %s", namenumber.c_str(), result.c_str(), resulttimestamp.c_str());
if (result.length() > 0) if (result.length() > 0)
InfluxDB_V2_Publish(measurement, namenumber, result, resulttimestamp); InfluxDB_V2_Publish(measurement, namenumber, result, resulttimeutc);
// InfluxDB_V2_Publish(namenumber, result, resulttimestamp);
} }
} }

View File

@@ -285,6 +285,7 @@ void ClassFlowPostProcessing::SavePreValue()
struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue); struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue);
strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo); strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
NUMBERS[j]->timeStamp = std::string(buffer); NUMBERS[j]->timeStamp = std::string(buffer);
NUMBERS[j]->timeStampTimeUTC = NUMBERS[j]->lastvalue;
// ESP_LOGD(TAG, "SaverPreValue %d, Value: %f, Nachkomma %d", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma); // ESP_LOGD(TAG, "SaverPreValue %d, Value: %f, Nachkomma %d", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
_zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n"; _zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n";

View File

@@ -5,6 +5,7 @@
#include <time.h> #include <time.h>
#include "ClassLogFile.h" #include "ClassLogFile.h"
#include "esp_http_client.h" #include "esp_http_client.h"
#include "time_sntp.h"
#include "../../include/defines.h" #include "../../include/defines.h"
@@ -30,7 +31,7 @@ void InfluxDB_V2_Init(std::string _uri, std::string _bucket, std::string _org, s
_influxDB_V2_Token = _token; _influxDB_V2_Token = _token;
} }
void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp) void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC)
{ {
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
esp_http_client_config_t http_config = { esp_http_client_config_t http_config = {
@@ -41,28 +42,20 @@ void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string
.user_data = response_buffer .user_data = response_buffer
}; };
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp); LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", timeUTC: " + std::to_string(_timeUTC));
std::string payload; std::string payload;
char nowTimestamp[21]; char nowTimestamp[21];
if (_timestamp.length() > 0) if (_timeUTC > 0)
{ {
struct tm tm; LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp (UTC): " + std::to_string(_timeUTC));
sprintf(nowTimestamp,"%ld000000000", _timeUTC); // UTC
time_t t;
time(&t);
localtime_r(&t, &tm); // Extract DST setting from actual time to consider it for timestamp evaluation
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
t = mktime(&tm);
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp: " + _timestamp + ", Timestamp (UTC): " + std::to_string(t));
sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp; payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
} }
else else
{ {
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "no timestamp given");
payload = _measurement + " " + _key + "=" + _content; payload = _measurement + " " + _key + "=" + _content;
} }
@@ -137,7 +130,7 @@ static esp_err_t http_event_handler(esp_http_client_event_t *evt)
return ESP_OK; return ESP_OK;
} }
void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp) { void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC) {
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
esp_http_client_config_t http_config = { esp_http_client_config_t http_config = {
.user_agent = "ESP32 Meter reader", .user_agent = "ESP32 Meter reader",
@@ -156,25 +149,17 @@ void InfluxDBPublish(std::string _measurement, std::string _key, std::string _co
std::string payload; std::string payload;
char nowTimestamp[21]; char nowTimestamp[21];
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp); LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", timeUTC: " + std::to_string(_timeUTC));
if (_timestamp.length() > 0) if (_timeUTC > 0)
{ {
struct tm tm; LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp (UTC): " + std::to_string(_timeUTC));
sprintf(nowTimestamp,"%ld000000000", _timeUTC); // UTC
time_t t;
time(&t);
localtime_r(&t, &tm); // Extract DST setting from actual time to consider it for timestamp evaluation
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
t = mktime(&tm);
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp: " + _timestamp + ", Timestamp (UTC): " + std::to_string(t));
sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp; payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
} }
else else
{ {
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "no timestamp given");
payload = _measurement + " " + _key + "=" + _content; payload = _measurement + " " + _key + "=" + _content;
} }

View File

@@ -10,11 +10,11 @@
// Interface to InfluxDB v1.x // Interface to InfluxDB v1.x
void InfluxDBInit(std::string _influxDBURI, std::string _database, std::string _user, std::string _password); void InfluxDBInit(std::string _influxDBURI, std::string _database, std::string _user, std::string _password);
void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp); void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC);
// Interface to InfluxDB v2.x // Interface to InfluxDB v2.x
void InfluxDB_V2_Init(std::string _uri, std::string _bucket, std::string _org, std::string _token); void InfluxDB_V2_Init(std::string _uri, std::string _bucket, std::string _org, std::string _token);
void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp); void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC);

View File

@@ -31,6 +31,9 @@ std::string getNtpStatusText(sntp_sync_status_t status);
static void setTimeZone(std::string _tzstring); static void setTimeZone(std::string _tzstring);
static std::string getServerName(void); static std::string getServerName(void);
int LocalTimeToUTCOffsetSeconds;
std::string ConvertTimeToString(time_t _time, const char * frm) std::string ConvertTimeToString(time_t _time, const char * frm)
{ {
@@ -89,15 +92,51 @@ bool time_manual_reset_sync(void)
} }
int getUTCOffsetSeconds(std::string &zeitzone)
{
int offset = 0;
int vorzeichen = 1;
int minuten = 0;
int stunden = 0;
time_t now;
struct tm timeinfo;
time (&now);
localtime_r(&now, &timeinfo);
char buffer[80];
strftime(buffer, 80, "%z", &timeinfo);
zeitzone = std::string(buffer);
if (zeitzone.length() == 5)
{
if (zeitzone[0] == '-')
vorzeichen = -1;
stunden = stoi(zeitzone.substr(1, 2));
minuten = stoi(zeitzone.substr(3, 2));
offset = ((stunden * 60) + minuten) * 60;
}
return offset;
}
void setTimeZone(std::string _tzstring) void setTimeZone(std::string _tzstring)
{ {
setenv("TZ", _tzstring.c_str(), 1); setenv("TZ", _tzstring.c_str(), 1);
tzset(); tzset();
_tzstring = "Time zone set to " + _tzstring; _tzstring = "Time zone set to " + _tzstring;
LogFile.WriteToFile(ESP_LOG_INFO, TAG, _tzstring); LogFile.WriteToFile(ESP_LOG_INFO, TAG, _tzstring);
std::string zeitzone;
LocalTimeToUTCOffsetSeconds = getUTCOffsetSeconds(zeitzone);
// std::string zw = std::to_string(LocalTimeToUTCOffsetSeconds);
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "time zone: " + zeitzone + " Delta to UTC: " + std::to_string(LocalTimeToUTCOffsetSeconds) + " seconds");
} }
std::string getNtpStatusText(sntp_sync_status_t status) { std::string getNtpStatusText(sntp_sync_status_t status) {
if (status == SNTP_SYNC_STATUS_COMPLETED) { if (status == SNTP_SYNC_STATUS_COMPLETED) {
return "Synchronized"; return "Synchronized";

View File

@@ -28,5 +28,7 @@ bool setupTime();
bool time_manual_reset_sync(void); bool time_manual_reset_sync(void);
extern int LocalTimeToUTCOffsetSeconds;
#endif //TIMESNTP_H #endif //TIMESNTP_H