use map to find sensors

This commit is contained in:
Nicu Hodos 2024-05-19 11:50:19 +02:00
parent 6733a6678a
commit 1d99c73bdf
3 changed files with 23 additions and 23 deletions

View File

@ -6,6 +6,12 @@
using namespace Ha; using namespace Ha;
typedef unordered_multimap<unsigned long, Ha::Switch*> mapswitches;
mapswitches onSwitches;
mapswitches offSwitches;
unordered_map<string, Ha::Switch*> p1Switches;
auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266"); auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266");
namespace OilTank { namespace OilTank {

View File

@ -109,7 +109,6 @@ namespace Ha {
publisher(configTopic, ""); publisher(configTopic, "");
} }
}; };
List<Component> Component::components;
struct AbstractBuilder { struct AbstractBuilder {
static List<AbstractBuilder> builders; static List<AbstractBuilder> builders;
@ -123,7 +122,6 @@ namespace Ha {
builders.empty(); builders.empty();
} }
}; };
List<AbstractBuilder> AbstractBuilder::builders;
template <class T> template <class T>
struct Builder : AbstractBuilder { struct Builder : AbstractBuilder {
@ -198,7 +196,6 @@ namespace Ha {
} }
}; };
unordered_map<string, Command*> Command::mapCommands;
struct Button : Command { struct Button : Command {
@ -227,7 +224,7 @@ namespace Ha {
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
} }
void publishState(bool state) { void updateState(bool state) {
publisher(stateTopic, state ? "ON" : "OFF"); publisher(stateTopic, state ? "ON" : "OFF");
} }
}; };
@ -235,13 +232,11 @@ namespace Ha {
struct Sensor : Component, StateConfig<Sensor> { struct Sensor : Component, StateConfig<Sensor> {
const char* unitMeasure = nullptr; const char* unitMeasure = nullptr;
const char* valueTemplate = nullptr; const char* valueTemplate = nullptr;
static unordered_map<string, Sensor*> mapSensors;
Sensor() : Component(name, id, "sensor") {
withStateTopic();
}
Sensor(const char* name, const char* id) : Component(name, id, "sensor") { Sensor(const char* name, const char* id) : Component(name, id, "sensor") {
withStateTopic(); withStateTopic();
mapSensors.insert({ id, this });
} }
void buildUniqueId(char* uniqueId) override { void buildUniqueId(char* uniqueId) override {
@ -267,6 +262,10 @@ namespace Ha {
jsonDoc["state_topic"] = stateTopic; jsonDoc["state_topic"] = stateTopic;
jsonDoc["suggested_display_precision"] = 2; jsonDoc["suggested_display_precision"] = 2;
} }
void updateState(const char* message) {
publisher(stateTopic, message);
}
}; };
struct TemperatureSensor : Sensor { struct TemperatureSensor : Sensor {
@ -307,4 +306,9 @@ namespace Ha {
// valueTemplate = "{{ value_json.sensor.pressure }}"; // valueTemplate = "{{ value_json.sensor.pressure }}";
} }
}; };
List<Component> Component::components;
List<AbstractBuilder> AbstractBuilder::builders;
unordered_map<string, Command*> Command::mapCommands;
unordered_map<string, Sensor*> Sensor::mapSensors;
} }

View File

@ -4,14 +4,6 @@ using namespace std;
Scheduler ts; Scheduler ts;
namespace Ha {
struct Switch;
}
typedef unordered_multimap<unsigned long, Ha::Switch*> mapswitches;
mapswitches onSwitches;
mapswitches offSwitches;
unordered_map<string, Ha::Switch*> p1Switches;
void turnLed(uint8_t led, bool on = true) { void turnLed(uint8_t led, bool on = true) {
on ? digitalWrite(led, LOW) : digitalWrite(led, HIGH); on ? digitalWrite(led, LOW) : digitalWrite(led, HIGH);
} }
@ -52,7 +44,7 @@ namespace Board {
case 1: { case 1: {
string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]); string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]);
Ha::Switch* el = p1Switches[id]; Ha::Switch* el = p1Switches[id];
if (el) el->publishState((bool)rcSwitch["state"]); if (el) el->updateState((bool)rcSwitch["state"]);
break; break;
} }
case 2: case 2:
@ -61,11 +53,11 @@ namespace Board {
unsigned long value = rcSwitch["value"]; unsigned long value = rcSwitch["value"];
auto range = onSwitches.equal_range(value); auto range = onSwitches.equal_range(value);
for_each(range.first, range.second, [](mapswitches::value_type& x){ for_each(range.first, range.second, [](mapswitches::value_type& x){
x.second->publishState(true); x.second->updateState(true);
}); });
range = offSwitches.equal_range(value); range = offSwitches.equal_range(value);
for_each(range.first, range.second, [](mapswitches::value_type& x){ 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) { void parseSensors(JsonDocument& jsonDoc, char* message) {
JsonObjectConst json = jsonDoc["sensor"]; JsonObjectConst json = jsonDoc["sensor"];
string id = to_string((unsigned int)json["id"]); string id = to_string((unsigned int)json["id"]);
char stateTopic[TOPIC_SIZE]; auto sensor = Sensor::mapSensors[id];
sprintf(stateTopic, "homeassistant/sensor/%s/%s/state", MAIN_DEVICE_ID, id.c_str()); if (sensor) sensor->updateState(message);
Mqtt::publish(stateTopic, message);
} }
void publishResponse(JsonDocument& jsonDoc) { void publishResponse(JsonDocument& jsonDoc) {