mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-16 13:15:03 +00:00
fix: move ReleaseRoute & RunRouteRequests to use a queue
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
using Serilog.Events;
|
using PepperDash.Essentials.Core.Queues;
|
||||||
|
using PepperDash.Essentials.Core.Routing;
|
||||||
|
using Serilog.Events;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@@ -17,6 +19,8 @@ namespace PepperDash.Essentials.Core
|
|||||||
{
|
{
|
||||||
private static readonly Dictionary<string, RouteRequest> RouteRequests = new Dictionary<string, RouteRequest>();
|
private static readonly Dictionary<string, RouteRequest> RouteRequests = new Dictionary<string, RouteRequest>();
|
||||||
|
|
||||||
|
private static readonly GenericQueue routeRequestQueue = new GenericQueue("routingQueue");
|
||||||
|
|
||||||
/// <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
|
||||||
@@ -33,6 +37,15 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
ReleaseAndMakeRoute(destination, source, signalType, inputPort, outputPort);
|
ReleaseAndMakeRoute(destination, source, signalType, inputPort, outputPort);
|
||||||
}
|
}
|
||||||
|
public static void ReleaseRoute(this IRoutingInputs destination)
|
||||||
|
{
|
||||||
|
routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, string.Empty));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ReleaseRoute(this IRoutingInputs destination, string inputPortKey)
|
||||||
|
{
|
||||||
|
routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination, inputPortKey));
|
||||||
|
}
|
||||||
|
|
||||||
public static void RemoveRouteRequestForDestination(string destinationKey)
|
public static void RemoveRouteRequestForDestination(string destinationKey)
|
||||||
{
|
{
|
||||||
@@ -59,9 +72,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
Source = source,
|
Source = source,
|
||||||
SourcePort = sourcePort,
|
SourcePort = sourcePort,
|
||||||
SignalType = signalType
|
SignalType = signalType
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var coolingDevice = destination as IWarmingCooling;
|
var coolingDevice = destination as IWarmingCooling;
|
||||||
|
|
||||||
@@ -101,9 +112,9 @@ 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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
destination.ReleaseRoute(destinationPort?.Key ?? string.Empty);
|
routeRequestQueue.Enqueue(new ReleaseRouteQueueItem(ReleaseRouteInternal, destination,destinationPort?.Key ?? string.Empty));
|
||||||
|
|
||||||
RunRouteRequest(routeRequest);
|
routeRequestQueue.Enqueue(new RouteRequestQueueItem(RunRouteRequest, routeRequest));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RunRouteRequest(RouteRequest request)
|
private static void RunRouteRequest(RouteRequest request)
|
||||||
@@ -133,19 +144,14 @@ namespace PepperDash.Essentials.Core
|
|||||||
{
|
{
|
||||||
Debug.LogMessage(ex, "Exception Running Route Request {request}", null, request);
|
Debug.LogMessage(ex, "Exception Running Route Request {request}", null, request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ReleaseRoute(this IRoutingInputs destination)
|
|
||||||
{
|
|
||||||
ReleaseRoute(destination, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="destination"></param>
|
/// <param name="destination"></param>
|
||||||
public static void ReleaseRoute(this IRoutingInputs destination, string inputPortKey)
|
private static void ReleaseRouteInternal(IRoutingInputs destination, string inputPortKey)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
using PepperDash.Essentials.Core.Queues;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials.Core.Routing
|
||||||
|
{
|
||||||
|
public class RouteRequestQueueItem : IQueueMessage
|
||||||
|
{
|
||||||
|
private readonly Action<RouteRequest> action;
|
||||||
|
private readonly RouteRequest routeRequest;
|
||||||
|
|
||||||
|
public RouteRequestQueueItem(Action<RouteRequest> routeAction, RouteRequest request)
|
||||||
|
{
|
||||||
|
action = routeAction;
|
||||||
|
routeRequest = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispatch()
|
||||||
|
{
|
||||||
|
action(routeRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ReleaseRouteQueueItem: IQueueMessage
|
||||||
|
{
|
||||||
|
private readonly Action<IRoutingInputs, string> action;
|
||||||
|
private readonly IRoutingInputs destination;
|
||||||
|
private readonly string inputPortKey;
|
||||||
|
|
||||||
|
public ReleaseRouteQueueItem(Action<IRoutingInputs, string> action, IRoutingInputs destination, string inputPortKey)
|
||||||
|
{
|
||||||
|
this.action = action;
|
||||||
|
this.destination = destination;
|
||||||
|
this.inputPortKey = inputPortKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispatch() {
|
||||||
|
action(destination, inputPortKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user