Advertisement
ben_mkiv

motionDetection.lua

Jun 28th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | None | 0 0
  1. event = require("event")
  2. modem = require("component").modem
  3. fs = require("filesystem")
  4. serialization = require("serialization")
  5.  
  6. configfile = "/etc/motionDetection.conf"
  7.  
  8. config = {}
  9.  
  10. defaultConfig = {
  11.     port = 420,
  12.     logfile = "/home/motion.log",
  13.     biosfile = "/usr/motionDetectionBios.lua",
  14.     whitelist = {}
  15. }
  16.  
  17. biosFile = "sensitivity=15 \
  18. \
  19. modem=component.proxy(component.list(\"modem\")()) \
  20. sensor=component.proxy(component.list(\"motion_sensor\")()) \
  21. function sendData(...) \
  22.    local args = table.pack(...) \
  23.    pcall(function() modem.broadcast(dPort,table.unpack(args)) end) \
  24. end \
  25. \
  26. modem.setWakeMessage(\"initSensors\") \
  27. modem.open(dPort) \
  28. modem.setStrength(400) \
  29. sensor.setSensitivity(15) \
  30. while true do sendData(computer.pullSignal(math.huge)) end"
  31.  
  32. function loadConfig(file)
  33.     local cf = io.open(file, "r")
  34.     local data = serialization.unserialize(cf:read("*a"))
  35.     cf:close()
  36.     return data
  37. end
  38.  
  39. function writeConfig(file, data)
  40.     fh = io.open(file, "w")
  41.     fh:write(serialization.serialize(data))
  42.     fh:close()
  43. end
  44.  
  45. function writeBios()
  46.     fh = io.open(config.biosfile, "w")
  47.     fh:write("dPort=" .. config.port .. "\n" .. biosFile)
  48.     fh:close()
  49. end
  50.  
  51. function flashBios()
  52.     writeBios()
  53.     print("flashing bios for port " .. config.port)
  54.     os.execute("flash "..config.biosfile.." motionDetector")
  55. end
  56.  
  57. function configuration()
  58.     print("")
  59.     print("path to logfile, press enter to use default ["..defaultConfig.logfile.."]")
  60.     local logfile = io.read("*line")
  61.     if logfile ~= "" then
  62.         defaultConfig.logfile = logfile
  63.     end
  64.  
  65.     print("path to bios file, press enter to use default ["..defaultConfig.biosfile.."]")
  66.     biosfile = io.read("*line")
  67.     if biosfile ~= "" then
  68.         defaultConfig.biosfile = biosfile
  69.     end
  70.  
  71.     print("port for network communication, press enter to use default ["..defaultConfig.port.."]")
  72.     port = io.read("*line")
  73.     if port ~= "" then
  74.         defaultConfig.port = tonumber(port)
  75.     end
  76.  
  77.     print("users to whitelist, dont enter any name to finish configuration")
  78.     local addUsers = true
  79.  
  80.     while addUsers do
  81.         local username = io.read("*line")
  82.         if username == "" then
  83.             addUsers = false;
  84.         else
  85.             table.insert(defaultConfig.whitelist, username)
  86.         end
  87.     end
  88.  
  89.     writeConfig(configfile, defaultConfig)
  90.  
  91.     print("done")
  92. end
  93.  
  94. if not fs.exists(configfile) then
  95.     print("no configuration found, running setup")
  96.     configuration()
  97. end
  98.  
  99. config = loadConfig(configfile)
  100.  
  101. if not fs.exists(config.biosfile) then
  102.     print("no bios file found")
  103.     writeBios()
  104.  
  105.     print("flash bios file now? [yes/no]")
  106.     local whatever = io.read("*line")
  107.     if whatever == "yes" or whatever == "y" then
  108.         flashBios()
  109.     end
  110. end
  111.  
  112. args = { ... }
  113.  
  114. if args[1] == "flash" then
  115.     flashBios()
  116.     os.exit(0)
  117. elseif args[1] == "config" or args[1] == "setup" then
  118.     configuration()
  119. elseif args[1] == "help" then
  120.     scriptName, whatever1, whatever2 = require("process").running()
  121.     print("# usage informations for motionDetection script")
  122.     print(scriptName.." flash -- flashs the eeprom")
  123.     print(scriptName.." config/setup -- runs the configuration wizard")
  124.     os.exit(0)
  125. end
  126.  
  127. -- start of client program
  128.  
  129. modem.open(config.port)
  130. modem.setStrength(400)
  131. modem.broadcast(config.port, "initSensors")
  132.  
  133. function msg(eventType, ourModemAddress, otherModemAddress, port, otherModemDistance, eventName, motionSensorAddress, x, y, z, name)
  134.     if eventName ~= "motion" then return; end
  135.  
  136.     local writeToFile = true
  137.  
  138.     x = math.floor(x + 0.5)
  139.     y = math.floor(y + 0.5)
  140.     z = math.floor(z + 0.5)
  141.     local msg = os.date().." (" ..name .. ") at [x: " .. x ..", y: ".. y ..", z: "..z.."]"
  142.  
  143.     for i=1,#config.whitelist do if config.whitelist[i]:lower() == name:lower() then writeToFile = false end end
  144.  
  145.     if writeToFile then
  146.         fh = io.open(config.logfile, "a")
  147.         fh:write(msg .. "\n")
  148.         fh:close()
  149.     end
  150.  
  151.     print("motion detected: " .. msg)
  152. end
  153.  
  154. event.listen("modem_message", msg)
  155. print("listening for network messages on port " .. config.port)
  156. event.pull("interrupted")
  157.  
  158. event.ignore("modem_message", msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement