WcsController.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. using Microsoft.AspNetCore.Mvc;
  2. using OfficeOpenXml;
  3. using ServiceCenter;
  4. using ServiceCenter.Extensions;
  5. using ServiceCenter.SqlSugars;
  6. using SqlSugar;
  7. using System.Net.NetworkInformation;
  8. using System.Text;
  9. using WCS.Core;
  10. using WCS.Entity.Protocol.HUB;
  11. using WCS.Entity.Protocol.SRM;
  12. using WCS.WorkEngineering.WebApi.Models.WCS.Request;
  13. using WCS.WorkEngineering.WebApi.Models.WCS.Response;
  14. //using Microsoft.OpenApi.Writers;
  15. // ReSharper disable PossibleLossOfFraction
  16. namespace WCS.WorkEngineering.WebApi.Controllers
  17. {
  18. /// <summary>
  19. /// 设备Ip通讯检测结构
  20. /// </summary>
  21. public class DeviceIpTestResults
  22. {
  23. public string Ip { get; set; }
  24. public bool Result { get; set; }
  25. }
  26. /// <summary>
  27. /// WCS相关接口控制器
  28. /// </summary>
  29. [ApiController]
  30. [Route("api/[controller]/[action]")]
  31. public class WcsController : ControllerBase
  32. {
  33. /// <summary>
  34. /// 获取设备配置信息接口
  35. /// </summary>
  36. /// <returns></returns>
  37. [HttpGet]
  38. public List<Device> GetDeviceList()
  39. {
  40. return Device.All.ToList();
  41. }
  42. /// <summary>
  43. /// 获取设备运行状态
  44. /// </summary>
  45. /// <param name="req"></param>
  46. /// <returns></returns>
  47. [HttpPost]
  48. public PagedInfoResponse<RunInfoViewMode> GetDevRunInfo(DevRunInfoViewMode req)
  49. {
  50. var runInfos = new List<RunInfoViewMode>();
  51. var db = new SqlSugarHelper().Default;
  52. var plcEx = new SqlSugarHelper().PLCEX;
  53. var type = "";
  54. List<List<DateTime>> dateList = new List<List<DateTime>>();
  55. switch (req.Scope)
  56. {
  57. case ScopeType.Day:
  58. dateList = GetDayRanges(req.StateTime, req.EndTime);
  59. break;
  60. case ScopeType.Hour:
  61. dateList = GetHourRanges(req.StateTime, req.EndTime);
  62. break;
  63. default:
  64. throw new Exception("不支持此时间范围统计,目前仅支持天与小时");
  65. }
  66. switch (req.Type)
  67. {
  68. case DevType.SRM: //堆垛机
  69. foreach (var date in dateList)
  70. {
  71. var minTime = date.MinBy(x => x);
  72. var maxTime = date.MaxBy(x => x);
  73. type = DevType.SRM.ToString();
  74. //获取时段内设备的运行状态
  75. var runInfoList = plcEx.Queryable<DevRunInfo>().Where(x => x.Type == type && x.Frame >= minTime && x.Frame < maxTime).ToList().GroupBy(x => x.Code);
  76. foreach (var runInfo in runInfoList)
  77. {
  78. var info = new RunInfoViewMode()
  79. {
  80. Code = runInfo.Key,
  81. Free = runInfo.Where(x =>
  82. x.RunMode == SrmMode.自动.GetDescription() &&
  83. x.RunStatus == SrmRunStatus.空闲.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  84. Alarm = runInfo.Where(x => x.RunMode == SrmMode.维修.GetDescription())
  85. .Sum(x => x.Duration) / 1000 / 60,
  86. Manual = runInfo.Where(x => x.RunMode == SrmMode.手动.GetDescription() || x.RunMode == SrmMode.半自动.GetDescription())
  87. .Sum(x => x.Duration) / 1000 / 60,
  88. Automatic = runInfo.Where(x => x.RunMode == SrmMode.自动.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  89. Working = runInfo.Where(x => x.RunMode == SrmMode.自动.GetDescription()
  90. && x.RunStatus != SrmRunStatus.空闲.GetDescription()
  91. && x.RunStatus != SrmRunStatus.保持维修.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  92. TotalTime = runInfo.Sum(x => x.Duration) / 1000 / 60
  93. };
  94. #region 开始分析设备动作,不计算停止时间
  95. var devActions = new List<DevActionViewMode>();
  96. double totalTime = 0; //总耗时
  97. double totalCount = 0; //总次数
  98. //入库
  99. var infos = runInfo.Where(x => x.RunMode == SrmMode.自动.GetDescription()).OrderBy(x => x.Frame).ToList();
  100. totalTime = infos.Where(x => x.RunStatus != SrmRunStatus.空闲.GetDescription() && x.RunStatus != SrmRunStatus.保持维修.GetDescription()).Sum(x => x.Duration);
  101. #endregion 开始分析设备动作,不计算停止时间
  102. #region 计算任务的工位使用数量
  103. var stationCounts = new List<StationCount>();
  104. type = DevType.SrmTaskSum.ToString();
  105. var taskSum = plcEx.Queryable<DevRunInfo>().Where(x => x.Code == runInfo.Key && x.Type == type && x.Frame >= minTime && x.Frame < maxTime)
  106. .ToList().Select(x =>
  107. {
  108. var item = x;
  109. var taskInfo = item.RunStatus.Split("--");
  110. if (taskInfo.Length < 3) return null;
  111. item.Duration = long.Parse(taskInfo[2]);
  112. return item;
  113. }).Where(x => x != null).ToList();
  114. //入库
  115. stationCounts.Add(new StationCount()
  116. {
  117. Type = DevActionType.In.GetDescription(),
  118. StationNum = taskSum.Count(x => x is { RunMode: "1" }),
  119. StationIndex = 1
  120. });
  121. stationCounts.Add(new StationCount()
  122. {
  123. Type = DevActionType.In.GetDescription(),
  124. StationNum = taskSum.Count(x => x is { RunMode: "2" }),
  125. StationIndex = 2
  126. });
  127. info.StationCount = stationCounts;
  128. totalCount = taskSum.Count;
  129. devActions.Add(new DevActionViewMode()
  130. {
  131. Type = DevActionType.In.GetDescription(),
  132. ActionAmount = totalCount,
  133. ActionTime = totalTime == 0 ? 0 : Math.Round((totalTime / totalCount) / 1000, 2),
  134. });
  135. info.DevAction = devActions;
  136. #endregion 计算任务的工位使用数量
  137. info.StartTime = minTime;
  138. info.EndTime = maxTime;
  139. runInfos.Add(info);
  140. }
  141. }
  142. break;
  143. case DevType.Truss:
  144. //type = DevType.Truss.ToString();
  145. //foreach (var date in dateList)
  146. //{
  147. // HandleTrussRunInfo(type, db, plcEx, runInfos, date.MinBy(x => x), date.MaxBy(x => x));
  148. //}
  149. break;
  150. }
  151. var res = new PagedInfoResponse<RunInfoViewMode>();
  152. res.Result = runInfos.OrderBy(x => x.StartTime).ThenBy(x => x.Code).ToList();
  153. res.TotalNum = runInfos.Count;
  154. // If you are a commercial business and have
  155. // purchased commercial licenses use the static property
  156. // LicenseContext of the ExcelPackage class:
  157. ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.Commercial;
  158. // If you use EPPlus in a noncommercial context
  159. // according to the Polyform Noncommercial license:
  160. ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
  161. // 创建一个新的 Excel 文件
  162. FileInfo excelFile = new FileInfo("D:\\output.xlsx");
  163. using (ExcelPackage package = new ExcelPackage(excelFile))
  164. {
  165. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
  166. // 写入表头
  167. int col = 1;
  168. var title = new List<string>() { "时间", "设备", "入库", "空闲", "手动", "自动", "报警", "工作时间", "总时间", "次数", "耗时", "稼动率", "单抓", "双爪" };
  169. foreach (var prop in title)
  170. {
  171. worksheet.Cells[1, col].Value = prop;
  172. col++;
  173. }
  174. // 写入数据
  175. int row = 2;
  176. var dateTimeList = res.Result.GroupBy(x => x.StartTime);
  177. foreach (var time in dateTimeList)
  178. {
  179. var codeLost = time.OrderBy(x => x.Code);
  180. foreach (var code in codeLost)
  181. {
  182. worksheet.Cells[row, 1].Value = code.StartTime;
  183. worksheet.Cells[row, 2].Value = code.Code;
  184. worksheet.Cells[row, 3].Value = code.StationCount?.Select(x => x.StationIndex * x.StationNum).Sum(x => x);
  185. worksheet.Cells[row, 4].Value = code.Free;
  186. worksheet.Cells[row, 5].Value = code.Manual;
  187. worksheet.Cells[row, 6].Value = code.Automatic;
  188. worksheet.Cells[row, 7].Value = code.Alarm;
  189. worksheet.Cells[row, 8].Value = code.Working;
  190. worksheet.Cells[row, 9].Value = code.TotalTime;
  191. worksheet.Cells[row, 10].Value = code.DevAction.FirstOrDefault(x => x.Type == "入库")?.ActionAmount;
  192. worksheet.Cells[row, 11].Value = code.DevAction.FirstOrDefault(x => x.Type == "入库")?.ActionTime;
  193. worksheet.Cells[row, 12].Value = code.Working / code.TotalTime;
  194. worksheet.Cells[row, 13].Value = code.StationCount.First(x => x is { Type: "入库", StationIndex: 1 }).StationNum;
  195. worksheet.Cells[row, 14].Value = code.StationCount.First(x => x is { Type: "入库", StationIndex: 2 }).StationNum;
  196. //worksheet.Cells[row, 16].Value = enterDepot2;
  197. //worksheet.Cells[row, 17].Value = outDepot1;
  198. //worksheet.Cells[row, 18].Value = outDepot2;
  199. row++;
  200. }
  201. //foreach (var prop in item.GetType().GetProperties())
  202. //{
  203. // worksheet.Cells[row, col].Value = prop.GetValue(item);
  204. // col++;
  205. //}
  206. }
  207. // 保存 Excel 文件
  208. package.Save();
  209. }
  210. return res;
  211. //var runInfos = new List<RunInfoViewMode>();
  212. //var db = new SqlSugarHelper().Default;
  213. //var plcEx = new SqlSugarHelper().PLCEX;
  214. //var type = "";
  215. //switch (req.Type)
  216. //{
  217. // case DevType.SRM:
  218. // type = DevType.SRM.ToString();
  219. // //获取时段内设备的运行状态
  220. // var runInfoList = plcEx.Queryable<DevRunInfo>().Where(x => x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime).ToList().GroupBy(x => x.Code);
  221. // foreach (var runInfo in runInfoList)
  222. // {
  223. // var info = new RunInfoViewMode()
  224. // {
  225. // Code = runInfo.Key,
  226. // //EnterDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
  227. // // .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.EnterDepot &&
  228. // // x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
  229. // // x.EndTime < req.EndTime)
  230. // // .SplitTable(x => x.Take(2))
  231. // // .Count(),
  232. // //OutDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
  233. // // .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.OutDepot &&
  234. // // x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
  235. // // x.EndTime < req.EndTime)
  236. // // .SplitTable(x => x.Take(2))
  237. // // .Count(),
  238. // //MoveDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
  239. // // .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.TransferDepot &&
  240. // // x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
  241. // // x.EndTime < req.EndTime)
  242. // // .SplitTable(x => x.Take(2))
  243. // // .Count(),
  244. // Free = runInfo.Where(x =>
  245. // x.RunMode == SrmMode.自动.GetDescription() &&
  246. // x.RunStatus == SrmRunStatus.空闲.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  247. // //Alarm = runInfo.Where(x => x.RunMode == SrmMode.维修.GetDescription())
  248. // // .Sum(x => x.Duration) / 1000 / 60,
  249. // Manual = runInfo.Where(x => x.RunMode == SrmMode.手动.GetDescription() || x.RunMode == SrmMode.半自动.GetDescription())
  250. // .Sum(x => x.Duration) / 1000 / 60,
  251. // Automatic = runInfo.Where(x => x.RunMode == SrmMode.自动.GetDescription()
  252. // && x.RunStatus != SrmRunStatus.空闲.GetDescription())
  253. // .Sum(x => x.Duration) / 1000 / 60,
  254. // };
  255. // #region 开始分析设备动作,不计算停止时间
  256. // var devActions = new List<DevActionViewMode>();
  257. // double totalTime = 0; //总耗时
  258. // double totalCount = 0; //总次数
  259. // //入库
  260. // var infos = runInfo.Where(x => x.RunMode == SrmMode.自动.GetDescription() && x.RunStatus != SrmRunStatus.空闲.GetDescription()).OrderBy(x => x.Frame).ToList();
  261. // totalTime = infos.Sum(x => x.Duration);
  262. // totalCount = infos.Count(x => x.RunStatus == SrmRunStatus.放货收叉.GetDescription());
  263. // devActions.Add(new DevActionViewMode()
  264. // {
  265. // //Type = DevActionType.In.GetDescription(),
  266. // ActionAmount = totalCount,
  267. // ActionTime = totalTime == 0 ? 0 : Math.Round(totalTime / totalCount, 2),
  268. // });
  269. // ////出库
  270. // //infos = runInfo.Where(x => x.RunMode == RobotMode.Automatic.GetDescription()).ToList();
  271. // //totalTime = infos.Where(x => x.RunStatus == RobotRunStatus.OutBoundCrawling.GetDescription() || x.RunStatus == RobotRunStatus.OutStorage.GetDescription()).Sum(x => x.Duration);
  272. // //totalCount = infos.Count(x => x.RunStatus == RobotRunStatus.OutBoundCrawling.GetDescription());
  273. // //devActions.Add(new DevActionViewMode()
  274. // //{
  275. // // Type = DevActionType.Out.GetDescription(),
  276. // // ActionAmount = totalCount,
  277. // // ActionTime = totalTime == 0 ? 0 : Math.Round(totalTime / totalCount, 2),
  278. // //});
  279. // info.DevAction = devActions;
  280. // #endregion 开始分析设备动作,不计算停止时间
  281. // //#region 计算任务的工位使用数量
  282. // //var stationCounts = new List<StationCount>();
  283. // //type = DevType.RobotTaskSum.ToString();
  284. // //var taskSum = plcEx.Queryable<DevRunInfo>().Where(x => x.Code == runInfo.Key && x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime)
  285. // // .ToList().Select(x =>
  286. // // {
  287. // // var item = x;
  288. // // item.Duration = long.Parse(item.RunStatus.Split("--")[2]);
  289. // // return item;
  290. // // }).ToList();
  291. // ////入库
  292. // //stationCounts.Add(new StationCount()
  293. // //{
  294. // // Type = DevActionType.In.GetDescription(),
  295. // // StationNum = taskSum.Count(x => x is { Duration: 3, RunMode: "1" }),
  296. // // StationIndex = 1
  297. // //});
  298. // //stationCounts.Add(new StationCount()
  299. // //{
  300. // // Type = DevActionType.In.GetDescription(),
  301. // // StationNum = taskSum.Count(x => x is { Duration: 3, RunMode: "2" }),
  302. // // StationIndex = 2
  303. // //});
  304. // ////出库
  305. // //stationCounts.Add(new StationCount()
  306. // //{
  307. // // Type = DevActionType.Out.GetDescription(),
  308. // // StationNum = taskSum.Count(x => x is { Duration: 4, RunMode: "1" }),
  309. // // StationIndex = 1
  310. // //});
  311. // //stationCounts.Add(new StationCount()
  312. // //{
  313. // // Type = DevActionType.Out.GetDescription(),
  314. // // StationNum = taskSum.Count(x => x is { Duration: 4, RunMode: "2" }),
  315. // // StationIndex = 2
  316. // //});
  317. // //info.StationCount = stationCounts;
  318. // //#endregion 计算任务的工位使用数量
  319. // runInfos.Add(info);
  320. // }
  321. // break;
  322. // case DevType.Robot: //机械臂
  323. // type = DevType.Robot.ToString();
  324. // //获取时段内设备的运行状态
  325. // var robotInfoList = plcEx.Queryable<DevRunInfo>().Where(x => x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime).ToList().GroupBy(x => x.Code);
  326. // foreach (var runInfo in robotInfoList)
  327. // {
  328. // var curPoints = new List<string>();
  329. // if (runInfo.Key == "Robot1")
  330. // {
  331. // curPoints = new List<string>() { "8090", "8092" };
  332. // }
  333. // else if (runInfo.Key == "Robot2")
  334. // {
  335. // curPoints = new List<string>() { "8096", "8098" };
  336. // }
  337. // var info = new RunInfoViewMode()
  338. // {
  339. // Code = runInfo.Key,
  340. // EnterDepot = db.Queryable<WCS_TaskDtl>().With(SqlWith.NoLock)
  341. // .Where(x => x.Desc == "码垛抓取完成" && x.AddTime >= req.StateTime && x.AddTime < req.EndTime && curPoints.Contains(x.CurPoint))
  342. // .SplitTable(x => x.Take(2))
  343. // .Count(),
  344. // //OutDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
  345. // // .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.OutDepot &&
  346. // // x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
  347. // // x.EndTime < req.EndTime)
  348. // // .SplitTable(x => x.Take(2))
  349. // // .Count(),
  350. // Free = runInfo.Where(x =>
  351. // x.RunMode == RobotMode.自动.GetDescription() &&
  352. // x.RunStatus == RobotRunStatus.空闲.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  353. // Alarm = runInfo.Where(x => x.RunMode == RobotMode.故障.GetDescription())
  354. // .Sum(x => x.Duration) / 1000 / 60,
  355. // Manual = runInfo.Where(x => x.RunMode == RobotMode.手动.GetDescription())
  356. // .Sum(x => x.Duration) / 1000 / 60,
  357. // Automatic = runInfo.Where(x => x.RunMode == RobotMode.自动.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  358. // Working = runInfo.Where(x => x.RunMode == RobotMode.自动.GetDescription() && (x.RunStatus == RobotRunStatus.抓取中.GetDescription() || x.RunStatus == RobotRunStatus.放置中.GetDescription()))
  359. // .Sum(x => x.Duration) / 1000 / 60,
  360. // TotalTime = runInfo.Sum(x => x.Duration) / 1000 / 60
  361. // };
  362. // #region 开始分析设备动作,不计算停止时间
  363. // var devActions = new List<DevActionViewMode>();
  364. // double totalTime = 0; //总耗时
  365. // double totalCount = 0; //总次数
  366. // //入库
  367. // var infos = runInfo.Where(x => x.RunMode == RobotMode.自动.GetDescription()).OrderBy(x => x.Frame).ToList();
  368. // totalTime = infos.Where(x => x.RunStatus == RobotRunStatus.抓取中.GetDescription() || x.RunStatus == RobotRunStatus.放置中.GetDescription()).Sum(x => x.Duration);
  369. // totalCount = infos.Count(x => x.RunStatus == RobotRunStatus.抓取中.GetDescription());
  370. // devActions.Add(new DevActionViewMode()
  371. // {
  372. // Type = DevActionType.In.GetDescription(),
  373. // ActionAmount = totalCount,
  374. // ActionTime = totalTime == 0 ? 0 : Math.Round(totalTime / totalCount, 2),
  375. // });
  376. // info.DevAction = devActions;
  377. // #endregion 开始分析设备动作,不计算停止时间
  378. // runInfos.Add(info);
  379. // }
  380. // break;
  381. // case DevType.Truss:
  382. // //type = DevType.Truss.ToString();
  383. // ////获取时段内设备的运行状态
  384. // //var trussInfoList = plcEx.Queryable<DevRunInfo>().Where(x => x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime).ToList().GroupBy(x => x.Code);
  385. // //foreach (var runInfo in trussInfoList)
  386. // //{
  387. // // var info = new RunInfo()
  388. // // {
  389. // // Code = runInfo.Key,
  390. // // EnterDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
  391. // // .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.SetPlate &&
  392. // // x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
  393. // // x.EndTime < req.EndTime)
  394. // // .SplitTable(x => x.Take(2))
  395. // // .Count(),
  396. // // Free = runInfo.Where(x => x.RunMode == TrussStatus.Idle.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  397. // // Alarm = runInfo.Where(x => x.RunMode == TrussStatus.Alarm.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  398. // // Manual = runInfo.Where(x => x.RunMode == TrussStatus.Manual.GetDescription() || x.RunMode == TrussStatus.KeepInRepair.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  399. // // Automatic = runInfo.Where(x => x.RunMode == TrussStatus.Execute.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
  400. // // };
  401. // // #region 开始分析设备动作,不计算停止时间
  402. // // var devActions = new List<DevActionViewMode>();
  403. // // double totalTime = 0; //总耗时
  404. // // double totalCount = 0; //总次数
  405. // // //入库
  406. // // var infos = runInfo.Where(x => x.RunMode == TrussStatus.Execute.GetDescription()).ToList();
  407. // // totalTime = infos.Where(x => x.RunMode == TrussStatus.Execute.GetDescription()).Sum(x => x.Duration);
  408. // // totalCount = infos.Count(x => x.RunMode == TrussStatus.Execute.GetDescription());
  409. // // devActions.Add(new DevActionViewMode()
  410. // // {
  411. // // Type = DevActionType.In.GetDescription(),
  412. // // ActionAmount = totalCount,
  413. // // ActionTime = totalTime == 0 ? 0 : Math.Round(totalTime / totalCount, 2),
  414. // // });
  415. // // info.DevAction = devActions;
  416. // // #endregion 开始分析设备动作,不计算停止时间
  417. // // #region 计算任务的工位使用数量
  418. // // var stationCounts = new List<StationCount>();
  419. // // type = DevType.TrussTaskSum.ToString();
  420. // // var taskSum = plcEx.Queryable<DevRunInfo>().Where(x => x.Code == runInfo.Key && x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime)
  421. // // .ToList();
  422. // // //入库
  423. // // stationCounts.Add(new StationCount()
  424. // // {
  425. // // Type = DevActionType.In.GetDescription(),
  426. // // StationNum = taskSum.Count(x => x is { RunMode: "1" }),
  427. // // StationIndex = 1
  428. // // });
  429. // // stationCounts.Add(new StationCount()
  430. // // {
  431. // // Type = DevActionType.In.GetDescription(),
  432. // // StationNum = taskSum.Count(x => x is { RunMode: "2" }),
  433. // // StationIndex = 2
  434. // // });
  435. // // stationCounts.Add(new StationCount()
  436. // // {
  437. // // Type = DevActionType.In.GetDescription(),
  438. // // StationNum = taskSum.Count(x => x is { RunMode: "3" }),
  439. // // StationIndex = 3
  440. // // });
  441. // // stationCounts.Add(new StationCount()
  442. // // {
  443. // // Type = DevActionType.In.GetDescription(),
  444. // // StationNum = taskSum.Count(x => x is { RunMode: "4" }),
  445. // // StationIndex = 4
  446. // // });
  447. // // stationCounts.Add(new StationCount()
  448. // // {
  449. // // Type = DevActionType.In.GetDescription(),
  450. // // StationNum = taskSum.Count(x => x is { RunMode: "5" }),
  451. // // StationIndex = 5
  452. // // });
  453. // // stationCounts.Add(new StationCount()
  454. // // {
  455. // // Type = DevActionType.In.GetDescription(),
  456. // // StationNum = taskSum.Count(x => x is { RunMode: "6" }),
  457. // // StationIndex = 6
  458. // // });
  459. // // stationCounts.Add(new StationCount()
  460. // // {
  461. // // Type = DevActionType.In.GetDescription(),
  462. // // StationNum = taskSum.Count(x => x is { RunMode: "7" }),
  463. // // StationIndex = 7
  464. // // });
  465. // // stationCounts.Add(new StationCount()
  466. // // {
  467. // // Type = DevActionType.In.GetDescription(),
  468. // // StationNum = taskSum.Count(x => x is { RunMode: "8" }),
  469. // // StationIndex = 8
  470. // // });
  471. // // stationCounts.Add(new StationCount()
  472. // // {
  473. // // Type = DevActionType.In.GetDescription(),
  474. // // StationNum = taskSum.Count(x => x is { RunMode: "9" }),
  475. // // StationIndex = 9
  476. // // });
  477. // // stationCounts.Add(new StationCount()
  478. // // {
  479. // // Type = DevActionType.In.GetDescription(),
  480. // // StationNum = taskSum.Count(x => x is { RunMode: "10" }),
  481. // // StationIndex = 10
  482. // // });
  483. // // info.StationCount = stationCounts;
  484. // // #endregion 计算任务的工位使用数量
  485. // // runInfos.Add(info);
  486. // //}
  487. // break;
  488. //}
  489. //var res = new PagedInfoResponse<RunInfoViewMode>();
  490. //res.Result = runInfos;
  491. //res.TotalNum = runInfos.Count;
  492. //return res;
  493. }
  494. #region 设备IP相关
  495. /// <summary>
  496. /// 检查Ip是否正常联通
  497. /// </summary>
  498. /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
  499. /// <returns></returns>
  500. [HttpGet]
  501. public static bool PingIpOrDomainName(string strIpOrDName)
  502. {
  503. try
  504. {
  505. var objPingSender = new Ping();
  506. var objPinOptions = new PingOptions
  507. {
  508. DontFragment = true
  509. };
  510. const string data = "";
  511. var buffer = Encoding.UTF8.GetBytes(data);
  512. const int intTimeout = 120;
  513. var objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
  514. var strInfo = objPinReply.Status.ToString();
  515. return strInfo == "Success";
  516. }
  517. catch (Exception)
  518. {
  519. return false;
  520. }
  521. }
  522. /// <summary>
  523. /// 获取设备IP检测结果
  524. /// </summary>
  525. /// <returns>设备IP检测结果</returns>
  526. [HttpGet]
  527. public List<DeviceIpTestResults> DeviceIpTest()
  528. {
  529. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  530. var deviceIpTestResults = new List<DeviceIpTestResults>();
  531. ServiceHub.DeviceIPList.ForEach(ip =>
  532. {
  533. deviceIpTestResults.Add(new DeviceIpTestResults
  534. {
  535. Ip = ip,
  536. Result = PingIpOrDomainName(ip)
  537. });
  538. });
  539. return deviceIpTestResults;
  540. }
  541. /// <summary>
  542. /// 获取设备Ip集合
  543. /// </summary>
  544. /// <returns>设备Ip集合</returns>
  545. [HttpGet]
  546. public List<string> GetDeviceIpList()
  547. {
  548. if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
  549. return ServiceHub.DeviceIPList;
  550. }
  551. #endregion 设备IP相关
  552. /// <summary>
  553. /// 获取小时范围
  554. /// </summary>
  555. /// <param name="startTime">开始时间</param>
  556. /// <param name="endTime">结束时间</param>
  557. /// <returns></returns>
  558. private List<List<DateTime>> GetHourRanges(DateTime startTime, DateTime endTime)
  559. {
  560. List<List<DateTime>> hourRanges = new List<List<DateTime>>(); // 存放每个小时的开始时间和结束时间对
  561. // 循环遍历每个小时
  562. DateTime currentHour = startTime;
  563. while (currentHour <= endTime)
  564. {
  565. DateTime hourStart = currentHour; // 当前小时的开始时间
  566. DateTime hourEnd = currentHour.AddHours(1).AddSeconds(-1); // 当前小时的结束时间
  567. hourRanges.Add(new[] { hourStart, hourEnd }.ToList()); // 将当前小时的开始时间和结束时间添加到列表中
  568. currentHour = currentHour.AddHours(1); // 移动到下一个小时
  569. }
  570. return hourRanges;
  571. }
  572. /// <summary>
  573. /// 获取小时范围
  574. /// </summary>
  575. /// <param name="startTime">开始时间</param>
  576. /// <param name="endTime">结束时间</param>
  577. /// <returns></returns>
  578. private List<List<DateTime>> GetDayRanges(DateTime startTime, DateTime endTime)
  579. {
  580. List<List<DateTime>> dayRanges = new List<List<DateTime>>(); // 存放每个小时的开始时间和结束时间对
  581. // 循环遍历每个小时
  582. DateTime currentDate = startTime;
  583. while (currentDate <= endTime)
  584. {
  585. DateTime dayStart = currentDate.Date; // 当天的开始时间(凌晨)
  586. DateTime dayEnd = currentDate.Date.AddDays(1).AddSeconds(-1); // 当天的结束时间(当天的23:59:59)
  587. dayRanges.Add(new[] { dayStart, dayEnd }.ToList()); // 将当天的开始时间和结束时间添加到列表中
  588. currentDate = currentDate.AddDays(1); // 移动到下一天
  589. }
  590. return dayRanges;
  591. }
  592. }
  593. /// <summary>
  594. /// 带分页的返回体
  595. /// </summary>
  596. /// <typeparam name="T"></typeparam>
  597. public class PagedInfoResponse<T>
  598. {
  599. /// <summary>
  600. /// 每页行数
  601. /// </summary>
  602. public int PageSize { get; set; } = 10;
  603. /// <summary>
  604. /// 当前页
  605. /// </summary>
  606. public int PageIndex { get; set; } = 1;
  607. /// <summary>
  608. /// 总记录数
  609. /// </summary>
  610. public int TotalNum { get; set; }
  611. /// <summary>
  612. /// 总页数
  613. /// </summary>
  614. public int TotalPage
  615. {
  616. get
  617. {
  618. if (TotalNum > 0)
  619. {
  620. return TotalNum % PageSize == 0 ? TotalNum / PageSize : TotalNum / PageSize + 1;
  621. }
  622. else
  623. {
  624. return 0;
  625. }
  626. }
  627. set { }
  628. }
  629. public List<T> Result { get; set; }
  630. public Dictionary<string, object> Extra { get; set; } = new Dictionary<string, object>();
  631. public PagedInfoResponse()
  632. {
  633. }
  634. }
  635. }