mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-10-31 22:02:17 +01:00
refactor: carry out shade requests from one module behind a LoopQueue
HTTP routes and MQTT commands now both build a shade request and submit it to the LoopQueue, which loop() drains into ShadeRequests. ShadeRequests finds the shade or group, turns client positions into stored ones, checks ranges and fills in default repeats, and reaches the shades through the ShadeOps seam: SomfyShadeOps on the device, a recording fake in the native tests. MqttCommands turns a .../set topic into a shade request. Behaviour that changes: - MQTT tiltTarget, mypos, myTiltPos and tiltPosition, and /setPositions, now honour flipPosition like the other routes. - MQTT mypos keeps the favorite tilt (was set to 0) and myTiltPos keeps the favorite lift. - repeat 0 on /shadeCommand means no repeats, as it did for groups; a JSON repeat over 127 is capped instead of wrapping to the default. - Position corrections go through the setters, so MQTT publishes them. - MQTT commands wait while the radio is full instead of being refused. - The queue is drained every loop() pass and no longer from inside a GitHub firmware download. WebCommand.h is replaced by LoopQueue.h and ShadeRequests.h. 45 new native tests, 271 in all. Flash: esp32devdbg +976 bytes, esp32dev +1,380 bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5fd5feda28
commit
395cb2d1c7
20 changed files with 1354 additions and 569 deletions
66
src/LoopQueue.cpp
Normal file
66
src/LoopQueue.cpp
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
#include "esp_log.h"
|
||||
#include "LoopQueue.h"
|
||||
#include "ConfigFile.h"
|
||||
#include "Somfy.h"
|
||||
#include "Utils.h"
|
||||
|
||||
static const char *TAG = "LoopQueue";
|
||||
|
||||
extern SomfyShadeController somfy;
|
||||
extern ShadeRequests shadeRequests;
|
||||
extern rebootDelay_t rebootDelay;
|
||||
|
||||
static restore_options_t restoreOptions(uint8_t parts) {
|
||||
restore_options_t opts;
|
||||
opts.settings = parts & RESTORE_SETTINGS;
|
||||
opts.shades = parts & RESTORE_SHADES;
|
||||
opts.network = parts & RESTORE_NETWORK;
|
||||
opts.transceiver = parts & RESTORE_TRANSCEIVER;
|
||||
opts.repeaters = parts & RESTORE_REPEATERS;
|
||||
opts.mqtt = parts & RESTORE_MQTT;
|
||||
return opts;
|
||||
}
|
||||
|
||||
void LoopQueue::begin() {
|
||||
if(!this->queue) this->queue = xQueueCreate(LOOP_QUEUE_SIZE, sizeof(loop_item_t));
|
||||
}
|
||||
bool LoopQueue::submit(const loop_item_t &item) {
|
||||
if(!this->queue) return false;
|
||||
// async_tcp also services the sockets, so it must never wait on loop() or
|
||||
// the radio.
|
||||
if(xQueueSend(static_cast<QueueHandle_t>(this->queue), &item, 0) != pdTRUE) {
|
||||
ESP_LOGE(TAG, "Loop queue full, dropping item");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void LoopQueue::drain() {
|
||||
if(!this->queue) return;
|
||||
QueueHandle_t q = static_cast<QueueHandle_t>(this->queue);
|
||||
loop_item_t item;
|
||||
// Items run in order, so only the one at the head can be held back.
|
||||
while(xQueuePeek(q, &item, 0) == pdTRUE) {
|
||||
if(holdLoopItem(item, somfy.transceiver.txSuspended(), somfy.transceiver.txHasRoom())) break;
|
||||
// loop() is the only consumer so this is the item we peeked.
|
||||
xQueueReceive(q, &item, 0);
|
||||
switch(item.type) {
|
||||
case loop_item_type_t::shade_request:
|
||||
if(!shadeRequests.run(item.request))
|
||||
ESP_LOGW(TAG, "Radio didn't take shade request %u", static_cast<uint8_t>(item.request.type));
|
||||
break;
|
||||
case loop_item_type_t::restore_shades: {
|
||||
restore_options_t opts = restoreOptions(item.restoreParts);
|
||||
ShadeConfigFile::restore(&somfy, "/shades.tmp", opts);
|
||||
ESP_LOGI(TAG, "Rebooting ESP for restored settings...");
|
||||
rebootDelay.reboot = true;
|
||||
rebootDelay.rebootTime = millis() + 1000;
|
||||
break;
|
||||
}
|
||||
case loop_item_type_t::load_shades:
|
||||
somfy.loadShadesFile("/shades.tmp");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue