HJDeviceMonitorController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 WMS.BZServices.HJ;
  13. using WCS.Protocol.HJ.DataStructure;
  14. namespace WMS.BZWeb.Areas.DeviceMonitorManager.Controllers
  15. {
  16. [Area("DeviceMonitorManager")]
  17. public class HJDeviceMonitorController : MvcControllerBase
  18. {
  19. private readonly RedisClient _HJRedis;
  20. private readonly WareCellService _wareCellService;
  21. private readonly WCSTaskOldService _wCSTaskOldService;
  22. private readonly AgvTaskInfoService _agvtaskInfoService;
  23. private DateTime MinTime = DateTime.Parse("2023/01/01");
  24. private readonly Dictionary<string, List<string>> _srmStations = new Dictionary<string, List<string>>()
  25. {
  26. { "SRM1",new List<string>{ "1021", "1022" , "1011", "1012" } },
  27. { "SRM2",new List<string>{ "1023", "1024" , "1013", "1014" } },
  28. { "SRM3",new List<string>{ "1025", "1026" , "1015", "1016" } }
  29. };
  30. public HJDeviceMonitorController(IServiceProvider serviceProvider, WareCellService wareCellService,
  31. WCSTaskOldService wCSTaskOldService, AgvTaskInfoService agvtaskInfoService)
  32. {
  33. _wCSTaskOldService = wCSTaskOldService;
  34. _wareCellService = wareCellService;
  35. _agvtaskInfoService = agvtaskInfoService;
  36. DeviceDataPack dataPack = new DeviceDataPack();
  37. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  38. var redisDict = serviceProvider.GetRequiredService<Dictionary<string, RedisClient>>();
  39. if (redisDict.Any())
  40. {
  41. _HJRedis = redisDict["HJRedis"];
  42. _HJRedis.Serialize = obj => MessagePackSerializer.Serialize(obj);// JsonConvert.SerializeObject(obj);
  43. _HJRedis.DeserializeRaw = (bytes, type) => MessagePackSerializer.Deserialize(type, bytes);// JsonConvert.DeserializeObject(json, type);
  44. }
  45. }
  46. #region 视图功能
  47. // GET: DeviceMonitorManager/HJDeviceMonitor
  48. public ActionResult Index()
  49. {
  50. //var sssd = _HJRedis.Get<DeviceDataPack>("DeviceDataPack");
  51. //_PTRedis.Set<DeviceDataPack>("DeviceDataPack", sssd);
  52. //var ppsssd = _PTRedis.Get<DeviceDataPack>("DeviceDataPack");
  53. var StartFrame = _HJRedis.LIndex<DeviceDataPack>("Packs", 0).Frame.ToLocalTime();
  54. var Len = _HJRedis.LLen("Packs");
  55. var LastFrame = _HJRedis.LIndex<DeviceDataPack>("Packs", Len - 1).Frame.ToLocalTime();
  56. ViewBag.Max = LastFrame.Ticks;
  57. ViewBag.Min = StartFrame.Ticks;
  58. ViewBag.Len = Len;
  59. ViewBag.Frame = LastFrame;
  60. ////long timestamp = DateTime.Now.ToLocalTime().Ticks; // 获取当前时间戳
  61. //////long ticks = timestamp / 10000 - 62135596800000; // 将时间戳转换为Ticks
  62. ////DateTime dateTime = new DateTime(timestamp); // 将Ticks转换为DateTime格式
  63. ////ViewBag.Max = timestamp;
  64. ////long pretimestamp = DateTime.Now.AddDays(-2).ToLocalTime().Ticks; // 获取当前时间戳
  65. ////ViewBag.Min = pretimestamp;
  66. //var Len = _HJRedis.LLen("Packs");
  67. //var sss = _HJRedis.LIndex<DeviceDataPack>("Packs", Len - 1);
  68. ////var sssd = _HJRedis.Get<DeviceDataPack>("DeviceDataPack");
  69. //var json = JsonConvert.SerializeObject(sssd);
  70. //for (int i = (int)Len; i > 0; i--)
  71. //{
  72. // var sdsdsd = _HJRedis.LIndex<DeviceDataPack>("Packs", i - 1);
  73. // _PTRedis.RPush("Packs", sdsdsd);
  74. //}
  75. ////var client = new RedisClient("118.178.107.197:6379,database=0,password=123456");
  76. ////client.Set("test", "tewtet");
  77. //_HJRedis.Set("testhjhjhj","testetsehjhjhjhjhjhjhjhj");
  78. ////// 设置hash值
  79. ////_HJRedis.HSet("myhash", "field1", "value1");
  80. ////// 获取hash值
  81. ////var value = _HJRedis.HGet("myhash", "field1");
  82. ////// 获取所有hash值
  83. ////var values = _HJRedis.HGetAll<string>("myhash");
  84. ////Dictionary<string, List<UserInfo>> result = new Dictionary<string, List<UserInfo>>();
  85. ////List<UserInfo> list = new List<UserInfo>();
  86. ////for (int i = 0; i < 50000; i++)
  87. ////{
  88. //// list.Add(new UserInfo { Email = "aa.com", QQ = i.ToString() });
  89. ////}
  90. ////result.Add("dictest", list);
  91. ////Stopwatch sw = Stopwatch.StartNew();
  92. ////_HJRedis.HSet<List<UserInfo>>("TestHash", result);
  93. ////sw.Stop();
  94. ////var sss = sw.ElapsedMilliseconds;
  95. ////sw.Start();
  96. ////var desgt = _HJRedis.HGet<List<UserInfo>>("TestHash", "dictest");
  97. ////sw.Stop();
  98. ////var swsw = sw.ElapsedMilliseconds;
  99. return View();
  100. }
  101. /// <summary>
  102. /// 表单页
  103. /// <summary>
  104. /// <returns></returns>
  105. [HttpGet]
  106. public ActionResult DDJDetail()
  107. {
  108. return View();
  109. }
  110. /// <summary>
  111. ///
  112. /// <summary>
  113. /// <returns></returns>
  114. [HttpGet]
  115. public ActionResult StationDetail()
  116. {
  117. return View();
  118. }
  119. #endregion
  120. public ActionResult GetDatetime(string timestamp)
  121. { // 638237068780288016
  122. // 565105737982546638
  123. var dateTime = new DateTime(Convert.ToInt64(timestamp));
  124. long ticks = Convert.ToInt64(timestamp) * 10000 + 62135596800000; // 将时间戳转换为Ticks
  125. //DateTime dateTime = new DateTime(ticks); // 将Ticks转换为DateTime格式
  126. return Success(dateTime);
  127. }
  128. public ActionResult GetLastDatas()
  129. {
  130. var len = _HJRedis.LLen("Packs");
  131. var device = _HJRedis.Get<DeviceDataPack>("DeviceDataPack");
  132. var wCSDevicePacks = ConvertData(device);
  133. wCSDevicePacks.Frame = device.Frame.ToLocalTime();
  134. wCSDevicePacks.Index = len.ToString();
  135. return Success(wCSDevicePacks);
  136. }
  137. /// <summary>
  138. ///
  139. /// </summary>
  140. /// <param name="timestamp"></param>
  141. /// <param name="len"></param>
  142. /// <returns></returns>
  143. public ActionResult GetStartDatas(string timestamp, int len)
  144. {
  145. if (timestamp == "undefined")
  146. {
  147. return Fail("");
  148. }
  149. var frame = new DateTime(Convert.ToInt64(timestamp));
  150. var Len = Convert.ToInt64(len);
  151. var temp = Len;
  152. var i = Len - 1;
  153. var isFind = true;
  154. WCSDevicePacks wCSDevicePacks = new WCSDevicePacks();
  155. try
  156. {
  157. while (isFind)
  158. {
  159. temp /= 2;
  160. if (temp == 0)
  161. temp = 1;
  162. var ps = _HJRedis.LRange<DeviceDataPack>("Packs", i - 1, i);
  163. if (ps.Any(v => v.Frame.ToLocalTime() <= frame) && ps.Any(v => v.Frame.ToLocalTime() >= frame))
  164. {
  165. isFind = false;
  166. var DataPack = ps.Last();
  167. if (DataPack != null)
  168. {
  169. wCSDevicePacks = ConvertData(DataPack);
  170. wCSDevicePacks.Frame = DataPack.Frame.ToLocalTime();
  171. wCSDevicePacks.Index = i.ToString();
  172. }
  173. }
  174. else if (ps.All(v => v.Frame.ToLocalTime() < frame))
  175. {
  176. i += temp;
  177. }
  178. else
  179. {
  180. i -= temp;
  181. }
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. return Fail("出错了" + ex.Message);
  187. }
  188. return Success(wCSDevicePacks);
  189. }
  190. /// <summary>
  191. ///
  192. /// </summary>
  193. /// <returns></returns>
  194. public ActionResult GetLastFrame()
  195. {
  196. var len = _HJRedis.LLen("Packs");
  197. var dateTime = _HJRedis.LIndex<DeviceDataPack>("Packs", len - 1).Frame.ToLocalTime();
  198. return Success(new { FrameTicks = dateTime.Ticks, Frame = dateTime, Len = len, LastFrameTicks = dateTime.Ticks, LastFrame = dateTime });
  199. }
  200. public ActionResult GetRangeData(int start, int end)
  201. {
  202. var arr = _HJRedis.LRange<DeviceDataPack>("Packs", start, end);
  203. List<WCSDevicePacks> list = new List<WCSDevicePacks>();
  204. foreach (var obj in arr)
  205. {
  206. obj.Frame = obj.Frame.ToLocalTime();
  207. var wCSDevicePacks = ConvertData(obj);
  208. list.Add(wCSDevicePacks);
  209. }
  210. return Success(list);
  211. }
  212. public ActionResult GetWarehouseLocationInfo()
  213. {
  214. var wareCellcount = _wareCellService.GetEmptyWareCell();
  215. var taskUnFinishCount=_wCSTaskOldService.GetTaskUnFinishCount();
  216. var agvTaskUnFinishCount= _agvtaskInfoService.GetTaskUnFinishCount();
  217. WarehouseLocationInfo warehouseLocationInfo = new WarehouseLocationInfo()
  218. {
  219. EmptySpaces = wareCellcount,TotalAGVs = agvTaskUnFinishCount,TotalTasks = taskUnFinishCount,
  220. };
  221. return Success(warehouseLocationInfo);
  222. }
  223. /// <summary>
  224. /// 获取首页设备数据
  225. /// </summary>
  226. /// <returns></returns>
  227. public ActionResult GetData()
  228. {
  229. WCSDevicePacks wCSDevicePacks = new WCSDevicePacks()
  230. {
  231. SRMStations = new List<Info.Models.WCSDeviceMonitor.SRMStation>() {
  232. new SRMStation(){Code="SRM1", IsAuto=1,TaskNum="5464"},
  233. },
  234. WCSStations = new List<WCSStation> { new WCSStation { Code = "1015", FaultInfo = "err", IsAuto = 1, PH_STATUS = true, TaskNum = "3423" } }
  235. };
  236. return Success(wCSDevicePacks);
  237. }
  238. /// <summary>
  239. /// 获取堆垛机详情数据
  240. /// </summary>
  241. /// <param name="keyValue"></param>
  242. /// <returns></returns>
  243. public ActionResult GetSRMData(string keyValue)
  244. {
  245. SRMStationDetail sRMStationDetail = new SRMStationDetail();
  246. sRMStationDetail.WCSSRM = new WCSSRM();
  247. if (_srmStations.TryGetValue(keyValue.ToUpper(), out var stations))
  248. {
  249. if (stations != null && stations.Count > 0)
  250. {
  251. sRMStationDetail.SRMStations = new List<SRMStation>() { };
  252. }
  253. }
  254. return Success(sRMStationDetail);
  255. }
  256. /// <summary>
  257. /// 获取设备信息
  258. /// </summary>
  259. /// <param name="keyValue">设备编号</param>
  260. /// <returns></returns>
  261. public ActionResult GetDeviceData(string keyValue)
  262. {
  263. if (string.IsNullOrEmpty(keyValue))
  264. {
  265. return Fail("设备编号不能为空!");
  266. }
  267. var deviceDataPack = _HJRedis.Get<DeviceDataPack>("DeviceDataPack");
  268. //DeviceDataPack dp = new DeviceDataPack();
  269. if (deviceDataPack != null && deviceDataPack.StationDatas != null && deviceDataPack.StationDatas.Datas != null
  270. && deviceDataPack.StationDatas.Datas.Count() > 0)
  271. {
  272. var data = deviceDataPack.StationDatas.Datas.FirstOrDefault<StationData>(o => o.Code == keyValue);
  273. if (data != null)
  274. {
  275. var result = data.GetAttributesDBDetail();
  276. return Success(result);
  277. }
  278. }
  279. if (deviceDataPack != null && deviceDataPack.SRMDatas != null && deviceDataPack.SRMDatas.Datas != null
  280. && deviceDataPack.SRMDatas.Datas.Count() > 0)
  281. {
  282. var data = deviceDataPack.SRMDatas.Datas.FirstOrDefault<SRMData>(o => o.Code == keyValue?.ToUpper());
  283. if (data != null)
  284. {
  285. var result = data.GetAttributesDBDetail();
  286. return Success(result);
  287. }
  288. }
  289. return Fail("没有数据!"); ;
  290. }
  291. private WCSDevicePacks ConvertData(DeviceDataPack deviceDataPack)
  292. {
  293. WCSDevicePacks wCSDevicePacks = new WCSDevicePacks()
  294. {
  295. WCSStations = new List<WCSStation>(),
  296. SRMStations = new List<SRMStation>(),
  297. FaultInfos=new List<FaultInfo>(),
  298. WCSSRMs = new List<WCSSRM>(),
  299. };
  300. if (deviceDataPack == null)
  301. {
  302. return wCSDevicePacks;
  303. }
  304. wCSDevicePacks.Frame = deviceDataPack.Frame.ToLocalTime();
  305. wCSDevicePacks.WCSStations.AddRange(from item in deviceDataPack.StationDatas?.Datas
  306. select new WCSStation
  307. {
  308. Code = item.Code,
  309. FaultInfo = (int)item.D523.Alarm > 0 ? item.D523.Alarm.GetDescription() : "",
  310. IsAuto = (int)item.D520.Mode,
  311. PH_STATUS = item.D523.Status.HasFlag(WCS.Protocol.HJ.Station.StatusEunm.PH_Status),
  312. TaskNum = item.D520.TaskNumber.ToString(),
  313. GoodsStart = item.D520.GoodsStart,
  314. GoodsEnd = item.D520.GoodsEnd,
  315. Request = item.D521.Request == 0 ? false : true,
  316. Status = item.D523.Status.GetDescription(),
  317. BarCode = item.D80.Content,
  318. });
  319. wCSDevicePacks.WCSSRMs.AddRange(from item in deviceDataPack.SRMDatas?.Datas
  320. select new WCSSRM
  321. {
  322. Code = item.Code,
  323. IsAuto = (int)item.D521.AutoStatus,
  324. SCAlarm = item.D537.Alarm.GetDescription(),
  325. TaskNum = item.D520.TaskNumber.ToString(),
  326. SRMStatus = item.D521.RunStatus.GetDescription(),
  327. LoadingStatus = item.D521.Status.GetDescription(),
  328. SHeight = 0,
  329. CarryingWeight = 0,
  330. TotalKM = item.D521.TotalKm,
  331. SLine = item.D520.SLine,
  332. SCol = item.D520.SCol,
  333. SLayer = item.D520.SLayer,
  334. SDepth = (int)item.D520.SDepth,
  335. ELine = item.D520.ELine,
  336. ECol = item.D520.ECol,
  337. EDepth = (int)item.D520.EDepth,
  338. });
  339. wCSDevicePacks.FaultInfos.AddRange(from item in deviceDataPack.StationDatas?.Datas
  340. where item?.D523 != null && (int)item.D523.Alarm > 0
  341. select new FaultInfo
  342. {
  343. Areas = "合金库",
  344. DeviceCode = item.Code,
  345. FaultInfos = (int)item.D523.Alarm > 0 ? item.D523.Alarm.GetDescription() : "",
  346. });
  347. wCSDevicePacks.FaultInfos.AddRange(from item in deviceDataPack.SRMDatas?.Datas
  348. where item?.D537 != null && (int)item.D537.Alarm > 0
  349. select new FaultInfo
  350. {
  351. Areas = "合金库",
  352. DeviceCode = item.Code,
  353. FaultInfos = (int)item.D537.Alarm > 0 ? item.D537.Alarm.GetDescription() : "",
  354. });
  355. return wCSDevicePacks;
  356. }
  357. }
  358. }