Guest User

card-client

a guest
Sep 3rd, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  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. if args[1]=="writemag" then -- name,daat
  13.   local s=table.concat({unpack(args,3)}," "):sub(1,79)
  14.   reader.beginWrite(args[2]:sub(1,20)..sep..s,args[2].."'s ID card")
  15.   rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s.."|")
  16.   print("Swipe a blank card!")
  17.   os.pullEvent("mag_write_done")
  18. elseif args[1]=="writerfid" then
  19.   local s=table.concat({unpack(args,3)}," "):sub(1,79)
  20.   writer.encode(args[2]:sub(1,20)..sep..s,args[2].."'s ID card")
  21.   print("Writing..")
  22.   rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s..sep)
  23.   os.pullEvent("rfid_written")
  24. else
  25.   while true do
  26.     reader.setInsertCardLight(true)
  27.     local e,data=os.pullEvent()
  28.     if e=="char" or e=="key" then
  29.       on()
  30.       os.sleep(3)
  31.       off()
  32.    
  33.     elseif e=="mag_swipe" then
  34.     reader.setInsertCardLight(false)
  35.     data=data..sep
  36.     rednet.send(serv_id,"CARD|CHECK|"..data)
  37.    
  38.     local id=0
  39.     while id~=serv_id do
  40.     local i,resp=rednet.receive()
  41.     id=i
  42.     data=resp
  43.     end
  44.     if data:sub(1,7)=="CARD|OK" then
  45.       print("Authentication sucessful.")
  46.       on()
  47.       os.sleep(1.5)
  48.       off()
  49.       else
  50.       off()
  51.     end
  52.     elseif e=="rfid_detected" then
  53.       data=data..sep
  54.       rednet.send(serv_id,"CARD|CHECK|"..data)
  55.       local id=0
  56.       while id~=serv_id do
  57.         local i,resp=rednet.receive()
  58.         id=i
  59.         data=resp
  60.       end  
  61.      
  62.       if data:sub(1,7)=="CARD|OK" then
  63.         on()
  64.         valid=true
  65.       else
  66.         if not valid then
  67.         off()
  68.         end
  69.       end
  70.      
  71.      
  72.     elseif e=="rfid_scan_done" then if not valid then off() end valid=false
  73.     else print(e,data) end
  74.     rfid.scan(3)  
  75.   end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment