|
|
@ -1,24 +1,23 @@
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
|
|
|
|
|
|
|
|
|
namespace HybirdFrameworkCore.Utils;
|
|
|
|
namespace HybirdFrameworkCore.Utils;
|
|
|
|
|
|
|
|
|
|
|
|
public static class ModelConvert
|
|
|
|
public static class ModelConvert
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private static readonly Type BOOLEAN = typeof(bool);
|
|
|
|
public static readonly Type BOOLEAN = typeof(bool);
|
|
|
|
private static readonly Type BYTE = typeof(byte);
|
|
|
|
public static readonly Type BYTE = typeof(byte);
|
|
|
|
private static readonly Type SBYTE = typeof(sbyte);
|
|
|
|
public static readonly Type SBYTE = typeof(sbyte);
|
|
|
|
private static readonly Type SHORT = typeof(short);
|
|
|
|
public static readonly Type SHORT = typeof(short);
|
|
|
|
private static readonly Type USHORT = typeof(ushort);
|
|
|
|
public static readonly Type USHORT = typeof(ushort);
|
|
|
|
private static readonly Type INT = typeof(int);
|
|
|
|
public static readonly Type INT = typeof(int);
|
|
|
|
private static readonly Type UINT = typeof(uint);
|
|
|
|
public static readonly Type UINT = typeof(uint);
|
|
|
|
private static readonly Type LONG = typeof(long);
|
|
|
|
public static readonly Type LONG = typeof(long);
|
|
|
|
private static readonly Type ULONG = typeof(ulong);
|
|
|
|
public static readonly Type ULONG = typeof(ulong);
|
|
|
|
private static readonly Type FLOAT = typeof(float);
|
|
|
|
public static readonly Type FLOAT = typeof(float);
|
|
|
|
private static readonly Type DOUBLE = typeof(double);
|
|
|
|
public static readonly Type DOUBLE = typeof(double);
|
|
|
|
private static readonly Type STRING = typeof(string);
|
|
|
|
public static readonly Type STRING = typeof(string);
|
|
|
|
|
|
|
|
|
|
|
|
public static T Declode<T>(byte[] bytes) where T : class, new()
|
|
|
|
public static T Declode<T>(byte[] bytes) where T : class, new()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -39,62 +38,16 @@ public static class ModelConvert
|
|
|
|
PropertyAttribute? attribute = field.GetCustomAttribute<PropertyAttribute>();
|
|
|
|
PropertyAttribute? attribute = field.GetCustomAttribute<PropertyAttribute>();
|
|
|
|
if (attribute != null)
|
|
|
|
if (attribute != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
double scale = attribute.Scale;
|
|
|
|
|
|
|
|
int length = PropertyReadConstant.Byte == attribute.Type ? attribute.Length * 8 : attribute.Length;
|
|
|
|
|
|
|
|
int start = attribute.Start;
|
|
|
|
int start = attribute.Start;
|
|
|
|
|
|
|
|
int length = PropertyReadConstant.Byte == attribute.Type ? attribute.Length * 8 : attribute.Length;
|
|
|
|
|
|
|
|
double scale = attribute.Scale;
|
|
|
|
|
|
|
|
int round = attribute.Round;
|
|
|
|
double offset = attribute.Offset;
|
|
|
|
double offset = attribute.Offset;
|
|
|
|
|
|
|
|
|
|
|
|
Type propertyType = field.PropertyType;
|
|
|
|
Type propertyType = field.PropertyType;
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == BOOLEAN)
|
|
|
|
object value = BitUtls.Bytes2Value(bytes, propertyType, start, length, scale, round, offset);
|
|
|
|
{
|
|
|
|
field.SetValue(t, value, null);
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(BitUtls.Byte2Bit(bytes, start, length) == 1, propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else if (propertyType == BYTE || propertyType == SBYTE)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(BitUtls.Byte2Bit(bytes, start, length) * scale - offset, propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (propertyType == USHORT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(BitUtls.Byte2UInt16(bytes, start, length) * scale - offset, propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (propertyType == SHORT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(BitUtls.Byte2Int16(bytes, start, length) * scale - offset, propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (propertyType == INT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(BitUtls.Byte2Int32(bytes, start, length) * scale - offset, propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (propertyType == UINT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(BitUtls.Byte2UInt32(bytes, start, length) * scale - offset, propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (propertyType == FLOAT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(
|
|
|
|
|
|
|
|
Math.Round(BitUtls.Byte2Float(bytes, start, length, scale) - offset, attribute.Round),
|
|
|
|
|
|
|
|
propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (propertyType == DOUBLE)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t,
|
|
|
|
|
|
|
|
Convert.ChangeType(
|
|
|
|
|
|
|
|
Math.Round(BitUtls.Byte2Double(bytes, start, length, scale) - offset, attribute.Round),
|
|
|
|
|
|
|
|
propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (propertyType == STRING)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.SetValue(t, Convert.ChangeType(BitUtls.BytesToHexStr(bytes, start, length), propertyType), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -150,57 +103,8 @@ public static class ModelConvert
|
|
|
|
double offset = attribute.Offset;
|
|
|
|
double offset = attribute.Offset;
|
|
|
|
double scale = attribute.Scale;
|
|
|
|
double scale = attribute.Scale;
|
|
|
|
Type propertyType = field.PropertyType;
|
|
|
|
Type propertyType = field.PropertyType;
|
|
|
|
object? value = field.GetValue(t);
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return Array.Empty<byte>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == BOOLEAN)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return BitConverter.GetBytes((bool)value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == BYTE || propertyType == SBYTE)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new[] { (byte)(((byte)value + offset) / scale) };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == USHORT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return BitConverter.GetBytes((ushort)(((ushort)value + offset) / scale));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == SHORT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return BitConverter.GetBytes((short)(((short)value + offset) / scale));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == INT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return BitConverter.GetBytes((int)(((int)value + offset) / scale));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == UINT)
|
|
|
|
object? value = field.GetValue(t);
|
|
|
|
{
|
|
|
|
return BitUtls.Value2Bytes(value, scale, offset);
|
|
|
|
return BitConverter.GetBytes((uint)(((uint)value + offset) / scale));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == FLOAT)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return BitConverter.GetBytes((uint)(((float)value + offset) / scale));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == DOUBLE)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return BitConverter.GetBytes((UInt64)(((double)value + offset) / scale));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == STRING)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return Encoding.ASCII.GetBytes((string)value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException($"参数类型{propertyType}不支持encode!");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|