From dbed260fd1523c25259e2d920e142ef7e60dc5f0 Mon Sep 17 00:00:00 2001 From: Matt Way Date: Wed, 3 Jan 2024 13:41:01 +1100 Subject: [PATCH] Publish a state topic for each shade --- src/main.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index d1b9758..d44cec0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -111,6 +111,8 @@ void processPacket(const Packet *packet) { uint8_t position = (uint8_t)std::round(((float)value / 0xFFFF) * 100); if (shades[i].lastPosition == position) { shades[i].lastPositionCount++; + } else { + shades[i].lastPositionCount = 0; } shades[i].lastPosition = position; String payload = String(position); @@ -216,11 +218,21 @@ bool updatePosition(uint16_t shadeID) { if (shades[i].lastTargetPosition != shades[i].lastPosition || shades[i].lastPosition == -1) { if (shades[i].positionFetchCount >= MAX_FETCH_COUNT) { // Give up waiting for the blind to reach the target position + if (shades[i].lastPosition > 0) { + client.publish("hotdog/" + shades[i].name + "/state", "open", true); + } else { + client.publish("hotdog/" + shades[i].name + "/state", "closed", true); + } return false; } - if (shades[i].lastPositionCount >= 3) { + if (shades[i].lastPositionCount >= 2) { // Blind hasn't moved after 3 fetches, so give up waiting + if (shades[i].lastPosition > 0) { + client.publish("hotdog/" + shades[i].name + "/state", "open", true); + } else { + client.publish("hotdog/" + shades[i].name + "/state", "closed", true); + } return false; } @@ -228,6 +240,11 @@ bool updatePosition(uint16_t shadeID) { sendFetchPosition(shadeID); return true; } else { + if (shades[i].lastPosition > 0) { + client.publish("hotdog/" + shades[i].name + "/state", "open", true); + } else { + client.publish("hotdog/" + shades[i].name + "/state", "closed", true); + } return false; } } @@ -250,6 +267,8 @@ void processSetMessage(const String &topic, const String &payload) { shades[i].positionFetchCount = 0; shades[i].lastPositionCount = 0; timer.every(2000, updatePosition, shades[i].ID); + + client.publish("hotdog/" + shades[i].name + "/state", "opening", true); } else if (payload == "CLOSE") { sendClosePacket(shades[i].ID); @@ -258,6 +277,8 @@ void processSetMessage(const String &topic, const String &payload) { shades[i].positionFetchCount = 0; shades[i].lastPositionCount = 0; timer.every(2000, updatePosition, shades[i].ID); + + client.publish("hotdog/" + shades[i].name + "/state", "closing", true); } else if (payload == "STOP") { sendStopPacket(shades[i].ID); @@ -281,6 +302,12 @@ void processSetPositionMessage(const String& topic, const String &payload) { float percentage = payload.toInt() / 100.0f; sendSetPosition(shades[i].ID, percentage); + if (payload.toInt() > shades[i].lastPosition) { + client.publish("hotdog/" + shades[i].name + "/state", "opening", true); + } else if (payload.toInt() < shades[i].lastPosition) { + client.publish("hotdog/" + shades[i].name + "/state", "closing", true); + } + shades[i].lastPosition = -1; shades[i].lastTargetPosition = payload.toInt(); shades[i].positionFetchCount = 0;