ProtocolProxy.cs 7.2 KB

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