This commit is contained in:
kepler155c
2018-10-26 23:08:44 -04:00
parent 87da95ca05
commit d227d6ebb7
5 changed files with 94 additions and 90 deletions

View File

@@ -67,6 +67,7 @@ local Util = require('util')
local fs = _G.fs local fs = _G.fs
local multishell = _ENV.multishell local multishell = _ENV.multishell
local os = _G.os
local shell = _ENV.shell local shell = _ENV.shell
if multishell then if multishell then
@@ -113,7 +114,6 @@ end)
Milo:init(context) Milo:init(context)
context.storage:initStorage() context.storage:initStorage()
context.storage:initTrashcan()
local function loadDirectory(dir) local function loadDirectory(dir)
for _, file in pairs(fs.list(dir)) do for _, file in pairs(fs.list(dir)) do
@@ -157,4 +157,8 @@ Event.onInterval(5, function()
end end
end) end)
os.queueEvent(
context.storage:isOnline() and 'storage_online' or 'storage_offline',
context.storage:isOnline())
UI:pullEvents() UI:pullEvents()

View File

@@ -116,7 +116,7 @@ end
function Milo:clearGrid() function Milo:clearGrid()
local function clear() local function clear()
turtle.eachFilledSlot(function(slot) 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) end)
for i = 1, 16 do for i = 1, 16 do

View File

