TaskController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Migrations;
  6. using System.Data.SqlClient;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web.Http;
  12. using XdCxRhDW.Dto;
  13. using XdCxRhDW.Api;
  14. using XdCxRhDW.Entity;
  15. using XdCxRhDW.Repostory;
  16. using XdCxRhDW.WebApi;
  17. using System.Threading;
  18. using System.Diagnostics;
  19. namespace XdCxRhDW.App.Controllers
  20. {
  21. /// <summary>
  22. /// 任务相关接口
  23. /// </summary>
  24. public class TaskController : BaseController
  25. {
  26. /// <summary>
  27. /// 获取客户端IP地址
  28. /// </summary>
  29. /// <returns></returns>
  30. [HttpGet]
  31. public async Task<AjaxResult<string>> GetClientIP()
  32. {
  33. try
  34. {
  35. var ip = RemoteIp;
  36. return Success(ip);
  37. }
  38. catch (Exception ex)
  39. {
  40. await XdCxRhDW.UI.Lib.LogHelper.Error("获取客户端IP出错", ex);
  41. return Error<string>("获取客户端IP出错");
  42. }
  43. }
  44. /// <summary>
  45. /// 推算参考时差
  46. /// </summary>
  47. /// <param name="dto">卫星参数</param>
  48. /// <returns></returns>
  49. [HttpPost]
  50. public async Task<AjaxResult<RefCafResultDto>> GetRefCalcAsync(SatDto dto)
  51. {
  52. try
  53. {
  54. var dtos = await RefCgRepository.GetLatestAsync(dto.SatCode, dto.SigTime);
  55. var cgOrderList = dtos.OrderBy(d => d.SigTime).ToList();
  56. var point1 = cgOrderList.Where(c => c.SigTime <= dto.SigTime).FirstOrDefault();
  57. var point2 = cgOrderList.Where(c => c.SigTime >= dto.SigTime).FirstOrDefault();
  58. var refdto = new RefCafResultDto();
  59. if (point1 == null || point2 == null)
  60. {
  61. refdto.dt = 0;
  62. refdto.df = 0;
  63. refdto.snr = 0;
  64. }
  65. else
  66. {
  67. double refDto = RefCgRepository.CalSigTimeDto(dto.SigTime, point1.SigTime, point2.SigTime, point1.YbDto, point2.YbDto);
  68. refdto.dt = refDto * 1e6;
  69. }
  70. return Success(refdto);
  71. }
  72. catch (Exception ex)
  73. {
  74. return Error<RefCafResultDto>(ex.Message);
  75. }
  76. }
  77. /// <summary>
  78. /// 停止任务
  79. /// </summary>
  80. /// <param name="dto">任务停止参数</param>
  81. /// <returns></returns>
  82. [HttpPost]
  83. public async Task<AjaxResult> StopTask(TaskStopHandleDto dto)
  84. {
  85. try
  86. {
  87. using (RHDWContext db = new RHDWContext())
  88. {
  89. var item = await db.TaskInfos.FirstOrDefaultAsync(p => p.ID == dto.ID);
  90. if (item == null)
  91. {
  92. return Error($"任务停止失败,找不到ID={dto.ID}的任务");
  93. }
  94. if (item.TaskState == EnumTaskState.Stopped)
  95. Thread.Sleep(2000);
  96. item.TaskState = EnumTaskState.Stopped;
  97. await db.SaveChangesAsync();
  98. }
  99. using (RHDWLogContext db = new RHDWLogContext())
  100. {
  101. LogRes res = new LogRes()
  102. {
  103. LogTime = DateTime.Now,
  104. Module = Process.GetCurrentProcess().ProcessName,
  105. LogType = EnumLogType.Info,
  106. Msg = $"任务停止完成,ID={dto.ID},停止原因={dto.StopReason}",
  107. };
  108. db.LogRes.Add(res);
  109. await db.SaveChangesAsync();
  110. }
  111. Messenger.Defalut.Pub("任务状态改变", dto.ID);
  112. return Success();
  113. }
  114. catch (Exception ex)
  115. {
  116. XdCxRhDW.Framework.LogHelper.Error($"任务停止异常.ID={dto.ID}", ex);
  117. return Error(ex.Message);
  118. }
  119. }
  120. }
  121. }