ProtocolProxy.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using DBHelper;
  2. using FreeRedis;
  3. using MessagePack;
  4. using MessagePack.Resolvers;
  5. using Microsoft.CodeAnalysis.Differencing;
  6. using Microsoft.EntityFrameworkCore;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Linq.Dynamic.Core;
  13. using System.Reflection;
  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. YG150Redis = new RedisClient("192.168.249.150,password=123456,database=1");
  106. YGWMS150Redis = new RedisClient("192.168.249.150,password=123456,database=0");
  107. YGWCS150Redis = new RedisClient("192.168.249.150,password=123456,database=10");
  108. YGWCS150Redis.Serialize = obj =>
  109. {
  110. var bytes = MessagePackSerializer.Serialize(obj);
  111. return bytes;
  112. };
  113. YGWCS150Redis.DeserializeRaw = (bytes, type) =>
  114. {
  115. var obj = MessagePackSerializer.Deserialize(type, bytes);
  116. return obj;
  117. };
  118. }
  119. //static ConcurrentDictionary<string, DeviceData> Datas = new ConcurrentDictionary<string, DeviceData>();
  120. public override void Publish(string code, WCS_PROTOCOLDATA obj)
  121. {
  122. try
  123. {
  124. var Datas = AllDatas;
  125. if (code.StartsWith("SRM"))
  126. {
  127. if (!Datas.ContainsKey(code))
  128. Datas[code] = new SCData { Code = code };
  129. }
  130. else if (code.StartsWith("RGV"))
  131. {
  132. if (!Datas.ContainsKey(code))
  133. Datas[code] = new RGVData { Code = code };
  134. }
  135. else if (code == "Robot")
  136. {
  137. if (!Datas.ContainsKey(code))
  138. Datas[code] = new RobotData { Code = code };
  139. }
  140. else if (code.Length == 4)
  141. {
  142. if (!Datas.ContainsKey(code))
  143. Datas[code] = new StationData { Code = code };
  144. }
  145. if (Datas.ContainsKey(code))
  146. {
  147. var data = Datas[code];
  148. data.Frame = LogicHandler.Frame;
  149. var p = data.GetType().GetProperties().Where(v => v.PropertyType == obj.GetType()).FirstOrDefault();
  150. if (p == null)
  151. {
  152. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  153. }
  154. else
  155. {
  156. p.SetValue(data, obj);
  157. }
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. Console.WriteLine(ex.Message);
  163. }
  164. }
  165. private static Dictionary<string, string> LastInfo = new Dictionary<string, string>();
  166. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  167. public static void Do()
  168. {
  169. Console.ForegroundColor = ConsoleColor.Green;
  170. Console.WriteLine($"更改:{total}");
  171. Console.ResetColor();
  172. total = 0;
  173. try
  174. {
  175. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  176. //Parallel.ForEach(gs, g =>
  177. //{
  178. // var value = g.Select(v => v.Value).ToArray();
  179. // var etype = g.Key;
  180. // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  181. // var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  182. // Redis.Set(etype.Name, coll);
  183. //});
  184. DeviceDataPack pack = new DeviceDataPack();
  185. pack.Frame = LogicHandler.Frame;
  186. foreach (var g in gs)
  187. {
  188. var value = g.Select(v => v.Value).ToArray();
  189. var etype = g.Key;
  190. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  191. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  192. var p = pack.GetType().GetProperties().Where(v => v.PropertyType == type).First();
  193. p.SetValue(pack, coll);
  194. }
  195. var sw = new Stopwatch();
  196. sw.Start();
  197. //Redis.Set(nameof(DeviceDataPack), pack);
  198. YGWCS150Redis.Set(nameof(DeviceDataPack), pack);
  199. sw.Stop();
  200. Console.ForegroundColor = ConsoleColor.Blue;
  201. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  202. Console.ResetColor();
  203. //Redis.RPush("Packs", pack);
  204. YGWCS150Redis.RPush("Packs", pack);
  205. //var len = Redis.LLen("Packs");
  206. //if (len > 150000)
  207. //{
  208. // Redis.LTrim("Packs", 20000, len);
  209. //}
  210. var len1 = YGWCS150Redis.LLen("Packs");
  211. if (len1 > 150000)
  212. {
  213. YGWCS150Redis.LTrim("Packs", 20000, len1);
  214. }
  215. foreach (var data in AllDatas)
  216. {
  217. // LastInfo[data.Key] = data.Value.Info;
  218. data.Value.Info = "";
  219. if (data.Value is ProdLineData)
  220. {
  221. var pld = data.Value as ProdLineData;
  222. pld.TaskList.Clear();
  223. pld.Frame = LogicHandler.Frame;
  224. pld.Code = data.Key;
  225. }
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. }
  231. //Datas.Clear();
  232. }
  233. }
  234. public class PackInfo
  235. {
  236. public DateTime Frame { get; set; }
  237. public byte[] Data { get; set; }
  238. }
  239. }