-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdriver.lua
795 lines (633 loc) · 17.6 KB
/
driver.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
WebSocketConnection = require("module.websocket")
Socket = nil
MessageID = 0
Connected = false
ForceDisconnect = false
UseSSL = false
CertificateDirectoryPrefix = "../../../../"
WrittenDriverCount = 0
EntityTable = {}
EC = {}
OPC = {}
RFP = {}
REQ = {}
function HandlerDebug(init, tParams, args)
if (not DEBUGPRINT) then
return
end
if (type(init) ~= 'table') then
return
end
local output = init
if (type(tParams) == 'table' and next(tParams) ~= nil) then
table.insert(output, '----PARAMS----')
for k, v in pairs(tParams) do
local line = tostring(k) .. ' = ' .. tostring(v)
table.insert(output, line)
end
end
if (type(args) == 'table' and next(args) ~= nil) then
table.insert(output, '----ARGS----')
for k, v in pairs(args) do
local line = tostring(k) .. ' = ' .. tostring(v)
table.insert(output, line)
end
end
local t, ms
if (C4.GetTime) then
t = C4:GetTime()
ms = '.' .. tostring(t % 1000)
t = math.floor(t / 1000)
else
t = os.time()
ms = ''
end
local s = os.date('%x %X') .. ms
table.insert(output, 1, '--> ' .. s)
table.insert(output, '<--')
output = table.concat(output, '\r\n')
print(output)
C4:DebugLog(output)
end
function ExecuteCommand(strCommand, tParams)
tParams = tParams or {}
local init = {
'ExecuteCommand: ' .. strCommand,
}
HandlerDebug(init, tParams)
if (strCommand == 'LUA_ACTION') then
if (tParams.ACTION) then
strCommand = tParams.ACTION
tParams.ACTION = nil
end
end
strCommand = string.gsub(strCommand, '%s+', '_')
local success, ret
if (EC and EC[strCommand] and type(EC[strCommand]) == 'function') then
success, ret = pcall(EC[strCommand], tParams)
end
if (success == true) then
return (ret)
elseif (success == false) then
print('ExecuteCommand error: ', ret, strCommand)
end
end
function OnPropertyChanged(strProperty)
local value = Properties[strProperty]
if (type(value) ~= 'string') then
value = ''
end
local init = {
'OnPropertyChanged: ' .. strProperty,
value,
}
HandlerDebug(init)
strProperty = string.gsub(strProperty, '%s+', '_')
local success, ret
if (OPC and OPC[strProperty] and type(OPC[strProperty]) == 'function') then
success, ret = pcall(OPC[strProperty], value)
end
if (success == true) then
return (ret)
elseif (success == false) then
print('OnPropertyChanged error: ', ret, strProperty, value)
end
end
function ReceivedFromProxy(idBinding, strCommand, tParams)
strCommand = strCommand or ''
tParams = tParams or {}
local args = {}
if (tParams.ARGS) then
local parsedArgs = C4:ParseXml(tParams.ARGS)
for _, v in pairs(parsedArgs.ChildNodes) do
args[v.Attributes.name] = v.Value
end
tParams.ARGS = nil
end
local init = {
'ReceivedFromProxy: ' .. idBinding,
strCommand,
}
HandlerDebug(init, tParams, args)
local success, ret
if (RFP and RFP[strCommand] and type(RFP[strCommand]) == 'function') then
success, ret = pcall(RFP[strCommand], idBinding, strCommand, tParams, args)
elseif (RFP and RFP[idBinding] and type(RFP[idBinding]) == 'function') then
success, ret = pcall(RFP[idBinding], idBinding, strCommand, tParams, args)
end
if (success == true) then
return (ret)
elseif (success == false) then
print('ReceivedFromProxy error: ', ret, idBinding, strCommand)
end
end
function OnDriverInit()
print("--driver init--")
C4:AddVariable("HA URL", "", "STRING")
end
function OnDriverLateInit(DIT)
print("--late init--")
if DIT ~= "DIT_ADDING" then
SetTimer('WaitForConnectAdd', 2 * ONE_SECOND, EC.WS_CONNECT())
end
C4:SetPropertyAttribs("Directory Start Path", 1)
C4:SetPropertyAttribs("Certificate Path", 1)
C4:SetPropertyAttribs("Private Key Path", 1)
C4:SetPropertyAttribs("CA Certificate Path", 1)
local version = C4:GetDriverConfigInfo('version')
C4:UpdateProperty('Driver Version', version)
OPC.Use_SSL(Properties["Use SSL"])
end
function OnDriverDestroyed()
print("--destroyed--")
Connected = false
C4:FireEvent('Home Assistant Disconnected')
if Socket ~= nil then
Socket:Close()
end
end
function OnBindingChanged(idBinding, strClass, bIsBound)
if idBinding == 1 or idBinding == 6001 then
print("BINDING CHANGED: " .. tostring(idBinding))
if bIsBound == true then
UpdateEntityIDs(false)
else
UpdateEntityIDs(true)
end
end
end
function UpdateEntityIDs(reconnect)
EntityTable = {}
local consumerDevices = C4:GetBoundConsumerDevices(0, 1)
if consumerDevices == nil then
if reconnect == true then
EC.WS_CONNECT()
end
return
end
local stringTable = {}
for deviceId, _ in pairs(consumerDevices) do
table.insert(stringTable, deviceId)
end
local deviceIdsString = table.concat(stringTable, ",")
local devicesFilter = { DeviceIds = deviceIdsString }
local getDevicesResult = C4:GetDevices(devicesFilter)
local debugString = "\n"
for deviceId, driverInfo in pairs(getDevicesResult) do
local driverFileName = driverInfo.driverFileName
debugString = debugString .. ("Query Connected Driver: " .. driverFileName) .. "\n"
local strValues = C4:SendUIRequest(deviceId, "GET_PROPERTIES_SYNC", {}, true)
if (not strValues) then
strValues = C4:SendUIRequest(deviceId, "GET_PROPERTIES", {}, true)
end
for property in string.gmatch(strValues, "<property>(.-)</property>") do
local name = XMLCapture(property, "name")
if name == "Entity ID" then
local value = XMLCapture(property, "value")
EntityTable[value] = deviceId
debugString = debugString .. ("-- Add entity to EntityTable: " .. value .. " --\n")
end
end
end
print(debugString)
if reconnect == true then
EC.WS_CONNECT()
end
end
function UIRequest(strCommand, tParams)
local success, ret
if (REQ and REQ[strCommand] and type(REQ[strCommand]) == 'function') then
success, ret = pcall(REQ[strCommand], strCommand, tParams)
end
if (success == true) then
return (ret)
elseif (success == false) then
print('UIRequest error: ', ret, strCommand)
end
end
function SocketSendTable(table)
if Connected == true then
MessageID = MessageID + 1
table.id = MessageID
end
SocketSendMessage(JSON:encode(table))
end
function SocketSendMessage(message)
if Socket == nil then
return
end
if DEBUGPRINT then
print("--SEND:-- " .. message)
end
Socket:Send(message)
end
function OPC.Home_Assistant_URL(value)
EC.WS_CONNECT()
C4:SetVariable("HA URL", tostring(value))
end
function OPC.Long_Lived_Access_Token(value)
EC.WS_CONNECT()
end
function OPC.Debug_Print(value)
CancelTimer('DEBUGPRINT')
DEBUGPRINT = (value == 'On')
if (DEBUGPRINT) then
local _timer = function(timer)
C4:UpdateProperty('Debug Print', 'Off')
OnPropertyChanged('Debug Print')
end
SetTimer('DEBUGPRINT', 36000000, _timer)
end
end
function OPC.Use_SSL(value)
UseSSL = (value == "Yes")
local showPropertyValue = 1
if UseSSL then showPropertyValue = 0 end
C4:SetPropertyAttribs("Directory Start Path", showPropertyValue)
C4:SetPropertyAttribs("Certificate Path", showPropertyValue)
C4:SetPropertyAttribs("Private Key Path", showPropertyValue)
C4:SetPropertyAttribs("CA Certificate Path", showPropertyValue)
EC.WS_CONNECT()
end
function OPC.Directory_Start_Path(value)
if value == "Driver" then
CertificateDirectoryPrefix = "./"
else
CertificateDirectoryPrefix = "../../../../"
end
EC.WS_CONNECT()
end
function OPC.Certificate_Path(value)
EC.WS_CONNECT()
end
function OPC.Private_Key_Path(value)
EC.WS_CONNECT()
end
function OPC.CA_Certificate_Path(value)
EC.WS_CONNECT()
end
function EC.PRINT_ENTITY_TABLE()
print("-- Force Update Entities --")
UpdateEntityIDs(false)
print("-- EntityTable --")
for k, _ in pairs(EntityTable) do
print("-- Entity: " .. k .. " --")
end
end
function EC.UPDATE_CONNECTED_DRIVERS()
local totalDrivers = 0
local downloadedDrivers = {}
WrittenDriverCount = 0
local consumerDevices = C4:GetBoundConsumerDevices(0, 1)
local stringTable = {}
for deviceId, _ in pairs(consumerDevices) do
table.insert(stringTable, deviceId)
end
local deviceIdsString = table.concat(stringTable, ",")
local devicesFilter = { DeviceIds = deviceIdsString }
local getDevicesResult = C4:GetDevices(devicesFilter)
for deviceId, driverInfo in pairs(getDevicesResult) do
local driverFileName = driverInfo.driverFileName
if (string.find(driverFileName, "HA")) then
if (downloadedDrivers[driverFileName] == nil) then
totalDrivers = totalDrivers + 1
downloadedDrivers[driverFileName] = deviceId
end
end
end
for driverFileName, deviceId in pairs(downloadedDrivers) do
local repoName = driverFileName:match("^(.-)%.c4z"):gsub(" ", "-")
local modifiedName = driverFileName:gsub(" ", ".")
local baseURL = "https://github.com/bphillips09/Control4-" .. repoName
local suffixURL = "/releases/latest/download/" .. modifiedName
local finalURL = baseURL .. suffixURL
local result =
function(driverBody, finalDriver)
if driverBody == nil or driverBody == "" then
print("-- Driver Download Error for driver: " .. driverFileName .. " --")
downloadedDrivers[driverFileName] = nil
WrittenDriverCount = WrittenDriverCount + 1
else
print("-- Downloaded Driver: " .. driverFileName .. " --")
local dir = C4:GetC4zDir()
local fileWriter = io.open(dir .. driverFileName, "w")
if fileWriter ~= nil and fileWriter ~= -1 then
fileWriter:write(driverBody)
fileWriter:close()
print("-- Wrote driver successfully: " .. driverFileName .. " --")
WrittenDriverCount = WrittenDriverCount + 1
print("Wrote " .. WrittenDriverCount .. " of " .. totalDrivers)
if WrittenDriverCount == totalDrivers then
InstallDrivers(downloadedDrivers)
end
end
end
end
GetFile(finalURL, result)
end
end
function EC.WS_CONNECT()
UpdateEntityIDs(false)
Sleep(1)
if Connected == true then
Disconnect()
SetTimer('WaitToShowStatus', 2 * ONE_SECOND, ShowDelayedStatus("Reconnecting..."))
SetTimer('WaitForConnect', 10 * ONE_SECOND, Connect())
return
end
Watchdog()
end
function Watchdog()
CancelTimer('WatchdogTimer')
SetTimer('WatchdogTimer', ONE_SECOND * 30, Watchdog)
Connect()
end
function ShowDelayedStatus(status)
C4:UpdateProperty('Status', status)
end
function EC.WS_DISCONNECT()
CancelTimer('WaitToShowStatus')
CancelTimer('WaitForConnect')
CancelTimer('WaitForConnectAdd')
CancelTimer('AuthTimer')
CancelTimer('WatchdogTimer')
Disconnect()
end
function Connect()
if Connected == true or Properties["Long Lived Access Token"] == "" then
return
end
local HA_URL = Properties["Home Assistant URL"]
if HA_URL == nil or HA_URL == "" then
return
end
local prefix = "ws://"
local headers = nil
local tParams = {}
if UseSSL then
prefix = "wss://"
tParams = {
CERTIFICATE = CertificateDirectoryPrefix .. Properties["Certificate Path"],
PRIVATE_KEY = CertificateDirectoryPrefix .. Properties["Private Key Path"],
CACERTFILE = CertificateDirectoryPrefix .. Properties["CA Certificate Path"]
}
end
local url = prefix .. HA_URL .. "/api/websocket"
Socket = WebSocketConnection:new(url, headers, tParams)
if Socket ~= nil then
print("Connecting...")
C4:UpdateProperty('Status', "Connecting...")
Socket:SetProcessMessageFunction(ReceieveMessage)
Socket:SetClosedByRemoteFunction(ConnectionStopped)
Socket:SetOfflineFunction(ConnectionStopped)
Socket:Start()
SetTimer('AuthTimer', ONE_SECOND * 5, AutoAuth)
end
end
function AutoAuth()
if not Connected then
print("Trying Auto Auth...")
local tParams = {
type = "auth",
access_token = Properties["Long Lived Access Token"]
}
SocketSendTable(tParams)
end
end
function Disconnect()
if Connected then
C4:UpdateProperty('Status', "Disconnecting...")
else
C4:UpdateProperty('Status', "Disconnected")
end
ForceDisconnect = true
Connected = false
C4:FireEvent('Home Assistant Disconnected')
if Socket ~= nil then
Socket:Close()
end
end
function EC.Call_Service(tParams)
local splitTable = Split(tParams["Service"], ".")
local dataString = tParams["Data"]
local variables = {
VAR1 = tParams["${VAR1}"] or "",
VAR2 = tParams["${VAR2}"] or "",
VAR3 = tParams["${VAR3}"] or ""
}
local data = JSON:decode(InterpolateString(dataString, variables)) or {}
if data == "" then
data = {}
end
local serviceCall = {
type = "call_service",
domain = splitTable[1],
service = splitTable[2],
service_data = data,
target = {
entity_id = tParams["Entity ID"]
}
}
SocketSendTable(serviceCall)
end
function RFP.HA_CALL_SERVICE(idBinding, strCommand, tParams)
tParams = JSON:decode(tParams.JSON)
tParams["type"] = "call_service"
SocketSendTable(tParams)
end
function RFP.HA_GET_STATE(idBinding, strCommand, tParams)
CallHomeAssistantAPI("states/" .. tParams.entity)
if EntityTable[tParams.entity] == nil then
local params = {
type = "subscribe_trigger",
trigger = {
platform = "state",
entity_id = tParams.entity
}
}
SocketSendTable(params)
end
EntityTable[tParams.entity] = ""
end
function ReceieveMessage(socket, data)
if DEBUGPRINT then
print("--RECV:-- " .. tostring(data))
end
local jsonData = JSON:decode(data)
local tParams = {}
if jsonData then
if jsonData.type == "auth_required" then
Connected = false
C4:UpdateProperty('Status', "Waiting for Auth...")
tParams = {
type = "auth",
access_token = Properties["Long Lived Access Token"]
}
SocketSendTable(tParams)
elseif jsonData.type == "auth_ok" then
Connected = true
print("Connected!")
C4:UpdateProperty('Status', "Connected")
C4:UpdateProperty('HA Version', jsonData.ha_version)
C4:FireEvent('Home Assistant Connected')
local entityArray = {}
for key in pairs(EntityTable) do
table.insert(entityArray, key)
end
tParams = {
type = "subscribe_trigger",
trigger = {
platform = "state",
entity_id = entityArray
}
}
SocketSendTable(tParams)
elseif jsonData.type == "event" then
if Connected == false then
Connected = true
print("Connected!")
C4:UpdateProperty('Status', "Connected")
C4:FireEvent('Home Assistant Connected')
end
if jsonData.event ~= nil and jsonData.event.data == nil and jsonData.event.variables ~= nil then
local newData = {
event = {
data = {
new_state = jsonData["event"]["variables"]["trigger"]["to_state"]
}
}
}
data = JSON:encode(newData)
end
tParams = {
data = data
}
C4:SendToProxy(1, "RECEIEVE_EVENT", tParams)
end
end
end
function ConnectionStopped(socket, data)
Connected = false
print("Disconnected...")
C4:FireEvent('Home Assistant Disconnected')
C4:UpdateProperty('Status', "Disconnected")
if ForceDisconnect == false then
C4:UpdateProperty('Status', "Waiting to retry connection...")
end
if ForceDisconnect == true then
ForceDisconnect = false
end
end
function CallHomeAssistantAPI(endpoint)
local endUrl = "http://" .. Properties["Home Assistant URL"] .. "/api/" .. endpoint
local headers = {
["Authorization"] = "Bearer " .. Properties["Long Lived Access Token"],
["Content-Type"] = "application/json"
}
local t = C4:url()
:OnDone(
function(transfer, responses, errCode, errMsg)
if (errCode == 0) then
local lresp = responses[#responses]
if lresp.code == 200 then
local tParams = {
response = lresp.body
}
C4:SendToProxy(1, "RECEIEVE_STATE", tParams)
end
else
if (errCode == -1) then
print("Transfer aborted")
else
print("Transfer failed with error " ..
errCode .. ": " .. errMsg .. " (" .. #responses .. " responses completed)")
end
end
end)
:SetOptions({
["fail_on_error"] = false,
["timeout"] = 15,
["connect_timeout"] = 10
})
:Get(endUrl, headers)
end
function GetFile(url, callback)
local t = C4:url()
:OnDone(
function(transfer, responses, errCode, errMsg)
if (errCode == 0) then
local lresp = responses[#responses]
if lresp.code == 200 then
if callback then
callback(lresp.body)
end
else
print("Invalid response: " .. lresp.code)
if callback then
callback("")
end
end
else
if callback then
callback("")
end
if (errCode == -1) then
print("Transfer aborted")
else
print("Transfer failed with error " ..
errCode .. ": " .. errMsg .. " (" .. #responses .. " responses completed)")
end
end
end)
:SetOptions({
["fail_on_error"] = false,
["timeout"] = 15,
["connect_timeout"] = 10
})
:Get(url)
end
function InstallDrivers(driverList)
for driverFilename, _ in pairs(driverList) do
C4:CreateTCPClient()
:OnConnect(function(client)
print("-- Request update driver on director: " .. driverFilename .. " --")
local c4soap =
'<c4soap name="UpdateProjectC4i" session="0" operation="RWX" category="composer" async="False"><param name="name" type="string">' ..
driverFilename .. '</param></c4soap>' .. string.char(0)
client:Write(c4soap)
client:Close()
end)
:OnError(function(client, errCode, errMsg)
client:Close()
print("-- Error Instructing Director: " .. errCode .. ": " .. errMsg .. " --")
end)
:Connect("127.0.0.1", 5020)
Sleep(0.5)
end
end
function Sleep(a)
local sec = tonumber(os.clock() + a)
while (os.clock() < sec) do
end
end
function Split(inputstr, sep)
sep = sep or '%s'
local t = {}
for field, s in string.gmatch(inputstr, "([^" .. sep .. "]*)(" .. sep .. "?)") do
table.insert(t, field)
if s == "" then return t end
end
end
function InterpolateString(str, variables)
local function replaceVariable(s)
local valid_variable = variables[s]
if valid_variable == nil then
return s
else
local variable_ids = Split(valid_variable, ",")
return C4:GetVariable(variable_ids[1], variable_ids[2])
end
end
local result = str:gsub("%${(%w+)}", replaceVariable)
return result
end