using iWare_SCADA_BusinessLogical.BLL;
|
using iWare_SCADA_BusinessLogical.Utils;
|
using iWare_SCADA_Model;
|
using Newtonsoft.Json.Linq;
|
using System;
|
using System.Collections.Generic;
|
using System.Data.Entity.Core.Common.CommandTrees;
|
using System.Data.Entity.Validation;
|
using System.Data.SqlTypes;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace iWare_SCADA_BusinessLogical
|
{
|
/// <summary>
|
/// HMI心跳监控
|
/// </summary>
|
public class HMIBeartBeatMonitor: DataCaptureHandler
|
{
|
public static readonly HMIBeartBeatMonitor Instance = new HMIBeartBeatMonitor();
|
|
public HMIBeartBeatMonitor()
|
{
|
}
|
|
public override string WorkingProcedure
|
{
|
get { return _dataCaptureConfig.WorkingProcedure; }
|
}
|
|
public override string DataCapturePointCode
|
{
|
get { return _dataCaptureConfig.DataCapturePointCode; }
|
}
|
public override string DataCapturePointCname
|
{
|
get { return _dataCaptureConfig.DataCapturePointCname; }
|
}
|
|
|
public override void RefreshDataList(List<DataCaptureConfig> dataCaptureConfig)
|
{
|
}
|
|
public override void DataCaptureStart()
|
{
|
if (IsCaptureflag)
|
{
|
var plcService = PLCManger.GetSinglePLCService(_dataCaptureConfig);
|
if (plcService == null)
|
{
|
SystemValue.lbl_Alert_HMIBeartBeatMonitor = $"{RandomHelper.GenerateRandomCode(4)} 没有找到{_dataCaptureConfig.WorkingProcedure}的PLC设备";
|
return;
|
}
|
if (plcService != null && !plcService.IsConnected)
|
{
|
plcService.Close();
|
plcService.OpenService();
|
}
|
Dictionary<bool, DateTime> beartBeatDic = new Dictionary<bool, DateTime>();
|
beartBeatDic.Add(false, DateTime.Now);
|
bool BeartBeatMonitorflag = true;
|
while (true)
|
{
|
ThreadStatusMonitor threadStatusMonitor = new ThreadStatusMonitor();
|
threadStatusMonitor.Threadcode = DataCapturePointCode;
|
threadStatusMonitor.Threadcname = DataCapturePointCname;
|
threadStatusMonitor.Threadendtime = DateTime.Now;
|
threadStatusMonitor.Threadlastmodifytime = DateTime.Now;
|
threadStatusMonitor.Threadstatue = 0;
|
threadStatusMonitor.ThreadId = Thread.CurrentThread.ManagedThreadId.ToString();
|
try
|
{
|
if(DataCapturePointCode.Equals("OP1008"))
|
{
|
|
}
|
WorkPieceLogMiddle wplog = new WorkPieceLogMiddle();
|
wplog.WorkingProcedure = WorkingProcedure;
|
wplog.EquipmentID = _dataCaptureConfig.EquipmentID;
|
wplog.Remarks = "HMI";
|
wplog.MonitoringPoint = DataCapturePointCode;
|
//_dataCaptureConfig
|
if (plcService == null || !plcService.IsConnected)
|
{
|
threadStatusMonitor.ErrorMsg = $" {RandomHelper.GenerateRandomCode(4)} {_dataCaptureConfig.WorkingProcedure} PLC连接已断开,正在尝试打开!";
|
SystemValue.lbl_Alert_HMIBeartBeatMonitor = $" {RandomHelper.GenerateRandomCode(4)} {_dataCaptureConfig.WorkingProcedure} PLC连接已断开,正在尝试打开!";
|
plcService.Close();
|
plcService.OpenService();
|
Thread.Sleep(100);
|
wplog.IsConnect = false;
|
WorkPieceInfoManager.EquipmentBeartBeatMonitor(wplog, PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure));
|
continue;
|
}
|
else
|
{
|
try
|
{//获取设备plc的心跳
|
|
var value = plcService.ReadValuePoint(_dataCaptureConfig.DbNumber, _dataCaptureConfig.Offset, PLCManger.GetTypeForString(_dataCaptureConfig.DataCaptureColumnType));
|
|
if (BeartBeatMonitorflag != (bool)value)
|
{//PLC心跳变化
|
|
if(beartBeatDic.ContainsKey((bool)value))
|
{
|
beartBeatDic[(bool)value]=DateTime.Now;
|
}
|
else
|
{
|
beartBeatDic.Add((bool)value, DateTime.Now);
|
}
|
|
}
|
|
var max = beartBeatDic.Max(kvp => kvp.Value);
|
if(max==null||(DateTime.Now-max).TotalSeconds>10)
|
{//心跳10秒没有更新认为设备断线了
|
wplog.IsConnect = false;
|
}
|
else
|
{
|
wplog.IsConnect = true;
|
}
|
//更新设备当前连接情况
|
WorkPieceInfoManager.EquipmentBeartBeatMonitor(wplog, PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure));
|
BeartBeatMonitorflag = (bool)value;
|
threadStatusMonitor.Threadstatue = 1;
|
}
|
catch (DbEntityValidationException ex1)
|
{
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"{WorkingProcedure}心跳读取异常1:", ex1);
|
}
|
catch (Exception ex)
|
{
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"{WorkingProcedure}心跳读取异常:", ex);
|
}
|
|
try
|
{//反写心跳
|
foreach (var col in colConfig.Where(o => o.DataCapturePointCode == DataCapturePointCode && o.IsFeedback == 1))
|
{//获取配置详情表中需写入心跳信息 pc
|
bool flag = true;
|
var colflag = plcService.ReadValuePoint(col.DbNumber, col.Offset, PLCManger.GetTypeForString(_dataCaptureConfig.DataCaptureColumnType));
|
if ((bool)colflag)
|
{
|
flag = false;
|
}
|
else
|
{
|
flag = true;
|
}
|
var write= plcService.WriteValuePointNew(col.DbNumber, col.Offset, flag);
|
if(!write.result)
|
{
|
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"{WorkingProcedure}数采心跳写入异常1:{write.resMsg}");
|
}
|
|
}
|
}
|
catch (Exception ex)
|
{
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"{WorkingProcedure}数采心跳写入异常2:", ex);
|
}
|
|
|
}
|
SystemValue.lbl_Alert_HMIBeartBeatMonitor = $"abcdefg:{RandomHelper.GenerateRandomCode(4)}";
|
|
}
|
catch (Exception ex)
|
{
|
SystemValue.lbl_Alert_HMIBeartBeatMonitor = $" {RandomHelper.GenerateRandomCode(4)} 读取{WorkingProcedure}工序心跳 出现异常,请查看日志!";
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"读取 {WorkingProcedure}工序心跳异常:", ex);
|
}
|
finally
|
{
|
WorkPieceInfoManager.ThreadMonitor(threadStatusMonitor);
|
}
|
}
|
}
|
else
|
{
|
SystemValue.lbl_Alert_HMIBeartBeatMonitor = $" {RandomHelper.GenerateRandomCode(4)} {WorkingProcedure}工序心跳 不做校验,请确认配置信息!";
|
Log4NetHelper.WriteInfoLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"读{WorkingProcedure}工序心跳 不做校验,请确认是否配置异常");
|
}
|
}
|
|
}
|
}
|