milo wip
This commit is contained in:
@@ -5,11 +5,7 @@ local UI = require('ui')
|
|||||||
local Util = require('util')
|
local Util = require('util')
|
||||||
|
|
||||||
local colors = _G.colors
|
local colors = _G.colors
|
||||||
|
local socket
|
||||||
local socket, msg = Socket.connect(1, 4242)
|
|
||||||
if not socket then
|
|
||||||
error(msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
local page = UI.Page {
|
local page = UI.Page {
|
||||||
menuBar = UI.MenuBar {
|
menuBar = UI.MenuBar {
|
||||||
@@ -89,6 +85,26 @@ local function filterItems(t, filter, displayMode)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function page:sendRequest(data)
|
||||||
|
local msg
|
||||||
|
|
||||||
|
for _ = 1, 2 do
|
||||||
|
if not socket or not socket.connected then
|
||||||
|
socket, msg = Socket.connect(1, 4242)
|
||||||
|
end
|
||||||
|
if socket then
|
||||||
|
if socket:write(data) then
|
||||||
|
local response = socket:read(2)
|
||||||
|
if response then
|
||||||
|
return response
|
||||||
|
end
|
||||||
|
end
|
||||||
|
socket:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self.notification:error(msg or 'Failed to connect')
|
||||||
|
end
|
||||||
|
|
||||||
function page.statusBar:draw()
|
function page.statusBar:draw()
|
||||||
return UI.Window.draw(self)
|
return UI.Window.draw(self)
|
||||||
end
|
end
|
||||||
@@ -122,13 +138,23 @@ function page:eventHandler(event)
|
|||||||
elseif event.type == 'eject' then
|
elseif event.type == 'eject' then
|
||||||
local item = self.grid:getSelected()
|
local item = self.grid:getSelected()
|
||||||
if item then
|
if item then
|
||||||
socket:write({ request = 'transfer', item = item, count = 1 })
|
local items = self:sendRequest({ request = 'transfer', item = item, count = 1 })
|
||||||
|
if items then
|
||||||
|
self.items = items
|
||||||
|
self:applyFilter()
|
||||||
|
self.grid:draw()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif event.type == 'eject_stack' then
|
elseif event.type == 'eject_stack' then
|
||||||
local item = self.grid:getSelected()
|
local item = self.grid:getSelected()
|
||||||
if item then
|
if item then
|
||||||
socket:write({ request = 'transfer', item = item, count = 64 })
|
local items = self:sendRequest({ request = 'transfer', item = item, count = 64 })
|
||||||
|
if items then
|
||||||
|
self.items = items
|
||||||
|
self:applyFilter()
|
||||||
|
self.grid:draw()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif event.type == 'refresh' then
|
elseif event.type == 'refresh' then
|
||||||
@@ -172,12 +198,10 @@ function page:enable()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function page:refresh()
|
function page:refresh()
|
||||||
socket:write({ request = 'list' })
|
local items = self:sendRequest({ request = 'list' })
|
||||||
self.items = socket:read()
|
|
||||||
|
|
||||||
if not self.items then
|
if items then
|
||||||
UI:exitPullEvents()
|
self.items = items
|
||||||
else
|
|
||||||
self:applyFilter()
|
self:applyFilter()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -190,4 +214,6 @@ end
|
|||||||
UI:setPage(page)
|
UI:setPage(page)
|
||||||
UI:pullEvents()
|
UI:pullEvents()
|
||||||
|
|
||||||
socket:close()
|
if socket then
|
||||||
|
socket:close()
|
||||||
|
end
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ local turtle = _G.turtle
|
|||||||
local context = Milo:getContext()
|
local context = Milo:getContext()
|
||||||
|
|
||||||
local function client(socket)
|
local function client(socket)
|
||||||
|
debug('connection from ' .. socket.dhost)
|
||||||
|
|
||||||
repeat
|
repeat
|
||||||
local data = socket:read()
|
local data = socket:read()
|
||||||
if not data then
|
if not data then
|
||||||
@@ -32,8 +34,14 @@ local function client(socket)
|
|||||||
slot.index,
|
slot.index,
|
||||||
slot.count)
|
slot.count)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local items = Milo:listItems()
|
||||||
|
Milo:mergeResources(items)
|
||||||
|
socket:write(items)
|
||||||
end
|
end
|
||||||
until not socket.connected
|
until not socket.connected
|
||||||
|
|
||||||
|
debug('disconnected from ' .. socket.dhost)
|
||||||
end
|
end
|
||||||
|
|
||||||
if device.wireless_modem then
|
if device.wireless_modem then
|
||||||
@@ -41,9 +49,6 @@ if device.wireless_modem then
|
|||||||
debug('Milo: listening on port 4242')
|
debug('Milo: listening on port 4242')
|
||||||
while true do
|
while true do
|
||||||
local socket = Socket.server(4242)
|
local socket = Socket.server(4242)
|
||||||
|
|
||||||
debug('connection from ' .. socket.dhost)
|
|
||||||
|
|
||||||
Event.addRoutine(function()
|
Event.addRoutine(function()
|
||||||
client(socket)
|
client(socket)
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user