SHOW:
|
|
- or go back to the newest paste.
| 1 | - | local accessLevel = 2 |
| 1 | + | local door = require("component").os_door
|
| 2 | - | local cryptKey = {1, 2, 3, 4, 5}
|
| 2 | + | |
| 3 | - | local modemPort = 199 |
| 3 | + | keypad = require("component").os_keypad
|
| 4 | event = require("event")
| |
| 5 | ||
| 6 | local pin = "1111" | |
| 7 | - | local component = require("component")
|
| 7 | + | local keypadInput = "" |
| 8 | - | local gpu = component.gpu |
| 8 | + | |
| 9 | - | local event = require("event")
|
| 9 | + | -- set this to true if you want to run the script as daemon |
| 10 | - | local ser = require("serialization")
|
| 10 | + | local runScriptInBackground = false |
| 11 | - | local term = require("term")
|
| 11 | + | |
| 12 | - | local computer = component.computer |
| 12 | + | function updateDisplay() |
| 13 | local displayString = "" | |
| 14 | - | local door = component.os_door |
| 14 | + | for i=1,#keypadInput do |
| 15 | - | local magReader = component.os_magreader |
| 15 | + | displayString = displayString .. "*" |
| 16 | end | |
| 17 | - | local modem = component.modem |
| 17 | + | |
| 18 | keypad.setDisplay(displayString, 7) | |
| 19 | end | |
| 20 | - | local function convert( chars, dist, inv ) |
| 20 | + | |
| 21 | - | return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 ) |
| 21 | + | function checkPin() |
| 22 | if keypadInput == pin then | |
| 23 | keypad.setDisplay("granted", 2)
| |
| 24 | - | local function crypt(str,k,inv) |
| 24 | + | |
| 25 | - | local enc= ""; |
| 25 | + | |
| 26 | - | for i=1,#str do |
| 26 | + | keypad.setDisplay("denied", 4)
|
| 27 | - | if(#str-k[5] >= i or not inv)then |
| 27 | + | end |
| 28 | - | for inc=0,3 do |
| 28 | + | keypadInput = "" |
| 29 | - | if(i%4 == inc)then |
| 29 | + | os.sleep(1) |
| 30 | - | enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv); |
| 30 | + | |
| 31 | - | break; |
| 31 | + | |
| 32 | - | end |
| 32 | + | function keypadEvent(eventName, address, button, button_label) |
| 33 | - | end |
| 33 | + | print("button pressed: " )
|
| 34 | ||
| 35 | - | end |
| 35 | + | if button_label == "*" then |
| 36 | - | if(not inv)then |
| 36 | + | -- remove last character from input cache |
| 37 | - | for i=1,k[5] do |
| 37 | + | keypadInput = string.sub(keypadInput, 1, -2) |
| 38 | - | enc = enc .. string.char(math.random(32,126)); |
| 38 | + | elseif button_label == "#" then |
| 39 | -- check the pin when the user confirmed the input | |
| 40 | - | end |
| 40 | + | checkPin() |
| 41 | - | return enc; |
| 41 | + | |
| 42 | -- add key to input cache if none of the above action apply | |
| 43 | keypadInput = keypadInput .. button_label | |
| 44 | end | |
| 45 | - | function splitString(str, sep) |
| 45 | + | |
| 46 | - | local sep, fields = sep or ":", {}
|
| 46 | + | updateDisplay() |
| 47 | - | local pattern = string.format("([^%s]+)", sep)
|
| 47 | + | |
| 48 | - | str:gsub(pattern, function(c) fields[#fields+1] = c end) |
| 48 | + | |
| 49 | - | return fields |
| 49 | + | -- listen to keypad events |
| 50 | event.listen("keypad", keypadEvent)
| |
| 51 | ||
| 52 | - | term.clear() |
| 52 | + | -- clear keypad display |
| 53 | - | print("Security door Access level : " .. tostring(accessLevel))
|
| 53 | + | keypad.setDisplay("")
|
| 54 | - | print("---------------------------------------------------------------------------")
|
| 54 | + | |
| 55 | ||
| 56 | - | if modem.isOpen(modemPort) == false then |
| 56 | + | if not runScriptInBackground then |
| 57 | - | modem.open(modemPort) |
| 57 | + | -- sleep until the user interrupts the program with CTRL + C |
| 58 | local stopMe = false | |
| 59 | - | data = crypt(tostring(accessLevel), cryptKey) |
| 59 | + | event.listen("interrupted", function() stopMe = true; end)
|
| 60 | - | modem.broadcast(modemPort, "setlevel", data) |
| 60 | + | while not stopMe do os.sleep(0.1) end |
| 61 | ||
| 62 | - | while true do |
| 62 | + | -- ignore keypad events on exit |
| 63 | - | ev, _, user, str, uuid = event.pull("magData")
|
| 63 | + | event.ignore("keypad", keypadEvent)
|
| 64 | - | local data = crypt(str, cryptKey, true) |
| 64 | + | |
| 65 | - | if ev then |
| 65 | + | -- show that the keypad is inactive |
| 66 | - | local tmpTable = splitString(data," ") |
| 66 | + | keypad.setDisplay("inactive", 6)
|
| 67 | - | term.write(tmpTable[3] .. ":") |
| 67 | + |