-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.lua
More file actions
147 lines (118 loc) · 3.35 KB
/
common.lua
File metadata and controls
147 lines (118 loc) · 3.35 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
DEBUG = false
-- MQTT --
MQTT_RETAIN = 1
MQTT_NO_RETAIN = 0
MQTT_QOS_AT_LEAST_ONCE = 1
MQTT_QOS_EXACLY_ONCE = 2
__mqtt_base_topic = ""
__mqtt_on_connected = nil
__mqtt_on_message = nil
function _mqtt_connected(client)
m:on("offline", function(client)
prnt("offline")
dofile("init.lua")
end)
m:on("message", function(client, topic, data)
local data_json
prnt("mqtt topic RX: "..topic)
if (topic == __mqtt_base_topic.."/param_set") then
prnt("PARAM SET "..data)
data_json = sjson.decode(data)
for k,v in pairs(data_json) do
param_set(k, v)
end
elseif (topic == __mqtt_base_topic.."/reboot") then
prnt("/reboot recieved. Restarting")
node.restart()
else
__mqtt_on_message(client, topic, data)
end
end)
m:subscribe(__mqtt_base_topic.."/param_set", MQTT_QOS_EXACLY_ONCE)
m:subscribe(__mqtt_base_topic.."/reboot", MQTT_QOS_EXACLY_ONCE)
__mqtt_on_connected(client)
end
function mqtt_connect(client_name, on_connected_callback, on_message_callback, base_topic)
local mqtt_ip = param_get("mqtt_ip")
__mqtt_on_connected = on_connected_callback
__mqtt_on_message = on_message_callback
__mqtt_base_topic = base_topic
if (mqtt_ip == nil) then
print("MQTT ip is missing... aborting")
do return end
end
m = mqtt.Client(client_name, 120)
m:connect(mqtt_ip, 1883, 0, _mqtt_connected,
function(client, reason)
prnt("MQTT connection failed, reason: " .. reason)
dofile("init.lua")
end)
return m
end
-- CONSISTENT PARAMETERS --
PARAM_FILENAME_PREFIX = ".data_"
function param_set(name, value)
if value == nil or file == nil then do return end end
if file.open(PARAM_FILENAME_PREFIX..name, "w") then
file.writeline(tostring(value))
--file.close() # Seems like causing crash for some reason
else
prnt("Error opening "..PARAM_FILENAME_PREFIX..name)
end
end
function param_get(name)
local res
if file.open(PARAM_FILENAME_PREFIX..name, "r") then
res = file.readline()
file.close()
else
prnt("Error opening "..PARAM_FILENAME_PREFIX..name)
end
return res
end
-- LOGGING --
function prnt(text)
print(text)
end
function prntd(text)
if (DEBUG == true) then
prnt(text)
end
end
-- GPIOs --
_gpios_cb = {}
_gpios_timer = {}
_gpios_data = {}
_gpios_pending_data = {}
function gpio_register(name, on_value_change)
if _gpios_cb[name] ~= nil then
return nil
end
_gpios_cb[name] = on_value_change
_gpios_timer[name] = tmr.create()
return true
end
function gpio_update(name, value)
if _gpios_cb[name] == nil then
return nil
end
_gpios_data[name] = value
_gpios_cb[name](value)
end
function gpio_get(name)
return _gpios_data[name]
end
--Updates value only if stayed unchanged at least delay_time(ms)
function gpio_delayUpdate(name, value, delay_time)
if _gpios_cb[name] == nil or _gpios_pending_data[name] == value then
return nil
end
_gpios_pending_data[name] = value
if gpio_get(name) == value then
tmr.stop(_gpios_timer[name])
return nil
end
tmr.alarm(_gpios_timer[name], delay_time, tmr.ALARM_SINGLE, function()
gpio_update(name, value)
end)
end