ProtocolProxy.cs 8.6 KB

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