From 6ce4039e8ad6881dff157ad786a29e757b9ae970 Mon Sep 17 00:00:00 2001 From: kepler155c Date: Thu, 25 Oct 2018 00:18:38 -0400 Subject: [PATCH] milo wip --- milo/Milo.lua | 5 +++ milo/MiloMonitor.lua | 62 +++++++++++++++++++++++++++++++ milo/MiloRemote.lua | 26 ++++++++++++- milo/plugins/autocraftTask.lua | 1 + milo/plugins/demandCraft.lua | 3 +- milo/plugins/exportTask.lua | 3 +- milo/plugins/importTask.lua | 3 +- milo/plugins/inputChestTask.lua | 3 +- milo/plugins/jobList.lua | 1 + milo/plugins/limitTask.lua | 3 +- milo/plugins/potionImportTask.lua | 3 +- milo/plugins/remote.lua | 24 ++++++++++-- milo/plugins/replenishTask.lua | 3 +- 13 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 milo/MiloMonitor.lua diff --git a/milo/Milo.lua b/milo/Milo.lua index a6478a4..8e26af9 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -184,6 +184,11 @@ table.sort(Milo.tasks, function(a, b) return a.priority < b.priority end) +debug('Tasks\n-----') +for _, task in ipairs(Milo.tasks) do + debug('%d: %s', task.priority, task.name) +end + Milo:clearGrid() local page = UI:getPage('listing') diff --git a/milo/MiloMonitor.lua b/milo/MiloMonitor.lua new file mode 100644 index 0000000..c36d4f2 --- /dev/null +++ b/milo/MiloMonitor.lua @@ -0,0 +1,62 @@ +_G.requireInjector(_ENV) + +local Util = require('util') + +local colors = _G.colors +local device = _G.device + +local args = { ... } +local mon = device[args[1] or 'monitor'] or error('Syntax: debug ') +local config = Util.readTable('/usr/config/milo') or error('Milo is not configured') + +local row +local monWidth, monHeight = mon.getSize() +local machines = { } + +local function write(x, y, s, bg, fg) + mon.setCursorPos(x, y) + mon.setBackgroundColor(bg) + if fg then + mon.setTextColor(fg) + end + mon.write(s) +end + +local function progress(y, percent) + local width = math.ceil(percent / 100 * monWidth) + write(2, y, string.rep(' ', monWidth - 2), colors.gray) + write(2, y, string.rep(' ', width), colors.lime) +end + +local function draw(machine, percent) + write(2, row, machine.displayName or machine.name, colors.black, colors.yellow) + progress(row + 1, percent) + row = row + 3 +end + +local function redraw() + row = 1 + mon.setBackgroundColor(colors.black) + mon.clear() + for _,machine in ipairs(machines) do + local dev = device[machine.name] + if dev then + local percent = 50 + if machine.mtype == 'storage' then + percent = Util.size(dev.list()) / dev.size() * 100 + end + draw(machine, percent) + end + end +end + +for _, v in pairs(config.remoteDefaults) do + table.insert(machines, v) +end + +table.sort(machines, function(a, b) + return (a.displayName or a.name) < (b.displayName or b.name) +end) + +mon.setTextScale(.5) +redraw() diff --git a/milo/MiloRemote.lua b/milo/MiloRemote.lua index a0a555e..cd9403d 100644 --- a/milo/MiloRemote.lua +++ b/milo/MiloRemote.lua @@ -7,6 +7,27 @@ local Util = require('util') local colors = _G.colors local socket +local options = { + user = { arg = 'u', type = 'string', + desc = 'User name associated with bound manipulator' }, + server = { arg = 's', type = 'number', + desc = 'ID of Milo server' }, + help = { arg = 'h', type = 'flag', value = false, + desc = 'Displays the options' }, +} + +local args = { ... } +if not Util.getOptions(options, args) then + print() + error('Invalid arguments') +end + +if not options.user.value or not options.server.value then + Util.showOptions(options) + print() + error('Invalid arguments') +end + local page = UI.Page { menuBar = UI.MenuBar { buttons = { @@ -90,7 +111,10 @@ function page:sendRequest(data) for _ = 1, 2 do if not socket or not socket.connected then - socket, msg = Socket.connect(1, 4242) + socket, msg = Socket.connect(options.server.value, 4242) + if socket then + socket:write(options.user.value) + end end if socket then if socket:write(data) then diff --git a/milo/plugins/autocraftTask.lua b/milo/plugins/autocraftTask.lua index caf83a5..fd59616 100644 --- a/milo/plugins/autocraftTask.lua +++ b/milo/plugins/autocraftTask.lua @@ -2,6 +2,7 @@ local Milo = require('milo') local Util = require('util') local Autocraft = { + name = 'autocraft', priority = 100, } diff --git a/milo/plugins/demandCraft.lua b/milo/plugins/demandCraft.lua index c30f2cf..fbb5d7c 100644 --- a/milo/plugins/demandCraft.lua +++ b/milo/plugins/demandCraft.lua @@ -132,7 +132,8 @@ function craftPage:eventHandler(event) end local demandCraftingTask = { - priority = 20, + name = 'demand crafting', + priority = 60, } function demandCraftingTask:cycle(context) diff --git a/milo/plugins/exportTask.lua b/milo/plugins/exportTask.lua index 4e25ee9..a655ad6 100644 --- a/milo/plugins/exportTask.lua +++ b/milo/plugins/exportTask.lua @@ -4,7 +4,8 @@ local Milo = require('milo') local device = _G.device local ExportTask = { - priority = 5, + name = 'exporter', + priority = 40, } function ExportTask:cycle(context) diff --git a/milo/plugins/importTask.lua b/milo/plugins/importTask.lua index 9fd9bda..0bcccf2 100644 --- a/milo/plugins/importTask.lua +++ b/milo/plugins/importTask.lua @@ -3,7 +3,8 @@ local Milo = require('milo') local device = _G.device local ImportTask = { - priority = 3, + name = 'importer', + priority = 20, } function ImportTask:cycle(context) diff --git a/milo/plugins/inputChestTask.lua b/milo/plugins/inputChestTask.lua index fb6072b..529f6f9 100644 --- a/milo/plugins/inputChestTask.lua +++ b/milo/plugins/inputChestTask.lua @@ -3,7 +3,8 @@ local Milo = require('milo') local device = _G.device local InputChest = { - priority = 1, + name = 'input', + priority = 10, } function InputChest:cycle(context) diff --git a/milo/plugins/jobList.lua b/milo/plugins/jobList.lua index 4b233e8..6e0f82b 100644 --- a/milo/plugins/jobList.lua +++ b/milo/plugins/jobList.lua @@ -56,6 +56,7 @@ jobList:draw() jobList:sync() local JobListTask = { + name = 'job status', priority = 80, } diff --git a/milo/plugins/limitTask.lua b/milo/plugins/limitTask.lua index a9e123b..f86c317 100644 --- a/milo/plugins/limitTask.lua +++ b/milo/plugins/limitTask.lua @@ -1,7 +1,8 @@ local Milo = require('milo') local LimitTask = { - priority = 10, + name = 'limiter', + priority = 50, } function LimitTask:cycle(context) diff --git a/milo/plugins/potionImportTask.lua b/milo/plugins/potionImportTask.lua index eb0db7b..ba42cae 100644 --- a/milo/plugins/potionImportTask.lua +++ b/milo/plugins/potionImportTask.lua @@ -3,7 +3,8 @@ local Milo = require('milo') local device = _G.device local PotionImportTask = { - priority = 3, + name = 'potions', + priority = 30, } function PotionImportTask:cycle(context) diff --git a/milo/plugins/remote.lua b/milo/plugins/remote.lua index e4bfcf5..b0b0088 100644 --- a/milo/plugins/remote.lua +++ b/milo/plugins/remote.lua @@ -2,15 +2,32 @@ local Event = require('event') local Milo = require('milo') local Socket = require('socket') -local device = _G.device -local manipulator = device.manipulator_1 -local turtle = _G.turtle +local device = _G.device +local turtle = _G.turtle local context = Milo:getContext() +local function getManipulatorForUser(user) + for _,v in pairs(device) do + if v.type == 'manipulator' and v.getName and v.getName() == user then + return v + end + end +end + local function client(socket) debug('connection from ' .. socket.dhost) + local user = socket:read(2) + if not user then + return + end + + local manipulator = getManipulatorForUser(user) + if not manipulator then + return + end + repeat local data = socket:read() if not data then @@ -51,6 +68,7 @@ if device.wireless_modem then local socket = Socket.server(4242) Event.addRoutine(function() client(socket) + socket:close() end) end end) diff --git a/milo/plugins/replenishTask.lua b/milo/plugins/replenishTask.lua index dc5616a..729a83c 100644 --- a/milo/plugins/replenishTask.lua +++ b/milo/plugins/replenishTask.lua @@ -2,7 +2,8 @@ local itemDB = require('itemDB') local Milo = require('milo') local ReplenishTask = { - priority = 30, + name = 'replenish', + priority = 70, } function ReplenishTask:cycle(context)