Skip to content

Commit

Permalink
Updating mqtt related libraries to add move semantic
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Nov 16, 2024
1 parent c1e7a58 commit 407b80e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 80 deletions.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ lib_deps =
mathieucarbou/MycilaDS18 @ 4.1.1
mathieucarbou/MycilaESPConnect @ 7.0.0
mathieucarbou/MycilaEasyDisplay @ 3.0.2
mathieucarbou/MycilaHADiscovery @ 6.0.0
mathieucarbou/MycilaHADiscovery @ 6.0.1
mathieucarbou/MycilaJSY @ 11.1.4
mathieucarbou/MycilaLogger @ 3.2.1
mathieucarbou/MycilaMQTT @ 6.0.0
mathieucarbou/MycilaMQTT @ 6.0.2
mathieucarbou/MycilaNTP @ 6.0.3
mathieucarbou/MycilaPulseAnalyzer @ 2.5.0
mathieucarbou/MycilaPZEM004Tv3 @ 5.0.0
Expand Down
8 changes: 3 additions & 5 deletions src/init/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ Mycila::Task initConfigTask("Init Config", [](void* params) {
Mycila::NTP.setTimeZone(config.get(KEY_NTP_TIMEZONE));

// Home Assistant Discovery
haDiscovery.setDiscoveryTopic(config.get(KEY_HA_DISCOVERY_TOPIC));
haDiscovery.setWillTopic((std::string(config.get(KEY_MQTT_TOPIC)) + YASOLR_MQTT_WILL_TOPIC).c_str());
haDiscovery.setDiscoveryTopic(config.getString(KEY_HA_DISCOVERY_TOPIC));
haDiscovery.setWillTopic(config.getString(KEY_MQTT_TOPIC) + YASOLR_MQTT_WILL_TOPIC);
haDiscovery.begin({
.id = Mycila::AppInfo.defaultMqttClientId,
.name = Mycila::AppInfo.defaultSSID,
Expand All @@ -116,9 +116,7 @@ Mycila::Task initConfigTask("Init Config", [](void* params) {
},
config.get(KEY_MQTT_TOPIC),
512,
[](const char* topic, const char* payload) {
mqtt.publish(topic, payload, true);
});
[](const char* topic, const char* payload) { mqtt.publish(topic, payload, true); });

// Lights
if (config.getBool(KEY_ENABLE_LIGHTS))
Expand Down
24 changes: 12 additions & 12 deletions src/init/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Mycila::Task initMqttSubscribersTask("Init MQTT Subscribers", [](void* params) {

// config

mqtt.subscribe((baseTopic + "/config/+/set").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/config/+/set", [](const std::string& topic, const std::string_view& payload) {
const std::size_t end = topic.rfind("/set");
if (end == std::string::npos)
return;
Expand All @@ -25,7 +25,7 @@ Mycila::Task initMqttSubscribersTask("Init MQTT Subscribers", [](void* params) {

// relays

mqtt.subscribe((baseTopic + "/router/relay1/set").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/router/relay1/set", [](const std::string& topic, const std::string_view& payload) {
if (relay1.isEnabled()) {
const std::string_view state = payload.substr(0, payload.find("="));
if (state.empty())
Expand All @@ -37,7 +37,7 @@ Mycila::Task initMqttSubscribersTask("Init MQTT Subscribers", [](void* params) {
}
});

mqtt.subscribe((baseTopic + "/router/relay2/set").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/router/relay2/set", [](const std::string& topic, const std::string_view& payload) {
if (relay2.isEnabled()) {
const std::string_view state = payload.substr(0, payload.find("="));
if (state.empty())
Expand All @@ -52,15 +52,15 @@ Mycila::Task initMqttSubscribersTask("Init MQTT Subscribers", [](void* params) {

// router

mqtt.subscribe((baseTopic + "/router/output1/duty_cycle/set").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/router/output1/duty_cycle/set", [](const std::string& topic, const std::string_view& payload) {
output1.setDimmerDutyCycle(std::stof(std::string(payload)) / 100);
});

mqtt.subscribe((baseTopic + "/router/output2/duty_cycle/set").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/router/output2/duty_cycle/set", [](const std::string& topic, const std::string_view& payload) {
output2.setDimmerDutyCycle(std::stof(std::string(payload)) / 100);
});

mqtt.subscribe((baseTopic + "/router/output1/bypass/set").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/router/output1/bypass/set", [](const std::string& topic, const std::string_view& payload) {
if (output1.isBypassEnabled()) {
if (payload == YASOLR_ON)
output1.setBypassOn();
Expand All @@ -69,7 +69,7 @@ Mycila::Task initMqttSubscribersTask("Init MQTT Subscribers", [](void* params) {
}
});

mqtt.subscribe((baseTopic + "/router/output2/bypass/set").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/router/output2/bypass/set", [](const std::string& topic, const std::string_view& payload) {
if (output2.isBypassEnabled()) {
if (payload == YASOLR_ON)
output2.setBypassOn();
Expand All @@ -80,15 +80,15 @@ Mycila::Task initMqttSubscribersTask("Init MQTT Subscribers", [](void* params) {

// device

mqtt.subscribe((baseTopic + "/system/device/restart").c_str(), [](const std::string& topic, const std::string_view& payload) {
mqtt.subscribe(baseTopic + "/system/device/restart", [](const std::string& topic, const std::string_view& payload) {
restartTask.resume();
});

// grid power
const std::string& gridPowerMQTTTopic = config.getString(KEY_GRID_POWER_MQTT_TOPIC);
if (!gridPowerMQTTTopic.empty()) {
logger.info(TAG, "Reading Grid Power from MQTT topic: %s", gridPowerMQTTTopic.c_str());
mqtt.subscribe(gridPowerMQTTTopic.c_str(), [](const std::string& topic, const std::string_view& payload) {
const char* gridPowerMQTTTopic = config.get(KEY_GRID_POWER_MQTT_TOPIC);
if (gridPowerMQTTTopic[0] != '\0') {
logger.info(TAG, "Reading Grid Power from MQTT topic: %s", gridPowerMQTTTopic);
mqtt.subscribe(gridPowerMQTTTopic, [](const std::string& topic, const std::string_view& payload) {
float p = std::stof(std::string(payload));
logger.debug(TAG, "Grid Power from MQTT: %f", p);
grid.mqttPower().update(p);
Expand Down
122 changes: 61 additions & 61 deletions src/tasks/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,30 @@ Mycila::Task mqttPublishStaticTask("MQTT Static", Mycila::TaskType::ONCE, [](voi
logger.debug(TAG, "Publishing static data to MQTT");
const std::string& baseTopic = config.getString(KEY_MQTT_TOPIC);

mqtt.publish((baseTopic + "/system/app/manufacturer").c_str(), Mycila::AppInfo.manufacturer, true);
mqtt.publish((baseTopic + "/system/app/model").c_str(), Mycila::AppInfo.model, true);
mqtt.publish((baseTopic + "/system/app/name").c_str(), Mycila::AppInfo.name, true);
mqtt.publish((baseTopic + "/system/app/trial").c_str(), YASOLR_BOOL(Mycila::AppInfo.trial), true);
mqtt.publish((baseTopic + "/system/app/version").c_str(), Mycila::AppInfo.version, true);
mqtt.publish(baseTopic + "/system/app/manufacturer", Mycila::AppInfo.manufacturer, true);
mqtt.publish(baseTopic + "/system/app/model", Mycila::AppInfo.model, true);
mqtt.publish(baseTopic + "/system/app/name", Mycila::AppInfo.name, true);
mqtt.publish(baseTopic + "/system/app/trial", YASOLR_BOOL(Mycila::AppInfo.trial), true);
mqtt.publish(baseTopic + "/system/app/version", Mycila::AppInfo.version, true);
yield();

mqtt.publish((baseTopic + "/system/device/boots").c_str(), std::to_string(Mycila::System::getBootCount()), true);
mqtt.publish((baseTopic + "/system/device/cores").c_str(), std::to_string(ESP.getChipCores()), true);
mqtt.publish((baseTopic + "/system/device/cpu_freq").c_str(), std::to_string(ESP.getCpuFreqMHz()), true);
mqtt.publish((baseTopic + "/system/device/id").c_str(), Mycila::AppInfo.id, true);
mqtt.publish((baseTopic + "/system/device/model").c_str(), ESP.getChipModel(), true);
mqtt.publish(baseTopic + "/system/device/boots", std::to_string(Mycila::System::getBootCount()), true);
mqtt.publish(baseTopic + "/system/device/cores", std::to_string(ESP.getChipCores()), true);
mqtt.publish(baseTopic + "/system/device/cpu_freq", std::to_string(ESP.getCpuFreqMHz()), true);
mqtt.publish(baseTopic + "/system/device/id", Mycila::AppInfo.id, true);
mqtt.publish(baseTopic + "/system/device/model", ESP.getChipModel(), true);
yield();

mqtt.publish((baseTopic + "/system/firmware/build/branch").c_str(), Mycila::AppInfo.buildBranch, true);
mqtt.publish((baseTopic + "/system/firmware/build/hash").c_str(), Mycila::AppInfo.buildHash, true);
mqtt.publish((baseTopic + "/system/firmware/build/timestamp").c_str(), Mycila::AppInfo.buildDate, true);
mqtt.publish((baseTopic + "/system/firmware/debug").c_str(), YASOLR_BOOL(Mycila::AppInfo.debug), true);
mqtt.publish((baseTopic + "/system/firmware/filename").c_str(), Mycila::AppInfo.firmware, true);
mqtt.publish(baseTopic + "/system/firmware/build/branch", Mycila::AppInfo.buildBranch, true);
mqtt.publish(baseTopic + "/system/firmware/build/hash", Mycila::AppInfo.buildHash, true);
mqtt.publish(baseTopic + "/system/firmware/build/timestamp", Mycila::AppInfo.buildDate, true);
mqtt.publish(baseTopic + "/system/firmware/debug", YASOLR_BOOL(Mycila::AppInfo.debug), true);
mqtt.publish(baseTopic + "/system/firmware/filename", Mycila::AppInfo.firmware, true);
yield();

mqtt.publish((baseTopic + "/system/network/eth/mac_address").c_str(), espConnect.getMACAddress(Mycila::ESPConnect::Mode::ETH), true);
mqtt.publish((baseTopic + "/system/network/hostname").c_str(), espConnect.getHostname(), true);
mqtt.publish((baseTopic + "/system/network/wifi/mac_address").c_str(), espConnect.getMACAddress(Mycila::ESPConnect::Mode::STA), true);
mqtt.publish(baseTopic + "/system/network/eth/mac_address", espConnect.getMACAddress(Mycila::ESPConnect::Mode::ETH), true);
mqtt.publish(baseTopic + "/system/network/hostname", espConnect.getHostname(), true);
mqtt.publish(baseTopic + "/system/network/wifi/mac_address", espConnect.getMACAddress(Mycila::ESPConnect::Mode::STA), true);
yield();
});

Expand All @@ -85,7 +85,7 @@ Mycila::Task mqttPublishConfigTask("MQTT Config", Mycila::TaskType::ONCE, [](voi
const char* value = config.get(key);
if (value[0] != '\0' && config.isPasswordKey(key))
value = "********";
mqtt.publish((baseTopic + "/config/" + key).c_str(), value, true);
mqtt.publish(baseTopic + "/config/" + key, value, true);
}
});

Expand All @@ -95,73 +95,73 @@ Mycila::Task mqttPublishTask("MQTT", [](void* params) {
Mycila::System::Memory memory;
Mycila::System::getMemory(memory);

mqtt.publish((baseTopic + "/system/device/heap/total").c_str(), std::to_string(memory.total));
mqtt.publish((baseTopic + "/system/device/heap/usage").c_str(), std::to_string(memory.usage));
mqtt.publish((baseTopic + "/system/device/heap/used").c_str(), std::to_string(memory.used));
mqtt.publish((baseTopic + "/system/device/uptime").c_str(), std::to_string(Mycila::System::getUptime()));
mqtt.publish(baseTopic + "/system/device/heap/total", std::to_string(memory.total));
mqtt.publish(baseTopic + "/system/device/heap/usage", std::to_string(memory.usage));
mqtt.publish(baseTopic + "/system/device/heap/used", std::to_string(memory.used));
mqtt.publish(baseTopic + "/system/device/uptime", std::to_string(Mycila::System::getUptime()));
yield();

mqtt.publish((baseTopic + "/system/network/eth/ip_address").c_str(), espConnect.getIPAddress(Mycila::ESPConnect::Mode::ETH).toString().c_str());
mqtt.publish((baseTopic + "/system/network/ip_address").c_str(), espConnect.getIPAddress().toString().c_str());
mqtt.publish((baseTopic + "/system/network/mac_address").c_str(), espConnect.getMACAddress());
mqtt.publish((baseTopic + "/system/network/ntp").c_str(), YASOLR_STATE(Mycila::NTP.isSynced()));
mqtt.publish((baseTopic + "/system/network/wifi/bssid").c_str(), espConnect.getWiFiBSSID());
mqtt.publish((baseTopic + "/system/network/wifi/ip_address").c_str(), espConnect.getIPAddress(Mycila::ESPConnect::Mode::STA).toString().c_str());
mqtt.publish((baseTopic + "/system/network/wifi/quality").c_str(), std::to_string(espConnect.getWiFiSignalQuality()));
mqtt.publish((baseTopic + "/system/network/wifi/rssi").c_str(), std::to_string(espConnect.getWiFiRSSI()));
mqtt.publish((baseTopic + "/system/network/wifi/ssid").c_str(), espConnect.getWiFiSSID());
mqtt.publish(baseTopic + "/system/network/eth/ip_address", espConnect.getIPAddress(Mycila::ESPConnect::Mode::ETH).toString().c_str());
mqtt.publish(baseTopic + "/system/network/ip_address", espConnect.getIPAddress().toString().c_str());
mqtt.publish(baseTopic + "/system/network/mac_address", espConnect.getMACAddress());
mqtt.publish(baseTopic + "/system/network/ntp", YASOLR_STATE(Mycila::NTP.isSynced()));
mqtt.publish(baseTopic + "/system/network/wifi/bssid", espConnect.getWiFiBSSID());
mqtt.publish(baseTopic + "/system/network/wifi/ip_address", espConnect.getIPAddress(Mycila::ESPConnect::Mode::STA).toString().c_str());
mqtt.publish(baseTopic + "/system/network/wifi/quality", std::to_string(espConnect.getWiFiSignalQuality()));
mqtt.publish(baseTopic + "/system/network/wifi/rssi", std::to_string(espConnect.getWiFiRSSI()));
mqtt.publish(baseTopic + "/system/network/wifi/ssid", espConnect.getWiFiSSID());
yield();

switch (espConnect.getMode()) {
case Mycila::ESPConnect::Mode::ETH:
mqtt.publish((baseTopic + "/system/network/mode").c_str(), "eth");
mqtt.publish(baseTopic + "/system/network/mode", "eth");
break;
case Mycila::ESPConnect::Mode::STA:
mqtt.publish((baseTopic + "/system/network/mode").c_str(), "wifi");
mqtt.publish(baseTopic + "/system/network/mode", "wifi");
break;
case Mycila::ESPConnect::Mode::AP:
mqtt.publish((baseTopic + "/system/network/mode").c_str(), "ap");
mqtt.publish(baseTopic + "/system/network/mode", "ap");
break;
default:
mqtt.publish((baseTopic + "/system/network/mode").c_str(), "");
mqtt.publish(baseTopic + "/system/network/mode", "");
break;
}

Mycila::Grid::Metrics gridMetrics;
grid.getMeasurements(gridMetrics);
mqtt.publish((baseTopic + "/grid/apparent_power").c_str(), std::to_string(gridMetrics.apparentPower));
mqtt.publish((baseTopic + "/grid/current").c_str(), std::to_string(gridMetrics.current));
mqtt.publish((baseTopic + "/grid/energy").c_str(), std::to_string(gridMetrics.energy));
mqtt.publish((baseTopic + "/grid/energy_returned").c_str(), std::to_string(gridMetrics.energyReturned));
mqtt.publish((baseTopic + "/grid/frequency").c_str(), std::to_string(gridMetrics.frequency));
mqtt.publish((baseTopic + "/grid/online").c_str(), YASOLR_BOOL(grid.isConnected()));
mqtt.publish((baseTopic + "/grid/power").c_str(), std::to_string(gridMetrics.power));
mqtt.publish((baseTopic + "/grid/power_factor").c_str(), std::to_string(gridMetrics.powerFactor));
mqtt.publish((baseTopic + "/grid/voltage").c_str(), std::to_string(gridMetrics.voltage));
mqtt.publish(baseTopic + "/grid/apparent_power", std::to_string(gridMetrics.apparentPower));
mqtt.publish(baseTopic + "/grid/current", std::to_string(gridMetrics.current));
mqtt.publish(baseTopic + "/grid/energy", std::to_string(gridMetrics.energy));
mqtt.publish(baseTopic + "/grid/energy_returned", std::to_string(gridMetrics.energyReturned));
mqtt.publish(baseTopic + "/grid/frequency", std::to_string(gridMetrics.frequency));
mqtt.publish(baseTopic + "/grid/online", YASOLR_BOOL(grid.isConnected()));
mqtt.publish(baseTopic + "/grid/power", std::to_string(gridMetrics.power));
mqtt.publish(baseTopic + "/grid/power_factor", std::to_string(gridMetrics.powerFactor));
mqtt.publish(baseTopic + "/grid/voltage", std::to_string(gridMetrics.voltage));
yield();

Mycila::Router::Metrics routerMeasurements;
router.getMeasurements(routerMeasurements);
mqtt.publish((baseTopic + "/router/apparent_power").c_str(), std::to_string(routerMeasurements.apparentPower));
mqtt.publish((baseTopic + "/router/current").c_str(), std::to_string(routerMeasurements.current));
mqtt.publish((baseTopic + "/router/energy").c_str(), std::to_string(routerMeasurements.energy));
mqtt.publish((baseTopic + "/router/lights").c_str(), lights.toString());
mqtt.publish((baseTopic + "/router/power_factor").c_str(), std::to_string(routerMeasurements.powerFactor));
mqtt.publish((baseTopic + "/router/power").c_str(), std::to_string(routerMeasurements.power));
mqtt.publish((baseTopic + "/router/relay1").c_str(), YASOLR_STATE(relay1.isOn()));
mqtt.publish((baseTopic + "/router/relay2").c_str(), YASOLR_STATE(relay2.isOn()));
mqtt.publish((baseTopic + "/router/temperature").c_str(), std::to_string(ds18Sys.getTemperature().value_or(0)));
mqtt.publish((baseTopic + "/router/thdi").c_str(), std::to_string(routerMeasurements.thdi));
mqtt.publish((baseTopic + "/router/virtual_grid_power").c_str(), std::to_string(gridMetrics.power - routerMeasurements.power));
mqtt.publish(baseTopic + "/router/apparent_power", std::to_string(routerMeasurements.apparentPower));
mqtt.publish(baseTopic + "/router/current", std::to_string(routerMeasurements.current));
mqtt.publish(baseTopic + "/router/energy", std::to_string(routerMeasurements.energy));
mqtt.publish(baseTopic + "/router/lights", lights.toString());
mqtt.publish(baseTopic + "/router/power_factor", std::to_string(routerMeasurements.powerFactor));
mqtt.publish(baseTopic + "/router/power", std::to_string(routerMeasurements.power));
mqtt.publish(baseTopic + "/router/relay1", YASOLR_STATE(relay1.isOn()));
mqtt.publish(baseTopic + "/router/relay2", YASOLR_STATE(relay2.isOn()));
mqtt.publish(baseTopic + "/router/temperature", std::to_string(ds18Sys.getTemperature().value_or(0)));
mqtt.publish(baseTopic + "/router/thdi", std::to_string(routerMeasurements.thdi));
mqtt.publish(baseTopic + "/router/virtual_grid_power", std::to_string(gridMetrics.power - routerMeasurements.power));
yield();

for (const auto& output : router.getOutputs()) {
const std::string outputTopic = baseTopic + "/router/" + output->getName();
mqtt.publish((outputTopic + "/state").c_str(), output->getStateName());
mqtt.publish((outputTopic + "/bypass").c_str(), YASOLR_STATE(output->isBypassOn()));
mqtt.publish((outputTopic + "/dimmer").c_str(), YASOLR_STATE(output->isDimmerOn()));
mqtt.publish((outputTopic + "/duty_cycle").c_str(), std::to_string(output->getDimmerDutyCycle() * 100));
mqtt.publish((outputTopic + "/temperature").c_str(), std::to_string(output->temperature().orElse(0)));
mqtt.publish(outputTopic + "/state", output->getStateName());
mqtt.publish(outputTopic + "/bypass", YASOLR_STATE(output->isBypassOn()));
mqtt.publish(outputTopic + "/dimmer", YASOLR_STATE(output->isDimmerOn()));
mqtt.publish(outputTopic + "/duty_cycle", std::to_string(output->getDimmerDutyCycle() * 100));
mqtt.publish(outputTopic + "/temperature", std::to_string(output->temperature().orElse(0)));
yield();
}
});

0 comments on commit 407b80e

Please sign in to comment.