refactor into smaller functions
This commit is contained in:
parent
cb95fbba71
commit
ac8dd2c807
@ -105,37 +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"]);
|
||||
// Serial.println((const char*)rcSwitch["value"]);
|
||||
}
|
||||
char output[256];
|
||||
serializeJson(jsonDoc, output);
|
||||
Serial.println(output);
|
||||
// blink();
|
||||
// delay(1000);
|
||||
}
|
||||
|
||||
void runJsonCommands(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"]);
|
||||
}
|
||||
char output[256];
|
||||
serializeJson(jsonDoc, output);
|
||||
Serial.println(output);
|
||||
blink();
|
||||
} else {
|
||||
Serial.println(err.c_str());
|
||||
runRcSwitchCommand(jsonDoc);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Serial.println(String(err.c_str()) + ": " + cmd);
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,6 +152,7 @@ void readCommand() {
|
||||
if (cmd.endsWith(",")) {
|
||||
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
||||
}
|
||||
runJsonCommand("[" + cmd + "]");
|
||||
cmd = "[" + cmd + "]";
|
||||
runJsonCommands(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user