summaryrefslogtreecommitdiff
path: root/platformio/common/libs/mqtt_module_relay/homekit/mqtt
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-05-31 09:22:00 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-06-10 02:07:23 +0300
commitf29e139cbb7e4a4d539cba6e894ef4a6acd312d6 (patch)
tree6246f126325c5c36fb573134a05f2771cd747966 /platformio/common/libs/mqtt_module_relay/homekit/mqtt
parent3e3753d726f8a02d98368f20f77dd9fa739e3d80 (diff)
WIP: big refactoring
Diffstat (limited to 'platformio/common/libs/mqtt_module_relay/homekit/mqtt')
-rw-r--r--platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp27
-rw-r--r--platformio/common/libs/mqtt_module_relay/homekit/mqtt/module/relay.h10
2 files changed, 29 insertions, 8 deletions
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<uint8_t*>(&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
+