HouseBinController.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using IvfTl.Control.Entity.DBEntitys;
  2. using IvfTl.Control.Entity.DTO;
  3. using IvfTl.Control.Entity.DTO.ApiRequestDTO;
  4. using IvfTl.Control.Entity.DTO.ApiResultDTO;
  5. using IvfTl.Control.Entity.DTO.ControllerResults;
  6. using IvfTl.Control.Entity.GlobalEntitys;
  7. using IvfTl.Control.Entity.GlobalEnums;
  8. using IvfTl.Control.Services;
  9. using IvfTl.Control.Services.HttpServices;
  10. using ivf_tl_UtilHelper;
  11. using Newtonsoft.Json;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Xml.Linq;
  18. namespace ivf_tl_Controller
  19. {
  20. public class HouseBinController
  21. {
  22. /// <summary>
  23. /// 日志事件
  24. /// </summary>
  25. public event Action<string, LogEnum> LogEvent;
  26. /// <summary>
  27. /// 异常日志事件
  28. /// </summary>
  29. public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
  30. private HttpService _httpService;
  31. private DBService _dBService;
  32. public HouseBinController(HttpService httpService, DBService dBService)
  33. {
  34. _httpService = httpService;
  35. _dBService= dBService;
  36. }
  37. private void ExLog(Exception ex, string name)
  38. {
  39. ExceptionLogEvent?.Invoke(ex, $"HouseBinController.{name}", null, LogEnum.RunException);
  40. }
  41. private void ErrorLog(string message, LogEnum logType)
  42. {
  43. LogEvent?.Invoke($"HouseBinController.{message}", logType);
  44. }
  45. /// <summary>
  46. /// 获取自动对焦数据
  47. /// </summary>
  48. /// <param name="body"></param>
  49. public List<HouseWellPhoto> GetAutoFocusController(PositionRequestDTO autoFocusDTO)
  50. {
  51. try
  52. {
  53. List<HouseWellPhoto> result = null;
  54. string body = JsonConvert.SerializeObject(autoFocusDTO);
  55. for (int i = 0; i < 3; i++)
  56. {
  57. result = ServiceAutoFocusData(body);
  58. if (result != null || (i == 2)) break;
  59. Thread.Sleep(500);
  60. }
  61. if (result == null)
  62. {
  63. return new List<HouseWellPhoto>();
  64. //result = DbGetPositionData(autoFocusDTO.wellSnList.Select(x => x.well).ToList(), autoFocusDTO.houseSn, autoFocusDTO.tlSn, 0);
  65. }
  66. else
  67. {
  68. DbUpdatePositionData(result, autoFocusDTO.houseSn, autoFocusDTO.tlSn, 0);
  69. }
  70. return result;
  71. }
  72. catch (Exception ex)
  73. {
  74. ExceptionLogEvent?.Invoke(ex, "HouseBinController.GetAutoFocusController", null, LogEnum.RunException);
  75. return new List<HouseWellPhoto>();
  76. }
  77. }
  78. /// <summary>
  79. /// 获取拍照数据
  80. /// </summary>
  81. /// <param name="autoFocusDTO"></param>
  82. /// <returns></returns>
  83. public PositionInfoResultDTO GetCCDPositionController(PositionRequestDTO autoFocusDTO)
  84. {
  85. try
  86. {
  87. string body = JsonConvert.SerializeObject(autoFocusDTO);
  88. PositionInfoResultDTO result = null;
  89. for (int i = 0; i < 3; i++)
  90. {
  91. result = ServiceCCDPositionData(body);
  92. if(result != null || (i == 2)) break;
  93. Thread.Sleep(500);
  94. //if(autoFocusPositionDTOs.complete == 0)//完成
  95. //{
  96. // List<HouseWellPhoto> CCDPositionList = new List<HouseWellPhoto>();
  97. // foreach (var item in autoFocusPositionDTOs.positionVOList)
  98. // {
  99. // CCDPositionList.Add(ConvertHelper.ConvertToHouseWellPhoto(item));
  100. // }
  101. // DbUpdatePositionData(CCDPositionList, autoFocusDTO.houseSn, autoFocusDTO.tlSn, 1);
  102. // return CCDPositionList;
  103. //}
  104. //else//未完成
  105. //{
  106. // return new List<HouseWellPhoto>();
  107. //}
  108. }
  109. return result;
  110. //return DbGetPositionData(autoFocusDTO.wellSnList.Select(x=>x.well).ToList(), autoFocusDTO.houseSn, autoFocusDTO.tlSn, 1);
  111. }
  112. catch (Exception ex)
  113. {
  114. ExceptionLogEvent?.Invoke(ex, "HouseBinController.GetCCDController", null, LogEnum.RunException);
  115. return null;
  116. }
  117. }
  118. #region 服务器操作
  119. /// <summary>
  120. /// 服务器-->获取自动对焦起点数据
  121. /// </summary>
  122. /// <param name="body"></param>
  123. /// <returns>null表示查询失败,空集合表示查询成功,但是服务器没有返回数据</returns>
  124. private List<HouseWellPhoto> ServiceAutoFocusData(string body)
  125. {
  126. string funcName = "ServiceAutoFocusData";
  127. try
  128. {
  129. string url = $"/api/tl/control/time-lapse-equipment/autofocus/position";
  130. string apiResultString = _httpService.callWebService(url, body);
  131. if (string.IsNullOrEmpty(apiResultString)) return null;
  132. HttpResult<List<positionResultDTO>> apiResult = JsonConvert.DeserializeObject<HttpResult<List<positionResultDTO>>>(apiResultString);
  133. if (!apiResult.success)
  134. {
  135. ErrorLog($"{funcName}接口返回失败,返回值{apiResultString}", LogEnum.RunError);
  136. return null;
  137. }
  138. if (apiResult.data != null && apiResult.data.Any())
  139. {
  140. List<HouseWellPhoto> autofocusList = new List<HouseWellPhoto>();
  141. foreach (var item in apiResult.data)
  142. {
  143. autofocusList.Add(ConvertHelper.ConvertToHouseWellPhoto(item));
  144. }
  145. return autofocusList;
  146. }
  147. ErrorLog($"{funcName}接口返回成功但是无数据 {apiResultString}", LogEnum.RunError);
  148. return null;
  149. }
  150. catch (Exception ex)
  151. {
  152. ExLog(ex, funcName);
  153. return null;
  154. }
  155. }
  156. /// <summary>
  157. /// 服务器-->获取拍照位置数据
  158. /// </summary>
  159. /// <param name="body"></param>
  160. /// <returns>null表示查询失败,空集合表示查询成功,但是服务器没有返回数据</returns>
  161. private PositionInfoResultDTO ServiceCCDPositionData(string body)
  162. {
  163. string name = "ServiceCCDPositionData";
  164. try
  165. {
  166. string url = $"/api/tl/control/time-lapse-equipment/ccd/position";
  167. string apiResultString = _httpService.callWebService(url, body);
  168. if (string.IsNullOrEmpty(apiResultString)) return null;
  169. HttpResult<PositionInfoResultDTO> apiResult = JsonConvert.DeserializeObject<HttpResult<PositionInfoResultDTO>>(apiResultString);
  170. if (!apiResult.success)
  171. {
  172. ErrorLog($"{name}接口返回失败,返回值{apiResultString}", LogEnum.RunError);
  173. return null;
  174. }
  175. if (apiResult.data != null) return apiResult.data;
  176. ErrorLog($"{name}接口返回成功但是无数据 {apiResultString}", LogEnum.RunError);
  177. return null;
  178. }
  179. catch (Exception ex)
  180. {
  181. ExLog(ex, name);
  182. return null;
  183. }
  184. }
  185. #endregion
  186. #region 数据库操作
  187. /// <summary>
  188. /// 更新电机位置
  189. /// </summary>
  190. /// <param name="list"></param>
  191. /// <param name="houseSn"></param>
  192. /// <param name="tlsn"></param>
  193. /// <param name="positionType">0表示自动对焦</param>
  194. public void DbUpdatePositionData(List<HouseWellPhoto> list,int houseSn, string tlsn, int positionType)
  195. {
  196. try
  197. {
  198. List<HouseWellPhotoDB> houseWellPhotoDbList = new List<HouseWellPhotoDB>();
  199. foreach (var item in list)
  200. {
  201. houseWellPhotoDbList.Add(ConvertHelper.ConvertToHouseWellPhotoDB(item));
  202. }
  203. _dBService.UpdatePosition(houseWellPhotoDbList, houseSn, tlsn, positionType);
  204. }
  205. catch (Exception ex)
  206. {
  207. ExceptionLogEvent?.Invoke(ex, "HouseBinController.DbUpdatePositionData", null, LogEnum.RunException);
  208. }
  209. }
  210. public List<HouseWellPhoto> DbGetPositionData(List<int> list, int houseSn, string tlsn, int positionType)
  211. {
  212. try
  213. {
  214. List<HouseWellPhoto> result = new List<HouseWellPhoto>();
  215. var newList = _dBService.GetPosition(list, houseSn, tlsn, positionType);
  216. foreach (var item in newList)
  217. {
  218. result.Add(ConvertHelper.ConvertToHouseWellPhoto(item));
  219. }
  220. return result;
  221. }
  222. catch (Exception ex)
  223. {
  224. ExceptionLogEvent?.Invoke(ex, "HouseBinController.DbGetPositionData", null, LogEnum.RunException);
  225. return new List<HouseWellPhoto>();
  226. }
  227. }
  228. #endregion
  229. }
  230. }