-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.lua
239 lines (224 loc) · 9.24 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
-------------
-- Globals --
-------------
do
EC = {}
OPC = {}
g_debugMode = 0
g_DbgPrint = nil
DIRECTOR_BINDING = 6001
DIRECTOR_PORT = 5020
end
----------------------------------------------------------------------------
--Function Name : OnDriverInit
--Description : Function invoked when a driver is loaded or being updated.
----------------------------------------------------------------------------
function OnDriverInit()
C4:UpdateProperty("Driver Name", C4:GetDriverConfigInfo("name"))
C4:UpdateProperty("Driver Version", C4:GetDriverConfigInfo("version"))
C4:AllowExecute(true)
end
------------------------------------------------------------------------------------------------
--Function Name : OnDriverLateInit
--Description : Function that serves as a callback into a project after the project is loaded.
------------------------------------------------------------------------------------------------
function OnDriverLateInit()
for k,v in pairs(Properties) do OnPropertyChanged(k) end
end
-----------------------------------------------------------------------------------------------------------------------------
--Function Name : OnDriverDestroyed
--Description : Function called when a driver is deleted from a project, updated within a project or Director is shut down.
-----------------------------------------------------------------------------------------------------------------------------
function OnDriverDestroyed()
if (g_DbgPrint ~= nil) then g_DbgPrint:Cancel() end
end
--------------------------------------------------------------------------
--Function Name : OnConnectionStatusChanged
--Description : Function called when a driver connection status changes.
--------------------------------------------------------------------------
function OnConnectionStatusChanged(idBinding, nPort, strStatus)
if (strStatus == "ONLINE") then
-- Send PIP command (refresh navs)
C4:SendToNetwork(DIRECTOR_BINDING, DIRECTOR_PORT, '<c4soap name="PIP" async="1"></c4soap>' .. string.char(0))
C4:NetDisconnect(DIRECTOR_BINDING, DIRECTOR_PORT)
end
end
----------------------------------------------------------------------------
--Function Name : OnPropertyChanged
--Parameters : strProperty(str)
--Description : Function called by Director when a property changes value.
----------------------------------------------------------------------------
function OnPropertyChanged(strProperty)
Dbg("OnPropertyChanged: " .. strProperty .. " (" .. Properties[strProperty] .. ")")
local propertyValue = Properties[strProperty]
if (propertyValue == nil) then propertyValue = "" end
local strProperty = string.upper(strProperty)
strProperty = string.gsub(strProperty, "%s+", "_")
local success, ret
if (OPC and OPC[strProperty] and type(OPC[strProperty]) == "function") then
success, ret = pcall(OPC[strProperty], propertyValue)
end
if (success == true) then
return (ret)
elseif (success == false) then
print ("OnPropertyChanged Lua error: ", strProperty, ret)
end
end
-------------------------------------------------------------------------
--Function Name : OPC.DEBUG_MODE
--Parameters : strProperty(str)
--Description : Function called when Debug Mode property changes value.
-------------------------------------------------------------------------
function OPC.DEBUG_MODE(strProperty)
if (strProperty == "Off") then
if (g_DbgPrint ~= nil) then g_DbgPrint:Cancel() end
g_debugMode = 0
print ("Debug Mode: Off")
else
g_debugMode = 1
print ("Debug Mode: On for 8 hours")
g_DbgPrint = C4:SetTimer(28800000, function(timer)
C4:UpdateProperty("Debug Mode", "Off")
timer:Cancel()
end, false)
end
end
---------------------------------------------------------------------------------
--Function Name : OPC.REFRESH_NAVIGATORS
--Parameters : strProperty(str)
--Description : Function called when Refresh Navigators property changes value.
---------------------------------------------------------------------------------
function OPC.REFRESH_NAVIGATORS(strProperty)
if (strProperty == "On") then
C4:CreateNetworkConnection(DIRECTOR_BINDING, "127.0.0.1")
end
end
---------------------------------------------------------------------------------
--Function Name : GetCommandParamList
--Parameters : commandName(str), paramName(str)
--Description : This function is required for DYNAMIC_LIST type commands.
---------------------------------------------------------------------------------
function GetCommandParamList(commandName, paramName)
local tList = {}
if (commandName == "Set Visibility") then
if (paramName == "Select Navigator") then
tList = {"Comfort", "Lights", "Listen", "Security", "Shades", "Watch"}
end
end
return (tList)
end
-----------------------------------------------------------------------------------------------------
--Function Name : ExecuteCommand
--Parameters : strCommand(str), tParams(table)
--Description : Function called by Director when a command is received for this DriverWorks driver.
-----------------------------------------------------------------------------------------------------
function ExecuteCommand(strCommand, tParams)
tParams = tParams or {}
Dbg("ExecuteCommand: " .. strCommand .. " (" .. formatParams(tParams) .. ")")
if (strCommand == "LUA_ACTION") then
if (tParams.ACTION) then
strCommand = tParams.ACTION
tParams.ACTION = nil
end
end
local strCommand = string.upper(strCommand)
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 Lua error: ", strCommand, ret)
end
end
----------------------------------------------------------------------------------
--Function Name : EC.SET_VISIBILITY
--Parameters : tParams(table)
--Description : Function called when "Set Visibility" ExecuteCommand is received.
----------------------------------------------------------------------------------
function EC.SET_VISIBILITY(tParams)
local navigator = tParams["Select Navigator"] or ""
local device = tParams["Select Device"] or ""
local rooms = tParams["Select Rooms"] or ""
local hidden = tParams["Hidden"] or ""
if (navigator == "") or (device == "") or (rooms == "") or (hidden == "") then return end
SendToRooms(navigator, device, rooms, hidden, "SET_DEVICE_HIDDEN_STATE")
end
---------------------------------------------------------------------------------------------------
--Function Name : SendToRooms
--Parameters : strNavigator(str), strDevice(str), tRooms(table), strHidden(str), strCommand(str)
--Description : Function called to send device visibility change to room.
---------------------------------------------------------------------------------------------------
function SendToRooms(strNavigator, strDevice, tRooms, strHidden, strCommand)
local ProxyGroup = {
["Comfort"] = "OrderedComfortList",
["Lights"] = "OrderedLightList",
["Listen"] = "OrderedListenList",
["Security"] = "OrderedSecurityList",
["Shades"] = "OrderedBlindList",
["Watch"] = "OrderedWatchList"
}
tRooms = trim(tRooms) .. ","
local tParams = {}
local proxyId = strDevice
if (proxyId == "") then return end
for id in tRooms:gfind("(%d+),") do
local roomId = tonumber(id) or 0
if (roomId == 0) then break end
tParams["PROXY_GROUP"] = ProxyGroup[strNavigator]
tParams["DEVICE_ID"] = proxyId
tParams["IS_HIDDEN"] = toboolean(strHidden)
C4:SendToDevice(roomId, strCommand, tParams)
Dbg("C4:SendToDevice(" .. roomId .. ", \"" .. strCommand .. "\", " .. formatParams(tParams) .. ")")
end
if (Properties["Refresh Navigators"] == "On") then
C4:NetConnect(DIRECTOR_BINDING, DIRECTOR_PORT)
end
end
---------------------------------------------------------------------------------------------
--Function Name : Dbg
--Parameters : strDebugText(str)
--Description : Function called when debug information is to be printed/logged (if enabled)
---------------------------------------------------------------------------------------------
function Dbg(strDebugText)
if (g_debugMode == 1) then print(strDebugText) end
end
----------------------------------------------------------------
--Function Name : trim
--Parameters : s(str)
--Description : Function called to trim whitespace in a string
----------------------------------------------------------------
function trim(s)
return s:gsub("^%s*(.-)%s*$", "%1")
end
----------------------------------------------------------------
--Function Name : toboolean
--Parameters : str(str)
--Description : Function called to convert string to boolean
----------------------------------------------------------------
function toboolean(str)
local bool = false
if str == "true" then
bool = true
end
return bool
end
---------------------------------------------------------
--Function Name : formatParams
--Parameters : tParams(table)
--Description : Function called to format table params.
---------------------------------------------------------
function formatParams(tParams)
tParams = tParams or {}
local out = {}
for k,v in pairs(tParams) do
if (type(v) == "string") then
table.insert(out, k .. " = \"" .. v .. "\"")
else
table.insert(out, k .. " = " .. tostring(v))
end
end
return "{" .. table.concat(out, ", ") .. "}"
end