-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toon_Water-02.lua
236 lines (182 loc) · 7.23 KB
/
Toon_Water-02.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
-- QUICKAPP TOON WATER
-- This Quickapp retrieves water consumption from the Toon Watermeter
-- The main devices containes the Water Flow (liters/minute)
-- This QuickApp has Child Device for Water Total (m³)
-- See http://www.toonwater.nl for the device and more info
-- Version 0.2 (20th February 2022)
-- Better check for bad responses Toon Water
-- Version 0.1 (1st January 2022)
-- Initial version
-- Variables (mandatory):
-- IPaddress = IP address of your Toonwater meter
-- Interval = Number in seconds
-- debugLevel = Number (1=some, 2=few, 3=all, 4=simulation mode) (default = 1)
-- No editing of this code is needed
class 'waterquantity'(QuickAppChild)
function waterquantity:__init(dev)
QuickAppChild.__init(self,dev)
end
function waterquantity:updateValue(data)
self:updateProperty("value", tonumber(data.waterquantity))
self:updateProperty("unit", "m³")
self:updateProperty("log", " ")
end
local function getChildVariable(child,varName)
for _,v in ipairs(child.properties.quickAppVariables or {}) do
if v.name==varName then return v.value end
end
return ""
end
-- QuickApp Functions
function QuickApp:updateChildDevices() -- Update Child Devices
for id,child in pairs(self.childDevices) do
child:updateValue(data)
end
end
function QuickApp:logging(level,text) -- Logging function for debug messages
if tonumber(debugLevel) >= tonumber(level) then
self:debug(text)
end
end
function QuickApp:updateProperties() -- Update the properties
self:logging(3,"QuickApp:updateProperties")
self:updateProperty("value", tonumber(data.waterflow))
self:updateProperty("unit", "l/m")
self:updateProperty("log", os.date("%d-%m-%Y %T"))
end
function QuickApp:updateLabels() -- Update the labels
self:logging(3,"QuickApp:updateLabels")
local labelText = ""
if debugLevel == 4 then
labelText = labelText .."SIMULATION MODE" .."\n\n"
end
labelText = labelText .."Water Flow: " ..data.waterflow .." liters/minute" .."\n"
labelText = labelText .."Total Water: " ..data.waterquantity .." m³" .."\n\n"
labelText = labelText ..os.date("%d-%m-%Y %T")
self:updateView("label1", "text", labelText)
self:logging(2,labelText)
end
function QuickApp:valuesToon() -- Get the values from json file
self:logging(3,"QuickApp:valuesToon")
data.waterflow = jsonTable.waterflow
data.waterquantity = tonumber(jsonTable.waterquantity)/1000
end
function QuickApp:simData() -- Simulate Toon Water
self:logging(3,"simData")
apiResult = '{"waterflow":"4","waterquantity":"635281"}'
jsonTable = json.decode(apiResult) -- Decode the json string from api to lua-table
self:valuesToon() -- Get the values for Toon Water
self:updateLabels() -- Update the labels
self:updateProperties() -- Update the properties
self:updateChildDevices() -- Update the Child Devices
self:logging(3,"SetTimeout " ..Interval .." seconds")
fibaro.setTimeout(Interval*1000, function()
self:simData()
end)
end
function QuickApp:getData() -- Get data from Toon Water
self:logging(3,"getData")
local url = "http://" ..IPaddress .."/json.html"
self:logging(3,"url: " ..url)
self.http:request(url, {
options = {
headers = {Accept = "application/json"}, method = 'GET'},
success = function(response)
self:logging(3,"Response status: " ..response.status)
self:logging(3,"Response data: " ..response.data)
if response.data == nil or response.data == "" or response.data == "[]" or response.status > 200 then -- Check for empty result
self:warning("Temporarily no production data from Toon Water")
return
end
jsonTable = json.decode(response.data) -- Decode the json string from api to lua-table
self:valuesToon() -- Get the values for Toon Water
self:updateLabels() -- Update the labels
self:updateProperties() -- Update the properties
self:updateChildDevices() -- Update the Child Devices
end,
error = function(error)
self:error("error: " ..json.encode(error))
self:updateProperty("log", "error: " ..json.encode(error))
end
})
self:logging(3,"SetTimeout " ..Interval .." seconds")
fibaro.setTimeout(Interval*1000, function() -- Checks every [Interval] seconds for new data
self:getData()
end)
end
function QuickApp:createVariables() -- Create all Variables
self:logging(3,"Start createVariables")
data = {}
data.waterflow = ""
data.waterquantity = ""
end
function QuickApp:getQuickappVariables() -- Get all Quickapp Variables or create them
IPaddress = self:getVariable("IPaddress")
Interval = tonumber(self:getVariable("Interval"))
debugLevel = tonumber(self:getVariable("debugLevel"))
-- Check existence of the mandatory variables, if not, create them with default values
if IPaddress == "" or IPaddress == nil then
IPaddress = "192.168.1.112" -- Default IPaddress
self:setVariable("IPaddress", IPaddress)
self:trace("Added QuickApp variable IPaddress")
end
if Interval == "" or Interval == nil then
Interval = "10" -- Default interval in seconds
self:setVariable("Interval", Interval)
self:trace("Added QuickApp variable Interval")
Interval = tonumber(Interval)
end
if debugLevel == "" or debugLevel == nil then
debugLevel = "1" -- Default value for debugLevel
self:setVariable("debugLevel",debugLevel)
self:trace("Added QuickApp variable debugLevel")
debugLevel = tonumber(debugLevel)
end
self:logging(3,"Interval: " ..Interval)
end
function QuickApp:setupChildDevices() -- Setup Child Devices
local cdevs = api.get("/devices?parentId="..self.id) or {} -- Pick up all Child Devices
function self:initChildDevices() end -- Null function, else Fibaro calls it after onInit()...
if #cdevs == 0 then -- If no Child Devices, create them
local initChildData = {
{className="waterquantity", name="Water Total", type="com.fibaro.multilevelSensor", value=0},
}
for _,c in ipairs(initChildData) do
local child = self:createChildDevice(
{name = c.name,
type=c.type,
value=c.value,
unit=c.unit,
initialInterfaces = {},
},
_G[c.className] -- Fetch class constructor from class name
)
child:setVariable("className",c.className) -- Save class name so we know when we load it next time
end
else
for _,child in ipairs(cdevs) do
local className = getChildVariable(child,"className") -- Fetch child class name
local childObject = _G[className](child) -- Create child object from the constructor name
self.childDevices[child.id]=childObject
childObject.parent = self -- Setup parent link to device controller
end
end
end
function QuickApp:onInit()
__TAG = fibaro.getName(plugin.mainDeviceId) .." ID:" ..plugin.mainDeviceId
self.http = net.HTTPClient({timeout=3000})
self:debug("onInit")
self:setupChildDevices() -- Setup the Child Devices
if not api.get("/devices/"..self.id).enabled then
self:warning("Device", fibaro.getName(plugin.mainDeviceId), "is disabled")
return
end
self:getQuickappVariables() -- Get Quickapp Variables or create them
self:createVariables() -- Create Variables
if tonumber(debugLevel) >= 4 then
self:simData() -- Go in simulation
else
self:getData() -- Get data
end
end
-- EOF