ProtocolProxy.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using DBHelper;
  2. using FreeRedis;
  3. using MessagePack;
  4. using MessagePack.Resolvers;
  5. using Microsoft.EntityFrameworkCore;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Linq.Dynamic.Core;
  12. using System.Reflection;
  13. using System.Threading.Tasks;
  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 LiaotcRedis;
  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. #region 公用云服务器
  95. Redis = new RedisClient("212.64.66.35,database=10");
  96. Redis.Serialize = obj =>
  97. {
  98. var bytes = MessagePackSerializer.Serialize(obj);
  99. return bytes;
  100. };
  101. Redis.DeserializeRaw = (bytes, type) =>
  102. {
  103. var obj = MessagePackSerializer.Deserialize(type, bytes);
  104. return obj;
  105. };
  106. #endregion 公用云服务器
  107. #region 私用云服务器
  108. LiaotcRedis = new RedisClient("liaotc.com,database=10");
  109. LiaotcRedis.Serialize = obj =>
  110. {
  111. var bytes = MessagePackSerializer.Serialize(obj);
  112. return bytes;
  113. };
  114. LiaotcRedis.DeserializeRaw = (bytes, type) =>
  115. {
  116. var obj = MessagePackSerializer.Deserialize(type, bytes);
  117. return obj;
  118. };
  119. #endregion 私用云服务器
  120. #region 永冠服务
  121. YGWCS150Redis = new RedisClient("192.168.249.150,password=123456,database=10");
  122. YGWCS150Redis.Serialize = obj =>
  123. {
  124. var bytes = MessagePackSerializer.Serialize(obj);
  125. return bytes;
  126. };
  127. YGWCS150Redis.DeserializeRaw = (bytes, type) =>
  128. {
  129. var obj = MessagePackSerializer.Deserialize(type, bytes);
  130. return obj;
  131. };
  132. #endregion 永冠服务
  133. }
  134. public override void Publish(string code, WCS_PROTOCOLDATA obj)
  135. {
  136. try
  137. {
  138. var Datas = AllDatas;
  139. if (code.StartsWith("SRM"))
  140. {
  141. if (!Datas.ContainsKey(code))
  142. Datas[code] = new SCData { Code = code };
  143. }
  144. else if (code.StartsWith("RGV"))
  145. {
  146. if (!Datas.ContainsKey(code))
  147. Datas[code] = new RGVData { Code = code };
  148. }
  149. else if (code == "Robot")
  150. {
  151. if (!Datas.ContainsKey(code))
  152. Datas[code] = new RobotData { Code = code };
  153. }
  154. else if (code.Length == 4)
  155. {
  156. if (!Datas.ContainsKey(code))
  157. Datas[code] = new StationData { Code = code };
  158. }
  159. if (Datas.ContainsKey(code))
  160. {
  161. var data = Datas[code];
  162. data.Frame = LogicHandler.Frame;
  163. var p = data.GetType().GetProperties().Where(v => v.PropertyType == obj.GetType()).FirstOrDefault();
  164. if (p == null)
  165. {
  166. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  167. }
  168. else
  169. {
  170. p.SetValue(data, obj);
  171. }
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. Console.WriteLine(ex.Message);
  177. }
  178. }
  179. private 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. YGWCS150Redis.Set(etype.Name, coll);
  198. LiaotcRedis.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. LiaotcRedis.Set(nameof(DeviceDataPack), pack);
  216. sw.Stop();
  217. Console.ForegroundColor = ConsoleColor.Blue;
  218. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  219. Console.ResetColor();
  220. Redis.RPush("Packs", pack);
  221. YGWCS150Redis.RPush("Packs", pack);
  222. LiaotcRedis.RPush("Packs", pack);
  223. var len = Redis.LLen("Packs");
  224. if (len > 150000)
  225. {
  226. Redis.LTrim("Packs", 20000, len);
  227. }
  228. var len1 = YGWCS150Redis.LLen("Packs");
  229. if (len1 > 150000)
  230. {
  231. YGWCS150Redis.LTrim("Packs", 20000, len1);
  232. }
  233. var len2 = LiaotcRedis.LLen("Packs");
  234. if (len2 > 150000)
  235. {
  236. LiaotcRedis.LTrim("Packs", 20000, len2);
  237. }
  238. foreach (var data in AllDatas)
  239. {
  240. LastInfo[data.Key] = data.Value.Info;
  241. data.Value.Info = "";
  242. if (data.Value is ProdLineData)
  243. {
  244. var pld = data.Value as ProdLineData;
  245. pld.TaskList.Clear();
  246. pld.Frame = LogicHandler.Frame;
  247. pld.Code = data.Key;
  248. }
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. }
  254. }
  255. }
  256. public class PackInfo
  257. {
  258. public DateTime Frame { get; set; }
  259. public byte[] Data { get; set; }
  260. }
  261. }