47 lines
1.3 KiB
C++
47 lines
1.3 KiB
C++
#pragma once
|
|
#include <ArduinoJson.h>
|
|
#include <RCSwitch.h>
|
|
|
|
struct RcData {
|
|
unsigned long value;
|
|
unsigned int bitLength;
|
|
unsigned int delay;
|
|
unsigned int* rawData;
|
|
};
|
|
|
|
class Protocol {
|
|
protected:
|
|
unsigned int protocol;
|
|
|
|
public:
|
|
Protocol(unsigned int protocol) {
|
|
this->protocol = protocol;
|
|
}
|
|
virtual ~Protocol() {}
|
|
|
|
virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) {
|
|
unsigned int protocol = rcSwitch["protocol"];
|
|
rcDevice.setProtocol(protocol);
|
|
rcDevice.send(rcSwitch["value"]);
|
|
}
|
|
|
|
virtual void toJson(const RcData& rcData, JsonDocument& jsonDoc) {
|
|
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
|
rcSwitch["protocol"] = protocol;
|
|
rcSwitch["value"] = rcData.value;
|
|
rcSwitch["bitLength"] = rcData.bitLength;
|
|
unsigned char rawData[rcData.bitLength*3];
|
|
outputRawData(rcData.bitLength, rcData.rawData, rawData);
|
|
rcSwitch["rawData"] = rawData;
|
|
}
|
|
|
|
private:
|
|
void outputRawData(unsigned int bitLength, const unsigned int* rawData, unsigned char* result) {
|
|
for (unsigned int i = 0; i <= bitLength * 2; i++) {
|
|
// serializeJson()
|
|
Serial.print(rawData[i]);
|
|
Serial.print(",");
|
|
}
|
|
}
|
|
};
|