555 lines
21 KiB
Plaintext
555 lines
21 KiB
Plaintext
using System;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers.DataTypes;
|
|
|
|
namespace Xylem.Common.Hardware.WaterMeter.Genesis.Registers
|
|
{
|
|
/// <summary>
|
|
/// Helper to convert genesis register values store in bytes to data types and back
|
|
/// </summary>
|
|
public static class RegisterConverter
|
|
{
|
|
/// <summary>
|
|
/// Convert single register from Genesis and return a message of the result. The value input is a byte array
|
|
/// with the LSB at the index 0 and the MSB at the highest index. A string is sorted with the first character
|
|
/// at index 0, this does not need to be reversed as it is readable by default.
|
|
/// The raw value is swapped byte-wise to get a human-readable value with MSB at leftmost and LSB at rightmost.
|
|
/// The message contains the register name, the swapped raw value and on the converted value.
|
|
/// </summary>
|
|
/// <param name="registerDefinition"></param>
|
|
/// <param name="regRawByteArray">the raw byte array is always % 4 0 padded for unused elements, the LSB is
|
|
/// on index 0 (little endian, LSB first)</param>
|
|
/// <returns>message with register name, swapped raw value and converted value</returns>
|
|
/// <remarks date="2023-Jul-19" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
/// <remarks date="2023-Sep-05" author="Thomas Wiedebusch">
|
|
/// - Reworked.
|
|
/// </remarks>
|
|
/// <remarks date="2023-Sep-27" author="Thomas Wiedebusch">
|
|
/// - Avoid logging of encryption key.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Mar-20" author="Thomas Wiedebusch">
|
|
/// - Hash password and encryption key with SHA256.
|
|
/// </remarks>
|
|
public static String GetRegisterContentText(RegisterDefinition registerDefinition, Byte[] regRawByteArray)
|
|
{
|
|
String msg;
|
|
String registerName = "?";
|
|
try
|
|
{
|
|
registerName = registerDefinition.GetIdent();
|
|
// getting a result value as text of the data type e.g. UInt32
|
|
var strValue = ConvertToText(regRawByteArray, registerDefinition.DataType);
|
|
// remove non-printable char of the converted result
|
|
var strValueResult = Regex.Replace(strValue, @"\p{C}+", string.Empty);
|
|
|
|
// use hashed output being able to compare written and read back values
|
|
String strRawResult;
|
|
if (registerName.Contains("EncryptionKey") || registerName.Contains("Password"))
|
|
{
|
|
strRawResult = GetRegisterRawText(registerDefinition, regRawByteArray, encryptData:true);
|
|
msg = $"{registerName}: ({strRawResult})h";
|
|
}
|
|
else
|
|
{
|
|
strRawResult = GetRegisterRawText(registerDefinition, regRawByteArray);
|
|
msg = $"{registerName}: ({strRawResult})h ({strValueResult})";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new ApplicationException($"Cannot convert register {registerName}");
|
|
}
|
|
|
|
return msg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert single register from Genesis and return a message of the result. The value input is a byte array
|
|
/// with the LSB at the index 0 and the MSB at the highest index. A string is sorted with the first character
|
|
/// at index 0, this does not need to be reversed as it is readable by default.
|
|
/// The raw value is swapped byte-wise to get a human-readable value with MSB at leftmost and LSB at rightmost.
|
|
/// The message contains the swapped raw value.
|
|
/// </summary>
|
|
/// <param name="registerDefinition"></param>
|
|
/// <param name="regRawByteArray">the raw byte array is always % 4 0 padded for unused elements, the LSB is
|
|
/// on index 0 (little endian, LSB first)</param>
|
|
/// <param name="encryptData">data encryption for data required SHA256 hash instead of clear text</param>
|
|
/// <returns>message with swapped raw value</returns>
|
|
/// <remarks date="2023-Sep-05" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Mar-20" author="Thomas Wiedebusch">
|
|
/// - Hash password and encryption key with SHA256.
|
|
/// </remarks>
|
|
public static String GetRegisterRawText(RegisterDefinition registerDefinition, Byte[] regRawByteArray,
|
|
Boolean encryptData = false)
|
|
{
|
|
String msg;
|
|
var strRawResult = "?";
|
|
try
|
|
{
|
|
// inverse the raw value to get a readable byte order MSB left and LSB last right position
|
|
var tmpList = regRawByteArray.ToList();
|
|
|
|
// reverse the list if it is not a string or the UInt48, UInt72, UInt88,UInt96 or UInt128 which
|
|
// will be used as placeholder for a string
|
|
if (registerDefinition.DataType != typeof(String) &&
|
|
registerDefinition.DataType != typeof(UInt48) &&
|
|
registerDefinition.DataType != typeof(UInt72) &&
|
|
registerDefinition.DataType != typeof(UInt88) &&
|
|
registerDefinition.DataType != typeof(UInt96) &&
|
|
registerDefinition.DataType != typeof(UInt128))
|
|
{
|
|
tmpList.Reverse();
|
|
}
|
|
|
|
// use hashed output being able to compare written and read back values
|
|
if (encryptData)
|
|
{
|
|
var hash = SHA256.Create().ComputeHash(regRawByteArray);
|
|
strRawResult = BitConverter.ToString(hash.ToArray());
|
|
msg = $"SHA256 - {strRawResult}";
|
|
}
|
|
else
|
|
{
|
|
strRawResult = BitConverter.ToString(tmpList.ToArray());
|
|
msg = $"{strRawResult}";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
msg = $"{strRawResult}";
|
|
}
|
|
|
|
return msg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Preset the raw byte array with the default value.
|
|
/// </summary>
|
|
/// <param name="registerDefinition"></param>
|
|
/// <param name="regRawByteArray"></param>
|
|
/// <returns>true if value could be set</returns>
|
|
/// <remarks date="2023-Sep-05" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public static Boolean SetToDefault(RegisterDefinition registerDefinition, out Byte[] regRawByteArray)
|
|
{
|
|
if (registerDefinition.Default.HasValue)
|
|
{
|
|
// will always return an Int64 value Byte[8] !
|
|
regRawByteArray = ValueToByteArray(registerDefinition.Default);
|
|
return true;
|
|
}
|
|
|
|
regRawByteArray = new Byte[] {0};
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert a value (T) into byte[]
|
|
/// </summary>
|
|
/// <typeparam name="T">Type to convert from</typeparam>
|
|
/// <param name="value">value to convert</param>
|
|
/// <returns>byte[] like it store in genesis meter</returns>
|
|
/// <remarks date="????" author="Roland Drabesch">
|
|
/// - Init.
|
|
/// </remarks>
|
|
/// <remarks date="2023-Jul-19" author="Thomas Wiedebusch">
|
|
/// - Used <see cref="TimeT"/> to calculate time in UTC based on 01. Jan 2000
|
|
/// and the given offset in seconds.
|
|
/// </remarks>
|
|
public static Byte[] ValueToByteArray<T>(T value)
|
|
{
|
|
var converted = new Byte[] { 0 };
|
|
if (value == null)
|
|
return converted;
|
|
|
|
if (value.GetType() == typeof(TimeT))
|
|
{
|
|
var teaTime = value as TimeT;
|
|
if (teaTime != null)
|
|
{
|
|
// the value contains always the seconds since 01.Jan 2000
|
|
var intValue = Convert.ToInt32(teaTime.SecondsSince2000);
|
|
return BitConverter.GetBytes(intValue);
|
|
}
|
|
}
|
|
|
|
if (value is Boolean)
|
|
{
|
|
var tmp = Convert.ToBoolean(value);
|
|
return BitConverter.GetBytes(tmp);
|
|
}
|
|
|
|
if (value is UInt16)
|
|
{
|
|
var tmp = Convert.ToUInt16(value);
|
|
return BitConverter.GetBytes(tmp);
|
|
}
|
|
|
|
if (value is UInt32)
|
|
{
|
|
var tmp = Convert.ToUInt32(value);
|
|
return BitConverter.GetBytes(tmp);
|
|
}
|
|
|
|
if (value is UInt64)
|
|
{
|
|
var tmp = Convert.ToUInt64(value);
|
|
return BitConverter.GetBytes(tmp);
|
|
}
|
|
|
|
if (value is Int16)
|
|
{
|
|
var intValue = Convert.ToInt16(value);
|
|
return BitConverter.GetBytes(intValue);
|
|
}
|
|
|
|
if (value is Int32)
|
|
{
|
|
var intValue = Convert.ToInt32(value);
|
|
return BitConverter.GetBytes(intValue);
|
|
}
|
|
|
|
if (value is Int64)
|
|
{
|
|
var tmp = Convert.ToInt64(value);
|
|
return BitConverter.GetBytes(tmp);
|
|
}
|
|
|
|
if (value is String)
|
|
{
|
|
return Encoding.ASCII.GetBytes(value.ToString());
|
|
}
|
|
|
|
if (value is Byte || value is Enum8)
|
|
{
|
|
converted[0] = Convert.ToByte(value);
|
|
}
|
|
else if (value is SByte)
|
|
{
|
|
return BitConverter.GetBytes(Convert.ToSByte(value));
|
|
}
|
|
|
|
else if (value is Byte[])
|
|
{
|
|
return (Byte[])Convert.ChangeType(value, typeof(Byte[]));
|
|
}
|
|
else
|
|
{
|
|
throw new ApplicationException($"Data type {typeof(T)} not implemented");
|
|
}
|
|
|
|
return converted;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get size of type
|
|
/// </summary>
|
|
/// <param name="registerDefinition"></param>
|
|
/// <returns>size in bytes</returns>
|
|
/// <remarks date="2023-Sep-05" author="Thomas Wiedebusch">
|
|
/// - Init.
|
|
/// </remarks>
|
|
public static Int32 SizeOf(RegisterDefinition registerDefinition)
|
|
{
|
|
var size = 4;
|
|
|
|
if (registerDefinition.DataType == typeof(Boolean))
|
|
{
|
|
size = sizeof(Boolean);
|
|
}
|
|
if (registerDefinition.DataType == typeof(Byte) ||
|
|
registerDefinition.DataType == typeof(SByte) ||
|
|
registerDefinition.DataType == typeof(Char) ||
|
|
registerDefinition.DataType == typeof(Enum8))
|
|
{
|
|
size = sizeof(Byte);
|
|
}
|
|
else if (registerDefinition.DataType == typeof(Int16) ||
|
|
registerDefinition.DataType == typeof(UInt16))
|
|
{
|
|
size = sizeof(Int16);
|
|
}
|
|
else if (registerDefinition.DataType == typeof(Int32) ||
|
|
registerDefinition.DataType == typeof(UInt32) ||
|
|
registerDefinition.DataType == typeof(TimeT))
|
|
{
|
|
size = sizeof(Int32);
|
|
}
|
|
|
|
else if (registerDefinition.DataType == typeof(Int64) ||
|
|
registerDefinition.DataType == typeof(UInt64))
|
|
{
|
|
size = sizeof(Int64);
|
|
}
|
|
|
|
else if (registerDefinition.DataType == typeof(UInt48))
|
|
{
|
|
// 48 Bits / 8 Bits/Byte
|
|
size = 6;
|
|
}
|
|
|
|
else if (registerDefinition.DataType == typeof(UInt72))
|
|
{
|
|
// 72 Bits / 8 Bits/Byte
|
|
size = 9;
|
|
}
|
|
|
|
else if (registerDefinition.DataType == typeof(UInt88))
|
|
{
|
|
// 88 Bits / 8 Bits/Byte
|
|
size = 11;
|
|
}
|
|
|
|
else if (registerDefinition.DataType == typeof(UInt96))
|
|
{
|
|
// 96 Bits / 8 Bits/Byte
|
|
size = 3 * 4;
|
|
}
|
|
|
|
else if (registerDefinition.DataType == typeof(UInt128))
|
|
{
|
|
// 128 Bits / 8 Bits/Byte
|
|
size = 4 * 4;
|
|
}
|
|
|
|
else if (registerDefinition.DataType == typeof(String))
|
|
{
|
|
// just some value
|
|
size = 6 * 4;
|
|
}
|
|
|
|
return size;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Build the result of the array as text.
|
|
/// </summary>
|
|
/// <param name="rawByteArray">byte value for conversion</param>
|
|
/// <param name="type">type of the result</param>
|
|
/// <returns></returns>
|
|
/// <remarks date="????" author="Roland Drabesch">
|
|
/// - Init.
|
|
/// </remarks>
|
|
/// <remarks date="2023-Jul-19" author="Thomas Wiedebusch">
|
|
/// - Used <see cref="TimeT"/> to calculate time in UTC based on 01. Jan 2000
|
|
/// and the given offset in seconds.
|
|
/// </remarks>
|
|
public static String ConvertToText(Byte[] rawByteArray, Type type)
|
|
{
|
|
try
|
|
{
|
|
if (type == typeof(Boolean))
|
|
{
|
|
return ByteArrayToValue<Boolean>(rawByteArray).ToString();
|
|
}
|
|
|
|
if (type == typeof(TimeT))
|
|
{
|
|
var teaTime = ByteArrayToValue<TimeT>(rawByteArray);
|
|
return teaTime.ToString();
|
|
}
|
|
if (type == typeof(UInt16))
|
|
{
|
|
return ByteArrayToValue<UInt16>(rawByteArray).ToString();
|
|
}
|
|
if (type == typeof(UInt32))
|
|
{
|
|
return ByteArrayToValue<UInt32>(rawByteArray).ToString();
|
|
}
|
|
if (type == typeof(UInt64))
|
|
{
|
|
return ByteArrayToValue<UInt64>(rawByteArray).ToString();
|
|
}
|
|
if (type == typeof(Int16))
|
|
{
|
|
return ByteArrayToValue<Int16>(rawByteArray).ToString();
|
|
}
|
|
if (type == typeof(Int32))
|
|
{
|
|
return ByteArrayToValue<Int32>(rawByteArray).ToString();
|
|
}
|
|
if (type == typeof(Int64))
|
|
{
|
|
return ByteArrayToValue<Int64>(rawByteArray).ToString();
|
|
}
|
|
if (type == typeof(Byte) || type == typeof(Enum8))
|
|
{
|
|
return ByteArrayToValue<Byte>(rawByteArray).ToString();
|
|
}
|
|
if (type == typeof(UInt48))
|
|
{
|
|
return "";
|
|
}
|
|
if (type == typeof(UInt72))
|
|
{
|
|
return "";
|
|
}
|
|
if (type == typeof(UInt88))
|
|
{
|
|
return "";
|
|
}
|
|
if (type == typeof(UInt96))
|
|
{
|
|
return "";
|
|
}
|
|
if (type == typeof(UInt128))
|
|
{
|
|
return "";
|
|
}
|
|
if (type == typeof(SByte))
|
|
{
|
|
return ByteArrayToValue<SByte>(rawByteArray).ToString();
|
|
}
|
|
|
|
return ByteArrayToValue<String>(rawByteArray);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// convert form byte (meter) to data type
|
|
/// </summary>
|
|
/// <typeparam name="T">Type to cast in</typeparam>
|
|
/// <param name="rawByteArray">byte value for conversion</param>
|
|
/// <returns>converted value</returns>
|
|
/// <remarks date="????" author="Roland Drabesch">
|
|
/// - Init.
|
|
/// </remarks>
|
|
/// <remarks date="2023-Jul-19" author="Thomas Wiedebusch">
|
|
/// - Used <see cref="TimeT"/> to calculate time in UTC based on 01. Jan 2000
|
|
/// and the given offset in seconds.
|
|
/// - String removed from first zero in string until the end to avoid ghost signs!
|
|
/// </remarks>
|
|
public static T ByteArrayToValue<T>(Byte[] rawByteArray)
|
|
{
|
|
try
|
|
{
|
|
Object convertedObj;
|
|
var type = typeof(T);
|
|
|
|
if (type == typeof(Boolean))
|
|
{
|
|
convertedObj = BitConverter.ToBoolean(rawByteArray, 0);
|
|
}
|
|
else if (type == typeof(UInt16))
|
|
{
|
|
convertedObj = BitConverter.ToUInt16(rawByteArray, 0);
|
|
}
|
|
else if (type == typeof(UInt32))
|
|
{
|
|
convertedObj = BitConverter.ToUInt32(rawByteArray, 0);
|
|
}
|
|
else if (type == typeof(UInt64))
|
|
{
|
|
convertedObj = BitConverter.ToUInt64(rawByteArray, 0);
|
|
}
|
|
else if (type == typeof(UInt48))
|
|
{
|
|
return default(T);
|
|
}
|
|
else if (type == typeof(UInt72))
|
|
{
|
|
return default(T);
|
|
}
|
|
else if (type == typeof(UInt88))
|
|
{
|
|
return default(T);
|
|
}
|
|
else if (type == typeof(UInt96))
|
|
{
|
|
return default(T);
|
|
}
|
|
else if (type == typeof(UInt128))
|
|
{
|
|
return default(T);
|
|
}
|
|
else if (type == typeof(Int16))
|
|
{
|
|
convertedObj = BitConverter.ToInt16(rawByteArray, 0);
|
|
}
|
|
else if (type == typeof(Int32))
|
|
{
|
|
convertedObj = BitConverter.ToInt32(rawByteArray, 0);
|
|
}
|
|
else if (type == typeof(Int64))
|
|
{
|
|
convertedObj = BitConverter.ToInt64(rawByteArray, 0);
|
|
}
|
|
else if (type == typeof(Byte) || type == typeof(Enum8))
|
|
{
|
|
convertedObj = rawByteArray[0];
|
|
}
|
|
else if (type == typeof(SByte))
|
|
{
|
|
// set all others to 0 as only the LSB is of interest
|
|
for (var idx = 1; idx < rawByteArray.Length; idx++)
|
|
rawByteArray[idx] = 0;
|
|
convertedObj = Convert.ToSByte((SByte)rawByteArray[0]);
|
|
}
|
|
else if (type == typeof(Boolean))
|
|
{
|
|
convertedObj = Convert.ToBoolean(rawByteArray[0]);
|
|
}
|
|
else if (type == typeof(String))
|
|
{
|
|
// the value may contain zeros at the beginning of the value-record, these have to be removed
|
|
var zeroPaddingCounter = 0;
|
|
while (rawByteArray.Length > zeroPaddingCounter && rawByteArray[zeroPaddingCounter] == 0)
|
|
zeroPaddingCounter += 1;
|
|
var trimmedValue = new Byte[rawByteArray.Length - zeroPaddingCounter];
|
|
Int32 i;
|
|
for (i = 0; i < rawByteArray.Length - zeroPaddingCounter; i++)
|
|
trimmedValue[i] = rawByteArray[i + zeroPaddingCounter];
|
|
|
|
// as a string is terminated with a zero all following elements after including the initial
|
|
// zero have to be removed. This is caused due to the 4 byte chunks which will be sent.
|
|
var validCharCtr = 0;
|
|
// find the first zero in the trimmed value array
|
|
while (trimmedValue.Length > validCharCtr && trimmedValue[validCharCtr] != 0)
|
|
validCharCtr += 1;
|
|
// reserve space for valid chr plus the trailing 0 which will be added if
|
|
var rawResult = new Byte[validCharCtr + 1];
|
|
for (i = 0; i < validCharCtr; i++)
|
|
rawResult[i] = trimmedValue[i];
|
|
if (validCharCtr < trimmedValue.Length)
|
|
{
|
|
rawResult[i] = 0;
|
|
convertedObj = Encoding.ASCII.GetString(rawResult);
|
|
}
|
|
else
|
|
{
|
|
convertedObj = Encoding.ASCII.GetString(trimmedValue);
|
|
}
|
|
}
|
|
else if (type == typeof(TimeT))
|
|
{
|
|
// the value contains always the seconds since 01.Jan 2000
|
|
convertedObj = new TimeT { SecondsSince2000 = ByteArrayToValue<Int32>(rawByteArray) };
|
|
}
|
|
else
|
|
{
|
|
throw new ApplicationException($"Data type {type} not implemented");
|
|
}
|
|
|
|
return (T)Convert.ChangeType(convertedObj, type);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return default(T);
|
|
}
|
|
}
|
|
}
|
|
} |