ProtocolProxy.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 LiaotcRedis;
  87. //public static RedisClient YGWMS150Redis;
  88. //public static RedisClient YGWCS150Redis;
  89. static ProtocolProxy()
  90. {
  91. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  92. //Redis = new RedisClient("192.168.249.120,password=123456,database=11");
  93. #region 公用云服务器
  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. #endregion 公用云服务器
  106. //#region 私用云服务器
  107. //LiaotcRedis = new RedisClient("liaotc.com,database=10");
  108. //LiaotcRedis.Serialize = obj =>
  109. //{
  110. // var bytes = MessagePackSerializer.Serialize(obj);
  111. // return bytes;
  112. //};
  113. //LiaotcRedis.DeserializeRaw = (bytes, type) =>
  114. //{
  115. // var obj = MessagePackSerializer.Deserialize(type, bytes);
  116. // return obj;
  117. //};
  118. //#endregion 私用云服务器
  119. //#region 永冠服务
  120. //YGWCS150Redis = new RedisClient("192.168.249.150,password=123456,database=10");
  121. //YGWCS150Redis.Serialize = obj =>
  122. //{
  123. // var bytes = MessagePackSerializer.Serialize(obj);
  124. // return bytes;
  125. //};
  126. //YGWCS150Redis.DeserializeRaw = (bytes, type) =>
  127. //{
  128. // var obj = MessagePackSerializer.Deserialize(type, bytes);
  129. // return obj;
  130. //};
  131. //#endregion 永冠服务
  132. }
  133. public override void Publish(string code, WCS_PROTOCOLDATA obj)
  134. {
  135. try
  136. {
  137. var Datas = AllDatas;
  138. if (code.StartsWith("SRM"))
  139. {
  140. if (!Datas.ContainsKey(code))
  141. Datas[code] = new SCData { Code = code };
  142. }
  143. else if (code.StartsWith("RGV"))
  144. {
  145. if (!Datas.ContainsKey(code))
  146. Datas[code] = new RGVData { Code = code };
  147. }
  148. else if (code == "Robot")
  149. {
  150. if (!Datas.ContainsKey(code))
  151. Datas[code] = new RobotData { Code = code };
  152. }
  153. else if (code.Length == 4)
  154. {
  155. if (!Datas.ContainsKey(code))
  156. Datas[code] = new StationData { Code = code };
  157. }
  158. if (Datas.ContainsKey(code))
  159. {
  160. var data = Datas[code];
  161. data.Frame = LogicHandler.Frame;
  162. var p = data.GetType().GetProperties().Where(v => v.PropertyType == obj.GetType()).FirstOrDefault();
  163. if (p == null)
  164. {
  165. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  166. }
  167. else
  168. {
  169. p.SetValue(data, obj);
  170. }
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. Console.WriteLine(ex.Message);
  176. }
  177. }
  178. private static Dictionary<string, string> LastInfo = new Dictionary<string, string>();
  179. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  180. public static void Do()
  181. {
  182. Console.ForegroundColor = ConsoleColor.Green;
  183. Console.WriteLine($"更改:{total}");
  184. Console.ResetColor();
  185. total = 0;
  186. try
  187. {
  188. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  189. //Parallel.ForEach(gs, g =>
  190. //{
  191. // var value = g.Select(v => v.Value).ToArray();
  192. // var etype = g.Key;
  193. // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  194. // var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  195. // Redis.Set(etype.Name, coll);
  196. // YGWCS150Redis.Set(etype.Name, coll);
  197. // LiaotcRedis.Set(etype.Name, coll);
  198. //});
  199. DeviceDataPack pack = new DeviceDataPack();
  200. pack.Frame = LogicHandler.Frame;
  201. foreach (var g in gs)
  202. {
  203. var value = g.Select(v => v.Value).ToArray();
  204. var etype = g.Key;
  205. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  206. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  207. var p = pack.GetType().GetProperties().Where(v => v.PropertyType == type).First();
  208. p.SetValue(pack, coll);
  209. }
  210. var sw = new Stopwatch();
  211. sw.Start();
  212. Redis.Set(nameof(DeviceDataPack), pack);
  213. sw.Stop();
  214. Console.ForegroundColor = ConsoleColor.Blue;
  215. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  216. Console.ResetColor();
  217. //var se = new Stopwatch();
  218. //se.Start();
  219. //YGWCS150Redis.Set(nameof(DeviceDataPack), pack);
  220. //se.Stop();
  221. //Console.ForegroundColor = ConsoleColor.Blue;
  222. //Console.WriteLine($"YGWCS150Redis耗时{se.ElapsedMilliseconds}");
  223. //Console.ResetColor();
  224. //var sr = new Stopwatch();
  225. //sr.Start();
  226. //LiaotcRedis.Set(nameof(DeviceDataPack), pack);
  227. //sr.Stop();
  228. //Console.ForegroundColor = ConsoleColor.Blue;
  229. //Console.WriteLine($"LiaotcRedis耗时{sr.ElapsedMilliseconds}");
  230. //Console.ResetColor();
  231. /////
  232. var st = new Stopwatch();
  233. st.Start();
  234. Redis.RPush("Packs", pack);
  235. st.Stop();
  236. Console.ForegroundColor = ConsoleColor.Blue;
  237. Console.WriteLine($"Redis耗时{st.ElapsedMilliseconds}");
  238. Console.ResetColor();
  239. //var sy = new Stopwatch();
  240. //sy.Start();
  241. //YGWCS150Redis.RPush("Packs", pack);
  242. //sy.Stop();
  243. //Console.ForegroundColor = ConsoleColor.Blue;
  244. //Console.WriteLine($"YGWCS150Redis耗时{sy.ElapsedMilliseconds}");
  245. //Console.ResetColor();
  246. //var su = new Stopwatch();
  247. //su.Start();
  248. //LiaotcRedis.RPush("Packs", pack);
  249. //su.Stop();
  250. //Console.ForegroundColor = ConsoleColor.Blue;
  251. //Console.WriteLine($"LiaotcRedis耗时{su.ElapsedMilliseconds}");
  252. //Console.ResetColor();
  253. //Redis.RPush("Packs", pack);
  254. //YGWCS150Redis.RPush("Packs", pack);
  255. //LiaotcRedis.RPush("Packs", pack);
  256. if (Redis.LLen("Packs") > 100000)
  257. {
  258. Redis.LTrim("Packs", 5000, -1);
  259. }
  260. //if (YGWCS150Redis.LLen("Packs") > 200000)
  261. //{
  262. // YGWCS150Redis.LTrim("Packs", 20000, -1);
  263. //}
  264. //if (LiaotcRedis.LLen("Packs") > 100000)
  265. //{
  266. // LiaotcRedis.LTrim("Packs", 5000, -1);
  267. //}
  268. foreach (var data in AllDatas)
  269. {
  270. LastInfo[data.Key] = data.Value.Info;
  271. data.Value.Info = "";
  272. if (data.Value is ProdLineData)
  273. {
  274. var pld = data.Value as ProdLineData;
  275. pld.TaskList.Clear();
  276. pld.Frame = LogicHandler.Frame;
  277. pld.Code = data.Key;
  278. }
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. }
  284. }
  285. }
  286. public class PackInfo
  287. {
  288. public DateTime Frame { get; set; }
  289. public byte[] Data { get; set; }
  290. }
  291. }