diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 275386c..1e27c5e 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -452,7 +452,7 @@ void Input::dht22Poll() { setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT / 3); } #endif - +/* bool Input::executeCommand(aJsonObject* cmd, int8_t toggle, char* defCmd) { if (!cmd) return false; @@ -500,7 +500,8 @@ if (emit) debugSerial << emit->valuestring<< F(" -> ")<type == aJson_String) { -/* + +// * TODO implement #ifdef WITH_DOMOTICZ if (getIdxField()) @@ -511,7 +512,7 @@ TODO implement getIdxField()); } else #endif -*/ +* // { char addrstr[MQTT_TOPIC_LENGTH]; @@ -535,6 +536,7 @@ return false; } //switch type return false; } +*/ // TODO Polling via timed interrupt with CHECK_INTERRUPT cause bool Input::changeState(uint8_t newState, short cause) diff --git a/lighthub/inputs.h b/lighthub/inputs.h index 808ee06..98e9193 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -181,5 +181,5 @@ protected: char* getIdxField(); bool changeState(uint8_t newState, short cause); - bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL); + //bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL); }; diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index e3cc631..34bf668 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -14,23 +14,6 @@ extern ModbusMaster node; extern short modbusBusy; extern void modbusIdle(void) ; -/* -const char float_P[] PROGMEM = "i16"; -const char hsv_P[] PROGMEM = "i32"; -const char int_P[] PROGMEM = "u16"; -const char enum_P[] PROGMEM = "u32"; -const char format_P[] PROGMEM = "i8h"; -const char true_P[] PROGMEM = "i8l"; -const char false_P[] PROGMEM = "false"; - -const char 8E1_P[] PROGMEM = "8E1"; -const char 8N1_P[] PROGMEM = "8N1"; -const char int_P[] PROGMEM = "8O1"; -const char enum_P[] PROGMEM = "u32"; -const char format_P[] PROGMEM = "$i8h"; -const char true_P[] PROGMEM = "i8l"; -const char false_P[] PROGMEM = "false"; -*/ struct reg_t { const char verb[4]; @@ -40,7 +23,7 @@ struct reg_t struct serial_t { const char verb[4]; - const int mode; + const uint16_t mode; }; #define PAR_I16 1 @@ -68,28 +51,35 @@ const reg_t regSize_P[] PROGMEM = const serial_t serialModes_P[] PROGMEM = { - { "8E1", SERIAL_8E1 }, - { "8N1", SERIAL_8N1 }, - { "8O1", SERIAL_8O1 }, - { "8M1", SERIAL_8M1 }, - { "8S1", SERIAL_8S1 } + { "8E1", SERIAL_8E1},//(uint16_t) US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_EVEN }, + { "8N1", SERIAL_8N1}, + { "8E2", SERIAL_8E2}, + { "8N2", SERIAL_8N2}, + { "8O1", SERIAL_8O1}, + { "8O2", SERIAL_8O2}, + { "8M1", SERIAL_8M1}, + { "8S1", SERIAL_8S1} } ; #define serialModesNum sizeof(serialModes_P)/sizeof(serial_t) -int str2SerialParam(char * str) -{ - for(uint8_t i=0; i"); + for(uint8_t i=0; i (SERIAL_8N1); +} int str2regSize(char * str) { - for(uint8_t i=0; itype == aJson_String && serialParamObj->valuestring) store->serialParam = (UARTClass::UARTModes) str2SerialParam(serialParamObj->valuestring); + if (serialParamObj && serialParamObj->type == aJson_String) store->serialParam = str2SerialParam(serialParamObj->valuestring); else store->serialParam = SERIAL_8N1; aJsonObject * baudObj=aJson.getObjectItem(templateObj, "baud"); @@ -131,7 +121,6 @@ bool out_Modbus::getConfig() int out_Modbus::Setup() { -Serial.println("Modbus Init"); if (!store) store= (mbPersistent *)item->setPersistent(new mbPersistent); if (!store) return 0; store->timestamp=millis(); @@ -139,7 +128,7 @@ if (getConfig()) { //item->clearFlag(ACTION_NEEDED); //item->clearFlag(ACTION_IN_PROCESS); - debugSerial<itemArr->name<driverStatus = CST_INITIALIZED; return 1; } @@ -158,8 +147,6 @@ Serial.println("Modbus De-Init"); delete store; item->setPersistent(NULL); store = NULL; - -//driverStatus = CST_UNKNOWN; return 1; } @@ -226,7 +213,9 @@ if (store->pollingRegisters && !modbusBusy && (Status() == CST_INITIALIZED) && i { debugSerial<itemArr->name << endl; modbusBusy=1; - modbusSerial.begin(store->baud, store->serialParam); + //store->serialParam=(USARTClass::USARTModes) SERIAL_8N1; + modbusSerial.begin(store->baud, static_cast (store->serialParam)); + debugSerial<< store->baud << F("---")<< store->serialParam<getArg(0), modbusSerial); aJsonObject * reg = store->pollingRegisters->child; diff --git a/lighthub/modules/out_modbus.h b/lighthub/modules/out_modbus.h index 7f2d499..162992a 100644 --- a/lighthub/modules/out_modbus.h +++ b/lighthub/modules/out_modbus.h @@ -11,7 +11,7 @@ public: // int addr int8_t driverStatus; int baud; - UARTClass::UARTModes serialParam; + uint16_t serialParam; uint16_t pollingInterval; uint32_t timestamp; aJsonObject * pollingRegisters; diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index 53fc98a..0b76da9 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -23,6 +23,14 @@ e-mail anklimov@gmail.com #include "stdarg.h" #include +#include "item.h" +#include +extern int8_t configLocked; +extern int8_t ethernetIdleCount; +extern PubSubClient mqttClient; + + + #if defined(__SAM3X8E__) || defined(ARDUINO_ARCH_STM32) #include #endif @@ -512,5 +520,100 @@ bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t m } +bool executeCommand(aJsonObject* cmd, int8_t toggle, char* defCmd) +{ +switch (cmd->type) +{ + case aJson_String: //legacy - no action + break; + case aJson_Array: //array - recursive iterate + { + configLocked++; + aJsonObject * command = cmd->child; + while (command) + { + executeCommand(command,toggle,defCmd); + command = command->next; + } + configLocked--; + } + break; + 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 *irev = NULL; +aJsonObject *erev = NULL; + +if (toggle>0){ +aJsonObject *irev = aJson.getObjectItem(cmd, "irev"); +aJsonObject *erev = aJson.getObjectItem(cmd, "erev"); +} + +char * itemCommand; +if (irev && toggle && irev->type == aJson_String) itemCommand = irev->valuestring; +else 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; + else emitCommand = defCmd; + +//debugSerial << F("IN:") << (pin) << F(" : ") <valuestring<< F(" -> ")<valuestring<< F(" -> ")<type == aJson_String) { +/* +TODO implement +#ifdef WITH_DOMOTICZ + if (getIdxField()) + { (newValue) ? publishDataToDomoticz(0, emit, "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"On\"}", + : publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField()); getIdxField()) + : publishDataToDomoticz(0, emit, + "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}", + getIdxField()); + } else +#endif +*/ + +{ +char addrstr[MQTT_TOPIC_LENGTH]; +strncpy(addrstr,emit->valuestring,sizeof(addrstr)); +if (mqttClient.connected() && !ethernetIdleCount) +{ +if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); +mqttClient.publish(addrstr, emitCommand , true); +} +} +} // emit +if (item && itemCommand && item->type == aJson_String) { + //debugSerial <valuestring <valuestring); + if (it.isValid()) it.Ctrl(itemCommand, true); + } +return true; +} +default: +return false; +} //switch type +return false; +} + + + + + + + #pragma message(VAR_NAME_VALUE(debugSerial)) #pragma message(VAR_NAME_VALUE(SERIAL_BAUD)) diff --git a/lighthub/utils.h b/lighthub/utils.h index 3ae1310..3303e30 100644 --- a/lighthub/utils.h +++ b/lighthub/utils.h @@ -60,3 +60,4 @@ void printUlongValueToStr(char *valstr, unsigned long value); void scan_i2c_bus(); void softRebootFunc(); bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo = 0xFFFFFFFF); +bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);