diff --git a/.drone.yml b/.drone.yml index 7c238b1..8fe9ac6 100644 --- a/.drone.yml +++ b/.drone.yml @@ -15,9 +15,6 @@ steps: - pio run -e pro-mini - echo -n 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v - service ser2net start - when: - target: - - production trigger: branch: diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index d819824..876cdef 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -33,8 +33,8 @@ void setup() { } void loop() { - StaticJsonDocument<200> jsonDoc; readCommand(); + StaticJsonDocument<200> jsonDoc; readRcSwitch(jsonDoc); Dht::read(jsonDoc); if (!jsonDoc.isNull()) { @@ -105,34 +105,38 @@ void blink() { digitalWrite(LED_BUILTIN, LOW); } -void runJsonCommand(String cmd) { - StaticJsonDocument<256> jsonArray; +void runRcSwitchCommand(JsonVariant jsonDoc) { + JsonObject rcSwitch = jsonDoc["rcSwitch"]; + unsigned int protocol = rcSwitch["protocol"]; + if (protocol == 1) { + mySwitch.setProtocol(protocol); + char* group = rcSwitch["group"]; + int channel = rcSwitch["channel"]; + rcSwitch["state"] ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel); + } else { + mySwitch.setProtocol(protocol); + mySwitch.send(rcSwitch["value"]); + } + serializeJson(jsonDoc, Serial); + Serial.println(); + // blink(); +} + +void runJsonCommands(const char* cmd) { + String origCmd = String(cmd); + StaticJsonDocument<512> jsonArray; DeserializationError err = deserializeJson(jsonArray, cmd); if (err == DeserializationError::Ok) { JsonArray array = jsonArray.as(); for (JsonVariant jsonDoc : array) { if (jsonDoc.containsKey("rcSwitch")) { - JsonObject rcSwitch = jsonDoc["rcSwitch"]; - unsigned int protocol = rcSwitch["protocol"]; - if (protocol == 1) { - mySwitch.setProtocol(protocol); - char* group = rcSwitch["group"]; - int channel = rcSwitch["channel"]; - if (rcSwitch["state"]) { - mySwitch.switchOn(group, channel); - } else { - mySwitch.switchOff(group, channel); - } - } else { - mySwitch.setProtocol(protocol); - mySwitch.send(rcSwitch["value"]); - // Serial.println((const char*)rcSwitch["value"]); - } - blink(); - } else { - Serial.println(err.c_str()); + runRcSwitchCommand(jsonDoc); } } + } else { + Serial.print(err.c_str()); + Serial.print(": "); + Serial.println(origCmd); } } @@ -149,7 +153,6 @@ void readCommand() { cmd = cmd.substring(0, cmd.lastIndexOf(',')); } cmd = "[" + cmd + "]"; - Serial.println(cmd); - runJsonCommand(cmd); + runJsonCommands(cmd.c_str()); } }