ProtocolProxy.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using DBHelper;
  2. using FreeRedis;
  3. using MessagePack;
  4. using MessagePack.Resolvers;
  5. using Microsoft.EntityFrameworkCore;
  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.Entity;
  15. using WCS.Entity.Protocol;
  16. namespace WCS.Service
  17. {
  18. public class ProtocolProxy : ProtocolProxyBase
  19. {
  20. private MethodInfo SetMethod;
  21. public ProtocolProxy(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL protocol) : base(id, db, start, protocol)
  22. {
  23. SetMethod = typeof(DbContext).GetMethod("Set", new Type[] { }).MakeGenericMethod(this.ProtocolDataType);
  24. }
  25. private static ConcurrentDictionary<Type, object[]> LastDatas = new ConcurrentDictionary<Type, object[]>();
  26. protected override WCS_PROTOCOLDATA GetLastData(DB db)
  27. {
  28. if (!LastDatas.ContainsKey(this.ProtocolDataType))
  29. {
  30. dynamic q = SetMethod.Invoke(db.Default, null);
  31. try
  32. {
  33. q = DynamicQueryableExtensions.Where(q, "ISLAST==@0", true);
  34. q = DynamicQueryableExtensions.OrderBy(q, "ID");
  35. q = DynamicQueryableExtensions.Select(q, "new(it.DEVICE.CODE as Code,it as Data)");
  36. var arr = Enumerable.ToArray(q);
  37. LastDatas[this.ProtocolDataType] = arr;
  38. }
  39. catch (Exception ex)
  40. {
  41. throw;
  42. }
  43. }
  44. dynamic datas = LastDatas[this.ProtocolDataType];
  45. var list = new List<WCS_PROTOCOLDATA>();
  46. foreach (var data in datas)
  47. {
  48. if (data.Code == PROTOCOL.DEVICE.CODE)
  49. list.Add(data.Data);
  50. }
  51. if (list.Count > 1)
  52. {
  53. for (int i = 0; i < list.Count - 1; i++)
  54. {
  55. var obj = list[i];
  56. db.Default.Attach(obj);
  57. obj.ISLAST = false;
  58. }
  59. db.Default.SaveChanges();
  60. }
  61. var res = Enumerable.LastOrDefault(list);
  62. return res;
  63. }
  64. private static int total;
  65. protected override WCS_PROTOCOLDATA SaveNewData(DB db, WCS_PROTOCOLDATA last, WCS_PROTOCOLDATA newobj, string user)
  66. {
  67. total++;
  68. if (last != null)
  69. {
  70. db.Default.Attach(last);
  71. last.ISLAST = false;
  72. }
  73. db.Default.Attach(PROTOCOL.DEVICE);
  74. newobj.DEVICE = PROTOCOL.DEVICE;
  75. newobj.ISLAST = true;
  76. newobj.UPDATETIME = DateTime.Now;
  77. newobj.UPDATEUSER = user;
  78. newobj.FRAME = LogicHandler.Frame;
  79. db.Default.Add(newobj);
  80. db.Default.SaveChanges();
  81. return newobj;
  82. }
  83. //static ConcurrentQueue<PackInfo> Packs = new ConcurrentQueue<PackInfo>();
  84. private static Dictionary<string, Playerback> Clients = new Dictionary<string, Playerback>();
  85. private static RedisClient Redis;
  86. public static RedisClient YGWCS150Redis;
  87. public static RedisClient WMS120Redis;
  88. static ProtocolProxy()
  89. {
  90. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  91. Redis = new RedisClient("212.64.66.35,password=Aa123456,database=11");
  92. WMS120Redis = new RedisClient("192.168.249.120,password=123456,database=0");
  93. Redis.Serialize = obj =>
  94. {
  95. var bytes = MessagePackSerializer.Serialize(obj);
  96. return bytes;
  97. };
  98. Redis.DeserializeRaw = (bytes, type) =>
  99. {
  100. var obj = MessagePackSerializer.Deserialize(type, bytes);
  101. return obj;
  102. };
  103. YGWCS150Redis = new RedisClient("192.168.249.120,password=123456,database=10");
  104. YGWCS150Redis.Serialize = obj =>
  105. {
  106. var bytes = MessagePackSerializer.Serialize(obj);
  107. return bytes;
  108. };
  109. YGWCS150Redis.DeserializeRaw = (bytes, type) =>
  110. {
  111. var obj = MessagePackSerializer.Deserialize(type, bytes);
  112. return obj;
  113. };
  114. }
  115. //static ConcurrentDictionary<string, DeviceData> Datas = new ConcurrentDictionary<string, DeviceData>();
  116. public override void Publish(string code, WCS_PROTOCOLDATA obj)
  117. {
  118. try
  119. {
  120. var Datas = AllDatas;
  121. if (code.StartsWith("SC"))
  122. {
  123. if (!Datas.ContainsKey(code))
  124. Datas[code] = new SCData { Code = code };
  125. }
  126. else if (code.StartsWith("RGV"))
  127. {
  128. if (code is "RGV4" or "RGV6")
  129. {
  130. if (!Datas.ContainsKey(code))
  131. Datas[code] = new DRGVData { Code = code };
  132. }
  133. else
  134. {
  135. if (!Datas.ContainsKey(code))
  136. Datas[code] = new RGVData { Code = code };
  137. }
  138. }
  139. else if (code == "Robot")
  140. {
  141. if (!Datas.ContainsKey(code))
  142. Datas[code] = new RobotData { Code = code };
  143. }
  144. else if (code.Length == 4)
  145. {
  146. if (!Datas.ContainsKey(code))
  147. Datas[code] = new StationData { Code = code };
  148. }
  149. if (Datas.ContainsKey(code))
  150. {
  151. var data = Datas[code];
  152. data.Frame = LogicHandler.Frame;
  153. var p = data.GetType().GetProperties().Where(v => v.PropertyType == obj.GetType()).FirstOrDefault();
  154. if (p == null)
  155. {
  156. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  157. }
  158. else
  159. {
  160. p.SetValue(data, obj);
  161. }
  162. }
  163. //var type = obj.GetType();
  164. ////if (type == typeof(WCS_STATION521))
  165. //{
  166. // var key = type.Name + "," + code;
  167. // var json = JsonConvert.SerializeObject(obj);
  168. // Redis.Set(key, json);
  169. // Redis.Publish("View," + key, json);
  170. //}
  171. }
  172. catch (Exception ex)
  173. {
  174. Console.WriteLine(ex.Message);
  175. }
  176. }
  177. private static Dictionary<string, string> LastInfo = new Dictionary<string, string>();
  178. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  179. public static void Do()
  180. {
  181. Console.ForegroundColor = ConsoleColor.Green;
  182. Console.WriteLine($"更改:{total}");
  183. Console.ResetColor();
  184. total = 0;
  185. try
  186. {
  187. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  188. //Parallel.ForEach(gs, g =>
  189. //{
  190. // var value = g.Select(v => v.Value).ToArray();
  191. // var etype = g.Key;
  192. // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  193. // var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  194. // Redis.Set(etype.Name, coll);
  195. //});
  196. DeviceDataPack pack = new DeviceDataPack();
  197. pack.Frame = LogicHandler.Frame;
  198. foreach (var g in gs)
  199. {
  200. var value = g.Select(v => v.Value).ToArray();
  201. var etype = g.Key;
  202. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  203. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  204. var p = pack.GetType().GetProperties().Where(v => v.PropertyType == type).First();
  205. p.SetValue(pack, coll);
  206. }
  207. var sw = new Stopwatch();
  208. sw.Start();
  209. YGWCS150Redis.Set(nameof(DeviceDataPack), pack);
  210. Redis.Set(nameof(DeviceDataPack), pack);
  211. sw.Stop();
  212. Console.ForegroundColor = ConsoleColor.Blue;
  213. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  214. Console.ResetColor();
  215. YGWCS150Redis.RPush("Packs", pack);
  216. Redis.RPush("Packs", pack);
  217. var len1 = YGWCS150Redis.LLen("Packs");
  218. var len = Redis.LLen("Packs");
  219. if (len1 > 150000)
  220. {
  221. YGWCS150Redis.LTrim("Packs", 20000, len);
  222. }
  223. if (len > 150000)
  224. {
  225. Redis.LTrim("Packs", 20000, len);
  226. }
  227. foreach (var data in AllDatas)
  228. {
  229. // LastInfo[data.Key] = data.Value.Info;
  230. data.Value.Info = "";
  231. if (data.Value is ProdLineData)
  232. {
  233. var pld = data.Value as ProdLineData;
  234. pld.TaskList.Clear();
  235. pld.Frame = LogicHandler.Frame;
  236. pld.Code = data.Key;
  237. }
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. }
  243. //Datas.Clear();
  244. }
  245. }
  246. public class PackInfo
  247. {
  248. public DateTime Frame { get; set; }
  249. public byte[] Data { get; set; }
  250. }
  251. }