This commit is contained in:
kepler155c
2018-11-15 12:17:50 -05:00
parent 560b9e42c9
commit 3e756abe42
10 changed files with 118 additions and 95 deletions

View File

@@ -78,6 +78,7 @@ local context = {
config = config, config = config,
resources = Util.readTable(Milo.RESOURCE_FILE) or { }, resources = Util.readTable(Milo.RESOURCE_FILE) or { },
state = { },
craftingQueue = { }, craftingQueue = { },
learnTypes = { }, learnTypes = { },
tasks = { }, tasks = { },
@@ -92,10 +93,6 @@ device[context.localName] = introspection.getInventory()
_G._p = context --debug _G._p = context --debug
Event.on('storage_offline', function()
Milo:showError('A storage chest has gone offline - Review configuration')
end)
Milo:init(context) Milo:init(context)
context.storage:initStorage() context.storage:initStorage()
@@ -125,9 +122,11 @@ Milo:clearGrid()
UI:setPage(UI:getPage('listing')) UI:setPage(UI:getPage('listing'))
local processing
Event.on('milo_cycle', function() Event.on('milo_cycle', function()
if not context.turtleBusy then if not processing and not Milo:isCraftingPaused() then
context.turtleBusy = true processing = true
Milo:resetCraftingStatus() Milo:resetCraftingStatus()
for _, task in ipairs(context.tasks) do for _, task in ipairs(context.tasks) do
@@ -139,7 +138,7 @@ Event.on('milo_cycle', function()
-- _G.printError(m) -- _G.printError(m)
end end
end end
context.turtleBusy = false processing = false
if not Util.empty(context.queue) then if not Util.empty(context.queue) then
os.queueEvent('milo_queue') os.queueEvent('milo_queue')
end end
@@ -147,8 +146,8 @@ Event.on('milo_cycle', function()
end) end)
Event.on('milo_queue', function() Event.on('milo_queue', function()
if not context.turtleBusy then if not processing and context.storage:isOnline() then
context.turtleBusy = true processing = true
for _, key in pairs(Util.keys(context.queue)) do for _, key in pairs(Util.keys(context.queue)) do
local entry = context.queue[key] local entry = context.queue[key]
@@ -156,16 +155,24 @@ Event.on('milo_queue', function()
context.queue[key] = nil context.queue[key] = nil
end end
context.turtleBusy = false processing = false
end end
end) end)
Event.onInterval(5, function() Event.onInterval(5, function()
if not Milo:isCraftingPaused() and context.storage:isOnline() then if not Milo:isCraftingPaused() then
os.queueEvent('milo_cycle') os.queueEvent('milo_cycle')
end end
end) end)
Event.on({ 'storage_offline', 'storage_online' }, function()
if context.storage:isOnline() then
Milo:resumeCrafting({ key = 'storageOnline' })
else
Milo:pauseCrafting({ key = 'storageOnline', msg = 'Storage offline' })
end
end)
os.queueEvent( os.queueEvent(
context.storage:isOnline() and 'storage_online' or 'storage_offline', context.storage:isOnline() and 'storage_online' or 'storage_offline',
context.storage:isOnline()) context.storage:isOnline())

View File

@@ -19,17 +19,29 @@ function Milo:getContext()
return self.context return self.context
end end
function Milo:pauseCrafting() function Milo:pauseCrafting(reason)
self.craftingPaused = true local _, key = Util.find(self.context.state, 'key', reason.key)
Milo:showError('Crafting Paused') if not key then
table.insert(self.context.state, reason)
os.queueEvent('milo_pause', reason)
end
end end
function Milo:resumeCrafting() function Milo:resumeCrafting(reason)
self.craftingPaused = false local _, key = Util.find(self.context.state, 'key', reason.key)
if key then
table.remove(self.context.state, key)
local n = self.context.state[#self.context.state]
if n then
os.queueEvent('milo_pause', n)
else
os.queueEvent('milo_resume')
end
end
end end
function Milo:isCraftingPaused() function Milo:isCraftingPaused()
return self.craftingPaused return self.context.state[#self.context.state]
end end
function Milo:getState(key) function Milo:getState(key)
@@ -72,13 +84,6 @@ function Milo:registerTask(task)
table.insert(self.context.tasks, task) table.insert(self.context.tasks, task)
end end
function Milo:showError(msg)
-- TODO: break dependency
if self.context.jobMonitor then
self.context.jobMonitor:showError(msg)
end
end
function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash) function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash)
if not ignoreDamage and not ignoreNbtHash then if not ignoreDamage and not ignoreNbtHash then
return items[inItem.key or self:uniqueKey(inItem)] return items[inItem.key or self:uniqueKey(inItem)]

View File

@@ -114,7 +114,7 @@ function Storage:filterActive(mtype, filter)
end) end)
end end
function Storage:onlineAdapters(reversed) function Storage:onlineAdapters()
local iter = { } local iter = { }
for _, v in pairs(self.nodes) do for _, v in pairs(self.nodes) do
if v.adapter and v.adapter.online and v.mtype == 'storage' then if v.adapter and v.adapter.online and v.mtype == 'storage' then
@@ -122,20 +122,14 @@ function Storage:onlineAdapters(reversed)
end end
end end
local function forwardSort(a, b) table.sort(iter, function(a, b)
if not a.priority then if not a.priority then
return false return false
elseif not b.priority then elseif not b.priority then
return true return true
end end
return a.priority > b.priority return a.priority > b.priority
end end)
local function backwardSort(a, b)
return not forwardSort(a, b)
end
table.sort(iter, reversed and backwardSort or forwardSort)
local i = 0 local i = 0
return function() return function()
@@ -378,12 +372,12 @@ function Storage:import(source, slot, count, item)
end end
-- high to low priority -- high to low priority
for remote in self:onlineAdapters() do for node in self:onlineAdapters() do
if count <= 0 then if count <= 0 then
break break
end end
if not remote.lock then if not node.lock then
insert(remote.adapter) insert(node.adapter)
end end
end end

View File

@@ -59,6 +59,11 @@ local networkPage = UI.Page {
help = 'Remove Node', help = 'Remove Node',
}, },
statusBar = UI.StatusBar { statusBar = UI.StatusBar {
ex = -9,
backgroundColor = colors.lightGray,
},
storageStatus = UI.Text {
x = -8, ex = -1, y = -1,
backgroundColor = colors.lightGray, backgroundColor = colors.lightGray,
}, },
notification = UI.Notification { }, notification = UI.Notification { },
@@ -109,16 +114,27 @@ function networkPage:getList()
end end
function networkPage:enable() function networkPage:enable()
self:getList() local function updateStatus()
self.grid:update() local isOnline = context.storage:isOnline()
self:setFocus(self.filter) self.storageStatus.value = isOnline and ' online' or 'offline'
UI.Page.enable(self) self.storageStatus.textColor = isOnline and colors.lime or colors.red
self.handler = Event.on({ 'device_attach', 'device_detach'}, function() self.storageStatus:draw()
end
self.handler = Event.on({ 'device_attach', 'device_detach', 'storage_online', 'storage_offline' }, function()
self:getList() self:getList()
self.grid:update() self:applyFilter()
self.grid:draw() self.grid:draw()
self.grid:sync() self.grid:sync()
updateStatus()
self:sync()
end) end)
self:getList()
self:applyFilter()
self:setFocus(self.filter)
UI.Page.enable(self)
updateStatus()
end end
function networkPage:disable() function networkPage:disable()
@@ -158,13 +174,16 @@ function networkPage:eventHandler(event)
context.config.nodes[node.name] = nil context.config.nodes[node.name] = nil
saveConfig() saveConfig()
end end
self.grid:update() self:applyFilter()
self.grid:draw() self.grid:draw()
elseif event.type == 'text_change' then elseif event.type == 'text_change' then
self:applyFilter() self:applyFilter()
self.grid:draw() self.grid:draw()
elseif event.type == 'grid_focus_row' then
self.statusBar:setStatus(event.selected.name)
elseif event.type == 'focus_change' then elseif event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help) self.statusBar:setStatus(event.focused.help)
@@ -298,13 +317,13 @@ function nodeWizard.filter:show(entry, callback, whitelistOnly)
UI.SlideOut.show(self) UI.SlideOut.show(self)
self:setFocus(self.form.scan) self:setFocus(self.form.scan)
Milo:pauseCrafting() Milo:pauseCrafting({ key = 'gridInUse', msg = 'Crafting paused' })
sync.lock(turtle) sync.lock(turtle)
end end
function nodeWizard.filter:hide() function nodeWizard.filter:hide()
UI.SlideOut.hide(self) UI.SlideOut.hide(self)
Milo:resumeCrafting() Milo:resumeCrafting({ key = 'gridInUse' })
sync.release(turtle) sync.release(turtle)
end end

