|
|
@@ -1,14 +1,13 @@
|
|
|
-using DBHelper;
|
|
|
+using DBHelper_SqlSugar;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
using System.Linq;
|
|
|
using System.Reflection;
|
|
|
using System.Runtime.InteropServices;
|
|
|
-using DBHelper_SqlSugar;
|
|
|
using WCS.Entity;
|
|
|
|
|
|
-namespace WCS.Core
|
|
|
+namespace WCS.Core.DataTrans
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// IL动态生成的类型所继承的基类
|
|
|
@@ -19,29 +18,27 @@ namespace WCS.Core
|
|
|
|
|
|
public static Func<WCS_PROTOCOLDATA, string, WCS_PROTOCOLDATA> DataChanged;
|
|
|
|
|
|
- private Dictionary<string, PlcItem> Items = new Dictionary<string, PlcItem>();
|
|
|
+ private readonly Dictionary<string, PlcItem> _items = new Dictionary<string, PlcItem>();
|
|
|
|
|
|
- //public ushort Start;
|
|
|
- //public string Name;
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
- public WCS_DEVICEPROTOCOL PROTOCOL { get; private set; }
|
|
|
+ public WCS_DEVICEPROTOCOL Protocol { get; private set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 起始便宜量,按字节算
|
|
|
/// </summary>
|
|
|
- private ushort Start;
|
|
|
+ private readonly ushort _start;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 长度,按字节算
|
|
|
/// </summary>
|
|
|
- private ushort Length;
|
|
|
+ private readonly ushort _length;
|
|
|
|
|
|
public WCS_DATABLOCK Db;
|
|
|
|
|
|
- private static Dictionary<Type, object> LockObjs = new Dictionary<Type, object>();
|
|
|
+ private static readonly Dictionary<Type, object> LockObjs = new Dictionary<Type, object>();
|
|
|
|
|
|
- public ProtocolProxyBase(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL deviceItem)
|
|
|
+ protected ProtocolProxyBase(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL deviceItem)
|
|
|
{
|
|
|
var type = this.GetType();
|
|
|
if (!LockObjs.ContainsKey(type))
|
|
|
@@ -49,12 +46,10 @@ namespace WCS.Core
|
|
|
|
|
|
this.Db = db;
|
|
|
db.Ex().DataChanged += PLCData_DataChanged;
|
|
|
- this.Start = start;
|
|
|
+ this._start = start;
|
|
|
var bitStart = start * 8;//偏移量,按位算
|
|
|
this.Id = id;
|
|
|
- this.PROTOCOL = deviceItem;
|
|
|
- //this.Start = start;
|
|
|
- //PlcDB db = new PlcDB(null, 50, 100);
|
|
|
+ this.Protocol = deviceItem;
|
|
|
var lst = this.GetType().GetInterfaces().ToList();
|
|
|
|
|
|
var ps = lst.SelectMany(v => v.GetProperties()).ToArray();
|
|
|
@@ -110,44 +105,44 @@ namespace WCS.Core
|
|
|
var m = typeof(DataBlock).GetMethod("Regist");
|
|
|
m = m.MakeGenericMethod(p.PropertyType);
|
|
|
var item = m.Invoke(db.Ex(), new object[] { this.Id, p.Name, bitStart, arrlen, strLen }) as PlcItem;
|
|
|
- Items.Add(p.Name, item);
|
|
|
+ _items.Add(p.Name, item);
|
|
|
bitStart += item.DataSizeOfBits;
|
|
|
- Length += item.DataSize;
|
|
|
+ _length += item.DataSize;
|
|
|
}
|
|
|
- ProtocolType = this.PROTOCOL.DB.GetProtocolType();
|
|
|
+ ProtocolType = this.Protocol.DB.GetProtocolType();
|
|
|
ProtocolDataType = ProtocolType.Assembly.GetTypes().Where(v => v.IsClass).First(v => v.GetInterface(ProtocolType.Name) != null && v != this.GetType());
|
|
|
}
|
|
|
|
|
|
private byte[] Data = null;
|
|
|
- private static object locobj = new object();
|
|
|
+ private static object _locobj = new object();
|
|
|
|
|
|
private void InvokeUpdate(string user, Db db)
|
|
|
{
|
|
|
SaveChangs(user, db);
|
|
|
}
|
|
|
|
|
|
- private WCS_PROTOCOLDATA last = null;
|
|
|
+ private WCS_PROTOCOLDATA _last = null;
|
|
|
|
|
|
public DateTime UpdateTime;
|
|
|
|
|
|
protected virtual void SaveChangs(string user, Db db)
|
|
|
{
|
|
|
var obj = Copy(this, ProtocolDataType) as WCS_PROTOCOLDATA;
|
|
|
- obj.WCSVERSION = this.WCSVersion;
|
|
|
- if (last == null)
|
|
|
+ obj!.WCSVERSION = this.WcsVersion;
|
|
|
+ if (_last == null)
|
|
|
{
|
|
|
- Publish(this.PROTOCOL.DEVICE.CODE, obj);
|
|
|
- last = GetLastData(db);
|
|
|
+ Publish(this.Protocol.DEVICE.CODE, obj);
|
|
|
+ _last = GetLastData(db);
|
|
|
}
|
|
|
- if (last != null)
|
|
|
+ if (_last != null)
|
|
|
{
|
|
|
- if (Equals(obj, last, ProtocolType))
|
|
|
+ if (Equals(obj, _last, ProtocolType))
|
|
|
return;
|
|
|
}
|
|
|
this.UpdateTime = DateTime.Now;
|
|
|
|
|
|
- Publish(this.PROTOCOL.DEVICE.CODE, obj);
|
|
|
- last = SaveNewData(db, last, obj, user);
|
|
|
+ Publish(this.Protocol.DEVICE.CODE, obj);
|
|
|
+ _last = SaveNewData(db, _last, obj, user);
|
|
|
}
|
|
|
|
|
|
public virtual void Publish(string code, WCS_PROTOCOLDATA obj)
|
|
|
@@ -160,7 +155,7 @@ namespace WCS.Core
|
|
|
|
|
|
private void PLCData_DataChanged(Db db, byte[] data)
|
|
|
{
|
|
|
- var temp = data.Skip(Start).Take(Length).ToArray();
|
|
|
+ var temp = data.Skip(_start).Take(_length).ToArray();
|
|
|
try
|
|
|
{
|
|
|
if (Data != null && Data.SequenceEqual(temp))
|
|
|
@@ -168,12 +163,11 @@ namespace WCS.Core
|
|
|
var d1 = DateTime.Now;
|
|
|
InvokeUpdate("PLC", db);
|
|
|
var d = (DateTime.Now - d1).TotalMilliseconds;
|
|
|
- //Console.WriteLine("DB " + d);
|
|
|
}
|
|
|
finally { Data = temp; }
|
|
|
}
|
|
|
|
|
|
- private object Copy(object obj, Type type)
|
|
|
+ private static object Copy(object obj, Type type)
|
|
|
{
|
|
|
var res = Activator.CreateInstance(type);
|
|
|
foreach (var p in type.GetProperties())
|
|
|
@@ -187,7 +181,7 @@ namespace WCS.Core
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
- private bool Equals(object a, object b, Type type)
|
|
|
+ private static bool Equals(object a, object b, Type type)
|
|
|
{
|
|
|
foreach (var p in type.GetProperties())
|
|
|
{
|
|
|
@@ -199,18 +193,15 @@ namespace WCS.Core
|
|
|
{
|
|
|
if (attr.IgnoreValueRange == 0)
|
|
|
continue;
|
|
|
- else
|
|
|
+ try
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- var d = Math.Abs((decimal.Parse(v1.ToString())) - (decimal.Parse(v2.ToString())));
|
|
|
- if (d < attr.IgnoreValueRange)
|
|
|
- continue;
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- Console.WriteLine("IgnoreValueRange属性只能用于数字类型");
|
|
|
- }
|
|
|
+ var d = Math.Abs((decimal.Parse(v1.ToString())) - (decimal.Parse(v2.ToString())));
|
|
|
+ if (d < attr.IgnoreValueRange)
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ Console.WriteLine("IgnoreValueRange属性只能用于数字类型");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -222,14 +213,14 @@ namespace WCS.Core
|
|
|
if (p.PropertyType.IsArray)
|
|
|
{
|
|
|
var m = typeof(Enumerable).GetMethods().Where(v => v.Name == nameof(Enumerable.SequenceEqual)).First(v => v.GetParameters().Length == 2);
|
|
|
- m = m.MakeGenericMethod(p.PropertyType.GetElementType());
|
|
|
- var res = (bool)m.Invoke(null, new object[] { v1, v2 });
|
|
|
+ m = m.MakeGenericMethod(p.PropertyType.GetElementType()!);
|
|
|
+ var res = (bool)m.Invoke(null, new[] { v1, v2 })!;
|
|
|
if (!res)
|
|
|
return false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (!v1.Equals(v2))
|
|
|
+ if (!v1!.Equals(v2))
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -263,15 +254,15 @@ namespace WCS.Core
|
|
|
/// <summary>
|
|
|
/// 版本号,WCS每修改一次数据,版本号变化一次
|
|
|
/// </summary>
|
|
|
- public int WCSVersion { get; private set; }
|
|
|
+ public int WcsVersion { get; private set; }
|
|
|
|
|
|
public void Set<T>(string propertyName, T value)
|
|
|
{
|
|
|
- var str = PROTOCOL.DEVICE.CODE + "." + ProtocolType.Name + "." + propertyName;
|
|
|
+ var str = Protocol.DEVICE.CODE + "." + ProtocolType.Name + "." + propertyName;
|
|
|
try
|
|
|
{
|
|
|
str += " = " + (value == null ? "null" : value.ToString());
|
|
|
- var item = Items[propertyName] as PlcItem<T>;
|
|
|
+ var item = _items[propertyName] as PlcItem<T>;
|
|
|
item.Value = value;
|
|
|
DBHelper_SqlSugar.Db.Do(db =>
|
|
|
{
|
|
|
@@ -279,7 +270,7 @@ namespace WCS.Core
|
|
|
});
|
|
|
|
|
|
Data = null;
|
|
|
- WCSVersion++;
|
|
|
+ WcsVersion++;
|
|
|
str += " 写入成功";
|
|
|
}
|
|
|
catch
|
|
|
@@ -295,7 +286,7 @@ namespace WCS.Core
|
|
|
|
|
|
public void Update(string prop, object value, string user)
|
|
|
{
|
|
|
- var item = Items[prop];
|
|
|
+ var item = _items[prop];
|
|
|
item.Value = value;
|
|
|
DBHelper_SqlSugar.Db.Do(db =>
|
|
|
{
|
|
|
@@ -305,15 +296,15 @@ namespace WCS.Core
|
|
|
Data = null;
|
|
|
if (user == "PLC")
|
|
|
return;
|
|
|
- WCSVersion++;
|
|
|
+ WcsVersion++;
|
|
|
}
|
|
|
|
|
|
public T Get<T>(string propertyName)
|
|
|
{
|
|
|
- var str = PROTOCOL.DEVICE.CODE + "." + ProtocolType.Name + "." + propertyName;
|
|
|
+ var str = Protocol.DEVICE.CODE + "." + ProtocolType.Name + "." + propertyName;
|
|
|
try
|
|
|
{
|
|
|
- var item = Items[propertyName] as PlcItem<T>;
|
|
|
+ var item = _items[propertyName] as PlcItem<T>;
|
|
|
var res = item.Value;
|
|
|
str += " : " + (res == null ? "null" : res.ToString());
|
|
|
return res;
|
|
|
@@ -323,10 +314,6 @@ namespace WCS.Core
|
|
|
str += " 读取失败";
|
|
|
throw;
|
|
|
}
|
|
|
- finally
|
|
|
- {
|
|
|
- //Ltc.Log(str);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
public T CallReturn<T>(string methodName, params object[] args)
|
|
|
@@ -355,12 +342,8 @@ namespace WCS.Core
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
- var str = "";
|
|
|
- foreach (var p in this.GetType().GetProperties())
|
|
|
- {
|
|
|
- str += p.Name + ":" + p.GetValue(this, null).ToString() + ",";
|
|
|
- }
|
|
|
- str = str.Substring(0, str.Length - 1);
|
|
|
+ var str = this.GetType().GetProperties().Aggregate("", (current, p) => current + (p.Name + ":" + p.GetValue(this, null)!.ToString() + ","));
|
|
|
+ str = str[..^1];
|
|
|
return str;
|
|
|
}
|
|
|
}
|