2017-09-05 06:09:31 +00:00
|
|
|
local Util = require('util')
|
|
|
|
|
2017-10-08 21:45:01 +00:00
|
|
|
local Peripheral = Util.shallowCopy(_G.peripheral)
|
2017-04-01 23:21:49 +00:00
|
|
|
|
2017-10-08 21:45:01 +00:00
|
|
|
function Peripheral.getList()
|
2017-04-01 23:21:49 +00:00
|
|
|
if _G.device then
|
|
|
|
return _G.device
|
|
|
|
end
|
|
|
|
|
|
|
|
local deviceList = { }
|
2017-10-08 21:45:01 +00:00
|
|
|
for _,side in pairs(Peripheral.getNames()) do
|
2017-04-01 23:21:49 +00:00
|
|
|
Peripheral.addDevice(deviceList, side)
|
|
|
|
end
|
|
|
|
|
|
|
|
return deviceList
|
|
|
|
end
|
|
|
|
|
|
|
|
function Peripheral.addDevice(deviceList, side)
|
2016-12-11 19:24:52 +00:00
|
|
|
local name = side
|
2017-10-08 21:45:01 +00:00
|
|
|
local ptype = Peripheral.getType(side)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
if not ptype then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if ptype == 'modem' then
|
2017-10-08 21:45:01 +00:00
|
|
|
if Peripheral.call(name, 'isWireless') then
|
2016-12-11 19:24:52 +00:00
|
|
|
ptype = 'wireless_modem'
|
|
|
|
else
|
|
|
|
ptype = 'wired_modem'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local sides = {
|
|
|
|
front = true,
|
|
|
|
back = true,
|
|
|
|
top = true,
|
|
|
|
bottom = true,
|
|
|
|
left = true,
|
|
|
|
right = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if sides[name] then
|
|
|
|
local i = 1
|
|
|
|
local uniqueName = ptype
|
2017-04-01 23:21:49 +00:00
|
|
|
while deviceList[uniqueName] do
|
2016-12-11 19:24:52 +00:00
|
|
|
uniqueName = ptype .. '_' .. i
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
name = uniqueName
|
|
|
|
end
|
|
|
|
|
2017-10-08 21:45:01 +00:00
|
|
|
local s, m = pcall(function() deviceList[name] = Peripheral.wrap(side) end)
|
2017-07-24 02:37:07 +00:00
|
|
|
if not s and m then
|
2017-10-08 21:45:01 +00:00
|
|
|
_G.printError('wrap failed')
|
|
|
|
_G.printError(m)
|
2017-07-24 02:37:07 +00:00
|
|
|
end
|
|
|
|
|
2017-06-23 06:04:56 +00:00
|
|
|
if deviceList[name] then
|
|
|
|
Util.merge(deviceList[name], {
|
|
|
|
name = name,
|
|
|
|
type = ptype,
|
|
|
|
side = side,
|
|
|
|
})
|
|
|
|
|
|
|
|
return deviceList[name]
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Peripheral.getBySide(side)
|
2017-10-08 21:45:01 +00:00
|
|
|
return Util.find(Peripheral.getList(), 'side', side)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Peripheral.getByType(typeName)
|
2017-10-08 21:45:01 +00:00
|
|
|
return Util.find(Peripheral.getList(), 'type', typeName)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Peripheral.getByMethod(method)
|
2017-10-08 21:45:01 +00:00
|
|
|
for _,p in pairs(Peripheral.getList()) do
|
2016-12-11 19:24:52 +00:00
|
|
|
if p[method] then
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- match any of the passed arguments
|
|
|
|
function Peripheral.get(args)
|
|
|
|
|
|
|
|
if type(args) == 'string' then
|
|
|
|
args = { type = args }
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.type then
|
|
|
|
local p = Peripheral.getByType(args.type)
|
|
|
|
if p then
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.method then
|
|
|
|
local p = Peripheral.getByMethod(args.method)
|
|
|
|
if p then
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.side then
|
|
|
|
local p = Peripheral.getBySide(args.side)
|
|
|
|
if p then
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Peripheral
|