using Admin.Core.Common; using Admin.Core.IService; using Admin.Core.Model; using Admin.Core.Model.Model_New; using log4net; using Serilog; using StackExchange.Profiling.Internal; using System.Collections.Generic; using System.Timers; /// /// 测温数据采集 /// namespace Aucma.Core.TemperatureTask { public class AucamTemperatureMeasurementService: IAucamTemperatureMeasurementService { public delegate Task RefreshBoxFoamDataDelegate(); public static event RefreshBoxFoamDataDelegate? RefreshBoxFoamDataDelegateEvent; private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(AucamTemperatureMeasurementService)); private readonly ITestedCodeMESServices _codeMESServices; private readonly ITemperatureHistoryServices _temperatureHistoryServices; private readonly IMaterialCompletionServices _materialCompletionServices; private readonly IPrintBarCodeServices _printBarCodeServices; private readonly IReportQualityInspectionServices _reportQualityInspectionServices; System.Timers.Timer timer1 = new System.Timers.Timer(5000); bool flag = true; public AucamTemperatureMeasurementService(ITemperatureHistoryServices temperatureHistoryServices, ITestedCodeMESServices codeMESServices, IMaterialCompletionServices materialCompletionServices, IPrintBarCodeServices printBarCodeServices, IReportQualityInspectionServices reportQualityInspectionServices) { _codeMESServices = codeMESServices; _temperatureHistoryServices = temperatureHistoryServices; _materialCompletionServices = materialCompletionServices; _printBarCodeServices = printBarCodeServices; _reportQualityInspectionServices = reportQualityInspectionServices; } public void Execute() { timer1.Elapsed += new System.Timers.ElapsedEventHandler(Run); //到达时间的时候执行事件; timer1.AutoReset = true;//设置是执行一次(false)还是一直执行(true); timer1.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它, timer1.Start();//timer.Start()的内部原理还是设置timer.Enabled = true; } #region 采集任务处理 /// /// 采集任务处理 /// /// private void Run(object? sender, ElapsedEventArgs e) { try { Console.WriteLine($"【{DateTime.Now}】开始读取测温数据"); if (flag) { try { flag = false; var historyList = _codeMESServices.QueryAsync(d=>d.IsSync==0).Result; Console.WriteLine(historyList.ToJson()); var query = (from a in historyList select new TemperatureHistory { Line_No = a.Line_No, GongwNo = a.GongwNo, CpNo = a.CpNo, CpModel = a.CpModel, FactoryModel = a.factoryModel, Voltage = a.Voltage, Frequency = a.Frequency, Point2 = a.Point2, TestTime = a.TestTime, TestedTime = a.TestedTime, Etemp = a.Etemp, BeginDateTime =Convert.ToDateTime(a.BeginDateTime), CpResult = a.CpResult, EndDateTime = Convert.ToDateTime(a.EndDateTime), UserName = a.UserName, Remark = a.Remark, TestNo = a.TestNo, Temp_Mes = a.Temp_Mes, Power_Mes = a.Power_Mes, Part_Mes = a.Part_Mes, LowV_Time = a.LowV_Time, LowV_PowerMin = a.LowV_PowerMin, LowV_PowerMax = a.LowV_PowerMax, LowV_PowerFact = a.LowV_PowerFact, LowVoltage = a.LowVoltage, SyncDate =DateTime.Now }).ToList(); if (query == null || query.Count == 0) return; logHelper.Error($"读取数据个数:{historyList.Count()}"); Console.WriteLine($"【{DateTime.Now}】读取数据个数:{historyList.Count()}"); var result = _temperatureHistoryServices.AddAsync(query).Result; if (result > 0) { historyList.ForEach(d => d.IsSync = 1); int r = _codeMESServices.UpateTemperatureHistoryFlag(historyList); if (r > 0) { InsertToCompleteAndCheck(query);//保存过点数据 //发送post 请求 PostToMes(query); Console.WriteLine($"【{DateTime.Now}】保存测温数据成功!保存数量:{query.Count}"); } } Console.WriteLine($"【{DateTime.Now}】测温数据同步成功!数量:{query.Count}"); } catch (Exception ex) { Console.WriteLine($"【{DateTime.Now}】 {ex.Message}"); logHelper.Error(ex.Message); } finally { flag = true; } } } catch (Exception ex) { logHelper.Error($"测温数据处理异常:{ex.Message}"); Console.WriteLine($"测温数据处理异常:{ex.Message}"); } } #endregion #region 保存过点数和测温质检 /// /// 保存过点数和测温质检 /// /// /// public bool InsertToCompleteAndCheck(List list) { List completionList = new List(); var printBarCodeList = _printBarCodeServices.QueryAsync().Result; foreach (var temperatureHistory in list) { PrintBarCode barcode = printBarCodeList.First(d => d.MaterialBarcode == temperatureHistory.CpNo); if (barcode != null) return false; #region 更新过点数据 MaterialCompletion completion = new MaterialCompletion(); completion.OrderCode = barcode.OrderCode; completion.MaterialBarcode = temperatureHistory.CpNo; completion.MaterialCode = barcode.MaterialCode; completion.MaterialName = barcode.MaterialName; completion.StationName = "测温检验01"; completion.ProductLineCode = "CX_02"; completion.CompleteDate = DateTime.Now; completionList.Add(completion); #endregion } int r = _materialCompletionServices.AddAsync(completionList).Result; if (r > 0) return true; else return false; } #endregion #region 发送质检请求 /// /// 发送质检请求 /// /// /// public bool PostToMes(List list) { string address = Appsettings.app("Common", "TemperatureApiAddRess");//测温地址 string check = HttpHelper.Post(address + "/report/qualityInspection/temperatureApi", list.ToJson()); Result r = check.FromJson(); if (r.code == 200) { Console.WriteLine("发送质检请求!"); return true; } return false; } #endregion } public class Result{ /// /// 返回抓鬼太 /// public int code { get; set; } /// /// 返回信息 /// public string msg { get; set; } } }