ProtocolProxy.cs 9.8 KB

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