2017-09-05 06:09:31 +00:00
|
|
|
local Event = require('event')
|
|
|
|
local GPS = require('gps')
|
2016-12-11 19:24:52 +00:00
|
|
|
local Socket = require('socket')
|
2017-09-05 06:09:31 +00:00
|
|
|
local Util = require('util')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-15 21:28:10 +00:00
|
|
|
local device = _G.device
|
|
|
|
local kernel = _G.kernel
|
|
|
|
local network = _G.network
|
|
|
|
local os = _G.os
|
|
|
|
local turtle = _G.turtle
|
2017-10-14 07:41:54 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
-- move this into gps api
|
|
|
|
local gpsRequested
|
|
|
|
local gpsLastPoint
|
|
|
|
local gpsLastRequestTime
|
|
|
|
|
|
|
|
local function snmpConnection(socket)
|
2018-01-24 22:39:38 +00:00
|
|
|
while true do
|
|
|
|
local msg = socket:read()
|
|
|
|
if not msg then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if msg.type == 'reboot' then
|
|
|
|
os.reboot()
|
|
|
|
|
|
|
|
elseif msg.type == 'shutdown' then
|
|
|
|
os.shutdown()
|
|
|
|
|
|
|
|
elseif msg.type == 'ping' then
|
|
|
|
socket:write('pong')
|
|
|
|
|
|
|
|
elseif msg.type == 'script' then
|
2018-04-01 22:40:08 +00:00
|
|
|
local env = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
|
|
|
local fn, err = load(msg.args, 'script', nil, env)
|
2018-01-24 22:39:38 +00:00
|
|
|
if fn then
|
|
|
|
kernel.run({
|
|
|
|
fn = fn,
|
2018-04-01 22:40:08 +00:00
|
|
|
env = env,
|
2018-01-24 22:39:38 +00:00
|
|
|
title = 'script',
|
|
|
|
})
|
|
|
|
else
|
|
|
|
_G.printError(err)
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif msg.type == 'scriptEx' then
|
|
|
|
local s, m = pcall(function()
|
|
|
|
local env = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
|
|
|
local fn, m = load(msg.args, 'script', nil, env)
|
|
|
|
if not fn then
|
|
|
|
error(m)
|
|
|
|
end
|
|
|
|
return { fn() }
|
|
|
|
end)
|
|
|
|
if s then
|
|
|
|
socket:write(m)
|
|
|
|
else
|
|
|
|
socket:write({ s, m })
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif msg.type == 'gps' then
|
|
|
|
if gpsRequested then
|
|
|
|
repeat
|
|
|
|
os.sleep(0)
|
|
|
|
until not gpsRequested
|
|
|
|
end
|
|
|
|
|
|
|
|
if gpsLastPoint and os.clock() - gpsLastRequestTime < .5 then
|
|
|
|
socket:write(gpsLastPoint)
|
|
|
|
else
|
|
|
|
|
|
|
|
gpsRequested = true
|
|
|
|
local pt = GPS.getPoint(2)
|
|
|
|
if pt then
|
|
|
|
socket:write(pt)
|
|
|
|
else
|
|
|
|
print('snmp: Unable to get GPS point')
|
|
|
|
end
|
|
|
|
gpsRequested = false
|
|
|
|
gpsLastPoint = pt
|
|
|
|
if pt then
|
|
|
|
gpsLastRequestTime = os.clock()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif msg.type == 'info' then
|
|
|
|
local info = {
|
|
|
|
id = os.getComputerID(),
|
|
|
|
label = os.getComputerLabel(),
|
|
|
|
uptime = math.floor(os.clock()),
|
|
|
|
}
|
|
|
|
if turtle then
|
|
|
|
info.fuel = turtle.getFuelLevel()
|
|
|
|
info.status = turtle.getStatus()
|
|
|
|
end
|
|
|
|
socket:write(info)
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.addRoutine(function()
|
2018-01-24 22:39:38 +00:00
|
|
|
print('snmp: listening on port 161')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
while true do
|
|
|
|
local socket = Socket.server(161)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
Event.addRoutine(function()
|
|
|
|
print('snmp: connection from ' .. socket.dhost)
|
|
|
|
snmpConnection(socket)
|
|
|
|
print('snmp: closing connection to ' .. socket.dhost)
|
|
|
|
end)
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end)
|
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
device.wireless_modem.open(999)
|
|
|
|
print('discovery: listening on port 999')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2017-10-14 07:41:54 +00:00
|
|
|
Event.on('modem_message', function(_, _, sport, id, info, distance)
|
2018-01-24 22:39:38 +00:00
|
|
|
if sport == 999 and tonumber(id) and type(info) == 'table' then
|
|
|
|
if not network[id] then
|
|
|
|
network[id] = { }
|
|
|
|
end
|
|
|
|
Util.merge(network[id], info)
|
|
|
|
network[id].distance = distance
|
|
|
|
network[id].timestamp = os.clock()
|
|
|
|
|
|
|
|
if not network[id].active then
|
|
|
|
network[id].active = true
|
|
|
|
os.queueEvent('network_attach', network[id])
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end)
|
|
|
|
|
2016-12-23 04:22:04 +00:00
|
|
|
local info = {
|
2018-01-24 22:39:38 +00:00
|
|
|
id = os.getComputerID()
|
2016-12-23 04:22:04 +00:00
|
|
|
}
|
2017-09-21 05:44:33 +00:00
|
|
|
local infoTimer = os.clock()
|
2016-12-23 04:22:04 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local function sendInfo()
|
2018-01-24 22:39:38 +00:00
|
|
|
if os.clock() - infoTimer >= 1 then -- don't flood
|
|
|
|
infoTimer = os.clock()
|
|
|
|
info.label = os.getComputerLabel()
|
|
|
|
info.uptime = math.floor(os.clock())
|
|
|
|
if turtle then
|
|
|
|
info.fuel = turtle.getFuelLevel()
|
|
|
|
info.status = turtle.getStatus()
|
|
|
|
info.point = turtle.point
|
|
|
|
info.inventory = turtle.getInventory()
|
|
|
|
info.slotIndex = turtle.getSelectedSlot()
|
|
|
|
end
|
2018-02-09 13:01:02 +00:00
|
|
|
if device.neuralInterface then
|
|
|
|
info.status = device.neuralInterface.status
|
2018-10-26 22:45:22 +00:00
|
|
|
pcall(function()
|
|
|
|
if not info.status and device.neuralInterface.getMetaOwner then
|
|
|
|
info.status = 'health: ' ..
|
|
|
|
math.floor(device.neuralInterface.getMetaOwner().health /
|
|
|
|
device.neuralInterface.getMetaOwner().maxHealth * 100)
|
|
|
|
end
|
|
|
|
end)
|
2018-02-09 13:01:02 +00:00
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
device.wireless_modem.transmit(999, os.getComputerID(), info)
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- every 10 seconds, send out this computer's info
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.onInterval(10, function()
|
2018-01-24 22:39:38 +00:00
|
|
|
sendInfo()
|
|
|
|
for _,c in pairs(_G.network) do
|
|
|
|
local elapsed = os.clock()-c.timestamp
|
|
|
|
if c.active and elapsed > 15 then
|
|
|
|
c.active = false
|
|
|
|
os.queueEvent('network_detach', c)
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end)
|
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.on('turtle_response', function()
|
2018-01-24 22:39:38 +00:00
|
|
|
if turtle.getStatus() ~= info.status or
|
|
|
|
turtle.fuel ~= info.fuel then
|
|
|
|
sendInfo()
|
|
|
|
end
|
2017-08-03 05:46:39 +00:00
|
|
|
end)
|