Use ConvertToType for both GetValue and CreateNotification

This commit is contained in:
Peter Butzhammer
2024-02-06 14:43:50 +01:00
parent ffa4ee6236
commit 5b86b3e984

View File

@@ -142,96 +142,8 @@ namespace Sharp7.Rx
var address = varaibleNameParser.Parse(variableName);
if (address == null) throw new ArgumentException("Input variable name is not valid", nameof(variableName));
if (typeof(TValue) == typeof(bool))
{
var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
return ConvertToType<TValue>(b, address);
}
if (typeof(TValue) == typeof(int))
{
var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
token.ThrowIfCancellationRequested();
if (address.Length == 2)
return (TValue)(object)((b[0] << 8) + b[1]);
if (address.Length == 4)
{
Array.Reverse(b);
return (TValue)(object)BitConverter.ToInt32(b,0);
}
throw new InvalidOperationException($"length must be 2 or 4 but is {address.Length}");
}
if (typeof(TValue) == typeof(long))
{
var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
token.ThrowIfCancellationRequested();
Array.Reverse(b);
return (TValue)(object)BitConverter.ToInt64(b,0);
}
if (typeof(TValue) == typeof(ulong))
{
var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
token.ThrowIfCancellationRequested();
Array.Reverse(b);
return (TValue)(object)BitConverter.ToUInt64(b, 0);
}
if (typeof(TValue) == typeof(short))
{
var b = await s7Connector.ReadBytes(address.Operand, address.Start, 2, address.DbNr, token);
token.ThrowIfCancellationRequested();
return (TValue)(object)(short)((b[0] << 8) + b[1]);
}
if (typeof(TValue) == typeof(byte) || typeof(TValue) == typeof(char))
{
var b = await s7Connector.ReadBytes(address.Operand, address.Start, 1, address.DbNr, token);
token.ThrowIfCancellationRequested();
return (TValue)(object)b[0];
}
if (typeof(TValue) == typeof(byte[]))
{
var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
token.ThrowIfCancellationRequested();
return (TValue)(object)b;
}
if (typeof(TValue) == typeof(double) || typeof(TValue) == typeof(float))
{
var bytes = await s7Connector.ReadBytes(address.Operand, address.Start, 4, address.DbNr, token);
token.ThrowIfCancellationRequested();
var d = BitConverter.ToSingle(bytes.Reverse().ToArray(),0);
return (TValue)(object)d;
}
if (typeof(TValue) == typeof(string))
{
if (address.Type == DbType.String)
{
var bytes = await s7Connector.ReadBytes(address.Operand, address.Start, 2, address.DbNr, token);
token.ThrowIfCancellationRequested();
var stringLength = bytes[1];
var stringStartAddress = (ushort)(address.Start + 2);
var stringInBytes = await s7Connector.ReadBytes(address.Operand, stringStartAddress, stringLength, address.DbNr, token);
token.ThrowIfCancellationRequested();
return (TValue)(object)Encoding.ASCII.GetString(stringInBytes);
}
else
{
var stringInBytes = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
token.ThrowIfCancellationRequested();
return (TValue)(object)Encoding.ASCII.GetString(stringInBytes).Trim();
}
}
throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue)));
var data = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
return ConvertToType<TValue>(data, address);
}