-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharmLog.lua
More file actions
62 lines (51 loc) · 1.41 KB
/
armLog.lua
File metadata and controls
62 lines (51 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- Arming logger
local armed = false
local function getSwitch()
local ok, v = pcall(getValue, "sa")
if not ok or v == nil then return 0 end
return v
end
local function getTimeParts()
local ok, t = pcall(getDateTime)
if not ok or not t then
return 0,0,0,0,0,0
end
return t.year or 0, t.mon or 0, t.day or 0,
t.hour or 0, t.min or 0, t.sec or 0
end
local function write(text, newline)
local y,mo,d = getTimeParts()
local filename = string.format("/LOGS/%02d_%02d_%04d.txt", d, mo, y)
local ok, f = pcall(io.open, filename, "a")
if ok and f then
pcall(io.write, f, text)
if newline then
pcall(io.write, f, "\n")
end
pcall(io.flush, f)
pcall(io.close, f)
else
playTone(400, 200, 0, 0)
end
end
local function step()
local v = getSwitch()
if v >= 100 and not armed then
armed = true
local _,_,_, h,m,s = getTimeParts()
local msg = "Armed: " .. h .. ":" .. m .. ":" .. s .. "; "
write(msg, false) -- NO newline
playTone(1500, 120, 0, 0)
end
if v < 100 and armed then
armed = false
local _,_,_, h,m,s = getTimeParts()
local msg = "Disarmed: " .. h .. ":" .. m .. ":" .. s .. ";"
write(msg, true) -- WITH newline
playTone(900, 120, 0, 0)
end
end
local function init() end
local function run(e) step() return 0 end
local function background() step() end
return { init = init, run = run, background = background }