refactor into smaller functions
This commit is contained in:
parent
cb95fbba71
commit
ac8dd2c807
@ -105,37 +105,38 @@ void blink() {
|
|||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void runJsonCommand(String cmd) {
|
void runRcSwitchCommand(JsonVariant jsonDoc) {
|
||||||
StaticJsonDocument<256> jsonArray;
|
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);
|
DeserializationError err = deserializeJson(jsonArray, cmd);
|
||||||
if (err == DeserializationError::Ok) {
|
if (err == DeserializationError::Ok) {
|
||||||
JsonArray array = jsonArray.as<JsonArray>();
|
JsonArray array = jsonArray.as<JsonArray>();
|
||||||
for (JsonVariant jsonDoc : array) {
|
for (JsonVariant jsonDoc : array) {
|
||||||
if (jsonDoc.containsKey("rcSwitch")) {
|
if (jsonDoc.containsKey("rcSwitch")) {
|
||||||
JsonObject rcSwitch = jsonDoc["rcSwitch"];
|
runRcSwitchCommand(jsonDoc);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println(String(err.c_str()) + ": " + cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,6 +152,7 @@ void readCommand() {
|
|||||||
if (cmd.endsWith(",")) {
|
if (cmd.endsWith(",")) {
|
||||||
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
||||||
}
|
}
|
||||||
runJsonCommand("[" + cmd + "]");
|
cmd = "[" + cmd + "]";
|
||||||
|
runJsonCommands(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user