ProtocolProxy.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 ConcurrentDictionary<Type, object[]>();
  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. //static ConcurrentQueue<PackInfo> Packs = new ConcurrentQueue<PackInfo>();
  76. private static Dictionary<string, Playerback> _clients = new Dictionary<string, Playerback>();
  77. private static readonly RedisClient Redis;
  78. public static RedisClient Yg150Redis;
  79. public static RedisClient Ygwms150Redis;
  80. public static RedisClient Ygwcs150Redis;
  81. static ProtocolProxy()
  82. {
  83. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  84. //Redis = new RedisClient("192.168.249.120,password=123456,database=11");
  85. Redis = new RedisClient("212.64.66.35,database=10");
  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("192.168.249.150,password=123456,database=1");
  97. Ygwms150Redis = new RedisClient("192.168.249.150,password=123456,database=0");
  98. Ygwcs150Redis = new RedisClient("192.168.249.150,password=123456,database=10");
  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. private static Dictionary<string, string> _lastInfo = new Dictionary<string, string>();
  155. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  156. public static void Do()
  157. {
  158. Console.ForegroundColor = ConsoleColor.Green;
  159. Console.WriteLine($"更改:{_total}");
  160. Console.ResetColor();
  161. _total = 0;
  162. try
  163. {
  164. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  165. //Parallel.ForEach(gs, g =>
  166. //{
  167. // var value = g.Select(v => v.Value).ToArray();
  168. // var etype = g.Key;
  169. // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  170. // var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  171. // Redis.Set(etype.Name, coll);
  172. //});
  173. var pack = new DeviceDataPack
  174. {
  175. Frame = LogicHandler.Frame
  176. };
  177. foreach (var g in gs)
  178. {
  179. var value = g.Select(v => v.Value).ToArray();
  180. var etype = g.Key;
  181. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  182. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  183. var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
  184. p.SetValue(pack, coll);
  185. }
  186. var sw = new Stopwatch();
  187. sw.Start();
  188. Redis.Set(nameof(DeviceDataPack), pack);
  189. Ygwcs150Redis.Set(nameof(DeviceDataPack), pack);
  190. sw.Stop();
  191. Console.ForegroundColor = ConsoleColor.Blue;
  192. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  193. Console.ResetColor();
  194. Redis.RPush("Packs", pack);
  195. Ygwcs150Redis.RPush("Packs", pack);
  196. var len = Redis.LLen("Packs");
  197. if (len > 150000)
  198. {
  199. Redis.LTrim("Packs", 20000, len);
  200. Ygwcs150Redis.LTrim("Packs", 20000, len);
  201. }
  202. foreach (var data in AllDatas)
  203. {
  204. // LastInfo[data.Key] = data.Value.Info;
  205. data.Value.Info = "";
  206. if (data.Value is not ProdLineData pld) continue;
  207. pld.TaskList.Clear();
  208. pld.Frame = LogicHandler.Frame;
  209. pld.Code = data.Key;
  210. }
  211. }
  212. catch (Exception)
  213. {
  214. }
  215. }
  216. }
  217. public class PackInfo
  218. {
  219. public DateTime Frame { get; set; }
  220. public byte[] Data { get; set; }
  221. }
  222. }