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.     on()
  31.     valid=true
  32.   else
  33.     if not valid then
  34.       off()
  35.     end
  36.   end
  37. end
  38.  
  39. if args[1]=="writemag" then -- name,daat
  40.   local s=table.concat({unpack(args,3)}," "):sub(1,79)
  41.   reader.beginWrite(args[2]:sub(1,20)..sep..s,args[2].."'s ID card")
  42.   rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s.."|"..args[3]..sep)
  43.   print("Swipe a blank card!")
  44.   os.pullEvent("mag_write_done")
  45. elseif args[1]=="writerfid" then
  46.   local s=table.concat({unpack(args,3)}," "):sub(1,79)
  47.   writer.encode(args[2]:sub(1,20)..sep..s,args[2].."'s ID card")
  48.   print("Writing..")
  49.   rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s..sep..args[3]..sep)
  50.   os.pullEvent("rfid_written")
  51. else
  52.   while true do
  53.     reader.setInsertCardLight(true)
  54.     local e,data=os.pullEvent()
  55.     if e=="char" or e=="key" then
  56.       on()
  57.       os.sleep(3)
  58.       off()
  59.     elseif e=="mag_swipe" then
  60.     reader.setInsertCardLight(false)
  61.     data=data..sep
  62.     rednet.send(serv_id,"CARD|CHECK|"..data)
  63.    
  64.     local id=0
  65.     while id~=serv_id do
  66.     local i,resp=rednet.receive()
  67.     id=i
  68.     data=resp
  69.     end
  70.     check(data)
  71.     elseif e=="rfid_detected" then
  72.       data=data..sep
  73.       rednet.send(serv_id,"CARD|CHECK|"..data)
  74.       local id=0
  75.       while id~=serv_id do
  76.         local i,resp=rednet.receive()
  77.         id=i
  78.         data=resp
  79.       end  
  80.    
  81.       checkrfid(data)
  82.      
  83.     elseif e=="rfid_scan_done" then if not valid then off() end valid=false
  84.     end
  85.     rfid.scan(3)  
  86.   end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment