refactor: send shade and group changes once per loop() pass

The p_* setters and emitState() no longer publish or emit straight away:
they set SHADE_CHANGED_* / GROUP_CHANGED_* bits on the shade or group.
At the end of every loop() pass SomfyShadeController::sendChanges()
publishes the MQTT topics whose bits are set and sends one shadeState or
groupState, then clears the bits. emitState(evt) with an event name and
emitState(num) for one client still go out at once.

publishShadeTopics() (ShadeChanges.cpp, tested natively) writes the topics
from a shade snapshot; publishState() on connect uses it too. That fixes
MQTT position and target, a constant 50 since c94819e, and direction, a
constant 0 on connect. position and tiltPosition are now retained when
they change, as they were on connect, and a group whose flags change sends
one groupState instead of two. somfy_flags_t moves to ShadeTypes.h.

8 new native tests, 327 in all. Flash: esp32devdbg +196 bytes, esp32dev
+228 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-16 10:40:44 +02:00
parent e70e499d75
commit 4c5bc01fe5
12 changed files with 268 additions and 51 deletions

View file

@ -8,6 +8,7 @@ extern SomfyShadeController somfy;
extern SocketEmitter sockEmit;
extern MQTTClass mqtt;
void SomfyShade::emitState() { this->changed |= SHADE_CHANGED_STATE; }
void SomfyShade::emitState(const char *evt) { this->emitState(255, evt); }
void SomfyShade::emitState(uint8_t num, const char *evt) {
JsonSockEvent *json = sockEmit.beginEmit(evt);
@ -65,6 +66,7 @@ void SomfyRoom::emitState(uint8_t num, const char *evt) {
sockEmit.endEmit(num);
this->publish();
}
void SomfyGroup::emitState() { this->changed |= GROUP_CHANGED_STATE; }
void SomfyGroup::emitState(const char *evt) { this->emitState(255, evt); }
void SomfyGroup::emitState(uint8_t num, const char *evt) {
uint8_t flags = 0;
@ -102,6 +104,26 @@ void emitRequestResult(uint32_t requestId, const edit_result_t &result) {
json->endObject();
sockEmit.endEmit();
}
// The change fan-out. Everything that changed about a shade or group in this
// loop() pass goes out once: its MQTT topics, then one state event.
void SomfyShadeController::sendChanges() {
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
SomfyShade &shade = this->shades[i];
const uint16_t changed = shade.changed;
shade.changed = 0;
if(!changed || shade.getShadeId() == 255) continue;
if(changed & SHADE_CHANGED_TOPICS) shade.publishTopics(changed);
shade.emitState(255, "shadeState");
}
for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) {
SomfyGroup &group = this->groups[i];
const uint8_t changed = group.changed;
group.changed = 0;
if(!changed || group.getGroupId() == 255) continue;
if(changed & GROUP_CHANGED_DIRECTION) group.publish("direction", group.direction);
group.emitState(255, "groupState");
}
}
void SomfyShadeController::emitState(uint8_t num) {
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
SomfyShade *shade = &this->shades[i];

View file

@ -1,6 +1,7 @@
#include "Utils.h"
#include "ConfigSettings.h"
#include "Somfy.h"
#include "SomfySnapshot.h"
#include "Sockets.h"
#include "entryPoints/MQTT.h"
@ -31,28 +32,23 @@ void SomfyRoom::unpublish() {
mqtt.unpublish(topic);
}
}
// Publishes one topic of a shade: shades/<id>/<topic>.
static void publishShadeTopic(const char *topic, int32_t value, bool retain, void *ctx) {
SomfyShade *shade = static_cast<SomfyShade *>(ctx);
if(value < 0) shade->publish(topic, static_cast<int8_t>(value), retain);
else shade->publish(topic, static_cast<uint32_t>(value), retain);
}
void SomfyShade::publishTopics(uint16_t changed) {
if(!mqtt.connected()) return;
shade_snapshot_t s;
snapshotShade(*this, s);
publishShadeTopics(changed, s, publishShadeTopic, this);
}
void SomfyShade::publishState() {
if(mqtt.connected()) {
this->publish("position", (uint8_t)50, true);
this->publish("direction", (int8_t)0, true);
this->publish("target", (uint8_t)50, true);
this->publish("lastRollingCode", this->lastRollingCode);
this->publish("mypos", this->transformPosition(this->myPos), true);
this->publish("myTiltPos", this->transformPosition(this->myTiltPos), true);
if(this->tiltType != tilt_types::none) {
this->publish("tiltDirection", this->tiltDirection, true);
this->publish("tiltPosition", this->transformPosition(this->currentTiltPos), true);
this->publish("tiltTarget", this->transformPosition(this->tiltTarget), true);
}
const uint8_t sunFlag = !!(this->flags & static_cast<uint8_t>(somfy_flags_t::SunFlag));
const uint8_t isSunny = !!(this->flags & static_cast<uint8_t>(somfy_flags_t::Sunny));
const uint8_t isWindy = !!(this->flags & static_cast<uint8_t>(somfy_flags_t::Windy));
if(this->hasSunSensor()) {
this->publish("sunFlag", sunFlag);
this->publish("sunny", isSunny);
}
this->publish("windy", isWindy);
}
if(!mqtt.connected()) return;
shade_snapshot_t s;
snapshotShade(*this, s);
publishShadeTopics(shadeStateTopics(s), s, publishShadeTopic, this);
}
void SomfyShade::publishDisco() {
if(!mqtt.connected() || !settings.MQTT.pubDisco) return;