From d9dc70bea2145e4c42b508f5d6fdccaab7b37d1a Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Fri, 2 May 2025 11:23:20 -0500 Subject: [PATCH] fix: add ClearRoute method --- .../Routing/Extensions.cs | 52 +++++++++++++++---- .../Routing/RouteDescriptor.cs | 18 ++++++- .../Routing/RouteRequestQueueItem.cs | 8 +-- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/PepperDash.Essentials.Core/Routing/Extensions.cs b/src/PepperDash.Essentials.Core/Routing/Extensions.cs index 1ad4dd7f..64a413a5 100644 --- a/src/PepperDash.Essentials.Core/Routing/Extensions.cs +++ b/src/PepperDash.Essentials.Core/Routing/Extensions.cs @@ -38,16 +38,49 @@ namespace PepperDash.Essentials.Core ReleaseAndMakeRoute(destination, source, signalType, inputPort, outputPort); } + + /// + /// Will release the existing route to the destination, if a route is found. This does not CLEAR the route, only stop counting usage time on any output ports that have a usage tracker set + /// + /// destination to clear public static void ReleaseRoute(this IRoutingInputs destination) { - routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, string.Empty)); + routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, string.Empty, false)); } + /// + /// Will release the existing route to the destination, if a route is found. This does not CLEAR the route, only stop counting usage time on any output ports that have a usage tracker set + /// + /// destination to clear + /// Input to use to find existing route public static void ReleaseRoute(this IRoutingInputs destination, string inputPortKey) { - routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, inputPortKey)); + routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, inputPortKey, false)); } + /// + /// Clears the route on the destination. This will remove any routes that are currently in use + /// + /// Destination + public static void ClearRoute(this IRoutingInputs destination) + { + routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, string.Empty, true)); + } + + /// + /// Clears the route on the destination. This will remove any routes that are currently in use + /// + /// destination + /// input to use to find existing route + public static void ClearRoute(this IRoutingInputs destination, string inputPortKey) + { + routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, inputPortKey, true)); + } + + /// + /// Removes the route request for the destination. This will remove any routes that are currently in use + /// + /// destination device key public static void RemoveRouteRequestForDestination(string destinationKey) { Debug.LogMessage(LogEventLevel.Information, "Removing route request for {destination}", null, destinationKey); @@ -184,7 +217,7 @@ namespace PepperDash.Essentials.Core Debug.LogMessage(LogEventLevel.Information, "Device: {destination} is NOT cooling down. Removing stored route request and routing to source key: {sourceKey}", null, destination.Key, routeRequest.Source.Key); } - routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination,destinationPort?.Key ?? string.Empty)); + routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination,destinationPort?.Key ?? string.Empty, false)); routeRequestQueue.Enqueue(new RouteRequestQueueItem(RunRouteRequest, routeRequest)); } @@ -216,14 +249,15 @@ namespace PepperDash.Essentials.Core { Debug.LogMessage(ex, "Exception Running Route Request {request}", null, request); } - } + } /// - /// Will release the existing route on the destination, if it is found in - /// RouteDescriptorCollection.DefaultCollection + /// Will release the existing route on the destination, if it is found in RouteDescriptorCollection.DefaultCollection /// - /// - private static void ReleaseRouteInternal(IRoutingInputs destination, string inputPortKey) + /// + /// The input port key to use to find the route. If empty, will use the first available input port + /// If true, will clear the route on the destination. This will remove any routes that are currently in use + private static void ReleaseRouteInternal(IRoutingInputs destination, string inputPortKey, bool clearRoute) { try { @@ -242,7 +276,7 @@ namespace PepperDash.Essentials.Core if (current != null) { Debug.LogMessage(LogEventLevel.Information, "Releasing current route: {0}", destination, current.Source.Key); - current.ReleaseRoutes(); + current.ReleaseRoutes(clearRoute); } } catch (Exception ex) { diff --git a/src/PepperDash.Essentials.Core/Routing/RouteDescriptor.cs b/src/PepperDash.Essentials.Core/Routing/RouteDescriptor.cs index b7dade4c..f88f489c 100644 --- a/src/PepperDash.Essentials.Core/Routing/RouteDescriptor.cs +++ b/src/PepperDash.Essentials.Core/Routing/RouteDescriptor.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using Crestron.SimplSharpPro; @@ -66,12 +67,25 @@ namespace PepperDash.Essentials.Core /// Releases all routes in this collection. Typically called via /// extension method IRoutingInputs.ReleaseAndMakeRoute() /// - public void ReleaseRoutes() + /// True to clear the route. Clearing sends `null` as the input selector to the ExecuteSwitch method + public void ReleaseRoutes(bool clearRoute = false) { foreach (var route in Routes.Where(r => r.SwitchingDevice is IRouting)) { if (route.SwitchingDevice is IRouting switchingDevice) { + if(clearRoute) + { + try + { + switchingDevice.ExecuteSwitch(null, route.OutputPort.Selector, SignalType); + } + catch (Exception e) + { + Debug.LogError("Error executing switch: {exception}", e.Message); + } + } + if (route.OutputPort == null) { continue; diff --git a/src/PepperDash.Essentials.Core/Routing/RouteRequestQueueItem.cs b/src/PepperDash.Essentials.Core/Routing/RouteRequestQueueItem.cs index 05681991..54e680b4 100644 --- a/src/PepperDash.Essentials.Core/Routing/RouteRequestQueueItem.cs +++ b/src/PepperDash.Essentials.Core/Routing/RouteRequestQueueItem.cs @@ -25,21 +25,23 @@ namespace PepperDash.Essentials.Core.Routing public class ReleaseRouteQueueItem : IQueueMessage { - private readonly Action action; + private readonly Action action; private readonly IRoutingInputs destination; private readonly string inputPortKey; + private readonly bool clearRoute; - public ReleaseRouteQueueItem(Action action, IRoutingInputs destination, string inputPortKey) + public ReleaseRouteQueueItem(Action action, IRoutingInputs destination, string inputPortKey, bool clearRoute) { this.action = action; this.destination = destination; this.inputPortKey = inputPortKey; + this.clearRoute = clearRoute; } public void Dispatch() { Debug.LogMessage(LogEventLevel.Information, "Dispatching release route request for {destination}:{inputPortKey}", null, destination?.Key ?? "no destination", string.IsNullOrEmpty(inputPortKey) ? "auto" : inputPortKey); - action(destination, inputPortKey); + action(destination, inputPortKey, clearRoute); } } }