using System;
using System.Collections.Generic;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharpPro;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Essentials.Core;
using Serilog.Events;
namespace PepperDash.Essentials.Core.Config
{
///
/// Represents the configuration data for a single tie line between two routing ports.
///
public class TieLineConfig
{
///
/// The key of the source device.
///
public string SourceKey { get; set; }
///
/// The key of the source card (if applicable, e.g., in a modular chassis).
///
public string SourceCard { get; set; }
///
/// The key of the source output port.
///
public string SourcePort { get; set; }
///
/// The key of the destination device.
///
public string DestinationKey { get; set; }
///
/// The key of the destination card (if applicable).
///
public string DestinationCard { get; set; }
///
/// The key of the destination input port.
///
public string DestinationPort { get; set; }
///
/// Optional override for the signal type of the tie line. If set, this overrides the destination port's type for routing calculations.
///
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(StringEnumConverter))]
public eRoutingSignalType? OverrideType { get; set; }
///
/// Returns the appropriate tie line for either a card-based device or
/// regular device with ports on-device.
///
/// null if config data does not match ports, cards or devices
public TieLine GetTieLine()
{
Debug.LogMessage(LogEventLevel.Information, "Build TieLine: {0}",null, this);
// Get the source device
var sourceDev = DeviceManager.GetDeviceForKey(SourceKey) as IRoutingOutputs;
if (sourceDev == null)
{
LogError("Routable source not found");
return null;
}
// Get the destination device
var destDev = DeviceManager.GetDeviceForKey(DestinationKey) as IRoutingInputs;
if (destDev == null)
{
LogError("Routable destination not found");
return null;
}
//Get the source port
var sourceOutputPort = sourceDev.OutputPorts[SourcePort];
if (sourceOutputPort == null)
{
LogError("Source does not contain port");
return null;
}
//Get the Destination port
var destinationInputPort = destDev.InputPorts[DestinationPort];
if (destinationInputPort == null)
{
LogError("Destination does not contain port");
return null;
}
return new TieLine(sourceOutputPort, destinationInputPort, OverrideType);
}
///
/// Logs an error message related to creating this tie line configuration.
///
/// The specific error message.
void LogError(string msg)
{
Debug.LogMessage(LogEventLevel.Error, "WARNING: Cannot create tie line: {message}:\r {tieLineConfig}",null, msg, this);
}
///
/// Returns a string representation of the tie line configuration.
///
/// A string describing the source and destination of the configured tie line.
public override string ToString()
{
return string.Format("{0}.{1}.{2} --> {3}.{4}.{5}", SourceKey, SourceCard, SourcePort,
DestinationKey, DestinationCard, DestinationPort);
}
}
}