-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.lua
114 lines (104 loc) · 3.65 KB
/
main.lua
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
113
114
-- logging for debugging
-- local dprint = function(...) end -- OFF
local dprint = function(...) print(...) end -- ON
-- common module initialization
cron.schedule("*/5 * * * *", function(e) OVL["nwfnet-sntp"]().dosntp(nil) end)
local nwfnet = require "nwfnet"
-- MQTT plumbing
local mqttLocnTopic = string.format("ctfws/devc/%s/location",mqttUser)
local mqttBootTopic = string.format("ctfws/dev/%s/beat",mqttUser)
mqc:lwt(mqttBootTopic,"dead",1,1)
local myBSSID = "00:00:00:00:00:00"
local mqtt_reconn_cronentry
local function mqtt_reconn()
dprint("main", "Trying reconn...")
mqtt_reconn_cronentry = cron.schedule("* * * * *", function(e)
mqc:close(); OVL.nwfmqtt().connect(mqc,"nwfmqtt.conf")
end)
OVL.nwfmqtt().connect(mqc,"nwfmqtt.conf")
end
local mqtt_beat_tmr = tmr.create()
mqtt_beat_tmr:register(20000, tmr.ALARM_AUTO, function(t)
mqc:publish(mqttBootTopic,string.format("beat %d %s",rtctime.get(),myBSSID),1,1)
end)
nwfnet.onmqtt["main"] = function(c,t,m)
dprint("main", "MQTT", t, m)
if t == "ctfws/game/config" then
if not m or m == "none"
then ctfws:deconfig()
else local st, -- start time
sd, -- setup duration
nr, -- number of rounds
rd, -- round duration
nf, -- number of flags
gn, -- game number
tc -- territory configuration string
-- st sd nr rd nf gn tc
= m:match("^%s*(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%S+).*$")
if st == nil
then ctfws:deconfig()
else -- the game's afoot!
ctfws:config(tonumber(st), tonumber(sd), tonumber(nr),
tonumber(rd), tonumber(nf), tonumber(gn), tc)
end
end
elseif t == "ctfws/game/endtime" then
ctfws:setEndTime(tonumber(m))
elseif t == "ctfws/game/flags" then
if not m or m == "" then
ctfws:setFlags("?","?")
return
end
local ts, fr, fy = m:match("^%s*(%d+)%s+(-?%d+)%s+(-?%d+).*$")
if ts ~= nil then
ctfws:setFlags(tonumber(fr),tonumber(fy))
return
end
-- we used to match on the ? explicitly, as in:
-- if m:match("^%s*(%d+)%s+%?.*$") then ... end
-- but for now, let's just take any ill-formed message
ctfws:setFlags("?","?")
elseif t == mqttLocnTopic then
ctfws:setTerritory(m)
end
end
-- network callbacks
local wifidog_tmr = tmr.create()
local function install_wifidog()
wifidog_tmr:alarm(60000, tmr.ALARM_SINGLE, function()
print("No WiFi in 60 seconds; rebooting!")
node.restart()
end)
end
install_wifidog()
nwfnet.onnet["main"] = function(e,c)
dprint("main", "NET", e)
if e == "mqttdscn" and c == mqc then
mqtt_beat_tmr:stop()
if not mqtt_reconn_cronentry then mqtt_reconn() end
elseif e == "mqttconn" and c == mqc then
if mqtt_reconn_cronentry then mqtt_reconn_cronentry:unschedule() mqtt_reconn_cronentry = nil end
mqtt_beat_tmr:start()
mqc:publish(mqttBootTopic,"alive",1,1)
mqc:subscribe({
["ctfws/game/config"] = 2,
["ctfws/game/endtime"] = 2,
["ctfws/game/flags"] = 2,
[mqttLocnTopic] = 2, -- my location
})
elseif e == "wstagoip" then
if not mqtt_reconn_cronentry then mqtt_reconn() end
elseif e == "wstadscn" then
install_wifidog()
elseif e == "wstaconn" then
myBSSID = c.BSSID
wifidog_tmr:unregister()
elseif e == "sntpsync" then
-- If we have a game configuration and just got SNTP sync, it might
-- be that we just lept far into the future, so go ahead and start
-- the game!
if ctfws.startT then ctfws:reconfig() end
end
end
-- OVL["nwfnet-diag"]()(true)
OVL["nwfnet-go"]()