ProtocolProxy.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using MessagePack;
  2. using MessagePack.Resolvers;
  3. using Newtonsoft.Json;
  4. using System.Collections.Concurrent;
  5. using System.Diagnostics;
  6. using System.Linq.Dynamic.Core;
  7. using WCS.Core;
  8. using WCS.Core.BaseExtensions;
  9. using WCS.Core.DataTrans;
  10. using WCS.Core.DbHelper;
  11. using WCS.Core.Redis;
  12. using WCS.Entity;
  13. using WCS.Entity.Protocol;
  14. namespace WCS.Service
  15. {
  16. /// <summary>
  17. /// 代理协议
  18. /// </summary>
  19. public class ProtocolProxy : ProtocolProxyBase
  20. {
  21. public ProtocolProxy(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL protocol) : base(id, db, start, protocol)
  22. {
  23. }
  24. private static readonly ConcurrentDictionary<Type, object[]> LastDatas = new();
  25. /// <summary>
  26. /// 获取新的数据
  27. /// </summary>
  28. /// <param name="db"></param>
  29. /// <returns></returns>
  30. protected override WCS_PROTOCOLDATA GetLastData(Db db)
  31. {
  32. if (!LastDatas.ContainsKey(this.ProtocolDataType))
  33. {
  34. LastDatas[this.ProtocolDataType] = db.Default.Queryable<dynamic>().AS($"{this.ProtocolDataType.Name}", "it").Where("ISLAST=0").OrderBy("ID").ToArray();
  35. }
  36. dynamic data = LastDatas[this.ProtocolDataType];
  37. var list = new List<WCS_PROTOCOLDATA>();
  38. foreach (var itemData in data)
  39. {
  40. try
  41. {
  42. if (itemData.DEVICECOD == Protocol.DEVICE.CODE)
  43. list.Add(itemData);
  44. }
  45. catch
  46. {
  47. // ignored
  48. }
  49. }
  50. if (list.Count > 1)
  51. {
  52. for (var i = 0; i < list.Count - 1; i++)
  53. {
  54. var obj = list[i];
  55. db.Default.Insertable((object)obj).ExecuteCommand();
  56. obj.ISLAST = false;
  57. }
  58. }
  59. var res = list.LastOrDefault();
  60. return res;
  61. }
  62. private static int _total;
  63. protected override WCS_PROTOCOLDATA SaveNewData(Db db, WCS_PROTOCOLDATA last, WCS_PROTOCOLDATA newobj, string user)
  64. {
  65. _total++;
  66. if (last != null)
  67. {
  68. var dc = TypeExtension.EntityClassToDictionary((object)last);
  69. db.Default.Insertable(dc).AS($"{last.GetType().Name}").ExecuteCommand();
  70. last.ISLAST = false;
  71. }
  72. newobj.DEVICECOD = Protocol.DEVICE.CODE;
  73. newobj.ISLAST = true;
  74. newobj.UPDATETIME = DateTime.Now;
  75. newobj.UPDATEUSER = user;
  76. newobj.FRAME = LogicHandler.Frame;
  77. var typeName = newobj.GetType().Name;
  78. var dc1 = TypeExtension.EntityClassToDictionary((object)newobj);
  79. db.Default.Insertable(dc1).AS($"{typeName}").ExecuteCommand();
  80. return newobj;
  81. }
  82. static ProtocolProxy()
  83. {
  84. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  85. #region 初始化Redis连接
  86. var redisConnectionStrings = RedisHelper.Default.Check("RedisConnectionStrings") ?? throw new Exception("请在Redis中配置RedisConnectionStrings");
  87. Configs.RedisConnectionStrings = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(redisConnectionStrings);
  88. if (Configs.RedisConnectionStrings != null)
  89. {
  90. if (Configs.RedisConnectionStrings.All(v => v.Key != "Monitor")) throw new Exception("请在RedisConnectionStrings中配置监控RedisDB库连接字符串");
  91. }
  92. foreach (var redisConnection in Configs.RedisConnectionStrings!)
  93. {
  94. RedisHelper.CreateContext(redisConnection.ConnectionString, redisConnection.Key);
  95. switch (redisConnection.Key)
  96. {
  97. case "Monitor":
  98. RedisHelper.SetMonitorContextType(redisConnection.Key);
  99. RedisHelper.Monitor.Serialize = obj =>
  100. {
  101. var bytes = MessagePackSerializer.Serialize(obj);
  102. return bytes;
  103. };
  104. RedisHelper.Monitor.DeserializeRaw = (bytes, type) =>
  105. {
  106. var obj = MessagePackSerializer.Deserialize(type, bytes);
  107. return obj;
  108. };
  109. break;
  110. }
  111. }
  112. #endregion 初始化Redis连接
  113. }
  114. public override void Publish(string code, WCS_PROTOCOLDATA obj)
  115. {
  116. try
  117. {
  118. var datas = AllDatas;
  119. if (code.StartsWith("SRM"))
  120. {
  121. if (!datas.ContainsKey(code))
  122. datas[code] = new SCData { Code = code };
  123. }
  124. else if (code.StartsWith("RGV"))
  125. {
  126. if (!datas.ContainsKey(code))
  127. datas[code] = new RGVData { Code = code };
  128. }
  129. else if (code == "Robot")
  130. {
  131. if (!datas.ContainsKey(code))
  132. datas[code] = new RobotData { Code = code };
  133. }
  134. else if (code.Length == 4)
  135. {
  136. if (!datas.ContainsKey(code))
  137. datas[code] = new StationData { Code = code };
  138. }
  139. if (!datas.ContainsKey(code)) return;
  140. var data = datas[code];
  141. data.Frame = LogicHandler.Frame;
  142. var p = data.GetType().GetProperties().FirstOrDefault(v => v.PropertyType == obj.GetType());
  143. if (p == null)
  144. {
  145. Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性");
  146. }
  147. else
  148. {
  149. p.SetValue(data, obj);
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154. Console.WriteLine(ex.Message);
  155. }
  156. }
  157. /// <summary>
  158. /// 所有的设备数据
  159. /// </summary>
  160. public static ConcurrentDictionary<string, DeviceData> AllDatas = new();
  161. public static void Do()
  162. {
  163. Console.ForegroundColor = ConsoleColor.Green;
  164. Console.WriteLine($"更改:{_total}");
  165. Console.ResetColor();
  166. _total = 0;
  167. try
  168. {
  169. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  170. var pack = new DeviceDataPack
  171. {
  172. Frame = LogicHandler.Frame
  173. };
  174. foreach (var g in gs)
  175. {
  176. var value = g.Select(v => v.Value).ToArray();
  177. var etype = g.Key;
  178. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  179. var coll = Activator.CreateInstance(type, LogicHandler.Frame, value);
  180. var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
  181. p.SetValue(pack, coll);
  182. }
  183. #region 存入Redis
  184. var sw = new Stopwatch();
  185. sw.Start();
  186. RedisHelper.Monitor.Set(nameof(DeviceDataPack), pack);
  187. RedisHelper.Monitor.RPush("Packs", pack);
  188. sw.Stop();
  189. Console.ForegroundColor = ConsoleColor.Blue;
  190. Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
  191. Console.ResetColor();
  192. var len = RedisHelper.Monitor.LLen("Packs");
  193. if (len > 150000)
  194. {
  195. RedisHelper.Monitor.LTrim("Packs", 20000, len);
  196. }
  197. #endregion 存入Redis
  198. var converter = new System.Text.UnicodeEncoding();
  199. var plcRawData = new PlcRawData
  200. {
  201. CONTENT = converter.GetBytes(JsonConvert.SerializeObject(pack)),
  202. WAREHOUSE = "1"
  203. };
  204. #region 存入PGSql
  205. var sw1 = new Stopwatch();
  206. sw1.Start();
  207. Core.DbHelper.Db.Do(db =>
  208. {
  209. db.Context("WCSDlog").Insertable(plcRawData).ExecuteCommand();
  210. });
  211. sw1.Stop();
  212. Console.ForegroundColor = ConsoleColor.Blue;
  213. Console.WriteLine($"PGSql耗时{sw1.ElapsedMilliseconds}");
  214. Console.ResetColor();
  215. RedisHelper.Default.RPush("Packs", pack);
  216. #endregion 存入PGSql
  217. #region 存入sqlServe
  218. var sw2 = new Stopwatch();
  219. sw2.Start();
  220. Core.DbHelper.Db.Do(db =>
  221. {
  222. db.Default.Insertable(plcRawData).ExecuteCommand();
  223. });
  224. sw2.Stop();
  225. Console.ForegroundColor = ConsoleColor.Blue;
  226. Console.WriteLine($"sqlServe耗时{sw2.ElapsedMilliseconds}");
  227. Console.ResetColor();
  228. RedisHelper.Default.RPush("Packs", pack);
  229. #endregion 存入sqlServe
  230. foreach (var data in AllDatas)
  231. {
  232. data.Value.Info = "";
  233. if (data.Value is not ProdLineData pld) continue;
  234. pld.TaskList.Clear();
  235. pld.Frame = LogicHandler.Frame;
  236. pld.Code = data.Key;
  237. }
  238. }
  239. catch (Exception)
  240. {
  241. // ignored
  242. }
  243. }
  244. }
  245. public class PackInfo
  246. {
  247. public DateTime Frame { get; set; }
  248. public byte[] Data { get; set; }
  249. }
  250. }