fix(light-server): try to separate main loop and secondary loop

This commit is contained in:
Guillaume ARM 2024-05-28 01:38:59 +02:00
parent 9950ff6883
commit 2c224f29c6

View File

@ -11,7 +11,7 @@ local MAIN_OUTPUT_SIDE = 'left'
local SECONDARY_INPUT_SIDE = 'back' local SECONDARY_INPUT_SIDE = 'back'
local SECONDARY_OUTPUT_SIDE = 'front' local SECONDARY_OUTPUT_SIDE = 'front'
local VERSION = '1.0.1' local VERSION = '1.0.2'
local function getMainColorsOrder() local function getMainColorsOrder()
return { return {
@ -67,50 +67,61 @@ local function switchOffLight(bundledOutput, colorsOrder, sleepTime)
end end
local mainLoopQueue = {} local mainLoopQueue = {}
local secondaryLoopQueue = {}
local function applyMainCommand(bundledOutput)
local nextState = table.remove(mainLoopQueue, 1)
if nextState == nil then
return
end
local function applyMainCommand(bundledOutput, lightIsOn)
local mainColorsOrder = getMainColorsOrder() local mainColorsOrder = getMainColorsOrder()
if lightIsOn then if nextState then
switchOnLight(bundledOutput, mainColorsOrder, MAIN_DELAY_ON) switchOnLight(bundledOutput, mainColorsOrder, MAIN_DELAY_ON)
else else
switchOffLight(bundledOutput, mainColorsOrder, MAIN_DELAY_OFF) switchOffLight(bundledOutput, mainColorsOrder, MAIN_DELAY_OFF)
end end
return applyMainCommand(bundledOutput)
end
local function applySecondaryCommand(bundledOutput)
local nextState = table.remove(secondaryLoopQueue, 1)
if nextState == nil then
return
end end
local function applySecondaryCommand(bundledOutput, lightIsOn)
local secondaryColorsOrder = getSecondaryColorsOrder() local secondaryColorsOrder = getSecondaryColorsOrder()
if lightIsOn then if nextState then
switchOnLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_ON) switchOnLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_ON)
else else
switchOffLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_OFF) switchOffLight(bundledOutput, secondaryColorsOrder, SECONDARY_DELAY_OFF)
end end
return applySecondaryCommand(bundledOutput)
end end
local function mainLoop() local function mainLoop()
local mainOutput = rs.createBundledOutput(MAIN_OUTPUT_SIDE) local mainOutput = rs.createBundledOutput(MAIN_OUTPUT_SIDE)
while true do
os.pullEvent('light_command_main')
table.remove(mainLoopQueue, 1)
applyMainCommand(mainOutput)
end
end
local function secondaryLoop()
local secondaryOutput = rs.createBundledOutput(SECONDARY_OUTPUT_SIDE) local secondaryOutput = rs.createBundledOutput(SECONDARY_OUTPUT_SIDE)
while true do while true do
local _, typeOfRoom, lightIsOn = os.pullEvent('light_command') os.pullEvent('light_command_secondary')
table.remove(mainLoopQueue, 1) table.remove(secondaryLoopQueue, 1)
applySecondaryCommand(secondaryOutput)
if typeOfRoom == 'main' then
applyMainCommand(mainOutput, lightIsOn)
elseif typeOfRoom == 'secondary' then
applySecondaryCommand(secondaryOutput, lightIsOn)
else
error('unknown type of room "' .. tostring(typeOfRoom) .. '"', 0)
end
local event = table.remove(mainLoopQueue, 1)
if event and event.typeOfRoom == 'main' then
applyMainCommand(mainOutput, event.lightIsOn)
elseif event and event.typeOfRoom == 'secondary' then
applySecondaryCommand(secondaryOutput, event.lightIsOn)
end
end end
end end
@ -125,13 +136,13 @@ local function redstoneLoop()
if mainState ~= newMainState then if mainState ~= newMainState then
mainState = newMainState mainState = newMainState
table.insert(mainLoopQueue, { typeOfRoom = 'main', lightIsOn = newMainState }) table.insert(mainLoopQueue, { typeOfRoom = 'main', lightIsOn = newMainState })
os.queueEvent('light_command', 'main', newMainState) os.queueEvent('light_command_main', newMainState)
end end
if secondaryState ~= newSecondaryState then if secondaryState ~= newSecondaryState then
secondaryState = newSecondaryState secondaryState = newSecondaryState
table.insert(mainLoopQueue, { typeOfRoom = 'secondary', lightIsOn = newSecondaryState }) table.insert(mainLoopQueue, { typeOfRoom = 'secondary', lightIsOn = newSecondaryState })
os.queueEvent('light_command', 'secondary', newSecondaryState) os.queueEvent('light_command_secondary', newSecondaryState)
end end
os.pullEvent('redstone') os.pullEvent('redstone')
@ -139,4 +150,4 @@ local function redstoneLoop()
end end
print('> Starting light server v' .. VERSION) print('> Starting light server v' .. VERSION)
parallel.waitForAny(mainLoop, redstoneLoop) parallel.waitForAny(mainLoop, secondaryLoop, redstoneLoop)