Stary2001

Untitled

Sep 3rd, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args={...}
  2. local sep="|"
  3. local serv_id=167
  4. rednet.open("top")
  5. reader = peripheral.wrap("back")
  6. rfid=peripheral.wrap("left")
  7. writer=peripheral.wrap("right")
  8.  
  9. function on() rs.setOutput("bottom",true) end
  10. function off() rs.setOutput("bottom",false) end
  11.  
  12. function check(data)
  13.   if data:sub(1,7)=="CARD|OK" then
  14.      local allow=false
  15.     for door in string.gmatch(data:sub(9),"(.-),") do
  16.       if door == ourdoor then allow=true break end
  17.     end
  18.  
  19.     if not allow then return end
  20.     on()
  21.     os.sleep(1.5)
  22.     off()
  23.   else
  24.     off()
  25.   end
  26. end
  27.  
  28. function checkrfid(data)
  29.   if data:sub(1,7)=="CARD|OK" then
  30.      local allow=false
  31.     for door in string.gmatch(data:sub(9),"(.-),") do
  32.       if door == ourdoor then allow=true break end
  33.     end
  34.  
  35.     if not allow then return end
  36.  
  37.     on()
  38.     valid=true
  39.   else
  40.     if not valid then
  41.       off()
  42.     end
  43.   end
  44. end
  45.  
  46. if args[1]=="writemag" then -- name,daat
  47.   local s=table.concat({unpack(args,3)}," "):sub(1,79)
  48.   reader.beginWrite(args[2]:sub(1,20)..sep..s,args[2].."'s ID card")
  49.   rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s.."|"..args[3]..sep)
  50.   print("Swipe a blank card!")
  51.   os.pullEvent("mag_write_done")
  52. elseif args[1]=="writerfid" then
  53.   local s=table.concat({unpack(args,3)}," "):sub(1,79)
  54.   writer.encode(args[2]:sub(1,20)..sep..s,args[2].."'s ID card")
  55.   print("Writing..")
  56.   rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s..sep..args[3]..sep)
  57.   os.pullEvent("rfid_written")
  58. else
  59.   while true do
  60.     reader.setInsertCardLight(true)
  61.     local e,data=os.pullEvent()
  62.     if e=="char" or e=="key" then
  63.       on()
  64.       os.sleep(3)
  65.       off()
  66.     elseif e=="mag_swipe" then
  67.     reader.setInsertCardLight(false)
  68.     data=data..sep
  69.     rednet.send(serv_id,"CARD|CHECK|"..data)
  70.    
  71.     local id=0
  72.     while id~=serv_id do
  73.     local i,resp=rednet.receive()
  74.     id=i
  75.     data=resp
  76.     end
  77.     check(data)
  78.     elseif e=="rfid_detected" then
  79.       data=data..sep
  80.       rednet.send(serv_id,"CARD|CHECK|"..data)
  81.       local id=0
  82.       while id~=serv_id do
  83.         local i,resp=rednet.receive()
  84.         id=i
  85.         data=resp
  86.       end  
  87.    
  88.       checkrfid(data)
  89.      
  90.     elseif e=="rfid_scan_done" then if not valid then off() end valid=false
  91.     end
  92.     rfid.scan(3)  
  93.   end
  94. end
Advertisement
Add Comment
Please, Sign In to add comment