57 lines
1.8 KiB
C++
57 lines
1.8 KiB
C++
#include <ArduinoJson.h>
|
|
#include "Rc.h"
|
|
|
|
namespace SerialInput {
|
|
|
|
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")) {
|
|
runRcSwitchCommand(jsonDoc);
|
|
}
|
|
}
|
|
} else {
|
|
Serial.print(err.c_str());
|
|
Serial.print(": ");
|
|
Serial.println(origCmd);
|
|
}
|
|
}
|
|
|
|
void readCommand() {
|
|
if (Serial.available() > 0) {
|
|
String cmd = Serial.readStringUntil('\n');
|
|
if (cmd == "reset") {
|
|
Serial.println("resetting...");
|
|
delay(200);
|
|
digitalWrite(RESET_PIN, LOW);
|
|
Serial.println("resetting failed");
|
|
}
|
|
if (cmd.endsWith(",")) {
|
|
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
|
}
|
|
cmd = "[" + cmd + "]";
|
|
runJsonCommands(cmd.c_str());
|
|
}
|
|
}
|
|
} |