Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- event = require("event")
- modem = require("component").modem
- fs = require("filesystem")
- serialization = require("serialization")
- configfile = "/etc/motionDetection.conf"
- config = {}
- defaultConfig = {
- port = 420,
- logfile = "/home/motion.log",
- biosfile = "/usr/motionDetectionBios.lua",
- whitelist = {}
- }
- biosFile = "sensitivity=15 \
- \
- modem=component.proxy(component.list(\"modem\")()) \
- sensor=component.proxy(component.list(\"motion_sensor\")()) \
- function sendData(...) \
- local args = table.pack(...) \
- pcall(function() modem.broadcast(dPort,table.unpack(args)) end) \
- end \
- \
- modem.setWakeMessage(\"initSensors\") \
- modem.open(dPort) \
- modem.setStrength(400) \
- sensor.setSensitivity(15) \
- while true do sendData(computer.pullSignal(math.huge)) end"
- function loadConfig(file)
- local cf = io.open(file, "r")
- local data = serialization.unserialize(cf:read("*a"))
- cf:close()
- return data
- end
- function writeConfig(file, data)
- fh = io.open(file, "w")
- fh:write(serialization.serialize(data))
- fh:close()
- end
- function writeBios()
- fh = io.open(config.biosfile, "w")
- fh:write("dPort=" .. config.port .. "\n" .. biosFile)
- fh:close()
- end
- function flashBios()
- writeBios()
- print("flashing bios for port " .. config.port)
- os.execute("flash "..config.biosfile.." motionDetector")
- end
- function configuration()
- print("")
- print("path to logfile, press enter to use default ["..defaultConfig.logfile.."]")
- local logfile = io.read("*line")
- if logfile ~= "" then
- defaultConfig.logfile = logfile
- end
- print("path to bios file, press enter to use default ["..defaultConfig.biosfile.."]")
- biosfile = io.read("*line")
- if biosfile ~= "" then
- defaultConfig.biosfile = biosfile
- end
- print("port for network communication, press enter to use default ["..defaultConfig.port.."]")
- port = io.read("*line")
- if port ~= "" then
- defaultConfig.port = tonumber(port)
- end
- print("users to whitelist, dont enter any name to finish configuration")
- local addUsers = true
- while addUsers do
- local username = io.read("*line")
- if username == "" then
- addUsers = false;
- else
- table.insert(defaultConfig.whitelist, username)
- end
- end
- writeConfig(configfile, defaultConfig)
- print("done")
- end
- if not fs.exists(configfile) then
- print("no configuration found, running setup")
- configuration()
- end
- config = loadConfig(configfile)
- if not fs.exists(config.biosfile) then
- print("no bios file found")
- writeBios()
- print("flash bios file now? [yes/no]")
- local whatever = io.read("*line")
- if whatever == "yes" or whatever == "y" then
- flashBios()
- end
- end
- args = { ... }
- if args[1] == "flash" then
- flashBios()
- os.exit(0)
- elseif args[1] == "config" or args[1] == "setup" then
- configuration()
- elseif args[1] == "help" then
- scriptName, whatever1, whatever2 = require("process").running()
- print("# usage informations for motionDetection script")
- print(scriptName.." flash -- flashs the eeprom")
- print(scriptName.." config/setup -- runs the configuration wizard")
- os.exit(0)
- end
- -- start of client program
- modem.open(config.port)
- modem.setStrength(400)
- modem.broadcast(config.port, "initSensors")
- function msg(eventType, ourModemAddress, otherModemAddress, port, otherModemDistance, eventName, motionSensorAddress, x, y, z, name)
- if eventName ~= "motion" then return; end
- local writeToFile = true
- x = math.floor(x + 0.5)
- y = math.floor(y + 0.5)
- z = math.floor(z + 0.5)
- local msg = os.date().." (" ..name .. ") at [x: " .. x ..", y: ".. y ..", z: "..z.."]"
- for i=1,#config.whitelist do if config.whitelist[i]:lower() == name:lower() then writeToFile = false end end
- if writeToFile then
- fh = io.open(config.logfile, "a")
- fh:write(msg .. "\n")
- fh:close()
- end
- print("motion detected: " .. msg)
- end
- event.listen("modem_message", msg)
- print("listening for network messages on port " .. config.port)
- event.pull("interrupted")
- event.ignore("modem_message", msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement