ProtocolProxy.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 (!Datas.ContainsKey(code))
  131. Datas[code] = new RGVData { Code = code };
  132. }
  133. else if (code == "Robot")
  134. {
  135. if (!Datas.ContainsKey(code))
  136. Datas[code] = new RobotData { Code = code };
  137. }
  138. else if (code.Length == 4)
  139. {
  140. if (!Datas.ContainsKey(code))
  141. Datas[code] = new StationData { Code = code };
  142. }
  143. if (Datas.ContainsKey(code))
  144. {
  145. var data = Datas[code];
  146. data.Frame = LogicHandler.Frame;
  147. var p = data.GetType().GetProperties().Where(v => v.PropertyType == obj.GetType()).FirstOrDefault();
  148. if (p == null)
  149. {
  150. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  151. }
  152. else
  153. {
  154. p.SetValue(data, obj);
  155. }
  156. }
  157. //var type = obj.GetType();
  158. ////if (type == typeof(WCS_STATION521))
  159. //{
  160. // var key = type.Name + "," + code;
  161. // var json = JsonConvert.SerializeObject(obj);
  162. // Redis.Set(key, json);
  163. // Redis.Publish("View," + key, json);
  164. //}
  165. }
  166. catch (Exception ex)
  167. {
  168. Console.WriteLine(ex.Message);
  169. }
  170. }
  171. static Dictionary<string, string> LastInfo = new Dictionary<string, string>();
  172. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  173. public static void Do()
  174. {
  175. Console.ForegroundColor = ConsoleColor.Green;
  176. Console.WriteLine($"更改:{total}");
  177. Console.ResetColor();
  178. total = 0;
  179. try
  180. {
  181. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  182. //Parallel.ForEach(gs, g =>
  183. //{
  184. // var value = g.Select(v => v.Value).ToArray();
  185. // var etype = g.Key;
  186. // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  187. // var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  188. // Redis.Set(etype.Name, coll);
  189. //});
  190. DeviceDataPack pack = new DeviceDataPack();
  191. pack.Frame = LogicHandler.Frame;
  192. foreach (var g in gs)
  193. {
  194. var value = g.Select(v => v.Value).ToArray();
  195. var etype = g.Key;
  196. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  197. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  198. var p = pack.GetType().GetProperties().Where(v => v.PropertyType == type).First();
  199. p.SetValue(pack, coll);
  200. }
  201. var sw = new Stopwatch();
  202. sw.Start();
  203. Redis.Set(nameof(DeviceDataPack), pack);
  204. YGWCS150Redis.Set(nameof(DeviceDataPack), pack);
  205. sw.Stop();
  206. Console.ForegroundColor = ConsoleColor.Blue;
  207. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  208. Console.ResetColor();
  209. Redis.RPush("Packs", pack);
  210. YGWCS150Redis.RPush("Packs", pack);
  211. var len = Redis.LLen("Packs");
  212. var len1 = YGWCS150Redis.LLen("Packs");
  213. if (len > 150000)
  214. {
  215. Redis.LTrim("Packs", 20000, len);
  216. }
  217. if (len1 > 150000)
  218. {
  219. YGWCS150Redis.LTrim("Packs", 20000, len);
  220. }
  221. foreach (var data in AllDatas)
  222. {
  223. // LastInfo[data.Key] = data.Value.Info;
  224. data.Value.Info = "";
  225. if (data.Value is ProdLineData)
  226. {
  227. var pld = data.Value as ProdLineData;
  228. pld.TaskList.Clear();
  229. pld.Frame = LogicHandler.Frame;
  230. pld.Code = data.Key;
  231. }
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. }
  237. //Datas.Clear();
  238. }
  239. }
  240. public class PackInfo
  241. {
  242. public DateTime Frame { get; set; }
  243. public byte[] Data { get; set; }
  244. }
  245. }