refactor: share shade remote matching in SomfyShade::findRemote

markTxPending copied the shade and linked-remote address matching from
SomfyShade::processFrame.  Both now use findRemote() so they can't drift.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-12 18:01:31 +02:00
parent b4d480caa0
commit 2ee7af51ab
2 changed files with 13 additions and 17 deletions

View file

@ -1767,22 +1767,21 @@ void SomfyShade::processWaitingFrame() {
} }
} }
} }
SomfyRemote *SomfyShade::findRemote(uint32_t address) {
if(this->getRemoteAddress() == address) return this;
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) {
if(this->linkedRemotes[i].getRemoteAddress() == address) return &this->linkedRemotes[i];
}
return nullptr;
}
void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) { void SomfyShade::processFrame(somfy_frame_t &frame, bool internal) {
// The reason why we are processing all frames here is so // The reason why we are processing all frames here is so
// any linked remotes that may happen to be on the same ESPSomfy RTS // any linked remotes that may happen to be on the same ESPSomfy RTS
// device can trigger the appropriate actions. // device can trigger the appropriate actions.
if(this->shadeId == 255) return; if(this->shadeId == 255) return;
bool hasRemote = this->getRemoteAddress() == frame.remoteAddress; SomfyRemote *remote = this->findRemote(frame.remoteAddress);
if(!hasRemote) { if(!remote) return;
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) { if(remote != this && frame.cmd != somfy_commands::Sensor) remote->setRollingCode(frame.rollingCode);
if(this->linkedRemotes[i].getRemoteAddress() == frame.remoteAddress) {
if(frame.cmd != somfy_commands::Sensor) this->linkedRemotes[i].setRollingCode(frame.rollingCode);
hasRemote = true;
break;
}
}
}
if(!hasRemote) return;
const uint64_t curTime = millis(); const uint64_t curTime = millis();
this->lastFrame.copy(frame); this->lastFrame.copy(frame);
int8_t dir = 0; int8_t dir = 0;
@ -3152,14 +3151,9 @@ void SomfyShadeController::processFrame(somfy_frame_t &frame, bool internal) {
} }
} }
void SomfyShadeController::markTxPending(uint32_t remoteAddress, uint32_t seq) { void SomfyShadeController::markTxPending(uint32_t remoteAddress, uint32_t seq) {
// Same matching as SomfyShade::processFrame for shade and linked remotes.
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) { for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
SomfyShade *shade = &this->shades[i]; SomfyShade *shade = &this->shades[i];
if(shade->getShadeId() == 255) continue; if(shade->getShadeId() != 255 && shade->findRemote(remoteAddress)) shade->markTxPending(seq);
bool hasRemote = shade->getRemoteAddress() == remoteAddress;
for(uint8_t j = 0; !hasRemote && j < SOMFY_MAX_LINKED_REMOTES; j++)
hasRemote = shade->linkedRemotes[j].getRemoteAddress() == remoteAddress;
if(hasRemote) shade->markTxPending(seq);
} }
// Group commands move their member shades through processInternalCommand. // Group commands move their member shades through processInternalCommand.
for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) { for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) {

View file

@ -319,6 +319,8 @@ class SomfyShade : public SomfyRemote {
bool isInGroup(); bool isInGroup();
void checkMovement(); void checkMovement();
void processFrame(somfy_frame_t &frame, bool internal = false); void processFrame(somfy_frame_t &frame, bool internal = false);
// The shade itself or the linked remote with this address, else nullptr.
SomfyRemote *findRemote(uint32_t address);
void processInternalCommand(somfy_commands cmd, uint8_t repeat = 1); void processInternalCommand(somfy_commands cmd, uint8_t repeat = 1);
void markTxPending(uint32_t seq); void markTxPending(uint32_t seq);
void processTxEvent(const somfy_tx_event_t &evt); void processTxEvent(const somfy_tx_event_t &evt);