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;
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");
namespace OilTank {

View File

@ -109,7 +109,6 @@ namespace Ha {
publisher(configTopic, "");
}
};
List<Component> Component::components;
struct AbstractBuilder {
static List<AbstractBuilder> builders;
@ -123,7 +122,6 @@ namespace Ha {
builders.empty();
}
};
List<AbstractBuilder> AbstractBuilder::builders;
template <class T>
struct Builder : AbstractBuilder {
@ -198,7 +196,6 @@ namespace Ha {
}
};
unordered_map<string, Command*> 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<Sensor> {
const char* unitMeasure = nullptr;
const char* valueTemplate = nullptr;
Sensor() : Component(name, id, "sensor") {
withStateTopic();
}
static unordered_map<string, Sensor*> 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> 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;
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) {
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) {