diff --git a/gateway/include/devices.h b/gateway/include/devices.h index ef4c115..4353af7 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -6,6 +6,12 @@ using namespace Ha; +typedef unordered_multimap mapswitches; + +mapswitches onSwitches; +mapswitches offSwitches; +unordered_map p1Switches; + auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266"); namespace OilTank { diff --git a/gateway/include/ha.h b/gateway/include/ha.h index abec285..6a56af9 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -109,7 +109,6 @@ namespace Ha { publisher(configTopic, ""); } }; - List Component::components; struct AbstractBuilder { static List builders; @@ -123,7 +122,6 @@ namespace Ha { builders.empty(); } }; - List AbstractBuilder::builders; template struct Builder : AbstractBuilder { @@ -198,7 +196,6 @@ namespace Ha { } }; - unordered_map Command::mapCommands; struct Button : Command { @@ -227,7 +224,7 @@ namespace Ha { if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; } - void publishState(bool state) { + void updateState(bool state) { publisher(stateTopic, state ? "ON" : "OFF"); } }; @@ -235,13 +232,11 @@ namespace Ha { struct Sensor : Component, StateConfig { const char* unitMeasure = nullptr; const char* valueTemplate = nullptr; - - Sensor() : Component(name, id, "sensor") { - withStateTopic(); - } + static unordered_map mapSensors; Sensor(const char* name, const char* id) : Component(name, id, "sensor") { withStateTopic(); + mapSensors.insert({ id, this }); } void buildUniqueId(char* uniqueId) override { @@ -267,6 +262,10 @@ namespace Ha { jsonDoc["state_topic"] = stateTopic; jsonDoc["suggested_display_precision"] = 2; } + + void updateState(const char* message) { + publisher(stateTopic, message); + } }; struct TemperatureSensor : Sensor { @@ -307,4 +306,9 @@ namespace Ha { // valueTemplate = "{{ value_json.sensor.pressure }}"; } }; + + List Component::components; + List AbstractBuilder::builders; + unordered_map Command::mapCommands; + unordered_map Sensor::mapSensors; } diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index 3290d17..69c82aa 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -4,14 +4,6 @@ using namespace std; Scheduler ts; -namespace Ha { - struct Switch; -} -typedef unordered_multimap mapswitches; -mapswitches onSwitches; -mapswitches offSwitches; -unordered_map p1Switches; - void turnLed(uint8_t led, bool on = true) { on ? digitalWrite(led, LOW) : digitalWrite(led, HIGH); } @@ -52,7 +44,7 @@ namespace Board { 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"]); + if (el) el->updateState((bool)rcSwitch["state"]); break; } case 2: @@ -61,11 +53,11 @@ namespace Board { 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); + x.second->updateState(true); }); range = offSwitches.equal_range(value); for_each(range.first, range.second, [](mapswitches::value_type& x){ - x.second->publishState(false); + x.second->updateState(false); }); } } @@ -74,10 +66,8 @@ namespace Board { 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); - + auto sensor = Sensor::mapSensors[id]; + if (sensor) sensor->updateState(message); } void publishResponse(JsonDocument& jsonDoc) {