From 94f268a62d767ae4e86bfc8bd0057940009c9534 Mon Sep 17 00:00:00 2001 From: proddy Date: Thu, 11 Jan 2024 21:37:40 +0100 Subject: [PATCH] add new 'add' test --- src/console.cpp | 6 +- src/mqtt.h | 32 ++++++---- src/system.cpp | 2 +- src/test/test.cpp | 159 +++++++++++++++++++++++++++------------------- src/test/test.h | 5 +- 5 files changed, 121 insertions(+), 83 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 0947bebef..1d642dfee 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -128,14 +128,16 @@ static void setup_commands(std::shared_ptr & commands) { commands->add_command(ShellContext::MAIN, CommandFlags::USER, string_vector{"test"}, - string_vector{F_(name_optional), F_(data_optional)}, + string_vector{F_(name_optional), F_(data_optional), F_(id_optional)}, [=](Shell & shell, const std::vector & arguments) { if (arguments.empty()) { Test::run_test(shell, "default"); } else if (arguments.size() == 1) { Test::run_test(shell, arguments.front()); - } else { + } else if (arguments.size() == 2) { Test::run_test(shell, arguments[0].c_str(), arguments[1].c_str()); + } else { + Test::run_test(shell, arguments[0].c_str(), arguments[1].c_str(), arguments[2].c_str()); } }); commands->add_command(ShellContext::MAIN, CommandFlags::USER, string_vector{"t"}, [=](Shell & shell, const std::vector & arguments) { diff --git a/src/mqtt.h b/src/mqtt.h index f15c165a5..909c2e5e7 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -77,20 +77,20 @@ class Mqtt { static bool queue_remove_topic(const char * topic); static bool publish_ha_sensor_config(DeviceValue & dv, const char * model, const char * brand, const bool remove, const bool create_device_config = false); - static bool publish_ha_sensor_config(uint8_t type, - uint8_t tag, - const char * const fullname, - const char * const en_name, - const uint8_t device_type, - const char * const entity, - const uint8_t uom, - const bool remove, - const bool has_cmd, - const char * const ** options, - uint8_t options_size, - const int16_t dv_set_min, - const uint32_t dv_set_max, - const int8_t num_op, + static bool publish_ha_sensor_config(uint8_t type, + uint8_t tag, + const char * const fullname, + const char * const en_name, + const uint8_t device_type, + const char * const entity, + const uint8_t uom, + const bool remove, + const bool has_cmd, + const char * const ** options, + uint8_t options_size, + const int16_t dv_set_min, + const uint32_t dv_set_max, + const int8_t num_op, const JsonObjectConst dev_json); static bool publish_system_ha_sensor_config(uint8_t type, const char * name, const char * entity, const uint8_t uom); @@ -167,6 +167,10 @@ class Mqtt { return entity_format_; } + static void entity_format(uint8_t n) { + entity_format_ = n; + } + static uint8_t discovery_type() { return discovery_type_; } diff --git a/src/system.cpp b/src/system.cpp index 029620b75..31bcfaf4a 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1467,7 +1467,7 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output #if defined(EMSESP_TEST) // run a test, e.g. http://ems-esp/api?device=system&cmd=test&data=boiler bool System::command_test(const char * value, const int8_t id) { - return Test::run_test(value, id); + return Test::test(value, id); } #endif diff --git a/src/test/test.cpp b/src/test/test.cpp index df085ca74..e11897da1 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -1,7 +1,6 @@ - /* * EMS-ESP - https://github.com/emsesp/EMS-ESP -* Copyright 2020-2023 Paul Derbyshire + * Copyright 2020-2023 Paul Derbyshire * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,12 +24,19 @@ namespace emsesp { // no shell, called via the API or 'call system test' command // or http://ems-esp/api?device=system&cmd=test&data=boiler -bool Test::run_test(const char * command, int8_t id) { - if ((command == nullptr) || (strlen(command) == 0)) { +bool Test::test(const std::string & cmd, int8_t id1, int8_t id2) { + if (cmd.empty()) { return false; } - if (strcmp(command, "memory") == 0) { + if (cmd == "add") { + Mqtt::entity_format(Mqtt::entityFormat::SINGLE_LONG); // SINGLE_LONG, SINGLE_SHORT, MULTI_SHORT + System::test_set_all_active(true); // include all entities and give them fake values + add_device(id1, id2); + return true; + } + + if (cmd == "memory") { EMSESP::logger().notice("Testing memory by adding lots of devices and entities..."); System::test_set_all_active(true); // include all entities and give them fake values @@ -43,7 +49,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "general") == 0) { + if (cmd == "general") { 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 @@ -77,7 +83,7 @@ bool Test::run_test(const char * command, int8_t id) { // #ifdef EMSESP_STANDALONE - if (strcmp(command, "heat_exchange") == 0) { + if (cmd == "heat_exchange") { EMSESP::logger().info("Testing heating exchange..."); add_device(0x08, 219); // Greenstar HIU/Logamax kompakt WS170 @@ -89,7 +95,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "2thermostats") == 0) { + if (cmd == "2thermostats") { EMSESP::logger().info("Testing with multiple thermostats..."); add_device(0x08, 123); // GB072 @@ -121,7 +127,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "310") == 0) { + if (cmd == "310") { EMSESP::logger().info("Adding a GB072/RC310 combo..."); add_device(0x08, 123); // GB072 @@ -148,7 +154,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "gateway") == 0) { + if (cmd == "gateway") { EMSESP::logger().info("Adding a Gateway..."); // add 0x48 KM200, via a version command @@ -168,7 +174,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "mixer") == 0) { + if (cmd == "mixer") { EMSESP::logger().info("Adding a mixer..."); // add controller @@ -190,7 +196,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "boiler") == 0) { + if (cmd == "boiler") { EMSESP::logger().info("Adding boiler..."); add_device(0x08, 123); // Nefit Trendline @@ -207,7 +213,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "thermostat") == 0) { + if (cmd == "thermostat") { EMSESP::logger().info("Adding thermostat..."); add_device(0x10, 192); // FW120 @@ -220,7 +226,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "solar") == 0) { + if (cmd == "solar") { EMSESP::logger().info("Adding solar..."); add_device(0x30, 163); // SM100 @@ -239,7 +245,7 @@ bool Test::run_test(const char * command, int8_t id) { return true; } - if (strcmp(command, "heatpump") == 0) { + if (cmd == "heatpump") { EMSESP::logger().info("Adding heatpump..."); add_device(0x38, 200); // Enviline module @@ -258,11 +264,13 @@ bool Test::run_test(const char * command, int8_t id) { } // These next tests are run from the Consol via the test command, so inherit the Shell -void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const std::string & data) { +void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const std::string & id1_s, const std::string & id2_s) { shell.add_flags(CommandFlags::ADMIN); // switch to su // init stuff Mqtt::ha_enabled(true); + Mqtt::entity_format(Mqtt::entityFormat::SINGLE_SHORT); // SINGLE_LONG, SINGLE_SHORT, MULTI_SHORT + EMSESP::rxservice_.ems_mask(EMSbus::EMS_MASK_BUDERUS); // EMSESP::watch(EMSESP::Watch::WATCH_RAW); // raw mode @@ -275,11 +283,36 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const command = cmd; } + // extract params + int8_t id1 = -1; + int8_t id2 = -1; + if (!id1_s.empty()) { + if (id1_s[0] == '0' && id1_s[1] == 'x') { + id1 = Helpers::hextoint(id1_s.c_str()); + } else { + id1 = Helpers::atoint(id1_s.c_str()); + } + } + if (!id2_s.empty()) { + id2 = Helpers::atoint(id2_s.c_str()); + } + bool ok = false; + if (command == "add") { + shell.printfln("Testing Adding a device (product_id %d), with all values...", id2); + test("add", id1, id2); // e.g. 8 172 + shell.invoke_command("show values"); + ok = true; + } + + // TODO Fix ! + // operating time compressor heating (uptimecompheating: 0 days 3 hours 27 minutes + // concat!!! + if (command == "general") { shell.printfln("Testing adding a boiler, thermostat and sensors..."); - run_test("general"); + test("general"); // add sensors emsesp::EMSESP::analogsensor_.test(); @@ -296,14 +329,14 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // https://github.com/emsesp/EMS-ESP32/issues/869 if (command == "memory") { shell.printfln("Testing memory by adding lots of devices and entities..."); - run_test("memory"); + test("memory"); shell.invoke_command("show values"); ok = true; } if (command == "custom_entities") { shell.printfln("custom entities..."); - run_test("general"); + test("general"); #ifdef EMSESP_STANDALONE AsyncWebServerRequest request; @@ -318,7 +351,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "coldshot") { shell.printfln("Testing coldshot..."); - run_test("general"); + test("general"); #ifdef EMSESP_STANDALONE AsyncWebServerRequest request; @@ -370,7 +403,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "modes") { shell.printfln("Testing thermostat modes..."); - run_test("general"); + test("general"); shell.invoke_command("call thermostat mode auto"); shell.invoke_command("call thermostat mode Manuell"); // DE shell.invoke_command("call thermostat mode 1"); @@ -506,13 +539,13 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "gateway") { shell.printfln("Testing Gateway..."); - run_test("gateway"); + test("gateway"); ok = true; } if (command == "310") { shell.printfln("Testing RC310..."); - run_test("310"); + test("310"); shell.invoke_command("show devices"); shell.invoke_command("show values"); shell.invoke_command("call system publish"); @@ -522,7 +555,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "heat_exchange") { shell.printfln("Testing heat exchange..."); - run_test("heat_exchange"); + test("heat_exchange"); shell.invoke_command("show devices"); shell.invoke_command("show values"); ok = true; @@ -530,7 +563,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "2thermostats") { shell.printfln("Testing multiple thermostats..."); - run_test("2thermostats"); + test("2thermostats"); shell.invoke_command("show values"); shell.invoke_command("show devices"); ok = true; @@ -542,8 +575,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::enabled(false); // turn off mqtt Mqtt::ha_enabled(false); // turn off ha - run_test("boiler"); - run_test("thermostat"); + test("boiler"); + test("thermostat"); JsonDocument doc; // some absurd high number for (const auto & emsdevice : EMSESP::emsdevices) { @@ -596,7 +629,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::ha_enabled(true); Mqtt::nested_format(1); - run_test("boiler"); + test("boiler"); shell.invoke_command("show devices"); shell.invoke_command("show values"); shell.invoke_command("call boiler info"); @@ -622,7 +655,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "shower_alert") { shell.printfln("Testing Shower Alert..."); - run_test("boiler"); + test("boiler"); // device type, command, data Command::call(EMSdevice::DeviceType::BOILER, "wwtapactivated", "false"); @@ -650,10 +683,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::nested_format(1); // is nested // Mqtt::nested_format(2); // not nested - run_test("boiler"); - run_test("thermostat"); - run_test("solar"); - run_test("mixer"); + test("boiler"); + test("thermostat"); + test("solar"); + test("mixer"); shell.invoke_command("call system publish"); shell.invoke_command("show mqtt"); @@ -672,8 +705,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::nested_format(1); // Mqtt::send_response(false); - run_test("boiler"); - // run_test("thermostat"); + test("boiler"); + // test("thermostat"); // 0xC2 // [emsesp] Boiler(0x08) -> Me(0x0B), UBAErrorMessage3(0xC2), data: 08 AC 00 10 31 48 30 31 15 80 95 0B 0E 10 38 00 7F FF FF FF 08 AC 00 10 09 41 30 @@ -692,8 +725,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::nested_format(1); // Mqtt::send_response(false); - run_test("boiler"); - run_test("thermostat"); + test("boiler"); + test("thermostat"); shell.invoke_command("call boiler wwseltemp"); shell.invoke_command("call system publish"); @@ -745,15 +778,13 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const } if (command == "healthcheck") { - uint8_t n = 0; - if (!data.empty()) { - n = Helpers::atoint(data.c_str()); - } - // n=1 = EMSESP::system_.HEALTHCHECK_NO_BUS // n=2 = EMSESP::system_.HEALTHCHECK_NO_NETWORK - shell.printfln("Testing healthcheck with %d", n); - EMSESP::system_.healthcheck(n); + if (id1 == -1) { + id1 = 0; + } + shell.printfln("Testing healthcheck with %d", id1); + EMSESP::system_.healthcheck(id1); ok = true; } @@ -763,7 +794,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::ha_enabled(true); // Mqtt::send_response(false); - run_test("thermostat"); + test("thermostat"); // shell.invoke_command("call thermostat seltemp"); // shell.invoke_command("call system publish"); @@ -794,7 +825,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::ha_enabled(true); // Mqtt::send_response(false); - run_test("boiler"); + test("boiler"); shell.invoke_command("call boiler wwseltemp"); shell.invoke_command("call system publish"); @@ -819,7 +850,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::ha_enabled(true); // Mqtt::send_response(false); - run_test("boiler"); + test("boiler"); shell.invoke_command("call boiler wwseltemp"); shell.invoke_command("call system publish"); @@ -842,8 +873,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // EMSESP::bool_format(BOOL_FORMAT_10); // BOOL_FORMAT_10_STR EMSESP::system_.bool_format(BOOL_FORMAT_TRUEFALSE); // BOOL_FORMAT_TRUEFALSE_STR - run_test("boiler"); - run_test("thermostat"); + test("boiler"); + test("thermostat"); AsyncWebServerRequest request; JsonDocument doc; @@ -875,8 +906,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const EMSESP::system_.bool_format(BOOL_FORMAT_10); // BOOL_FORMAT_10_STR // EMSESP::bool_format(BOOL_FORMAT_TRUEFALSE); // BOOL_FORMAT_TRUEFALSE_STR - run_test("boiler"); - run_test("thermostat"); + test("boiler"); + test("thermostat"); EMSESP::mqtt_.incoming("ems-esp/boiler/wwseltemp", "59"); ok = true; @@ -887,15 +918,15 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const shell.printfln("Testing API wwmode"); Mqtt::ha_enabled(false); Mqtt::nested_format(1); - run_test("310"); + test("310"); AsyncWebServerRequest request; request.method(HTTP_POST); JsonDocument doc; JsonVariant json; - char data[] = "{\"value\":\"off\"}"; - deserializeJson(doc, data); + char odata[] = "{\"value\":\"off\"}"; + deserializeJson(doc, odata); json = doc.as(); request.url("/api/thermostat/wwmode"); EMSESP::webAPIService.webAPIService_post(&request, json); @@ -911,8 +942,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const Mqtt::nested_format(1); // Mqtt::send_response(true); - run_test("boiler"); - run_test("thermostat"); + test("boiler"); + test("thermostat"); AsyncWebServerRequest requestX; JsonDocument docX; @@ -1181,10 +1212,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const shell.printfln("Testing nested MQTT"); Mqtt::ha_enabled(false); // turn off HA Discovery to stop the chatter - run_test("boiler"); - run_test("thermostat"); - run_test("solar"); - run_test("mixer"); + test("boiler"); + test("thermostat"); + test("solar"); + test("mixer"); // first with nested Mqtt::nested_format(1); @@ -1201,7 +1232,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "thermostat") { shell.printfln("Testing adding a thermostat FW120..."); - run_test("thermostat"); + test("thermostat"); shell.invoke_command("show values"); shell.invoke_command("call system publish"); @@ -1229,7 +1260,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "solar") { shell.printfln("Testing Solar"); - run_test("solar"); + test("solar"); uart_telegram("30 00 FF 0A 02 6A 04"); // SM100 pump on (1)sh EMSESP::show_device_values(shell); @@ -1243,7 +1274,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "heatpump") { shell.printfln("Testing Heat Pump"); - run_test("heatpump"); + test("heatpump"); shell.invoke_command("call"); shell.invoke_command("call heatpump info"); ok = true; @@ -1698,7 +1729,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const if (command == "mixer") { shell.printfln("Testing Mixer..."); - run_test("mixer"); + test("mixer"); // check for error "No telegram type handler found for ID 0x255 (src 0x20)" uart_telegram({0xA0, 0x00, 0xFF, 0x00, 0x01, 0x55, 0x00, 0x1A}); diff --git a/src/test/test.h b/src/test/test.h index b8091a0e3..83caa6e84 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -22,6 +22,7 @@ #define EMSESP_TEST_H #include "emsesp.h" + #include namespace emsesp { @@ -60,8 +61,8 @@ namespace emsesp { class Test { public: - static void run_test(uuid::console::Shell & shell, const std::string & command, const std::string & data = ""); - static bool run_test(const char * command, int8_t id = 0); + static void run_test(uuid::console::Shell & shell, const std::string & command, const std::string & id1 = "", const std::string & id2 = ""); + static bool test(const std::string & command, int8_t id1 = -1, int8_t id2 = -1); static void dummy_mqtt_commands(const char * message); static void rx_telegram(const std::vector & data); static void uart_telegram(const std::vector & rx_data);