ProtocolProxy.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if (!typeName.Contains("SRM") && !typeName.Contains("RGV"))
  71. {
  72. var f = 0;
  73. }
  74. var dc1 = TypeExtension.EntityClassToDictionary((object)newobj);
  75. db.Default.Insertable(dc1).AS($"{typeName}").ExecuteCommand();
  76. return newobj;
  77. }
  78. private static readonly RedisClient Redis;
  79. public static RedisClient Yg150Redis;
  80. public static RedisClient Ygwms150Redis;
  81. public static RedisClient Ygwcs150Redis;
  82. static ProtocolProxy()
  83. {
  84. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  85. Redis = new RedisClient("127.0.0.1,database=2");
  86. Redis.Serialize = obj =>
  87. {
  88. var bytes = MessagePackSerializer.Serialize(obj);
  89. return bytes;
  90. };
  91. Redis.DeserializeRaw = (bytes, type) =>
  92. {
  93. var obj = MessagePackSerializer.Deserialize(type, bytes);
  94. return obj;
  95. };
  96. Yg150Redis = new RedisClient("127.0.0.1,database=1");
  97. Ygwms150Redis = new RedisClient("127.0.0.1,database=0");
  98. Ygwcs150Redis = new RedisClient("127.0.0.1,database=3");
  99. Ygwcs150Redis.Serialize = obj =>
  100. {
  101. var bytes = MessagePackSerializer.Serialize(obj);
  102. return bytes;
  103. };
  104. Ygwcs150Redis.DeserializeRaw = (bytes, type) =>
  105. {
  106. var obj = MessagePackSerializer.Deserialize(type, bytes);
  107. return obj;
  108. };
  109. }
  110. //static ConcurrentDictionary<string, DeviceData> Datas = new ConcurrentDictionary<string, DeviceData>();
  111. public override void Publish(string code, WCS_PROTOCOLDATA obj)
  112. {
  113. try
  114. {
  115. var datas = AllDatas;
  116. if (code.StartsWith("SRM"))
  117. {
  118. if (!datas.ContainsKey(code))
  119. datas[code] = new SCData { Code = code };
  120. }
  121. else if (code.StartsWith("RGV"))
  122. {
  123. if (!datas.ContainsKey(code))
  124. datas[code] = new RGVData { Code = code };
  125. }
  126. else if (code == "Robot")
  127. {
  128. if (!datas.ContainsKey(code))
  129. datas[code] = new RobotData { Code = code };
  130. }
  131. else if (code.Length == 4)
  132. {
  133. if (!datas.ContainsKey(code))
  134. datas[code] = new StationData { Code = code };
  135. }
  136. if (!datas.ContainsKey(code)) return;
  137. var data = datas[code];
  138. data.Frame = LogicHandler.Frame;
  139. var p = data.GetType().GetProperties().FirstOrDefault(v => v.PropertyType == obj.GetType());
  140. if (p == null)
  141. {
  142. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  143. }
  144. else
  145. {
  146. p.SetValue(data, obj);
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. Console.WriteLine(ex.Message);
  152. }
  153. }
  154. public static ConcurrentDictionary<string, DeviceData> AllDatas = new();
  155. public static void Do()
  156. {
  157. Console.ForegroundColor = ConsoleColor.Green;
  158. Console.WriteLine($"更改:{_total}");
  159. Console.ResetColor();
  160. _total = 0;
  161. try
  162. {
  163. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  164. //Parallel.ForEach(gs, g =>
  165. //{
  166. // var value = g.Select(v => v.Value).ToArray();
  167. // var etype = g.Key;
  168. // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  169. // var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  170. // Redis.Set(etype.Name, coll);
  171. //});
  172. var pack = new DeviceDataPack
  173. {
  174. Frame = LogicHandler.Frame
  175. };
  176. foreach (var g in gs)
  177. {
  178. var value = g.Select(v => v.Value).ToArray();
  179. var etype = g.Key;
  180. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  181. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  182. var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
  183. p.SetValue(pack, coll);
  184. }
  185. var sw = new Stopwatch();
  186. sw.Start();
  187. Redis.Set(nameof(DeviceDataPack), pack);
  188. Ygwcs150Redis.Set(nameof(DeviceDataPack), pack);
  189. sw.Stop();
  190. Console.ForegroundColor = ConsoleColor.Blue;
  191. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  192. Console.ResetColor();
  193. Redis.RPush("Packs", pack);
  194. Ygwcs150Redis.RPush("Packs", pack);
  195. var len = Redis.LLen("Packs");
  196. if (len > 150000)
  197. {
  198. Redis.LTrim("Packs", 20000, len);
  199. Ygwcs150Redis.LTrim("Packs", 20000, len);
  200. }
  201. foreach (var data in AllDatas)
  202. {
  203. data.Value.Info = "";
  204. if (data.Value is not ProdLineData pld) continue;
  205. pld.TaskList.Clear();
  206. pld.Frame = LogicHandler.Frame;
  207. pld.Code = data.Key;
  208. }
  209. }
  210. catch (Exception)
  211. {
  212. // ignored
  213. }
  214. }
  215. }
  216. public class PackInfo
  217. {
  218. public DateTime Frame { get; set; }
  219. public byte[] Data { get; set; }
  220. }
  221. }