changes to backend, some refactoring

This commit is contained in:
Proddy
2022-04-15 13:04:52 +02:00
parent 9e293136b9
commit 4b3b9524ef
13 changed files with 340 additions and 329 deletions

View File

@@ -61,7 +61,7 @@ void WebCustomization::read(WebCustomization & settings, JsonObject & root) {
JsonArray sensorsJson = root.createNestedArray("sensors");
for (const SensorCustomization & sensor : settings.sensorCustomizations) {
JsonObject sensorJson = sensorsJson.createNestedObject();
sensorJson["id_str"] = sensor.id_str; // is
sensorJson["id"] = sensor.id; // is
sensorJson["name"] = sensor.name; // n
sensorJson["offset"] = sensor.offset; // o
}
@@ -70,7 +70,7 @@ void WebCustomization::read(WebCustomization & settings, JsonObject & root) {
JsonArray analogJson = root.createNestedArray("analogs");
for (const AnalogCustomization & sensor : settings.analogCustomizations) {
JsonObject sensorJson = analogJson.createNestedObject();
sensorJson["id"] = sensor.id; // i
sensorJson["gpio"] = sensor.gpio; // g
sensorJson["name"] = sensor.name; // n
sensorJson["offset"] = sensor.offset; // o
sensorJson["factor"] = sensor.factor; // f
@@ -101,7 +101,7 @@ StateUpdateResult WebCustomization::update(JsonObject & root, WebCustomization &
for (const JsonObject sensorJson : root["sensors"].as<JsonArray>()) {
// create each of the sensor, overwritting any previous settings
auto sensor = SensorCustomization();
sensor.id_str = sensorJson["id_str"].as<std::string>();
sensor.id = sensorJson["id"].as<std::string>();
sensor.name = sensorJson["name"].as<std::string>();
sensor.offset = sensorJson["offset"];
settings.sensorCustomizations.push_back(sensor); // add to list
@@ -114,7 +114,7 @@ StateUpdateResult WebCustomization::update(JsonObject & root, WebCustomization &
for (const JsonObject analogJson : root["analogs"].as<JsonArray>()) {
// create each of the sensor, overwritting any previous settings
auto sensor = AnalogCustomization();
sensor.id = analogJson["id"];
sensor.gpio = analogJson["gpio"];
sensor.name = analogJson["name"].as<std::string>();
sensor.offset = analogJson["offset"];
sensor.factor = analogJson["factor"];

View File

@@ -35,14 +35,14 @@ namespace emsesp {
// Customization for dallas sensor
class SensorCustomization {
public:
std::string id_str;
std::string id;
std::string name;
uint16_t offset;
};
class AnalogCustomization {
public:
uint8_t id;
uint8_t gpio;
std::string name;
float offset;
float factor;
@@ -51,7 +51,7 @@ class AnalogCustomization {
// used for removing from a list
bool operator==(const AnalogCustomization & a) const {
return id == a.id;
return gpio == a.gpio;
}
bool operator!=(const AnalogCustomization & a) const {
return !operator==(a);

View File

@@ -76,17 +76,18 @@ void WebDataService::core_data(AsyncWebServerRequest * request) {
// list is already sorted by device type
// Ignore Contoller
JsonArray devices = root.createNestedArray("devices");
char buffer[3];
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice && (emsdevice->device_type() != EMSdevice::DeviceType::CONTROLLER || emsdevice->count_entities() > 0)) {
JsonObject obj = devices.createNestedObject();
obj["i"] = emsdevice->unique_id(); // a unique id
obj["t"] = emsdevice->device_type_name(); // type
obj["b"] = emsdevice->brand_to_string(); // brand
obj["n"] = emsdevice->name(); // name
obj["d"] = emsdevice->device_id(); // deviceid
obj["p"] = emsdevice->product_id(); // productid
obj["v"] = emsdevice->version(); // version
obj["e"] = emsdevice->count_entities(); // number of entities (device values)
obj["id"] = Helpers::smallitoa(buffer, emsdevice->unique_id()); // a unique id as a string
obj["t"] = emsdevice->device_type_name(); // type
obj["b"] = emsdevice->brand_to_string(); // brand
obj["n"] = emsdevice->name(); // name
obj["d"] = emsdevice->device_id(); // deviceid
obj["p"] = emsdevice->product_id(); // productid
obj["v"] = emsdevice->version(); // version
obj["e"] = emsdevice->count_entities(); // number of entities (device values)
}
}
@@ -110,8 +111,8 @@ void WebDataService::sensor_data(AsyncWebServerRequest * request) {
if (EMSESP::dallassensor_.have_sensors()) {
for (const auto & sensor : EMSESP::dallassensor_.sensors()) {
JsonObject obj = sensors.createNestedObject();
obj["is"] = sensor.id_str(); // id
obj["n"] = sensor.name(); // name
obj["id"] = sensor.id(); // id as string
obj["n"] = sensor.name(); // name
if (EMSESP::system_.fahrenheit()) {
if (Helpers::hasValue(sensor.temperature_c)) {
obj["t"] = (float)sensor.temperature_c * 0.18 + 32;
@@ -129,14 +130,16 @@ void WebDataService::sensor_data(AsyncWebServerRequest * request) {
}
// analog sensors
// assume list is already sorted by id
JsonArray analogs = root.createNestedArray("analogs");
if (EMSESP::analog_enabled() && EMSESP::analogsensor_.have_sensors()) {
uint8_t count = 0;
char buffer[3];
for (const auto & sensor : EMSESP::analogsensor_.sensors()) {
// don't send if it's marked for removal
if (sensor.type() != AnalogSensor::AnalogType::MARK_DELETED) {
JsonObject obj = analogs.createNestedObject();
obj["i"] = sensor.id();
obj["id"] = Helpers::smallitoa(buffer, ++count); // needed for sorting table
obj["g"] = sensor.gpio();
obj["n"] = sensor.name();
obj["u"] = sensor.uom();
obj["o"] = sensor.offset();
@@ -145,6 +148,8 @@ void WebDataService::sensor_data(AsyncWebServerRequest * request) {
if (sensor.type() != AnalogSensor::AnalogType::NOTUSED) {
obj["v"] = Helpers::round2(sensor.value(), 0); // is optional and is a float
} else {
obj["v"] = 0; // must have a value for web sorting to work
}
}
}
@@ -169,6 +174,12 @@ void WebDataService::device_data(AsyncWebServerRequest * request, JsonVariant &
#ifndef EMSESP_STANDALONE
JsonObject output = response->getRoot();
emsdevice->generate_values_web(output);
#endif
#ifdef EMSESP_USE_SERIAL
#ifdef EMSESP_DEBUG
serializeJson(output, Serial);
#endif
#endif
response->setLength();
request->send(response);
@@ -246,8 +257,8 @@ void WebDataService::write_sensor(AsyncWebServerRequest * request, JsonVariant &
if (json.is<JsonObject>()) {
JsonObject sensor = json;
std::string id_str = sensor["id_str"]; // this is the key
std::string name = sensor["name"];
std::string id = sensor["id"]; // this is the key
std::string name = sensor["name"];
// calculate offset. We'll convert it to an int and * 10
float offset = sensor["offset"];
@@ -255,7 +266,7 @@ void WebDataService::write_sensor(AsyncWebServerRequest * request, JsonVariant &
if (EMSESP::system_.fahrenheit()) {
offset10 = offset / 0.18;
}
ok = EMSESP::dallassensor_.update(id_str, name, offset10);
ok = EMSESP::dallassensor_.update(id, name, offset10);
}
AsyncWebServerResponse * response = request->beginResponse(ok ? 200 : 204);
@@ -268,13 +279,13 @@ void WebDataService::write_analog(AsyncWebServerRequest * request, JsonVariant &
if (json.is<JsonObject>()) {
JsonObject analog = json;
uint8_t id = analog["id"]; // this is the unique key
uint8_t gpio = analog["gpio"]; // this is the unique key, the GPIO
std::string name = analog["name"];
float factor = analog["factor"];
float offset = analog["offset"];
uint8_t uom = analog["uom"];
int8_t type = analog["type"];
ok = EMSESP::analogsensor_.update(id, name, offset, factor, uom, type);
ok = EMSESP::analogsensor_.update(gpio, name, offset, factor, uom, type);
}
AsyncWebServerResponse * response = request->beginResponse(ok ? 200 : 204);