Advertisement
Guest User

motion.lua

a guest
Dec 7th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. local sensor = require("component").motion_sensor
  2. local redstone = require("component").redstone
  3. local event = require("event")
  4. local term = require("term")
  5. local thread = require("thread")
  6. local fs = require("filesystem")
  7. local serialization = require("serialization")
  8. local shell = require("shell")
  9.  
  10. dataPath = fs.concat(shell.getWorkingDirectory() .. "/" .. "data-motion.dat")
  11. history = {}
  12. users = {}
  13. eventThread = nil
  14. passive = false
  15. alerted = false
  16.  
  17. sensor.setSensitivity(0.2)
  18.  
  19. -- Load data
  20. if (fs.exists(dataPath)) then
  21.   local dataFile = io.open(dataPath, "r")
  22.   users = serialization.unserialize(dataFile:read("*a"))
  23.   dataFile:close()
  24. end
  25.  
  26. function hasName(name)
  27.   for i, v in ipairs(users) do
  28.     if v.name == name then
  29.       return true
  30.     end
  31.   end
  32.   return false
  33. end
  34.  
  35. function indexName(name)
  36.   for i, v in ipairs(users) do
  37.     if v.name == name then
  38.       return i
  39.     end
  40.   end
  41. end
  42.  
  43. function clean(table)
  44.   local result = {}
  45.   for _, v in pairs(table) do
  46.     result[#result + 1] = v
  47.   end
  48.   return result
  49. end
  50.  
  51. function alert()
  52.   term.write("ALERT\n")
  53.   redstone.setOutput({15, 15, 15, 15, 15, 15})
  54.   alerted = true
  55. end
  56.  
  57. function unalert()
  58.   term.write("UNALERT\n")
  59.   redstone.setOutput({0, 0, 0, 0, 0, 0})
  60.   alerted = false
  61. end
  62.  
  63. function getPrompt()
  64.   return "motion> "
  65. end
  66.  
  67. function promptMode()
  68.   term.write("Prompt Mode\n")
  69.   if (eventThread ~= nil) then
  70.     eventThread:kill()
  71.   end
  72.   passive = false
  73. end
  74.  
  75. function passiveMode()
  76.   term.write("Passive Mode\nPress enter to return\nPress space to toggle alert\n")
  77.   eventThread = thread.create(function()
  78.     while true do
  79.       local _, _, x, y, z, name = event.pull("motion")
  80.       if (not hasName(name)) then
  81.         term.write("MOTION: " .. name .. " @ (" .. x .. ", " .. y .. ", " .. z .. ")\n")
  82.         if (not alerted) then
  83.           alert()
  84.         end
  85.       end
  86.     end
  87.   end)
  88.   passive = true
  89. end
  90.  
  91. commands = {
  92.   ["exit"] = {
  93.     description = "Exits the shell.",
  94.     usage = "",
  95.     func = function(args)
  96.       return false
  97.     end
  98.   },
  99.   ["help"] = {
  100.     description = "Shows the help menu.",
  101.     usage = "",
  102.     func = function(args)
  103.       term.write(#commands)
  104.       for k, v in ipairs(commands) do
  105.         term.write(k .. ":\n")
  106.         term.write("  " .. v["description"] .. "\n")
  107.         term.write("  Usage: " .. k .. " " .. v["usage"] .. "\n")
  108.       end
  109.       return true
  110.     end
  111.   },
  112.   ["listuser"] = {
  113.     description = "Lists all registered users.",
  114.     usage = "",
  115.     func = function(args)
  116.       for k, v in ipairs(users) do
  117.         term.write("  " .. v.name .. "\n")
  118.       end
  119.       term.write("Allowed users: " .. #users .. "\n")
  120.       return true
  121.     end
  122.   },
  123.   ["adduser"] = {
  124.     description = "Adds a user.",
  125.     usage = "<username>",
  126.     func = function(args)
  127.       if #args <= 1 then
  128.         term.write("Usage: " .. args[1] .. " <username>\n")
  129.         return true
  130.       end
  131.       local username = args[2]
  132.  
  133.       if (hasName(username)) then
  134.         term.write("User is already registered\n")
  135.       else
  136.         table.insert(users, { name = username })
  137.       end
  138.       return true
  139.     end
  140.   },
  141.   ["deluser"] = {
  142.     description = "Deletes a user.",
  143.     usage = "<username>",
  144.     func = function(args)
  145.       if #args <= 1 then
  146.         term.write("Usage: " .. args[1] .. " <username>\n")
  147.         return true
  148.       end
  149.       local username = args[2]
  150.      
  151.       local i = indexName(username)
  152.       if (i) then
  153.         users[i] = nil
  154.         users = clean(users)
  155.       else
  156.         term.write("User is not registered\n")
  157.       end
  158.       return true
  159.     end
  160.   },
  161.   ["alert"] = {
  162.     description = "Activates the alert.",
  163.     usage = "",
  164.     func = function(args)
  165.       alert()
  166.       return true
  167.     end
  168.   },
  169.   ["unalert"] = {
  170.     description = "Deactivates the alert.",
  171.     usage = "",
  172.     func = function(args)
  173.       unalert()
  174.       return true
  175.     end
  176.   },
  177.   ["passive"] = {
  178.     description = "Changes into passive mode.",
  179.     usage = "",
  180.     func = function(args)
  181.       passiveMode()
  182.       return true
  183.     end
  184.   }
  185. }
  186.  
  187. function runCommand(cmd)
  188.   local args = {}
  189.   for arg in cmd:gmatch("%S+") do
  190.     table.insert(args, arg)
  191.   end
  192.   if #args == 0 then
  193.     return true
  194.   end
  195.   local name = args[1]
  196.  
  197.   local command = commands[name]
  198.   if (command) then
  199.     local func = command.func
  200.     return func(args)
  201.   else
  202.     term.write("Invalid command. Use 'help' to list all commands.\n")
  203.     return true
  204.   end
  205. end
  206.  
  207. promptMode()
  208.  
  209. while true do
  210.   if (passive) then
  211.     local _, _, char = event.pull("key_down")
  212.     if (char == 13) then
  213.       promptMode()
  214.       goto continue
  215.     elseif (char == 32) then
  216.       if (alerted) then
  217.         unalert()
  218.       else
  219.         alert()
  220.       end
  221.     end
  222.   else
  223.     term.write(getPrompt())
  224.     local input = term.read(history)
  225.     input = input:sub(1, -2)
  226.     if (not runCommand(input)) then
  227.       break
  228.     end
  229.   end
  230.   ::continue::
  231. end
  232.  
  233. -- Save data
  234.  
  235. local dataFile = io.open(dataPath, "w")
  236. dataFile:write(serialization.serialize(users))
  237. dataFile:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement