Skip to content

Commit

Permalink
ADDED: Reconnect to command interface if no ping requests in 1 minute
Browse files Browse the repository at this point in the history
ADDED: RegisterVariableListener(Current_Selected_Device) in room to pause on change source (need more testing, will probably pause in other rooms if on a matrix, mini-driver, etc)
FIXED: Properties["On Room Off"] to Properties["On Room OFF"], pause works on power off as well now
ADDED: "Turn OFF" to "On Room OFF" Property
ADDED: "POWER Mapping" for flexibility
IMPROVED: Better Configuration Exports
  • Loading branch information
Greg-MM committed Aug 21, 2022
1 parent 67dd90f commit a93175f
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 111 deletions.
137 changes: 120 additions & 17 deletions debugging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ DebugTimerID = 0
DebugPrint = false

function StartDebugTimer()
if (g_DebugTimer) then
g_DebugTimer = C4:KillTimer(g_DebugTimer)
if (DebugTimerID ~= 0) then
DebugTimerID = C4:KillTimer(DebugTimerID)
end
g_DebugTimer = C4:AddTimer(120, "MINUTES")
DebugTimerID = C4:AddTimer(120, "MINUTES")
end


Expand Down Expand Up @@ -89,36 +89,139 @@ function DebugDivider(DividerChar)
end
end

AlreadyExported = ""
-- Generates LUA thay can be pasted in another instance to copy configuration
-- The device specific (Public Key & Exponent) are commented out by default
function BackupConfiguration()
AlreadyExported = ""

print("--[[ BEGIN CONFIGURATION BACKUP ]]--")
print("-- Vendor: " .. Properties["Vendor Name"])
print("-- Model: " .. Properties["Model Name"])
print("-- Version: " .. Properties["Device Version"])

print("-----------------------------")
print("-- Launch App URLs")
print("-----------------------------")
for App = 1, 20, 1
do
BackupProperty("Launch App URL " .. App)
end

print("-----------------------------")
print("-- Key Mappings")
print("-----------------------------")
BackupProperty("GUIDE Mapping")
BackupProperty("INFO Mapping")
BackupProperty("MENU Mapping")
BackupProperty("DVR Mapping")

BackupProperty("UP Mapping")
BackupProperty("DOWN Mapping")
BackupProperty("LEFT Mapping")
BackupProperty("RIGHT Mapping")
BackupProperty("ENTER Mapping")
BackupProperty("CANCEL Mapping")

BackupProperty("CUSTOM_1 Mapping")
BackupProperty("CUSTOM_2 Mapping")
BackupProperty("CUSTOM_3 Mapping")

BackupProperty("PREVIOUS CHANNEL Mapping")
BackupProperty("CHANNEL UP Mapping")
BackupProperty("CHANNEL DOWN Mapping")

BackupProperty("PAUSE_ROOM_OFF Mapping")

BackupProperty("RECORD Mapping")
BackupProperty("REWIND Mapping")
BackupProperty("FAST FORWARD Mapping")
BackupProperty("SKIP BACKWARD Mapping")
BackupProperty("SKIP FORWARD Mapping")

BackupProperty("PLAY Mapping")
BackupProperty("PAUSE Mapping")
BackupProperty("STOP Mapping")

BackupProperty("RED Mapping")
BackupProperty("GREEN Mapping")
BackupProperty("YELLOW Mapping")
BackupProperty("BLUE Mapping")

BackupProperty("PAGE UP Mapping")
BackupProperty("PAGE DOWN Mapping")

BackupProperty("1 Mapping")
BackupProperty("2 Mapping")
BackupProperty("3 Mapping")
BackupProperty("4 Mapping")
BackupProperty("5 Mapping")
BackupProperty("6 Mapping")
BackupProperty("7 Mapping")
BackupProperty("8 Mapping")
BackupProperty("9 Mapping")
BackupProperty("* Mapping")
BackupProperty("0 Mapping")
BackupProperty("# Mapping")

for k, v in pairs(Properties) do
if(k:find("Mapping")) then
BackupProperty(k)
end
end

print("-----------------------------")
print("-- Other Settings")
print("-----------------------------")
for k, v in pairs(Properties) do
repeat
if(k == "Power Status") then break end
if(k == "Debug Mode") then break end
if(k == "Connection") then break end
if(k == "MAC Address") then break end
if(k == "Current App") then break end
if(k == "Device App Version") then break end
if(k == "Device Version") then break end
if(k == "Device Package Name") then break end
if(k == "Model Name") then break end
if(k == "Driver Version") then break end
if(k == "Vendor Name") then break end
if(k:find("Header")) then break end

