milo: better monitor handling + assigning machines to items

This commit is contained in:
kepler155c
2018-11-17 21:27:45 -05:00
parent 939ec9516d
commit 2605410c6d
10 changed files with 262 additions and 225 deletions

View File

@@ -7,15 +7,12 @@ local Util = require('util')
local colors = _G.colors
local context = Milo:getContext()
local device = _G.device
local monitor = context.storage:getSingleNode('activity')
--[[ Configuration Page ]]--
local template =
[[%sDisplays the amount of items entering or leaving storage.%s
Right-clicking on the activity monitor will reset the totals.
%sMilo must be restarted to activate diplay.]]
Right-clicking on the activity monitor will reset the totals.]]
local activityWizardPage = UI.Window {
title = 'Activity Monitor',
@@ -24,7 +21,7 @@ local activityWizardPage = UI.Window {
[1] = UI.TextArea {
x = 2, ex = -2, y = 2, ey = -2,
marginRight = 0,
value = string.format(template, Ansi.yellow, Ansi.reset, Ansi.orange),
value = string.format(template, Ansi.yellow, Ansi.reset),
},
}
@@ -43,122 +40,128 @@ end
UI:getPage('nodeWizard').wizard:add({ activity = activityWizardPage })
if not monitor then
return
end
local page = UI.Window {
parent = UI.Device {
device = monitor.adapter,
textScale = .5,
},
grid = UI.Grid {
columns = {
{ heading = 'Qty', key = 'count', width = 6 },
{ heading = 'Change', key = 'change', width = 6 },
{ heading = 'Rate', key = 'rate', width = 6 },
{ heading = 'Name', key = 'displayName' },
--[[ Display ]]--
local function createPage(node)
local page = UI.Window {
parent = UI.Device {
device = node.adapter,
textScale = .5,
},
sortColumn = 'displayName',
},
}
grid = UI.Grid {
columns = {
{ heading = 'Qty', key = 'count', width = 6 },
{ heading = 'Change', key = 'change', width = 6 },
{ heading = 'Rate', key = 'rate', width = 6 },
{ heading = 'Name', key = 'displayName' },
},
sortColumn = 'displayName',
},
}
function page.grid:getRowTextColor(row, selected)
if row.lastCount and row.lastCount ~= row.count then
return row.count > row.lastCount and colors.yellow or colors.lightGray
end
return UI.Grid:getRowTextColor(row, selected)
end
function page.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
local ind = '+'
if row.change < 0 then
ind = ''
end
row.change = ind .. Util.toBytes(row.change)
row.count = Util.toBytes(row.count)
row.rate = Util.toBytes(row.rate)
return row
end
function page:reset()
self.lastItems = nil
self.grid:setValues({ })
self.grid:clear()
self.grid:draw()
end
function page:refresh()
local t = context.storage.cache
if not self.lastItems then
self.lastItems = { }
for k,v in pairs(t) do
self.lastItems[k] = {
displayName = v.displayName,
initialCount = v.count,
}
function page.grid:getRowTextColor(row, selected)
if row.lastCount and row.lastCount ~= row.count then
return row.count > row.lastCount and colors.yellow or colors.lightGray
end
self.timestamp = os.clock()
return UI.Grid:getRowTextColor(row, selected)
end
function page.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
local ind = '+'
if row.change < 0 then
ind = ''
end
row.change = ind .. Util.toBytes(row.change)
row.count = Util.toBytes(row.count)
row.rate = Util.toBytes(row.rate)
return row
end
function page:reset()
self.lastItems = nil
self.grid:setValues({ })
self.grid:clear()
self.grid:draw()
end
else
for _,v in pairs(self.lastItems) do
v.lastCount = v.count
v.count = nil
end
function page:refresh()
local t = context.storage.cache
self.elapsed = os.clock() - self.timestamp
for k,v in pairs(t) do
local v2 = self.lastItems[k]
if v2 then
v2.count = v.count
else
if not self.lastItems then
self.lastItems = { }
for k,v in pairs(t) do
self.lastItems[k] = {
displayName = v.displayName,
count = v.count,
initialCount = 0,
initialCount = v.count,
}
end
end
self.timestamp = os.clock()
self.grid:setValues({ })
local changedItems = { }
for k,v in pairs(self.lastItems) do
if not v.count then
v.count = 0
else
for _,v in pairs(self.lastItems) do
v.lastCount = v.count
v.count = nil
end
if v.count ~= v.initialCount then
v.change = v.count - v.initialCount
v.rate = Util.round(60 / self.elapsed * v.change, 1)
changedItems[k] = v
end
end
self.grid:setValues(changedItems)
self.elapsed = os.clock() - self.timestamp
for k,v in pairs(t) do
local v2 = self.lastItems[k]
if v2 then
v2.count = v.count
else
self.lastItems[k] = {
displayName = v.displayName,
count = v.count,
initialCount = 0,
}
end
end
local changedItems = { }
for k,v in pairs(self.lastItems) do
if not v.count then
v.count = 0
end
if v.count ~= v.initialCount then
v.change = v.count - v.initialCount
v.rate = Util.round(60 / self.elapsed * v.change, 1)
changedItems[k] = v
end
end
self.grid:setValues(changedItems)
end
self.grid:draw()
end
self.grid:draw()
end
function page:update()
page:refresh()
page:sync()
end
Event.on('monitor_touch', function(_, side)
if side == monitor.adapter.side then
page:reset()
function page:update()
page:refresh()
page:sync()
end
end)
page:enable()
page:draw()
page:sync()
page:enable()
page:draw()
page:sync()
return page
end
local pages = { }
Event.on('monitor_touch', function(_, side)
local function filter(node)
return node.adapter.name == side and pages[node.name]
end
for node in context.storage:filterActive('activity', filter) do
pages[node.name]:reset()
pages[node.name]:sync()
end
end)
--[[ Task ]]--
local ActivityTask = {
@@ -167,7 +170,12 @@ local ActivityTask = {
}
function ActivityTask:cycle()
page:update()
for node in context.storage:filterActive('activity') do
if not pages[node.name] then
pages[node.name] = createPage(node)
end
pages[node.name]:update()
end
end
Milo:registerTask(ActivityTask)