ProtocolProxy.cs 9.6 KB

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