local PropertySetLine = ""
if(k:find("Device Public Key")) then PropertySetLine = "--" end

if (type(v) == "number") then
PropertySetLine = PropertySetLine .. "C4:UpdateProperty(\"" .. k .. "\", " .. Properties[k] .. ")"
elseif (type(v) == "string") then
if(v ~= nil and v~="") then
PropertySetLine = PropertySetLine .. "C4:UpdateProperty(\"" .. k .. "\", \"" .. Properties[k] .. "\")"
end
end
if(k:find("Public Key")) then break end

BackupProperty(k)

if(PropertySetLine ~= "") then
print(PropertySetLine)
end
until true
end

print("-----------------------------")
print("-- Device Specific Properties")
print("-----------------------------")
for k, v in pairs(Properties) do
if(k:find("Public Key")) then
BackupProperty(k)
end
end

print("--[[ END CONFIGURATION BACKUP ]]--")
end

function BackupProperty(k)
local v = Properties[k]
local BackupLine = ""

if(k:find("Device Public Key")) then BackupLine = "--" end

if (type(v) == "number") then
BackupLine = BackupLine .. "C4:UpdateProperty(\"" .. k .. "\", " .. Properties[k] .. ")"
elseif (type(v) == "string") then
if(v ~= nil and v~="") then
BackupLine = BackupLine .. "C4:UpdateProperty(\"" .. k .. "\", \"" .. Properties[k] .. "\")"
end
end
local ExportCheck = "<" .. k:gsub("%*", "STAR") .. ">"
if(BackupLine ~= "" and AlreadyExported:find(ExportCheck) == nil) then
AlreadyExported = AlreadyExported .. ExportCheck
print(BackupLine)
end
end
80 changes: 73 additions & 7 deletions driver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "helpers"
require "protobuf"
require "pairing"

local DriverVersion = "1.0.3"
local DriverVersion = "1.0.4"
EventID_CurrentAppChanged = 1

EX_CMD = {}
Expand All @@ -15,6 +15,9 @@ function OnDriverInit(driverInitType)
OnPropertyChanged(k)
end
C4:AddVariable("CURRENT_APP", "", "STRING")

local RoomID = C4:RoomGetId()
C4:RegisterVariableListener(RoomID, 1000) -- Current_Selected_Device

if (driverInitType == "DIT_ADDING") then
-- Initialization needed only when the driver is added to a project
Expand Down Expand Up @@ -93,14 +96,53 @@ function OnTimerExpired(TimerID)
Debug("Turning Debug Mode OFF")
C4:UpdateProperty("Debug Mode", "OFF")
DebugTimerID = C4:KillTimer(TimerID)
elseif(TimerID == PingTimerID) then
--Debug("Ping Timer")
ExecutePingTimer()
else
Debug("Unknown Timer: " .. TimerID)
C4:KillTimer(TimerID)
end
end

function GetVariableName(DeviceID, VariableID)
for k,v in pairs(C4:GetDeviceVariables(DeviceID)) do
if(k == VariableID) then
return v.name
end
end
return ""
end


function OnWatchedVariableChanged(DeviceID, VariableID, strValue)
DebugHeader("OnWatchedVariableChanged")
Debug("• Device: " .. DeviceID)
Debug("• Variable ID: " .. VariableID)

if(DebugPrint) then -- do not call GetVariableName if not showing (not the fastest function)
Debug("• Variable Name: " .. GetVariableName(tonumber(DeviceID), VariableID))
end

Debug("• Value: " .. strValue)
DebugDivider("-")

local SwitchedToDeviceID = tonumber(strValue)

if(VariableID == 1000) then
if(SwitchedToDeviceID == C4:GetDeviceID() or SwitchedToDeviceID == C4:GetProxyDevices()) then
Debug("Switched To Android TV Device, DO NOTHING")
else
if (Properties["On Room OFF"] == "Pause") then
Debug("Switched AWAY from Android TV Device, PAUSE")
SendKey(tonumber(Properties["PAUSE_ROOM_OFF Mapping"]))
else
Debug("Switched AWAY from Android TV Device, PAUSE NOT ENABLED, DO NOTHING")
end

end
end

end


