mirror of
https://github.com/kepler155c/opus
synced 2025-10-18 01:07:40 +00:00
phase out peripheral.lookup
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
local Event = require('event')
|
||||
local Socket = require('socket')
|
||||
local Util = require('util')
|
||||
|
||||
local os = _G.os
|
||||
|
||||
local Peripheral = Util.shallowCopy(_G.peripheral)
|
||||
|
||||
function Peripheral.getList()
|
||||
@@ -121,122 +117,4 @@ function Peripheral.get(args)
|
||||
end
|
||||
end
|
||||
|
||||
local function getProxy(pi)
|
||||
local socket, msg = Socket.connect(pi.host, 189)
|
||||
|
||||
if not socket then
|
||||
error("Timed out attaching peripheral: " .. pi.uri .. '\n' .. msg)
|
||||
end
|
||||
|
||||
-- write the uri of the periperal we are requesting...
|
||||
-- ie. type/monitor
|
||||
socket:write(pi.path)
|
||||
local proxy = socket:read(3)
|
||||
|
||||
if not proxy then
|
||||
error("Timed out attaching peripheral: " .. pi.uri)
|
||||
end
|
||||
|
||||
if type(proxy) == 'string' then
|
||||
error(proxy)
|
||||
end
|
||||
|
||||
local methods = proxy.methods
|
||||
proxy.methods = nil
|
||||
|
||||
for _,method in pairs(methods) do
|
||||
proxy[method] = function(...)
|
||||
socket:write({ fn = method, args = { ... } })
|
||||
local resp = socket:read()
|
||||
if not resp then
|
||||
error("Timed out communicating with peripheral: " .. pi.uri)
|
||||
end
|
||||
return table.unpack(resp)
|
||||
end
|
||||
end
|
||||
|
||||
if proxy.blit then
|
||||
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
|
||||
'setTextColor', 'setTextColour', 'setBackgroundColor',
|
||||
'setBackgroundColour', 'scroll', 'setCursorBlink', }
|
||||
local queue = nil
|
||||
|
||||
for _,method in pairs(methods) do
|
||||
proxy[method] = function(...)
|
||||
if not queue then
|
||||
queue = { }
|
||||
Event.onTimeout(0, function()
|
||||
if not socket:write({ fn = 'fastBlit', args = { queue } }) then
|
||||
error("Timed out communicating with peripheral: " .. pi.uri)
|
||||
end
|
||||
queue = nil
|
||||
socket:read()
|
||||
end)
|
||||
end
|
||||
if not socket.connected then
|
||||
error("Timed out communicating with peripheral: " .. pi.uri)
|
||||
end
|
||||
|
||||
table.insert(queue, {
|
||||
fn = method,
|
||||
args = { ... },
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if proxy.type == 'monitor' then
|
||||
Event.addRoutine(function()
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
break
|
||||
end
|
||||
if data.fn and data.fn == 'event' then
|
||||
os.queueEvent(table.unpack(data.data))
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return proxy
|
||||
end
|
||||
|
||||
--[[
|
||||
Parse a uri into it's components
|
||||
|
||||
Examples:
|
||||
monitor = { name = 'monitor' }
|
||||
side/top = { side = 'top' }
|
||||
method/list = { method = 'list' }
|
||||
12://name/monitor = { host = 12, name = 'monitor' }
|
||||
]]--
|
||||
local function parse(uri)
|
||||
local pi = Util.split(uri:gsub('^%d*://', ''), '(.-)/')
|
||||
|
||||
if #pi == 1 then
|
||||
pi = {
|
||||
'name',
|
||||
pi[1],
|
||||
}
|
||||
end
|
||||
|
||||
return {
|
||||
host = uri:match('^(%d*)%:'), -- 12
|
||||
uri = uri, -- 12://name/monitor
|
||||
path = uri:gsub('^%d*://', ''), -- name/monitor
|
||||
[ pi[1] ] = pi[2], -- name = 'monitor'
|
||||
}
|
||||
end
|
||||
|
||||
function Peripheral.lookup(uri)
|
||||
local pi = parse(uri)
|
||||
|
||||
if pi.host and _G.device.wireless_modem then
|
||||
return getProxy(pi)
|
||||
end
|
||||
|
||||
return Peripheral.get(pi)
|
||||
end
|
||||
|
||||
return Peripheral
|
||||
|
32
sys/apis/proxy.lua
Normal file
32
sys/apis/proxy.lua
Normal file
@@ -0,0 +1,32 @@
|
||||
local Socket = require('socket')
|
||||
|
||||
local Proxy = { }
|
||||
|
||||
function Proxy.create(remoteId, uri)
|
||||
local socket, msg = Socket.connect(remoteId, 188)
|
||||
|
||||
if not socket then
|
||||
error(msg)
|
||||
end
|
||||
|
||||
socket.co = coroutine.running()
|
||||
|
||||
socket:write(uri)
|
||||
local methods = socket:read() or error('Timed out')
|
||||
|
||||
local hijack = { }
|
||||
for _,method in pairs(methods) do
|
||||
hijack[method] = function(...)
|
||||
socket:write({ method, ... })
|
||||
local resp = socket:read()
|
||||
if not resp then
|
||||
error('timed out: ' .. method)
|
||||
end
|
||||
return table.unpack(resp)
|
||||
end
|
||||
end
|
||||
|
||||
return hijack, socket
|
||||
end
|
||||
|
||||
return Proxy
|
@@ -2,7 +2,6 @@ local Canvas = require('ui.canvas')
|
||||
local class = require('class')
|
||||
local Event = require('event')
|
||||
local Input = require('input')
|
||||
local Peripheral = require('peripheral')
|
||||
local Transition = require('ui.transition')
|
||||
local Util = require('util')
|
||||
|
||||
@@ -12,6 +11,7 @@ local colors = _G.colors
|
||||
local device = _G.device
|
||||
local fs = _G.fs
|
||||
local os = _G.os
|
||||
local peripheral = _G.peripheral
|
||||
local term = _G.term
|
||||
|
||||
--[[
|
||||
@@ -178,13 +178,12 @@ function Manager:configure(appName, ...)
|
||||
Util.merge(defaults.device, optionValues)
|
||||
|
||||
if defaults.device.name then
|
||||
|
||||
local dev
|
||||
|
||||
if defaults.device.name == 'terminal' then
|
||||
dev = term.current()
|
||||
else
|
||||
dev = Peripheral.lookup(defaults.device.name) --- device[defaults.device.name]
|
||||
dev = peripheral.wrap(defaults.device.name)
|
||||
end
|
||||
|
||||
if not dev then
|
||||
|
Reference in New Issue
Block a user