From f43d46454970b85e8b959a3c768e3545acb77f33 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 17 Nov 2018 16:12:51 +0800 Subject: [PATCH] Collect Drone Console v1.4 --- drone_console.lua | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 drone_console.lua diff --git a/drone_console.lua b/drone_console.lua new file mode 100644 index 0000000..935da07 --- /dev/null +++ b/drone_console.lua @@ -0,0 +1,100 @@ +require("libevent") +local text=require('text') +local term=require('term') +local colors=require('colors') +local component=require('component') +local computer=require('computer') + +local modem=component.modem + +term.clear() +print('Drone Debug Console') +print('Initializing...') +modem.open(99) +print('Broadcasting initial commands...') +modem.broadcast(98,'drone=component.proxy(component.list("drone")())') +modem.broadcast(98,'modem=component.proxy(component.list("modem")())') +print('Done') + +term.clear() +print('Drone Debug Console v1.4') +print('Command Prompt (Ctrl+D to exit)') + +local last_response=computer.uptime() + +local listener=AddEventListener("modem_message",function(e) + if(e.data[1]~=nil and e.data[1]=='console_info') then + last_response=computer.uptime() + + local a,b=term.getCursor() + + local w,h=term.gpu().getResolution() + term.gpu().fill(1,h,w,1,' ') + local text="offset: " .. e.data[2] .. " energy:" + term.gpu().set(1,h,text) + if(e.data[3]>3000) then + local temp=term.gpu().setForeground(0x00FF00) + term.gpu().set(string.len(text),h," " .. e.data[3]) + term.gpu().setForeground(temp) + elseif(e.data[3]>1500) then + local temp=term.gpu().setForeground(0xFFFF00) + term.gpu().set(string.len(text),h," " .. e.data[3]) + term.gpu().setForeground(temp) + else + local temp=term.gpu().setForeground(0xFF0000) + term.gpu().set(string.len(text),h," " .. e.data[3]) + term.gpu().setForeground(temp) + end + + term.setCursor(a,b) + elseif(e.data[1]~=nil) then + local a,b=term.getCursor() + + local w,h=term.gpu().getResolution() + term.gpu().fill(1,h/2,w,h/2-3,' ') + + term.setCursor(1,h/2) + print("Remote Response") + print(e.data[1]) + + term.setCursor(a,b) + end -- nil response are not rendered. +end) + +local timer=AddTimer(5,function() + if(computer.uptime()-last_response>15) then + local a,b=term.getCursor() + + local w,h=term.gpu().getResolution() + term.gpu().fill(1,h,w,1,' ') + + local temp=term.gpu().setForeground(0xFF0000) + term.gpu().set(1,h,"No drone in range.") + term.gpu().setForeground(temp) + + term.setCursor(a,b) + end + + modem.broadcast(98,"execute_command","modem.broadcast(99,'console_info',drone.getOffset(),computer.energy())") +end,-1) + +local history={} + +while true do + local w,h=term.gpu().getResolution() + term.gpu().fill(1,3,w,h/2-3,' ') + term.setCursor(1,3) + + local str=term.read(history) -- read a line + if(str==nil) then break end + modem.broadcast(98,'execute_command',str) + if(#history>25) then table.remove(history,1) end +end + +term.clear() +print("Closing port...") +modem.close(99) +print("Removing listeners...") +RemoveEventListener(listener) +print("Removing timers...") +RemoveTimer(timer)