View File

@@ -145,20 +145,10 @@ function page:refresh()
end end
function page:update() function page:update()
if context.storage:isOnline() then page:refresh()
page:refresh() page:sync()
page:sync()
else
page.grid:clear()
page.grid:centeredWrite(math.ceil(page.height / 2), 'Storage Offline')
page:sync()
end
end end
Event.on({ 'storage_offline', 'storage_online' }, function()
page:update()
end)
Event.on('monitor_touch', function(_, side) Event.on('monitor_touch', function(_, side)
if side == monitor.adapter.side then if side == monitor.adapter.side then
page:reset() page:reset()
@@ -166,6 +156,7 @@ Event.on('monitor_touch', function(_, side)
end end
end) end)
page:enable()
page:draw() page:draw()
page:sync() page:sync()

View File

@@ -1,14 +1,15 @@
local Ansi = require('ansi') local Ansi = require('ansi')
local Craft = require('turtle.craft') local Craft = require('turtle.craft')
local itemDB = require('itemDB') local Event = require('event')
local Milo = require('milo') local itemDB = require('itemDB')
local UI = require('ui') local Milo = require('milo')
local Util = require('util') local UI = require('ui')
local Util = require('util')
local colors = _G.colors local colors = _G.colors
local context = Milo:getContext() local context = Milo:getContext()
local device = _G.device local device = _G.device
local monitor = context.storage:getSingleNode('jobs') local monitor = context.storage:getSingleNode('jobs')
--[[ Configuration Screen ]] --[[ Configuration Screen ]]
local template = local template =
@@ -16,7 +17,7 @@ local template =
%sMilo must be restarted to activate diplay.]] %sMilo must be restarted to activate diplay.]]
local jobsWizardPage = UI.Window { local wizardPage = UI.Window {
title = 'Crafting Monitor', title = 'Crafting Monitor',
index = 2, index = 2,
backgroundColor = colors.cyan, backgroundColor = colors.cyan,
@@ -27,7 +28,7 @@ local jobsWizardPage = UI.Window {
}, },
} }
function jobsWizardPage:isValidType(node) function wizardPage:isValidType(node)
local m = device[node.name] local m = device[node.name]
return m and m.type == 'monitor' and { return m and m.type == 'monitor' and {
name = 'Crafting Monitor', name = 'Crafting Monitor',
@@ -36,11 +37,11 @@ function jobsWizardPage:isValidType(node)
} }
end end
function jobsWizardPage:isValidFor(node) function wizardPage:isValidFor(node)
return node.mtype == 'jobs' return node.mtype == 'jobs'
end end
UI:getPage('nodeWizard').wizard:add({ jobs = jobsWizardPage }) UI:getPage('nodeWizard').wizard:add({ jobs = wizardPage })
--[[ Display ]] --[[ Display ]]
if not monitor then if not monitor then
@@ -61,22 +62,16 @@ local jobMonitor = UI.Page {
{ heading = 'Qty', key = 'remaining', width = 4 }, { heading = 'Qty', key = 'remaining', width = 4 },
{ heading = 'Crafting', key = 'displayName', }, { heading = 'Crafting', key = 'displayName', },
{ heading = 'Status', key = 'status', }, { heading = 'Status', key = 'status', },
{ heading = 'need', key = 'need', width = 4 }, -- { heading = 'need', key = 'need', width = 4 },
{ heading = 'total', key = 'total', width = 4 }, -- { heading = 'total', key = 'total', width = 4 },
{ heading = 'used', key = 'used', width = 4 }, -- { heading = 'used', key = 'used', width = 4 },
{ heading = 'count', key = 'count', width = 4 }, -- { heading = 'count', key = 'count', width = 4 },
{ heading = 'crafted', key = 'crafted', width = 4 }, { heading = 'crafted', key = 'crafted', width = 4 },
-- { heading = 'Progress', key = 'progress', width = 8 }, -- { heading = 'Progress', key = 'progress', width = 8 },
}, },
}, },
} }
function jobMonitor:showError(msg)
self.grid:clear()
self.grid:centeredWrite(math.ceil(self.grid.height / 2), msg)
self:sync()
end
function jobMonitor:updateList(craftList) function jobMonitor:updateList(craftList)
if not Milo:isCraftingPaused() then if not Milo:isCraftingPaused() then
local t = { } local t = { }
@@ -125,6 +120,16 @@ function jobMonitor.grid:getRowTextColor(row, selected)
UI.Grid:getRowTextColor(row, selected) UI.Grid:getRowTextColor(row, selected)
end end
Event.on({ 'milo_resume', 'milo_pause' }, function(_, reason)
if reason then
jobMonitor.grid:clear()
jobMonitor.grid:centeredWrite(math.ceil(jobMonitor.grid.height / 2), reason.msg)
else
jobMonitor.grid:draw()
end
jobMonitor:sync()
end)
jobMonitor:enable() jobMonitor:enable()
jobMonitor:draw() jobMonitor:draw()
jobMonitor:sync() jobMonitor:sync()

