82 lines
2.1 KiB
C++

#include <TaskScheduler.h>
#define SEND_PIN 12
#define RECEIVE_PIN 13
#define RED_LED LED_BUILTIN
// #define BLUE_LED 2
using namespace std;
Scheduler ts;
#include "wifi.h"
namespace Board {
Task tReadCommand(TASK_IMMEDIATE, TASK_FOREVER, [](){
if (serialReader.readLine(Serial) > 0) {
char* cmd = serialReader.getBuffer();
runJsonCommand(cmd);
}
}, &ts);
void turnOffLed(uint8_t led) {
digitalWrite(led, HIGH);
}
void setup() {
// Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
pinMode(RED_LED, OUTPUT);
turnOffLed(RED_LED);
// pinMode(BLUE_LED, OUTPUT);
// turnOffLed(BLUE_LED);
Wifi::setup();
Ota::setup();
Mqtt::setup();
tReadCommand.enable();
}
void loop() {
ts.execute();
}
void publishResponse(JsonDocument& jsonDoc) {
char message[255];
serializeJson(jsonDoc, message);
Mqtt::publish("homeassistant/sensor/rc-gateway/raw", message);
if (jsonDoc.containsKey("rcSwitch")) {
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
switch ((unsigned int)rcSwitch["protocol"]) {
case 1: {
// buildId returns a new pointer, should it be deleted, or string will take care of it?
string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]);
Ha::Switch* el = Mqtt::mapSwitches[id];
if (el != nullptr) {
el->publishState((bool)rcSwitch["state"]);
}
break;
}
case 2:
break;
default: {
unsigned long value = rcSwitch["value"];
auto range = Mqtt::onSwitches.equal_range(value);
for_each(range.first, range.second, [](unordered_multimap<unsigned long, Ha::Switch*>::value_type& x){
x.second->publishState(true);
});
range = Mqtt::offSwitches.equal_range(value);
for_each(range.first, range.second, [](unordered_multimap<unsigned long, Ha::Switch*>::value_type& x){
x.second->publishState(false);
});
}
}
}
}
void handleJsonError(JsonDocument& jsonError) {
char message[255];
serializeJson(jsonError, message);
Mqtt::publish("homeassistant/sensor/rc-gateway/raw", message);
}
}
// {"rcSwitch":{"protocol":1,"state":false,"group":"11111","channel":4}}