diff --git a/code/components/jomjol_flowcontroll/ClassFlowDefineTypes.h b/code/components/jomjol_flowcontroll/ClassFlowDefineTypes.h index 10c7b8e5..059a55b8 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowDefineTypes.h +++ b/code/components/jomjol_flowcontroll/ClassFlowDefineTypes.h @@ -34,6 +34,7 @@ struct NumberPost { bool AllowNegativeRates; bool checkDigitIncreaseConsistency; time_t lastvalue; + time_t timeStampTimeUTC; string timeStamp; double FlowRateAct; // m3 / min double PreValue; // last value that was read out well diff --git a/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp b/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp index abe524ed..d6544004 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp @@ -137,6 +137,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime) std::string resultraw = ""; std::string resultrate = ""; std::string resulttimestamp = ""; + long int timeutc; string zw = ""; string namenumber = ""; @@ -152,6 +153,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime) resulterror = (*NUMBERS)[i]->ErrorMessageText; resultrate = (*NUMBERS)[i]->ReturnRateValue; resulttimestamp = (*NUMBERS)[i]->timeStamp; + timeutc = (*NUMBERS)[i]->timeStampTimeUTC; if ((*NUMBERS)[i]->FieldV1.length() > 0) { @@ -167,7 +169,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime) } if (result.length() > 0) - InfluxDBPublish(measurement, namenumber, result, resulttimestamp); + InfluxDBPublish(measurement, namenumber, result, timeutc); } } diff --git a/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp b/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp index bd423305..669e11c1 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp @@ -196,6 +196,7 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime) std::string resultraw = ""; std::string resultrate = ""; std::string resulttimestamp = ""; + long int resulttimeutc = 0; string zw = ""; string namenumber = ""; @@ -212,6 +213,8 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime) resulterror = (*NUMBERS)[i]->ErrorMessageText; resultrate = (*NUMBERS)[i]->ReturnRateValue; resulttimestamp = (*NUMBERS)[i]->timeStamp; + resulttimeutc = (*NUMBERS)[i]->timeStampTimeUTC; + 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()); if (result.length() > 0) - InfluxDB_V2_Publish(measurement, namenumber, result, resulttimestamp); -// InfluxDB_V2_Publish(namenumber, result, resulttimestamp); + InfluxDB_V2_Publish(measurement, namenumber, result, resulttimeutc); } } diff --git a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp index 70c192ff..dbe1f11f 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp @@ -285,6 +285,7 @@ void ClassFlowPostProcessing::SavePreValue() struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue); strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo); 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); _zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n"; diff --git a/code/components/jomjol_influxdb/interface_influxdb.cpp b/code/components/jomjol_influxdb/interface_influxdb.cpp index dfad9320..79c4b8d7 100644 --- a/code/components/jomjol_influxdb/interface_influxdb.cpp +++ b/code/components/jomjol_influxdb/interface_influxdb.cpp @@ -5,6 +5,7 @@ #include #include "ClassLogFile.h" #include "esp_http_client.h" +#include "time_sntp.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; } -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}; 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 }; - 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; char nowTimestamp[21]; - if (_timestamp.length() > 0) + if (_timeUTC > 0) { - struct tm tm; - - 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 + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp (UTC): " + std::to_string(_timeUTC)); + sprintf(nowTimestamp,"%ld000000000", _timeUTC); // UTC payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp; } else { + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "no timestamp given"); payload = _measurement + " " + _key + "=" + _content; } @@ -137,7 +130,7 @@ static esp_err_t http_event_handler(esp_http_client_event_t *evt) 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}; esp_http_client_config_t http_config = { .user_agent = "ESP32 Meter reader", @@ -156,25 +149,17 @@ void InfluxDBPublish(std::string _measurement, std::string _key, std::string _co std::string payload; 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; - - 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 + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp (UTC): " + std::to_string(_timeUTC)); + sprintf(nowTimestamp,"%ld000000000", _timeUTC); // UTC payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp; } else { + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "no timestamp given"); payload = _measurement + " " + _key + "=" + _content; } diff --git a/code/components/jomjol_influxdb/interface_influxdb.h b/code/components/jomjol_influxdb/interface_influxdb.h index 0d969800..66778621 100644 --- a/code/components/jomjol_influxdb/interface_influxdb.h +++ b/code/components/jomjol_influxdb/interface_influxdb.h @@ -10,11 +10,11 @@ // Interface to InfluxDB v1.x 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 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); diff --git a/code/components/jomjol_time_sntp/time_sntp.cpp b/code/components/jomjol_time_sntp/time_sntp.cpp index 3c620f16..7560bcd7 100644 --- a/code/components/jomjol_time_sntp/time_sntp.cpp +++ b/code/components/jomjol_time_sntp/time_sntp.cpp @@ -31,6 +31,9 @@ std::string getNtpStatusText(sntp_sync_status_t status); static void setTimeZone(std::string _tzstring); static std::string getServerName(void); +int LocalTimeToUTCOffsetSeconds; + + 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) { setenv("TZ", _tzstring.c_str(), 1); tzset(); + _tzstring = "Time zone set to " + _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) { if (status == SNTP_SYNC_STATUS_COMPLETED) { return "Synchronized"; diff --git a/code/components/jomjol_time_sntp/time_sntp.h b/code/components/jomjol_time_sntp/time_sntp.h index a5df8bf1..9b5d8796 100644 --- a/code/components/jomjol_time_sntp/time_sntp.h +++ b/code/components/jomjol_time_sntp/time_sntp.h @@ -28,5 +28,7 @@ bool setupTime(); bool time_manual_reset_sync(void); +extern int LocalTimeToUTCOffsetSeconds; + #endif //TIMESNTP_H \ No newline at end of file