ProtocolProxy.cs 7.9 KB

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