using DBEntity; using Dm.Config; using IvfTl.Control.Entity.DBEntitys; using IvfTl.Control.Entity.DTO; using IvfTl.Control.Entity.DTO.ApiResutlDatas; using IvfTl.Control.Entity.DTO.ControllerResults; using IvfTl.Control.Entity.GlobalEntitys; using IvfTl.Control.Entity.GlobalEnums; using IvfTl.Control.Entity.InitEntitys; using IvfTl.Control.Services; using IvfTl.Control.Services.HttpServices; using ivf_tl_UtilHelper; using Newtonsoft.Json; using System; using System.Diagnostics; using System.Security.Claims; using System.Xml.Linq; namespace ivf_tl_Controller { public class SerialBinController { /// /// 日志事件 /// public event Action LogEvent; /// /// 异常日志事件 /// public event Action ExceptionLogEvent; private HttpService _httpService; private DBService _dBService; public SerialBinController(HttpService httpService, DBService dBService) { _httpService = httpService; _dBService = dBService; } private void Alarm() { } private void ExLog(Exception ex, string name) { ExceptionLogEvent?.Invoke(ex, $"SerialBinController.{name}", null, LogEnum.RunException); } private void ErrorLog(string message, LogEnum logType) { LogEvent?.Invoke($"SerialBinController.{message}", logType); } /// /// 每次开机都要更新TL的数据 /// public TLInitControllerResult UpdateTLInfoController(TLInitData tLInitData) { try { string body = JsonConvert.SerializeObject(tLInitData); TLInitControllerResult tLInitControllerResult = null; for (int i = 0; i < 3; i++) { tLInitControllerResult = ServiceUpdateTLInfo(body); if (tLInitControllerResult != null) break; ErrorLog($"服务器第{i + 1}次获取配置文件失败,[tlsn:{tLInitData.tlSn}]", LogEnum.RunRecord); Thread.Sleep(500); } if (tLInitControllerResult == null) { ErrorLog($"读取本地配置文件,[tlsn:{tLInitData.tlSn}]", LogEnum.RunRecord); tLInitControllerResult = DbGetTLInfo(tLInitData); } else { _dBService.DBUpdateTLInfo(tLInitControllerResult); } return tLInitControllerResult; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "SerialBinController.TLInitController", null, LogEnum.RunException); return null; } } /// /// 获取培养以及平衡数据 /// /// public HouseDataControllerResult GetDishAndBalanceDataController(string tlSn) { try { HouseDataControllerResult houseDataControllerResult = null; for (int i = 0; i < 3; i++) { houseDataControllerResult = ServiceDishAndBalanceData(tlSn); if (houseDataControllerResult != null) break; ErrorLog($"服务器第{i + 1}次读取培养记录失败,[tlsn:{tlSn}]", LogEnum.RunRecord); Thread.Sleep(500); } if (houseDataControllerResult == null) { ErrorLog($"读取本地培养记录,[tlsn:{tlSn}]", LogEnum.RunRecord); houseDataControllerResult = DbDishAndBalanceData(tlSn); } else { DbUpdateDishAndBalance(houseDataControllerResult, tlSn); } return houseDataControllerResult; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "SerialBinController.GetDishAndBalanceDataController", null, LogEnum.RunException); return new HouseDataControllerResult(); } } /// /// mqtt处理结果 /// /// /// /// public void MqttResultController(int code, bool isSuccess, string messageId, string uuid) { string name = "MqttResultController"; try { string body = JsonConvert.SerializeObject(new { code = code, message = isSuccess ? "操作成功" : "操作失败", messageId = messageId, success = isSuccess, uuid = uuid, }); TLInitControllerResult tLInitControllerResult = new TLInitControllerResult(); string url = $"/api/tl/control/time-lapse-equipment/result"; string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) return; HttpResult apiResult = JsonConvert.DeserializeObject(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return; } } catch (Exception ex) { ExLog(ex, name); return; } } /// /// mqtt处理结果 /// /// /// /// public void MqttResultController(int code, bool isSuccess, string messageId) { string name = "MqttResultController"; try { string body = JsonConvert.SerializeObject(new { code = code, message = isSuccess ? "操作成功" : "操作失败", messageId = messageId, success = isSuccess, }); TLInitControllerResult tLInitControllerResult = new TLInitControllerResult(); string url = $"/api/tl/control/time-lapse-equipment/result"; string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) return; HttpResult apiResult = JsonConvert.DeserializeObject(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return; } } catch (Exception ex) { ExLog(ex, name); return; } } /// /// 上报历史数据 /// /// public void ReportDataController(string body) { string name = "ReportDataController"; try { string url = $"/api/tl/control/time-lapse-equipment/report/data"; string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) return; HttpResult apiResult = JsonConvert.DeserializeObject(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return; } } catch (Exception ex) { ExLog(ex, $"{name}"); return; } } /// /// 更新仓室自动对焦状态 /// /// /// /// public void UpdateAutofocusStateController(string tlSn, int houseSN, int newValue) { string name = "UpdateAutofocusStateController"; try { string body = JsonConvert.SerializeObject(new { tlSn = tlSn, houseSn = houseSN, state = newValue, }); string url = $"/api/tl/control/time-lapse-equipment/change/autofocus/state"; string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) return; HttpResult apiResult = JsonConvert.DeserializeObject(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return; } } catch (Exception ex) { ExLog(ex, name); return; } } /// /// 保存图片 /// /// public void SavePictreController(ImageDTO imageDTO) { string name = "SavePictreController"; try { PictureDB pictureDB = ConvertHelper.ConvertToPictureDB(imageDTO); if (pictureDB != null) { _dBService.AddPictrue(pictureDB); } } catch (Exception ex) { ExLog(ex, name); return; } } /// /// 删除数据库图片 /// /// /// public void DeletePictrueController(string fileName, string tlsn) { string name = "DeletePictrueController"; try { _dBService.DeletePictrue(fileName, tlsn); } catch (Exception ex) { ExLog(ex, name); return; } } /// /// 查询数据库图片 /// /// /// /// public ImageDTO SearchPictureController(string fileName, string tlsn) { string name = "DeletePictrueController"; try { var aa = _dBService.SearchPicture(fileName, tlsn); return ConvertHelper.ConvertToPictureDB(aa); } catch (Exception ex) { ExLog(ex, name); return null; } } /// /// tl心跳检测 /// /// /// 是否退出 0 退出 1 在线 public void PushMessageController(string tlSn, int loginState, DiskInfo diskInfo) { string name = "PushMessageController"; try { string url = $"/api/tl/control/alarm/pushMessage"; string body = JsonConvert.SerializeObject(new { tlSn = tlSn, logout = loginState }); if (diskInfo != null) body = JsonConvert.SerializeObject(new { tlSn = tlSn, logout = loginState, diskPath = diskInfo.diskPath, diskExist = diskInfo.diskExist, diskSpace = diskInfo.diskSpace }); string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) return; HttpResult apiResult = JsonConvert.DeserializeObject(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return; } } catch (Exception ex) { ExLog(ex, name); return; } } /// /// 下位机报警 /// /// /// /// /// public void ReportAlarmController(string tlSn, int housesn, int houseState, int comState, int photoState, int wellSN,int airSwapState) { string name = "ReportAlarmController"; try { string url = $"/api/tl/control/alarm/reportAlarm"; string body = JsonConvert.SerializeObject(new { tlSn = tlSn, houseSn = housesn, houseState = houseState, portState = comState, photoState = photoState, airSwapState= airSwapState }); if (wellSN != -1) { body = JsonConvert.SerializeObject(new { tlSn = tlSn, houseSn = housesn, houseState = houseState, portState = comState, photoState = photoState, wellSn = wellSN, airSwapState = airSwapState }); } string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) return; HttpResult apiResult = JsonConvert.DeserializeObject(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return; } } catch (Exception ex) { ExLog(ex, name); return; } } /// /// 更新本地配置信息 /// /// public TLInitControllerResult UpdataSettingController(string tlSn) { string name = "UpdataSettingController"; try { string url = $"/api/tl/control/time-lapse-equipment/info"; string body = JsonConvert.SerializeObject(new { tlSn = tlSn, }); string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) return null; HttpResult apiResult = JsonConvert.DeserializeObject>(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return null; } if (apiResult.data == null) { Alarm(); ErrorLog($"{name}接口返回成功,但是无数据,返回值{rsString}", LogEnum.RunError); return null; } List houseList = new List(); List houseWellSettingList = new List(); TLSetting tLSetting = ConvertHelper.ConvertToTLSetting(apiResult.data.tlInfo, apiResult.data.tlSetting); TLInitControllerResult tLInitControllerResult = new TLInitControllerResult(); tLInitControllerResult.TLSetting = tLSetting; tLInitControllerResult.HouseList = houseList; tLInitControllerResult.HouseWellList = houseWellSettingList; foreach (var item in apiResult.data.houseList) { houseList.Add(ConvertHelper.ConvertToHouse(item)); } foreach (var item in apiResult.data.houseWellSettingList) { houseWellSettingList.Add(ConvertHelper.ConvertToHouseWellSetting(item)); } _dBService.DBUpdateTLInfo(tLInitControllerResult); return tLInitControllerResult; } catch (Exception ex) { ExLog(ex, name); return null; } } #region 服务器操作 /// /// 服务器-->更新TL仪器的数据 /// /// private TLInitControllerResult ServiceUpdateTLInfo(string body) { string name = "ServiceUpdateTLInfo"; string url = "/api/tl/control/time-lapse-equipment/link"; try { string rsString = _httpService.callWebServiceSteam(url, body); if (string.IsNullOrEmpty(rsString)) return null; HttpResult apiResult = JsonConvert.DeserializeObject>(rsString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return null; } if (apiResult.data == null) { Alarm(); ErrorLog($"{name}接口返回成功,但是无数据,返回值{rsString}", LogEnum.RunError); return null; } List houseList = new List(); List houseWellSettingList = new List(); TLSetting tLSetting = ConvertHelper.ConvertToTLSetting(apiResult.data.tlInfo, apiResult.data.tlSetting); TLInitControllerResult tLInitControllerResult = new TLInitControllerResult(); tLInitControllerResult.TLSetting = tLSetting; tLInitControllerResult.HouseList = houseList; tLInitControllerResult.HouseWellList = houseWellSettingList; foreach (var item in apiResult.data.houseList) { houseList.Add(ConvertHelper.ConvertToHouse(item)); } foreach (var item in apiResult.data.houseWellSettingList) { houseWellSettingList.Add(ConvertHelper.ConvertToHouseWellSetting(item)); } return tLInitControllerResult; } catch (Exception ex) { ExLog(ex, name); return null; } } /// /// 服务器(弃用)-->TL仪器数据初始化(仅第一次连接使用) /// /// /// private TLInitControllerResult ServiceInitTLInfo(string body) { string name = "ServiceInitTLInfo"; try { TLInitControllerResult tLInitControllerResult = new TLInitControllerResult(); string url = $"/api/tl/control/time-lapse-equipment/init"; string rsString = _httpService.callWebService(url, body); if (string.IsNullOrEmpty(rsString)) { ErrorLog($"{name}返回值为空", LogEnum.RunError); return null; } HttpResult apiResult = JsonConvert.DeserializeObject>(rsString); if (!apiResult.success) { ErrorLog($"{name}接口返回失败,返回值{rsString}", LogEnum.RunError); return null; } List houseList = new List(); List houseWellSettingList = new List(); foreach (var item in apiResult.data.houseList) { houseList.Add(ConvertHelper.ConvertToHouse(item)); } foreach (var item in apiResult.data.houseWellSettingList) { houseWellSettingList.Add(ConvertHelper.ConvertToHouseWellSetting(item)); } tLInitControllerResult.HouseList = houseList; tLInitControllerResult.HouseWellList = houseWellSettingList; ErrorLog($"SerialBinController.ServiceInitTLInfo,结果:{JsonConvert.SerializeObject(tLInitControllerResult)}", LogEnum.RunError); return tLInitControllerResult; } catch (Exception ex) { ExLog(ex, name); return null; } } /// /// 服务器-->获取培养以及平衡数据 /// /// private HouseDataControllerResult ServiceDishAndBalanceData(string tlSn) { string name = "ServiceDishAndBalanceData"; try { HouseDataControllerResult houseDataControllerResult = new HouseDataControllerResult(); string url = $"/api/tl/control/time-lapse-equipment/house/data"; Dictionary keyValuePairs = new Dictionary(); keyValuePairs.Add("tlSn", tlSn); string apiResultString = _httpService.callWebService(url, keyValuePairs); if (string.IsNullOrEmpty(apiResultString)) return null; HttpResult apiResult = JsonConvert.DeserializeObject>(apiResultString); if (!apiResult.success) { Alarm(); ErrorLog($"{name}接口返回失败,返回值{apiResultString}", LogEnum.RunError); return null; } if (apiResult.data == null) { Alarm(); ErrorLog($"{name}接口返回成功,但是无数据,返回值{apiResultString}", LogEnum.RunError); return null; } List balances = new List(); List dishes = new List(); foreach (var item in apiResult.data.balanceList) { balances.Add(ConvertHelper.ConvertToBalance(item)); } foreach (var item in apiResult.data.dishList) { dishes.Add(ConvertHelper.ConvertToDish(item)); } houseDataControllerResult.BalanceList = balances; houseDataControllerResult.Dishes = dishes; return houseDataControllerResult; } catch (Exception ex) { ExLog(ex, name); return null; } } #endregion #region 数据库操作 /// /// 从数据库获取TL设置 /// /// /// private TLInitControllerResult DbGetTLInfo(TLInitData tLInitData) { try { ErrorLog("使用本地TL设置开始运行程序", LogEnum.RunRecord); var dbInfo = _dBService.DBGetTLInfo(tLInitData); TLInitControllerResult tLInitControllerResult = new TLInitControllerResult(); tLInitControllerResult.TLSetting = ConvertHelper.ConvertToTLSetting(dbInfo.Item1); tLInitControllerResult.HouseList = new List(); foreach (var item in dbInfo.Item2) { tLInitControllerResult.HouseList.Add(ConvertHelper.ConvertToHouse(item)); } tLInitControllerResult.HouseWellList = new List(); foreach (var item in dbInfo.Item3) { tLInitControllerResult.HouseWellList.Add(ConvertHelper.ConvertToHouseWellSetting(item)); } return tLInitControllerResult; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "HouseBinController.DbGetTLInfo", null, LogEnum.RunException); return null; } } /// /// 从数据库获取培养记录以及平衡记录 /// /// /// private HouseDataControllerResult DbDishAndBalanceData(string tlSn) { try { ErrorLog("从本地数据库获取培养记录以及平衡记录", LogEnum.RunRecord); HouseDataControllerResult houseDataControllerResult = new HouseDataControllerResult(); houseDataControllerResult.Dishes = _dBService.GetDBDishs(tlSn); houseDataControllerResult.BalanceList = _dBService.DbGetBalances(tlSn); return houseDataControllerResult; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "HouseBinController.DbDishAndBalanceData", null, LogEnum.RunException); return new HouseDataControllerResult(); } } /// /// 数据库-->更新培养记录以及平衡记录 /// /// /// private void DbUpdateDishAndBalance(HouseDataControllerResult houseDataControllerResult, string tlsn) { try { List dishDbList = new List(); List embryoDbList = new List(); List balanceDbList = new List(); foreach (var item in houseDataControllerResult.Dishes) { dishDbList.Add(ConvertHelper.ConvertToDishDB(item)); foreach (var itemEmbryo in item.Embryo) { embryoDbList.Add(ConvertHelper.ConvertToEmbryoDB(itemEmbryo)); } } foreach (var item in houseDataControllerResult.BalanceList) { balanceDbList.Add(ConvertHelper.ConvertToBalanceDB(item)); } _dBService.AddDishs(dishDbList, tlsn); _dBService.AddEmbryos(embryoDbList, tlsn); _dBService.AddBalances(balanceDbList, tlsn); } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "HouseBinController.DbUpdateDishAndBalance", null, LogEnum.RunException); } } #endregion #region 弃用 /// /// tl数据初始化 /// /// /// /// private TLInitControllerResult TLInitController(TLInitData tLInitData, List houseEEPROInfos) { try { string body1 = JsonConvert.SerializeObject(tLInitData); string body2 = JsonConvert.SerializeObject(houseEEPROInfos); TLInitControllerResult tLInitControllerResult = LinkServiceController(body1, body2); if (tLInitControllerResult == null) { } return tLInitControllerResult; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "SerialBinController.TLInitController", null, LogEnum.RunException); return null; } } /// /// 连接服务器获取TL数据 /// /// /// private TLInitControllerResult LinkServiceController(string body1, string body2) { string name = "LinkServiceController"; try { TLInitControllerResult tLInitControllerResult = new TLInitControllerResult(); string url = $"/api/tl/control/time-lapse-equipment/link"; string apiResultString = _httpService.callWebService(url, body1); if (string.IsNullOrEmpty(apiResultString)) { ErrorLog($"{name}返回值为空", LogEnum.RunError); return null; } HttpResult apiResult = JsonConvert.DeserializeObject>(apiResultString); if (!apiResult.success) { ErrorLog($"{name}接口返回失败,返回值{apiResultString}", LogEnum.RunError); return null; } List houseList = new List(); List houseWellSettingList = new List(); TLSetting tLSetting = ConvertHelper.ConvertToTLSetting(apiResult.data.tlInfo, apiResult.data.tlSetting); tLInitControllerResult.TLSetting = tLSetting; tLInitControllerResult.HouseList = houseList; tLInitControllerResult.HouseWellList = houseWellSettingList; //if (true) //{ // url = $"{_urlIp}/api/tl/control/time-lapse-equipment/init"; // ss = _httpService.PostMethod(url, body2, _token); // LogEvent?.Invoke($"SerialBinController.LinkServiceController,init返回值{ss}", LogEnum.RunError); // apiResult = JsonConvert.DeserializeObject(ss); // if (apiResult == null) // { // LogEvent?.Invoke($"SerialBinController.LinkServiceController,init返回值反序列化失败,返回值{ss}", LogEnum.RunError); // return null; // } // if (!apiResult.success) // { // LogEvent?.Invoke($"SerialBinController.LinkServiceController,init接口返回失败,返回值{ss}", LogEnum.RunError); // return null; // } // InitApiResutlData initApiResutlData = JsonConvert.DeserializeObject(apiResult.data.ToString()); // foreach (var item in initApiResutlData.houseList) // { // houseList.Add(ConvertHelper.ConvertToHouse(item)); // } // foreach (var item in initApiResutlData.houseWellSettingList) // { // houseWellSettingList.Add(ConvertHelper.ConvertToHouseWellSetting(item)); // } //} //else //{ // foreach (var item in linkApiResutlData.houseList) // { // houseList.Add(ConvertHelper.ConvertToHouse(item)); // } // foreach (var item in linkApiResutlData.houseWellSettingList) // { // houseWellSettingList.Add(ConvertHelper.ConvertToHouseWellSetting(item)); // } //} //LogEvent?.Invoke($"tlSetting:{JsonConvert.SerializeObject(tLInitControllerResult)}", LogEnum.RunError); return tLInitControllerResult; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "SerialBinController.LinkServiceController", null, LogEnum.RunException); return null; } } /// /// TL仪器数据初始化(仅第一次连接使用) /// /// /// private TLInitControllerResult InitTLInfoController(List houseEEPROInfos) { try { string body = JsonConvert.SerializeObject(houseEEPROInfos); TLInitControllerResult tLInitControllerResult = ServiceInitTLInfo(body); return tLInitControllerResult; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "SerialBinController.TLInitController", null, LogEnum.RunException); return null; } } #endregion } }