From 906d26112b4a229f18b54587b0da1817e833bd80 Mon Sep 17 00:00:00 2001 From: kepler155c Date: Sun, 28 Oct 2018 23:37:44 -0400 Subject: [PATCH] milo wip --- milo/apis/milo.lua | 2 +- milo/plugins/activityMonitor.lua | 159 +++++++++++++++++++ milo/plugins/{jobList.lua => jobMonitor.lua} | 26 +-- milo/plugins/listing.lua | 1 + 4 files changed, 174 insertions(+), 14 deletions(-) create mode 100644 milo/plugins/activityMonitor.lua rename milo/plugins/{jobList.lua => jobMonitor.lua} (82%) diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index a81d5f2..a8b9e9f 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -80,7 +80,7 @@ function Milo:registerTask(task) end function Milo:showError(msg) - self.context.jobList:showError(msg) + self.context.jobMonitor:showError(msg) end function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash) diff --git a/milo/plugins/activityMonitor.lua b/milo/plugins/activityMonitor.lua new file mode 100644 index 0000000..684599d --- /dev/null +++ b/milo/plugins/activityMonitor.lua @@ -0,0 +1,159 @@ +local Event = require('event') +local Milo = require('milo') +local Peripheral = require('peripheral') +local UI = require('ui') +local Util = require('util') + +local colors = _G.colors + +local context = Milo:getContext() +local mon = Peripheral.lookup(context.config.activityMonitor) + +if not mon then + return +end + +local changedPage = UI.Window { + parent = UI.Device { + device = mon, + textScale = .5, + }, + grid = UI.Grid { + ey = -6, + 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', + }, + buttons = UI.Window { + y = -5, height = 5, + backgroundColor = colors.gray, + prevButton = UI.Button { + x = 2, y = 2, height = 3, width = 5, + event = 'previous', + backgroundColor = colors.lightGray, + text = ' < ' + }, + resetButton = UI.Button { + x = 8, y = 2, height = 3, ex = -8, + event = 'reset', + backgroundColor = colors.lightGray, + text = 'Reset' + }, + nextButton = UI.Button { + x = -6, y = 2, height = 3, width = 5, + event = 'next', + backgroundColor = colors.lightGray, + text = ' > ' + }, + }, + accelerators = { + q = 'quit', + } +} + +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.count = Util.toBytes(row.count) + row.rate = Util.toBytes(row.rate) + + return row +end + +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.Window.eventHandler(self, event) + end + + return true +end + +function changedPage: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, + lastCount = v.count, + } + end + self.timestamp = os.clock() + self.grid:setValues({ }) + + else + for _,v in pairs(self.lastItems) do + v.count = nil + end + + 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, + lastCount = 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.lastCount then + v.change = v.count - v.lastCount + v.rate = Util.round(60 / self.elapsed * v.change, 1) + changedItems[k] = v + end + end + + self.grid:setValues(changedItems) + end + self.grid:draw() +end + +Event.onInterval(5, function() + if context.storage:isOnline() then + changedPage:refresh() + changedPage:sync() + else + changedPage.grid:clear() + changedPage.grid:centeredWrite(math.ceil(changedPage.height / 2), 'Storage Offline') + changedPage:sync() + end +end) + +changedPage:draw() +changedPage:sync() diff --git a/milo/plugins/jobList.lua b/milo/plugins/jobMonitor.lua similarity index 82% rename from milo/plugins/jobList.lua rename to milo/plugins/jobMonitor.lua index 4862d3a..8d4b535 100644 --- a/milo/plugins/jobList.lua +++ b/milo/plugins/jobMonitor.lua @@ -11,7 +11,7 @@ local context = Milo:getContext() local mon = Peripheral.lookup(context.config.monitor) or error('Monitor is not attached') -local jobList = UI.Page { +local jobMonitor = UI.Page { parent = UI.Device { device = mon, textScale = .5, @@ -28,13 +28,13 @@ local jobList = UI.Page { }, } -function jobList:showError(msg) +function jobMonitor:showError(msg) self.grid:clear() self.grid:centeredWrite(math.ceil(self.grid.height / 2), msg) self:sync() end -function jobList:updateList(craftList) +function jobMonitor:updateList(craftList) if not Milo:isCraftingPaused() then local t = { } for _,v in pairs(craftList) do @@ -58,7 +58,7 @@ function jobList:updateList(craftList) end end -function jobList.grid:getDisplayValues(row) +function jobMonitor.grid:getDisplayValues(row) row = Util.shallowCopy(row) if row.showRemaining then row.remaining = math.max(0, row.count - row.crafted) @@ -69,7 +69,7 @@ function jobList.grid:getDisplayValues(row) return row end -function jobList.grid:getRowTextColor(row, selected) +function jobMonitor.grid:getRowTextColor(row, selected) local statusColor = { [ Craft.STATUS_ERROR ] = colors.red, [ Craft.STATUS_WARNING ] = colors.orange, @@ -80,18 +80,18 @@ function jobList.grid:getRowTextColor(row, selected) UI.Grid:getRowTextColor(row, selected) end -jobList:enable() -jobList:draw() -jobList:sync() +jobMonitor:enable() +jobMonitor:draw() +jobMonitor:sync() -local JobListTask = { +local jobMonitorTask = { name = 'job status', priority = 80, } -function JobListTask:cycle() - jobList:updateList(context.craftingQueue) +function jobMonitorTask:cycle() + jobMonitor:updateList(context.craftingQueue) end -Milo:registerTask(JobListTask) -context.jobList = jobList +Milo:registerTask(jobMonitorTask) +context.jobMonitor = jobMonitor diff --git a/milo/plugins/listing.lua b/milo/plugins/listing.lua index 5a5699a..fd04423 100644 --- a/milo/plugins/listing.lua +++ b/milo/plugins/listing.lua @@ -10,6 +10,7 @@ local os = _G.os local context = Milo:getContext() +-- TODO: fix local function queue(fn) while Milo:isCraftingPaused() do os.sleep(1)