From 0687949bb7ba84aa10c0eae88868b908ec1b9698 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Thu, 21 May 2020 15:36:35 +0300 Subject: [PATCH] API simplificatoin ph1 (@send param remove --- lighthub/abstractch.cpp | 2 +- lighthub/abstractout.h | 2 +- lighthub/inputs.cpp | 8 ++++---- lighthub/item.cpp | 34 +++++++++++++++++--------------- lighthub/item.h | 24 +++++++++++++++++----- lighthub/main.cpp | 10 +++++++--- lighthub/main.h | 1 + lighthub/modules/out_ac.cpp | 12 +++++------ lighthub/modules/out_ac.h | 2 +- lighthub/modules/out_modbus.cpp | 20 +++++++++++++------ lighthub/modules/out_modbus.h | 2 +- lighthub/modules/out_motor.cpp | 10 +++++----- lighthub/modules/out_motor.h | 2 +- lighthub/modules/out_spiled.cpp | 16 +++++++-------- lighthub/modules/out_spiled.h | 4 ++-- lighthub/textconst.h | 35 ++++++++++++++++++++++++++++++++- lighthub/utils.cpp | 34 +++++++++++++++++--------------- 17 files changed, 141 insertions(+), 77 deletions(-) diff --git a/lighthub/abstractch.cpp b/lighthub/abstractch.cpp index a71e1a9..57e1ade 100644 --- a/lighthub/abstractch.cpp +++ b/lighthub/abstractch.cpp @@ -26,7 +26,7 @@ int abstractCh::publishTopic(const char* topic, float value, const char* subtopi int abstractCh::publishTopic(const char* topic, const char * value, const char* subtopic) { char addrstr[MQTT_TOPIC_LENGTH]; - + if (!isNotRetainingStatus()) return 0; if (topic) { strncpy(addrstr,topic,sizeof(addrstr)); diff --git a/lighthub/abstractout.h b/lighthub/abstractout.h index dfd0c03..39c1bb2 100644 --- a/lighthub/abstractout.h +++ b/lighthub/abstractout.h @@ -7,7 +7,7 @@ class chPersistent {}; class abstractOut : public abstractCh{ public: abstractOut(Item * _item):abstractCh(){item=_item;}; - virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) =0; + virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) =0; virtual int isActive(){return 0;}; virtual int getDefaultOnVal(){return 100;}; virtual int getChanType(){return 0;} diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 1e27c5e..e87d5ee 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -952,13 +952,13 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri Item it(item->valuestring); if (it.isValid()) { if (newValue) { //send set command - if (!scmd || scmd->type != aJson_String) it.Ctrl(CMD_ON, 0, NULL, true); + if (!scmd || scmd->type != aJson_String) it.Ctrl(CMD_ON, 0, NULL); else if (strlen(scmd->valuestring)) - it.Ctrl(scmd->valuestring, true); + it.Ctrl(scmd->valuestring); } else { //send reset command - if (!rcmd || rcmd->type != aJson_String) it.Ctrl(CMD_OFF, 0, NULL, true); + if (!rcmd || rcmd->type != aJson_String) it.Ctrl(CMD_OFF, 0, NULL); else if (strlen(rcmd->valuestring)) - it.Ctrl(rcmd->valuestring, true); + it.Ctrl(rcmd->valuestring); } } } diff --git a/lighthub/item.cpp b/lighthub/item.cpp index e66f983..29996dc 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -456,9 +456,10 @@ return suffixCode; #define MAXCTRLPAR 3 // myhome/dev/item/subItem -int Item::Ctrl(char * payload, boolean send, char * subItem){ +int Item::Ctrl(char * payload, char * subItem){ if (!payload) return 0; +bool send = isNotRetainingStatus() ; int suffixCode = 0; //int setCommand = CMD_SET; //default SET behavior now - not turn on channels @@ -495,7 +496,7 @@ debugSerial<0) //if channel was'nt active before CMD_XON { debugSerial<Ctrl(CMD_ON, n, Par, send, suffixCode, subItem); + res = driver->Ctrl(CMD_ON, n, Par, suffixCode, subItem); setCmd(CMD_XON); } else @@ -768,7 +769,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int suffixCode case CMD_HALT: if (chActive>0) //if channel was active before CMD_HALT { - res = driver->Ctrl(CMD_OFF, n, Par, send, suffixCode, subItem); + res = driver->Ctrl(CMD_OFF, n, Par, suffixCode, subItem); setCmd(CMD_HALT); return res; } @@ -781,7 +782,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int suffixCode case CMD_OFF: if (getCmd() != CMD_HALT) //Halted, ignore OFF { - res = driver->Ctrl(cmd, n, Par, send, suffixCode, subItem); + res = driver->Ctrl(cmd, n, Par, suffixCode, subItem); setCmd(CMD_OFF); } else @@ -796,7 +797,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int suffixCode break; */ default: - res = driver->Ctrl(cmd, n, Par, send, suffixCode, subItem); + res = driver->Ctrl(cmd, n, Par, suffixCode, subItem); if (cmd) setCmd(cmd); } return res; @@ -1114,7 +1115,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int suffixCode if (i->type == aJson_String) { Item it(i->valuestring); - it.Ctrl(cmd, n, Par, send,suffixCode,subItem); //// was true + it.Ctrl(cmd, n, Par, suffixCode,subItem); //// was true } i = i->next; } //while @@ -1197,7 +1198,7 @@ return 1; } int Item::isActive() { - CHstore st; + itemStore st; int val = 0; @@ -1804,13 +1805,14 @@ void Item::sendDelayedStatus() int Item::SendStatus(int sendFlags) { - int chancmd=getCmd(); - if ((sendFlags & SEND_DEFFERED) || (lanStatus==RETAINING_COLLECTING)) { + + if ((sendFlags & SEND_DEFFERED) || (!isNotRetainingStatus() )) { setFlag(sendFlags & (SEND_COMMAND | SEND_PARAMETERS)); debugSerial<getChanType(); //retrive stored values diff --git a/lighthub/item.h b/lighthub/item.h index 3298bda..283e88d 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -120,6 +120,20 @@ extern short thermoSetCurTemp(char *name, float t); int txt2cmd (char * payload); +enum itemStoreType { +ST_VOID = 0, +ST_PERCENTS = 1, +ST_HS = 2, +ST_HSV = 3, +ST_FLOAT_CELSIUS= 4, +ST_FLOAT_FARENHEIT= 5, +ST_RGB = 6, +ST_RGBW = 7, +ST_PERCENTS255 = 8, +ST_HSV255 = 9 + +}; + #pragma pack(push, 1) typedef union { @@ -136,10 +150,10 @@ typedef union uint8_t r; uint8_t g; uint8_t b; - uint8_t w:7; - uint8_t rgb_flag:1; + uint8_t w;//:7; +// uint8_t rgb_flag:1; }; -} CHstore; +} itemStore; /* typedef union @@ -164,8 +178,8 @@ class Item boolean isValid (); boolean Setup(); - virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL); - virtual int Ctrl(char * payload, boolean send=true, char * subItem=NULL); + int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL); + int Ctrl(char * payload, char * subItem=NULL); int getArg(short n=0); //int getVal(short n); //From VAL array. Negative if no array diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 573c904..b1f6b6d 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -189,7 +189,7 @@ while (configLocked) #ifdef _dmxin DMXCheck(); #endif - if (lanStatus != RETAINING_COLLECTING) pollingLoop(); + if (isNotRetainingStatus()) pollingLoop(); thermoLoop(); inputLoop(); } @@ -215,6 +215,10 @@ debugSerial<= 10 && set_tmp <= 30) { data[B_SET_TMP] = set_tmp -16; - if (send) publishTopic(item->itemArr->name,(long) set_tmp,"/set"); + publishTopic(item->itemArr->name,(long) set_tmp,"/set"); } break; @@ -288,7 +288,7 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixC data[B_POWER] = power; data[B_POWER] |= 1; SendData(on, sizeof(on)/sizeof(byte)); - if (send) publishTopic(item->itemArr->name,"ON","/cmd"); + publishTopic(item->itemArr->name,"ON","/cmd"); return 1; break; case CMD_OFF: @@ -296,7 +296,7 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixC data[B_POWER] = power; data[B_POWER] &= ~1; SendData(off, sizeof(off)/sizeof(byte)); - if (send) publishTopic(item->itemArr->name,"OFF","/cmd"); + publishTopic(item->itemArr->name,"OFF","/cmd"); return 1; break; case CMD_AUTO: @@ -334,7 +334,7 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixC return -1; break; } - if (send) publishTopic(item->itemArr->name,s_mode,"/cmd"); + publishTopic(item->itemArr->name,s_mode,"/cmd"); break; case S_FAN: @@ -361,7 +361,7 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixC if (n) data[B_FAN_SPD] = Parameters[0]; //TODO - mapping digits to speed } - if (send) publishTopic(item->itemArr->name,s_mode,"/fan"); + publishTopic(item->itemArr->name,s_mode,"/fan"); break; case S_MODE: diff --git a/lighthub/modules/out_ac.h b/lighthub/modules/out_ac.h index 33a5247..33bd691 100644 --- a/lighthub/modules/out_ac.h +++ b/lighthub/modules/out_ac.h @@ -29,7 +29,7 @@ public: int Stop() override; int Status() override; int isActive() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) override; + int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; protected: void InsertData(byte data[], size_t size); diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index 34bf668..a78fa34 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -58,7 +58,15 @@ const serial_t serialModes_P[] PROGMEM = { "8O1", SERIAL_8O1}, { "8O2", SERIAL_8O2}, { "8M1", SERIAL_8M1}, - { "8S1", SERIAL_8S1} + { "8S1", SERIAL_8S1}, + { "7E1", SERIAL_7E1},//(uint16_t) US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_EVEN }, + { "7N1", SERIAL_7N1}, + { "7E2", SERIAL_7E2}, + { "7N2", SERIAL_7N2}, + { "7O1", SERIAL_7O1}, + { "7O2", SERIAL_7O2}, + { "7M1", SERIAL_7M1}, + { "7S1", SERIAL_7S1} } ; #define serialModesNum sizeof(serialModes_P)/sizeof(serial_t) @@ -275,7 +283,7 @@ int out_Modbus::getChanType() -int out_Modbus::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixCode, char* subItem) +int out_Modbus::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem) { int chActive = item->isActive(); bool toExecute = (chActive>0); @@ -314,12 +322,12 @@ case S_CMD: st = item->getVal(); - if (st && (stsetVal(st); if (st) //Stored smthng { - if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS); + item->SendStatus(SEND_COMMAND | SEND_PARAMETERS); debugSerial<setVal(st); - if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); + item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); } // if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time return 1; case CMD_OFF: - if (send) item->SendStatus(SEND_COMMAND); + item->SendStatus(SEND_COMMAND); // if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time return 1; diff --git a/lighthub/modules/out_modbus.h b/lighthub/modules/out_modbus.h index 162992a..af150c2 100644 --- a/lighthub/modules/out_modbus.h +++ b/lighthub/modules/out_modbus.h @@ -31,7 +31,7 @@ public: int Status() override; int isActive() override; int getChanType() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) override; + int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; protected: diff --git a/lighthub/modules/out_motor.cpp b/lighthub/modules/out_motor.cpp index 8efba95..bf6f5c0 100644 --- a/lighthub/modules/out_motor.cpp +++ b/lighthub/modules/out_motor.cpp @@ -210,7 +210,7 @@ int out_Motor::getChanType() -int out_Motor::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixCode, char* subItem) +int out_Motor::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem) { int chActive = item->isActive(); bool toExecute = (chActive>0); @@ -249,12 +249,12 @@ case S_CMD: st = item->getVal(); - if (st && (stsetVal(st); if (st) //Stored smthng { - if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS); + item->SendStatus(SEND_COMMAND | SEND_PARAMETERS); debugSerial<setVal(st); - if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); + item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); } if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time return 1; case CMD_OFF: - if (send) item->SendStatus(SEND_COMMAND); + item->SendStatus(SEND_COMMAND); if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time return 1; diff --git a/lighthub/modules/out_motor.h b/lighthub/modules/out_motor.h index fcb62ee..156d623 100644 --- a/lighthub/modules/out_motor.h +++ b/lighthub/modules/out_motor.h @@ -25,7 +25,7 @@ public: int Status() override; int isActive() override; int getChanType() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) override; + int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; int8_t pinUp; int8_t pinDown; diff --git a/lighthub/modules/out_spiled.cpp b/lighthub/modules/out_spiled.cpp index 921046c..df6e9df 100644 --- a/lighthub/modules/out_spiled.cpp +++ b/lighthub/modules/out_spiled.cpp @@ -88,7 +88,7 @@ return driverStatus; int out_SPILed::isActive() { -CHstore st; +itemStore st; st.aslong = item->getVal(); //Restore old params debugSerial<< F(" val:")<isActive(); bool toExecute = (chActive>0); -CHstore st; +itemStore st; if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it int from=0, to=numLeds-1; //All LEDs on the strip by default @@ -287,12 +287,12 @@ case S_CMD: PixelCtrl(&st,CMD_ON,from,to); else //whole strip { - if (st.aslong && (st.vsetVal(st.aslong); if (st.aslong ) //Stored smthng { - if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS); + item->SendStatus(SEND_COMMAND | SEND_PARAMETERS); debugSerial<setVal(st.aslong); - if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); + item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); } PixelCtrl(&st,0,from,to); @@ -319,7 +319,7 @@ case S_CMD: { st.v=0; PixelCtrl(&st,0,from,to); - if (send) item->SendStatus(SEND_COMMAND); + item->SendStatus(SEND_COMMAND); // if (send) item->publishTopic(item->itemArr->name,"OFF","/cmd"); } return 1; diff --git a/lighthub/modules/out_spiled.h b/lighthub/modules/out_spiled.h index 40c4f21..240bdd1 100644 --- a/lighthub/modules/out_spiled.h +++ b/lighthub/modules/out_spiled.h @@ -21,8 +21,8 @@ public: int Status() override; int isActive() override; int getChanType() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) override; - int PixelCtrl(CHstore *st, short cmd, int from =0 , int to = 1024, bool show = 1, bool rgb = 0); + int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; + int PixelCtrl(itemStore *st, short cmd, int from =0 , int to = 1024, bool show = 1, bool rgb = 0); int numLeds; int8_t pin; int ledsType; diff --git a/lighthub/textconst.h b/lighthub/textconst.h index 681700a..2ff1b13 100644 --- a/lighthub/textconst.h +++ b/lighthub/textconst.h @@ -5,12 +5,15 @@ const char disconnected_P[] PROGMEM = "disconnected"; const char ready_P[] PROGMEM = "ready"; const char homie_P[] PROGMEM = "$homie"; -const char homiever_P[] PROGMEM = "2.1.0"; +const char homiever_P[] PROGMEM = "3.0.1"; const char name_P[] PROGMEM = "$name"; const char nameval_P[] PROGMEM = "LightHub "; const char nodes_P[] PROGMEM = "$nodes"; +const char properties_P[] PROGMEM = "$properties"; +const char propertiesVal_P[] PROGMEM = "set,cmd"; +const char settable_P[] PROGMEM = "settable"; const char localip_P[] PROGMEM = "$localip"; const char mac_P[] PROGMEM = "$mac"; const char fwname_P[] PROGMEM = "$fw/name"; @@ -37,6 +40,36 @@ const char statsval_P[] PROGMEM = "uptime,freeheap"; const char uptime_P[] PROGMEM = "uptime"; const char freeheap_P[] PROGMEM = "freeheap"; +/* + +{"name": "Спальня LED", +"command_topic": "myhome/in/bedr/cmd", +"state_topic": "myhome/s_out/bedr/cmd", +"hs_command_topic": "myhome/in/bedr/set", +"hs_state_topic": "myhome/s_out/bedr/set", +"hs_value_template": "{%set a=value.split(',')%} {% if a[1] -%} {{a[0]+','+a[1]}} {%- endif %}", +"brightness_scale": 100, +"brightness_command_topic": "myhome/in/bedr/set", +"brightness_state_topic": "myhome/s_out/bedr/set", +"brightness_value_template": "{{ value.split(',')[2] }}" +} + +{ +"~":"myhome" +"name": "Спальня LED", +"cmd_t": "~/in/bedr/cmd", +"tat_t": "~/s_out/bedr/cmd", +"hs_cmd_t": "~/in/bedr/set", +"hs_stat_t": "~/s_out/bedr/set", +"hs_val_tpl": "{%set a=value.split(',')%}{% if a[1] -%}{{a[0]+','+a[1]}}{%- endif %}", +"bri_scl": 100, +"bri_cmd_t": "~/in/bedr/set", +"bri_stat_t": "~/s_out/bedr/set", +"bri_val_tpl": "{{ value.split(',')[2] }}" +} + +*/ + //Commands const char ON_P[] PROGMEM = "ON"; diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index 0b76da9..fb1d2c3 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -541,28 +541,30 @@ switch (cmd->type) case aJson_Object: { aJsonObject *item = aJson.getObjectItem(cmd, "item"); -aJsonObject *icmd = aJson.getObjectItem(cmd, "icmd"); - aJsonObject *emit = aJson.getObjectItem(cmd, "emit"); -aJsonObject *ecmd = aJson.getObjectItem(cmd, "ecmd"); +aJsonObject *icmd = NULL; +aJsonObject *ecmd = NULL; - -aJsonObject *irev = NULL; -aJsonObject *erev = NULL; - -if (toggle>0){ -aJsonObject *irev = aJson.getObjectItem(cmd, "irev"); -aJsonObject *erev = aJson.getObjectItem(cmd, "erev"); -} +switch (toggle) + { + case 0: + icmd = aJson.getObjectItem(cmd, "icmd"); + ecmd = aJson.getObjectItem(cmd, "ecmd"); + break; + case 1: + icmd = aJson.getObjectItem(cmd, "irev"); + ecmd = aJson.getObjectItem(cmd, "erev"); + //no *rev parameters - fallback + if (!icmd) icmd = aJson.getObjectItem(cmd, "icmd"); + if (!ecmd) ecmd = aJson.getObjectItem(cmd, "ecmd"); + } char * itemCommand; -if (irev && toggle && irev->type == aJson_String) itemCommand = irev->valuestring; -else if(icmd && icmd->type == aJson_String) itemCommand = icmd->valuestring; +if(icmd && icmd->type == aJson_String) itemCommand = icmd->valuestring; else itemCommand = defCmd; char * emitCommand; -if (erev && toggle && erev->type == aJson_String) emitCommand = erev->valuestring; -else if(ecmd && ecmd->type == aJson_String) emitCommand = ecmd->valuestring; +if(ecmd && ecmd->type == aJson_String) emitCommand = ecmd->valuestring; else emitCommand = defCmd; //debugSerial << F("IN:") << (pin) << F(" : ") <type == aJson_String) { //debugSerial <valuestring <valuestring); - if (it.isValid()) it.Ctrl(itemCommand, true); + if (it.isValid()) it.Ctrl(itemCommand); } return true; }