From 473256d591ffe4603d8a13d3076c0c2dd8ae7c56 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 16 May 2021 03:19:23 +0300 Subject: [PATCH] delayed command & some mbus optimization --- lighthub/bright.cpp | 2 +- lighthub/item.cpp | 255 +++++++++++++++++++++++++++++++------------ lighthub/item.h | 17 +-- lighthub/itemCmd.h | 8 +- lighthub/textconst.h | 1 + 5 files changed, 202 insertions(+), 81 deletions(-) diff --git a/lighthub/bright.cpp b/lighthub/bright.cpp index 2806bc5..52c49ee 100644 --- a/lighthub/bright.cpp +++ b/lighthub/bright.cpp @@ -6,7 +6,7 @@ uint8_t getBright255(uint8_t percent255) #ifdef BRIGHT_LINEAR return percent255; #else - return pgm_read_byte_near(stepvar[percent255]); + return pgm_read_byte_near(&stepvar[percent255]); /* int val = stepvar[index]; if (val>255) val=255; diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 6c4781d..33f1673 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -102,6 +102,7 @@ int txt2subItem(char *payload) { else if (strcmp_P(payload, SAT_P) == 0) cmd = S_SAT; else if (strcmp_P(payload, TEMP_P) == 0) cmd = S_TEMP; else if (strcmp_P(payload, VAL_P) == 0) cmd = S_VAL; + else if (strcmp_P(payload, DEL_P) == 0) cmd = S_DELAYED; return cmd; } @@ -500,11 +501,26 @@ switch (suffixCode) { st.setSuffix(suffixCode); switch (cmd) { + + /* + case CMD_ON: + case CMD_OFF: + case CMD_TOGGLE: + case CMD_HALT: + case CMD_RESTORE: + case CMD_XON: + case CMD_XOFF: + case CMD_UP: case CMD_DN: - st.Int((int32_t)getInt((char **) &payload)); + { + + return Ctrl(st,subItem); + } break; + */ + case CMD_HSV: st.Cmd(0); case CMD_VOID: @@ -550,7 +566,7 @@ st.setSuffix(suffixCode); } return Ctrl(st,subItem); - } + } //Void command break; case CMD_UNKNOWN: //Not known command @@ -576,8 +592,11 @@ st.setSuffix(suffixCode); return Ctrl(st,subItem); } default: //some known command + { + int32_t intParam = getInt((char **) &payload); + if (intParam) st.Int(intParam); return Ctrl(st, subItem); - + } } //ctrl return 0; } @@ -637,7 +656,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion) int fr = freeRam(); - debugSerial<0) + { + timestampObj->valueint = millis()+cmd.getInt(); + timestampObj->subtype=cmd.getCmd(); + debugSerial<valueint<subtype=0; + debugSerial<0); @@ -980,10 +1022,10 @@ switch (itemType) { #ifndef MODBUS_DISABLE case CH_MODBUS: - if (toExecute) + if (toExecute && !(chActive && st.getCmd()==CMD_ON && !cmd.isValue())) { int vol; - if (st.getCmd()== CMD_ON && (vol=st.getPercents())=0) + if (!chActive && st.getCmd()== CMD_ON && (vol=st.getPercents())=0) { st.setPercents(INIT_VOLUME); st.saveItem(this); @@ -994,10 +1036,10 @@ switch (itemType) { } break; case CH_VC: - if (toExecute) VacomSetFan(st); + if (toExecute && !(chActive && st.getCmd()==CMD_ON && !cmd.isValue())) VacomSetFan(st); break; case CH_VCTEMP: - if (toExecute) VacomSetHeat(st); + if (toExecute && !(chActive && st.getCmd()==CMD_ON && !cmd.isValue())) VacomSetHeat(st); break; #endif /// rest of Legacy monolite core code (to be refactored ) END /// @@ -1097,42 +1139,36 @@ int Item::isActive() { if (val) return 1; else return 0; } -bool Item::resumeModbus() -{ - -if (modbusBusy) return false; -debugSerial<child; - while (items && item) - if (item->type == aJson_Array && aJson.getArraySize(item)>1) { - Item it(item); - if (it.isValid()) { - switch (it.itemType){ - case CH_MODBUS: - bool res = it.checkModbusRetry(); - debugSerial<name<next; - } //if -debugSerial<subtype; + int32_t remain = (uint32_t) timestampObj->valueint - (uint32_t)millis(); + + if (cmd) { + itemCmd st(ST_UINT32,cmd); + st.Int(remain); + + if (!(remain % 1000)) + { + SendStatusImmediate(st,SEND_DELAYED); + debugSerial<< remain/1000 << F(" sec remaining") << endl; + } + + if (remain <0 && abs(remain)< 0xFFFFFFFFUL/2) + { + SendStatusImmediate(st,SEND_DELAYED); + Ctrl(itemCmd(ST_VOID,cmd)); + timestampObj->subtype=0; + } + } +} + switch (cause) { case POLLING_SLOW: @@ -1149,11 +1185,13 @@ switch (cause) sendDelayedStatus(); return INTERVAL_SLOW_POLLING; break; + /* case CH_VCTEMP: checkHeatRetry(); sendDelayedStatus(); return INTERVAL_SLOW_POLLING; - break; + break; */ + #endif /* case CH_RGB: //All channels with slider generate too many updates case CH_RGBW: @@ -1274,17 +1312,21 @@ int Item::SendStatus(int sendFlags) { // myhome/s_out/item/cmd // myhome/s_out/item/set - if (sendFlags & SEND_PARAMETERS) + if ((sendFlags & SEND_PARAMETERS) || (sendFlags & SEND_DELAYED)) { setTopic(addrstr,sizeof(addrstr),T_OUT); strncat(addrstr, itemArr->name, sizeof(addrstr)-1); if (subItem) { - strncat(addrstr, "/", sizeof(addrstr)); + strncat(addrstr, "/", sizeof(addrstr)-1); strncat(addrstr, subItem, sizeof(addrstr)-1); } - strncat(addrstr, "/", sizeof(addrstr)); + strncat(addrstr, "/", sizeof(addrstr)-1); + + if (sendFlags & SEND_DELAYED) + strncat_P(addrstr, DEL_P, sizeof(addrstr)-1); + else strncat_P(addrstr, SET_P, sizeof(addrstr)-1); // Preparing parameters payload ////////// switch (st.getArgType()) { @@ -1295,17 +1337,11 @@ int Item::SendStatus(int sendFlags) { st.toString(valstr, sizeof(valstr), SEND_PARAMETERS|SEND_COMMAND); break; default: - st.toString(valstr, sizeof(valstr), SEND_PARAMETERS,(SCALE_VOLUME_100)); + st.toString(valstr, sizeof(valstr), (sendFlags & SEND_DELAYED)?SEND_COMMAND|SEND_PARAMETERS:SEND_PARAMETERS,(SCALE_VOLUME_100)); } - switch (st.getArgType()) { - case ST_RGB: - case ST_RGBW: - strncat_P(addrstr, SET_P, sizeof(addrstr)); - break; - default: - strncat_P(addrstr, SET_P, sizeof(addrstr)); - } + + debugSerial<")<getChanType(); return itemType; } + +// Setup SEND_RETRY flag to repeat unsucsessfull modbus tranzaction after release line +void Item::mb_fail(int result) { + debugSerial<Ctrl(val); + #ifndef MODBUS_DISABLE + else switch (itemType) + { + + case CH_MODBUS: + result=modbusDimmerSet(val); + break; + case CH_VC: + result=VacomSetFan(val); + break; + case CH_VCTEMP: + result=VacomSetHeat(val); + break; + default: + break; + } + #endif + + switch (result){ + case -1: return M_CLEAN; + case 0: + debugSerial<name<name<child; + while (items && item) + if (item->type == aJson_Array && aJson.getArraySize(item)>1) { + Item it(item); + if (it.isValid()) { + + short res = it.checkModbusRetry(); + if (res<=0) success = false; + + } //isValid + yield(); + item = item->next; + } //if +debugSerial< poll: 0A 03 08 34 00 0A 87 18 */ + #ifndef MODBUS_DISABLE +extern ModbusMaster node; + int Item::modbusDimmerSet(itemCmd st) { @@ -1438,17 +1565,6 @@ int Item::modbusDimmerSet(itemCmd st) } -void Item::mb_fail(int result) { - debugSerial<