ProtocolProxy.cs 9.2 KB

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