From f29e139cbb7e4a4d539cba6e894ef4a6acd312d6 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 May 2023 09:22:00 +0300 Subject: WIP: big refactoring --- .../homekit/mqtt/module/relay.cpp | 27 +++++++++++++++++----- .../mqtt_module_relay/homekit/mqtt/module/relay.h | 10 ++++++-- .../common/libs/mqtt_module_relay/library.json | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) (limited to 'platformio/common/libs/mqtt_module_relay') diff --git a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp b/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp index ab40727..90c57f9 100644 --- a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp +++ b/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp @@ -5,19 +5,28 @@ namespace homekit::mqtt { static const char TOPIC_RELAY_SWITCH[] = "relay/switch"; +static const char TOPIC_RELAY_STATUS[] = "relay/status"; -void MqttRelayModule::init(Mqtt &mqtt) { - String topic(TOPIC_RELAY_SWITCH); - mqtt.subscribeModule(topic, this, 1); +void MqttRelayModule::onConnect(Mqtt &mqtt) { + String topic(TOPIC_RELAY_SWITCH); + mqtt.subscribeModule(topic, this, 1); +} + +void MqttRelayModule::onDisconnect(Mqtt &mqtt, espMqttClientTypes::DisconnectReason reason) { +#ifdef CONFIG_RELAY_OFF_ON_DISCONNECT + if (relay::state()) { + relay::off(); + } +#endif } void MqttRelayModule::tick(homekit::mqtt::Mqtt& mqtt) {} void MqttRelayModule::handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId, const uint8_t *payload, size_t length, size_t index, size_t total) { - if (topic != TOPIC_RELAY_SWITCH) - return; + if (topic != TOPIC_RELAY_SWITCH) + return; - if (length != sizeof(MqttRelaySwitchPayload)) { + if (length != sizeof(MqttRelaySwitchPayload)) { PRINTF("error: size of payload (%ul) does not match expected (%ul)\n", length, sizeof(MqttRelaySwitchPayload)); return; @@ -29,6 +38,8 @@ void MqttRelayModule::handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId return; } + MqttRelayStatusPayload resp{}; + if (pd->state == 1) { PRINTLN("mqtt: turning relay on"); relay::on(); @@ -38,6 +49,10 @@ void MqttRelayModule::handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId } else { PRINTLN("error: unexpected state value"); } + + resp.opened = relay::state(); + mqtt.publish(TOPIC_RELAY_STATUS, reinterpret_cast(&resp), sizeof(resp)); } } + diff --git a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h b/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h index 6420de1..e245527 100644 --- a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h +++ b/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h @@ -10,14 +10,20 @@ struct MqttRelaySwitchPayload { uint8_t state; } __attribute__((packed)); +struct MqttRelayStatusPayload { + uint8_t opened; +} __attribute__((packed)); + class MqttRelayModule : public MqttModule { public: MqttRelayModule() : MqttModule(0) {} - void init(Mqtt& mqtt) override; + void onConnect(Mqtt& mqtt) override; + void onDisconnect(Mqtt& mqtt, espMqttClientTypes::DisconnectReason reason) override; void tick(Mqtt& mqtt) override; - void handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId, const uint8_t *payload, size_t length, size_t index, size_t total) override; + void handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId, const uint8_t *payload, size_t length, size_t index, size_t total) override; }; } #endif //HOMEKIT_LIB_MQTT_MODULE_RELAY_H + diff --git a/platformio/common/libs/mqtt_module_relay/library.json b/platformio/common/libs/mqtt_module_relay/library.json index e71cf95..6cbbfb0 100644 --- a/platformio/common/libs/mqtt_module_relay/library.json +++ b/platformio/common/libs/mqtt_module_relay/library.json @@ -1,6 +1,6 @@ { "name": "homekit_mqtt_module_relay", - "version": "1.0.3", + "version": "1.0.5", "build": { "flags": "-I../../include" }, -- cgit v1.2.3 From a6d8ba93056c1a4e243d56da447e241b2504fae2 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 23:20:37 +0300 Subject: move files again --- .../homekit/mqtt/module/relay.cpp | 58 ---------------------- .../mqtt_module_relay/homekit/mqtt/module/relay.h | 29 ----------- .../common/libs/mqtt_module_relay/library.json | 11 ---- 3 files changed, 98 deletions(-) delete mode 100644 platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp delete mode 100644 platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h delete mode 100644 platformio/common/libs/mqtt_module_relay/library.json (limited to 'platformio/common/libs/mqtt_module_relay') diff --git a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp b/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp deleted file mode 100644 index 90c57f9..0000000 --- a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "./relay.h" -#include -#include - -namespace homekit::mqtt { - -static const char TOPIC_RELAY_SWITCH[] = "relay/switch"; -static const char TOPIC_RELAY_STATUS[] = "relay/status"; - -void MqttRelayModule::onConnect(Mqtt &mqtt) { - String topic(TOPIC_RELAY_SWITCH); - mqtt.subscribeModule(topic, this, 1); -} - -void MqttRelayModule::onDisconnect(Mqtt &mqtt, espMqttClientTypes::DisconnectReason reason) { -#ifdef CONFIG_RELAY_OFF_ON_DISCONNECT - if (relay::state()) { - relay::off(); - } -#endif -} - -void MqttRelayModule::tick(homekit::mqtt::Mqtt& mqtt) {} - -void MqttRelayModule::handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId, const uint8_t *payload, size_t length, size_t index, size_t total) { - if (topic != TOPIC_RELAY_SWITCH) - return; - - if (length != sizeof(MqttRelaySwitchPayload)) { - PRINTF("error: size of payload (%ul) does not match expected (%ul)\n", - length, sizeof(MqttRelaySwitchPayload)); - return; - } - - auto pd = reinterpret_cast(payload); - if (strncmp(pd->secret, MQTT_SECRET, sizeof(pd->secret)) != 0) { - PRINTLN("error: invalid secret"); - return; - } - - MqttRelayStatusPayload resp{}; - - if (pd->state == 1) { - PRINTLN("mqtt: turning relay on"); - relay::on(); - } else if (pd->state == 0) { - PRINTLN("mqtt: turning relay off"); - relay::off(); - } else { - PRINTLN("error: unexpected state value"); - } - - resp.opened = relay::state(); - mqtt.publish(TOPIC_RELAY_STATUS, reinterpret_cast(&resp), sizeof(resp)); -} - -} - diff --git a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h b/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h deleted file mode 100644 index e245527..0000000 --- a/platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef HOMEKIT_LIB_MQTT_MODULE_RELAY_H -#define HOMEKIT_LIB_MQTT_MODULE_RELAY_H - -#include - -namespace homekit::mqtt { - -struct MqttRelaySwitchPayload { - char secret[12]; - uint8_t state; -} __attribute__((packed)); - -struct MqttRelayStatusPayload { - uint8_t opened; -} __attribute__((packed)); - -class MqttRelayModule : public MqttModule { -public: - MqttRelayModule() : MqttModule(0) {} - void onConnect(Mqtt& mqtt) override; - void onDisconnect(Mqtt& mqtt, espMqttClientTypes::DisconnectReason reason) override; - void tick(Mqtt& mqtt) override; - void handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId, const uint8_t *payload, size_t length, size_t index, size_t total) override; -}; - -} - -#endif //HOMEKIT_LIB_MQTT_MODULE_RELAY_H - diff --git a/platformio/common/libs/mqtt_module_relay/library.json b/platformio/common/libs/mqtt_module_relay/library.json deleted file mode 100644 index 6cbbfb0..0000000 --- a/platformio/common/libs/mqtt_module_relay/library.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "homekit_mqtt_module_relay", - "version": "1.0.5", - "build": { - "flags": "-I../../include" - }, - "dependencies": { - "homekit_mqtt": "file://../common/libs/mqtt", - "homekit_relay": "file://../common/libs/relay" - } -} -- cgit v1.2.3