diff --git a/lighthub/candriver.cpp b/lighthub/candriver.cpp index 2f38da5..ba79fc2 100644 --- a/lighthub/candriver.cpp +++ b/lighthub/candriver.cpp @@ -156,12 +156,21 @@ return res; bool canDriver::begin() { - if (!root) return false; - canConfigObj = aJson.getObjectItem(root, "can"); - if (!canConfigObj) return false; - canRemoteConfigObj= aJson.getObjectItem(canConfigObj, "conf"); + if (root) + { + canConfigObj = aJson.getObjectItem(root, "can"); + if (canConfigObj) + { + canRemoteConfigObj= aJson.getObjectItem(canConfigObj, "conf"); + controllerId = getMyId(); + } + confCRC=getCRC(root); + } + - controllerId = getMyId(); + #ifndef NOIP + if (!canConfigObj) return false; + #endif if (!ready) // not reInitialization { @@ -314,7 +323,7 @@ switch (state) if (CANConfStream.peek() == '{') { debugSerial<name<<" "; ic.debugOut(); - if (ic.isCommand()) flags |= FLAG_COMMAND; if (ic.isValue()) flags |= FLAG_PARAMETERS; + if (ic.getSuffix()==S_DELAYED) flags |= FLAG_SEND_DELAYED; + else if (ic.isCommand()) flags |= FLAG_COMMAND; + ic.saveItem(&it,flags); - it.SendStatusImmediate(ic,flags | FLAG_NOT_SEND_CAN,it.getSubItemStrById(id.subItemId)); + it.SendStatusImmediate(ic,flags | FLAG_NOT_SEND_CAN, it.getSubItemStrById(id.subItemId)); return true; } @@ -465,7 +478,7 @@ if (id.status){ case canState::MACLookup: if ((id.payloadType == payloadType::lookupMAC)) { - if (canConfigObj && (id.subjId == getCRC(canConfigObj))) ///? + if (root && (id.subjId == confCRC)) ///? { infoSerial << (F("Valid config already onboard")) << endl; state = canState::Idle; @@ -661,8 +674,11 @@ return 0; } bool canDriver::write(uint32_t msg_id, datagram_t * buf, uint8_t size) - { //return 0; - if (!ready) errorSerial<<"CAN: not initialized"<8) size = 8; diff --git a/lighthub/candriver.h b/lighthub/candriver.h index 329a666..999b736 100644 --- a/lighthub/candriver.h +++ b/lighthub/candriver.h @@ -119,7 +119,7 @@ Error class canDriver { public: -canDriver(){ready=false; controllerId=0; responseTimer=0; state=canState::stateUnknown;canConfigObj=NULL;canRemoteConfigObj=NULL;}; +canDriver(){ready=false; controllerId=0; responseTimer=0; state=canState::stateUnknown;canConfigObj=NULL;canRemoteConfigObj=NULL;confCRC=0xFFFF;}; uint8_t getMyId(); bool sendStatus(uint16_t itemNum, itemCmd cmd, int subItem = NO_SUBITEM); bool sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool status=false, int subItemID=NO_SUBITEM ); @@ -143,6 +143,7 @@ uint8_t getControllerID(){return controllerId;}; uint8_t getIdByMac(macAddress mac); aJsonObject * canConfigObj; aJsonObject * canRemoteConfigObj; +uint16_t confCRC; datagram_t RXpacket; canid_t RXid; uint8_t RXlen; diff --git a/lighthub/flashstream.cpp b/lighthub/flashstream.cpp index 7b09186..b0d546b 100644 --- a/lighthub/flashstream.cpp +++ b/lighthub/flashstream.cpp @@ -287,6 +287,10 @@ NRFFlashStorage EEPROM; putEOF(); debugSerial<valueint; + /* itemType = replaceTypeToInt (aJson.getArrayItem(itemArr, I_TYPE)); itemArg = aJson.getArrayItem(itemArr, I_ARG); itemVal = aJson.getArrayItem(itemArr, I_VAL); itemExt = aJson.getArrayItem(itemArr, I_EXT); + */ + + aJsonObject * itemTypeObj = itemArr->child; + if (itemTypeObj) itemArg = itemTypeObj->next; + if (itemArg) itemVal = itemArg->next; + if (itemVal) itemExt = itemVal->next; + + itemType = replaceTypeToInt (itemTypeObj); + + switch (itemType) { #ifndef PWM_DISABLE @@ -861,7 +872,9 @@ bool Item::digGroup (aJsonObject *itemArr, itemCmd *cmd, char* subItem, bool aut aJsonObject *i = itemArr->child; configLocked++; while (i) { - if (i->type == aJson_String) + switch (i->type) + { + case aJson_String: { //debugSerial<< i->valuestring<valuestring); if (nextItem && nextItem->type == aJson_Array) //nextItem is correct item @@ -869,8 +882,9 @@ bool Item::digGroup (aJsonObject *itemArr, itemCmd *cmd, char* subItem, bool aut Item it(nextItem); if (cmd && it.isValid()) it.Ctrl(*cmd,subItem,false,authorized); //Execute (non recursive) //Retrieve itemType - aJsonObject * itemtype = aJson.getArrayItem(nextItem,0); - if (itemtype && itemtype->type == aJson_Int && itemtype->valueint == CH_GROUP) + //aJsonObject * itemtype = aJson.getArrayItem(nextItem,0); + //if (itemtype && itemtype->type == aJson_Int && itemtype->valueint == CH_GROUP) + if (it.itemType == CH_GROUP) { //is Group aJsonObject * itemSubArray = aJson.getArrayItem(nextItem,1); short res = digGroup(itemSubArray,cmd,subItem,authorized); @@ -888,6 +902,11 @@ bool Item::digGroup (aJsonObject *itemArr, itemCmd *cmd, char* subItem, bool aut } } } + break; + case aJson_Object: + case aJson_Array: + executeCommand(i,-1,*cmd); + }//switch i = i->next; } //while configLocked--; diff --git a/lighthub/main.cpp b/lighthub/main.cpp index b49dfde..520f708 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -177,11 +177,11 @@ int8_t mqttErrorRate=0; void watchdogSetup(void) {} //Do not remove - strong re-definition WDT Init for DUE #endif -bool cleanConf(bool wait) +bool cleanConf(short locksAlowed ) { if (!root) return true; bool clean = true; -if (configLocked) +if (configLocked>locksAlowed) { errorSerial<1)) { mqttClient.deleteTopic(topic); - if (forLocal)((char *)payload); + if (forLocal || forBcast)((char *)payload); //TODO implement for remote return;// -4; } @@ -518,7 +533,7 @@ else bool succeeded = false; - if (forLocal) + if (forLocal || forBcast) { Item item(itemName); if (item.isValid()) //Local item @@ -536,7 +551,8 @@ else Item remoteItem(itemName,remoteItems); if (remoteItem.Ctrl((char *)payload,NULL,remoteID)>0) succeeded = true; } - } + } + /// TODO bcast - scan CAN #endif if (succeeded && savedTopic[0] && lanStatus != RETAINING_COLLECTING) @@ -1446,8 +1462,8 @@ return 200; } #endif -void applyConfig() { - if (!root || configLocked) return; +bool applyConfig() { + if (!root || configLocked) return false; configLocked++; infoSerial<subtype & MB_NEED_SEND)) settedValue->valueint=param; @@ -696,7 +700,7 @@ int out_Modbus::Poll(short cause) if (cause==POLLING_SLOW) return 0; bool lineInitialized = false; -if (modbusBusy || (Status() != CST_INITIALIZED) || ( mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),100))) return 0; +if (modbusBusy || (Status() != CST_INITIALIZED) || ( mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),200))) return 0; aJsonObject * itemParametersObj = aJson.getArrayItem(item->itemArg, 2); if (itemParametersObj && itemParametersObj->type ==aJson_Object) @@ -731,7 +735,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object) debugSerial<<"MBUS: SEND "<itemArr->name<<" "; sendRes = sendModbus(execObj->name,outValue); needResend = (savedValue != outValue->valueint); - while(needResend && mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),100)) modbusIdle(); + while(needResend && mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),200)) modbusIdle(); } while (needResend); //repeat sending if target value changed while we're waited for mbus responce @@ -768,7 +772,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object) } -if (isTimeOver(store->timestamp,millis(),store->pollingInterval) && ( !mbusSlenceTimer || isTimeOver(mbusSlenceTimer,millis(),100))) +if (isTimeOver(store->timestamp,millis(),store->pollingInterval) && ( !mbusSlenceTimer || isTimeOver(mbusSlenceTimer,millis(),200))) { // Clean_up FLAG_SEND_ERROR flag diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index d44fb44..b3e80d9 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -983,7 +983,10 @@ uint16_t getCRC(aJsonObject * in) if (!in) return 0; CRCStream crcStream; aJsonStream aJsonCrcStream = aJsonStream(&crcStream); -aJson.print(in,&aJsonCrcStream); +debugSerial<<"CRC: in"; +debugSerial.print(aJson.print(in)); +aJson.print(in,&aJsonCrcStream,false); +debugSerial<<"\nCRC:"<