MIKhanik

SHCS-doors.lua

Oct 28th, 2015
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component=require('component')
  2. local event=require('event')
  3. local modem=component.modem
  4.  
  5.  
  6. PORT=111
  7.  
  8. doorControl={
  9.   type='doorControl',
  10.   doors={},
  11.   worked={}
  12. }
  13.  
  14. doorControl.__index=doorControl
  15.  
  16. function doorControl:new(_name)
  17.   local obj={name = _name,
  18.     type='doorControl',
  19.     doors={}
  20.   }
  21.   setmetatable(obj,self)
  22.   print('New door control group: '..obj.name)
  23.   return obj
  24. end
  25.  
  26. function doorControl:addDoor(name,of,clf,chf)
  27.   self.doors[name]={}
  28.   self.doors[name]['open']=of
  29.   self.doors[name]['close']=clf
  30.   self.doors[name]['check']=chf
  31. end
  32.  
  33. function doorControl:removeDoor(name)
  34.   self.doors[name]=nil
  35. end
  36.  
  37. function doorControl.msgHandler(id, _, sender, s_port, distance, ...)
  38.   if s_port==PORT then
  39.     ans=table.pack(...)
  40.     if doorControl.worked[ans[2]]~=nil then
  41.       print('We have a doors gr '..ans[2])
  42.       local wdoor=doorControl.worked[ans[2]]
  43.       if wdoor.doors[ans[3]]~=nil then
  44.         print('We have a door '..ans[3])
  45.         if ans[1]=='openDoor' then
  46.           print('openDoor')
  47.           answer=table.pack(wdoor.doors[ans[3]].open())
  48.         elseif ans[1]=='closeDoor' then
  49.           print('openDoor')
  50.           answer=table.pack(wdoor.doors[ans[3]].close())
  51.         elseif ans[1]=='checkDoor' then
  52.           print('checkDoor')
  53.           answer=table.pack(wdoor.doors[ans[3]].open())
  54.         end
  55.         if answer~=nil then
  56.           modem.send(sender,PORT,table.unpack(answer))
  57.         end
  58.       end
  59.     end
  60.   end
  61. end
  62.  
  63. function doorControl:pre_start()
  64.   doorControl.worked[self['name']]=self
  65.   event.listen('modem_message',doorControl.msgHandler)
  66. end
  67.  
  68. return doorControl
RAW Paste Data