PTDeviceMonitorController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using Microsoft.AspNetCore.Mvc;
  2. using SqlSugar;
  3. using FreeRedis;
  4. using WMS.Util;
  5. using WMS.Info.Models.WCSDeviceMonitor.WireRods;
  6. using WMS.Info.Models.WCSDeviceMonitor;
  7. using MessagePack;
  8. using MessagePack.Resolvers;
  9. using WMS.BZWeb.Extensions;
  10. using WMS.BZModels;
  11. using ServiceStack;
  12. using WCS.Entity.Protocol.DataStructure;
  13. using WCS.Entity.Protocol.Station;
  14. using WCS.Entity.Protocol.Protocol.DataStructure;
  15. using WMS.BZServices.PT;
  16. namespace WMS.BZWeb.Areas.DeviceMonitorManager.Controllers
  17. {
  18. [Area("DeviceMonitorManager")]
  19. public class PTDeviceMonitorController : MvcControllerBase
  20. {
  21. private readonly RedisClient _PTRedis;
  22. private readonly WareCellService _wareCellService;
  23. private readonly WCSTaskOldService _wCSTaskOldService;
  24. private readonly AgvTaskInfoService _agvtaskInfoService;
  25. private readonly RedisClient _FJRedis;
  26. private readonly Dictionary<string, List<string>> _srmStations = new Dictionary<string, List<string>>()
  27. {
  28. { "SRM1",new List<string>{ "1001", "1002" , "1003", "1004" } },
  29. { "SRM2",new List<string>{ "1005", "1006" , "1007", "1008" } },
  30. { "SRM3",new List<string>{ "1009", "1010" , "1011", "1012" } },
  31. { "SRM4",new List<string>{ "1013", "1014" , "1015", "1016" } }
  32. };
  33. public PTDeviceMonitorController(IServiceProvider serviceProvider, WareCellService wareCellService,
  34. WCSTaskOldService wCSTaskOldService, AgvTaskInfoService agvtaskInfoService)
  35. {
  36. var redisDict = serviceProvider.GetRequiredService<Dictionary<string, RedisClient>>();
  37. if (redisDict.Any())
  38. {
  39. _PTRedis = redisDict["PTRedis"];
  40. _PTRedis.Serialize = obj => MessagePackSerializer.Serialize(obj);// JsonConvert.SerializeObject(obj);
  41. _PTRedis.DeserializeRaw = (bytes, type) => MessagePackSerializer.Deserialize(type, bytes);// JsonConvert.DeserializeObject(json, type);
  42. }
  43. _wCSTaskOldService = wCSTaskOldService;
  44. _wareCellService = wareCellService;
  45. _agvtaskInfoService = agvtaskInfoService;
  46. _FJRedis = redisDict["FJRedis"];
  47. _FJRedis.Serialize = obj => MessagePackSerializer.Serialize(obj);// JsonConvert.SerializeObject(obj);
  48. _FJRedis.DeserializeRaw = (bytes, type) => MessagePackSerializer.Deserialize(type, bytes);// JsonConvert.DeserializeObject(json, type);
  49. }
  50. #region 视图功能
  51. // GET: DeviceMonitorManager/PTDeviceMonitor
  52. public ActionResult Index()
  53. {
  54. var StartFrame = _PTRedis.LIndex<DeviceDataPack>("Packs", 0).Frame.ToLocalTime();
  55. var Len = _PTRedis.LLen("Packs");
  56. var LastFrame = _PTRedis.LIndex<DeviceDataPack>("Packs", Len - 1).Frame.ToLocalTime();
  57. ViewBag.Max = LastFrame.Ticks;
  58. ViewBag.Min = StartFrame.Ticks;
  59. ViewBag.Len = Len;
  60. ViewBag.Frame = LastFrame;
  61. return View();
  62. }
  63. /// <summary>
  64. /// 表单页
  65. /// <summary>
  66. /// <returns></returns>
  67. [HttpGet]
  68. public ActionResult DDJDetail()
  69. {
  70. return View();
  71. }
  72. [HttpGet]
  73. public ActionResult InteractionPointIndex()
  74. {
  75. var keylists = _FJRedis.Keys("*WCSLog**");
  76. // keylist.Split(':').Last()
  77. foreach (var keylist in keylists)
  78. {
  79. //keylist.Replace(_FJRedis.Prefix);
  80. var value = _FJRedis.Get(keylist);
  81. }
  82. return View();
  83. }
  84. #endregion
  85. public ActionResult GetLastDatas()
  86. {
  87. var len = _PTRedis.LLen("Packs");
  88. var device = _PTRedis.Get<DeviceDataPack>("DeviceDataPack");
  89. var wCSDevicePacks = ConvertData(device);
  90. wCSDevicePacks.Frame = device.Frame.ToLocalTime();
  91. wCSDevicePacks.Index = len.ToString();
  92. return Success(wCSDevicePacks);
  93. }
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. /// <param name="timestamp"></param>
  98. /// <param name="len"></param>
  99. /// <returns></returns>
  100. public ActionResult GetStartDatas(string timestamp, int len)
  101. {
  102. if (timestamp == "undefined")
  103. {
  104. return Fail("");
  105. }
  106. var frame = new DateTime(Convert.ToInt64(timestamp));
  107. var Len = Convert.ToInt64(len);
  108. var temp = Len;
  109. var i = Len - 1;
  110. var isFind = true;
  111. WCSDevicePacks wCSDevicePacks = new WCSDevicePacks();
  112. try
  113. {
  114. while (isFind)
  115. {
  116. temp /= 2;
  117. if (temp == 0)
  118. temp = 1;
  119. var ps = _PTRedis.LRange<DeviceDataPack>("Packs", i - 1, i);
  120. if (ps.Any(v => v.Frame.ToLocalTime() <= frame) && ps.Any(v => v.Frame.ToLocalTime() >= frame))
  121. {
  122. isFind = false;
  123. var DataPack = ps.Last();
  124. if (DataPack != null)
  125. {
  126. wCSDevicePacks = ConvertData(DataPack);
  127. wCSDevicePacks.Frame = DataPack.Frame.ToLocalTime();
  128. wCSDevicePacks.Index = i.ToString();
  129. }
  130. }
  131. else if (ps.All(v => v.Frame.ToLocalTime() < frame))
  132. {
  133. i += temp;
  134. }
  135. else
  136. {
  137. i -= temp;
  138. }
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. return Fail("出错了" + ex.Message);
  144. }
  145. return Success(wCSDevicePacks);
  146. }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. /// <returns></returns>
  151. public ActionResult GetLastFrame()
  152. {
  153. var len = _PTRedis.LLen("Packs");
  154. var dateTime = _PTRedis.LIndex<DeviceDataPack>("Packs", len - 1).Frame.ToLocalTime();
  155. return Success(new { FrameTicks = dateTime.Ticks, Frame = dateTime, Len = len, LastFrameTicks = dateTime.Ticks, LastFrame = dateTime });
  156. }
  157. public ActionResult GetRangeData(int start, int end)
  158. {
  159. var arr = _PTRedis.LRange<DeviceDataPack>("Packs", start, end);
  160. List<WCSDevicePacks> list = new List<WCSDevicePacks>();
  161. foreach (var obj in arr)
  162. {
  163. obj.Frame = obj.Frame.ToLocalTime();
  164. var wCSDevicePacks = ConvertData(obj);
  165. list.Add(wCSDevicePacks);
  166. }
  167. return Success(list);
  168. }
  169. public ActionResult GetWarehouseLocationInfo()
  170. {
  171. var wareCellcount = _wareCellService.GetEmptyWareCell();
  172. var taskUnFinishCount = _wCSTaskOldService.GetTaskUnFinishCount();
  173. var agvTaskUnFinishCount = _agvtaskInfoService.GetTaskUnFinishCount();
  174. WarehouseLocationInfo warehouseLocationInfo = new WarehouseLocationInfo()
  175. {
  176. EmptySpaces = wareCellcount,
  177. TotalAGVs = agvTaskUnFinishCount,
  178. TotalTasks = taskUnFinishCount,
  179. };
  180. return Success(warehouseLocationInfo);
  181. }
  182. public ActionResult GetSRMData(string keyValue)
  183. {
  184. SRMStationDetail sRMStationDetail = new SRMStationDetail();
  185. sRMStationDetail.WCSSRM = new WCSSRM();
  186. if (_srmStations.TryGetValue(keyValue.ToUpper(), out var stations))
  187. {
  188. if (stations != null && stations.Count > 0)
  189. {
  190. sRMStationDetail.SRMStations = new List<SRMStation>() { };
  191. }
  192. }
  193. return Success(sRMStationDetail);
  194. }
  195. /// <summary>
  196. /// 获取设备信息
  197. /// </summary>
  198. /// <param name="keyValue">设备编号</param>
  199. /// <returns></returns>
  200. public ActionResult GetDeviceData(string keyValue)
  201. {
  202. if (string.IsNullOrEmpty(keyValue))
  203. {
  204. return Fail("设备编号不能为空!");
  205. }
  206. var deviceDataPack = _PTRedis.Get<DeviceDataPack>("DeviceDataPack");
  207. //DeviceDataPack dp = new DeviceDataPack();
  208. if (deviceDataPack != null && deviceDataPack.StationDatas != null && deviceDataPack.StationDatas.Datas != null
  209. && deviceDataPack.StationDatas.Datas.Count() > 0)
  210. {
  211. var data = deviceDataPack.StationDatas.Datas.FirstOrDefault<StationData>(o => o.Code == keyValue);
  212. if (data != null)
  213. {
  214. var result = data.GetAttributesDBDetail();
  215. return Success(result);
  216. }
  217. }
  218. if (deviceDataPack != null && deviceDataPack.SRMDatas != null && deviceDataPack.SRMDatas.Datas != null
  219. && deviceDataPack.SRMDatas.Datas.Count() > 0)
  220. {
  221. var data = deviceDataPack.SRMDatas.Datas.FirstOrDefault<SRMData>(o => o.Code == keyValue?.ToUpper());
  222. if (data != null)
  223. {
  224. var result = data.GetAttributesDBDetail();
  225. return Success(result);
  226. }
  227. }
  228. return Fail("没有数据!"); ;
  229. }
  230. private WCSDevicePacks ConvertData(DeviceDataPack deviceDataPack)
  231. {
  232. WCSDevicePacks wCSDevicePacks = new WCSDevicePacks()
  233. {
  234. WCSStations = new List<WCSStation>(),
  235. SRMStations = new List<SRMStation>(),
  236. FaultInfos = new List<FaultInfo>(),
  237. WCSSRMs = new List<WCSSRM>(),
  238. };
  239. if (deviceDataPack == null)
  240. {
  241. return wCSDevicePacks;
  242. }
  243. wCSDevicePacks.Frame = deviceDataPack.Frame.ToLocalTime();
  244. if (deviceDataPack.StationDatas != null)
  245. {
  246. wCSDevicePacks.WCSStations.AddRange(from item in deviceDataPack.StationDatas?.Datas
  247. select new WCSStation
  248. {
  249. Code = item.Code,
  250. FaultInfo = item.D523 != null && (int)item.D523.Alarm > 0 ? item.D523.Alarm.GetDescription() : "",
  251. IsAuto = (int)item.D520.Mode,
  252. PH_STATUS = item.D523.Status.HasFlag(StationStatus.PH_Status),
  253. TaskNum = item.D520.TaskNumber.ToString(),
  254. GoodsStart = item.D520.GoodsStart,
  255. GoodsEnd = item.D520.GoodsEnd,
  256. Request = item.D521.Request == 0 ? false : true,
  257. Status = item.D523.Status.GetDescription(),
  258. BarCode = item.D80?.Content,
  259. });
  260. }
  261. wCSDevicePacks.WCSSRMs.AddRange(from item in deviceDataPack.SRMDatas?.Datas
  262. select new WCSSRM
  263. {
  264. Code = item.Code,
  265. IsAuto = (int)item.D521.AutoStatus,
  266. SCAlarm = item.D537.Alarm.GetDescription(),
  267. TaskNum = item.D520.TaskNumber.ToString(),
  268. SRMStatus = item.D521.RunStatus.GetDescription(),
  269. LoadingStatus = item.D521.Status.GetDescription(),
  270. SHeight = 0,
  271. CarryingWeight = 0,
  272. TotalKM = item.D521.TotalKm,
  273. SLine = item.D520.SLine,
  274. SCol = item.D520.SCol,
  275. SLayer = item.D520.SLayer,
  276. SDepth = (int)item.D520.SDepth,
  277. ELine = item.D520.ELine,
  278. ECol = item.D520.ECol,
  279. EDepth = (int)item.D520.EDepth,
  280. });
  281. if (deviceDataPack.StationDatas != null)
  282. {
  283. wCSDevicePacks.FaultInfos.AddRange(from item in deviceDataPack.StationDatas?.Datas
  284. where item?.D523 != null && (int)item.D523.Alarm > 0
  285. select new FaultInfo
  286. {
  287. Areas = "盘条库",
  288. DeviceCode = item.Code,
  289. FaultInfos = (int)item.D523.Alarm > 0 ? item.D523.Alarm.GetDescription() : "",
  290. });
  291. }
  292. wCSDevicePacks.FaultInfos.AddRange(from item in deviceDataPack.SRMDatas?.Datas
  293. where item?.D537 != null && (int)item.D537.Alarm > 0
  294. select new FaultInfo
  295. {
  296. Areas = "盘条库",
  297. DeviceCode = item.Code,
  298. FaultInfos = (int)item.D537.Alarm > 0 ? item.D537.Alarm.GetDescription() : "",
  299. });
  300. return wCSDevicePacks;
  301. }
  302. }
  303. }