Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component=require('component')
- local event=require('event')
- local modem=component.modem
- PORT=111
- doorControl={
- type='doorControl',
- doors={},
- worked={}
- }
- doorControl.__index=doorControl
- function doorControl:new(_name)
- local obj={name = _name,
- type='doorControl',
- doors={}
- }
- setmetatable(obj,self)
- print('New door control group: '..obj.name)
- return obj
- end
- function doorControl:addDoor(name,of,clf,chf)
- self.doors[name]={}
- self.doors[name]['open']=of
- self.doors[name]['close']=clf
- self.doors[name]['check']=chf
- end
- function doorControl:removeDoor(name)
- self.doors[name]=nil
- end
- function doorControl.msgHandler(id, _, sender, s_port, distance, ...)
- if s_port==PORT then
- ans=table.pack(...)
- if doorControl.worked[ans[2]]~=nil then
- print('We have a doors gr '..ans[2])
- local wdoor=doorControl.worked[ans[2]]
- if wdoor.doors[ans[3]]~=nil then
- print('We have a door '..ans[3])
- if ans[1]=='openDoor' then
- print('openDoor')
- answer=table.pack(wdoor.doors[ans[3]].open())
- elseif ans[1]=='closeDoor' then
- print('openDoor')
- answer=table.pack(wdoor.doors[ans[3]].close())
- elseif ans[1]=='checkDoor' then
- print('checkDoor')
- answer=table.pack(wdoor.doors[ans[3]].open())
- end
- if answer~=nil then
- modem.send(sender,PORT,table.unpack(answer))
- end
- end
- end
- end
- end
- function doorControl:pre_start()
- doorControl.worked[self['name']]=self
- event.listen('modem_message',doorControl.msgHandler)
- end
- return doorControl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement