ProtocolProxy.cs 9.9 KB

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