Enhance command polling: implement deduplication of processed commands and acknowledgment of command IDs
This commit is contained in:
@@ -196,17 +196,27 @@ local function stateForwarder()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Task 3: Poll web server for commands
|
-- Task 3: Poll web server for commands
|
||||||
|
local lastProcessedId = 0 -- track highest processed command ID for dedup
|
||||||
|
|
||||||
local function commandPoller()
|
local function commandPoller()
|
||||||
while running do
|
while running do
|
||||||
local ok, err = pcall(function()
|
local ok, err = pcall(function()
|
||||||
local result = httpGet("/api/bridge/commands")
|
local result = httpGet("/api/bridge/commands")
|
||||||
if result and result.commands and #result.commands > 0 then
|
if result and result.commands and #result.commands > 0 then
|
||||||
-- Process each command
|
local maxId = lastProcessedId
|
||||||
|
-- Process each command, skipping already-processed ones
|
||||||
for _, cmd in ipairs(result.commands) do
|
for _, cmd in ipairs(result.commands) do
|
||||||
pcall(processCommand, cmd)
|
local cmdId = cmd.id or 0
|
||||||
|
if cmdId > lastProcessedId then
|
||||||
|
pcall(processCommand, cmd)
|
||||||
|
if cmdId > maxId then maxId = cmdId end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Acknowledge up to the highest processed ID
|
||||||
|
if maxId > lastProcessedId then
|
||||||
|
lastProcessedId = maxId
|
||||||
|
httpPost("/api/bridge/commands/ack", { lastProcessedId = lastProcessedId })
|
||||||
end
|
end
|
||||||
-- Acknowledge
|
|
||||||
httpPost("/api/bridge/commands/ack", {})
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
sleep(POLL_INTERVAL)
|
sleep(POLL_INTERVAL)
|
||||||
|
|||||||
Reference in New Issue
Block a user