moved back to std:: containers for esp32

This commit is contained in:
proddy
2021-01-03 18:03:41 +01:00
parent fa040fb2d5
commit 22f5e56622
14 changed files with 498 additions and 257 deletions

View File

@@ -69,6 +69,10 @@ const std::string EMSdevice::tag_to_string(uint8_t tag) {
return uuid::read_flash_string(DeviceValueTAG_s[tag - 1]); // offset by 1 to account for NONE
}
const std::vector<EMSdevice::DeviceValue> EMSdevice::devicevalues() const {
return devicevalues_;
}
std::string EMSdevice::brand_to_string() const {
switch (brand_) {
case EMSdevice::Brand::BOSCH:
@@ -260,7 +264,8 @@ std::string EMSdevice::to_string_short() const {
void EMSdevice::fetch_values() {
EMSESP::logger().debug(F("Fetching values for device ID 0x%02X"), device_id());
for (const auto & tf : *telegram_functions_) {
// for (const auto & tf : *telegram_functions_) {
for (const auto & tf : telegram_functions_) {
if (tf.fetch_) {
read_command(tf.telegram_type_id_);
}
@@ -271,7 +276,8 @@ void EMSdevice::fetch_values() {
void EMSdevice::toggle_fetch(uint16_t telegram_id, bool toggle) {
EMSESP::logger().debug(F("Toggling fetch for device ID 0x%02X, telegram ID 0x%02X to %d"), device_id(), telegram_id, toggle);
for (auto & tf : *telegram_functions_) {
for (auto & tf : telegram_functions_) {
// for (auto & tf : *telegram_functions_) {
if (tf.telegram_type_id_ == telegram_id) {
tf.fetch_ = toggle;
}
@@ -280,7 +286,8 @@ void EMSdevice::toggle_fetch(uint16_t telegram_id, bool toggle) {
// get status of automatic fetch for a telegram id
bool EMSdevice::get_toggle_fetch(uint16_t telegram_id) {
for (auto & tf : *telegram_functions_) {
for (auto & tf : telegram_functions_) {
// for (auto & tf : *telegram_functions_) {
if (tf.telegram_type_id_ == telegram_id) {
return tf.fetch_;
}
@@ -292,7 +299,8 @@ bool EMSdevice::get_toggle_fetch(uint16_t telegram_id) {
void EMSdevice::show_device_values(uuid::console::Shell & shell) {
size_t total_s = 0;
uint8_t count = 0;
for (const auto & dv : *devicevalues_) {
// for (const auto & dv : *devicevalues_) {
for (const auto & dv : devicevalues_) {
size_t s = sizeof(dv);
if (dv.full_name) {
shell.printfln("[%s] %d", uuid::read_flash_string(dv.full_name).c_str(), s);
@@ -309,12 +317,14 @@ void EMSdevice::show_device_values(uuid::console::Shell & shell) {
// list all the telegram type IDs for this device
void EMSdevice::show_telegram_handlers(uuid::console::Shell & shell) {
if (telegram_functions_->size() == 0) {
// if (telegram_functions_->size() == 0) {
if (telegram_functions_.size() == 0) {
return;
}
shell.printf(F(" This %s will respond to telegram type IDs: "), device_type_name().c_str());
for (const auto & tf : *telegram_functions_) {
for (const auto & tf : telegram_functions_) {
// for (const auto & tf : *telegram_functions_) {
shell.printf(F("0x%02X "), tf.telegram_type_id_);
}
shell.println();
@@ -322,7 +332,8 @@ void EMSdevice::show_telegram_handlers(uuid::console::Shell & shell) {
// list all the telegram type IDs for this device, outputting to a string (max size 200)
char * EMSdevice::show_telegram_handlers(char * result) {
uint8_t size = telegram_functions_->size();
// uint8_t size = telegram_functions_->size();
uint8_t size = telegram_functions_.size();
strlcpy(result, "", 200);
@@ -332,7 +343,8 @@ char * EMSdevice::show_telegram_handlers(char * result) {
char str[10];
uint8_t i = 0;
for (const auto & tf : *telegram_functions_) {
// for (const auto & tf : *telegram_functions_) {
for (const auto & tf : telegram_functions_) {
snprintf_P(str, sizeof(str), PSTR("0x%02X"), tf.telegram_type_id_);
strlcat(result, str, 200);
if (++i < size) {
@@ -359,21 +371,16 @@ void EMSdevice::register_mqtt_cmd(const __FlashStringHelper * cmd, cmdfunction_p
// register a call back function for a specific telegram type
void EMSdevice::register_telegram_type(const uint16_t telegram_type_id, const __FlashStringHelper * telegram_type_name, bool fetch, process_function_p f) {
/*
TelegramFunction tf;
tf.fetch_ = fetch;
tf.process_function_ = f;
tf.telegram_type_id_ = telegram_type_id;
tf.telegram_type_name_ = telegram_type_name;
telegram_functions_->push(tf);
*/
// TelegramFunction(uint16_t telegram_type_id, const __FlashStringHelper * telegram_type_name, bool fetch, process_function_p process_function)
// : telegram_type_id_(telegram_type_id)
// , telegram_type_name_(telegram_type_name)
// , fetch_(fetch)
// , process_function_(process_function) {
// }
// telegram_functions_.emplace_back(telegram_type_id, telegram_type_name, fetch, f);
telegram_functions_.emplace_back(telegram_type_id, telegram_type_name, fetch, f);
}
// add to device value library
@@ -410,6 +417,8 @@ void EMSdevice::register_device_value(uint8_t tag,
}
// add to our library
/*
DeviceValue dv;
dv.device_type = device_type_;
dv.tag = tag;
@@ -430,6 +439,18 @@ void EMSdevice::register_device_value(uint8_t tag,
}
devicevalues_->push(dv);
*/
// count #options
uint8_t options_size = 0;
if (options != nullptr) {
uint8_t i = 0;
while (options[i++]) {
options_size++;
};
}
devicevalues_.emplace_back(device_type_, tag, value_p, type, options, options_size, short_name, full_name, uom);
}
// looks up the uom (suffix) for a given key from the device value table
@@ -445,9 +466,12 @@ std::string EMSdevice::get_value_uom(const char * key) {
}
// find the key (p) in the name
// because the new container is not multi-threaded can't use the iterator
for (uint8_t i = 0; i < devicevalues_->size(); i++) {
//
for (const auto & dv : devicevalues_) {
/*
for (uint8_t i = 0; i < devicevalues_->size(); i++) { // because the new container is not multi-threaded can't use the iterator
auto dv = (*devicevalues_)[i];
*/
if (dv.full_name != nullptr) {
if (uuid::read_flash_string(dv.full_name) == p) {
// ignore TIME since "minutes" is already included
@@ -469,8 +493,12 @@ bool EMSdevice::generate_values_json_web(JsonObject & json) {
JsonArray data = json.createNestedArray("data");
uint8_t num_elements = 0;
for (const auto & dv : devicevalues_) {
/*
for (uint8_t i = 0; i < devicevalues_->size(); i++) {
auto dv = (*devicevalues_)[i];
*/
// for (const auto & dv : devicevalues()) {
// ignore if full_name empty
@@ -479,6 +507,8 @@ bool EMSdevice::generate_values_json_web(JsonObject & json) {
if ((dv.type == DeviceValueType::BOOL) && Helpers::hasValue(*(uint8_t *)(dv.value_p), EMS_VALUE_BOOL)) {
// see if we have options for the bool's
if (dv.options_size == 2) {
// if (dv.options_size == 2) {
data.add(*(uint8_t *)(dv.value_p) ? dv.options[0] : dv.options[1]);
} else {
// see how to render the value depending on the setting
@@ -502,6 +532,7 @@ bool EMSdevice::generate_values_json_web(JsonObject & json) {
// handle ENUMs
else if ((dv.type == DeviceValueType::ENUM) && Helpers::hasValue(*(uint8_t *)(dv.value_p))) {
// if (*(uint8_t *)(dv.value_p) < dv.options_size) {
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
data.add(dv.options[*(uint8_t *)(dv.value_p)]);
}
@@ -512,7 +543,8 @@ bool EMSdevice::generate_values_json_web(JsonObject & json) {
// If a divider is specified, do the division to 2 decimals places and send back as double/float
// otherwise force as an integer whole
// the nested if's is necessary due to the way the ArduinoJson templates are pre-processed by the compiler
uint8_t divider = ((dv.options_size) == 1) ? Helpers::atoint(uuid::read_flash_string(dv.options[0]).c_str()) : 0;
// uint8_t divider = ((dv.options_size) == 1) ? Helpers::atoint(uuid::read_flash_string(dv.options[0]).c_str()) : 0;
uint8_t divider = (dv.options_size == 1) ? Helpers::atoint(uuid::read_flash_string(dv.options[0]).c_str()) : 0;
// INT
if ((dv.type == DeviceValueType::INT) && Helpers::hasValue(*(int8_t *)(dv.value_p))) {
@@ -589,8 +621,12 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
uint8_t old_tag = 255;
JsonObject json = root;
for (uint8_t i = 0; i < devicevalues_->size(); i++) {
for (const auto & dv : devicevalues_) {
/*
for (uint8_t i = 0; i < devicevalues_->size(); i++) {
auto dv = (*devicevalues_)[i];
*/
// for (const auto & dv : devicevalues()) {
// only show if tag is either empty or matches a value, and don't show if full_name is empty unless we're outputing for mqtt payloads
if (((tag_filter == DeviceValueTAG::TAG_NONE) || (tag_filter == dv.tag)) && (dv.full_name != nullptr || !verbose)) {
@@ -616,6 +652,7 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
// handle Booleans (true, false)
if ((dv.type == DeviceValueType::BOOL) && Helpers::hasValue(*(uint8_t *)(dv.value_p), EMS_VALUE_BOOL)) {
// see if we have options for the bool's
// if (dv.options_size == 2) {
if (dv.options_size == 2) {
json[name] = *(uint8_t *)(dv.value_p) ? dv.options[0] : dv.options[1];
has_value = true;
@@ -645,6 +682,7 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
// handle ENUMs
else if ((dv.type == DeviceValueType::ENUM) && Helpers::hasValue(*(uint8_t *)(dv.value_p))) {
// if (*(uint8_t *)(dv.value_p) < dv.options_size) {
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
json[name] = dv.options[*(uint8_t *)(dv.value_p)];
has_value = true;
@@ -715,7 +753,8 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
// create the Home Assistant configs for each value as a sensor
void EMSdevice::publish_mqtt_ha_sensor() {
for (const auto & dv : *devicevalues_) {
// for (const auto & dv : *devicevalues_) {
for (const auto & dv : devicevalues_) {
Mqtt::register_mqtt_ha_sensor(dv.type, dv.tag, dv.full_name, device_type_, dv.short_name, dv.uom);
}
@@ -733,7 +772,7 @@ std::string EMSdevice::telegram_type_name(std::shared_ptr<const Telegram> telegr
return read_flash_string(F("UBADevices"));
}
for (const auto & tf : *telegram_functions_) {
for (const auto & tf : telegram_functions_) {
if ((tf.telegram_type_id_ == telegram->type_id) && (telegram->type_id != 0xFF)) {
return uuid::read_flash_string(tf.telegram_type_name_);
}
@@ -745,7 +784,7 @@ std::string EMSdevice::telegram_type_name(std::shared_ptr<const Telegram> telegr
// take a telegram_type_id and call the matching handler
// return true if match found
bool EMSdevice::handle_telegram(std::shared_ptr<const Telegram> telegram) {
for (const auto & tf : *telegram_functions_) {
for (const auto & tf : telegram_functions_) {
if (tf.telegram_type_id_ == telegram->type_id) {
// if the data block is empty, assume that this telegram is not recognized by the bus master
// so remove it from the automatic fetch list