From 4b5c6b930fbafecbfa716b0a60a7ace95568dd68 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 12 Oct 2022 21:26:52 +0200 Subject: [PATCH] don't send 0 voltage --- gateway/src/gateway.cpp | 57 ++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 17a4ed2..6d5d310 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -43,6 +43,36 @@ void loop() { } } +bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { + jsonDoc["id"] = ID(value); + + JsonObject sensor = jsonDoc.createNestedObject("sensor"); + + float voltage = GET_VCC(value); + if (voltage != 0) { + sensor["voltage"] = voltage; + } + + switch (GET_TYPE(value)) { + case SensorType::GENERIC: + sensor["value"] = GET_VALUE(value); + break; + case SensorType::TEMPERATURE: + sensor["temperature"] = GET_TEMP(value); + break; + case SensorType::HUMIDITY: + sensor["humidity"] = GET_HUMIDITY(value); + break; + case SensorType::CONTACT: + sensor["state"] = GET_STATE(value) ? "on" : "off"; + break; + default: + return false; + } + + return true; +} + void readRcSwitch(JsonDocument& jsonDoc) { if (mySwitch.available()) { unsigned long value = mySwitch.getReceivedValue(); @@ -59,32 +89,7 @@ void readRcSwitch(JsonDocument& jsonDoc) { motion["basement"] = value == 1879048230L ? "on" : "off"; return; } - if (GET_TYPE(value) == SensorType::GENERIC) { - JsonObject sensor = jsonDoc.createNestedObject("sensor"); - jsonDoc["id"] = ID(value); - sensor["value"] = GET_VALUE(value); - sensor["voltage"] = GET_VCC(value); - return; - } - if (GET_TYPE(value) == SensorType::TEMPERATURE) { - JsonObject sensor = jsonDoc.createNestedObject("sensor"); - jsonDoc["id"] = ID(value); - sensor["temperature"] = GET_TEMP(value); - sensor["voltage"] = GET_VCC(value); - return; - } - if (GET_TYPE(value) == SensorType::HUMIDITY) { - JsonObject sensor = jsonDoc.createNestedObject("sensor"); - jsonDoc["id"] = ID(value); - sensor["humidity"] = GET_HUMIDITY(value); - sensor["voltage"] = GET_VCC(value); - return; - } - if (GET_TYPE(value) == SensorType::CONTACT) { - JsonObject sensor = jsonDoc.createNestedObject("contact"); - jsonDoc["id"] = ID(value); - sensor["state"] = GET_STATE(value) ? "on" : "off"; - sensor["voltage"] = GET_VCC(value); + if (buildSensorJson(jsonDoc, value)) { return; } }