optionally append timezone to timestamp in JSON messages

This commit is contained in:
Rodney Gitzel 2018-09-02 01:42:52 -07:00
parent 52af61b278
commit 9413ba9f47
6 changed files with 46 additions and 38 deletions

3
.gitignore vendored
View File

@ -12,3 +12,6 @@ build
## Visual Studio Code specific ######
.vscode
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json

View File

@ -170,7 +170,7 @@ enum WeekInMonthOptions {Last, First, Second, Third, Fourth};
enum DayOfTheWeekOptions {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};
enum MonthNamesOptions {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
enum HemisphereOptions {North, South};
enum GetDateAndTimeOptions { DT_LOCAL, DT_UTC, DT_RESTART, DT_UPTIME };
enum GetDateAndTimeOptions { DT_LOCAL, DT_UTC, DT_RESTART };
enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL};

View File

@ -1418,7 +1418,7 @@ void PublishStatus(uint8_t payload)
if ((0 == payload) || (1 == payload)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS1_PARAMETER "\":{\"" D_JSON_BAUDRATE "\":%d,\"" D_CMND_GROUPTOPIC "\":\"%s\",\"" D_CMND_OTAURL "\":\"%s\",\"" D_JSON_RESTARTREASON "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\",\"" D_JSON_STARTUPUTC "\":\"%s\",\"" D_CMND_SLEEP "\":%d,\"" D_JSON_BOOTCOUNT "\":%d,\"" D_JSON_SAVECOUNT "\":%d,\"" D_JSON_SAVEADDRESS "\":\"%X\"}}"),
baudrate, Settings.mqtt_grptopic, Settings.ota_url, GetResetReason().c_str(), GetDateAndTime(DT_UPTIME).c_str(), GetDateAndTime(DT_RESTART).c_str(), Settings.sleep, Settings.bootcount, Settings.save_flag, GetSettingsAddress());
baudrate, Settings.mqtt_grptopic, Settings.ota_url, GetResetReason().c_str(), GetUptime().c_str(), GetDateAndTime(DT_RESTART).c_str(), Settings.sleep, Settings.bootcount, Settings.save_flag, GetSettingsAddress());
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "1"));
}
@ -1509,7 +1509,7 @@ void MqttShowState()
{
char stemp1[33];
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_JSON_TIME "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\""), mqtt_data, GetDateAndTime(DT_LOCAL).c_str(), GetDateAndTime(DT_UPTIME).c_str());
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_JSON_TIME "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\""), mqtt_data, GetDateAndTime(DT_LOCAL).c_str(), GetUptime().c_str());
#ifdef USE_ADC_VCC
dtostrfd((double)ESP.getVcc()/1000, 3, stemp1);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_VCC "\":%s"), mqtt_data, stemp1);
@ -1626,7 +1626,7 @@ void PerformEverySecond()
if ((2 == RtcTime.minute) && latest_uptime_flag) {
latest_uptime_flag = false;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_TIME "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\"}"), GetDateAndTime(DT_LOCAL).c_str(), GetDateAndTime(DT_UPTIME).c_str());
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_TIME "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\"}"), GetDateAndTime(DT_LOCAL).c_str(), GetUptime().c_str());
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_UPTIME));
}
if ((3 == RtcTime.minute) && !latest_uptime_flag) latest_uptime_flag = true;

View File

