Debug influxdb (#2283)

* Fix time offset issues in InfluxDB component. (#2278)

Closes #2273
Closes #2150

* Update interface_influxdb.cpp

* Update interface_influxdb.cpp

* Improve Logging

* Implement TimeSync at beginning

* Update time_sntp.cpp

* Update time_sntp.cpp

* Set Time After WLAN Init

---------

Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
This commit is contained in:
jomjol
2023-04-02 17:39:36 +02:00
committed by GitHub
parent e2cf8337d4
commit 63ac38a52d
4 changed files with 64 additions and 23 deletions

View File

@@ -46,10 +46,8 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
// Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
char nowTimestamp[21];
std::string payload;
char nowTimestamp[21];
if (_timestamp.length() > 0)
{
@@ -57,20 +55,22 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
struct tm * ptm;
ptm = gmtime ( &t );
time_t utc = mktime(ptm);
utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// struct tm * ptm;
// ptm = gmtime ( &t );
// time_t utc = mktime(ptm);
sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t));
// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));
sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp;
// payload = _influxDB_V2_Measurement + " " + _key + "=774 " + nowTimestamp;
}
else
{
payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
// payload = _influxDB_V2_Measurement + " " + _key + "=774";
}
payload.shrink_to_fit();
@@ -157,10 +157,10 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest
http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
}
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
char nowTimestamp[21];
std::string payload;
char nowTimestamp[21];
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
if (_timestamp.length() > 0)
{
@@ -168,19 +168,22 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
struct tm * ptm;
ptm = gmtime ( &t );
time_t utc = mktime(ptm);
utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// struct tm * ptm;
// ptm = gmtime ( &t );
// time_t utc = mktime(ptm);
sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t));
// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));
sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
// payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp;
}
else
{
payload = _influxDBMeasurement + " " + _key + "=" + _content;
payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
}
payload.shrink_to_fit();

View File

@@ -72,6 +72,24 @@ void time_sync_notification_cb(struct timeval *tv)
}
bool time_manual_reset_sync(void)
{
sntp_restart();
// sntp_init();
int retry = 0;
const int retry_count = 10;
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Waiting for system time to be set... " + std::to_string(retry) + "/" + std::to_string(retry_count));
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
if (retry >= retry_count)
return false;
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Waiting for system time successfull with " + std::to_string(retry) + "/" + std::to_string(retry_count));
return true;
}
void setTimeZone(std::string _tzstring)
{
setenv("TZ", _tzstring.c_str(), 1);
@@ -220,11 +238,16 @@ bool setupTime() {
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Configuring NTP Client...");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, timeServer.c_str());
sntp_init();
sntp_set_time_sync_notification_cb(time_sync_notification_cb);
setTimeZone(timeZone);
sntp_init();
/*
if (!wait_for_timesync())
{
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Timesync at startup failed.");
}
*/
}
@@ -250,3 +273,5 @@ bool setupTime() {
return true;
}

View File

@@ -27,4 +27,7 @@ bool getTimeWasNotSetAtBoot(void);
bool getUseNtp(void);
bool setupTime();
bool time_manual_reset_sync(void);
#endif //TIMESNTP_H