View File

@@ -36,7 +36,7 @@ function learnPage:enable()
Milo:getState('learnType') or Milo:getState('learnType') or
self.chooser.choices[1].value self.chooser.choices[1].value
Milo:pauseCrafting() Milo:pauseCrafting({ key = 'gridInUse', msg = 'Crafting paused' })
sync.lock(turtle) sync.lock(turtle)
self:focusFirst() self:focusFirst()
@@ -50,7 +50,7 @@ end
function learnPage:eventHandler(event) function learnPage:eventHandler(event)
if event.type == 'cancel' then if event.type == 'cancel' then
sync.release(turtle) sync.release(turtle)
Milo:resumeCrafting() Milo:resumeCrafting({ key = 'gridInUse' })
UI:setPreviousPage() UI:setPreviousPage()
elseif event.type == 'accept' then elseif event.type == 'accept' then

View File

@@ -224,6 +224,7 @@ end
function listingPage:enable() function listingPage:enable()
self:refresh() self:refresh()
self:setFocus(self.statusBar.filter) self:setFocus(self.statusBar.filter)
self.timer = Event.onInterval(3, function() self.timer = Event.onInterval(3, function()
for _,v in pairs(self.allItems) do for _,v in pairs(self.allItems) do
local c = context.storage.cache[v.key] local c = context.storage.cache[v.key]
@@ -232,11 +233,22 @@ function listingPage:enable()
self.grid:draw() self.grid:draw()
self:sync() self:sync()
end) end)
self.handler = Event.on({ 'storage_offline', 'storage_online' }, function(_, isOnline)
self.statusBar.storageStatus.value =
isOnline and '' or 'offline'
self.statusBar.storageStatus.textColor =
isOnline and colors.lime or colors.red
self.statusBar.storageStatus:draw()
self:sync()
end)
UI.Page.enable(self) UI.Page.enable(self)
end end
function listingPage:disable() function listingPage:disable()
Event.off(self.timer) Event.off(self.timer)
Event.off(self.handler)
UI.Page.disable(self) UI.Page.disable(self)
end end
@@ -250,14 +262,4 @@ function listingPage:applyFilter()
self.grid:setValues(t) self.grid:setValues(t)
end end
Event.on({ 'storage_offline', 'storage_online' }, function(e, isOnline)
-- TODO: Fix button
listingPage.statusBar.storageStatus.value =
isOnline and '' or 'offline'
listingPage.statusBar.storageStatus.textColor =
isOnline and colors.lime or colors.red
listingPage.statusBar.storageStatus:draw()
listingPage:sync()
end)
UI:addPage('listing', listingPage) UI:addPage('listing', listingPage)

View File

@@ -135,7 +135,7 @@ function pages.confirmation:validate()
end end
function machineLearnWizard:disable() function machineLearnWizard:disable()
Milo:resumeCrafting() Milo:resumeCrafting({ key = 'gridInUse' })
sync.release(turtle) sync.release(turtle)
UI.Page.disable(self) UI.Page.disable(self)
end end

View File

@@ -124,7 +124,7 @@ local turtleLearnWizard = UI.Page {
} }
function turtleLearnWizard:disable() function turtleLearnWizard:disable()
Milo:resumeCrafting() Milo:resumeCrafting({ key = 'gridInUse' })
sync.release(turtle) sync.release(turtle)
UI.Page.disable(self) UI.Page.disable(self)
end end