blunty666

remotePeripheralClient

May 8th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. local REMOTE_PERIPHERAL_PROTOCOL = "REMOTE_PERIPHERAL"
  2.  
  3. local remotePeripheralClientMethods = {
  4.     GetAllData = function(self, peripheralName)
  5.         return self.list[peripheralName] or false
  6.     end,
  7.     GetMainData = function(self, peripheralName)
  8.         local allData = self:GetAllData(peripheralName)
  9.         return (allData and allData.main) or false
  10.     end,
  11.     GetSourceData = function(self, peripheralName, sourceType)
  12.         local allData = self:GetAllData(peripheralName)
  13.         return (allData and allData.sources and allData.sources[sourceType]) or false
  14.     end,
  15.     GetTimeoutInterval = function(self)
  16.         return self.timeoutInterval
  17.     end,
  18.     SetTimeoutInterval = function(self, timeoutInterval)
  19.         if type(timeoutInterval) == "number" and timeoutInterval >= 0 then
  20.             self.timeoutInterval = timeoutInterval
  21.             return true
  22.         end
  23.         return false
  24.     end,
  25.     Run = function(self)
  26.    
  27.         -- open rednet
  28.         for _, side in ipairs(redstone.getSides()) do
  29.             if peripheral.getType(side) == "modem" then
  30.                 rednet.open(side)
  31.             end
  32.         end
  33.         if not rednet.isOpen() then
  34.             printError("could not open rednet")
  35.             return
  36.         end
  37.  
  38.         -- main loop
  39.         local timer = os.startTimer(self.timeoutInterval)
  40.         local event, eventType
  41.         while true do
  42.             event = {os.pullEvent()}
  43.             eventType = event[1]
  44.             if eventType == "rednet_message" then
  45.                 local senderID, message, protocol = unpack(event, 2)
  46.                 if protocol == REMOTE_PERIPHERAL_PROTOCOL and type(message) == "table" then
  47.                     for peripheralName, peripheralData in pairs(message) do
  48.                         if not self.list[peripheralName] then
  49.                             peripheralData.ID = senderID
  50.                             peripheralData.timeout = 0
  51.                             self.list[peripheralName] = peripheralData
  52.                             os.queueEvent("remote_peripheral_add", peripheralName)
  53.                         elseif self.list[peripheralName].ID == senderID then
  54.                             self.list[peripheralName].main = peripheralData.main
  55.                             self.list[peripheralName].sources = peripheralData.sources
  56.                             self.list[peripheralName].timeout = 0
  57.                         end
  58.                     end
  59.                 end
  60.             elseif eventType == "timer" and event[2] == timer then
  61.                 local toDelete = {}
  62.                 for peripheralName, peripheralData in pairs(self.list) do
  63.                     peripheralData.timeout = peripheralData.timeout + 1
  64.                     if peripheralData.timeout >= 3 then
  65.                         table.insert(toDelete, peripheralName)
  66.                     end
  67.                 end
  68.                 for _, peripheralName in ipairs(toDelete) do
  69.                     self.list[peripheralName] = nil
  70.                     os.queueEvent("remote_peripheral_remove", peripheralName)
  71.                 end
  72.                 timer = os.startTimer(self.timeoutInterval)
  73.             end
  74.         end
  75.     end,
  76. }
  77. local remotePeripheralClientMetatable = {__index = remotePeripheralClientMethods}
  78.  
  79. function new()
  80.     local remotePeripheralClient = {
  81.         list = {},
  82.         timeoutInterval = 20,
  83.     }
  84.    
  85.     return setmetatable(remotePeripheralClient, remotePeripheralClientMetatable)
  86. end
Add Comment
Please, Sign In to add comment