-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.lua
218 lines (206 loc) · 8.09 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
-------------
-- Globals --
-------------
do
EC = {}
OPC = {}
g_debugMode = 0
g_DbgPrint = nil
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 : 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 : 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_WALLPAPER
--Parameters : tParams(table)
--Description : Function called when "Set Wallpaper" ExecuteCommand is received.
----------------------------------------------------------------------------------
function EC.SET_WALLPAPER(tParams)
local list = tParams["Room Selection"] or ""
local wallpaper = tParams["Wallpaper"] or ""
if (list == "") or (wallpaper == "") then return end
SendToDevices(list, "SET_LOCATION_PREFERENCE", wallpaper)
end
-----------------------------------------------------------------------------
--Function Name : SendToDevices
--Parameters : tList(table), strCommand(str), tParams(table)
--Description : Function called to send wallpaper change to Identity agent.
-----------------------------------------------------------------------------
function SendToDevices(tList, strCommand, strWallpaper)
tList = trim(tList) .. ","
local tParams = {}
local agent = "control4_agent_identity.c4i"
local devices = C4:GetDevicesByC4iName(agent)
local proxyId = ""
if (devices ~= nil) then
for k,v in pairs(devices) do
if (v == "Identity") then proxyId = k end
end
end
if (proxyId == "") then return end
for id in tList:gfind("(%d+),") do
local roomId = tonumber(id) or 0
if (roomId == 0) then break end
tParams["USERNAME"] = "primaryuser"
tParams["LOCATION_ID"] = roomId
tParams["NAME"] = "wallpaper"
tParams["VALUE"] = strWallpaper
C4:SendToDevice(proxyId, strCommand, tParams)
Dbg("C4:SendToDevice(" .. proxyId .. ", \"" .. strCommand .. "\", " .. formatParams(tParams) .. ")")
end
end
--------------------------------------------------------------------------
--Function Name : GetWallpaperList
--Parameters : currentValue(str)
--Description : Function called with CUSTOM_SELECT Property in Commands.
--------------------------------------------------------------------------
function GetWallpaperList(currentValue)
local list = {}
local wallpapers = {}
-- Custom Wallpapers
local custom_dir = C4:FileSetDir("/media/wallpaper/onscreen/custom")
local custom_wallpapers = C4:FileList(custom_dir)
---- Add in custom wallpaper list from files on controller
for id, filename in pairs (custom_wallpapers) do
local item = {
value = "/media/wallpaper/onscreen/custom/" .. filename,
text = filename,
}
table.insert (list, item)
end
-- Default Wallpapers
local def_dir = C4:FileSetDir("/media/wallpaper/onscreen/default")
local def_wallpapers = C4:FileList(def_dir)
---- Add in wallpaper list from files on controller
for id, filename in pairs (def_wallpapers) do
local item = {
value = "/media/wallpaper/onscreen/default/" .. filename,
text = filename,
}
table.insert (list, item)
end
local _sort = function (a, b)
return (a.text < b.text)
end
table.sort (list, _sort)
return list
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 : 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