Merge branch 'v2_cmd' of https://github.com/proddy/EMS-ESP into v2_c

This commit is contained in:
MichaelDvP
2020-08-13 18:36:24 +02:00
10 changed files with 75 additions and 87 deletions

View File

@@ -26,6 +26,8 @@ uuid::log::Logger Boiler::logger_{F_(boiler), uuid::log::Facility::CONSOLE};
Boiler::Boiler(uint8_t device_type, int8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand)
: EMSdevice(device_type, device_id, product_id, version, name, flags, brand) {
this->reserve_mem(20); // reserve some space for the telegram registries, to avoid memory fragmentation
LOG_DEBUG(F("Adding new Boiler with device ID 0x%02X"), device_id);
// the telegram handlers...

View File

@@ -26,6 +26,8 @@ uuid::log::Logger Thermostat::logger_{F_(thermostat), uuid::log::Facility::CONSO
Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand)
: EMSdevice(device_type, device_id, product_id, version, name, flags, brand) {
this->reserve_mem(25); // reserve some space for the telegram registries, to avoid memory fragmentation
// common telegram handlers
register_telegram_type(EMS_TYPE_RCOutdoorTemp, F("RCOutdoorTemp"), false, [&](std::shared_ptr<const Telegram> t) { process_RCOutdoorTemp(t); });
register_telegram_type(EMS_TYPE_RCTime, F("RCTime"), false, [&](std::shared_ptr<const Telegram> t) { process_RCTime(t); });

View File

@@ -24,8 +24,6 @@ namespace emsesp {
uuid::log::Logger EMSdevice::logger_{F_(emsesp), uuid::log::Facility::CONSOLE};
std::vector<EMSdevice::TelegramFunction> EMSdevice::telegram_functions_;
std::string EMSdevice::brand_to_string() const {
switch (brand_) {
case EMSdevice::Brand::BOSCH:
@@ -248,7 +246,6 @@ void EMSdevice::register_mqtt_topic(const std::string & topic, mqtt_subfunction_
}
void EMSdevice::register_mqtt_cmd(const __FlashStringHelper * cmd, mqtt_cmdfunction_p f) {
LOG_DEBUG(F("Registering MQTT cmd %s for device type %s"), uuid::read_flash_string(cmd).c_str(), this->device_type_name().c_str());
Mqtt::add_command(this->device_type_, this->device_id_, cmd, f);
}

View File

@@ -147,7 +147,7 @@ class EMSdevice {
void fetch_values();
void toggle_fetch(uint16_t telegram_id, bool toggle);
static void reserve_mem(size_t n) {
void reserve_mem(size_t n) {
telegram_functions_.reserve(n);
}
@@ -229,8 +229,7 @@ class EMSdevice {
};
enum DeviceType : uint8_t {
UNKNOWN = 0,
SERVICEKEY, // us
SERVICEKEY = 0, // this is us
BOILER,
THERMOSTAT,
MIXING,
@@ -280,11 +279,11 @@ class EMSdevice {
private:
uint8_t unique_id_;
uint8_t device_type_ = DeviceType::UNKNOWN;
uint8_t device_type_ = DeviceType::SERVICEKEY;
uint8_t device_id_ = 0;
uint8_t product_id_ = 0;
std::string version_;
std::string name_; // the long name of the EMS model
std::string name_; // the long name for the EMS model
uint8_t flags_ = 0;
uint8_t brand_ = Brand::NO_BRAND;
@@ -304,7 +303,7 @@ class EMSdevice {
, process_function_(process_function) {
}
};
static std::vector<TelegramFunction> telegram_functions_; // each EMS device has its own set of registered telegram types
std::vector<TelegramFunction> telegram_functions_; // each EMS device has its own set of registered telegram types
};
} // namespace emsesp

View File

@@ -577,7 +577,7 @@ void EMSESP::show_devices(uuid::console::Shell & shell) {
}
shell.println();
emsdevice->show_telegram_handlers(shell);
emsdevice->show_mqtt_handlers(shell);
// emsdevice->show_mqtt_handlers(shell);
shell.println();
}
}
@@ -797,19 +797,21 @@ void EMSESP::start() {
esp8266React.begin(); // loads system settings (wifi, mqtt, etc)
emsespSettingsService.begin(); // load EMS-ESP specific settings
// system_.check_upgrade(); // see if we need to migrate from previous versions
mqtt_.start(); // mqtt init
console_.start(); // telnet and serial console
mqtt_.start(); // mqtt init
system_.start(); // starts syslog, uart, sets version, initializes LED. Requires pre-loaded settings.
shower_.start(); // initialize shower timer and shower alert
txservice_.start(); // sets bus ID, sends out request for EMS devices
sensors_.start(); // dallas external sensors
webServer.begin(); // start web server
// reserve some space for the telegram registries, to avoid memory fragmentation
EMSdevice::reserve_mem(EMSdevice::EMS_DEVICES_MAX_TELEGRAMS); // space for 20 telegram handlers
emsdevices.reserve(5); // reserve space for initially 5 devices
emsdevices.reserve(5); // reserve space for initially 5 devices to avoid mem
LOG_INFO("EMS Device library loaded with %d records", device_library_.size());
#if defined(EMSESP_STANDALONE)
mqtt_.on_connect(); // simulate an MQTT connection
#endif
}
// main loop calling all services

View File

@@ -78,6 +78,8 @@ void Mqtt::add_command(const uint8_t device_type, const uint8_t device_id, const
Mqtt::subscribe(device_type, cmd_topic, nullptr); // use an empty function handler to signal this is a command function
}
LOG_DEBUG(F("Registering MQTT cmd %s with topic %s"), uuid::read_flash_string(cmd).c_str(), EMSdevice::device_type_topic_name(device_type).c_str());
mqtt_cmdfunctions_.emplace_back(device_type, device_id, cmd, cb);
}
@@ -135,11 +137,16 @@ void Mqtt::show_mqtt(uuid::console::Shell & shell) {
// show subscriptions
shell.printfln(F("MQTT subscriptions:"));
for (const auto & mqtt_subfunction : mqtt_subfunctions_) {
shell.printf(F(" topic: %s, [cmd]:"), mqtt_subfunction.full_topic_.c_str());
// show the commands associated with this subscription
for (const auto & mqtt_cmdfunction : mqtt_cmdfunctions_) {
if (EMSdevice::device_type_topic_name(mqtt_cmdfunction.device_type_) == mqtt_subfunction.topic_) {
shell.printf(F(" %s"), uuid::read_flash_string(mqtt_cmdfunction.cmd_).c_str());
// don't show commands if its homeassistant
if ((strncmp(mqtt_subfunction.full_topic_.c_str(), "homeassistant/", 13) == 0)) {
shell.printf(F(" topic: %s"), mqtt_subfunction.full_topic_.c_str());
} else {
// show the commands associated with this subscription
shell.printf(F(" topic: %s, [cmd]:"), mqtt_subfunction.full_topic_.c_str());
for (const auto & mqtt_cmdfunction : mqtt_cmdfunctions_) {
if (EMSdevice::device_type_topic_name(mqtt_cmdfunction.device_type_) == mqtt_subfunction.topic_) {
shell.printf(F(" %s"), uuid::read_flash_string(mqtt_cmdfunction.cmd_).c_str());
}
}
}
shell.println();
@@ -365,11 +372,6 @@ void Mqtt::start() {
on_publish(packetId);
});
#if defined(EMSESP_STANDALONE)
// simulate an MQTT connection
on_connect();
#endif
// create space for command buffer, to avoid heap memory fragmentation
mqtt_cmdfunctions_.reserve(40); // current count with boiler+thermostat is 37
mqtt_subfunctions_.reserve(10);
@@ -400,12 +402,9 @@ void Mqtt::on_connect() {
resubscribe(); // in case this is a reconnect, re-subscribe again to all MQTT topics
// add the system MQTT subscriptions, only if its a fresh start with no previous subscriptions
// these commands respond to the topic "system_cmd" and take a payload like {cmd:"", data:"", id:""}
if (mqtt_subfunctions_.empty()) {
add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("pin"), System::mqtt_command_pin);
add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("send"), System::mqtt_command_send);
}
add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("pin"), System::mqtt_command_pin);
add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("send"), System::mqtt_command_send);
LOG_INFO(F("MQTT connected"));
}

View File

@@ -171,8 +171,8 @@ class Mqtt {
// function handlers for MQTT subscriptions
struct MQTTSubFunction {
uint8_t device_type_; // which device type, from DeviceType::
const std::string topic_;
uint8_t device_type_; // which device type, from DeviceType::
const std::string topic_; // short topic name
const std::string full_topic_; // the fully qualified topic name, usually with the hostname prefixed
mqtt_subfunction_p mqtt_subfunction_; // can be empty

View File

@@ -35,8 +35,6 @@
using uuid::console::Shell;
#define MQTT_SYSTEM_CMD "system_cmd"
namespace emsesp {
class System {

View File

@@ -537,9 +537,6 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & command) {
if (command == "mqtt") {
shell.printfln(F("Testing MQTT..."));
// simulate an on connect
Mqtt::on_connect();
// add a boiler
// question: do we need to set the mask?
std::string version("1.2.3");