diff --git a/lighthub/colorchannel.cpp b/lighthub/colorchannel.cpp index 0779716..c125c73 100644 --- a/lighthub/colorchannel.cpp +++ b/lighthub/colorchannel.cpp @@ -62,7 +62,7 @@ case S_HSV: PixelCtrl(cmd, subItem, toExecute); return 1; case S_CMD: - item->setCmd(cmd.getCmd()); + //item->setCmd(cmd.getCmd()); switch (cmd.getCmd()) { case CMD_ON: diff --git a/lighthub/config.cpp b/lighthub/config.cpp index 9800232..80ac370 100644 --- a/lighthub/config.cpp +++ b/lighthub/config.cpp @@ -58,7 +58,7 @@ bool systemConfig::isValidSysConf() }; - bool systemConfig::getMAC() + bool systemConfig::getMAC() { if (!stream || !isValidSysConf()) return false; stream->seek(offsetof(systemConfigData,mac)); @@ -71,49 +71,74 @@ bool systemConfig::isValidSysConf() return isMacValid; } - - bool systemConfig::getMQTTpwd(char * buffer, uint16_t bufLen) + bool systemConfig::setMAC(macAddress& _mac) { - return 0; + if (!stream || !isValidSysConf()) return false; + stream->seek(offsetof(systemConfigData,mac)); + ///stream->write ((const uint8_t *)&_mac,sizeof(_mac)); + memcpy(mac, _mac, sizeof(mac)); + return true; + } + + char * systemConfig::getMQTTpwd(char * buffer, uint16_t bufLen) + { + if (!stream || !isValidSysConf()) return NULL; + stream->seek(offsetof(systemConfigData,MQTTpwd)); + if (stream->readBytesUntil(0,buffer,bufLen)) return buffer; + return NULL; } bool systemConfig::setMQTTpwd(char * pwd) { - return 0; + if (!stream || !isValidSysConf()) return false; + stream->seek(offsetof(systemConfigData,MQTTpwd)); + stream->print(pwd); + return stream->write(0); } - bool systemConfig::getOTApwd(char * buffer, uint16_t bufLen) + char * systemConfig::getOTApwd(char * buffer, uint16_t bufLen) { - return 0; - + if (!stream || !isValidSysConf()) return NULL; + stream->seek(offsetof(systemConfigData,OTApwd)); + if (stream->readBytesUntil(0,buffer,bufLen)) return buffer; + return NULL; } bool systemConfig::setOTApwd(char * pwd) { - return 0; + if (!stream || !isValidSysConf()) return false; + stream->seek(offsetof(systemConfigData,OTApwd)); + stream->print(pwd); + return stream->write(0); } - bool systemConfig::setMAC(macAddress mac) + + char * systemConfig::getServer(char * buffer, uint16_t bufLen) { - return 0; + if (!stream || !isValidSysConf()) return NULL; + stream->seek(offsetof(systemConfigData,configURL)); + if (stream->readBytesUntil(0,buffer,bufLen)) return buffer; + return NULL; } bool systemConfig::setServer(char* url) { - return 0; - } - - bool systemConfig::getServer(char* url) - { - return 0; + if (!stream || !isValidSysConf()) return false; + stream->seek(offsetof(systemConfigData,OTApwd)); + stream->print(url); + return stream->write(0); + } bool systemConfig::getIP(IPAddress& ip) { - return 0; + if (!stream || !isValidSysConf()) return false; + stream->seek(offsetof(systemConfigData,ip)); + stream->readBytes((char *)&ip,sizeof(ip)); + return ip; } diff --git a/lighthub/config.h b/lighthub/config.h index 57f6afc..bcf0a21 100644 --- a/lighthub/config.h +++ b/lighthub/config.h @@ -57,21 +57,20 @@ class systemConfig { macAddress mac; systemConfig() {stream=NULL;}; systemConfig(flashStream * fs){stream=fs;}; - //systemConfigData data; + bool isValidSysConf(); - //bool isValidJSON(); + bool getMAC(); - //inline macAddress * getMAC() {return &mac;}; + bool setMAC(macAddress& mac); - bool getMQTTpwd(char * buffer, uint16_t bufLen); + char * getMQTTpwd(char * buffer, uint16_t bufLen); bool setMQTTpwd(char * pwd = NULL); - bool getOTApwd(char * buffer, uint16_t bufLen); + char * getOTApwd(char * buffer, uint16_t bufLen); bool setOTApwd(char * pwd = NULL); - bool setMAC(macAddress mac); bool setServer(char* url); - bool getServer(char* url); + char * getServer(char * buffer, uint16_t bufLen); bool getIP(IPAddress& ip); bool getMask(IPAddress& mask); diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 4b3eaff..f54691e 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -127,7 +127,7 @@ void Item::Parse() { if (isValid()) { // Todo - avoid static enlarge for every types for (int i = aJson.getArraySize(itemArr); i < 4; i++) - aJson.addItemToArray(itemArr, aJson.createItem( (long int) 0)); + aJson.addItemToArray(itemArr, aJson.createNull());//( (long int) 0)); // int(defval[i]) )); //Enlarge item to 4 elements. VAL=int if no other definition in conf itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint; itemArg = aJson.getArrayItem(itemArr, I_ARG); @@ -301,7 +301,7 @@ void Item::setFlag (short flag) if (itemCmd) { itemCmd->valueint |= flag & FLAG_MASK; // Preserve CMD bits - debugSerial<valueint &= CMD_MASK | ~(flag & FLAG_MASK); // Preserve CMD bits - debugSerial<type == aJson_Int || itemVal->type == aJson_Float) return itemVal->subtype; + if (itemVal->type == aJson_Int || itemVal->type == aJson_Float || itemVal->type == aJson_NULL) return itemVal->subtype; else if (itemVal->type == aJson_Array) { aJsonObject *t = aJson.getArrayItem(itemVal, 0); if (t) return t->subtype; @@ -382,7 +382,7 @@ void Item::setVal(short n, int par) // Only store if VAL is array defined in c void Item::setVal(long int par) // Only store if VAL is int (autogenerated or config-defined) { - if (!itemVal || (itemVal->type != aJson_Int && itemVal->type != aJson_Float)) return; + if (!itemVal || (itemVal->type != aJson_Int && itemVal->type != aJson_Float && itemVal->type != aJson_NULL)) return; //debugSerial<type != aJson_Int)) return NULL; + if(!itemExt ) return NULL; + if(itemExt->type == aJson_NULL) itemExt->type=aJson_Int; + else if(itemExt->type != aJson_Int ) return NULL; + itemExt->valueint = 0; itemExt->child = (aJsonObject *) par; // debugSerial<subtype=0; - debugSerial<0); - bool toExecute = chActive; // execute if channel is active now + /// + int8_t chActive = -1; + bool toExecute = false; bool scale100 = false; + int res = -1; + uint16_t status2Send = 0; + uint8_t command2Set = 0; + + /// Common (GRP & NO GRP) commands + switch (cmd.getCmd()) + { + case CMD_TOGGLE: + chActive=(isActive()>0); + toExecute=true; + + if (chActive) cmd.Cmd(CMD_OFF); + else + { + cmd.loadItemDef(this); + cmd.Cmd(CMD_ON); + } + status2Send |=SEND_COMMAND | SEND_IMMEDIATE; + break; + // TBD - INCREMENT-DECREMENT + } - debugSerial<0 && !cmd.getInt()) st.Cmd(CMD_OFF); - if (chActive==0 && cmd.getInt()) st.Cmd(CMD_ON); - setCmd(st.getCmd()); - st.saveItem(this); - SendStatus(SEND_COMMAND | SEND_DEFFERED); - + if (chActive>0 && !cmd.getInt()) {cmd.Cmd(CMD_OFF);status2Send |= SEND_COMMAND | SEND_IMMEDIATE;} + if (chActive==0 && cmd.getInt()) {cmd.Cmd(CMD_ON);status2Send |= SEND_COMMAND | SEND_IMMEDIATE;} + // continue processing as SET case S_SET: + stored.loadItemDef(this); // if previous color was in RGB notation but new value is HSV - discard previous val and change type; - if ((st.getArgType() == ST_RGB || st.getArgType() == ST_RGBW) && + if ((stored.getArgType() == ST_RGB || stored.getArgType() == ST_RGBW) && (cmd.getArgType() == ST_HSV255)) - st.setArgType(cmd.getArgType()); + stored.setArgType(cmd.getArgType()); - if (itemType == CH_GROUP && cmd.isColor()) st.setArgType(ST_HSV255);//Extend storage for group channel + //if (itemType == CH_GROUP && cmd.isColor()) stored.setArgType(ST_HSV255);//Extend storage for group channel //Convert value to most approptiate type for channel - st.assignFrom(cmd,getChanType()); - if (scale100 || SCALE_VOLUME_100) st.scale100(); - st.saveItem(this); - SendStatus(SEND_PARAMETERS | SEND_DEFFERED); + stored.assignFrom(cmd,getChanType()); + if (scale100 || SCALE_VOLUME_100) stored.scale100(); + cmd=stored; + status2Send |= SEND_PARAMETERS | SEND_DEFFERED; break; case S_VAL: - st=cmd; break; case S_SAT: - //if (cmd.isColor()) st.convertTo(ST_HSV255);//Extend storage for color channel - if (st.setS(cmd.getS())) + stored.loadItemDef(this); + if (stored.setS(cmd.getS())) { - st.setSuffix(S_SET); - st.saveItem(this); - SendStatus(SEND_PARAMETERS | SEND_DEFFERED); + cmd=stored; + cmd.setSuffix(S_SET); + status2Send |= SEND_PARAMETERS | SEND_DEFFERED; } break; case S_HUE: - //if (cmd.isColor()) st.convertTo(ST_HSV255);//Extend storage for color channel - if (st.setH(cmd.getH())) + stored.loadItemDef(this); + if (stored.setH(cmd.getH())) { - st.setSuffix(S_SET); - st.saveItem(this); - SendStatus(SEND_PARAMETERS | SEND_DEFFERED); + cmd=stored; + cmd.setSuffix(S_SET); + status2Send |= SEND_PARAMETERS | SEND_DEFFERED; } break; case S_TEMP: - //st.setSuffix(suffixCode); - st.setColorTemp(cmd.getColorTemp()); - st.saveItem(this); - SendStatus(SEND_PARAMETERS | SEND_DEFFERED); + stored.loadItemDef(this); + stored.setColorTemp(cmd.getColorTemp()); + cmd=stored; + status2Send |= SEND_PARAMETERS | SEND_DEFFERED; } - break; - - default: - st.Cmd(cmd.getCmd()); - st.setSuffix(suffixCode); - toExecute=true; - } //Switch commands - -if (driver) //New style modular code - { - int res = -1; - switch (st.getCmd()) ///cmd///???????st ??? wtf - { + } + break; + case CMD_XON: if (!chActive) //if channel was'nt active before CMD_XON { - debugSerial<Ctrl(st, subItem); - setCmd(CMD_XON); - st.saveItem(this); - SendStatus(SEND_COMMAND); + cmd.Cmd(CMD_ON); + command2Set=CMD_XON; + status2Send |= SEND_COMMAND | SEND_IMMEDIATE; + toExecute=true; } else - { //cmd = CMD_ON; - debugSerial<Ctrl(st, subItem); - setCmd(CMD_HALT); - st.saveItem(this); - SendStatus(SEND_COMMAND); - return res; + cmd.Cmd(CMD_OFF); + command2Set=CMD_HALT; + status2Send |= SEND_COMMAND | SEND_IMMEDIATE; + toExecute=true; } else { - debugSerial<Ctrl(st, subItem); - setCmd(CMD_OFF); - st.saveItem(this); - SendStatus(SEND_COMMAND); - } - else - { - debugSerial<Ctrl(st, subItem, toExecute); - - break; - case CMD_ON: - //debugSerial<<"ON!"<Ctrl(st, subItem); - st.saveItem(this); - setCmd(st.getCmd()); - SendStatus(SEND_COMMAND); + case CMD_OFF: + if (getCmd() == CMD_HALT) return -3; //Halted, ignore OFF + status2Send |= SEND_COMMAND | SEND_IMMEDIATE; + toExecute=true; break; - default: //another command - if (cmd.isCommand()) st.Cmd(cmd.getCmd()); - //debugSerial<<"DEF!"<Ctrl(st, subItem); - st.saveItem(this); - if (st.isCommand()) - { - setCmd(st.getCmd()); - SendStatus(SEND_COMMAND); - } - } - debugSerial<Ctrl(cmd, subItem, toExecute); +else { switch (itemType) { - case CH_GROUP: - - if (allowRecursion && itemArg->type == aJson_Array && operation) - digGroup(itemArg,&cmd,subItem); - - break; - - - /// rest of Legacy monolite core code (to be refactored ) BEGIN /// case CH_RELAY: { short iaddr=getArg(); - short icmd =st.getCmd(); + short icmd =cmd.getCmd(); if (iaddr) { @@ -1040,10 +993,12 @@ switch (itemType) { pinMode(iaddr, OUTPUT); if (inverse) - digitalWrite(iaddr, k = ((icmd == CMD_ON || icmd == CMD_XON) ? LOW : HIGH)); + digitalWrite(iaddr, k = ((icmd == CMD_ON) ? LOW : HIGH)); else - digitalWrite(iaddr, k = ((icmd == CMD_ON || icmd == CMD_XON) ? HIGH : LOW)); + digitalWrite(iaddr, k = ((icmd == CMD_ON) ? HIGH : LOW)); debugSerial<name); - tStore.tempX100=st.getFloat()*100.; //Save measurement + tStore.tempX100=cmd.getFloat()*100.; //Save measurement tStore.timestamp16=millisNZ(8) & 0xFFFF; //And timestamp debugSerial<=0) + if (!chActive && cmd.getCmd()== CMD_ON && (vol=cmd.getPercents())=0) { - st.setPercents(INIT_VOLUME); - st.saveItem(this); - SendStatus(SEND_PARAMETERS | SEND_DEFFERED); + cmd.setPercents(INIT_VOLUME); + status2Send |= SEND_PARAMETERS | SEND_IMMEDIATE; }; - modbusDimmerSet(st); + res=modbusDimmerSet(cmd); } break; case CH_VC: - if (toExecute && !(chActive && st.getCmd()==CMD_ON && !cmd.isValue())) VacomSetFan(st); + if (toExecute && !(chActive && cmd.getCmd()==CMD_ON && !cmd.isValue())) res=VacomSetFan(cmd); break; case CH_VCTEMP: - if (toExecute && !(chActive && st.getCmd()==CMD_ON && !cmd.isValue())) VacomSetHeat(st); + if (toExecute && !(chActive && cmd.getCmd()==CMD_ON && !cmd.isValue())) res=VacomSetHeat(cmd); break; #endif /// rest of Legacy monolite core code (to be refactored ) END /// } //switch - if (st.isCommand()) - { - if (cmd.getCmd() == CMD_HALT) - { - if (chActive>0) //if channel was active before CMD_HALT - { - setCmd(CMD_HALT); - cmd.saveItem(this); /// - SendStatus(SEND_COMMAND); - } - } - else - { - setCmd(st.getCmd()); - st.saveItem(this); - SendStatus(SEND_COMMAND); - } - } - } -return 1; +} //else (nodriver) + + //update command for HALT & XON and send MQTT status + if (command2Set) setCmd(command2Set | SEND_COMMAND); + if (operation) SendStatus(status2Send); + +debugSerial<name; + debugSerial<name<type == aJson_Array) { - debugSerial<Ctrl(val); @@ -1638,7 +1588,7 @@ int Item::VacomSetFan(itemCmd st) { if (modbusBusy) { // setCmd(cmd); // setVal(val); - st.saveItem(this,true); + st.saveItem(this,SEND_PARAMETERS|SEND_COMMAND); mb_fail(); return 0; } @@ -1685,7 +1635,7 @@ int addr; if (modbusBusy) { //setCmd(cmd); //setVal(val); - st.saveItem(this,true); + st.saveItem(this,SEND_COMMAND|SEND_PARAMETERS); mb_fail(); return 0; } @@ -1883,47 +1833,7 @@ int Item::checkFM() { //resumeModbus(); return 1; } -/* -boolean Item::checkModbusRetry() { - if (modbusBusy) return false; - if (getFlag(SEND_RETRY)) { // if last sending attempt of command was failed - itemCmd val(ST_VOID,CMD_VOID); - val.loadItem(this, true); - debugSerial< AT:")<")<")<")<isValid()) { short subtype =item->getSubtype(); + if (optionsFlag & SEND_COMMAND) cmd.cmdCode = item->getCmd(); + if (subtype) { - param.asInt32=item->getVal(); cmd.itemArgType= subtype; - if (includeCommand) cmd.cmdCode=item->getCmd(); - // debugSerial<getVal(); + debugSerial<itemVal->type) - { - case aJson_Int: + + if (optionsFlag & SEND_PARAMETERS) + switch (item->itemVal->type) + { + case aJson_Int: - Int((int32_t)item->itemVal->valueint); - // debugSerial<itemVal->valueint); - // debugSerial<itemVal->valueint); + debugSerial<itemVal->valueint); + debugSerial<getChanType()); + setDefault(); + saveItem(item); + debugOut(); + item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED); + return false; + } +return true; +} + +bool itemCmd::saveItem(Item * item, uint16_t optionsFlag) { if (item && item->isValid()) { - item->setVal(param.asInt32); - item->setSubtype(cmd.itemArgType); - if (includeCommand) item->setCmd(cmd.cmdCode); + if (optionsFlag & SEND_COMMAND) item->setCmd(cmd.cmdCode); + if (optionsFlag & SEND_PARAMETERS) + { + item->setSubtype(cmd.itemArgType); + item->setVal(param.asInt32); + } debugSerial<1)) { // mqttClient.publish(topic, ""); cmd_parse((char *)payload); @@ -1419,7 +1411,7 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args) sysConf.setServer(configServer); //saveFlash(OFFSET_CONFIGSERVER, configServer); infoSerial< timerInputCheck) @@ -2130,7 +2122,7 @@ configLocked++; timerSensorCheck = millis();// + INTERVAL_CHECK_SENSOR; } configLocked--; -inputLoopBusy= false; +inputLoopBusy--; } diff --git a/lighthub/modules/out_motor.cpp b/lighthub/modules/out_motor.cpp index 4980ca8..09db0e2 100644 --- a/lighthub/modules/out_motor.cpp +++ b/lighthub/modules/out_motor.cpp @@ -298,7 +298,7 @@ case S_SET: //break; case S_CMD: - item->setCmd(cmd.getCmd()); + //item->setCmd(cmd.getCmd()); switch (cmd.getCmd()) { case CMD_ON: diff --git a/lighthub/modules/out_multivent.cpp b/lighthub/modules/out_multivent.cpp index bb481c8..49b940e 100644 --- a/lighthub/modules/out_multivent.cpp +++ b/lighthub/modules/out_multivent.cpp @@ -210,7 +210,7 @@ case S_SET: //break; case S_CMD: - item->setCmd(cmd.getCmd()); + //item->setCmd(cmd.getCmd()); switch (cmd.getCmd()) { case CMD_ON: diff --git a/lighthub/modules/out_pid.cpp b/lighthub/modules/out_pid.cpp index be7fcd9..fa0829f 100644 --- a/lighthub/modules/out_pid.cpp +++ b/lighthub/modules/out_pid.cpp @@ -200,8 +200,8 @@ case S_CMD: { case CMD_ON: case CMD_OFF: - item->setCmd(cmd.getCmd()); - item->SendStatus(SEND_COMMAND); + //item->setCmd(cmd.getCmd()); + //item->SendStatus(SEND_COMMAND); return 1; default: debugSerial<reset_search(); for (short i = 0; i < t_count; i++) wstat[i] &= ~SW_FIND; //absent @@ -67,7 +74,8 @@ int owUpdate() { debugSerial.println(F(" alive")); break; }; //alive - if (ifind < 0 && sensors) { + if (ifind < 0 && sensors && !zero(term[t_count],8)) + { wstat[t_count] = SW_FIND; //Newly detected debugSerial<=t_max) return; + if (zero(term[t_count],8)) return; + wstat[t_count] = SW_FIND; //Newly detected memcpy(term[t_count], addr, 8); - debugSerial<setStrongPullup(); @@ -238,6 +250,6 @@ void owAdd(DeviceAddress addr) { void setupOwIdle (void (*ptr)()) { #ifdef DS2482_100_I2C_TO_1W_BRIDGE - if (oneWire) oneWire->idle(ptr); + if (oneWire) oneWire->idle(ptr); #endif } diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index 94fe605..7f590ad 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -53,11 +53,11 @@ extern aJsonObject *topics; void PrintBytes(uint8_t *addr, uint8_t count, bool newline) { for (uint8_t i = 0; i < count; i++) { - Serial.print(addr[i] >> 4, HEX); - Serial.print(addr[i] & 0x0f, HEX); + infoSerial<< _HEX(addr[i] >> 4); + infoSerial<< _HEX(addr[i] & 0x0f); } if (newline) - Serial.println(); + infoSerial<