-
Notifications
You must be signed in to change notification settings - Fork 18
/
dtg_domoticz.lua
369 lines (351 loc) · 12.1 KB
/
dtg_domoticz.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
-- A set of support functions currently aimed at dtgbot,
-- but probably more general
function form_device_name(parsed_cli)
-- joins together parameters after the command name to form the full "device name"
command = parsed_cli[2]
DeviceName = parsed_cli[3]
len_parsed_cli = #parsed_cli
if len_parsed_cli > 3 then
for i = 4, len_parsed_cli do
DeviceName = DeviceName..' '..parsed_cli[i]
end
end
return DeviceName
end
-- returns list of all user variables - called early by dtgbot
-- in case Domoticz is not running will retry
-- allowing Domoticz time to start
function variable_list()
local t, jresponse, status, decoded_response
t = server_url.."/json.htm?type=command¶m=getuservariables"
jresponse = nil
domoticz_tries = 1
-- Domoticz seems to take a while to respond to getuservariables after start-up
-- So just keep trying after 1 second sleep
while (jresponse == nil) do
print_to_log(1,"JSON request <"..t..">");
jresponse, status = http.request(t)
if (jresponse == nil) then
socket.sleep(1)
domoticz_tries = domoticz_tries + 1
if domoticz_tries > 20 then
print_to_log(0,'Domoticz not sending back user variable list')
break
end
end
end
print_to_log(0,'Domoticz returned getuservariables after '..domoticz_tries..' attempts')
if jresponse ~= nil then
decoded_response = JSON:decode(jresponse)
else
decoded_response = {}
decoded_response["result"] = "{}"
end
return decoded_response
end
-- returns idx of a user variable from name
function variable_list_names_idxs()
local idx, k, record, decoded_response
decoded_response = variable_list()
result = decoded_response["result"]
variables = {}
for i = 1, #result do
record = result[i]
if type(record) == "table" then
variables[record['Name']] = record['idx']
end
end
return variables
end
function idx_from_variable_name(DeviceName)
return Variablelist[DeviceName]
end
-- returns the value of the variable from the idx
function get_variable_value(idx)
local t, jresponse, decoded_response
if idx == nil then
return ""
end
t = server_url.."/json.htm?type=command¶m=getuservariable&idx="..tostring(idx)
print_to_log(1,"JSON request <"..t..">");
jresponse, status = http.request(t)
decoded_response = JSON:decode(jresponse)
print_to_log(0,'Decoded '..decoded_response["result"][1]["Value"])
return decoded_response["result"][1]["Value"]
end
function set_variable_value(idx, name, Type, value)
-- store the value of a user variable
local t, jresponse, decoded_response
t = server_url.."/json.htm?type=command¶m=updateuservariable&idx="..idx.."&vname="..name.."&vtype="..Type.."&vvalue="..tostring(value)
print_to_log(1,"JSON request <"..t..">");
jresponse, status = http.request(t)
return
end
function create_variable(name, Type, value)
-- creates user variable
local t, jresponse, decoded_response
t = server_url.."/json.htm?type=command¶m=saveuservariable&vname="..name.."&vtype="..Type.."&vvalue="..tostring(value)
print_to_log(1,"JSON request <"..t..">");
jresponse, status = http.request(t)
return
end
function get_names_from_variable(DividedString)
Names = {}
for Name in string.gmatch(DividedString, "[^|]+") do
Names[#Names + 1] = Name
print_to_log(1,'Name :'..Name)
end
if Names == {} then
Names = nil
end
return Names
end
-- returns a device table of Domoticz items based on type i.e. devices or scenes
function device_list(DeviceType)
local t, jresponse, status, decoded_response
t = server_url.."/json.htm?type="..DeviceType.."&order=name&used=true"
print_to_log(1,"JSON request <"..t..">");
jresponse, status = http.request(t)
if jresponse ~= nil then
decoded_response = JSON:decode(jresponse)
else
decoded_response = {}
decoded_response["result"] = "{}"
end
return decoded_response
end
-- returns a list of Domoticz items based on type i.e. devices or scenes
function device_list_names_idxs(DeviceType)
--returns a devcie idx based on its name
local idx, k, record, decoded_response
decoded_response = device_list(DeviceType)
result = decoded_response['result']
devices = {}
devicesproperties = {}
if result ~= nil then
for i = 1, #result do
record = result[i]
if type(record) == "table" then
if DeviceType == "plans" then
devices[record['Name']] = record['idx']
else
devices[string.lower(record['Name'])] = record['idx']
devices[record['idx']] = record['Name']
if DeviceType == 'scenes' then
devicesproperties[record['idx']] = {Type=record['Type'], SwitchType = record['Type']}
end
end
end
end
else
print_to_log(0," !!!! device_list_names_idxs(): nothing found for ",DeviceType)
end
return devices, devicesproperties
end
function idx_from_name(DeviceName,DeviceType)
--returns a devcie idx based on its name
if DeviceType == "devices" then
return Devicelist[string.lower(DeviceName)]
elseif DeviceType == "scenes" then
return Scenelist[string.lower(DeviceName)]
else
return Roomlist[DeviceName]
end
end
function retrieve_status(idx,DeviceType)
local t, jresponse, status, decoded_response
t = server_url.."/json.htm?type="..DeviceType.."&rid="..tostring(idx)
print_to_log(2,"JSON request <"..t..">");
jresponse, status = http.request(t)
if jresponse ~= nil then
decoded_response = JSON:decode(jresponse)
else
decoded_response = {}
decoded_response['result'] = ""
end
return decoded_response
end
-- support function to scan through the Devices and Scenes idx tables and retrieve the required information for it
function devinfo_from_name(idx,DeviceName,DeviceScene)
local k, record, Type,DeviceType,SwitchType
local found = 0
local rDeviceName=""
local status=""
local LevelNames=""
local LevelInt=0
local MaxDimLevel=100
local ridx=0
local tvar
if DeviceScene~="scenes" then
-- Check for Devices
-- Have the device name
if DeviceName ~= "" then
idx = idx_from_name(DeviceName,'devices')
end
print_to_log(2,"==> start devinfo_from_name", idx,DeviceName)
if idx ~= nil then
tvar = retrieve_status(idx, "devices")['result']
if tvar == nil then
found = 9
else
record = tvar[1]
if record ~= nil and record.Name ~= nil and record.idx ~= nil then
print_to_log(2,'device ',DeviceName,record.Name,idx,record.idx)
end
if type(record) == "table" then
ridx = record.idx
rDeviceName = record.Name
DeviceType="devices"
Type=record.Type
LevelInt=record.LevelInt
if LevelInt == nil then LevelInt = 0 end
LevelNames=record.LevelNames
if LevelNames == nil then LevelNames = "" end
-- as default simply use the status field
-- use the dtgbot_type_status to retrieve the status from the "other devices" field as defined in the table.
print_to_log(2,'Type ',Type)
if dtgbot_type_status[Type] ~= nil then
print_to_log(2,'dtgbot_type_status[Type] ',dtgbot_type_status[Type])
if dtgbot_type_status[Type].Status ~= nil then
status = ''
CurrentStatus = dtgbot_type_status[Type].Status
print_to_log(2,'CurrentStatus ',CurrentStatus)
for i=1, #CurrentStatus do
if status ~= '' then
status = status .. ' - '
end
cindex, csuffix = next(CurrentStatus[i])
status = status .. tostring(record[cindex])..tostring(csuffix)
print_to_log(2,'status ',status)
end
end
else
SwitchType=record.SwitchType
-- Check for encoded selector LevelNames
if SwitchType == "Selector" then
if string.find(LevelNames, "[|,]+") then
print_to_log(2, "-- < 4.9700 selector switch levelnames: ",LevelNames)
else
LevelNames=mime.unb64(LevelNames)
print_to_log(2, "-- >= 4.9700 decoded selector switch levelnames: ",LevelNames)
end
end
MaxDimLevel=record.MaxDimLevel
status = tostring(record.Status)
end
found = 1
--~ print_to_log(2," !!!! found device",record.Name,rDeviceName,record.idx,ridx)
end
end
end
--~ print_to_log(2," !!!! found device",rDeviceName,ridx)
end
-- Check for Scenes
if found == 0 then
if DeviceName ~= "" then
idx = idx_from_name(DeviceName,'scenes')
else
DeviceName = idx_from_name(idx,'scenes')
end
if idx ~= nil then
DeviceName = Scenelist[idx]
DeviceType="scenes"
ridx = idx
rDeviceName = DeviceName
SwitchType = Sceneproperties[tostring(idx)]['SwitchType']
Type = Sceneproperties[tostring(idx)]['Type']
found = 1
end
end
-- Check for Scenes
if found == 0 or found == 9 then
ridx = 9999
DeviceType="command"
Type="command"
SwitchType="command"
end
print_to_log(2," --< devinfo_from_name:",found,ridx,rDeviceName,DeviceType,Type,SwitchType,status,LevelNames,LevelInt)
return ridx,rDeviceName,DeviceType,Type,SwitchType,MaxDimLevel,status,LevelNames,LevelInt
end
-- Switch functions
function SwitchID(DeviceName, idx, DeviceType, state, SendTo)
if string.lower(state) == "on" then
state = "On";
elseif string.lower(state) == "off" then
state = "Off";
else
return "state must be on or off!";
end
t = server_url.."/json.htm?type=command¶m=switch"..DeviceType.."&idx="..idx.."&switchcmd="..state;
print_to_log (1,"JSON request <"..t..">");
jresponse, status = http.request(t)
print_to_log(1,"raw jason", jresponse)
response = 'Switched '..DeviceName..' '..state
return response
end
function sSwitchName(DeviceName, DeviceType, SwitchType,idx,state)
local status
if idx == nil then
response = 'Device '..DeviceName..' not found.'
else
local subgroup = "light"
if DeviceType == "scenes" then
subgroup = "scene"
end
if string.lower(state) == "on" then
state = "On";
t = server_url.."/json.htm?type=command¶m=switch"..subgroup.."&idx="..idx.."&switchcmd="..state;
elseif string.lower(state) == "off" then
state = "Off";
t = server_url.."/json.htm?type=command¶m=switch"..subgroup.."&idx="..idx.."&switchcmd="..state;
elseif string.lower(string.sub(state,1,9)) == "set level" then
t = server_url.."/json.htm?type=command¶m=switch"..subgroup.."&idx="..idx.."&switchcmd=Set%20Level&level="..string.sub(state,11)
else
return "state must be on, off or set level!";
end
print_to_log(3,"JSON request <"..t..">");
jresponse, status = http.request(t)
print_to_log(3,"JSON feedback: ", jresponse)
response = translate_desc(language,"Switched") .. ' ' ..(DeviceName or "?")..' => '..(state or "?")
end
print_to_log(0," -< sSwitchName:",DeviceName,idx, status,response)
return response, status
end
-- other functions
function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
function domoticz_language()
local t, jresponse, status, decoded_response
t = server_url.."/json.htm?type=command¶m=getlanguage"
jresponse = nil
print_to_log(1,"JSON request <"..t..">");
jresponse, status = http.request(t)
if jresponse ~= nil then
decoded_response = JSON:decode(jresponse)
else
decoded_response = {}
decoded_response["result"] = "{}"
end
local language = decoded_response['language']
if language ~= nil then
return language
else
return 'en'
end
end
-- get translation
function translate_desc(language,input,default)
language = language or "en"
input = input or "?"
local response = default or input
if (dtgmenu_lang[language] == nil) then
print_to_log(0," - Language not defined in config", language)
elseif (dtgmenu_lang[language].text[input] == nil) then
print_to_log(0," - Language keyword not defined in config:", language, input)
else
response = dtgmenu_lang[language].text[input]
end
return response
end