53 lines
1.8 KiB
C++
53 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <RCSwitch.h>
|
|
#include "RcDecoder.h"
|
|
#include "JsonHA.h"
|
|
|
|
RCSwitch mySwitch = RCSwitch();
|
|
|
|
namespace Rc {
|
|
void setup() {
|
|
mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN));
|
|
mySwitch.enableTransmit(SEND_PIN);
|
|
mySwitch.setRepeatTransmit(10);
|
|
}
|
|
|
|
void readRcSwitch(JsonDocument& jsonDoc) {
|
|
if (mySwitch.available()) {
|
|
unsigned long value = mySwitch.getReceivedValue();
|
|
mySwitch.resetAvailable();
|
|
|
|
if (mySwitch.getReceivedProtocol() == 2) {
|
|
if (value == 637541753L || value == 771759481L) {
|
|
JsonObject motion = jsonDoc.createNestedObject("motion");
|
|
motion["kitchen"] = value == 637541753L ? "on" : "off";
|
|
return;
|
|
}
|
|
if (value == 1879048230L || value == 1879048198L) {
|
|
JsonObject motion = jsonDoc.createNestedObject("motion");
|
|
motion["basement"] = value == 1879048230L ? "on" : "off";
|
|
return;
|
|
}
|
|
if (JsonHA::buildSensor(jsonDoc, value)) {
|
|
return;
|
|
}
|
|
}
|
|
JsonHA::buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value);
|
|
}
|
|
}
|
|
|
|
void runRcSwitchCommand(JsonObject 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"]);
|
|
}
|
|
}
|
|
}
|