From d227d6ebb7ddf1ae7ee9c1bb2f0531a9c845bdde Mon Sep 17 00:00:00 2001 From: kepler155c Date: Fri, 26 Oct 2018 23:08:44 -0400 Subject: [PATCH] milo wip --- milo/Milo.lua | 6 ++- milo/apis/milo.lua | 2 +- milo/apis/storage.lua | 67 +++++++++--------------------- milo/core/machines.lua | 88 +++++++++++++++++++++++----------------- milo/plugins/listing.lua | 21 +++++++++- 5 files changed, 94 insertions(+), 90 deletions(-) diff --git a/milo/Milo.lua b/milo/Milo.lua index c088274..e902594 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -67,6 +67,7 @@ local Util = require('util') local fs = _G.fs local multishell = _ENV.multishell +local os = _G.os local shell = _ENV.shell if multishell then @@ -113,7 +114,6 @@ end) Milo:init(context) context.storage:initStorage() -context.storage:initTrashcan() local function loadDirectory(dir) for _, file in pairs(fs.list(dir)) do @@ -157,4 +157,8 @@ Event.onInterval(5, function() end end) +os.queueEvent( + context.storage:isOnline() and 'storage_online' or 'storage_offline', + context.storage:isOnline()) + UI:pullEvents() diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index e139353..f02401c 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -116,7 +116,7 @@ end function Milo:clearGrid() local function clear() turtle.eachFilledSlot(function(slot) - self.context.storage:insert(slot.index, slot.count, nil, slot) + self.context.storage:import(self.context.localName, slot.index, slot.count, slot) end) for i = 1, 16 do diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index 8a9d8f3..ddb2f93 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -24,32 +24,20 @@ listCount = 0, self.localName = modem.getNameLocal() Event.on({ 'device_attach' }, function(_, dev) - if self.remoteDefaults[dev] then - if self.remoteDefaults[dev].mtype == 'storage' then - self:initStorage() - end - if self.remoteDefaults[dev].mtype == 'trashcan' then - self:initTrashcan() - end - end +debug('attach: ' .. dev) + self:initStorage() end) Event.on({ 'device_detach' }, function(_, dev) - if self.remoteDefaults[dev] then - if self.remoteDefaults[dev].mtype == 'storage' then - self:initStorage(dev) - end - if self.remoteDefaults[dev].mtype == 'trashcan' then - self:initTrashcan(dev) - end - end +debug('detach: ' .. dev) + self:initStorage(dev) end) end function NetworkedAdapter:setOnline(online) if online ~= self.storageOnline then self.storageOnline = online - os.queueEvent(self.storageOnline and 'storage_online' or 'storage_offline') + os.queueEvent(self.storageOnline and 'storage_online' or 'storage_offline', online) debug('Storage: %s', self.storageOnline and 'online' or 'offline') end end @@ -58,22 +46,6 @@ function NetworkedAdapter:isOnline() return self.storageOnline end -function NetworkedAdapter:initTrashcan(detachedDevice) - local trashcan = Util.find(self.remoteDefaults, 'mtype', 'trashcan') - - if (detachedDevice and self.trashcan and self.trashcan.name == detachedDevice) or - (trashcan and not device[trashcan.name]) then - self.trashcan = nil -debug(' Trashcan: none') - - elseif trashcan and device[trashcan.name] then - if not self.trashcan or (self.trashcan and self.trashcan.name ~= trashcan.name) then -debug(' Trashcan: ' .. trashcan.name) - self.trashcan = device[trashcan.name] - end - end -end - function NetworkedAdapter:initStorage() local online = true @@ -81,15 +53,13 @@ function NetworkedAdapter:initStorage() for k,v in pairs(self.remoteDefaults) do if v.adapter then v.adapter.online = not not device[k] - if v.mtype == 'storage' then - online = online and v.adapter.online - end - elseif v.mtype == 'storage' then + elseif device[k] and device[k].list and device[k].size and device[k].pullItems then v.adapter = InventoryAdapter.wrap({ side = k }) v.adapter.online = true end if v.mtype == 'storage' then - debug(' %s: %s', v.adapter.online and ' online' or 'offline', k) + online = online and not not (v.adapter and v.adapter.online) + debug(' %s: %s', v.adapter and v.adapter.online and ' online' or 'offline', k) end end @@ -184,11 +154,11 @@ function NetworkedAdapter:provide(item, qty, slot, direction) local total = 0 for _, adapter in self:onlineAdapters() do - local amount = adapter:provide(item, qty, slot, direction) + local amount = adapter:provide(item, qty, slot, direction or self.localName) if amount > 0 then ---debug('EXT: %s(%d): %s -> %s%s', --- item.name, amount, remote.side, direction or self.localName, --- slot and string.format('[%d]', slot) or '') +debug('EXT: %s(%d): %s -> %s%s', + item.name, amount, adapter.name, direction or self.localName, + slot and string.format('[%d]', slot) or '') self.dirty = true adapter.dirty = true end @@ -203,9 +173,10 @@ function NetworkedAdapter:provide(item, qty, slot, direction) end function NetworkedAdapter:trash(source, slot, count) - if self.trashcan then - debug('TRA: %s[%d] (%d)', source, slot, count) - return self.trashcan.pullItems(source, slot, count) + local trashcan = Util.find(self.remoteDefaults, 'mtype', 'trashcan') + if trashcan and trashcan.adapter and trashcan.adapter.online then +debug('TRA: %s[%d] (%d)', source or self.localName, slot, count or 64) + return trashcan.adapter.pullItems(source or self.localName, slot, count) end return 0 end @@ -233,7 +204,7 @@ function NetworkedAdapter:insert(slot, qty, toSlot, item, source) if amount > 0 then debug('INS: %s(%d): %s[%d] -> %s', item.name, amount, - source or self.localName, slot, adapter.side) + source, slot, adapter.name) self.dirty = true adapter.dirty = true local entry = self.activity[key] or 0 @@ -261,7 +232,7 @@ debug('INS: %s(%d): %s[%d] -> %s', if qty <= 0 then break end - if adapter.cache and adapter.cache[key] and not adapter.lockWith then + if adapter.cache and adapter.cache[key] and not adapter.lock then insert(adapter) end end @@ -272,7 +243,7 @@ debug('INS: %s(%d): %s[%d] -> %s', if qty <= 0 then break end - if not remote.lockWith then + if not remote.lock then insert(remote.adapter) end end diff --git a/milo/core/machines.lua b/milo/core/machines.lua index be7f3c0..10d3187 100644 --- a/milo/core/machines.lua +++ b/milo/core/machines.lua @@ -10,6 +10,20 @@ local device = _G.device local context = Milo:getContext() +local function saveConfig() + local t = { } + for k,v in pairs(context.config.remoteDefaults) do + t[k] = v.adapter + v.adapter = nil + end + + Config.update('milo', context.config) + + for k,v in pairs(t) do + context.config.remoteDefaults[k].adapter = v + end +end + local machinesPage = UI.Page { titleBar = UI.TitleBar { previousPage = true, @@ -19,14 +33,21 @@ local machinesPage = UI.Page { y = 2, ey = -2, values = context.config.remoteDefaults, columns = { + { key = 'priority', width = 3 }, { heading = 'Name', key = 'displayName' }, - { heading = 'Priority', key = 'priority', width = 5 }, - { heading = 'Type', key = 'mtype', width = 5 }, + { heading = 'Type', key = 'mtype', width = 5 }, }, sortColumn = 'displayName', + help = 'Select Machine', + }, + remove = UI.Button { + y = -1, x = -4, + text = '-', event = 'remove_machine', + help = 'Remove Machine', }, statusBar = UI.StatusBar { - values = 'Select Machine', + ex = -7, + backgroundColor = colors.cyan, }, } @@ -80,6 +101,19 @@ end function machinesPage:eventHandler(event) if event.type == 'grid_select' then UI:setPage('machineWizard', event.selected) + + elseif event.type == 'remove_machine' then + local machine = self.grid:getSelected() + if machine then + context.config.remoteDefaults[machine.name] = nil + saveConfig() + end + self.grid:update() + self.grid:draw() + + elseif event.type == 'focus_change' then + self.statusBar:setStatus(event.focused.help) + else UI.Page.eventHandler(self, event) end @@ -200,10 +234,11 @@ function machineWizard:enable(machine) local adapter = machine.adapter machine.adapter = nil -- don't deep copy the adapter self.machine = Util.deepCopy(machine) + self.machine.adapter = adapter machine.adapter = adapter self.wizard.pages.general.form:setValues(self.machine) - self.wizard.pages.general.form[1].shadowText = machine.name + self.wizard.pages.general.form[1].shadowText = self.machine.name -- restore indices for _, page in pairs(self.wizard.pages) do @@ -227,50 +262,27 @@ function machineWizard:eventHandler(event) UI:setPreviousPage() elseif event.type == 'accept' then - -_G._p2 = self.machine -debug(1) - -- todo: no need for calling this function - use validate instead for _, v in pairs(self.wizard.pages) do if v.save and v.index then -- only save if the page was valid for this mtype v:save(self.machine) end end -debug(2) - local adapter = self.machine.adapter - self.machine.adapter = nil + Util.prune(self.machine, function(v) + if type(v) == 'boolean' then + return v + elseif type(v) == 'string' then + return #v > 0 + elseif type(v) == 'table' then + return not Util.empty(v) + end + return true + end) -local t = { } -for k,v in pairs(context.config.remoteDefaults) do - t[k] = v.adapter - v.adapter = nil -end - - context.config.remoteDefaults[self.machine.name] = - Util.prune(self.machine, function(v) - if type(v) == 'boolean' then - return v - elseif type(v) == 'string' then - return #v > 0 - elseif type(v) == 'table' then - return not Util.empty(v) - end - return true - end) - Config.update('milo', context.config) - -for k,v in pairs(t) do - context.config.remoteDefaults[k].adapter = v -end - -debug(3) Util.clear(context.config.remoteDefaults[self.machine.name]) -debug(4) Util.merge(context.config.remoteDefaults[self.machine.name], self.machine) -debug(5) - context.config.remoteDefaults[self.machine.name].adapter = adapter + saveConfig() UI:setPreviousPage() diff --git a/milo/plugins/listing.lua b/milo/plugins/listing.lua index f4b9023..6a18aef 100644 --- a/milo/plugins/listing.lua +++ b/milo/plugins/listing.lua @@ -1,5 +1,6 @@ local Craft = require('turtle.craft') local itemDB = require('itemDB') +local Event = require('event') local Milo = require('milo') local UI = require('ui') local Util = require('util') @@ -59,7 +60,7 @@ local listingPage = UI.Page { }, statusBar = UI.StatusBar { filter = UI.TextEntry { - x = 1, ex = -4, + x = 1, ex = -13, limit = 50, shadowText = 'filter', shadowTextColor = colors.gray, @@ -69,6 +70,13 @@ local listingPage = UI.Page { [ 'enter' ] = 'craft', }, }, + storageStatus = UI.Button { + x = -12, ex = -4, + event = 'toggle_online', + backgroundColor = colors.cyan, + textColor = colors.lime, + text = '', + }, display = UI.Button { x = -3, event = 'toggle_display', @@ -130,7 +138,7 @@ function listingPage:eventHandler(event) UI:exitPullEvents() elseif event.type == 'resume' then - Milo:resumeCrafting() + context.storage:setOnline(true) elseif event.type == 'eject' then local item = self.grid:getSelected() @@ -241,4 +249,13 @@ function listingPage:applyFilter() self.grid:setValues(t) end +Event.on({ 'storage_offline', 'storage_online' }, function(e, isOnline) + listingPage.statusBar.storageStatus.text = + isOnline and 'online' or 'offline' + listingPage.statusBar.storageStatus.textColor = + isOnline and colors.lime or colors.red + listingPage.statusBar.storageStatus:draw() + listingPage:sync() +end) + UI:addPage('listing', listingPage)