From 5462057cadebf4e2d368042867c5ff8cf6b90dbb Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Fri, 4 May 2018 09:54:56 +0200 Subject: [PATCH] Fix timer data I/O errors 5.13.1a * Fix several timer data input and output errors (#2597, #2620) --- sonoff/_releasenotes.ino | 1 + sonoff/xdrv_09_timers.ino | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 0ab8d3efc..e02b9d3f6 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,6 +1,7 @@ /* 5.13.1a * Change user_config.h otaurl to http://sonoff.maddox.co.uk/tasmota/sonoff.bin (#2588, #2602) * Fix compile error when ADC is enabled and Rules are disabled (#2608) + * Fix several timer data input and output errors (#2597, #2620) * * 5.13.1 20180501 * Fix JSON buffers size too small for execution in some situations (#2580) diff --git a/sonoff/xdrv_09_timers.ino b/sonoff/xdrv_09_timers.ino index e85349da7..533ee818e 100644 --- a/sonoff/xdrv_09_timers.ino +++ b/sonoff/xdrv_09_timers.ino @@ -302,6 +302,7 @@ void TimerEverySecond() void PrepShowTimer(uint8_t index) { char days[8] = { 0 }; + char sign[2] = { 0 }; char soutput[80]; Timer xtimer = Settings.timer[index -1]; @@ -318,10 +319,13 @@ void PrepShowTimer(uint8_t index) #ifdef USE_SUNRISE int16_t hour = xtimer.time / 60; if ((1 == xtimer.mode) || (2 == xtimer.mode)) { // Sunrise or Sunset - if (hour > 11) { hour = (hour -12) * -1; } + if (hour > 11) { + hour -= 12; + sign[0] = '-'; + } } - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"" D_CMND_TIMER "%d\":{\"" D_JSON_TIMER_ARM "\":%d,\"" D_JSON_TIMER_MODE "\":%d,\"" D_JSON_TIMER_TIME "\":\"%02d:%02d\",\"" D_JSON_TIMER_WINDOW "\":%d,\"" D_JSON_TIMER_DAYS "\":\"%s\",\"" D_JSON_TIMER_REPEAT "\":%d%s,\"" D_JSON_TIMER_ACTION "\":%d}"), - mqtt_data, index, xtimer.arm, xtimer.mode, hour, xtimer.time % 60, xtimer.window, days, xtimer.repeat, soutput, xtimer.power); + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"" D_CMND_TIMER "%d\":{\"" D_JSON_TIMER_ARM "\":%d,\"" D_JSON_TIMER_MODE "\":%d,\"" D_JSON_TIMER_TIME "\":\"%s%02d:%02d\",\"" D_JSON_TIMER_WINDOW "\":%d,\"" D_JSON_TIMER_DAYS "\":\"%s\",\"" D_JSON_TIMER_REPEAT "\":%d%s,\"" D_JSON_TIMER_ACTION "\":%d}"), + mqtt_data, index, xtimer.arm, xtimer.mode, sign, hour, xtimer.time % 60, xtimer.window, days, xtimer.repeat, soutput, xtimer.power); #else snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"" D_CMND_TIMER "%d\":{\"" D_JSON_TIMER_ARM "\":%d,\"" D_JSON_TIMER_TIME "\":\"%02d:%02d\",\"" D_JSON_TIMER_WINDOW "\":%d,\"" D_JSON_TIMER_DAYS "\":\"%s\",\"" D_JSON_TIMER_REPEAT "\":%d%s,\"" D_JSON_TIMER_ACTION "\":%d}"), mqtt_data, index, xtimer.arm, xtimer.time / 60, xtimer.time % 60, xtimer.window, days, xtimer.repeat, soutput, xtimer.power); @@ -374,13 +378,18 @@ boolean TimerCommand() if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_TIME))].success()) { uint16_t itime = 0; int8_t value = 0; + uint8_t sign = 0; char time_str[10]; snprintf(time_str, sizeof(time_str), root[parm_uc]); const char *substr = strtok(time_str, ":"); if (substr != NULL) { + if (strchr(substr, '-')) { + sign = 1; + substr++; + } value = atoi(substr); - if (value < 0) { value = abs(value) +12; } // Allow entering timer offset from -11:59 to -00:01 converted to 12:01 to 23:59 + if (sign) { value += 12; } // Allow entering timer offset from -11:59 to -00:01 converted to 12:01 to 23:59 if (value > 23) { value = 23; } itime = value * 60; substr = strtok(NULL, ":"); @@ -401,14 +410,13 @@ boolean TimerCommand() // SMTWTFS = 1234567 = 0011001 = 00TW00S = --TW--S Settings.timer[index].days = 0; const char *tday = root[parm_uc]; - char ch = '.'; - uint8_t i = 0; + char ch = *tday++; while ((ch != '\0') && (i < 7)) { - ch = *tday++; if (ch == '-') { ch = '0'; } uint8_t mask = 1 << i++; Settings.timer[index].days |= (ch == '0') ? 0 : mask; + ch = *tday++; } } if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_REPEAT))].success()) { @@ -419,7 +427,7 @@ boolean TimerCommand() Settings.timer[index].device = (device < devices_present) ? device : 0; } if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_ACTION))].success()) { - uint8_t action = ((uint8_t)root[parm_uc] -1) & 0x03; + uint8_t action = (uint8_t)root[parm_uc] & 0x03; Settings.timer[index].power = (devices_present) ? action : 3; // If no devices than only allow rules }