@ -1802,44 +1802,46 @@ String GetBuildDateAndTime()
return String(bdt);
}
/*
* timestamps in https://en.wikipedia.org/wiki/ISO_8601 format
*
* DT_UTC - current data and time in Greenwich, England (aka GMT)
* DT_LOCAL - current date and time taking timezone into account
* DT_RESTART - the date and time this device last started, in local timezone
*
* Format:
* "2017-03-07T11:08:02-07:00" - if DT_LOCAL and TIME_APPEND_TIMEZONE=1
* "2017-03-07T11:08:02" - otherwise
*/
String GetDateAndTime(byte time_type)
{
// enum GetDateAndTimeOptions { DT_LOCAL, DT_UTC, DT_RESTART, DT_UPTIME };
// "2017-03-07T11:08:02" - ISO8601:2004
char dt[21];
// "2017-03-07T11:08:02-07:00" - ISO8601:2004
char dt[27];
TIME_T tmpTime;
if (DT_UPTIME == time_type) {
if (restart_time) {
BreakTime(utc_time - restart_time, tmpTime);
} else {
BreakTime(uptime, tmpTime);
}
// "P128DT14H35M44S" - ISO8601:2004 - https://en.wikipedia.org/wiki/ISO_8601 Durations
// snprintf_P(dt, sizeof(dt), PSTR("P%dDT%02dH%02dM%02dS"), ut.days, ut.hour, ut.minute, ut.second);
// "128 14:35:44" - OpenVMS
// "128T14:35:44" - Tasmota
snprintf_P(dt, sizeof(dt), PSTR("%dT%02d:%02d:%02d"),
tmpTime.days, tmpTime.hour, tmpTime.minute, tmpTime.second);
} else {
switch (time_type) {
case DT_UTC:
BreakTime(utc_time, tmpTime);
tmpTime.year += 1970;
break;
case DT_RESTART:
if (restart_time == 0) {
return "";
}
BreakTime(restart_time, tmpTime);
tmpTime.year += 1970;
break;
default:
tmpTime = RtcTime;
}
snprintf_P(dt, sizeof(dt), PSTR("%04d-%02d-%02dT%02d:%02d:%02d"),
tmpTime.year, tmpTime.month, tmpTime.day_of_month, tmpTime.hour, tmpTime.minute, tmpTime.second);
switch (time_type) {
case DT_UTC:
BreakTime(utc_time, tmpTime);
tmpTime.year += 1970;
break;
case DT_RESTART:
if (restart_time == 0) {
return "";
}
BreakTime(restart_time, tmpTime);
tmpTime.year += 1970;
break;
default:
tmpTime = RtcTime;
}
snprintf_P(dt, sizeof(dt), PSTR("%04d-%02d-%02dT%02d:%02d:%02d"),
tmpTime.year, tmpTime.month, tmpTime.day_of_month, tmpTime.hour, tmpTime.minute, tmpTime.second);
if (TIME_APPEND_TIMEZONE && (time_type == DT_LOCAL)) {
snprintf_P(dt, sizeof(dt), PSTR("%s%+03d:00"), dt, Settings.timezone);
}
return String(dt);
}

View File

@ -152,6 +152,9 @@
#define TIME_STD_HOUR 3 // Hour (0 to 23)
#define TIME_STD_OFFSET +60 // Offset from UTC in minutes (-780 to +780)
// -- Time - formatting options
#define TIME_APPEND_TIMEZONE 0 // for local timestamps: 0 = no timezone in string, 1 = append numeric timezone (e.g. "+1:00" or "-7:00")
// -- Location ------------------------------------
#define LATITUDE 48.858360 // [Latitude] Your location to be used with sunrise and sunset
#define LONGITUDE 2.294442 // [Longitude] Your location to be used with sunrise and sunset

View File

@ -1291,7 +1291,7 @@ void HandleInformation()
func += F(D_PROGRAM_VERSION "}2"); func += my_version;
func += F("}1" D_BUILD_DATE_AND_TIME "}2"); func += GetBuildDateAndTime();
func += F("}1" D_CORE_AND_SDK_VERSION "}2" ARDUINO_ESP8266_RELEASE "/"); func += String(ESP.getSdkVersion());
func += F("}1" D_UPTIME "}2"); func += GetDateAndTime(DT_UPTIME);
func += F("}1" D_UPTIME "}2"); func += GetUptime();
snprintf_P(stopic, sizeof(stopic), PSTR(" at %X"), GetSettingsAddress());
func += F("}1" D_FLASH_WRITE_COUNT "}2"); func += String(Settings.save_flag); func += stopic;
func += F("}1" D_BOOT_COUNT "}2"); func += String(Settings.bootcount);