ProtocolProxy.cs 7.1 KB

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