forked from info-beamer/package-conference-room
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.lua
More file actions
112 lines (87 loc) · 3.23 KB
/
device.lua
File metadata and controls
112 lines (87 loc) · 3.23 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
-------------------------------------------------------------------------------
--- device state
-- register self
local device = rawget(_G, "device")
if not device then
device = {}
rawset(_G, "device", device)
device.baseTime = 0
device.timeH = nil
device.timeM = nil
device.timeS = nil
device.lastTimeUpdate = 0
device.timestamps = {}
device.config = nil
device.location = nil
end
-------------------------------------------------------------------------------
--- wall-clock time
function device.getWallClockTime()
local now = sys.now()
return device.baseTime + now, now - device.lastTimeUpdate
end
-- interpolates since last update received
function device.getWallClockTimeString()
local h, m, s = device.timeH, device.timeM, device.timeS
if not (h and m and s) then
return "--:--"
end
local overflow = sys.now() - device.lastTimeUpdate
local si = math.floor(((s + overflow) % 60))
overflow = (s + overflow) / 60
local mi = math.floor((m + overflow) % 60)
overflow = (m + overflow) / 60
local hi = math.floor((h + overflow) % 24)
return ("%02d:%02d"):format(hi, mi)
end
function device.getTimestampOfDate(date)
return device.timestamps[date]
end
function device.updateTime(tm)
local now = sys.now() -- this is relative to the start of info-beamer
device.lastTimeUpdate = now
local u, h, m, s = tm:match("([%d%.]+),(%d+),(%d+),([%d%.]+)")
device.baseTime = tonumber(u) - now
device.timeH = tonumber(h)
device.timeM = tonumber(m)
device.timeS = tonumber(s)
tools.debugPrint(4, "updated base time: " .. device.baseTime .. ", it's now: " ..
device.getWallClockTimeString() .. " (" .. device.getWallClockTime() .. ")")
end
function device.updateTimestamps(date, ts)
device.timestamps[date] = tonumber(ts)
end
-------------------------------------------------------------------------------
--- device specific configuration
local UNKNOWN_LOCATION = { id = "unk", name = "Unknown location" }
function device.getLocation()
return device.location or UNKNOWN_LOCATION
end
function device.getBackgroundStyle()
return (device.config and device.config.bg_style)
end
function device.updateConfig()
tools.debugPrint(2, "Updating device configuration...")
local serial = assert(sys.get_env("SERIAL"), "SERIAL not set! Please set INFOBEAMER_ENV_SERIAL")
local locations = assert(CONFIG.locations, "WARNING: CONFIG.locations missing")
local lutLocations = tools.createLookupTable(locations)
device.config = nil
device.location = nil
for _, cfgDevice in pairs(CONFIG.devices) do
if serial == tostring(cfgDevice.serial) then
device.config = cfgDevice
device.location = lutLocations[cfgDevice.location]
tools.debugPrint(2, "I'm device '" .. serial .. "' and located at: " .. device.getLocation().id)
break
end
end
if not device.config then
tools.debugPrint(1, "WARNING: I'm device '" .. serial .. "' but am not listed in config")
end
end
if CONFIG then
device.updateConfig()
end
-------------------------------------------------------------------------------
print("device.lua loaded completely")
return device