@@ -24,32 +24,20 @@ listCount = 0,
self.localName = modem.getNameLocal() self.localName = modem.getNameLocal()
Event.on({ 'device_attach' }, function(_, dev) Event.on({ 'device_attach' }, function(_, dev)
if self.remoteDefaults[dev] then debug('attach: ' .. dev)
if self.remoteDefaults[dev].mtype == 'storage' then self:initStorage()
self:initStorage()
end
if self.remoteDefaults[dev].mtype == 'trashcan' then
self:initTrashcan()
end
end
end) end)
Event.on({ 'device_detach' }, function(_, dev) Event.on({ 'device_detach' }, function(_, dev)
if self.remoteDefaults[dev] then debug('detach: ' .. dev)
if self.remoteDefaults[dev].mtype == 'storage' then self:initStorage(dev)
self:initStorage(dev)
end
if self.remoteDefaults[dev].mtype == 'trashcan' then
self:initTrashcan(dev)
end
end
end) end)
end end
function NetworkedAdapter:setOnline(online) function NetworkedAdapter:setOnline(online)
if online ~= self.storageOnline then if online ~= self.storageOnline then
self.storageOnline = online 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') debug('Storage: %s', self.storageOnline and 'online' or 'offline')
end end
end end
@@ -58,22 +46,6 @@ function NetworkedAdapter:isOnline()
return self.storageOnline return self.storageOnline
end 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() function NetworkedAdapter:initStorage()
local online = true local online = true
@@ -81,15 +53,13 @@ function NetworkedAdapter:initStorage()
for k,v in pairs(self.remoteDefaults) do for k,v in pairs(self.remoteDefaults) do
if v.adapter then if v.adapter then
v.adapter.online = not not device[k] v.adapter.online = not not device[k]
if v.mtype == 'storage' then elseif device[k] and device[k].list and device[k].size and device[k].pullItems then
online = online and v.adapter.online
end
elseif v.mtype == 'storage' then
v.adapter = InventoryAdapter.wrap({ side = k }) v.adapter = InventoryAdapter.wrap({ side = k })
v.adapter.online = true v.adapter.online = true
end end
if v.mtype == 'storage' then 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
end end
@@ -184,11 +154,11 @@ function NetworkedAdapter:provide(item, qty, slot, direction)
local total = 0 local total = 0
for _, adapter in self:onlineAdapters() do 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 if amount > 0 then
--debug('EXT: %s(%d): %s -> %s%s', debug('EXT: %s(%d): %s -> %s%s',
-- item.name, amount, remote.side, direction or self.localName, item.name, amount, adapter.name, direction or self.localName,
-- slot and string.format('[%d]', slot) or '') slot and string.format('[%d]', slot) or '')
self.dirty = true self.dirty = true
adapter.dirty = true adapter.dirty = true
end end
@@ -203,9 +173,10 @@ function NetworkedAdapter:provide(item, qty, slot, direction)
end end
function NetworkedAdapter:trash(source, slot, count) function NetworkedAdapter:trash(source, slot, count)
if self.trashcan then local trashcan = Util.find(self.remoteDefaults, 'mtype', 'trashcan')
debug('TRA: %s[%d] (%d)', source, slot, count) if trashcan and trashcan.adapter and trashcan.adapter.online then
return self.trashcan.pullItems(source, slot, count) debug('TRA: %s[%d] (%d)', source or self.localName, slot, count or 64)
return trashcan.adapter.pullItems(source or self.localName, slot, count)
end end
return 0 return 0
end end
@@ -233,7 +204,7 @@ function NetworkedAdapter:insert(slot, qty, toSlot, item, source)
if amount > 0 then if amount > 0 then
debug('INS: %s(%d): %s[%d] -> %s', debug('INS: %s(%d): %s[%d] -> %s',
item.name, amount, item.name, amount,
source or self.localName, slot, adapter.side) source, slot, adapter.name)
self.dirty = true self.dirty = true
adapter.dirty = true adapter.dirty = true
local entry = self.activity[key] or 0 local entry = self.activity[key] or 0
@@ -261,7 +232,7 @@ debug('INS: %s(%d): %s[%d] -> %s',
if qty <= 0 then if qty <= 0 then
break break
end 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) insert(adapter)
end end
end end
@@ -272,7 +243,7 @@ debug('INS: %s(%d): %s[%d] -> %s',
if qty <= 0 then if qty <= 0 then
break break
end end
if not remote.lockWith then if not remote.lock then
insert(remote.adapter) insert(remote.adapter)
end end
end end

View File

@@ -10,6 +10,20 @@ local device = _G.device
local context = Milo:getContext() 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 { local machinesPage = UI.Page {
titleBar = UI.TitleBar { titleBar = UI.TitleBar {
previousPage = true, previousPage = true,
@@ -19,14 +33,21 @@ local machinesPage = UI.Page {
y = 2, ey = -2, y = 2, ey = -2,
values = context.config.remoteDefaults, values = context.config.remoteDefaults,
columns = { columns = {
{ key = 'priority', width = 3 },
{ heading = 'Name', key = 'displayName' }, { heading = 'Name', key = 'displayName' },
{ heading = 'Priority', key = 'priority', width = 5 }, { heading = 'Type', key = 'mtype', width = 5 },
{ heading = 'Type', key = 'mtype', width = 5 },
}, },
sortColumn = 'displayName', sortColumn = 'displayName',
help = 'Select Machine',
},
remove = UI.Button {
y = -1, x = -4,
text = '-', event = 'remove_machine',
help = 'Remove Machine',
}, },
statusBar = UI.StatusBar { statusBar = UI.StatusBar {
values = 'Select Machine', ex = -7,
backgroundColor = colors.cyan,
}, },
} }
@@ -80,6 +101,19 @@ end
function machinesPage:eventHandler(event) function machinesPage:eventHandler(event)
if event.type == 'grid_select' then if event.type == 'grid_select' then
UI:setPage('machineWizard', event.selected) 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 else
UI.Page.eventHandler(self, event) UI.Page.eventHandler(self, event)
end end
@@ -200,10 +234,11 @@ function machineWizard:enable(machine)
local adapter = machine.adapter local adapter = machine.adapter
machine.adapter = nil -- don't deep copy the adapter machine.adapter = nil -- don't deep copy the adapter
self.machine = Util.deepCopy(machine) self.machine = Util.deepCopy(machine)
self.machine.adapter = adapter
machine.adapter = adapter machine.adapter = adapter
self.wizard.pages.general.form:setValues(self.machine) 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 -- restore indices
for _, page in pairs(self.wizard.pages) do for _, page in pairs(self.wizard.pages) do
@@ -227,50 +262,27 @@ function machineWizard:eventHandler(event)
UI:setPreviousPage() UI:setPreviousPage()
elseif event.type == 'accept' then elseif event.type == 'accept' then
_G._p2 = self.machine
debug(1)
-- todo: no need for calling this function - use validate instead -- todo: no need for calling this function - use validate instead
for _, v in pairs(self.wizard.pages) do 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 if v.save and v.index then -- only save if the page was valid for this mtype
v:save(self.machine) v:save(self.machine)
end end
end end
debug(2)
local adapter = self.machine.adapter Util.prune(self.machine, function(v)
self.machine.adapter = nil 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]) Util.clear(context.config.remoteDefaults[self.machine.name])
debug(4)
Util.merge(context.config.remoteDefaults[self.machine.name], self.machine) Util.merge(context.config.remoteDefaults[self.machine.name], self.machine)
debug(5) saveConfig()
context.config.remoteDefaults[self.machine.name].adapter = adapter
UI:setPreviousPage() UI:setPreviousPage()

View File

@@ -1,5 +1,6 @@
local Craft = require('turtle.craft') local Craft = require('turtle.craft')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Event = require('event')
local Milo = require('milo') local Milo = require('milo')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
@@ -59,7 +60,7 @@ local listingPage = UI.Page {
}, },
statusBar = UI.StatusBar { statusBar = UI.StatusBar {
filter = UI.TextEntry { filter = UI.TextEntry {
x = 1, ex = -4, x = 1, ex = -13,
limit = 50, limit = 50,
shadowText = 'filter', shadowText = 'filter',
shadowTextColor = colors.gray, shadowTextColor = colors.gray,
@@ -69,6 +70,13 @@ local listingPage = UI.Page {
[ 'enter' ] = 'craft', [ 'enter' ] = 'craft',
}, },
}, },
storageStatus = UI.Button {
x = -12, ex = -4,
event = 'toggle_online',
backgroundColor = colors.cyan,
textColor = colors.lime,
text = '',
},
display = UI.Button { display = UI.Button {
x = -3, x = -3,
event = 'toggle_display', event = 'toggle_display',
@@ -130,7 +138,7 @@ function listingPage:eventHandler(event)
UI:exitPullEvents() UI:exitPullEvents()
elseif event.type == 'resume' then elseif event.type == 'resume' then
Milo:resumeCrafting() context.storage:setOnline(true)
elseif event.type == 'eject' then elseif event.type == 'eject' then
local item = self.grid:getSelected() local item = self.grid:getSelected()
@@ -241,4 +249,13 @@ function listingPage:applyFilter()
self.grid:setValues(t) self.grid:setValues(t)
end 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) UI:addPage('listing', listingPage)