Skip to content

Commit ddf49fc

Browse files
committed
[tmpod] sensor config publishing is now exclusively handled by tmpod
1 parent 8e6bb20 commit ddf49fc

File tree

3 files changed

+36
-51
lines changed

3 files changed

+36
-51
lines changed

openwrt/package/flukso/luasrc/kubed.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ local MOSQ_QOS1 = 1
6666
local MOSQ_RETAIN = true
6767

6868
local MOSQ_TOPIC_KUBE_CONFIG = string.format("/device/%s/config/kube", DEVICE)
69-
local MOSQ_TOPIC_SENSOR_CONFIG = string.format("/device/%s/config/sensor", DEVICE)
7069
local MOSQ_TOPIC_SENSOR = "/sensor/%s/%s"
7170

7271
local FMT_CMD_SET_GROUP = "sg %s"
@@ -209,8 +208,6 @@ local function load_config()
209208

210209
local kube = luci.json.encode(config_clean(KUBE))
211210
mqtt:publish(MOSQ_TOPIC_KUBE_CONFIG, kube, MOSQ_QOS0, MOSQ_RETAIN)
212-
local sensor = luci.json.encode(config_clean(SENSOR))
213-
mqtt:publish(MOSQ_TOPIC_SENSOR_CONFIG, sensor, MOSQ_QOS0, MOSQ_RETAIN)
214211

215212
if DEBUG.config then
216213
dbg.vardump(KUBE)

openwrt/package/flukso/luasrc/tmpod.lua

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ local MOSQ_QOS1 = 1
9494
local MOSQ_RETAIN = true
9595
local MOSQ_ERROR = "MQTT error: %s"
9696

97+
local MOSQ_TOPIC_SENSOR_CONFIG = string.format("/device/%s/config/sensor", DEVICE)
98+
9799
-- increase process niceness
98100
nixio.nice(TMPO_NICE)
99101

@@ -117,40 +119,54 @@ local config = {
117119
sensor = nil,
118120

119121
load = function(self)
120-
local function clean(itbl)
121-
local otbl = { }
122-
for sidx, params in pairs(itbl) do
123-
if tonumber(sidx) -- only interested in sensor entries
122+
local function clean(tbl)
123+
for section, params in pairs(tbl) do
124+
params[".index"] = nil
125+
params[".name"] = nil
126+
params[".type"] = nil
127+
params[".anonymous"] = nil
128+
129+
if tonumber(section) -- sensor entries only
124130
and params.enable
125131
and params.enable == "1"
126132
then
127-
otbl[params.id] = params
128-
params[".index"] = nil
129-
params[".name"] = nil
130-
params[".type"] = nil
131-
params[".anonymous"] = nil
132-
params.enable = nil
133-
params.counter = nil
134133
if not params.data_type then params.data_type = "counter" end
135134
if not params.rid then params.rid = 0 end
135+
end
136136

137-
for option, value in pairs(params) do
138-
params[option] = tonumber(value) or value
139-
if type(value) == "table" then -- dealing with a list
140-
for i in pairs(value) do
141-
value[i] = tonumber(value[i]) or value[i]
142-
end
143-
else
144-
params[option] = tonumber(value) or value
137+
for option, value in pairs(params) do
138+
params[option] = tonumber(value) or value
139+
if type(value) == "table" then -- we're dealing with a list
140+
for i in pairs(value) do
141+
value[i] = tonumber(value[i]) or value[i]
145142
end
146143
end
147144
end
148145
end
146+
return tbl
147+
end
148+
149+
local function purge(itbl)
150+
local otbl = { }
151+
for section, params in pairs(itbl) do
152+
if tonumber(section)
153+
and params.enable
154+
and params.enable == "1"
155+
then
156+
otbl[params.id] = params
157+
params.enable = nil
158+
params.counter = nil
159+
end
160+
end
149161
return otbl
150162
end
151163

152164
uci:load("flukso")
153-
self.sensor = clean(uci:get_all("flukso"))
165+
local sensor = clean(uci:get_all("flukso"))
166+
local jsensor = luci.json.encode(sensor)
167+
mqtt:publish(MOSQ_TOPIC_SENSOR_CONFIG, jsensor, MOSQ_QOS0, MOSQ_RETAIN)
168+
self.sensor = purge(sensor)
169+
154170
if DEBUG.config then dbg.vardump(self.sensor) end
155171
end
156172
}

openwrt/package/flukso/luasrc/wwd.lua

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ local MOSQ_MAX_PKTS = 10 -- packets
6767
local MOSQ_QOS0 = 0
6868
local MOSQ_QOS1 = 1
6969
local MOSQ_RETAIN = true
70-
local MOSQ_TOPIC_SENSOR_CONFIG = string.format("/device/%s/config/sensor", DEVICE)
7170
local MOSQ_TOPIC_SENSOR = "/sensor/%s/%s"
7271
local MOSQ_TOPIC_WW_UPGRADE = "/device/%s/ww/upgrade"
7372

@@ -542,32 +541,6 @@ local sensor = {
542541
local payload = luci.json.encode({ timestamp, bitfield, cfg.unit })
543542
mqtt:publish(topic, payload, MOSQ_QOS0, MOSQ_RETAIN)
544543
end
545-
end,
546-
547-
publish_cfg = function(self)
548-
local function config_clean(itbl)
549-
local otbl = luci.util.clone(itbl, true)
550-
for section, section_tbl in pairs(otbl) do
551-
section_tbl[".index"] = nil
552-
section_tbl[".name"] = nil
553-
section_tbl[".type"] = nil
554-
section_tbl[".anonymous"] = nil
555-
556-
for option, value in pairs(section_tbl) do
557-
section_tbl[option] = tonumber(value) or value
558-
if type(value) == "table" then -- we're dealing with a list
559-
for i in pairs(value) do
560-
value[i] = tonumber(value[i]) or value[i]
561-
end
562-
end
563-
end
564-
end
565-
566-
return otbl
567-
end
568-
569-
local flukso = luci.json.encode(config_clean(uci:get_all("flukso")))
570-
mqtt:publish(MOSQ_TOPIC_SENSOR_CONFIG, flukso, MOSQ_QOS0, MOSQ_RETAIN)
571544
end
572545
}
573546

@@ -605,7 +578,6 @@ local root = state {
605578
load_config = state {
606579
entry = function()
607580
sensor:load_cfg()
608-
sensor:publish_cfg()
609581
end
610582
},
611583

0 commit comments

Comments
 (0)