function ReceivedFromProxy(BindingID, strCommand, tParams)
Expand All @@ -117,12 +159,18 @@ function ReceivedFromProxy(BindingID, strCommand, tParams)
SendMagicPacket()
end
if (Properties["Power Status"] == "OFF") then
SendKey(KEYCODE_POWER)
SendKey(tonumber(Properties["POWER Mapping"]))
end

elseif (strCommand == "OFF") then
if (Properties["On Room Off"] == "Pause") then
if (Properties["On Room OFF"] == "Pause") then
SendKey(tonumber(Properties["PAUSE_ROOM_OFF Mapping"]))
elseif (Properties["On Room OFF"] == "Turn OFF") then
if (Properties["Power Status"] == "ON") then
SendKey(tonumber(Properties["POWER Mapping"]))
end
end

else
if (ProcessInputCommand(strCommand, tParams) == false) then
DebugDivider("!")
Expand Down Expand Up @@ -335,7 +383,26 @@ end



PingRequestCount = 0
PingTimerID = 0

function StartPingTimer()
if (PingTimerID ~= 0) then
PingTimerID = C4:KillTimer(PingTimerID)
end
PingTimerID = C4:AddTimer(60, "SECONDS", true)
end

function ExecutePingTimer()
--Debug("Ping Request Count: " .. PingRequestCount)
if(string.len(Properties["Device Public Key Modulus"]) > 0 and PingRequestCount == 0) then
Debug("Communication Failed, attempting to connect...")
LUA_ACTION.DisconnectFromCommand(nil)
LUA_ACTION.ConnectToCommand(nil)
end

PingRequestCount = 0
end


function ProcessNetworkMessage(Message)
Expand All @@ -350,9 +417,9 @@ function ProcessNetworkMessage(Message)
end

if(FingerPrint == "[F8][F1][WT0][F2][WT0]") then -- Ping Request
PingRequestCount = PingRequestCount + 1
local PingRequestNumber = WireMessage[8][1].Value
SendWireCommand({[9]={[1]={ID=1, WireType=WireType_VarInt, Value=PingRequestNumber}}})

elseif(FingerPrint == "[F20][F1][F12][WT2]") then
Debug("CURRENT APP")
local CurrentApp = WireMessage[20][1][12].Value
Expand Down Expand Up @@ -485,6 +552,7 @@ end
function LUA_ACTION.ConnectToCommand(tQueryParams)
Debug("Connecting To Command Interface...")
C4:NetConnect(6001, 6466)
StartPingTimer()
end

function LUA_ACTION.DisconnectFromCommand(tQueryParams)
Expand All @@ -510,9 +578,7 @@ function LUA_ACTION.TestKeyCodePressThenRelease(tQueryParams)
end

function LUA_ACTION.BackupConfiguration(tQueryParams)
print("--[[ BEGIN CONFIGURATION BACKUP ]]--")
BackupConfiguration()
print("--[[ END CONFIGURATION BACKUP ]]--")
end

function LUA_ACTION.SendWOL(tQueryParams)
Expand Down
14 changes: 11 additions & 3 deletions driver.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<creator>Greg Moerler</creator>
<created>08/01/2022 12:00 PM</created>
<modified>08/07/2022 4:20 PM</modified>
<version>3</version>
<version>4</version>
<control>lua_gen</control>
<driver>DriverWorks</driver>
<small image_source="c4z">icons/device_sm.png</small>
Expand Down Expand Up @@ -114,6 +114,7 @@
<items>
<item>Do Nothing</item>
<item>Pause</item>
<item>Turn OFF</item>
</items>
<default>Do Nothing</default>
<readonly>false</readonly>
Expand Down Expand Up @@ -264,7 +265,14 @@
<minimum>0</minimum>
<maximum>5000</maximum>
<default>550</default>
<description>Time (ms) between press and release for a long press</description>
<description>Time (ms) between press and release for a long press (used for 1XXX mappings)</description>
</property>
<property>
<name>POWER Mapping</name>
<type>RANGED_INTEGER</type>
<minimum>0</minimum>
<maximum>5000</maximum>
<default>26</default>
</property>
<property>
<name>GUIDE Mapping</name>
Expand Down Expand Up @@ -419,7 +427,7 @@
<minimum>0</minimum>
<maximum>5000</maximum>
<default>127</default>
<description>Used when 'On Room OFF' is 'PAUSE', should be discreet and not a play/pause toggle</description>
<description>Used when 'On Room OFF' is 'Pause', should be discreet and not a play/pause toggle</description>
</property>
<property>
<name>RECORD Mapping</name>
Expand Down

0 comments on commit a93175f

Please sign in to comment.