View difference between Paste ID: SYGgZg3y and wTfZ7hpz
SHOW: | | - or go back to the newest paste.
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-
  rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s.."|")
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-
  rednet.send(serv_id,"CARD|ADD|"..args[2]:sub(1,20)..sep..s..sep)
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-
    if data:sub(1,7)=="CARD|OK" then
44+
45-
      print("Authentication sucessful.")
45+
46
  local s=table.concat({unpack(args,3)}," "):sub(1,79)
47-
      os.sleep(1.5)
47+
48
  print("Writing..")
49-
      else
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-
      if data:sub(1,7)=="CARD|OK" then
62+
63-
        on()
63+
64-
        valid=true
64+
65-
      else
65+
66-
        if not valid then
66+
67-
        off()
67+
68-
        end
68+
69-
      end
69+
70
    check(data)
71
    elseif e=="rfid_detected" then
72
      data=data..sep
73-
    else print(e,data) end 
73+
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