mirror of
https://github.com/ckaczor/OneWireAPI.git
synced 2026-01-26 09:35:37 -05:00
More cleanup
This commit is contained in:
52
Identifer.cs
52
Identifer.cs
@@ -1,80 +1,68 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OneWireAPI
|
||||
{
|
||||
public class Identifier
|
||||
{
|
||||
private readonly short[] _rawId; // The raw ID array
|
||||
private readonly string _friendlyName; // Friendly display name
|
||||
private readonly int _familyCode; // Family code
|
||||
public short[] RawId { get; private set; }
|
||||
|
||||
public string Name { get; private set; }
|
||||
|
||||
public int Family { get; private set; }
|
||||
|
||||
public Identifier()
|
||||
{
|
||||
// Create a blank ID
|
||||
_rawId = new short[8];
|
||||
RawId = new short[8];
|
||||
}
|
||||
|
||||
public Identifier(byte[] deviceId)
|
||||
public Identifier(IList<byte> deviceId)
|
||||
{
|
||||
// Create a blank ID
|
||||
_rawId = new short[8];
|
||||
RawId = new short[8];
|
||||
|
||||
// Copy the byte array to the short array
|
||||
for (var i = 0; i < deviceId.Length; i++)
|
||||
_rawId[i] = deviceId[i];
|
||||
for (var index = 0; index < deviceId.Count; index++)
|
||||
RawId[index] = deviceId[index];
|
||||
|
||||
// Get the friendly name
|
||||
_friendlyName = ConvertToString(_rawId);
|
||||
Name = ConvertToString(RawId);
|
||||
|
||||
// Get the family code
|
||||
_familyCode = _rawId[0];
|
||||
Family = RawId[0];
|
||||
}
|
||||
|
||||
public Identifier(short[] deviceId)
|
||||
{
|
||||
// Store the ID supplied
|
||||
_rawId = deviceId;
|
||||
RawId = deviceId;
|
||||
|
||||
// Get the friendly name
|
||||
_friendlyName = ConvertToString(_rawId);
|
||||
Name = ConvertToString(RawId);
|
||||
|
||||
// Get the family code
|
||||
_familyCode = _rawId[0];
|
||||
Family = RawId[0];
|
||||
}
|
||||
|
||||
private static string ConvertToString(short[] rawId)
|
||||
private static string ConvertToString(IList<short> rawId)
|
||||
{
|
||||
var friendlyId = new StringBuilder();
|
||||
|
||||
// Loop backwards over the ID array
|
||||
for (var iIndex = rawId.Length - 1; iIndex >= 0; iIndex--)
|
||||
for (var index = rawId.Count - 1; index >= 0; index--)
|
||||
{
|
||||
// Convert the short value into a hex string and append it to the ID string
|
||||
friendlyId.AppendFormat("{0:X2}", rawId[iIndex]);
|
||||
friendlyId.AppendFormat("{0:X2}", rawId[index]);
|
||||
}
|
||||
|
||||
// Return the ID string
|
||||
return friendlyId.ToString();
|
||||
}
|
||||
|
||||
public short[] RawId
|
||||
{
|
||||
get { return _rawId; }
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return _friendlyName; }
|
||||
}
|
||||
|
||||
public int Family
|
||||
{
|
||||
get { return _familyCode; }
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return _friendlyName;
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user