Skip to content

Commit

Permalink
fix: update usb auto-routing
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor Payne committed Mar 2, 2023
1 parent 89f371b commit 24919b0
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 15 deletions.
124 changes: 114 additions & 10 deletions src/NvxEpi/Extensions/UsbStreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Linq;
using Crestron.SimplSharp.Net;
using NvxEpi.Abstractions.Usb;
using PepperDash.Core;
using PepperDash.Essentials.Core;

namespace NvxEpi.Extensions
{
Expand All @@ -24,40 +26,142 @@ public static void AddRemoteUsbStreamToLocal(this IUsbStreamWithHardware local,
if (local.UsbRemoteIds.ContainsValue(remote.UsbLocalId))
return;
*/
Debug.Console(1, remote, "Automatic Pairing is {0}.", remote.Hardware.UsbInput.AutomaticUsbPairingEnabledFeedback.BoolValue ? "Enabled" : "Disabled");

Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);

if (local.UsbRemoteIds.Any((x) => x.Value.StringValue.Equals(remote.UsbLocalId.StringValue)))
{
var results =
DeviceManager.AllDevices.OfType<IUsbStreamWithHardware>()
.Where(x => !x.IsRemote)
.Where(
o =>
o.UsbRemoteIds.Any((x) => x.Value.StringValue.Equals(remote.UsbLocalId.StringValue))).ToList();

Debug.Console(2, "Found {0} Hosts with client {1} connected", results.Count(), remote.UsbLocalId);
foreach (var usb in results)
{
var localUsb = usb;
Debug.Console(2, "Clearing clients from {0}", localUsb.UsbLocalId);
if (localUsb.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
localUsb.Hardware.UsbInput.RemovePairing();
localUsb.Hardware.UsbInput.RemoteDeviceId.StringValue = ClearUsbValue;
foreach (var remoteId in localUsb.UsbRemoteIds)
{
if (remoteId.Value == remote.UsbLocalId)
{
localUsb.Hardware.UsbInput.RemoteDeviceIds[remoteId.Key].StringValue = ClearUsbValue;
}
}
if(localUsb.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
localUsb.Hardware.UsbInput.RemovePairing();
}
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);

Debug.Console(2, remote, "Remote {0} already added to list. Setting remote to {1}",
remote.UsbLocalId, local.UsbLocalId);
remote.Hardware.UsbInput.RemoteDeviceId.StringValue = ClearUsbValue;
if (remote.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
remote.Hardware.UsbInput.RemovePairing();
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);

remote.Hardware.UsbInput.RemoteDeviceId.StringValue = local.UsbLocalId.StringValue;
local.Hardware.UsbInput.RemoteDeviceId.StringValue = remote.UsbLocalId.StringValue;
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);
Debug.Console(2, remote, "There are {0} devices in RemoteIds", remote.Hardware.UsbInput.RemoteDeviceIds.Count);
foreach (var connection in remote.Hardware.UsbInput.RemoteDeviceIds)
{
Debug.Console(2, remote, connection.StringValue);
}
Debug.Console(2, local, "There are {0} devices in RemoteIds", local.Hardware.UsbInput.RemoteDeviceIds.Count);
foreach (var connection in local.Hardware.UsbInput.RemoteDeviceIds)
{
Debug.Console(2, local, connection.StringValue);
}
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);
if (remote.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
remote.Hardware.UsbInput.Pair();
if (local.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
local.Hardware.UsbInput.Pair();
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);

return;
}
if (local.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
local.Hardware.UsbInput.RemovePairing();
if (remote.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
remote.Hardware.UsbInput.RemovePairing();
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);


var index =
foreach (var remoteId in local.Hardware.UsbInput.RemoteDeviceIds)
{
remoteId.StringValue = ClearUsbValue;
}
foreach (var remoteId in remote.Hardware.UsbInput.RemoteDeviceIds)
{
remoteId.StringValue = ClearUsbValue;
}

Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);
/*
var indexLocal =
local.UsbRemoteIds.FirstOrDefault(
x => string.IsNullOrEmpty(x.Value.StringValue) || x.Value.StringValue.Equals(ClearUsbValue));
if (index.Value == null)
var indexRemote =
remote.UsbRemoteIds.FirstOrDefault(
x => string.IsNullOrEmpty(x.Value.StringValue) || x.Value.StringValue.Equals(ClearUsbValue));
if (indexLocal.Value == null)
{
Debug.Console(1, remote, "Cannot pair to: {0}, it doesn't support any more connections", local.Key);
Debug.Console(0, remote, "Cannot pair to: {0}, it doesn't support any more connections", local.Key);
return;
}

var inputSig = local
if (indexRemote.Value == null)
{
Debug.Console(0, remote, "Cannot pair to: {0}, it doesn't support any more connections", remote.Key);
return;
}
/*
var localSig = local
.Hardware
.UsbInput
.RemoteDeviceIds[index.Key];
.RemoteDeviceIds[1];
var remoteSig = remote
.Hardware
.UsbInput
.RemoteDeviceIds[1];
if (inputSig == null)
if (localSig == null)
{
Debug.Console(0, local, "Somehow local sig and index:{0} doesn't exist", indexLocal.Key);
return;
}
if (remoteSig == null)
{
Debug.Console(0, local, "Somehow input sig and index:{0} doesn't exist", index.Key);
Debug.Console(0, remote, "Somehow remote sig and index:{0} doesn't exist", indexRemote.Key);
return;
}
* */

Debug.Console(1, local, "Setting Remote Id: {0} to {1}", index, remote.UsbLocalId.StringValue);
inputSig.StringValue = remote.UsbLocalId.StringValue;
//Debug.Console(0, local, "Setting Remote Id: {0} to {1}", 1, remote.UsbLocalId.StringValue);
Debug.Console(1, local, "Setting Remote Id to {0}", remote.UsbLocalId.StringValue);
local.Hardware.UsbInput.RemoteDeviceId.StringValue = remote.UsbLocalId.StringValue;
//localSig.StringValue = remote.UsbLocalId.StringValue;
//Debug.Console(0, remote, "Setting Remote Id: {0} to {1}", 1, local.UsbLocalId.StringValue);
Debug.Console(1, remote, "Setting Remote Id to {0}", local.UsbLocalId.StringValue);
remote.Hardware.UsbInput.RemoteDeviceId.StringValue = local.UsbLocalId.StringValue;
//remoteSig.StringValue = local.UsbLocalId.StringValue;
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(500);

if (local.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
local.Hardware.UsbInput.Pair();
if (remote.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
remote.Hardware.UsbInput.Pair();
Crestron.SimplSharpPro.CrestronThread.Thread.Sleep(1000);

}
catch (Exception ex)
{
Expand Down
23 changes: 18 additions & 5 deletions src/NvxEpi/Features/Streams/Usb/UsbStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ private void FollowCurrentRoute(string streamUrl)


var currentRoute = result;
if (currentRoute == null)
ClearCurrentRoute();
else if (IsRemote && !currentRoute.IsRemote)
//if (currentRoute == null)
ClearCurrentRoute();
/*else*/ if (IsRemote && !currentRoute.IsRemote)
{
Debug.Console(1, this, "Routing to Local from CurrentRoute : {0}!", currentRoute.Name);

Expand All @@ -155,6 +155,12 @@ private void ClearCurrentRoute()
{
Debug.Console(1, this, "Setting remote id to : {0}", UsbStreamExt.ClearUsbValue);
Hardware.UsbInput.RemoteDeviceId.StringValue = UsbStreamExt.ClearUsbValue;
foreach (var usb in Hardware.UsbInput.RemoteDeviceIds)
{
usb.StringValue = UsbStreamExt.ClearUsbValue;
}
if(Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
Hardware.UsbInput.RemovePairing();
ClearRemoteUsbStreamToLocal(UsbLocalId.StringValue);
}

Expand Down Expand Up @@ -185,7 +191,7 @@ private static void ClearRemoteUsbStreamToLocal(string usbId)

if (local == null)
return;

/*
var inputSig = local
.Hardware
.UsbInput
Expand All @@ -196,9 +202,16 @@ private static void ClearRemoteUsbStreamToLocal(string usbId)
Debug.Console(0, local, "Somehow input sig and index:{0} doesn't exist", index);
return;
}
*/

Debug.Console(1, local, "Setting remote id to : {0}", UsbStreamExt.ClearUsbValue);
inputSig.StringValue = UsbStreamExt.ClearUsbValue;
local.Hardware.UsbInput.RemoteDeviceId.StringValue = UsbStreamExt.ClearUsbValue;
foreach (var usb in local.Hardware.UsbInput.RemoteDeviceIds)
{
usb.StringValue = UsbStreamExt.ClearUsbValue;
}
if(local.Hardware.UsbInput.AutomaticUsbPairingDisabledFeedback.BoolValue)
local.Hardware.UsbInput.RemovePairing();
}

private void SetDefaultStream(bool isRemote, string defaultPair)
Expand Down

0 comments on commit 24919b0

Please sign in to comment.