Publish a state topic for each shade
This commit is contained in:
29
src/main.cpp
29
src/main.cpp
@@ -111,6 +111,8 @@ void processPacket(const Packet *packet) {
|
|||||||
uint8_t position = (uint8_t)std::round(((float)value / 0xFFFF) * 100);
|
uint8_t position = (uint8_t)std::round(((float)value / 0xFFFF) * 100);
|
||||||
if (shades[i].lastPosition == position) {
|
if (shades[i].lastPosition == position) {
|
||||||
shades[i].lastPositionCount++;
|
shades[i].lastPositionCount++;
|
||||||
|
} else {
|
||||||
|
shades[i].lastPositionCount = 0;
|
||||||
}
|
}
|
||||||
shades[i].lastPosition = position;
|
shades[i].lastPosition = position;
|
||||||
String payload = String(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].lastTargetPosition != shades[i].lastPosition || shades[i].lastPosition == -1) {
|
||||||
if (shades[i].positionFetchCount >= MAX_FETCH_COUNT) {
|
if (shades[i].positionFetchCount >= MAX_FETCH_COUNT) {
|
||||||
// Give up waiting for the blind to reach the target position
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shades[i].lastPositionCount >= 3) {
|
if (shades[i].lastPositionCount >= 2) {
|
||||||
// Blind hasn't moved after 3 fetches, so give up waiting
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,6 +240,11 @@ bool updatePosition(uint16_t shadeID) {
|
|||||||
sendFetchPosition(shadeID);
|
sendFetchPosition(shadeID);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,6 +267,8 @@ void processSetMessage(const String &topic, const String &payload) {
|
|||||||
shades[i].positionFetchCount = 0;
|
shades[i].positionFetchCount = 0;
|
||||||
shades[i].lastPositionCount = 0;
|
shades[i].lastPositionCount = 0;
|
||||||
timer.every(2000, updatePosition, shades[i].ID);
|
timer.every(2000, updatePosition, shades[i].ID);
|
||||||
|
|
||||||
|
client.publish("hotdog/" + shades[i].name + "/state", "opening", true);
|
||||||
} else if (payload == "CLOSE") {
|
} else if (payload == "CLOSE") {
|
||||||
sendClosePacket(shades[i].ID);
|
sendClosePacket(shades[i].ID);
|
||||||
|
|
||||||
@@ -258,6 +277,8 @@ void processSetMessage(const String &topic, const String &payload) {
|
|||||||
shades[i].positionFetchCount = 0;
|
shades[i].positionFetchCount = 0;
|
||||||
shades[i].lastPositionCount = 0;
|
shades[i].lastPositionCount = 0;
|
||||||
timer.every(2000, updatePosition, shades[i].ID);
|
timer.every(2000, updatePosition, shades[i].ID);
|
||||||
|
|
||||||
|
client.publish("hotdog/" + shades[i].name + "/state", "closing", true);
|
||||||
} else if (payload == "STOP") {
|
} else if (payload == "STOP") {
|
||||||
sendStopPacket(shades[i].ID);
|
sendStopPacket(shades[i].ID);
|
||||||
|
|
||||||
@@ -281,6 +302,12 @@ void processSetPositionMessage(const String& topic, const String &payload) {
|
|||||||
float percentage = payload.toInt() / 100.0f;
|
float percentage = payload.toInt() / 100.0f;
|
||||||
sendSetPosition(shades[i].ID, percentage);
|
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].lastPosition = -1;
|
||||||
shades[i].lastTargetPosition = payload.toInt();
|
shades[i].lastTargetPosition = payload.toInt();
|
||||||
shades[i].positionFetchCount = 0;
|
shades[i].positionFetchCount = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user