If the code hasn't been touched in this long, its probably release-worthy.
This commit is contained in:
@@ -117,39 +117,37 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
|
||||
Log.notice("Connected to MQTT");
|
||||
m_needHeartbeat = true;
|
||||
|
||||
StaticJsonDocument<1024> configJson;
|
||||
|
||||
Lightswitch.toJson(configJson);
|
||||
m_json.clear();
|
||||
Lightswitch.toJson(m_json);
|
||||
|
||||
int i = 0;
|
||||
for(const Sequencer::Scene& scene : m_sequencer->scenes()) {
|
||||
configJson["fx_list"][i++] = scene.name;
|
||||
m_json["fx_list"][i++] = scene.name;
|
||||
}
|
||||
configJson["brightness"] = true;
|
||||
configJson["rgb"] = true;
|
||||
m_json["brightness"] = true;
|
||||
m_json["rgb"] = true;
|
||||
|
||||
char buf[1024];
|
||||
serializeJson(configJson, buf, sizeof(buf));
|
||||
publishDoc(Lightswitch.configTopic().c_str(), true);
|
||||
|
||||
Log.verbose("Publish %s %s", Lightswitch.configTopic().c_str(), buf);
|
||||
m_mqtt.publish(Lightswitch.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
//Log.verbose("Publish %s %s", Lightswitch.configTopic().c_str(), buf);
|
||||
//m_mqtt.publish(Lightswitch.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
m_mqtt.subscribe(Lightswitch.commandTopic().c_str());
|
||||
|
||||
configJson.clear();
|
||||
flashlightSwitch.toJson(configJson, false);
|
||||
configJson["cmd_t"] = "~/set";
|
||||
configJson["ret"] = true;
|
||||
serializeJson(configJson, buf, sizeof(buf));
|
||||
m_mqtt.publish(flashlightSwitch.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
m_json.clear();
|
||||
flashlightSwitch.toJson(m_json, false);
|
||||
m_json["cmd_t"] = "~/set";
|
||||
m_json["ret"] = true;
|
||||
publishDoc(flashlightSwitch.configTopic().c_str(), true);
|
||||
//m_mqtt.publish(flashlightSwitch.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
m_mqtt.subscribe(flashlightSwitch.commandTopic().c_str());
|
||||
|
||||
configJson.clear();
|
||||
FPSSensor.toJson(configJson, false);
|
||||
configJson["unit_of_meas"] = "Frames/s";
|
||||
serializeJson(configJson, buf, sizeof(buf));
|
||||
m_json.clear();
|
||||
FPSSensor.toJson(m_json, false);
|
||||
m_json["unit_of_meas"] = "Frames/s";
|
||||
publishDoc(FPSSensor.configTopic().c_str(), true);
|
||||
|
||||
Log.verbose("Publish %s %s", FPSSensor.configTopic().c_str(), buf);
|
||||
m_mqtt.publish(FPSSensor.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
//Log.verbose("Publish %s %s", FPSSensor.configTopic().c_str(), buf);
|
||||
//m_mqtt.publish(FPSSensor.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
m_mqtt.subscribe(FPSSensor.commandTopic().c_str());
|
||||
|
||||
#ifdef BOARD_ESP8266
|
||||
@@ -172,39 +170,28 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
|
||||
String flashlightStatTopic = flashlightSwitch.statTopic();
|
||||
m_mqtt.publish(flashlightStatTopic.c_str(), "ON");
|
||||
} else if (evt.intent == InputEvent::SetPower) {
|
||||
StaticJsonDocument<256> doc;
|
||||
char buf[256];
|
||||
m_json.clear();
|
||||
m_isOn = evt.asInt() ? true : false;
|
||||
doc["state"] = m_isOn ? "ON" : "OFF";
|
||||
serializeJson(doc, buf, sizeof(buf));
|
||||
m_mqtt.publish(statTopic.c_str(), buf);
|
||||
m_json["state"] = m_isOn ? "ON" : "OFF";
|
||||
publishDoc(statTopic.c_str());
|
||||
} else if (evt.intent == InputEvent::SetBrightness) {
|
||||
StaticJsonDocument<256> doc;
|
||||
char buf[256];
|
||||
doc["brightness"] = evt.asInt();
|
||||
doc["state"] = m_isOn ? "ON" : "OFF";
|
||||
serializeJson(doc, buf, sizeof(buf));
|
||||
m_mqtt.publish(statTopic.c_str(), buf);
|
||||
m_json.clear();
|
||||
m_json["brightness"] = evt.asInt();
|
||||
m_json["state"] = m_isOn ? "ON" : "OFF";
|
||||
publishDoc(statTopic.c_str());
|
||||
} else if (evt.intent == InputEvent::SetColor) {
|
||||
StaticJsonDocument<256> doc;
|
||||
char buf[256];
|
||||
CRGB color = evt.asRGB();
|
||||
doc["color"]["r"] = color.r;
|
||||
doc["color"]["g"] = color.g;
|
||||
doc["color"]["b"] = color.b;
|
||||
doc["state"] = m_isOn ? "ON" : "OFF";
|
||||
serializeJson(doc, buf, sizeof(buf));
|
||||
m_mqtt.publish(statTopic.c_str(), buf);
|
||||
m_json.clear();
|
||||
m_json["color"]["r"] = color.r;
|
||||
m_json["color"]["g"] = color.g;
|
||||
m_json["color"]["b"] = color.b;
|
||||
m_json["state"] = m_isOn ? "ON" : "OFF";
|
||||
publishDoc(statTopic.c_str());
|
||||
} else if (evt.intent == InputEvent::SetPattern) {
|
||||
StaticJsonDocument<256> doc;
|
||||
char buf[256];
|
||||
doc["effect"] = evt.asString();
|
||||
doc["state"] = m_isOn ? "ON" : "OFF";
|
||||
serializeJson(doc, buf, sizeof(buf));
|
||||
m_mqtt.publish(statTopic.c_str(), buf);
|
||||
} else if (evt.intent == InputEvent::FirmwareUpdate) {
|
||||
String updateTopic = m_debugTopic + "/firmware";
|
||||
m_mqtt.publish(updateTopic.c_str(), "firmware update!");
|
||||
m_json.clear();
|
||||
m_json["effect"] = evt.asString();
|
||||
m_json["state"] = m_isOn ? "ON" : "OFF";
|
||||
publishDoc(statTopic.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -241,22 +228,20 @@ MQTTTelemetry::loopOnline()
|
||||
m_needHeartbeat = true;
|
||||
}
|
||||
if (m_needHeartbeat) {
|
||||
char buf[512];
|
||||
StaticJsonDocument<512> response;
|
||||
response["device_id"] = Platform::deviceID();
|
||||
response["sketch_version"] = ESP.getSketchMD5();
|
||||
response["os_version"] = ESP.getSdkVersion();
|
||||
response["localip"] = WiFi.localIP().toString();
|
||||
response["pixelCount"] = Static<ConfigService>::instance()->coordMap()->pixelCount;
|
||||
response["startPixel"] = Static<ConfigService>::instance()->coordMap()->startPixel;
|
||||
response["RSSI"] = WiFi.RSSI();
|
||||
response["free_ram"] = ESP.getFreeHeap();
|
||||
response["fps"] = FastLED.getFPS();
|
||||
serializeJson(response, buf, sizeof(buf));
|
||||
m_json.clear();
|
||||
m_json["device_id"] = Platform::deviceID();
|
||||
m_json["sketch_version"] = ESP.getSketchMD5();
|
||||
m_json["os_version"] = ESP.getSdkVersion();
|
||||
m_json["localip"] = WiFi.localIP().toString();
|
||||
m_json["pixelCount"] = Static<ConfigService>::instance()->coordMap()->physicalPixelCount();
|
||||
//m_json["startPixel"] = Static<ConfigService>::instance()->coordMap()->startPixel;
|
||||
m_json["RSSI"] = WiFi.RSSI();
|
||||
m_json["free_ram"] = ESP.getFreeHeap();
|
||||
m_json["fps"] = FastLED.getFPS();
|
||||
String availTopic = m_rootTopic + "/available";
|
||||
m_mqtt.publish(Lightswitch.heartbeatTopic().c_str(), buf);
|
||||
publishDoc(Lightswitch.heartbeatTopic().c_str());
|
||||
m_mqtt.publish(Device.availabilityTopic.c_str(), "online");
|
||||
//Log.notice("Heartbeat: %s", buf);
|
||||
//Log.trace("Heartbeat: %s", buf);
|
||||
|
||||
String fpsCounter = String(FastLED.getFPS());
|
||||
m_mqtt.publish(FPSSensor.statTopic().c_str(), fpsCounter.c_str());
|
||||
@@ -280,89 +265,106 @@ MQTTTelemetry::callback(char* topic, const char* payload)
|
||||
setEvent(InputEvent{InputEvent::SetPattern, "Idle"});
|
||||
}
|
||||
} else if (Lightswitch.isCommandTopic(topic)) {
|
||||
StaticJsonDocument<512> doc;
|
||||
deserializeJson(doc, payload);
|
||||
deserializeJson(m_json, payload);
|
||||
|
||||
if (doc.containsKey("state")) {
|
||||
if (doc["state"] == "ON") {
|
||||
if (m_json.containsKey("state")) {
|
||||
if (m_json["state"] == "ON") {
|
||||
Log.notice("Turning on power");
|
||||
setEvent(InputEvent{InputEvent::SetPower, true});
|
||||
} else if (doc["state"] == "OFF") {
|
||||
} else if (m_json["state"] == "OFF") {
|
||||
Log.notice("Turning off power");
|
||||
setEvent(InputEvent{InputEvent::SetPattern, "Idle"});
|
||||
setEvent(InputEvent{InputEvent::SetPower, false});
|
||||
}
|
||||
}
|
||||
|
||||
if (doc.containsKey("start")) {
|
||||
strcpy(m_patternBuf, doc["start"].as<const char*>());
|
||||
if (m_json.containsKey("start")) {
|
||||
strcpy(m_patternBuf, m_json["start"].as<const char*>());
|
||||
setEvent(InputEvent{InputEvent::StartThing, m_patternBuf});
|
||||
}
|
||||
|
||||
if (doc.containsKey("stop")) {
|
||||
if (doc["stop"] == name) {
|
||||
Log.notice("You can't kill an idea, or stop the MQTT Task via MQTT.");
|
||||
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, doc["stop"].as<const char*>());
|
||||
strcpy(m_patternBuf, m_json["stop"].as<const char*>());
|
||||
setEvent(InputEvent{InputEvent::StopThing, m_patternBuf});
|
||||
}
|
||||
}
|
||||
|
||||
if (doc.containsKey("pixelCount")) {
|
||||
setEvent(InputEvent{InputEvent::SetDisplayLength, (int)doc["pixelCount"]});
|
||||
if (m_json.containsKey("pixelCount")) {
|
||||
Log.notice("Pixel count changed");
|
||||
setEvent(InputEvent{InputEvent::SetDisplayLength, m_json["pixelCount"].as<int>()});
|
||||
}
|
||||
|
||||
if (doc.containsKey("startPixel")) {
|
||||
setEvent(InputEvent{InputEvent::SetDisplayOffset, (int)doc["startPixel"]});
|
||||
if (m_json.containsKey("startPixel")) {
|
||||
Log.notice("Start pixel changed");
|
||||
setEvent(InputEvent{InputEvent::SetDisplayOffset, m_json["startPixel"].as<int>()});
|
||||
}
|
||||
|
||||
if (doc.containsKey("save")) {
|
||||
if (m_json.containsKey("save")) {
|
||||
setEvent(InputEvent{InputEvent::SaveConfigurationRequest});
|
||||
}
|
||||
|
||||
if (doc.containsKey("restart")) {
|
||||
#ifdef BOARD_ESP8266
|
||||
ESP.wdtDisable();
|
||||
ESP.restart();
|
||||
#endif
|
||||
if (m_json.containsKey("restart")) {
|
||||
Platform::restart();
|
||||
}
|
||||
|
||||
if (doc.containsKey("reconnect")) {
|
||||
if (m_json.containsKey("reconnect")) {
|
||||
m_mqtt.disconnect();
|
||||
}
|
||||
|
||||
if (doc.containsKey("ping")) {
|
||||
if (m_json.containsKey("ping")) {
|
||||
m_needHeartbeat = true;
|
||||
Log.notice("Queuing up heartbeat");
|
||||
}
|
||||
|
||||
if (doc.containsKey("effect")) {
|
||||
strcpy(m_patternBuf, doc["effect"].as<const char*>());
|
||||
if (m_json.containsKey("effect")) {
|
||||
strcpy(m_patternBuf, m_json["effect"].as<const char*>());
|
||||
setEvent(InputEvent{InputEvent::SetPattern, m_patternBuf});
|
||||
}
|
||||
|
||||
if (doc.containsKey("color")) {
|
||||
uint8_t r = doc["color"]["r"];
|
||||
uint8_t g = doc["color"]["g"];
|
||||
uint8_t b = doc["color"]["b"];
|
||||
if (m_json.containsKey("color")) {
|
||||
uint8_t r = m_json["color"]["r"];
|
||||
uint8_t g = m_json["color"]["g"];
|
||||
uint8_t b = m_json["color"]["b"];
|
||||
setEvent(InputEvent{InputEvent::SetColor, CRGB(r, g, b)});
|
||||
}
|
||||
|
||||
if (doc.containsKey("brightness")) {
|
||||
setEvent(InputEvent{InputEvent::SetBrightness, (int)doc["brightness"]});
|
||||
if (m_json.containsKey("brightness")) {
|
||||
setEvent(InputEvent{InputEvent::SetBrightness, m_json["brightness"].as<int>()});
|
||||
}
|
||||
|
||||
Log.notice("Event done.");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
MQTTTelemetry::s_callback(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
char topicBuf[128];
|
||||
char payloadBuf[512];
|
||||
strcpy(topicBuf, topic);
|
||||
memcpy(payloadBuf, payload, length);
|
||||
payloadBuf[std::min(sizeof(payloadBuf) - 1, length)] = 0;
|
||||
Static<MQTTTelemetry>::instance()->callback(topicBuf, payloadBuf);
|
||||
strcpy(s_topicBuf, topic);
|
||||
memcpy(s_payloadBuf, payload, length);
|
||||
s_payloadBuf[std::min(sizeof(s_payloadBuf) - 1, length)] = 0;
|
||||
Static<MQTTTelemetry>::instance()->callback(s_topicBuf, s_payloadBuf);
|
||||
}
|
||||
|
||||
char MQTTTelemetry::s_topicBuf[128] = {0};
|
||||
char MQTTTelemetry::s_payloadBuf[512] = {0};
|
||||
|
||||
void
|
||||
MQTTTelemetry::publishDoc(const char* topic, bool retain)
|
||||
{
|
||||
m_mqtt.beginPublish(topic, measureJson(m_json), retain);
|
||||
serializeJson(m_json, m_mqtt);
|
||||
m_mqtt.endPublish();
|
||||
}
|
||||
|
||||
void
|
||||
MQTTTelemetry::publishDoc(const char* topic)
|
||||
{
|
||||
publishDoc(topic, false);
|
||||
}
|
||||
|
||||
|
||||
STATIC_ALLOC(MQTTTelemetry);
|
||||
STATIC_TASK(MQTTTelemetry);
|
||||
|
Reference in New Issue
Block a user