gpsServer final tweaks

This commit is contained in:
xAnavrins
2019-07-19 22:37:11 -04:00
parent 7b91ddefe9
commit edb29f5875

View File

@@ -26,7 +26,16 @@ local positions = { }
---UI:configure('gps', ...) ---UI:configure('gps', ...)
local page = UI.Page { local page = UI.Page {
menuBar = UI.MenuBar {
showBackButton = true,
buttons = {
{ text = 'Clear:', inactive = true },
{ text = 'Inactive', event = 'clear_inactive' },
{ text = 'All', event = 'clear_all' },
},
},
grid = UI.ScrollingGrid { grid = UI.ScrollingGrid {
y = 2,
sortColumn = 'id', sortColumn = 'id',
autospace = true, autospace = true,
focusIndicator = ' ', focusIndicator = ' ',
@@ -35,11 +44,12 @@ local page = UI.Page {
{ heading = 'ID', key = 'id', align = 'right', width = 5, textColor = colors.pink }, { heading = 'ID', key = 'id', align = 'right', width = 5, textColor = colors.pink },
{ heading = 'X', key = 'x', align = 'right', width = 6 }, { heading = 'X', key = 'x', align = 'right', width = 6 },
{ heading = 'Y', key = 'y', align = 'right', width = 4 }, { heading = 'Y', key = 'y', align = 'right', width = 4 },
{ heading = 'Z', key = 'z', width = 6 }, { heading = 'Z', key = 'z', width = 6, align = 'right' },
{ heading = 'Dist', key = 'dist', align = 'right', width = 5, textColor = colors.orange }, { heading = 'Dist', key = 'dist', align = 'right', width = 7, textColor = colors.orange },
} }
} }
} }
page:setFocus(page.grid)
function page.grid:getDisplayValues(row) function page.grid:getDisplayValues(row)
row = Util.shallowCopy(row) row = Util.shallowCopy(row)
@@ -49,10 +59,25 @@ function page.grid:getDisplayValues(row)
end end
function page.grid:getRowTextColor(row, selected) function page.grid:getRowTextColor(row, selected)
return ((row.x ~= row.lastPos.x) or return row.changed and colors.yellow or not row.alive and colors.lightGray or UI.Grid.getRowTextColor(self, row, selected)
(row.y ~= row.lastPos.y) or end
(row.z ~= row.lastPos.z)) and
colors.yellow or not row.alive and colors.lightGray or UI.Grid.getRowTextColor(self, row, selected) function page.menuBar:eventHandler(event)
if event.type == 'clear_inactive' then
for id, detail in pairs(positions) do
if not detail.alive then
positions[id] = nil
end
end
elseif event.type == 'clear_all' then
for id, detail in pairs(positions) do
positions[id] = nil
end
else return UI.MenuBar.eventHandler(self, event)
end
page.grid:update()
page:draw()
page:sync()
end end
local function build() local function build()
@@ -189,28 +214,33 @@ local function server(mode)
if #computer == 4 then if #computer == 4 then
local pt = GPS.trilaterate(computer) local pt = GPS.trilaterate(computer)
if pt then if pt then
if not positions[computerId] then local comp = { lastPos = {} }
positions[computerId] = { lastPos = {} } if positions[computerId] then
comp = positions[computerId]
end end
positions[computerId].lastPos.x = positions[computerId].x or 0
positions[computerId].lastPos.y = positions[computerId].y or 0 comp.lastPos.x = comp.x or 0
positions[computerId].lastPos.z = positions[computerId].z or 0 comp.lastPos.y = comp.y or 0
positions[computerId].x = pt.x comp.lastPos.z = comp.z or 0
positions[computerId].y = pt.y comp.x = pt.x
positions[computerId].z = pt.z comp.y = pt.y
positions[computerId].id = computerId comp.z = pt.z
positions[computerId].hbeat = not positions[computerId].hbeat comp.id = computerId
positions[computerId].alive = true comp.hbeat = not comp.hbeat
positions[computerId].timestamp = os.clock() comp.alive = true
local dist = (vector.new(config.x, config.y, config.z) - vector.new(positions[computerId].x, positions[computerId].y, positions[computerId].z)):length() comp.timestamp = os.clock()
if positions[computerId].dist ~= dist then comp.dist = (vector.new(config.x, config.y, config.z) - vector.new(comp.x, comp.y, comp.z)):length()
positions[computerId].needUpdate = true if (comp.x ~= comp.lastPos.x or comp.y ~= comp.lastPos.y or comp.z ~= comp.lastPos.z) then
comp.changed = true
comp.lastChanged = os.clock()
end end
positions[computerId].dist = dist if mode == 'snmp' and type(msg) == "table" then
end comp.label = msg.label or '*'
if mode == 'snmp' and type(msg) == "table" then end
positions[computerId].label = msg.label or '*'
positions[computerId] = comp
end end
computers[computerId] = nil computers[computerId] = nil
page.grid.values = positions page.grid.values = positions
page.grid:update() page.grid:update()
@@ -237,15 +267,11 @@ local function server(mode)
Event.onInterval(1, function() Event.onInterval(1, function()
local resync = false local resync = false
for id, detail in pairs(positions) do for id, detail in pairs(positions) do
local elapsed = os.clock() - detail.timestamp if os.clock() - detail.lastChanged > 10 then
if elapsed > 15 and detail.needUpdate then detail.changed = false
detail.lastPos.x = detail.x
detail.lastPos.y = detail.y
detail.lastPos.z = detail.z
detail.timestamp = os.clock()
detail.needUpdate = false
resync = true resync = true
elseif elapsed > 60 and detail.alive then end
if os.clock() - detail.timestamp > 60 and detail.alive then
detail.alive = false detail.alive = false
detail.hbeat = false detail.hbeat = false
resync = true resync = true
@@ -276,7 +302,7 @@ elseif args[1] == 'snmp' then
table.insert(page.grid.columns, table.insert(page.grid.columns,
{ heading = 'Label', key = 'label', textColor = colors.cyan } { heading = 'Label', key = 'label', textColor = colors.cyan }
) )
page.grid:adjustWidth() page.grid:adjustWidth()
server('snmp') server('snmp')
else else