mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 20:54:55 +00:00
feat: add cooldown logic to Magic Routing
In some cases, a route can be requested while a sink is cooling down. In those cases, the routing logic should keep track of requests for a destination and wait until cooling is complete to request the new route.
This commit is contained in:
@@ -11,12 +11,34 @@ using PepperDash.Core;
|
|||||||
|
|
||||||
namespace PepperDash.Essentials.Core
|
namespace PepperDash.Essentials.Core
|
||||||
{
|
{
|
||||||
|
public class RouteRequest
|
||||||
|
{
|
||||||
|
public IRoutingSink Destination {get; set;}
|
||||||
|
public IRoutingOutputs Source {get; set;}
|
||||||
|
public eRoutingSignalType SignalType {get; set;}
|
||||||
|
|
||||||
|
public void HandleCooldown(object sender, FeedbackEventArgs args)
|
||||||
|
{
|
||||||
|
var coolingDevice = sender as IWarmingCooling;
|
||||||
|
|
||||||
|
if(args.BoolValue == false)
|
||||||
|
{
|
||||||
|
Destination.ReleaseAndMakeRoute(Source, SignalType);
|
||||||
|
|
||||||
|
if(sender == null) return;
|
||||||
|
|
||||||
|
coolingDevice.IsCoolingDownFeedback.OutputChange -= HandleCooldown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extensions added to any IRoutingInputs classes to provide discovery-based routing
|
/// Extensions added to any IRoutingInputs classes to provide discovery-based routing
|
||||||
/// on those destinations.
|
/// on those destinations.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class IRoutingInputsExtensions
|
public static class IRoutingInputsExtensions
|
||||||
{
|
{
|
||||||
|
private static Dictionary<string, RouteRequest> RouteRequests = new Dictionary<string, RouteRequest>();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets any existing RouteDescriptor for a destination, clears it using ReleaseRoute
|
/// Gets any existing RouteDescriptor for a destination, clears it using ReleaseRoute
|
||||||
/// and then attempts a new Route and if sucessful, stores that RouteDescriptor
|
/// and then attempts a new Route and if sucessful, stores that RouteDescriptor
|
||||||
@@ -24,16 +46,51 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void ReleaseAndMakeRoute(this IRoutingSink destination, IRoutingOutputs source, eRoutingSignalType signalType)
|
public static void ReleaseAndMakeRoute(this IRoutingSink destination, IRoutingOutputs source, eRoutingSignalType signalType)
|
||||||
{
|
{
|
||||||
destination.ReleaseRoute();
|
var routeRequest = new RouteRequest {
|
||||||
|
Destination = destination,
|
||||||
|
Source = source,
|
||||||
|
SignalType = signalType
|
||||||
|
};
|
||||||
|
|
||||||
if (source == null) return;
|
var coolingDevice = destination as IWarmingCooling;
|
||||||
var newRoute = destination.GetRouteToSource(source, signalType);
|
|
||||||
if (newRoute == null) return;
|
RouteRequest existingRouteRequest;
|
||||||
RouteDescriptorCollection.DefaultCollection.AddRouteDescriptor(newRoute);
|
|
||||||
Debug.Console(2, destination, "Executing full route");
|
//We already have a route request for this device, and it's a cooling device and is cooling
|
||||||
newRoute.ExecuteRoutes();
|
if (RouteRequests.TryGetValue(destination.Key, out existingRouteRequest) && coolingDevice != null && coolingDevice.IsCoolingDownFeedback.BoolValue == true)
|
||||||
|
{
|
||||||
|
coolingDevice.IsCoolingDownFeedback.OutputChange -= existingRouteRequest.HandleCooldown;
|
||||||
|
|
||||||
|
coolingDevice.IsCoolingDownFeedback.OutputChange += routeRequest.HandleCooldown;
|
||||||
|
|
||||||
|
RouteRequests[destination.Key] = routeRequest;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//New Request
|
||||||
|
if (coolingDevice != null && coolingDevice.IsCoolingDownFeedback.BoolValue == true)
|
||||||
|
{
|
||||||
|
coolingDevice.IsCoolingDownFeedback.OutputChange += routeRequest.HandleCooldown;
|
||||||
|
|
||||||
|
RouteRequests.Add(destination.Key, routeRequest);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
destination.ReleaseRoute();
|
||||||
|
|
||||||
|
RunRouteRequest(routeRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void RunRouteRequest(RouteRequest request)
|
||||||
|
{
|
||||||
|
if (request.Source == null) return;
|
||||||
|
var newRoute = request.Destination.GetRouteToSource(request.Source, request.SignalType);
|
||||||
|
if (newRoute == null) return;
|
||||||
|
RouteDescriptorCollection.DefaultCollection.AddRouteDescriptor(newRoute);
|
||||||
|
Debug.Console(2, request.Destination, "Executing full route");
|
||||||
|
newRoute.ExecuteRoutes();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Will release the existing route on the destination, if it is found in
|
/// Will release the existing route on the destination, if it is found in
|
||||||
/// RouteDescriptorCollection.DefaultCollection
|
/// RouteDescriptorCollection.DefaultCollection
|
||||||
|
|||||||
Reference in New Issue
Block a user