From 2771b91571415bc9a5e851463d574d46e0e1c8cf Mon Sep 17 00:00:00 2001 From: cjkas Date: Tue, 13 Oct 2026 20:35:44 +0200 Subject: [PATCH] refactor: move socket events into SomfyEvents.cpp The emitState and emitCommand events for rooms, shades, groups and the controller move out of Somfy.cpp unchanged. Also drop the log tag from SomfyMqtt.cpp, which never logs. Co-Authored-By: Claude Opus 5.5 --- src/Somfy.cpp | 90 ----------------------------------------- src/SomfyEvents.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++ src/SomfyMqtt.cpp | 3 -- 3 files changed, 99 insertions(+), 93 deletions(-) create mode 100644 src/SomfyEvents.cpp diff --git a/src/Somfy.cpp b/src/Somfy.cpp index 8fd01d3..7a75559 100644 --- a/src/Somfy.cpp +++ b/src/Somfy.cpp @@ -817,88 +817,6 @@ float SomfyShade::p_myTiltPos(float pos) { return old; } -void SomfyShade::emitState(const char *evt) { this->emitState(255, evt); } -void SomfyShade::emitState(uint8_t num, const char *evt) { - JsonSockEvent *json = sockEmit.beginEmit(evt); - json->beginObject(); - json->addElem("shadeId", this->shadeId); - json->addElem("type", static_cast(this->shadeType)); - json->addElem("remoteAddress", (uint32_t)this->getRemoteAddress()); - json->addElem("name", this->name); - json->addElem("direction", this->direction); - json->addElem("position", this->transformPosition(this->currentPos)); - json->addElem("target", this->transformPosition(this->target)); - json->addElem("myPos", this->transformPosition(this->myPos)); - json->addElem("tiltType", static_cast(this->tiltType)); - json->addElem("flipCommands", this->flipCommands); - json->addElem("flipPosition", this->flipPosition); - json->addElem("flags", this->flags); - json->addElem("sunSensor", this->hasSunSensor()); - json->addElem("light", this->hasLight()); - json->addElem("sortOrder", this->sortOrder); - if(this->tiltType != tilt_types::none) { - json->addElem("tiltDirection", this->tiltDirection); - json->addElem("tiltTarget", this->transformPosition(this->tiltTarget)); - json->addElem("tiltPosition", this->transformPosition(this->currentTiltPos)); - json->addElem("myTiltPos", this->transformPosition(this->myTiltPos)); - } - json->endObject(); - sockEmit.endEmit(num); -} -void SomfyShade::emitCommand(somfy_commands cmd, const char *source, uint32_t sourceAddress, const char *evt) { this->emitCommand(255, cmd, source, sourceAddress, evt); } -void SomfyShade::emitCommand(uint8_t num, somfy_commands cmd, const char *source, uint32_t sourceAddress, const char *evt) { - JsonSockEvent *json = sockEmit.beginEmit(evt); - json->beginObject(); - json->addElem("shadeId", this->shadeId); - json->addElem("remoteAddress", (uint32_t)this->getRemoteAddress()); - json->addElem("cmd", translateSomfyCommand(cmd).c_str()); - json->addElem("source", source); - json->addElem("rcode", (uint32_t)this->lastRollingCode); - json->addElem("sourceAddress", (uint32_t)sourceAddress); - json->endObject(); - sockEmit.endEmit(num); - if(mqtt.connected()) { - this->publish("cmdSource", source); - this->publish("cmdAddress", sourceAddress); - this->publish("cmd", translateSomfyCommand(cmd).c_str()); - } -} -void SomfyRoom::emitState(const char *evt) { this->emitState(255, evt); } -void SomfyRoom::emitState(uint8_t num, const char *evt) { - JsonSockEvent *json = sockEmit.beginEmit(evt); - json->beginObject(); - json->addElem("roomId", this->roomId); - json->addElem("name", this->name); - json->addElem("sortOrder", this->sortOrder); - json->endObject(); - sockEmit.endEmit(num); - this->publish(); -} -void SomfyGroup::emitState(const char *evt) { this->emitState(255, evt); } -void SomfyGroup::emitState(uint8_t num, const char *evt) { - uint8_t flags = 0; - JsonSockEvent *json = sockEmit.beginEmit(evt); - json->beginObject(); - json->addElem("groupId", this->groupId); - json->addElem("remoteAddress", (uint32_t)this->getRemoteAddress()); - json->addElem("name", this->name); - json->addElem("sunSensor", this->hasSunSensor()); - json->beginArray("shades"); - for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) { - if(this->linkedShades[i] != 255 && this->linkedShades[i] != 0) { - SomfyShade *shade = somfy.getShadeById(this->linkedShades[i]); - if(shade) { - json->addElem(this->linkedShades[i]); - flags |= shade->flags; - } - } - } - json->endArray(); - json->addElem("flags", flags); - json->endObject(); - sockEmit.endEmit(num); - this->publish(); -} int8_t SomfyShade::transformPosition(float fpos) { return displayPosition(fpos, this->flipPosition); } bool SomfyShade::isIdle() { return this->isAtTarget() && this->direction == 0 && this->tiltDirection == 0; @@ -2261,14 +2179,6 @@ void SomfyShadeController::processWaitingFrame() { for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) if(this->shades[i].getShadeId() != 255) this->shades[i].processWaitingFrame(); } -void SomfyShadeController::emitState(uint8_t num) { - for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) { - SomfyShade *shade = &this->shades[i]; - if(shade->getShadeId() == 255) continue; - esp_task_wdt_reset(); - shade->emitState(num); - } -} uint8_t SomfyShadeController::getNextShadeId() { // There is no shortcut for this since the deletion of // a shade in the middle makes all of this very difficult. diff --git a/src/SomfyEvents.cpp b/src/SomfyEvents.cpp new file mode 100644 index 0000000..75b6cdd --- /dev/null +++ b/src/SomfyEvents.cpp @@ -0,0 +1,99 @@ +#include +#include "Somfy.h" +#include "Sockets.h" +#include "MQTT.h" + +extern SomfyShadeController somfy; +extern SocketEmitter sockEmit; +extern MQTTClass mqtt; + +void SomfyShade::emitState(const char *evt) { this->emitState(255, evt); } +void SomfyShade::emitState(uint8_t num, const char *evt) { + JsonSockEvent *json = sockEmit.beginEmit(evt); + json->beginObject(); + json->addElem("shadeId", this->shadeId); + json->addElem("type", static_cast(this->shadeType)); + json->addElem("remoteAddress", (uint32_t)this->getRemoteAddress()); + json->addElem("name", this->name); + json->addElem("direction", this->direction); + json->addElem("position", this->transformPosition(this->currentPos)); + json->addElem("target", this->transformPosition(this->target)); + json->addElem("myPos", this->transformPosition(this->myPos)); + json->addElem("tiltType", static_cast(this->tiltType)); + json->addElem("flipCommands", this->flipCommands); + json->addElem("flipPosition", this->flipPosition); + json->addElem("flags", this->flags); + json->addElem("sunSensor", this->hasSunSensor()); + json->addElem("light", this->hasLight()); + json->addElem("sortOrder", this->sortOrder); + if(this->tiltType != tilt_types::none) { + json->addElem("tiltDirection", this->tiltDirection); + json->addElem("tiltTarget", this->transformPosition(this->tiltTarget)); + json->addElem("tiltPosition", this->transformPosition(this->currentTiltPos)); + json->addElem("myTiltPos", this->transformPosition(this->myTiltPos)); + } + json->endObject(); + sockEmit.endEmit(num); +} +void SomfyShade::emitCommand(somfy_commands cmd, const char *source, uint32_t sourceAddress, const char *evt) { this->emitCommand(255, cmd, source, sourceAddress, evt); } +void SomfyShade::emitCommand(uint8_t num, somfy_commands cmd, const char *source, uint32_t sourceAddress, const char *evt) { + JsonSockEvent *json = sockEmit.beginEmit(evt); + json->beginObject(); + json->addElem("shadeId", this->shadeId); + json->addElem("remoteAddress", (uint32_t)this->getRemoteAddress()); + json->addElem("cmd", translateSomfyCommand(cmd).c_str()); + json->addElem("source", source); + json->addElem("rcode", (uint32_t)this->lastRollingCode); + json->addElem("sourceAddress", (uint32_t)sourceAddress); + json->endObject(); + sockEmit.endEmit(num); + if(mqtt.connected()) { + this->publish("cmdSource", source); + this->publish("cmdAddress", sourceAddress); + this->publish("cmd", translateSomfyCommand(cmd).c_str()); + } +} +void SomfyRoom::emitState(const char *evt) { this->emitState(255, evt); } +void SomfyRoom::emitState(uint8_t num, const char *evt) { + JsonSockEvent *json = sockEmit.beginEmit(evt); + json->beginObject(); + json->addElem("roomId", this->roomId); + json->addElem("name", this->name); + json->addElem("sortOrder", this->sortOrder); + json->endObject(); + sockEmit.endEmit(num); + this->publish(); +} +void SomfyGroup::emitState(const char *evt) { this->emitState(255, evt); } +void SomfyGroup::emitState(uint8_t num, const char *evt) { + uint8_t flags = 0; + JsonSockEvent *json = sockEmit.beginEmit(evt); + json->beginObject(); + json->addElem("groupId", this->groupId); + json->addElem("remoteAddress", (uint32_t)this->getRemoteAddress()); + json->addElem("name", this->name); + json->addElem("sunSensor", this->hasSunSensor()); + json->beginArray("shades"); + for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) { + if(this->linkedShades[i] != 255 && this->linkedShades[i] != 0) { + SomfyShade *shade = somfy.getShadeById(this->linkedShades[i]); + if(shade) { + json->addElem(this->linkedShades[i]); + flags |= shade->flags; + } + } + } + json->endArray(); + json->addElem("flags", flags); + json->endObject(); + sockEmit.endEmit(num); + this->publish(); +} +void SomfyShadeController::emitState(uint8_t num) { + for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) { + SomfyShade *shade = &this->shades[i]; + if(shade->getShadeId() == 255) continue; + esp_task_wdt_reset(); + shade->emitState(num); + } +} diff --git a/src/SomfyMqtt.cpp b/src/SomfyMqtt.cpp index 237c582..04a3200 100644 --- a/src/SomfyMqtt.cpp +++ b/src/SomfyMqtt.cpp @@ -1,12 +1,9 @@ -#include "esp_log.h" #include "Utils.h" #include "ConfigSettings.h" #include "Somfy.h" #include "Sockets.h" #include "MQTT.h" -static const char *TAG = "Somfy"; - extern SomfyShadeController somfy; extern SocketEmitter sockEmit; extern ConfigSettings settings;