Files
tbf/NfcC7_DLL/NfcHanler/Utils/ChannelizedFlexNetId.cs
T

172 lines
5.4 KiB
C#

using Sensus.Protocols.FlexNet;
namespace NfcC7_DLL.NfcHanler.Utils
{
public class ChannelizedFlexNetId : IEquatable<ChannelizedFlexNetId> // TODO: maybe refactor this to include NA2W in the name
{
public static char SeparationChar = ':';
public FlexNetID ID;
public UInt32 Channel; // defaults to primary channel, which also can mean not used
public ChannelizedFlexNetId()
: this(0, 0)
{ }
public ChannelizedFlexNetId(UInt32 id)
: this(id, 0)
{ }
public ChannelizedFlexNetId(UInt32 id, UInt32 channel)
{
ID = (FlexNetID)id;
Channel = channel;
}
public ChannelizedFlexNetId(ChannelizedFlexNetId other)
{
ID = other.ID;
Channel = other.Channel;
}
public ChannelizedFlexNetId(string GUIstring)
{
string[] parts = GUIstring.Split(SeparationChar);
UInt32 temp;
bool parsedCorrectly = true;
// get FNID
if (parts.Length >= 1)
{
if (Conversion.TryParseHexOrDecString(parts[0], out temp))
{
ID = (FlexNetID)temp;
}
else
{
ID = 0;
parsedCorrectly = false;
}
}
// get Channel
if (parts.Length == 2)
{
if (!Conversion.TryParseHexOrDecString(parts[1], out Channel))
{
Channel = 0;
parsedCorrectly = false;
}
}
// throw exception here to keep ID or Channel, as one or both may be correct
if (!parsedCorrectly)
{
throw new Exception("FNID and/or Channel is not hex or decimal string");
}
else if (!IsValid())
{
throw new Exception("FNID and/or Channel are/is not valid");
}
}
public static bool IsValid(UInt32 id, UInt32 channel)
{
// only have 2^3=8 channels, but allow a little higher for testing
// on firmware such that it will reject invalid channels
return FlexNetID.IsValidDirect(id) && (channel < 16);
}
public bool IsValid()
{
return ChannelizedFlexNetId.IsValid(ID, Channel);
}
public override string ToString()
{
string retStr = ID.ToString();
if (Channel > 0)
{
retStr += (SeparationChar.ToString() + Channel.ToString());
}
return retStr;
}
public bool Equals(ChannelizedFlexNetId other)
{
if (other == null)
{
return false;
}
else if ((ID.ToString() == other.ID.ToString()) // need ToString(), else override Equals in class FlexNetID
&& (Channel == other.Channel))
{
return true;
}
return false;
}
/// <summary>
/// Attempts to parse a string containing a FlexNet ID or a group address
/// </summary>
/// <param name="userText">The string containing the FlexNet ID in hex or decimal, or
/// a group address in IPv4 octet notation. Can also contain FNID:Channel.</param>
/// <param name="newFNID"></param>
/// <returns>true if userText can successfully be parsed, false otherwise</returns>
internal static bool TryParseFlexNetID(string userText, out ChannelizedFlexNetId newFNID)
{
UInt32 temp = 0;
newFNID = new ChannelizedFlexNetId();
try
{
if (userText.Contains('.'))
{
// user might be using group addressing in IPv4 format
string[] octets = userText.Split('.');
if (octets.Length == 4)
{
// user *IS* using group addressing in IPv4 format
foreach (var oct_str in octets)
{
byte oct_byte = byte.Parse(oct_str);
temp = (UInt32)((temp << 8) | oct_byte);
}
newFNID.ID = temp;
return true; // we got through all 4 octets w/o failing
}
// channel zero in newFNID
}
else if (userText.Contains(ChannelizedFlexNetId.SeparationChar))
{
try
{
newFNID = new ChannelizedFlexNetId(userText);
return true;
}
catch
{
return false;
}
}
else
{
if (Conversion.TryParseHexOrDecString(userText, out temp))
{
newFNID.ID = temp; // channel is zero in newFNID
return true;
}
}
}
catch
{
// any kind of exception means user screwed up
// continue to return false
}
// none of the above worked out
return false;
}
}
}