Refactor Timer commands

Refactor Timer commands and fix random time trigger around midnight (#6169)
This commit is contained in:
Theo Arends 2019-08-02 15:13:51 +02:00
parent 8875787935
commit 8f2ddea708

View File

@ -37,17 +37,20 @@
#define XDRV_09 9 #define XDRV_09 9
enum TimerCommands { CMND_TIMER, CMND_TIMERS const char kTimerCommands[] PROGMEM =
#ifdef USE_SUNRISE D_CMND_TIMER "|" D_CMND_TIMERS
, CMND_LATITUDE, CMND_LONGITUDE
#endif
};
const char kTimerCommands[] PROGMEM = D_CMND_TIMER "|" D_CMND_TIMERS
#ifdef USE_SUNRISE #ifdef USE_SUNRISE
"|" D_CMND_LATITUDE "|" D_CMND_LONGITUDE "|" D_CMND_LATITUDE "|" D_CMND_LONGITUDE
#endif #endif
; ;
void (* const TimerCommand[])(void) PROGMEM = {
&CmndTimer, &CmndTimers
#ifdef USE_SUNRISE
, &CmndLatitude, &CmndLongitude
#endif
};
uint16_t timer_last_minute = 60; uint16_t timer_last_minute = 60;
int8_t timer_window[MAX_TIMERS] = { 0 }; int8_t timer_window[MAX_TIMERS] = { 0 };
@ -219,7 +222,7 @@ void ApplyTimerOffsets(Timer *duskdawn)
duskdawn->time = timeBuffer; duskdawn->time = timeBuffer;
} }
String GetSun(uint8_t dawn) String GetSun(uint32_t dawn)
{ {
char stime[6]; char stime[6];
@ -232,7 +235,7 @@ String GetSun(uint8_t dawn)
return String(stime); return String(stime);
} }
uint16_t SunMinutes(uint8_t dawn) uint16_t SunMinutes(uint32_t dawn)
{ {
uint8_t hour[2]; uint8_t hour[2];
uint8_t minute[2]; uint8_t minute[2];
@ -265,13 +268,13 @@ void TimerEverySecond(void)
if (!RtcTime.hour && !RtcTime.minute && !RtcTime.second) { TimerSetRandomWindows(); } // Midnight if (!RtcTime.hour && !RtcTime.minute && !RtcTime.second) { TimerSetRandomWindows(); } // Midnight
if (Settings.flag3.timers_enable && (uptime > 60) && (RtcTime.minute != timer_last_minute)) { // Execute from one minute after restart every minute only once if (Settings.flag3.timers_enable && (uptime > 60) && (RtcTime.minute != timer_last_minute)) { // Execute from one minute after restart every minute only once
timer_last_minute = RtcTime.minute; timer_last_minute = RtcTime.minute;
int16_t time = (RtcTime.hour *60) + RtcTime.minute; int32_t time = (RtcTime.hour *60) + RtcTime.minute;
uint8_t days = 1 << (RtcTime.day_of_week -1); uint8_t days = 1 << (RtcTime.day_of_week -1);
for (uint32_t i = 0; i < MAX_TIMERS; i++) { for (uint32_t i = 0; i < MAX_TIMERS; i++) {
// if (Settings.timer[i].device >= devices_present) Settings.timer[i].data = 0; // Reset timer due to change in devices present // if (Settings.timer[i].device >= devices_present) Settings.timer[i].data = 0; // Reset timer due to change in devices present
Timer xtimer = Settings.timer[i]; Timer xtimer = Settings.timer[i];
uint16_t set_time = xtimer.time; int32_t set_time = xtimer.time;
#ifdef USE_SUNRISE #ifdef USE_SUNRISE
if ((1 == xtimer.mode) || (2 == xtimer.mode)) { // Sunrise or Sunset if ((1 == xtimer.mode) || (2 == xtimer.mode)) { // Sunrise or Sunset
ApplyTimerOffsets(&xtimer); ApplyTimerOffsets(&xtimer);
@ -300,24 +303,23 @@ void TimerEverySecond(void)
} }
} }
void PrepShowTimer(uint8_t index) void PrepShowTimer(uint32_t index)
{ {
char days[8] = { 0 };
char sign[2] = { 0 };
char soutput[80];
Timer xtimer = Settings.timer[index -1]; Timer xtimer = Settings.timer[index -1];
char days[8] = { 0 };
for (uint32_t i = 0; i < 7; i++) { for (uint32_t i = 0; i < 7; i++) {
uint8_t mask = 1 << i; uint8_t mask = 1 << i;
snprintf(days, sizeof(days), "%s%d", days, ((xtimer.days & mask) > 0)); snprintf(days, sizeof(days), "%s%d", days, ((xtimer.days & mask) > 0));
} }
char soutput[80];
soutput[0] = '\0'; soutput[0] = '\0';
if (devices_present) { if (devices_present) {
snprintf_P(soutput, sizeof(soutput), PSTR(",\"" D_JSON_TIMER_OUTPUT "\":%d"), xtimer.device +1); snprintf_P(soutput, sizeof(soutput), PSTR(",\"" D_JSON_TIMER_OUTPUT "\":%d"), xtimer.device +1);
} }
#ifdef USE_SUNRISE #ifdef USE_SUNRISE
char sign[2] = { 0 };
int16_t hour = xtimer.time / 60; int16_t hour = xtimer.time / 60;
if ((1 == xtimer.mode) || (2 == xtimer.mode)) { // Sunrise or Sunset if ((1 == xtimer.mode) || (2 == xtimer.mode)) { // Sunrise or Sunset
if (hour > 11) { if (hour > 11) {
@ -337,20 +339,11 @@ void PrepShowTimer(uint8_t index)
* Commands * Commands
\*********************************************************************************************/ \*********************************************************************************************/
bool TimerCommand(void) void CmndTimer(void)
{ {
char command[CMDSZ]; uint32_t index = XdrvMailbox.index;
char dataBufUc[XdrvMailbox.data_len]; if ((index > 0) && (index <= MAX_TIMERS)) {
bool serviced = true; uint32_t error = 0;
uint8_t index = XdrvMailbox.index;
UpperCase(dataBufUc, XdrvMailbox.data);
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kTimerCommands);
if (-1 == command_code) {
serviced = false; // Unknown command
}
else if ((CMND_TIMER == command_code) && (index > 0) && (index <= MAX_TIMERS)) {
uint8_t error = 0;
if (XdrvMailbox.data_len) { if (XdrvMailbox.data_len) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= MAX_TIMERS)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= MAX_TIMERS)) {
if (XdrvMailbox.payload == 0) { if (XdrvMailbox.payload == 0) {
@ -363,6 +356,8 @@ bool TimerCommand(void)
#if defined(USE_RULES)==0 && defined(USE_SCRIPT)==0 #if defined(USE_RULES)==0 && defined(USE_SCRIPT)==0
if (devices_present) { if (devices_present) {
#endif #endif
char dataBufUc[XdrvMailbox.data_len];
UpperCase(dataBufUc, XdrvMailbox.data);
StaticJsonBuffer<256> jsonBuffer; StaticJsonBuffer<256> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(dataBufUc); JsonObject& root = jsonBuffer.parseObject(dataBufUc);
if (!root.success()) { if (!root.success()) {
@ -453,7 +448,10 @@ bool TimerCommand(void)
ResponseJsonEnd(); ResponseJsonEnd();
} }
} }
else if (CMND_TIMERS == command_code) { }
void CmndTimers(void)
{
if (XdrvMailbox.data_len) { if (XdrvMailbox.data_len) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1)) {
Settings.flag3.timers_enable = XdrvMailbox.payload; Settings.flag3.timers_enable = XdrvMailbox.payload;
@ -463,11 +461,11 @@ bool TimerCommand(void)
} }
} }
Response_P(S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.flag3.timers_enable)); Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, GetStateText(Settings.flag3.timers_enable));
MqttPublishPrefixTopic_P(RESULT_OR_STAT, command); MqttPublishPrefixTopic_P(RESULT_OR_STAT, XdrvMailbox.command);
uint8_t jsflg = 0; uint32_t jsflg = 0;
uint8_t lines = 1; uint32_t lines = 1;
for (uint32_t i = 0; i < MAX_TIMERS; i++) { for (uint32_t i = 0; i < MAX_TIMERS; i++) {
if (!jsflg) { if (!jsflg) {
Response_P(PSTR("{\"" D_CMND_TIMERS "%d\":{"), lines++); Response_P(PSTR("{\"" D_CMND_TIMERS "%d\":{"), lines++);
@ -484,28 +482,28 @@ bool TimerCommand(void)
} }
mqtt_data[0] = '\0'; mqtt_data[0] = '\0';
} }
#ifdef USE_SUNRISE #ifdef USE_SUNRISE
else if (CMND_LONGITUDE == command_code) { void CmndLongitude(void)
{
if (XdrvMailbox.data_len) { if (XdrvMailbox.data_len) {
Settings.longitude = (int)(CharToFloat(XdrvMailbox.data) *1000000); Settings.longitude = (int)(CharToFloat(XdrvMailbox.data) *1000000);
} }
char lbuff[33]; char lbuff[33];
dtostrfd(((float)Settings.longitude) /1000000, 6, lbuff); dtostrfd(((float)Settings.longitude) /1000000, 6, lbuff);
Response_P(S_JSON_COMMAND_SVALUE, command, lbuff); Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, lbuff);
} }
else if (CMND_LATITUDE == command_code) {
void CmndLatitude(void)
{
if (XdrvMailbox.data_len) { if (XdrvMailbox.data_len) {
Settings.latitude = (int)(CharToFloat(XdrvMailbox.data) *1000000); Settings.latitude = (int)(CharToFloat(XdrvMailbox.data) *1000000);
} }
char lbuff[33]; char lbuff[33];
dtostrfd(((float)Settings.latitude) /1000000, 6, lbuff); dtostrfd(((float)Settings.latitude) /1000000, 6, lbuff);
Response_P(S_JSON_COMMAND_SVALUE, command, lbuff); Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, lbuff);
}
#endif
else serviced = false; // Unknown command
return serviced;
} }
#endif // USE_SUNRISE
/*********************************************************************************************\ /*********************************************************************************************\
* Presentation * Presentation
@ -782,7 +780,7 @@ bool Xdrv09(uint8_t function)
TimerEverySecond(); TimerEverySecond();
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = TimerCommand(); result = DecodeCommand(kTimerCommands, TimerCommand);
break; break;
} }
return result; return result;