rc-gateway/gateway/include/Protocol_1.h

69 lines
1.8 KiB
C++

# pragma once
#include "Protocol.h"
#define RC_STATE(value) value & 0x1
#define RC_DEVICE(value) (value >> 1) & 0x1F
#define RC_GROUP(value) (value >> 6) & 0x1F
class Protocol_1 : public Protocol {
public:
Protocol_1() : Protocol(1) {
}
void fromJson(JsonObjectConst rcSwitch, RCSwitch& rcDevice) override {
unsigned int protocol = rcSwitch["protocol"];
rcDevice.setProtocol(protocol);
char* group = rcSwitch["group"];
int channel = rcSwitch["channel"];
rcSwitch["state"] ? rcDevice.switchOn(group, channel) : rcDevice.switchOff(group, channel);
}
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
rcSwitch["protocol"] = 1;
Decoder decoder;
decoder.decode(value);
rcSwitch["state"] = decoder.state;
rcSwitch["group"] = String(decoder.group, BIN);
rcSwitch["channel"] = decoder.device;
}
private:
struct Decoder {
bool state;
char group;
byte device;
void decode(unsigned long value) {
value = value >> 2;
unsigned long res = 0;
for (int i = 0; i < 12; i++) {
res |= ((value & 1) ^ 1) << i;
value = value >> 2;
}
state = RC_STATE(res);
group = RC_GROUP(res);
switch (RC_DEVICE(res)) {
case 0b10000:
device = 1;
break;
case 0b01000:
device = 2;
break;
case 0b00100:
device = 3;
break;
case 0b00010:
device = 4;
break;
case 0b00001:
device = 5;
break;
}
}
};
} protocol_1;