This commit is contained in:
MichaelDvP
2023-11-15 11:27:19 +01:00
13 changed files with 807 additions and 791 deletions

View File

@@ -841,7 +841,7 @@ std::string EMSdevice::get_value_uom(const std::string & shortname) const {
return std::string{}; // not found
}
bool EMSdevice::export_values(uint8_t device_type, JsonObject & output, const int8_t id, const uint8_t output_target) {
bool EMSdevice::export_values(uint8_t unique_id, JsonObject & output, const int8_t id, const uint8_t output_target) {
bool has_value = false;
uint8_t tag;
if (id >= 1 && id <= (1 + DeviceValueTAG::TAG_HS16 - DeviceValueTAG::TAG_HC1)) {
@@ -854,7 +854,7 @@ bool EMSdevice::export_values(uint8_t device_type, JsonObject & output, const in
if (id > 0 || output_target == EMSdevice::OUTPUT_TARGET::API_VERBOSE) {
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice && (emsdevice->device_type() == device_type)) {
if (emsdevice->unique_id() == unique_id) {
has_value |= emsdevice->generate_values(output, tag, (id < 1), output_target); // use nested for id -1 and 0
}
}
@@ -866,7 +866,7 @@ bool EMSdevice::export_values(uint8_t device_type, JsonObject & output, const in
JsonObject output_hc = output;
bool nest_created = false;
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice && (emsdevice->device_type() == device_type)) {
if (emsdevice->unique_id() == unique_id) {
if (!nest_created && emsdevice->has_tags(tag)) {
output_hc = output.createNestedObject(EMSdevice::tag_to_mqtt(tag));
nest_created = true;

View File

@@ -112,14 +112,10 @@ bool System::command_allvalues(const char * value, const int8_t id, JsonObject &
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XXXLARGE);
JsonObject device_output;
for (const auto & device_class : EMSFactory::device_handlers()) {
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice->device_type() == device_class.first) {
std::string title = emsdevice->device_type_2_device_name_translated() + std::string(" ") + emsdevice->to_string();
device_output = output.createNestedObject(title);
emsesp::EMSdevice::export_values(emsdevice->device_type(), device_output, id, EMSdevice::OUTPUT_TARGET::API_VERBOSE);
}
}
for (const auto & emsdevice : EMSESP::emsdevices) {
std::string title = emsdevice->device_type_2_device_name_translated() + std::string(" ") + emsdevice->to_string();
device_output = output.createNestedObject(title);
emsesp::EMSdevice::export_values(emsdevice->unique_id(), device_output, id, EMSdevice::OUTPUT_TARGET::API_VERBOSE);
}
// Custom entities

View File

@@ -37,9 +37,8 @@ bool Test::run_test(const char * command, int8_t id) {
// simulate HansRemmerswaal's setup - see https://github.com/emsesp/EMS-ESP32/issues/859
add_device(0x08, 172); // 176 entities - boiler: Enviline/Compress 6000AW/Hybrid 3000-7000iAW/SupraEco/Geo 5xx/WLW196i
// add_device(0x10, 158); // 62 entities - thermostat: RC300/RC310/Moduline 3000/1010H/CW400/Sense II/HPC410
// add_device(0x38, 200); // 4 entities - thermostat: RC100H
add_device(0x10, 158); // 62 entities - thermostat: RC300/RC310/Moduline 3000/1010H/CW400/Sense II/HPC410
add_device(0x38, 200); // 4 entities - thermostat: RC100H
return true;
}
@@ -47,9 +46,13 @@ bool Test::run_test(const char * command, int8_t id) {
if (strcmp(command, "general") == 0) {
EMSESP::logger().info("Testing general. Adding a Boiler and Thermostat");
// System::test_set_all_active(true); // uncomment if we want to show all entities and give them fake values
add_device(0x08, 123); // Nefit Trendline
add_device(0x18, 157); // Bosch CR100
// add_device(0x10, 158); // RC300 - there's no data here
// add some data
// Boiler -> Me, UBAMonitorFast(0x18), telegram: 08 00 18 00 00 02 5A 73 3D 0A 10 65 40 02 1A 80 00 01 E1 01 76 0E 3D 48 00 C9 44 02 00 (#data=25)
uart_telegram({0x08, 0x00, 0x18, 0x00, 0x00, 0x02, 0x5A, 0x73, 0x3D, 0x0A, 0x10, 0x65, 0x40, 0x02, 0x1A,

View File

@@ -230,17 +230,23 @@ void WebDataService::device_data(AsyncWebServerRequest * request) {
// assumes the service has been checked for admin authentication
void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVariant & json) {
if (json.is<JsonObject>()) {
JsonObject dv = json["devicevalue"];
uint8_t unique_id = json["id"];
uint8_t unique_id = json["id"]; // unique ID
const char * cmd = json["c"]; // the command
JsonVariant data = json["v"]; // the value in any format
// quit on bad values
if (strlen(cmd) == 0 || data.isNull()) {
AsyncWebServerResponse * response = request->beginResponse(400); // bad request
request->send(response);
return;
}
// using the unique ID from the web find the real device type
// id is the selected device
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice->unique_id() == unique_id) {
// parse the command as it could have a hc or wwc prefixed, e.g. hc2/seltemp
const char * cmd = dv["c"]; // the command
int8_t id = -1; // default
cmd = Command::parse_command_string(cmd, id); // extract hc or wwc
int8_t id = -1; // default
cmd = Command::parse_command_string(cmd, id); // extract hc or wwc
// create JSON for output
auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
@@ -248,9 +254,8 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
// the data could be in any format, but we need string
// authenticated is always true
JsonVariant data = dv["v"]; // the value in any format
uint8_t return_code = CommandRet::NOT_FOUND;
uint8_t device_type = emsdevice->device_type();
uint8_t return_code = CommandRet::NOT_FOUND;
uint8_t device_type = emsdevice->device_type();
if (data.is<const char *>()) {
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
} else if (data.is<int>()) {
@@ -278,16 +283,16 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
return;
}
}
// special check for custom entities (which have a unique id of 99)
if (unique_id == 99) {
// parse the command as it could have a hc or wwc prefixed, e.g. hc2/seltemp
const char * cmd = dv["c"];
int8_t id = -1;
cmd = Command::parse_command_string(cmd, id);
auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
JsonObject output = response->getRoot();
JsonVariant data = dv["v"]; // the value in any format
uint8_t return_code = CommandRet::NOT_FOUND;
uint8_t device_type = EMSdevice::DeviceType::CUSTOM;
int8_t id = -1;
cmd = Command::parse_command_string(cmd, id);
auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
JsonObject output = response->getRoot();
uint8_t return_code = CommandRet::NOT_FOUND;
uint8_t device_type = EMSdevice::DeviceType::CUSTOM;
if (data.is<const char *>()) {
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
} else if (data.is<int>()) {
@@ -304,6 +309,7 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
EMSESP::logger().debug("Write command successful");
#endif
}
response->setCode((return_code == CommandRet::OK) ? 200 : 400); // bad request
response->setLength();
request->send(response);
@@ -311,6 +317,7 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
}
}
// if we reach here, fail
AsyncWebServerResponse * response = request->beginResponse(400); // bad request
request->send(response);
}