//******************************************************************************************************
// DigitalChannel.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/19/2011 - J. Ritchie Carroll
// Generated original version of source code.
// 12/13/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace GSF.COMTRADE
{
///
/// Represents a digital channel definition of the .
///
public class DigitalChannel
{
#region [ Members ]
// Fields
private readonly int m_version;
private string m_stationName;
private string m_channelName;
private string m_phaseID;
private string m_circuitComponent;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the .
///
/// Target schema version.
public DigitalChannel(int version = 1999)
{
m_version = version;
}
///
/// Creates a new instance of the from an existing line image.
///
/// Line image to parse.
/// Target schema version.
/// Indicates whether to relax validation on the number of line image elements.
public DigitalChannel(string lineImage, int version = 1999, bool useRelaxedValidation = false)
{
// Dn,ch_id,ph,ccbm,y
string[] parts = lineImage.Split(',');
m_version = version;
if (parts.Length < 3 || (!useRelaxedValidation && parts.Length != 3 && parts.Length != 5))
throw new InvalidOperationException($"Unexpected number of line image elements for digital channel definition: {parts.Length} - expected 3 or 5{Environment.NewLine}Image = {lineImage}");
if (parts.Length >= 5)
{
Index = int.Parse(parts[0].Trim());
Name = parts[1];
PhaseID = parts[2];
CircuitComponent = parts[3];
NormalState = parts[4].Trim().ParseBoolean();
}
else
{
Index = int.Parse(parts[0].Trim());
Name = parts[1];
NormalState = parts[2].Trim().ParseBoolean();
}
}
#endregion
#region [ Properties ]
///
/// Gets or sets index of this .
///
public int Index { get; set; }
///
/// Gets or sets name of this formatted as station_name:channel_name.
///
/// Name must be formatted as station_name:channel_name.
public string Name
{
get
{
if (string.IsNullOrWhiteSpace(m_stationName))
return m_channelName;
return $"{m_stationName}:{m_channelName}";
}
set
{
string[] parts = value.Split(':');
if (parts.Length == 2)
{
m_stationName = parts[0].Trim();
m_channelName = parts[1].Trim();
}
else
{
m_stationName = "";
m_channelName = parts[0].Trim();
}
}
}
///
/// Gets or sets station name component of this .
///
public string StationName
{
get
{
return m_stationName;
}
set
{
m_stationName = value.Replace(":", "_").Trim();
}
}
///
/// Gets or sets channel name component of this .
///
public string ChannelName
{
get
{
return m_channelName;
}
set
{
m_channelName = value.Replace(":", "_").Trim();
}
}
///
/// Gets or sets the 2-character phase identifier for this .
///
public string PhaseID
{
get
{
return m_phaseID;
}
set
{
m_phaseID = string.IsNullOrWhiteSpace(value) ? "" : value.Trim();
// Even though the COMTRADE standard specifically says this is 2 characters in length, the
// sample data in the schema for phasor data has phase IDs with lengths of 3 (e.g., T10)
if (m_phaseID.Length > 3)
m_phaseID = m_phaseID.Substring(0, 3);
}
}
///
/// Gets or sets circuit component of this .
///
public string CircuitComponent
{
get
{
return m_circuitComponent;
}
set
{
if (!string.IsNullOrWhiteSpace(value))
m_circuitComponent = value.Trim();
else
m_circuitComponent = "";
if (m_circuitComponent.Length > 64)
m_circuitComponent = m_circuitComponent.Substring(0, 64);
}
}
///
/// Gets or sets normal state of this .
///
public bool NormalState { get; set; }
///
/// Gets target schema version.
///
[JsonIgnore]
public int Version => m_version;
#endregion
#region [ Methods ]
///
/// Converts to its string format.
///
public override string ToString()
{
List values;
if (m_version >= 1999)
{
// Dn,ch_id,ph,ccbm,y
values = new List
{
Index.ToString(),
Name,
PhaseID,
CircuitComponent,
NormalState ? "1" : "0"
};
}
else
{
// Dn,ch_id,y
values = new List
{
Index.ToString(),
Name,
NormalState ? "1" : "0"
};
}
return string.Join(",", values);
}
#endregion
}
}