| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- using IvfTl.Control.Entity.DBEntitys;
- using IvfTl.Control.Entity.DTO;
- using IvfTl.Control.Entity.DTO.ApiRequestDTO;
- using IvfTl.Control.Entity.DTO.ApiResultDTO;
- using IvfTl.Control.Entity.DTO.ControllerResults;
- using IvfTl.Control.Entity.GlobalEntitys;
- using IvfTl.Control.Entity.GlobalEnums;
- using IvfTl.Control.Services;
- using IvfTl.Control.Services.HttpServices;
- using ivf_tl_UtilHelper;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace ivf_tl_Controller
- {
- public class HouseBinController
- {
- /// <summary>
- /// 日志事件
- /// </summary>
- public event Action<string, LogEnum> LogEvent;
- /// <summary>
- /// 异常日志事件
- /// </summary>
- public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
- private HttpService _httpService;
- private DBService _dBService;
- public HouseBinController(HttpService httpService, DBService dBService)
- {
- _httpService = httpService;
- _dBService= dBService;
- }
- private void ExLog(Exception ex, string name)
- {
- ExceptionLogEvent?.Invoke(ex, $"HouseBinController.{name}", null, LogEnum.RunException);
- }
- private void ErrorLog(string message, LogEnum logType)
- {
- LogEvent?.Invoke($"HouseBinController.{message}", logType);
- }
- /// <summary>
- /// 获取自动对焦数据
- /// </summary>
- /// <param name="body"></param>
- public List<HouseWellPhoto> GetAutoFocusController(PositionRequestDTO autoFocusDTO)
- {
- try
- {
- List<HouseWellPhoto> result = null;
- string body = JsonConvert.SerializeObject(autoFocusDTO);
- for (int i = 0; i < 3; i++)
- {
- result = ServiceAutoFocusData(body);
- if (result != null || (i == 2)) break;
- Thread.Sleep(500);
- }
- if (result == null)
- {
- return new List<HouseWellPhoto>();
- //result = DbGetPositionData(autoFocusDTO.wellSnList.Select(x => x.well).ToList(), autoFocusDTO.houseSn, autoFocusDTO.tlSn, 0);
- }
- else
- {
- DbUpdatePositionData(result, autoFocusDTO.houseSn, autoFocusDTO.tlSn, 0);
- }
- return result;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "HouseBinController.GetAutoFocusController", null, LogEnum.RunException);
- return new List<HouseWellPhoto>();
- }
- }
- /// <summary>
- /// 获取拍照数据
- /// </summary>
- /// <param name="autoFocusDTO"></param>
- /// <returns></returns>
- public PositionInfoResultDTO GetCCDPositionController(PositionRequestDTO autoFocusDTO)
- {
- try
- {
- string body = JsonConvert.SerializeObject(autoFocusDTO);
- PositionInfoResultDTO result = null;
- for (int i = 0; i < 3; i++)
- {
- result = ServiceCCDPositionData(body);
- if(result != null || (i == 2)) break;
- Thread.Sleep(500);
- //if(autoFocusPositionDTOs.complete == 0)//完成
- //{
- // List<HouseWellPhoto> CCDPositionList = new List<HouseWellPhoto>();
- // foreach (var item in autoFocusPositionDTOs.positionVOList)
- // {
- // CCDPositionList.Add(ConvertHelper.ConvertToHouseWellPhoto(item));
- // }
- // DbUpdatePositionData(CCDPositionList, autoFocusDTO.houseSn, autoFocusDTO.tlSn, 1);
- // return CCDPositionList;
- //}
- //else//未完成
- //{
- // return new List<HouseWellPhoto>();
- //}
- }
- return result;
- //return DbGetPositionData(autoFocusDTO.wellSnList.Select(x=>x.well).ToList(), autoFocusDTO.houseSn, autoFocusDTO.tlSn, 1);
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "HouseBinController.GetCCDController", null, LogEnum.RunException);
- return null;
- }
- }
- #region 服务器操作
- /// <summary>
- /// 服务器-->获取自动对焦起点数据
- /// </summary>
- /// <param name="body"></param>
- /// <returns>null表示查询失败,空集合表示查询成功,但是服务器没有返回数据</returns>
- private List<HouseWellPhoto> ServiceAutoFocusData(string body)
- {
- string funcName = "ServiceAutoFocusData";
- try
- {
- string url = $"/api/tl/control/time-lapse-equipment/autofocus/position";
- string apiResultString = _httpService.callWebService(url, body);
- if (string.IsNullOrEmpty(apiResultString)) return null;
- HttpResult<List<positionResultDTO>> apiResult = JsonConvert.DeserializeObject<HttpResult<List<positionResultDTO>>>(apiResultString);
- if (!apiResult.success)
- {
- ErrorLog($"{funcName}接口返回失败,返回值{apiResultString}", LogEnum.RunError);
- return null;
- }
- if (apiResult.data != null && apiResult.data.Any())
- {
- List<HouseWellPhoto> autofocusList = new List<HouseWellPhoto>();
- foreach (var item in apiResult.data)
- {
- autofocusList.Add(ConvertHelper.ConvertToHouseWellPhoto(item));
- }
- return autofocusList;
- }
- ErrorLog($"{funcName}接口返回成功但是无数据 {apiResultString}", LogEnum.RunError);
- return null;
- }
- catch (Exception ex)
- {
- ExLog(ex, funcName);
- return null;
- }
- }
- /// <summary>
- /// 服务器-->获取拍照位置数据
- /// </summary>
- /// <param name="body"></param>
- /// <returns>null表示查询失败,空集合表示查询成功,但是服务器没有返回数据</returns>
- private PositionInfoResultDTO ServiceCCDPositionData(string body)
- {
- string name = "ServiceCCDPositionData";
- try
- {
- string url = $"/api/tl/control/time-lapse-equipment/ccd/position";
- string apiResultString = _httpService.callWebService(url, body);
- if (string.IsNullOrEmpty(apiResultString)) return null;
- HttpResult<PositionInfoResultDTO> apiResult = JsonConvert.DeserializeObject<HttpResult<PositionInfoResultDTO>>(apiResultString);
- if (!apiResult.success)
- {
- ErrorLog($"{name}接口返回失败,返回值{apiResultString}", LogEnum.RunError);
- return null;
- }
- if (apiResult.data != null) return apiResult.data;
- ErrorLog($"{name}接口返回成功但是无数据 {apiResultString}", LogEnum.RunError);
- return null;
- }
- catch (Exception ex)
- {
- ExLog(ex, name);
- return null;
- }
- }
- #endregion
- #region 数据库操作
- /// <summary>
- /// 更新电机位置
- /// </summary>
- /// <param name="list"></param>
- /// <param name="houseSn"></param>
- /// <param name="tlsn"></param>
- /// <param name="positionType">0表示自动对焦</param>
- public void DbUpdatePositionData(List<HouseWellPhoto> list,int houseSn, string tlsn, int positionType)
- {
- try
- {
- List<HouseWellPhotoDB> houseWellPhotoDbList = new List<HouseWellPhotoDB>();
- foreach (var item in list)
- {
- houseWellPhotoDbList.Add(ConvertHelper.ConvertToHouseWellPhotoDB(item));
- }
- _dBService.UpdatePosition(houseWellPhotoDbList, houseSn, tlsn, positionType);
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "HouseBinController.DbUpdatePositionData", null, LogEnum.RunException);
- }
- }
- public List<HouseWellPhoto> DbGetPositionData(List<int> list, int houseSn, string tlsn, int positionType)
- {
- try
- {
- List<HouseWellPhoto> result = new List<HouseWellPhoto>();
- var newList = _dBService.GetPosition(list, houseSn, tlsn, positionType);
- foreach (var item in newList)
- {
- result.Add(ConvertHelper.ConvertToHouseWellPhoto(item));
- }
- return result;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "HouseBinController.DbGetPositionData", null, LogEnum.RunException);
- return new List<HouseWellPhoto>();
- }
- }
- #endregion
- }
- }
|