using System;
using System.Timers;
using HslCommunication.Profinet.Siemens;
using Mesnac.Action.ChemicalWeighing.Services;
namespace Mesnac.Action.ChemicalWeighing.FinishBatch
{
///
/// 生产管理-后台业务-生产数据存盘业务服务类
///
public class FinishBatchService
{
#region 字段定义
///
/// 运行状态,true:已运行,false:未运行
///
private bool runState = false;
private bool flag = true;
#endregion
#region 单例实现
private static FinishBatchService _instance = null;
private FinishBatchService()
{
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += new ElapsedEventHandler(ReadData);
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}
public static FinishBatchService Instance
{
get
{
lock (String.Empty)
{
if (_instance == null)
{
_instance = new FinishBatchService();
}
return _instance;
}
}
}
#endregion
#region 启动服务
public void Start()
{
try
{
if (this.runState)
{
return;
}
lock (String.Empty)
{
//BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.read_data, out int[] p);
//Mesnac.Equips.Factory.Instance.SetMinHZReadDataEvent(ReadData);
//Mesnac.Equips.Factory.Instance.SetMinHZReadDataEvent(UpdateAlarmData);
this.runState = true;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("自动更新计划状态服务异常:" + ex.Message, ex);
}
}
#endregion
#region 停止服务
public void Stop()
{
try
{
if (!this.runState)
{
return;
}
lock (String.Empty)
{
ICSharpCode.Core.LoggingService.Debug("开始停止生产数据存盘服务...");
Mesnac.Equips.Factory.Instance.ClearMinHZReadDataEvent(ReadData);
ICSharpCode.Core.LoggingService.Debug("停止生产数据存盘服务完毕!");
this.runState = false;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("停止生产数据存盘业务服务异常:" + ex.Message, ex);
}
}
#endregion
#region 事件处理 普力通物料下传2023-06-12
private void ReadData(object sender, Mesnac.Equips.ReadEventArgs e)
{
try
{
lock (String.Empty)
{
#region 实时刷新自动保存报警
// SaveHelper.AlarmSaveHelper.Save();
#endregion
#region 更新小料计划状态
XlPlcHelper.PlcPlanHelper.ExecPlanState();
#endregion
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(ex.Message, ex);
}
}
#endregion
private void ReadData(object sender, ElapsedEventArgs e)
{
if (flag)
{
flag = false;
try
{
PlanHelperServer.SavePlcData();
}
catch (Exception ex)
{
Console.WriteLine($"FinishBatchService异常:{ex.Message}");
ICSharpCode.Core.LoggingService.Error(ex.Message, ex);
}
finally
{
GC.Collect();
flag = true;
}
}
}
}
}