-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.lua
148 lines (138 loc) · 5.99 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
-------------
-- Globals --
-------------
do
OPC = {}
RFP = {}
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()
ShowProxyInRoom(5001)
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 : ReceivedFromProxy
--Parameters : idBinding(int), strCommand(str), tParams(table)
--Description : Function called when proxy command is called
-----------------------------------------------------------------
function ReceivedFromProxy(idBinding, strCommand, tParams)
tParams = tParams or {}
Dbg("ReceivedFromProxy: [" .. idBinding .. "] : " ..strCommand .. " (" .. formatParams(tParams) .. ")")
local strCommand = string.upper(strCommand)
strCommand = string.gsub(strCommand, "%s+", "_")
local success, ret
if (RFP and RFP[strCommand] and type(RFP[strCommand]) == "function") then
success, ret = pcall(RFP[strCommand], idBinding, tParams)
end
if (success == true) then
return (ret)
elseif (success == false) then
print ("ReceivedFromProxy Lua error: ", strCommand, ret)
end
end
------------------------------------------------------------------------------
--Function Name : RFP.SELECT
--Parameters : idBinding(int), tParams(table)
--Description : Function called when "SELECT" ReceivedFromProxy is received.
------------------------------------------------------------------------------
function RFP.SELECT(idBinding, tParams)
C4:SendToDevice(tParams["Room"], "CONTROL4", {})
end
---------------------------------------------------------------------------
--Function Name : ShowProxyInRoom
--Parameters : idBinding(int)
--Description : Function called to show the UI experience button in room.
---------------------------------------------------------------------------
function ShowProxyInRoom(idBinding)
idBinding = idBinding or 0
if (idBinding == 0) then return end
Dbg("ShowProxyInRoom: Adding Driver to Room (" .. idBinding .. ")")
local id, name = next(C4:GetBoundConsumerDevices(C4:GetDeviceID(), idBinding))
idRoom = C4:RoomGetId()
C4:SendToDevice(idRoom, "SET_DEVICE_HIDDEN_STATE", {PROXY_GROUP = "OrderedWatchList", DEVICE_ID = id, IS_HIDDEN = false})
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 : 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