You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.7 KiB
Lua
68 lines
1.7 KiB
Lua
-- Line Output Router
|
|
local id = "name"
|
|
local grab = "green"
|
|
local pass = "black"
|
|
|
|
ctrl = {
|
|
-- Initialization
|
|
program = function(event)
|
|
mem.allocs = mem.allocs or {}
|
|
|
|
print(id .. " programmed" )
|
|
interrupt(1)
|
|
end,
|
|
|
|
-- Tube Processing
|
|
item = function(event)
|
|
for itemstring,count in pairs(mem.allocs) do
|
|
if event.itemstring == itemstring and count > 0 then
|
|
mem.allocs[itemstring] = mem.allocs[itemstring] - 1
|
|
return grab
|
|
end
|
|
end
|
|
|
|
return pass
|
|
end,
|
|
|
|
-- digiline processing
|
|
action_allocate = function(msg)
|
|
for itemstring,count in pairs(msg.allocs) do
|
|
mem.allocs[ itemstring ] = ( mem.allocs[ itemstring ] or 0 ) + count
|
|
end
|
|
end,
|
|
|
|
-- Line Output Handling
|
|
lo_action_error = function(msg)
|
|
end,
|
|
lo_action_query = function(msg)
|
|
digiline_send( msg.replyto, { action = "lo_query_result", name = id } )
|
|
end,
|
|
|
|
-- Internal Handling
|
|
interrupt = function(event) interrupt(1) end,
|
|
|
|
digiline = function(event)
|
|
local msg = event.msg
|
|
|
|
if event.channel == "line_outputs" then
|
|
local cmd = "lo_action_" .. ( msg.action or "error" )
|
|
if ctrl[cmd] == nil then cmd = "lo_action_error" end
|
|
ctrl[cmd](msg)
|
|
end
|
|
if event.channel ~= id then return end
|
|
|
|
-- Convert simple commands to table format
|
|
if type(msg) ~= "table" then msg = { action = msg } end
|
|
|
|
local cmd = "action_" .. (msg.action or "error")
|
|
if ctrl[cmd] == nil then cmd = "action_error" end
|
|
|
|
--print( id .. ": processing " .. msg.action )
|
|
return ctrl[cmd](msg)
|
|
end,
|
|
action_error = function(msg) end
|
|
}
|
|
|
|
if ctrl[event.type] ~= nil then return ctrl[event.type](event) or pass end
|
|
print( id .. ": unhandled event: " .. event.type )
|
|
return pass |