summaryrefslogtreecommitdiff
path: root/include/pio/libs/mqtt_module_relay/homekit
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-06-10 23:20:37 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-06-10 23:20:37 +0300
commita6d8ba93056c1a4e243d56da447e241b2504fae2 (patch)
tree3ee5372ef4e4a2b8532bf8bc57d7151d77d96f71 /include/pio/libs/mqtt_module_relay/homekit
parentb0bf43e6a272d42a55158e657bd937cb82fc3d8d (diff)
move files again
Diffstat (limited to 'include/pio/libs/mqtt_module_relay/homekit')
-rw-r--r--include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp58
-rw-r--r--include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.h29
2 files changed, 87 insertions, 0 deletions
diff --git a/include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp b/include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp
new file mode 100644
index 0000000..90c57f9
--- /dev/null
+++ b/include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.cpp
@@ -0,0 +1,58 @@
+#include "./relay.h"
+#include <homekit/relay.h>
+#include <homekit/logging.h>
+
+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<const struct MqttRelaySwitchPayload*>(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<uint8_t*>(&resp), sizeof(resp));
+}
+
+}
+
diff --git a/include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.h b/include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.h
new file mode 100644
index 0000000..e245527
--- /dev/null
+++ b/include/pio/libs/mqtt_module_relay/homekit/mqtt/module/relay.h
@@ -0,0 +1,29 @@
+#ifndef HOMEKIT_LIB_MQTT_MODULE_RELAY_H
+#define HOMEKIT_LIB_MQTT_MODULE_RELAY_H
+
+#include <homekit/mqtt/module.h>
+
+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
+