Advertisement
Guest User

spawnDoor

a guest
Nov 17th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.75 KB | None | 0 0
  1. -- rfidDoor.lua - Simple RFID reader and writer utility
  2. -- Author: O5-6 (Master10104)
  3. -- Version: 1.0
  4.  
  5. local REQ = {
  6.     4,    -- Clearance
  7.     2,    -- Department
  8.     1     -- AD
  9. }
  10.  
  11. local Rfid = {}
  12. Rfid.__index = Rfid
  13.  
  14. local SECTORS = 16
  15. local SECTOR_SIZE = 4
  16. local KEY_TYPE_A = 0 -- Type B is 1
  17. local KEY_A = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} -- Default key
  18. local BLOCK_SIZE = 16
  19.  
  20.  
  21. -- Create new Rfid simple reader/writer instance
  22. function Rfid:new()
  23.     local rfid = {}
  24.     setmetatable(rfid, Rfid)
  25.     -- ComputerCraft
  26.     if peripheral then
  27.         rfid.rfid = peripheral.find("rfid_reader_writer")
  28.     else
  29.         error("RFID Reader/Writer not found")
  30.     end
  31.     return rfid
  32. end
  33.  
  34. -- Main command line entry point
  35. -- @param args table command line arguments
  36.  
  37. function Rfid:run(args)
  38.     self:check_argument_count(args, 1, "argument", false)
  39.     local arg = args[1]
  40.     if arg == "read" then
  41.         self:read()
  42.     elseif arg == "write" then
  43.         self:check_argument_count(args, 2, "argument", true)
  44.         self:write(args[2])
  45.     else
  46.         error(string.format("No argument \"%s\" found. Try \"help rfid\".", arg))
  47.     end
  48. end
  49.  
  50. -- Writes a table's values to a string
  51. function table_to_string(t)
  52.     local s = ""
  53.     for k, v in ipairs(t) do
  54.         s = s .. v .. ","
  55.     end
  56.     return s
  57. end
  58.  
  59. -- Block and wait for a chip to be found. When found, the chip will be selected
  60. function Rfid:find_chip()
  61.     print("Searching for RFID chip...")
  62.     local rfid_chip
  63.     repeat
  64.         rfid_chip = self.rfid.search()
  65.         os.sleep(0.1)
  66.     until rfid_chip
  67.     print(string.format("Found RFID chip\nID: %s", table_to_string(rfid_chip)))
  68.     self.rfid.select(rfid_chip)
  69. end
  70.  
  71. -- Return a shallow copy of a table constrained to the index
  72. -- @param start_index number index to start from - inclusive
  73. -- @param end_index number index to stop at - inclusive
  74. -- @return table
  75. function copy_table(t, start_index, end_index, empty_value)
  76.     end_index = end_index or #t
  77.     local new_table = {}
  78.     for item_index = start_index, end_index do
  79.         local item = t[item_index]
  80.         table.insert(new_table, item or empty_value)
  81.     end
  82.     return new_table
  83. end
  84.  
  85. -- Checks if two table are equal.
  86. -- Only performs a shallow check
  87. -- @param table_one table first table
  88. -- @param table_two table second table
  89. -- @return boolean equality
  90. function areTablesEqual(table_one, table_two)
  91.     if #table_one ~= #table_two then
  92.         return false
  93.     end
  94.     for table_one_index, table_one_value in pairs(table_one) do
  95.         if table_one_value ~= table_two[table_one_index] then
  96.             return false
  97.         end
  98.     end
  99.     return true
  100. end
  101.  
  102. -- Read from the card's data blocks, converting the read values to characters.
  103. -- This ignores trailing blocks and the first block.
  104. function Rfid:read()
  105.     self:find_chip()
  106.     local blocks = SECTOR_SIZE * SECTORS
  107.     local data = ""
  108.     for block = 1, blocks - 1 do
  109.         if (block + 1) % SECTOR_SIZE ~= 0 then
  110.             self.rfid.auth(KEY_TYPE_A, block, KEY_A)
  111.             local block_table = self.rfid.read(block)
  112.             if not block_table then
  113.                 error(string.format("Auth error. Block: %d", block))
  114.             end
  115.             for byte_index, byte in ipairs(block_table) do
  116.                 data = data .. string.char(byte)
  117.             end
  118.         end
  119.     end
  120.     self.rfid.deauth()
  121.     data = data:gsub(string.char(0), "")
  122.     print(data)
  123.     redstone.setOutput("right", doorCheck(data))
  124.     sleep(2)
  125.     redstone.setOutput("right", false)
  126. end
  127.  
  128. -- Does Door stuff based on
  129. -- local door requirements
  130. -- @param s - String, RFID data
  131. function doorCheck(s)
  132.  
  133.     local code = tonumber(s)
  134.     local isL5 = code/500 >= 1
  135.     local isNotSec = code%2
  136.    
  137.     local clearance = (code/100)-((code/100)%1)
  138.     local depart = ((code/10) - ((code/10)%1))%10
  139.    
  140.     local fitsClearance = clearance >= REQ[1]
  141.     local fitsDepartment = depart == REQ[2] or REQ[2]==0
  142.     local fitsSec = isNotSec <= REQ[3]
  143.    
  144.     return (fitsClearance and
  145.            fitsDepartment and
  146.            fitsSec) or isL5
  147. end
  148.  
  149.  
  150. -- Write to the card's data blocks, converting string characters to bytes
  151. --- This does not write to trailing blocks or the first block
  152. -- @param s string string to write
  153. function Rfid:write(s)
  154.     local data = {}
  155.     for character_index = 1, string.len(s) do
  156.         local byte = string.byte(s:sub(character_index))
  157.         table.insert(data, byte)
  158.     end
  159.     self:find_chip()
  160.     local blocks = SECTOR_SIZE * SECTORS
  161.     for block = 1, blocks - 1 do
  162.         if (block + 1) % SECTOR_SIZE ~= 0 then
  163.             self.rfid.auth(KEY_TYPE_A, block, KEY_A)
  164.             local chunk = copy_table(data, 1, BLOCK_SIZE, 0)
  165.             data = copy_table(data, BLOCK_SIZE + 1)
  166.             local written = self.rfid.write(block, chunk)
  167.             if not written or not areTablesEqual(self.rfid.read(block), chunk) then
  168.                 error(string.format("Auth error. Block: %d", block))
  169.             end
  170.         end
  171.     end
  172.     self.rfid.deauth()
  173.     print("Write finished")
  174. end
  175.  
  176. -- Check the argument count matches the bounds passed
  177. -- Errors with a reason message.
  178. --
  179. -- @param args table arguments
  180. -- @param index number minimun amount of arguments
  181. -- @param name string noun to use for error messages
  182. -- @param max boolean should the arguments end at the index passed
  183. function Rfid:check_argument_count(args, index, name, max)
  184.     if max == nil then
  185.         max = true
  186.     end
  187.     if #args < index then
  188.         error("Missing " .. name .. ". Try Try \"help rfid\".")
  189.     elseif max and #args > index then
  190.         error("Too many " .. name .. "s passed. Try \"help rfid\".")
  191.     end
  192. end
  193.  
  194. -- Init
  195. local rfid = Rfid:new()
  196. rfid:run({...})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement