OpenComputerScripts/programs/drone/drone_radar.lua

122 lines
3.3 KiB
Lua
Raw Permalink Normal View History

2018-12-04 14:32:18 +08:00
-- Passive Drone Radar
-- Created by Kiritow.
local component=require('component')
local computer=require('computer')
local term=require('term')
local text=require('text')
require("libevent")
2018-12-04 14:32:18 +08:00
local radar_version="Passive Drone Radar v0.1.5"
local modem=component.modem
local gpu=term.gpu()
local function status(msg)
local w,h=gpu.getResolution()
gpu.set(1,h,"Status: " .. msg)
end
term.clear()
print("Drone Radar")
print("Checking hardware...")
if(modem==nil) then
print("This program requires a modem component to work")
return
end
print("Opening modem port...")
if(not modem.open(99)) then
if(modem.isOpen(99)) then
print("Port 99 is already opened.")
else
print("Unable to open port 99.")
return
end
end
print("Adding listener...")
local tb_drone_old={}
local tb_drone_new={}
local listener=AddEventListener("modem_message",function(e)
2018-12-04 14:32:18 +08:00
if(e.port==99 and e.data[1]=='drone_info') then
tb_drone_old[e.senderAddress]=tb_drone_new[e.senderAddress]
tb_drone_new[e.senderAddress]={
distance=e.distance,
offset=e.data[2],
energy=e.data[3],
update=computer.uptime()
}
PushEvent("radar_gui_update")
end
end)
2018-12-07 13:38:11 +08:00
print("Adding update timer...")
local timer=AddTimer(5,function()
PushEvent("radar_gui_update")
end,-1)
local function show_tb(gpu,tb)
local maxLen={}
for idx,v in ipairs(tb) do
for nidx,val in ipairs(v) do
if(not maxLen[nidx] or maxLen[nidx]<string.len(val)) then
maxLen[nidx]=string.len(val)
end
end
end
for idx,v in ipairs(tb) do
for nidx,val in ipairs(v) do
v[nidx]=text.padRight(val,maxLen[nidx])
end
gpu.set(1,1+idx,table.concat(v," "))
end
end
term.clear()
print(radar_version)
while true do
local w,h=gpu.getResolution()
gpu.fill(1,2,w,h-1,' ')
local now=computer.uptime()
local show={}
2018-12-07 13:38:11 +08:00
table.insert(show,{"Address","Status","Distance","Offset","Energy","LastUpdated"})
for addr,tb in pairs(tb_drone_new) do
2018-12-07 13:38:11 +08:00
local newt={
string.sub(addr,1,8),
"[Missing]",
string.format("%.1f",tb.distance),
string.format("%.1f",tb.offset),
string.format("%.1f",tb.energy),
string.format("%.0fs",now-tb.update)
}
if(now-tb.update<30) then
if(tb.offset<1) then
newt[2]="[OK]"
else
if(tb_drone_old[addr]) then
local diff=tb_drone_new[addr].distance-tb_drone_old[addr].distance
2018-12-04 14:32:18 +08:00
if(diff>0) then
newt[2]="[Flying away]"
2018-12-04 14:32:18 +08:00
elseif(diff<0) then
newt[2]="[Flying in]"
end
else
newt[2]="[Flying]"
end
end
end
table.insert(show,newt)
2018-12-07 13:38:11 +08:00
if(now-tb.update>60) then
tb_drone_new[addr]=nil
tb_drone_old[addr]=nil
end
end
show_tb(gpu,show)
local e=WaitMultipleEvent("interrupted","radar_gui_update")
if(e.event=="interrupted") then break end
end
term.clear()
print("Stopping listener...")
RemoveEventListener(listener)
print("Closing port...")
modem.close(99)