ProtocolProxy.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using DBHelper;
  2. using FreeRedis;
  3. using MessagePack;
  4. using MessagePack.Resolvers;
  5. using Microsoft.CodeAnalysis.Differencing;
  6. using Microsoft.EntityFrameworkCore;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Linq.Dynamic.Core;
  13. using System.Reflection;
  14. using System.Threading.Tasks;
  15. using WCS.Core;
  16. using WCS.Entity;
  17. using WCS.Entity.Protocol;
  18. namespace WCS.Service
  19. {
  20. public class ProtocolProxy : ProtocolProxyBase
  21. {
  22. private MethodInfo SetMethod;
  23. public ProtocolProxy(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL protocol) : base(id, db, start, protocol)
  24. {
  25. SetMethod = typeof(DbContext).GetMethod("Set", new Type[] { }).MakeGenericMethod(this.ProtocolDataType);
  26. }
  27. private static ConcurrentDictionary<Type, object[]> LastDatas = new ConcurrentDictionary<Type, object[]>();
  28. protected override WCS_PROTOCOLDATA GetLastData(DB db)
  29. {
  30. if (!LastDatas.ContainsKey(this.ProtocolDataType))
  31. {
  32. dynamic q = SetMethod.Invoke(db.Default, null);
  33. try
  34. {
  35. q = DynamicQueryableExtensions.Where(q, "ISLAST==@0", true);
  36. q = DynamicQueryableExtensions.OrderBy(q, "ID");
  37. q = DynamicQueryableExtensions.Select(q, "new(it.DEVICE.CODE as Code,it as Data)");
  38. var arr = Enumerable.ToArray(q);
  39. LastDatas[this.ProtocolDataType] = arr;
  40. }
  41. catch (Exception ex)
  42. {
  43. throw;
  44. }
  45. }
  46. dynamic datas = LastDatas[this.ProtocolDataType];
  47. var list = new List<WCS_PROTOCOLDATA>();
  48. foreach (var data in datas)
  49. {
  50. if (data.Code == PROTOCOL.DEVICE.CODE)
  51. list.Add(data.Data);
  52. }
  53. if (list.Count > 1)
  54. {
  55. for (int i = 0; i < list.Count - 1; i++)
  56. {
  57. var obj = list[i];
  58. db.Default.Attach(obj);
  59. obj.ISLAST = false;
  60. }
  61. db.Default.SaveChangesAsync();
  62. }
  63. var res = Enumerable.LastOrDefault(list);
  64. return res;
  65. }
  66. private static int total;
  67. protected override WCS_PROTOCOLDATA SaveNewData(DB db, WCS_PROTOCOLDATA last, WCS_PROTOCOLDATA newobj, string user)
  68. {
  69. total++;
  70. if (last != null)
  71. {
  72. db.Default.Attach(last);
  73. last.ISLAST = false;
  74. }
  75. db.Default.Attach(PROTOCOL.DEVICE);
  76. newobj.DEVICE = PROTOCOL.DEVICE;
  77. newobj.ISLAST = true;
  78. newobj.UPDATETIME = DateTime.Now;
  79. newobj.UPDATEUSER = user;
  80. newobj.FRAME = LogicHandler.Frame;
  81. db.Default.Add(newobj);
  82. db.Default.SaveChangesAsync();
  83. return newobj;
  84. }
  85. private static Dictionary<string, Playerback> Clients = new Dictionary<string, Playerback>();
  86. public static RedisClient YG150Redis;
  87. public static RedisClient YGWMS150Redis;
  88. static ProtocolProxy()
  89. {
  90. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  91. YG150Redis = new RedisClient("192.168.249.150,password=123456,database=1");
  92. YGWMS150Redis = new RedisClient("192.168.249.150,password=123456,database=0");
  93. }
  94. public override void Publish(string code, WCS_PROTOCOLDATA obj)
  95. {
  96. try
  97. {
  98. var Datas = AllDatas;
  99. if (code.StartsWith("SRM"))
  100. {
  101. if (!Datas.ContainsKey(code))
  102. Datas[code] = new SCData { Code = code };
  103. }
  104. else if (code.StartsWith("RGV"))
  105. {
  106. if (!Datas.ContainsKey(code))
  107. Datas[code] = new RGVData { Code = code };
  108. }
  109. else if (code == "Robot")
  110. {
  111. if (!Datas.ContainsKey(code))
  112. Datas[code] = new RobotData { Code = code };
  113. }
  114. else if (code.Length == 4)
  115. {
  116. if (!Datas.ContainsKey(code))
  117. Datas[code] = new StationData { Code = code };
  118. }
  119. if (Datas.ContainsKey(code))
  120. {
  121. var data = Datas[code];
  122. data.Frame = LogicHandler.Frame;
  123. var p = data.GetType().GetProperties().Where(v => v.PropertyType == obj.GetType()).FirstOrDefault();
  124. if (p == null)
  125. {
  126. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  127. }
  128. else
  129. {
  130. p.SetValue(data, obj);
  131. }
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. Console.WriteLine(ex.Message);
  137. }
  138. }
  139. private static Dictionary<string, string> LastInfo = new Dictionary<string, string>();
  140. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  141. public static void Do()
  142. {
  143. Console.ForegroundColor = ConsoleColor.Green;
  144. Console.WriteLine($"更改:{total}");
  145. Console.ResetColor();
  146. total = 0;
  147. try
  148. {
  149. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  150. DeviceDataPack pack = new DeviceDataPack();
  151. pack.Frame = LogicHandler.Frame;
  152. foreach (var g in gs)
  153. {
  154. var value = g.Select(v => v.Value).ToArray();
  155. var etype = g.Key;
  156. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  157. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  158. var p = pack.GetType().GetProperties().Where(v => v.PropertyType == type).First();
  159. p.SetValue(pack, coll);
  160. }
  161. var sw = new Stopwatch();
  162. sw.Start();
  163. sw.Stop();
  164. Console.ForegroundColor = ConsoleColor.Blue;
  165. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  166. Console.ResetColor();
  167. foreach (var data in AllDatas)
  168. {
  169. LastInfo[data.Key] = data.Value.Info;
  170. data.Value.Info = "";
  171. if (data.Value is ProdLineData)
  172. {
  173. var pld = data.Value as ProdLineData;
  174. pld.TaskList.Clear();
  175. pld.Frame = LogicHandler.Frame;
  176. pld.Code = data.Key;
  177. }
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. }
  183. }
  184. }
  185. public class PackInfo
  186. {
  187. public DateTime Frame { get; set; }
  188. public byte[] Data { get; set; }
  189. }
  190. }