diff --git a/gateway/include/devices.h b/gateway/include/devices.h index 0809e01..46d395e 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -8,9 +8,9 @@ using namespace Ha; DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID})->withName("RC Gateway")->withManufacturer("Adafruit")->withModel("Huzzah Esp8266"); -struct OilTankRoomSensorBuilder { - static Sensor* build(const char* id) { - auto device = (new DeviceConfig{ id }) +namespace OilTank { + Sensor* buildRoomSensor(const char* id) { + auto device = DeviceConfig::create(id) ->withName("Oil tank room") ->withManufacturer("Atmel") ->withModel("AtTiny85") @@ -23,11 +23,9 @@ struct OilTankRoomSensorBuilder { .withDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_room_battery_voltage')|float-2.5)|round(2)*100/2)|int }}"}) .build(); } -}; -struct OilSensorBuilder { - static Sensor* build(const char* id) { - auto device = (new DeviceConfig{ id }) + Sensor* buildTankSensor(const char* id) { + auto device = DeviceConfig::create(id) ->withName("Oil tank") ->withManufacturer("Arduino") ->withModel("Pro Mini") @@ -48,7 +46,7 @@ struct OilSensorBuilder { .withDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"}) .build(); } -}; +} struct PollinSwitch : Switch { const char* group; @@ -68,7 +66,7 @@ struct PollinSwitch : Switch { } void onCommand(const char* msg) override { - (String{ "ON" }.equals(msg)) ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel); + strcmp("ON", msg) == 0 ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel); publisher(stateTopic, msg); } @@ -92,7 +90,7 @@ struct EasyHomeSwitch : Switch { void onCommand(const char* msg) override { mySwitch.setProtocol(4); - String{ "ON" }.equals(msg) ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24); + strcmp("ON", msg) == 0 ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24); publisher(stateTopic, msg); } }; diff --git a/gateway/include/ha.h b/gateway/include/ha.h index ad354ff..c8a4f33 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -19,7 +19,10 @@ namespace Ha { DeviceConfig* parent = nullptr; DeviceConfig(const char* id) : id(id) {} - DeviceConfig(DeviceConfig& d) : id(d.id), name(d.name), model(d.model), manufacturer(d.manufacturer), area(d.area), parent(d.parent) {} + + static DeviceConfig* create(const char* id) { + return new DeviceConfig{ id }; + } void buildConfig(JsonDocument& jsonDoc) { JsonObject device = jsonDoc.createNestedObject("device"); diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index 42fd882..317582b 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -48,43 +48,46 @@ namespace Board { ts.execute(); } + void parseSwitches(JsonDocument& jsonDoc) { + JsonObjectConst rcSwitch = jsonDoc["rcSwitch"]; + switch ((unsigned int)rcSwitch["protocol"]) { + case 1: { + string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]); + Ha::Switch* el = p1Switches[id]; + if (el) el->publishState((bool)rcSwitch["state"]); + break; + } + case 2: + break; + default: { + unsigned long value = rcSwitch["value"]; + auto range = onSwitches.equal_range(value); + for_each(range.first, range.second, [](mapswitches::value_type& x){ + x.second->publishState(true); + }); + range = offSwitches.equal_range(value); + for_each(range.first, range.second, [](mapswitches::value_type& x){ + x.second->publishState(false); + }); + } + } + } + + void parseSensors(JsonDocument& jsonDoc, char* message) { + JsonObjectConst json = jsonDoc["sensor"]; + string id = to_string((unsigned int)json["id"]); + char stateTopic[TOPIC_SIZE]; + sprintf(stateTopic, "homeassistant/sensor/%s/%s/state", MAIN_DEVICE_ID, id.c_str()); + Mqtt::publish(stateTopic, message); + + } + 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: { - string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]); - Ha::Switch* el = p1Switches[id]; - if (el != nullptr) { - el->publishState((bool)rcSwitch["state"]); - } - break; - } - case 2: - break; - default: { - unsigned long value = rcSwitch["value"]; - auto range = onSwitches.equal_range(value); - for_each(range.first, range.second, [](mapswitches::value_type& x){ - x.second->publishState(true); - }); - range = offSwitches.equal_range(value); - for_each(range.first, range.second, [](mapswitches::value_type& x){ - x.second->publishState(false); - }); - } - } - } - if (jsonDoc.containsKey("sensor")) { - JsonObjectConst json = jsonDoc["sensor"]; - string id = to_string((unsigned int)json["id"]); - char stateTopic[TOPIC_SIZE]; - sprintf(stateTopic, "homeassistant/sensor/%s/%s/state", MAIN_DEVICE_ID, id.c_str()); - Mqtt::publish(stateTopic, message); - } + if (jsonDoc.containsKey("rcSwitch")) parseSwitches(jsonDoc); + if (jsonDoc.containsKey("sensor")) parseSensors(jsonDoc, message); } void handleJsonError(JsonDocument& jsonError) { diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index a10e07e..bfa5bcc 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -12,12 +12,11 @@ namespace Mqtt { AsyncMqttClient client; - void publishInit(); - void disconnect(); Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []() { Serial.println("Connecting to MQTT..."); client.connect(); }, &ts); + void publishInit(); Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts); const char* mainTopic = "homeassistant/+/rc-gateway/#"; @@ -34,7 +33,7 @@ namespace Mqtt { Command* commands[] = { Builder