2016-12-11 19:24:52 +00:00
|
|
|
require = requireInjector(getfenv(1))
|
2017-04-16 03:39:15 +00:00
|
|
|
local Util = require('util')
|
2016-12-11 19:24:52 +00:00
|
|
|
local Event = require('event')
|
|
|
|
local UI = require('ui')
|
2017-04-15 15:38:08 +00:00
|
|
|
local RefinedProvider = require('refinedProvider')
|
|
|
|
local MEProvider = require('meProvider')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2017-04-15 15:38:08 +00:00
|
|
|
local storage = RefinedProvider()
|
|
|
|
if not storage:isValid() then
|
|
|
|
storage = MEProvider()
|
|
|
|
end
|
|
|
|
|
|
|
|
if not storage:isValid() then
|
|
|
|
error('Not connected to a storage device')
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
multishell.setTitle(multishell.getCurrent(), 'Storage Activity')
|
|
|
|
UI:configure('StorageActivity', ...)
|
|
|
|
|
|
|
|
local changedPage = UI.Page({
|
|
|
|
grid = UI.Grid({
|
|
|
|
columns = {
|
2017-04-16 03:39:15 +00:00
|
|
|
{ heading = 'Qty', key = 'qty', width = 5 },
|
|
|
|
{ heading = 'Change', key = 'change', width = 6 },
|
|
|
|
{ heading = 'Name', key = 'display_name', width = UI.term.width - 15 },
|
2016-12-11 19:24:52 +00:00
|
|
|
},
|
2017-04-16 03:39:15 +00:00
|
|
|
sortColumn = 'display_name',
|
|
|
|
rey = -6,
|
2016-12-11 19:24:52 +00:00
|
|
|
}),
|
|
|
|
buttons = UI.Window({
|
2017-04-16 03:39:15 +00:00
|
|
|
ry = -4,
|
2016-12-11 19:24:52 +00:00
|
|
|
height = 5,
|
|
|
|
backgroundColor = colors.gray,
|
|
|
|
prevButton = UI.Button({
|
|
|
|
event = 'previous',
|
|
|
|
backgroundColor = colors.lightGray,
|
|
|
|
x = 2,
|
|
|
|
y = 2,
|
|
|
|
height = 3,
|
|
|
|
width = 5,
|
|
|
|
text = ' < '
|
|
|
|
}),
|
|
|
|
resetButton = UI.Button({
|
|
|
|
event = 'reset',
|
|
|
|
backgroundColor = colors.lightGray,
|
|
|
|
x = 8,
|
|
|
|
y = 2,
|
|
|
|
height = 3,
|
2017-04-16 03:39:15 +00:00
|
|
|
rex = -8,
|
2016-12-11 19:24:52 +00:00
|
|
|
text = 'Reset'
|
|
|
|
}),
|
|
|
|
nextButton = UI.Button({
|
|
|
|
event = 'next',
|
|
|
|
backgroundColor = colors.lightGray,
|
2017-04-16 03:39:15 +00:00
|
|
|
rx = -5,
|
2016-12-11 19:24:52 +00:00
|
|
|
y = 2,
|
|
|
|
height = 3,
|
|
|
|
width = 5,
|
|
|
|
text = ' > '
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
accelerators = {
|
|
|
|
q = 'quit',
|
|
|
|
}
|
|
|
|
})
|
2017-04-16 03:39:15 +00:00
|
|
|
|
|
|
|
function changedPage.grid:getDisplayValues(row)
|
|
|
|
row = Util.shallowCopy(row)
|
|
|
|
|
|
|
|
local ind = '+'
|
|
|
|
if row.change < 0 then
|
|
|
|
ind = ''
|
|
|
|
end
|
|
|
|
row.change = ind .. Util.toBytes(row.change)
|
|
|
|
row.qty = Util.toBytes(row.qty)
|
|
|
|
|
|
|
|
return row
|
|
|
|
end
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
function changedPage:eventHandler(event)
|
|
|
|
|
|
|
|
if event.type == 'reset' then
|
|
|
|
self.lastItems = nil
|
|
|
|
self.grid:setValues({ })
|
|
|
|
self.grid:clear()
|
|
|
|
self.grid:draw()
|
|
|
|
|
|
|
|
elseif event.type == 'next' then
|
|
|
|
self.grid:nextPage()
|
|
|
|
|
|
|
|
elseif event.type == 'previous' then
|
|
|
|
self.grid:previousPage()
|
|
|
|
|
|
|
|
elseif event.type == 'quit' then
|
|
|
|
Event.exitPullEvents()
|
|
|
|
|
|
|
|
else
|
|
|
|
return UI.Page.eventHandler(self, event)
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function changedPage:refresh()
|
2017-04-15 15:38:08 +00:00
|
|
|
local t = storage:listItems('all')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
if not t or Util.empty(t) then
|
|
|
|
self:clear()
|
|
|
|
self:centeredWrite(math.ceil(self.height/2), 'Communication failure')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
for k,v in pairs(t) do
|
2017-04-15 15:38:08 +00:00
|
|
|
t[k] = Util.shallowCopy(v)
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
if not self.lastItems then
|
|
|
|
self.lastItems = t
|
|
|
|
self.grid:setValues({ })
|
|
|
|
else
|
|
|
|
local changedItems = {}
|
|
|
|
for _,v in pairs(self.lastItems) do
|
|
|
|
found = false
|
|
|
|
for k2,v2 in pairs(t) do
|
|
|
|
if v.id == v2.id and
|
|
|
|
v.dmg == v2.dmg then
|
|
|
|
if v.qty ~= v2.qty then
|
|
|
|
local c = Util.shallowCopy(v2)
|
|
|
|
c.lastQty = v.qty
|
|
|
|
table.insert(changedItems, c)
|
|
|
|
end
|
|
|
|
table.remove(t, k2)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- New item
|
|
|
|
if not found then
|
|
|
|
local c = Util.shallowCopy(v)
|
|
|
|
c.lastQty = v.qty
|
|
|
|
c.qty = 0
|
|
|
|
table.insert(changedItems, c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- No items left
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
v.lastQty = 0
|
|
|
|
table.insert(changedItems, v)
|
|
|
|
end
|
2017-04-16 03:39:15 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
for k,v in pairs(changedItems) do
|
2017-04-16 03:39:15 +00:00
|
|
|
v.change = v.qty - v.lastQty
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
2017-04-16 03:39:15 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
self.grid:setValues(changedItems)
|
|
|
|
end
|
2017-04-16 03:39:15 +00:00
|
|
|
self.grid:draw()
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Event.addTimer(5, true, function()
|
|
|
|
changedPage:refresh()
|
|
|
|
changedPage:sync()
|
|
|
|
end)
|
|
|
|
|
|
|
|
UI:setPage(changedPage)
|
2017-04-16 03:39:15 +00:00
|
|
|
UI:pullEvents()
|
2016-12-11 19:24:52 +00:00
|
|
|
UI.term:reset()
|