diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-05-30 01:01:09 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-05-30 01:01:09 +0300 |
commit | d1435e2b1acc31fba64d21994d40297ae900c549 (patch) | |
tree | 18665fe89efc7819ac4ad72f2dda00f5d4cb8ffb /platformio/common/libs/mqtt | |
parent | 0e021d0f1e17f4e2c07f927284cda4bd2754caf0 (diff) |
platformio: make relay a library
Diffstat (limited to 'platformio/common/libs/mqtt')
-rw-r--r-- | platformio/common/libs/mqtt/homekit/mqtt/module.h | 6 | ||||
-rw-r--r-- | platformio/common/libs/mqtt/homekit/mqtt/mqtt.cpp | 15 | ||||
-rw-r--r-- | platformio/common/libs/mqtt/homekit/mqtt/mqtt.h | 6 | ||||
-rw-r--r-- | platformio/common/libs/mqtt/library.json | 2 |
4 files changed, 18 insertions, 11 deletions
diff --git a/platformio/common/libs/mqtt/homekit/mqtt/module.h b/platformio/common/libs/mqtt/homekit/mqtt/module.h index 7bf2522..e4a01f8 100644 --- a/platformio/common/libs/mqtt/homekit/mqtt/module.h +++ b/platformio/common/libs/mqtt/homekit/mqtt/module.h @@ -39,9 +39,13 @@ public: initialized = true; } + inline short getTickInterval() { + return tickInterval; + } + friend class Mqtt; }; } -#endif //HOMEKIT_LIB_MQTT_MODULE_H
\ No newline at end of file +#endif //HOMEKIT_LIB_MQTT_MODULE_H diff --git a/platformio/common/libs/mqtt/homekit/mqtt/mqtt.cpp b/platformio/common/libs/mqtt/homekit/mqtt/mqtt.cpp index 9d96f9f..cb2cea7 100644 --- a/platformio/common/libs/mqtt/homekit/mqtt/mqtt.cpp +++ b/platformio/common/libs/mqtt/homekit/mqtt/mqtt.cpp @@ -25,7 +25,7 @@ using namespace espMqttClientTypes; Mqtt::Mqtt() { auto cfg = config::read(); - homeId = String(cfg.flags.node_configured ? cfg.node_id : wifi::NODE_ID); + nodeId = String(cfg.flags.node_configured ? cfg.node_id : wifi::NODE_ID); randomSeed(micros()); @@ -79,7 +79,7 @@ Mqtt::Mqtt() { PRINTF("mqtt: message received, topic=%s, qos=%d, dup=%d, retain=%d, len=%ul, index=%ul, total=%ul\n", topic, properties.qos, (int)properties.dup, (int)properties.retain, len, index, total); - const char *ptr = topic + homeId.length() + 10; + const char *ptr = topic + nodeId.length() + 10; String relevantTopic(ptr); auto it = moduleSubscriptions.find(relevantTopic); @@ -130,22 +130,24 @@ void Mqtt::disconnect() { void Mqtt::loop() { client.loop(); for (auto& module: modules) { - module->tick(*this); + if (module->getTickInterval() != 0) + module->tick(*this); } } uint16_t Mqtt::publish(const String& topic, uint8_t* payload, size_t length) { - String fullTopic = "hk/" + homeId + "/temphum/" + topic; + String fullTopic = "hk/" + nodeId + "/" + topic; return client.publish(fullTopic.c_str(), 1, false, payload, length); } uint16_t Mqtt::subscribe(const String& topic, uint8_t qos) { - String fullTopic = "hk/" + homeId + "/temphum/" + topic; + String fullTopic = "hk/" + nodeId + "/" + topic; PRINTF("mqtt: subscribing to %s...\n", fullTopic.c_str()); uint16_t packetId = client.subscribe(fullTopic.c_str(), qos); if (!packetId) PRINTF("error: failed to subscribe to %s\n", fullTopic.c_str()); + return packetId; } @@ -157,8 +159,9 @@ void Mqtt::addModule(MqttModule* module) { } } -void Mqtt::subscribeModule(String& topic, MqttModule* module) { +void Mqtt::subscribeModule(String& topic, MqttModule* module, uint8_t qos) { moduleSubscriptions[topic] = module; + subscribe(topic, qos); } } diff --git a/platformio/common/libs/mqtt/homekit/mqtt/mqtt.h b/platformio/common/libs/mqtt/homekit/mqtt/mqtt.h index 983cc5a..9e0c2be 100644 --- a/platformio/common/libs/mqtt/homekit/mqtt/mqtt.h +++ b/platformio/common/libs/mqtt/homekit/mqtt/mqtt.h @@ -22,7 +22,7 @@ class MqttModule; class Mqtt { private: - String homeId; + String nodeId; WiFiClientSecure httpsSecureClient; espMqttClientSecure client; Ticker reconnectTimer; @@ -39,10 +39,10 @@ public: void reconnect(); void loop(); void addModule(MqttModule* module); - void subscribeModule(String& topic, MqttModule* module); + void subscribeModule(String& topic, MqttModule* module, uint8_t qos = 0); uint16_t publish(const String& topic, uint8_t* payload, size_t length); }; } -#endif //HOMEKIT_LIB_MQTT_H
\ No newline at end of file +#endif //HOMEKIT_LIB_MQTT_H diff --git a/platformio/common/libs/mqtt/library.json b/platformio/common/libs/mqtt/library.json index 179b10a..d1ad420 100644 --- a/platformio/common/libs/mqtt/library.json +++ b/platformio/common/libs/mqtt/library.json @@ -1,6 +1,6 @@ { "name": "homekit_mqtt", - "version": "1.0.8", + "version": "1.0.9", "build": { "flags": "-I../../include" } |