You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AUCMA_SCADA/Aucma.Core.TemperatureTask/AucamTemperatureMeasurement...

209 lines
9.6 KiB
C#

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;
/// <summary>
/// 测温数据采集
/// </summary>
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 采集任务处理
/// <summary>
/// 采集任务处理
/// </summary>
/// <returns></returns>
private void Run(object? sender, ElapsedEventArgs e)
{
try
{
Console.WriteLine($"【{DateTime.Now}】开始读取测温数据");
if (flag)
{
try
{
flag = false;
10 months ago
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,
10 months ago
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,
10 months ago
SyncDate = DateTime.Now
}).ToList();
if (query == null || query.Count == 0) return;
logHelper.Error($"读取数据个数:{historyList.Count()}");
Console.WriteLine($"【{DateTime.Now}】读取数据个数:{historyList.Count()}");
10 months ago
var result = _temperatureHistoryServices.AddAsync(query).Result;
if (result > 0)
{
historyList.ForEach(d => d.IsSync = 1);
10 months ago
int r = _codeMESServices.UpateTemperatureHistoryFlag(historyList);
if (r > 0)
{
InsertToCompleteAndCheck(query);//保存过点数据
//发送post 请求
PostToMes(query);
10 months ago
Console.WriteLine($"【{DateTime.Now}】保存测温数据成功!保存数量:{query.Count}");
10 months ago
}
}
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 保存过点数和测温质检
/// <summary>
/// 保存过点数和测温质检
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public bool InsertToCompleteAndCheck(List<TemperatureHistory> list)
{
List<MaterialCompletion> completionList = new List<MaterialCompletion>();
var printBarCodeList = _printBarCodeServices.QueryAsync().Result;
foreach (var temperatureHistory in list)
{
PrintBarCode barcode = printBarCodeList.First(d => d.MaterialBarcode == temperatureHistory.CpNo);
10 months ago
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 发送质检请求
/// <summary>
/// 发送质检请求
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public bool PostToMes(List<TemperatureHistory> list)
{
Console.WriteLine($"【{DateTime.Now}】开始向质检发送测温数据");
string address = Appsettings.app("Common", "TemperatureApiAddRess");//测温地址
Console.WriteLine($"【{DateTime.Now}】请求路径:{address}/report/qualityInspection/temperatureApi");
Console.WriteLine($"【{DateTime.Now}】发送数据:{list.ToJson()}");
10 months ago
list= list.Where(d=>d.CpResult== "OK").ToList();
Console.WriteLine($"发送Post信息{list.ToJson()}");
string check = HttpHelper.Post(address + "/report/qualityInspection/temperatureApi", list.ToJson());
Result r = check.FromJson<Result>();
if (r.code == 200)
{
Console.WriteLine($"【{DateTime.Now}】发送质检请求成功!");
Console.WriteLine($"【{DateTime.Now}】开始向质检发送测温数据");
return true;
}
10 months ago
else
{
Console.WriteLine($"【{DateTime.Now}】向质检发送测温数据失败:返回结果:{r.code};{r.msg}");
}
return false;
}
#endregion
}
public class Result{
/// <summary>
/// 返回抓鬼太
/// </summary>
public int code { get; set; }
/// <summary>
/// 返回信息
/// </summary>
public string msg { get; set; }
}
}