Merge pull request 'gateway: execute array but echo each executed command' (#2) from gateway into master

Reviewed-on: #2
This commit is contained in:
Nicu Hodos 2022-10-25 14:02:46 +02:00
commit 369b27fbdb
2 changed files with 27 additions and 27 deletions

View File

@ -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:

View File

@ -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<JsonArray>();
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());
}
}