ProtocolProxy.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using WCS.Core;
  2. using WCS.Entity.Protocol.DataStructure;
  3. using WCS.Entity.Protocol.Protocol.DataStructure;
  4. using WCS.WorkEngineering.Systems;
  5. namespace WCS.WorkEngineering
  6. {
  7. public class ProtocolProxy : ProtocolProxyBase
  8. {
  9. public static DeviceDataPack DataPack { get; set; } = new DeviceDataPack();
  10. public ProtocolProxy(Device dev, ProtocolInfo info, Type protocolType, World world) : base(dev, info, protocolType, world)
  11. {
  12. }
  13. protected override void DataChanged()
  14. {
  15. //if (Device.Code == "RGV1" && Info.DBInfo.No == 520)
  16. //{
  17. // var a = Items;
  18. // //var dev=Device.Protocol(ProtocolType)
  19. // var b = a;
  20. //}
  21. try
  22. {
  23. var datas = DataCollectionSysyem.AllDatas;
  24. if (Device.Code.All(char.IsNumber))
  25. {
  26. if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new StationData { Code = Device.Code };
  27. }
  28. else if (Device.Code.Contains("SRM"))
  29. {
  30. if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new SRMData { Code = Device.Code };
  31. }
  32. //else if (Device.Code.Contains("Truss"))
  33. //{
  34. // if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new TrussData() { Code = Device.Code };
  35. //}
  36. else if (Device.Code.Contains("Robot"))
  37. {
  38. if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new RobotData() { Code = Device.Code };
  39. }
  40. else if (Device.Code.Contains("RGV"))
  41. {
  42. if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new RGVData { Code = Device.Code };
  43. }
  44. if (!datas.TryGetValue(Device.Code, out var data)) return;
  45. data.Frame = DateTime.Now;
  46. var p = data.GetType().GetProperties().FirstOrDefault(v => v.PropertyType == ProtocolDataType);
  47. if (p == null)
  48. {
  49. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + ProtocolType.Name + "的属性");
  50. }
  51. else
  52. {
  53. p.SetValue(data, DictionaryToEntity(ProtocolDataType, Items));
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. Console.WriteLine(ex.Message);
  59. }
  60. }
  61. public object DictionaryToEntity(Type type, Dictionary<string, PlcItem> items)
  62. {
  63. var a = Activator.CreateInstance(type);
  64. foreach (var ty in type.GetProperties())
  65. {
  66. foreach (var item in items.Where(item => ty.Name == item.Key))
  67. {
  68. ty.SetValue(a, item.Value.Value);
  69. break;
  70. }
  71. }
  72. return a;
  73. }
  74. }
  75. }