From 77422d5f59a5864fdb7bf1b443d66e1d95b2638d Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 30 Sep 2025 18:47:29 +0200 Subject: [PATCH] use wrapper class for SensorId and overload operators so that it can be used both as number and string --- include/devices.h | 59 ++++++++----------- lib/Tiny/ContactSensor.h | 4 +- lib/Tiny/GenericSensor.h | 4 +- lib/Tiny/HumiditySensor.h | 4 +- lib/Tiny/TemperatureSensor.h | 4 +- lib/Tiny/Tiny.h | 35 +++++++---- lib/Tiny/TinySensor.h | 4 +- .../test_sensor_builder/sensor_builder.cpp | 16 ++--- 8 files changed, 63 insertions(+), 67 deletions(-) diff --git a/include/devices.h b/include/devices.h index fad2dd5..27e7696 100644 --- a/include/devices.h +++ b/include/devices.h @@ -15,46 +15,40 @@ unordered_map p1Switches; auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266"); -Sensor* buildRoomSensor(const char* id) { - DeviceConfig* device = &DeviceConfig::create(id) +auto roomSensor = Builder::instance(TEMP_SENSOR) + .asDevice(&DeviceConfig::create(TEMP_SENSOR) .withName("Servers room") .withManufacturer("Atmel") .withModel("AtTiny85") .withArea("Basement") - .withParent(gatewayDevice); - return Builder::instance(id) - .asDevice(device) - .withValueTemplate("{{ value_json.sensor.temperature }}") - .addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}) - .addDiagnostic(new BatterySensor{id, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.5)|round(2)*100/2)|int }}"}) - .build(); -} + .withParent(gatewayDevice)) + .withValueTemplate("{{ value_json.sensor.temperature }}") + .addDiagnostic(new VoltageSensor{TEMP_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}) + .addDiagnostic(new BatterySensor{TEMP_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.5)|round(2)*100/2)|int }}"}) + .build(); -Sensor* buildTankSensor(const char* id) { - DeviceConfig* device = &DeviceConfig::create(id) +auto tankSensor = Builder::instance(new Sensor{ "Level", OIL_SENSOR }) + .asDevice(&DeviceConfig::create(OIL_SENSOR) .withName("Oil tank") .withManufacturer("Arduino") .withModel("Pro Mini") .withArea("Basement") - .withParent(gatewayDevice); - return Builder::instance(new Sensor{ "Level", id }) - .asDevice(device) - .withUnitMeasure("%") + .withParent(gatewayDevice)) + .withUnitMeasure("%") + .withSensorStateClass(MEASUREMENT) + .withIcon("mdi:hydraulic-oil-level") + .withValueTemplate("{{ 100 - ((value_json.sensor.value-12)|float*100/120)|round(2) }}") + .addSecondary( + Builder::instance(new Sensor{ "Depth", OIL_SENSOR }) + .withDeviceClass("distance") + .withUnitMeasure("cm") .withSensorStateClass(MEASUREMENT) - .withIcon("mdi:hydraulic-oil-level") - .withValueTemplate("{{ 100 - ((value_json.sensor.value-12)|float*100/120)|round(2) }}") - .addSecondary( - Builder::instance(new Sensor{ "Depth", id }) - .withDeviceClass("distance") - .withUnitMeasure("cm") - .withSensorStateClass(MEASUREMENT) - .withValueTemplate("{{ value_json.sensor.value }}") - .build() - ) - .addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}) - .addDiagnostic(new BatterySensor{id, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-4.0)|round(2)*100/2.4)|int }}"}) - .build(); -} + .withValueTemplate("{{ value_json.sensor.value }}") + .build() + ) + .addDiagnostic(new VoltageSensor{OIL_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}) + .addDiagnostic(new BatterySensor{OIL_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-4.0)|round(2)*100/2.4)|int }}"}) + .build(); struct PollinSwitch : Switch { constexpr static const char* man = "Pollin"; @@ -143,8 +137,3 @@ Command* commands[] = { new PollinSwitch{"00011", 4, "homebox"}, new PollinSwitch{"11111", 4, "Train", "Playroom"} }; - -Sensor* sensors[] = { - buildRoomSensor("4"), - buildTankSensor("7") -}; diff --git a/lib/Tiny/ContactSensor.h b/lib/Tiny/ContactSensor.h index dd3e702..ab15b29 100644 --- a/lib/Tiny/ContactSensor.h +++ b/lib/Tiny/ContactSensor.h @@ -6,9 +6,7 @@ class ContactSensor: public TinySensor { SensorType sensorType = CONTACT; public: - ContactSensor(short id) : - TinySensor(id) { - } + ContactSensor(SensorId id) : TinySensor(id) {} void sendStateAndVoltage(bool state) { sendInfo(ID(id) | VCC(readVcc()) | STATE(!state) | TYPE(sensorType)); diff --git a/lib/Tiny/GenericSensor.h b/lib/Tiny/GenericSensor.h index 2522267..110a9e0 100644 --- a/lib/Tiny/GenericSensor.h +++ b/lib/Tiny/GenericSensor.h @@ -6,9 +6,7 @@ class GenericSensor : public TinySensor { SensorType sensorType = GENERIC; public: - GenericSensor(short id) : - TinySensor(id) { - } + GenericSensor(SensorId id) : TinySensor(id) {} void sendValueAndVoltage(int value) { sendInfo(ID(id) | VCC(readVcc()) | VALUE(value) | TYPE(sensorType)); diff --git a/lib/Tiny/HumiditySensor.h b/lib/Tiny/HumiditySensor.h index 44a7db1..de59f4f 100644 --- a/lib/Tiny/HumiditySensor.h +++ b/lib/Tiny/HumiditySensor.h @@ -6,9 +6,7 @@ class HumiditySensor : public TinySensor { SensorType sensorType = HUMIDITY; public: - HumiditySensor(short id) : - TinySensor(id) { - } + HumiditySensor(SensorId id) : TinySensor(id) {} void sendHumidityAndVoltage(int humidity) { sendInfo(ID(id) | VCC(readVcc()) | HUMIDITY(humidity) | TYPE(sensorType)); diff --git a/lib/Tiny/TemperatureSensor.h b/lib/Tiny/TemperatureSensor.h index f3669e7..3d49bce 100644 --- a/lib/Tiny/TemperatureSensor.h +++ b/lib/Tiny/TemperatureSensor.h @@ -6,9 +6,7 @@ class TemperatureSensor : public TinySensor { SensorType sensorType = TEMPERATURE; public: - TemperatureSensor(short id) : - TinySensor(id) { - } + TemperatureSensor(SensorId id) : TinySensor(id) {} void sendTempAndVoltage(int temp) { sendInfo(ID(id) | VCC(readVcc()) | TEMP(temp) | TYPE(sensorType)); diff --git a/lib/Tiny/Tiny.h b/lib/Tiny/Tiny.h index b2825c7..a7cae64 100644 --- a/lib/Tiny/Tiny.h +++ b/lib/Tiny/Tiny.h @@ -28,13 +28,28 @@ enum SensorType : unsigned short { CONTACT = 7 }; -enum SensorId : unsigned short { - WINDOW1 = 1, - WINDOW2 = 2, - WATER_SENSOR = 3, - TEMP_SENSOR = 4, - LIGHT_SENSOR = 5, - MOVEMENT_SENSOR = 6, - OIL_SENSOR = 7, - PRESENCE_SENSOR = 8 -}; +class SensorId { + uint8_t value = 0; + char strValue[4]; + +public: + SensorId(uint8_t id) { + value = id; + snprintf(strValue, 4, "%d", value); + } + + operator uint8_t() { + return value; + } + + operator const char*() { + return strValue; + } +} +WINDOW1(1), +WINDOW2(2), +WATER_SENSOR(3), +TEMP_SENSOR(4), +LIGHT_SENSOR(5), +MOVEMENT_SENSOR(6), +OIL_SENSOR(7); diff --git a/lib/Tiny/TinySensor.h b/lib/Tiny/TinySensor.h index 8864fa5..673c160 100644 --- a/lib/Tiny/TinySensor.h +++ b/lib/Tiny/TinySensor.h @@ -7,7 +7,7 @@ using TinySwitch::sendInfo; class TinySensor { protected: - short id; + uint8_t id; long readVcc() { // Read 1.1V reference against AVcc @@ -33,7 +33,7 @@ protected: } public: - TinySensor(short id) { + TinySensor(SensorId id) { this->id = id; } }; diff --git a/test/native/test_sensor_builder/sensor_builder.cpp b/test/native/test_sensor_builder/sensor_builder.cpp index f011daa..7b094f7 100644 --- a/test/native/test_sensor_builder/sensor_builder.cpp +++ b/test/native/test_sensor_builder/sensor_builder.cpp @@ -62,21 +62,21 @@ void test_overflow_voltage(void) { void test_temp_sensor(void) { StaticJsonDocument<200> jsonDoc; - unsigned long value = ID(SensorId::TEMP_SENSOR) | TEMP(210) | TYPE(SensorType::TEMPERATURE); + unsigned long value = ID(TEMP_SENSOR) | TEMP(210) | TYPE(SensorType::TEMPERATURE); TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; - TEST_ASSERT_EQUAL(SensorId::TEMP_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(TEMP_SENSOR, sensor["id"]); TEST_ASSERT_EQUAL(21, sensor["temperature"]); } void test_temp_sensor_with_voltage(void) { StaticJsonDocument<200> jsonDoc; - unsigned long value = ID(SensorId::TEMP_SENSOR) | TEMP(320) | TYPE(SensorType::TEMPERATURE) | VCC(2847L); + unsigned long value = ID(TEMP_SENSOR) | TEMP(320) | TYPE(SensorType::TEMPERATURE) | VCC(2847L); TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; - TEST_ASSERT_EQUAL(SensorId::TEMP_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(TEMP_SENSOR, sensor["id"]); TEST_ASSERT_EQUAL(32, sensor["temperature"]); JsonObject diagnostic = sensor["diagnostic"]; @@ -85,21 +85,21 @@ void test_temp_sensor_with_voltage(void) { void test_oil_sensor(void) { StaticJsonDocument<200> jsonDoc; - unsigned long value = ID(SensorId::OIL_SENSOR) | VALUE(150) | TYPE(SensorType::GENERIC); + unsigned long value = ID(OIL_SENSOR) | VALUE(150) | TYPE(SensorType::GENERIC); TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; - TEST_ASSERT_EQUAL(SensorId::OIL_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(OIL_SENSOR, sensor["id"]); TEST_ASSERT_EQUAL(150, sensor["value"]); } void test_oil_sensor_with_voltage(void) { StaticJsonDocument<200> jsonDoc; - unsigned long value = ID(SensorId::OIL_SENSOR) | TEMP(200) | TYPE(SensorType::GENERIC) | VCC(2847L); + unsigned long value = ID(OIL_SENSOR) | TEMP(200) | TYPE(SensorType::GENERIC) | VCC(2847L); TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); JsonObject sensor = jsonDoc["sensor"]; - TEST_ASSERT_EQUAL(SensorId::OIL_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(OIL_SENSOR, sensor["id"]); TEST_ASSERT_EQUAL(200, sensor["value"]); JsonObject diagnostic = sensor["diagnostic"];