ProtocolProxy.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. public static RedisClient WMS120Redis;
  92. static ProtocolProxy()
  93. {
  94. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  95. Redis = new RedisClient("212.64.66.35,database=11");
  96. WMS120Redis = new RedisClient("192.168.249.120,password=123456,database=0");
  97. Redis.Serialize = obj =>
  98. {
  99. var bytes = MessagePackSerializer.Serialize(obj);
  100. return bytes;
  101. };
  102. Redis.DeserializeRaw = (bytes, type) =>
  103. {
  104. var obj = MessagePackSerializer.Deserialize(type, bytes);
  105. return obj;
  106. };
  107. YGWCS150Redis = new RedisClient("192.168.249.120,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("SC"))
  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. //var type = obj.GetType();
  160. ////if (type == typeof(WCS_STATION521))
  161. //{
  162. // var key = type.Name + "," + code;
  163. // var json = JsonConvert.SerializeObject(obj);
  164. // Redis.Set(key, json);
  165. // Redis.Publish("View," + key, json);
  166. //}
  167. }
  168. catch (Exception ex)
  169. {
  170. Console.WriteLine(ex.Message);
  171. }
  172. }
  173. static Dictionary<string, string> LastInfo = new Dictionary<string, string>();
  174. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  175. public static void Do()
  176. {
  177. Console.ForegroundColor = ConsoleColor.Green;
  178. Console.WriteLine($"更改:{total}");
  179. Console.ResetColor();
  180. total = 0;
  181. try
  182. {
  183. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  184. //Parallel.ForEach(gs, g =>
  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. // Redis.Set(etype.Name, coll);
  191. //});
  192. DeviceDataPack pack = new DeviceDataPack();
  193. pack.Frame = LogicHandler.Frame;
  194. foreach (var g in gs)
  195. {
  196. var value = g.Select(v => v.Value).ToArray();
  197. var etype = g.Key;
  198. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  199. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  200. var p = pack.GetType().GetProperties().Where(v => v.PropertyType == type).First();
  201. p.SetValue(pack, coll);
  202. }
  203. var sw = new Stopwatch();
  204. sw.Start();
  205. Redis.Set(nameof(DeviceDataPack), pack);
  206. YGWCS150Redis.Set(nameof(DeviceDataPack), pack);
  207. sw.Stop();
  208. Console.ForegroundColor = ConsoleColor.Blue;
  209. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  210. Console.ResetColor();
  211. Redis.RPush("Packs", pack);
  212. YGWCS150Redis.RPush("Packs", pack);
  213. var len = Redis.LLen("Packs");
  214. var len1 = YGWCS150Redis.LLen("Packs");
  215. if (len > 150000)
  216. {
  217. Redis.LTrim("Packs", 20000, len);
  218. }
  219. if (len1 > 150000)
  220. {
  221. YGWCS150Redis.LTrim("Packs", 20000, len);
  222. }
  223. foreach (var data in AllDatas)
  224. {
  225. // LastInfo[data.Key] = data.Value.Info;
  226. data.Value.Info = "";
  227. if (data.Value is ProdLineData)
  228. {
  229. var pld = data.Value as ProdLineData;
  230. pld.TaskList.Clear();
  231. pld.Frame = LogicHandler.Frame;
  232. pld.Code = data.Key;
  233. }
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. }
  239. //Datas.Clear();
  240. }
  241. }
  242. public class PackInfo
  243. {
  244. public DateTime Frame { get; set; }
  245. public byte[] Data { get; set; }
  246. }
  247. }