From 3772db01fa19a5afa35bcb869fcbdab85691659b Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Fri, 1 Oct 2021 23:14:17 +0200 Subject: [PATCH] Add custom fan animation example --- src/hasp/hasp_dispatch.cpp | 10 +++++++++- src/mqtt/hasp_mqtt.h | 4 ++++ src/mqtt/hasp_mqtt_paho_single.cpp | 8 ++++++++ src/mqtt/hasp_mqtt_pubsubclient.cpp | 9 ++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 05673ae2..28792d52 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -340,6 +340,14 @@ void dispatch_topic_payload(const char* topic, const char* payload, bool update, } #endif +#if HASP_USE_CUSTOM > 0 + if(topic == strstr_P(topic, PSTR(MQTT_TOPIC_CUSTOM "/"))) { // startsWith custom + topic += 7u; + custom_topic_payload(topic, (char*)payload, source); + return; + } +#endif + dispatch_command(topic, (char*)payload, update, source); // dispatch as is } @@ -615,7 +623,7 @@ void dispatch_parse_jsonl(std::istream& stream) { uint8_t savedPage = haspPages.get(); uint16_t line = 1; - DynamicJsonDocument jsonl(MQTT_MAX_PACKET_SIZE / 2 + 128); + DynamicJsonDocument jsonl(MQTT_MAX_PACKET_SIZE / 2 + 128); DeserializationError jsonError = deserializeJson(jsonl, stream); #ifdef ARDUINO diff --git a/src/mqtt/hasp_mqtt.h b/src/mqtt/hasp_mqtt.h index c3cac0d5..a8af21d0 100644 --- a/src/mqtt/hasp_mqtt.h +++ b/src/mqtt/hasp_mqtt.h @@ -72,6 +72,10 @@ bool mqttSetConfig(const JsonObject& settings); #define MQTT_TOPIC_SENSORS "sensors" #endif +#ifndef MQTT_TOPIC_CUSTOM +#define MQTT_TOPIC_CUSTOM "custom" +#endif + #define MQTT_TOPIC_LWT "LWT" //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/mqtt/hasp_mqtt_paho_single.cpp b/src/mqtt/hasp_mqtt_paho_single.cpp index 2d1eb581..6f55dc7f 100644 --- a/src/mqtt/hasp_mqtt_paho_single.cpp +++ b/src/mqtt/hasp_mqtt_paho_single.cpp @@ -264,6 +264,14 @@ static void onConnect(void* context) topic = mqttNodeTopic + "config/#"; mqtt_subscribe(mqtt_client, topic.c_str()); +#if HASP_USE_CUSTOM > 0 + topic = mqttGroupTopic + MQTT_TOPIC_CUSTOM "/#"; + mqtt_subscribe(mqtt_client, topic.c_str()); + + topic = mqttNodeTopic + MQTT_TOPIC_CUSTOM "/#"; + mqtt_subscribe(mqtt_client, topic.c_str()); +#endif + #ifdef HASP_USE_BROADCAST topic = MQTT_PREFIX "/" MQTT_TOPIC_BROADCAST "/" MQTT_TOPIC_COMMAND "/#"; mqtt_subscribe(mqtt_client, topic.c_str()); diff --git a/src/mqtt/hasp_mqtt_pubsubclient.cpp b/src/mqtt/hasp_mqtt_pubsubclient.cpp index 82ffbede..a2f964fd 100644 --- a/src/mqtt/hasp_mqtt_pubsubclient.cpp +++ b/src/mqtt/hasp_mqtt_pubsubclient.cpp @@ -119,7 +119,7 @@ int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload) int mqtt_send_state(const char* subtopic, const char* payload) { - char tmp_topic[strlen(mqttNodeTopic) + 20]; + char tmp_topic[strlen(mqttNodeTopic) + strlen(subtopic) + 16]; snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR("%s" MQTT_TOPIC_STATE "/%s"), mqttNodeTopic, subtopic); return mqttPublish(tmp_topic, payload, false); } @@ -300,6 +300,13 @@ void mqttStart() snprintf_P(topic, sizeof(topic), PSTR("%s" MQTT_TOPIC_CONFIG "/#"), mqttNodeTopic); mqttSubscribeTo(topic); +#if HASP_USE_CUSTOM > 0 + snprintf_P(topic, sizeof(topic), PSTR("%s" MQTT_TOPIC_CUSTOM "/#"), mqttGroupTopic); + mqttSubscribeTo(topic); + snprintf_P(topic, sizeof(topic), PSTR("%s" MQTT_TOPIC_CUSTOM "/#"), mqttNodeTopic); + mqttSubscribeTo(topic); +#endif + #ifdef HASP_USE_BROADCAST snprintf_P(topic, sizeof(topic), PSTR(MQTT_PREFIX "/" MQTT_TOPIC_BROADCAST "/" MQTT_TOPIC_COMMAND "/#")); mqttSubscribeTo(topic);