|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using PepperDash.Core; |
| 6 | +using PepperDash.Essentials.Core; |
| 7 | + |
| 8 | +namespace PepperDash.Essentials.AppServer.Messengers |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Messenger for devices implementing <see cref="IHasNamedRoutingSlots"/> — sends named |
| 12 | + /// input/output slots with per-signal-type current-route feedback, which the bare |
| 13 | + /// <see cref="IRoutingMidpointWithFeedbackMessenger"/> (registered for every |
| 14 | + /// <see cref="IRoutingMidpointWithFeedback"/> device, including this one) cannot provide. |
| 15 | + /// </summary> |
| 16 | + public class IHasNamedRoutingSlotsMessenger : MessengerBase |
| 17 | + { |
| 18 | + private readonly IHasNamedRoutingSlots _device; |
| 19 | + |
| 20 | + public IHasNamedRoutingSlotsMessenger(string key, string messagePath, IHasNamedRoutingSlots device) |
| 21 | + : base(key, messagePath, device as IKeyName) |
| 22 | + { |
| 23 | + _device = device; |
| 24 | + } |
| 25 | + |
| 26 | + protected override void RegisterActions() |
| 27 | + { |
| 28 | + base.RegisterActions(); |
| 29 | + |
| 30 | + AddAction("/fullStatus", (id, content) => SendFullStatus(id)); |
| 31 | + |
| 32 | + foreach (var output in _device.OutputSlots.Values) |
| 33 | + { |
| 34 | + output.OutputSlotChanged += (sender, args) => SendFullStatus(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static RoutingSlotMessage BuildSlotMessage(IRoutingSlotInfo slot) => new RoutingSlotMessage |
| 39 | + { |
| 40 | + Key = slot.Key, |
| 41 | + Name = slot.Name, |
| 42 | + SlotNumber = slot.SlotNumber, |
| 43 | + SupportedSignalTypes = slot.SupportedSignalTypes.ToString() |
| 44 | + }; |
| 45 | + |
| 46 | + private void SendFullStatus(string id = null) |
| 47 | + { |
| 48 | + var inputSlots = _device.InputSlots.ToDictionary(kvp => kvp.Key, kvp => BuildSlotMessage(kvp.Value)); |
| 49 | + |
| 50 | + var outputSlots = _device.OutputSlots.ToDictionary(kvp => kvp.Key, kvp => |
| 51 | + { |
| 52 | + var message = BuildSlotMessage(kvp.Value); |
| 53 | + message.CurrentRouteInputKeys = kvp.Value.CurrentRouteInputKeys |
| 54 | + .ToDictionary(r => r.Key.ToString(), r => r.Value); |
| 55 | + return message; |
| 56 | + }); |
| 57 | + |
| 58 | + var content = JToken.FromObject(new |
| 59 | + { |
| 60 | + inputSlots, |
| 61 | + outputSlots |
| 62 | + }); |
| 63 | + |
| 64 | + PostStatusMessage(content, MessagePath, id); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public class RoutingSlotMessage |
| 69 | + { |
| 70 | + [JsonProperty("key")] |
| 71 | + public string Key { get; set; } |
| 72 | + |
| 73 | + [JsonProperty("name")] |
| 74 | + public string Name { get; set; } |
| 75 | + |
| 76 | + [JsonProperty("slotNumber")] |
| 77 | + public int SlotNumber { get; set; } |
| 78 | + |
| 79 | + [JsonProperty("supportedSignalTypes")] |
| 80 | + public string SupportedSignalTypes { get; set; } |
| 81 | + |
| 82 | + [JsonProperty("currentRouteInputKeys", NullValueHandling = NullValueHandling.Ignore)] |
| 83 | + public Dictionary<string, string> CurrentRouteInputKeys { get; set; } |
| 84 | + } |
| 85 | +} |
0 commit comments