use wrapper class for SensorId and overload operators so that it can be used both as number and string
This commit is contained in:
parent
4ea9c83ff9
commit
77422d5f59
@ -15,46 +15,40 @@ unordered_map<string, Ha::Switch*> 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<TemperatureSensor>::instance(TEMP_SENSOR)
|
||||
.asDevice(&DeviceConfig::create(TEMP_SENSOR)
|
||||
.withName("Servers room")
|
||||
.withManufacturer("Atmel")
|
||||
.withModel("AtTiny85")
|
||||
.withArea("Basement")
|
||||
.withParent(gatewayDevice);
|
||||
return Builder<TemperatureSensor>::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<Sensor>::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<Sensor>::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<Sensor>::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<Sensor>::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")
|
||||
};
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
@ -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"];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user