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");
|
auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266");
|
||||||
|
|
||||||
Sensor* buildRoomSensor(const char* id) {
|
auto roomSensor = Builder<TemperatureSensor>::instance(TEMP_SENSOR)
|
||||||
DeviceConfig* device = &DeviceConfig::create(id)
|
.asDevice(&DeviceConfig::create(TEMP_SENSOR)
|
||||||
.withName("Servers room")
|
.withName("Servers room")
|
||||||
.withManufacturer("Atmel")
|
.withManufacturer("Atmel")
|
||||||
.withModel("AtTiny85")
|
.withModel("AtTiny85")
|
||||||
.withArea("Basement")
|
.withArea("Basement")
|
||||||
.withParent(gatewayDevice);
|
.withParent(gatewayDevice))
|
||||||
return Builder<TemperatureSensor>::instance(id)
|
.withValueTemplate("{{ value_json.sensor.temperature }}")
|
||||||
.asDevice(device)
|
.addDiagnostic(new VoltageSensor{TEMP_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
||||||
.withValueTemplate("{{ value_json.sensor.temperature }}")
|
.addDiagnostic(new BatterySensor{TEMP_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.5)|round(2)*100/2)|int }}"})
|
||||||
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
.build();
|
||||||
.addDiagnostic(new BatterySensor{id, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.5)|round(2)*100/2)|int }}"})
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
Sensor* buildTankSensor(const char* id) {
|
auto tankSensor = Builder<Sensor>::instance(new Sensor{ "Level", OIL_SENSOR })
|
||||||
DeviceConfig* device = &DeviceConfig::create(id)
|
.asDevice(&DeviceConfig::create(OIL_SENSOR)
|
||||||
.withName("Oil tank")
|
.withName("Oil tank")
|
||||||
.withManufacturer("Arduino")
|
.withManufacturer("Arduino")
|
||||||
.withModel("Pro Mini")
|
.withModel("Pro Mini")
|
||||||
.withArea("Basement")
|
.withArea("Basement")
|
||||||
.withParent(gatewayDevice);
|
.withParent(gatewayDevice))
|
||||||
return Builder<Sensor>::instance(new Sensor{ "Level", id })
|
.withUnitMeasure("%")
|
||||||
.asDevice(device)
|
.withSensorStateClass(MEASUREMENT)
|
||||||
.withUnitMeasure("%")
|
.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)
|
.withSensorStateClass(MEASUREMENT)
|
||||||
.withIcon("mdi:hydraulic-oil-level")
|
.withValueTemplate("{{ value_json.sensor.value }}")
|
||||||
.withValueTemplate("{{ 100 - ((value_json.sensor.value-12)|float*100/120)|round(2) }}")
|
.build()
|
||||||
.addSecondary(
|
)
|
||||||
Builder<Sensor>::instance(new Sensor{ "Depth", id })
|
.addDiagnostic(new VoltageSensor{OIL_SENSOR, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
||||||
.withDeviceClass("distance")
|
.addDiagnostic(new BatterySensor{OIL_SENSOR, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-4.0)|round(2)*100/2.4)|int }}"})
|
||||||
.withUnitMeasure("cm")
|
.build();
|
||||||
.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();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PollinSwitch : Switch {
|
struct PollinSwitch : Switch {
|
||||||
constexpr static const char* man = "Pollin";
|
constexpr static const char* man = "Pollin";
|
||||||
@ -143,8 +137,3 @@ Command* commands[] = {
|
|||||||
new PollinSwitch{"00011", 4, "homebox"},
|
new PollinSwitch{"00011", 4, "homebox"},
|
||||||
new PollinSwitch{"11111", 4, "Train", "Playroom"}
|
new PollinSwitch{"11111", 4, "Train", "Playroom"}
|
||||||
};
|
};
|
||||||
|
|
||||||
Sensor* sensors[] = {
|
|
||||||
buildRoomSensor("4"),
|
|
||||||
buildTankSensor("7")
|
|
||||||
};
|
|
||||||
|
|||||||
@ -6,9 +6,7 @@ class ContactSensor: public TinySensor {
|
|||||||
SensorType sensorType = CONTACT;
|
SensorType sensorType = CONTACT;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ContactSensor(short id) :
|
ContactSensor(SensorId id) : TinySensor(id) {}
|
||||||
TinySensor(id) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendStateAndVoltage(bool state) {
|
void sendStateAndVoltage(bool state) {
|
||||||
sendInfo(ID(id) | VCC(readVcc()) | STATE(!state) | TYPE(sensorType));
|
sendInfo(ID(id) | VCC(readVcc()) | STATE(!state) | TYPE(sensorType));
|
||||||
|
|||||||
@ -6,9 +6,7 @@ class GenericSensor : public TinySensor {
|
|||||||
SensorType sensorType = GENERIC;
|
SensorType sensorType = GENERIC;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GenericSensor(short id) :
|
GenericSensor(SensorId id) : TinySensor(id) {}
|
||||||
TinySensor(id) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendValueAndVoltage(int value) {
|
void sendValueAndVoltage(int value) {
|
||||||
sendInfo(ID(id) | VCC(readVcc()) | VALUE(value) | TYPE(sensorType));
|
sendInfo(ID(id) | VCC(readVcc()) | VALUE(value) | TYPE(sensorType));
|
||||||
|
|||||||
@ -6,9 +6,7 @@ class HumiditySensor : public TinySensor {
|
|||||||
SensorType sensorType = HUMIDITY;
|
SensorType sensorType = HUMIDITY;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HumiditySensor(short id) :
|
HumiditySensor(SensorId id) : TinySensor(id) {}
|
||||||
TinySensor(id) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendHumidityAndVoltage(int humidity) {
|
void sendHumidityAndVoltage(int humidity) {
|
||||||
sendInfo(ID(id) | VCC(readVcc()) | HUMIDITY(humidity) | TYPE(sensorType));
|
sendInfo(ID(id) | VCC(readVcc()) | HUMIDITY(humidity) | TYPE(sensorType));
|
||||||
|
|||||||
@ -6,9 +6,7 @@ class TemperatureSensor : public TinySensor {
|
|||||||
SensorType sensorType = TEMPERATURE;
|
SensorType sensorType = TEMPERATURE;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TemperatureSensor(short id) :
|
TemperatureSensor(SensorId id) : TinySensor(id) {}
|
||||||
TinySensor(id) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendTempAndVoltage(int temp) {
|
void sendTempAndVoltage(int temp) {
|
||||||
sendInfo(ID(id) | VCC(readVcc()) | TEMP(temp) | TYPE(sensorType));
|
sendInfo(ID(id) | VCC(readVcc()) | TEMP(temp) | TYPE(sensorType));
|
||||||
|
|||||||
@ -28,13 +28,28 @@ enum SensorType : unsigned short {
|
|||||||
CONTACT = 7
|
CONTACT = 7
|
||||||
};
|
};
|
||||||
|
|
||||||
enum SensorId : unsigned short {
|
class SensorId {
|
||||||
WINDOW1 = 1,
|
uint8_t value = 0;
|
||||||
WINDOW2 = 2,
|
char strValue[4];
|
||||||
WATER_SENSOR = 3,
|
|
||||||
TEMP_SENSOR = 4,
|
public:
|
||||||
LIGHT_SENSOR = 5,
|
SensorId(uint8_t id) {
|
||||||
MOVEMENT_SENSOR = 6,
|
value = id;
|
||||||
OIL_SENSOR = 7,
|
snprintf(strValue, 4, "%d", value);
|
||||||
PRESENCE_SENSOR = 8
|
}
|
||||||
};
|
|
||||||
|
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 {
|
class TinySensor {
|
||||||
protected:
|
protected:
|
||||||
short id;
|
uint8_t id;
|
||||||
|
|
||||||
long readVcc() {
|
long readVcc() {
|
||||||
// Read 1.1V reference against AVcc
|
// Read 1.1V reference against AVcc
|
||||||
@ -33,7 +33,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TinySensor(short id) {
|
TinySensor(SensorId id) {
|
||||||
this->id = id;
|
this->id = id;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -62,21 +62,21 @@ void test_overflow_voltage(void) {
|
|||||||
|
|
||||||
void test_temp_sensor(void) {
|
void test_temp_sensor(void) {
|
||||||
StaticJsonDocument<200> jsonDoc;
|
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));
|
TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
|
||||||
|
|
||||||
JsonObject sensor = jsonDoc["sensor"];
|
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"]);
|
TEST_ASSERT_EQUAL(21, sensor["temperature"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_temp_sensor_with_voltage(void) {
|
void test_temp_sensor_with_voltage(void) {
|
||||||
StaticJsonDocument<200> jsonDoc;
|
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));
|
TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
|
||||||
|
|
||||||
JsonObject sensor = jsonDoc["sensor"];
|
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"]);
|
TEST_ASSERT_EQUAL(32, sensor["temperature"]);
|
||||||
|
|
||||||
JsonObject diagnostic = sensor["diagnostic"];
|
JsonObject diagnostic = sensor["diagnostic"];
|
||||||
@ -85,21 +85,21 @@ void test_temp_sensor_with_voltage(void) {
|
|||||||
|
|
||||||
void test_oil_sensor(void) {
|
void test_oil_sensor(void) {
|
||||||
StaticJsonDocument<200> jsonDoc;
|
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));
|
TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
|
||||||
|
|
||||||
JsonObject sensor = jsonDoc["sensor"];
|
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"]);
|
TEST_ASSERT_EQUAL(150, sensor["value"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_oil_sensor_with_voltage(void) {
|
void test_oil_sensor_with_voltage(void) {
|
||||||
StaticJsonDocument<200> jsonDoc;
|
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));
|
TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
|
||||||
|
|
||||||
JsonObject sensor = jsonDoc["sensor"];
|
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"]);
|
TEST_ASSERT_EQUAL(200, sensor["value"]);
|
||||||
|
|
||||||
JsonObject diagnostic = sensor["diagnostic"];
|
JsonObject diagnostic = sensor["diagnostic"];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user