mqtt: refactor big messy functions to smaller messy functions

This commit is contained in:
Torrie Fischer 2023-12-20 12:16:20 +01:00
parent 31e4072305
commit 3b2951db79
2 changed files with 81 additions and 117 deletions

View File

@ -6,7 +6,7 @@
#include "../../Config.h" #include "../../Config.h"
#include "../../Platform.h" #include "../../Platform.h"
StaticJsonDocument<1024> m_json; static StaticJsonDocument<1024> m_json;
struct MQTTDevice { struct MQTTDevice {
const String id; const String id;
@ -108,24 +108,8 @@ MQTTTelemetry::MQTTTelemetry() : BufferedInputSource("MQTT"),
} }
void void
MQTTTelemetry::handleEventOnline(const InputEvent& evt) MQTTTelemetry::publishScenes()
{ {
if (!m_mqtt.connected()) {
Log.notice("Connecting to MQTT as %s on %s...", Platform::deviceID(), Device.availabilityTopic.c_str());
if (m_mqtt.connect(Platform::deviceID(), NULL, NULL, Device.availabilityTopic.c_str(), 0, true, "offline")) {
Log.notice("Connected to MQTT");
String logTopic = m_debugTopic + "/log";
Log.info("MQTT logs are available at %s", logTopic.c_str());
m_needHeartbeat = true;
m_json.clear();
Lightswitch.toJson(m_json);
m_json["brightness"] = true;
m_json["rgb"] = true;
publishDoc(Lightswitch.configTopic().c_str(), true);
m_mqtt.subscribe(Lightswitch.commandTopic().c_str());
for(auto scene : Static<Sequencer>::instance()->scenes()) { for(auto scene : Static<Sequencer>::instance()->scenes()) {
m_json.clear(); m_json.clear();
String strName{scene.name}; String strName{scene.name};
@ -142,17 +126,32 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
m_mqtt.subscribe(sceneObj.commandTopic().c_str()); m_mqtt.subscribe(sceneObj.commandTopic().c_str());
Log.info("Published scene %s", sceneObj.configTopic().c_str()); Log.info("Published scene %s", sceneObj.configTopic().c_str());
} }
}
void
MQTTTelemetry::onMQTTOnline()
{
Log.notice("Connected to MQTT");
String logTopic = m_debugTopic + "/log";
Log.info("MQTT logs are available at %s", logTopic.c_str());
m_needHeartbeat = true;
m_json.clear();
Lightswitch.toJson(m_json);
m_json["brightness"] = true;
m_json["rgb"] = true;
publishDoc(Lightswitch.configTopic().c_str(), true);
m_mqtt.subscribe(Lightswitch.commandTopic().c_str());
publishScenes();
m_json.clear(); m_json.clear();
FPSSensor.toJson(m_json, false); FPSSensor.toJson(m_json, false);
m_json["unit_of_meas"] = "Frames/s"; m_json["unit_of_meas"] = "Frames/s";
publishDoc(FPSSensor.configTopic().c_str(), true); publishDoc(FPSSensor.configTopic().c_str(), true);
//Log.verbose("Publish %s %s", FPSSensor.configTopic().c_str(), buf); #ifdef ESP8266
//m_mqtt.publish(FPSSensor.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
m_mqtt.subscribe(FPSSensor.commandTopic().c_str());
#ifdef BOARD_ESP8266
struct rst_info resetInfo = *ESP.getResetInfoPtr(); struct rst_info resetInfo = *ESP.getResetInfoPtr();
if (resetInfo.reason != 0) { if (resetInfo.reason != 0) {
char buff[200]; char buff[200];
@ -160,6 +159,15 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
Log.warning("Previous crash detected! %s", buff); Log.warning("Previous crash detected! %s", buff);
} }
#endif #endif
}
void
MQTTTelemetry::handleEventOnline(const InputEvent& evt)
{
if (!m_mqtt.connected()) {
Log.notice("Connecting to MQTT as %s on %s...", Platform::deviceID(), Device.availabilityTopic.c_str());
if (m_mqtt.connect(Platform::deviceID(), NULL, NULL, Device.availabilityTopic.c_str(), 0, true, "offline")) {
onMQTTOnline();
} else { } else {
Log.warning("Could not connect to MQTT"); Log.warning("Could not connect to MQTT");
} }
@ -185,7 +193,6 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
publishDoc(statTopic.c_str()); publishDoc(statTopic.c_str());
} else if (evt.intent == InputEvent::SetScene) { } else if (evt.intent == InputEvent::SetScene) {
m_json.clear(); m_json.clear();
m_json["effect"] = evt.asString();
m_json["state"] = m_isOn ? "ON" : "OFF"; m_json["state"] = m_isOn ? "ON" : "OFF";
publishDoc(statTopic.c_str()); publishDoc(statTopic.c_str());
} }
@ -246,25 +253,29 @@ MQTTTelemetry::loopOnline()
m_needHeartbeat = true; m_needHeartbeat = true;
} }
if (m_needHeartbeat) { if (m_needHeartbeat) {
publishHeartbeat();
m_needHeartbeat = false;
}
}
void
MQTTTelemetry::publishHeartbeat()
{
m_json.clear(); m_json.clear();
m_json["device_id"] = Platform::deviceID(); m_json["device_id"] = Platform::deviceID();
m_json["sketch_version"] = ESP.getSketchMD5(); m_json["sketch_version"] = ESP.getSketchMD5();
m_json["os_version"] = ESP.getSdkVersion(); m_json["os_version"] = Platform::version();
m_json["localip"] = WiFi.localIP().toString(); m_json["localip"] = WiFi.localIP().toString();
m_json["pixelCount"] = Static<ConfigService>::instance()->coordMap()->physicalPixelCount(); m_json["pixelCount"] = Static<ConfigService>::instance()->coordMap()->physicalPixelCount();
m_json["loadedProfile"] = Static<ConfigService>::instance()->loadedProfile(); m_json["loadedProfile"] = Static<ConfigService>::instance()->loadedProfile();
m_json["RSSI"] = WiFi.RSSI(); m_json["RSSI"] = WiFi.RSSI();
m_json["free_ram"] = ESP.getFreeHeap(); m_json["free_ram"] = Platform::freeRam();
m_json["fps"] = FastLED.getFPS(); m_json["fps"] = FastLED.getFPS();
String availTopic = m_rootTopic + "/available";
publishDoc(Lightswitch.heartbeatTopic().c_str()); publishDoc(Lightswitch.heartbeatTopic().c_str());
m_mqtt.publish(Device.availabilityTopic.c_str(), "online"); m_mqtt.publish(Device.availabilityTopic.c_str(), "online");
String fpsCounter = String(FastLED.getFPS()); String fpsCounter = String(FastLED.getFPS());
m_mqtt.publish(FPSSensor.statTopic().c_str(), fpsCounter.c_str()); m_mqtt.publish(FPSSensor.statTopic().c_str(), fpsCounter.c_str());
m_needHeartbeat = false;
}
} }
void void
@ -285,57 +296,6 @@ MQTTTelemetry::callback(char* topic, const char* payload)
} }
} }
if (m_json.containsKey("start")) {
strcpy(m_patternBuf, m_json["start"].as<const char*>());
setEvent(InputEvent{InputEvent::StartThing, m_patternBuf});
}
if (m_json.containsKey("stop")) {
if (m_json["stop"] == name) {
Log.warning("You can't kill an idea, or stop the MQTT Task via MQTT.");
} else {
strcpy(m_patternBuf, m_json["stop"].as<const char*>());
setEvent(InputEvent{InputEvent::StopThing, m_patternBuf});
}
}
if (m_json.containsKey("pixelCount")) {
Log.notice("Pixel count changed");
setEvent(InputEvent{InputEvent::SetDisplayLength, m_json["pixelCount"].as<int>()});
}
if (m_json.containsKey("startPixel")) {
Log.notice("Start pixel changed");
setEvent(InputEvent{InputEvent::SetDisplayOffset, m_json["startPixel"].as<int>()});
}
if (m_json.containsKey("loadConfig")) {
Log.notice("Loading new config");
setEvent(InputEvent{InputEvent::LoadConfigurationByName, m_json["loadConfig"].as<const char*>()});
}
if (m_json.containsKey("save")) {
setEvent(InputEvent{InputEvent::SaveConfigurationRequest});
}
if (m_json.containsKey("restart")) {
Platform::restart();
}
if (m_json.containsKey("reconnect")) {
m_mqtt.disconnect();
}
if (m_json.containsKey("ping")) {
m_needHeartbeat = true;
Log.notice("Queuing up heartbeat");
}
if (m_json.containsKey("effect")) {
strcpy(m_patternBuf, m_json["effect"].as<const char*>());
setEvent(InputEvent{InputEvent::SetScene, m_patternBuf});
}
if (m_json.containsKey("color")) { if (m_json.containsKey("color")) {
uint8_t r = m_json["color"]["r"]; uint8_t r = m_json["color"]["r"];
uint8_t g = m_json["color"]["g"]; uint8_t g = m_json["color"]["g"];

View File

@ -73,6 +73,10 @@ class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin, ConfigTaskMix
void publishDoc(const char* topic); void publishDoc(const char* topic);
void publishDoc(const char* topic, bool retain); void publishDoc(const char* topic, bool retain);
void onMQTTOnline();
void publishScenes();
void publishHeartbeat();
Sequencer *m_sequencer = 0; Sequencer *m_sequencer = 0;
WiFiClient m_wifi; WiFiClient m_wifi;
PubSubClient m_mqtt; PubSubClient m_mqtt;