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...

127 lines
5.7 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Admin.Core.IService;
using Admin.Core.Model;
using log4net;
using Serilog;
using StackExchange.Profiling.Internal;
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;
System.Timers.Timer timer1 = new System.Timers.Timer(5000);
bool flag = true;
public AucamTemperatureMeasurementService(ITemperatureHistoryServices temperatureHistoryServices,
ITestedCodeMESServices codeMESServices)
{
_codeMESServices = codeMESServices;
_temperatureHistoryServices = temperatureHistoryServices;
}
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;
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)
{
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
}
}