OpenComputerScripts/drone_bios.lua

51 lines
1.5 KiB
Lua
Raw Normal View History

2018-11-17 16:12:06 +08:00
drone=component.proxy(component.list("drone")())
modem=component.proxy(component.list("modem")())
2018-11-24 00:45:58 +08:00
drone_version="Drone v3.1"
drone.setStatusText(drone_version .. '\n' .. modem.address)
2018-11-17 16:12:06 +08:00
modem.open(98)
handlers={}
handlers["modem_message"]={}
table.insert(handlers["modem_message"],function(event)
local sender=event[3]
local tag=event[6]
local cmd=event[7]
if(tag~=nil and cmd~=nil and tag=='execute_command') then
local ok,err=pcall(function()
local f=load(cmd)
local cmdok,cmdresult=pcall(f)
if(not cmdok) then
modem.send(sender,99,"Failed to execute: " .. cmdresult)
else
modem.send(sender,99,cmdresult)
end
end)
if(not ok) then
modem.send(sender,99,"Command Execute Failed: " .. err)
end
end
end)
handle_event=function(raw_event)
if(handlers[raw_event[1]]~=nil) then
for idx,callback in ipairs(handlers[raw_event[1]]) do
2018-11-24 00:45:58 +08:00
local ok,result=pcall(function()
2018-11-17 16:12:06 +08:00
callback(raw_event)
end)
2018-11-24 00:45:58 +08:00
if(ok and result) then break end
2018-11-17 16:12:06 +08:00
end
end
end
sleep=function(sec)
2018-11-24 00:45:58 +08:00
local current=computer.uptime()
local deadline=current + sec
2018-11-17 16:12:06 +08:00
while true do
2018-11-24 00:45:58 +08:00
local raw_event=table.pack(computer.pullSignal(deadline-current))
2018-11-17 16:12:06 +08:00
if(raw_event[1]==nil) then break
2018-11-24 00:45:58 +08:00
else handle_event(raw_event) end
current=computer.uptime()
if(current<dealine) then break end
2018-11-17 16:12:06 +08:00
end
end
while true do
handle_event(table.pack(computer.pullSignal()))
end