#include <ArduinoJson.h>
#include "Tiny.h"

bool buildSensorJson(unsigned long value, JsonDocument& jsonDoc) {
    JsonObject sensor = jsonDoc.createNestedObject("sensor");
    sensor["id"] = ID(value);

    float voltage = (float)GET_VCC(value) / 1000;
    if (voltage != 0) {
        JsonObject diagnostic = sensor.createNestedObject("diagnostic");
        diagnostic["voltage"] = voltage;
    }

    switch (GET_TYPE(value)) {
    case SensorType::GENERIC:
        sensor["value"] = GET_VALUE(value);
        break;
    case SensorType::TEMPERATURE:
        sensor["temperature"] = (float)GET_TEMP(value) / 10;
        break;
    case SensorType::HUMIDITY:
        sensor["humidity"] = (float)GET_HUMIDITY(value) / 10;
        break;
    case SensorType::CONTACT:
        sensor["state"] = GET_STATE(value) ? "on" : "off";
        break;
    default:
        return false;
    }

    return true;
}
