forked from steps39/dtgbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtg_domoticz.lua
256 lines (241 loc) · 8.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
-- 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 > 100 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')
decoded_response = JSON:decode(jresponse)
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 == nill 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)
decoded_response = JSON:decode(jresponse)
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 = {}
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
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(1,"JSON request <"..t..">");
jresponse, status = http.request(t)
decoded_response = JSON:decode(jresponse)
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 MaxDimLevel=100
local ridx=0
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
record = retrieve_status(idx,"devices")['result'][1]
print_to_log(2,'device ',DeviceName,record.Name,idx,record.idx)
if type(record) == "table" then
ridx = record.idx
rDeviceName = record.Name
DeviceType="devices"
Type=record.Type
-- 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.
if dtgbot_type_status[Type] ~= nil then
if dtgbot_type_status[Type].Status ~= nil then
status = ''
CurrentStatus = dtgbot_type_status[Type].Status
for i=1, #CurrentStatus do
if status ~= '' then
status = status .. ' - '
end
cindex, csuffix = next(CurrentStatus[i])
status = status .. tostring(record[cindex])..tostring(csuffix)
end
end
else
SwitchType=record.SwitchType
MaxDimLevel=record.MaxDimLevel
status = tostring(record.Status)
end
found = 1
print_to_log(2," !!!! found device",record.Name,rDeviceName,record.idx,ridx)
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 then
ridx = 9999
DeviceType="command"
Type="command"
SwitchType="command"
end
print_to_log(2," --< devinfo_from_name:",found,ridx,rDeviceName,DeviceType,Type,SwitchType,status)
return ridx,rDeviceName,DeviceType,Type,SwitchType,MaxDimLevel,status
end
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)
decoded_response = JSON:decode(jresponse)
local language = decoded_response['language']
if language ~= nil then
return language
else
return 'en'
end
end