using iWare_SCADA_BusinessLogical.BLL;
|
using iWare_SCADA_BusinessLogical.Utils;
|
using iWare_SCADA_Model;
|
using iWare_SCADA_Model.MiddleModel;
|
using Newtonsoft.Json.Linq;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using System.Web.UI.WebControls;
|
|
namespace iWare_SCADA_BusinessLogical
|
{
|
/// <summary>
|
/// 设备信息采集
|
/// </summary>
|
public class DataCaptureHandler_06 : DataCaptureHandler
|
{
|
public static readonly DataCaptureHandler_06 Instance = new DataCaptureHandler_06();
|
|
public DataCaptureHandler_06()
|
{
|
}
|
|
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 (SystemValue.isStartedImitateModel)
|
{
|
var plcService = PLCManger.GetSinglePLCService(_dataCaptureConfig);
|
WorkPieceLogMiddle wplog = new WorkPieceLogMiddle();
|
wplog.Id = Yitter.IdGenerator.YitIdHelper.NextId();
|
wplog.WorkingProcedure = WorkingProcedure;
|
wplog.EquipmentID = WorkingProcedure;
|
wplog.Remarks = WorkingProcedure;
|
wplog.MonitoringPoint = DataCapturePointCode;
|
wplog.CreatedTime = DateTimeHelper.GetDateTime();
|
wplog.CreatedUserName = DataCapturePointCode;
|
wplog.IsDeleted = false;
|
|
|
wplog.WorkPieceID = WorkPieceID;
|
wplog.EquipmentID = _dataCaptureConfig.EquipmentID;
|
wplog.UpdatedTime = DateTimeHelper.GetDateTime();
|
wplog.UpdatedUserName = DataCapturePointCode;
|
//更新WorkPieceInfo表以及插入WorkPieceLog表和WorkPieceInfoLog表
|
WorkPieceInfoManager.WorkPiecePushOff(wplog, PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure));
|
|
return;
|
}
|
Dictionary<bool, DateTime> fullBeatDic = new Dictionary<bool, DateTime>();//FullMaterial 堵料 10秒false算堵料
|
Dictionary<bool, DateTime> lackBeatDic = new Dictionary<bool, DateTime>();//LackMaterial 缺料 10秒false算缺料
|
fullBeatDic.Add(false, DateTime.Now);
|
lackBeatDic.Add(false, DateTime.Now);
|
bool fullBeatMonitorflag = true;
|
bool lackBeatMonitorflag = true;
|
|
ThreadStatusMonitorMiddle threadStatusMonitor = new ThreadStatusMonitorMiddle();
|
if (IsCaptureflag)
|
{
|
//var plcService = PLCManger.GetSinglePLCService(_dataCaptureConfig);
|
var plcService = SystemValue.GetPLCService(_dataCaptureConfig);
|
if (plcService == null)
|
{
|
threadStatusMonitor.ErrorMsg = $"{RandomHelper.GenerateRandomCode(4)} 没有找到{_dataCaptureConfig.WorkingProcedure}的PLC设备";
|
return;
|
}
|
if (plcService != null && !plcService.IsConnected)
|
{
|
SystemValue.PLCServiceReconnect(plcService);
|
//plcService.Close();
|
//plcService.OpenService();
|
}
|
string value_06 = "";
|
string AlertStatus = "";//故障状态是否变更
|
string MessageStatus = "";//告警状态是否变更
|
while (true)
|
{
|
threadStatusMonitor.ErrorMsg = "";
|
threadStatusMonitor.Threadcode = DataCapturePointCode;
|
threadStatusMonitor.Threadcname = DataCapturePointCname;
|
threadStatusMonitor.Threadendtime = DateTime.Now;
|
threadStatusMonitor.Threadlastmodifytime = DateTime.Now;
|
threadStatusMonitor.Threadstatue = 0;
|
threadStatusMonitor.ThreadId = Thread.CurrentThread.ManagedThreadId.ToString();
|
try
|
{
|
//_dataCaptureConfig
|
if (plcService == null || !plcService.IsConnected)
|
{
|
threadStatusMonitor.ErrorMsg = $" {RandomHelper.GenerateRandomCode(4)} {_dataCaptureConfig.WorkingProcedure} PLC连接已断开,正在尝试打开!";
|
WorkPieceInfoManager.ThreadMonitor(threadStatusMonitor);
|
SystemValue.PLCServiceReconnect(plcService);
|
//plcService.Close();
|
//plcService.OpenService();
|
Thread.Sleep(100);
|
continue;
|
}
|
else
|
{
|
//业务代码
|
WorkPieceLogMiddle wplog = new WorkPieceLogMiddle();
|
wplog.Id = Yitter.IdGenerator.YitIdHelper.NextId();
|
wplog.WorkingProcedure = WorkingProcedure;
|
wplog.EquipmentID = _dataCaptureConfig.EquipmentID;;
|
wplog.MonitoringPoint = DataCapturePointCode;
|
wplog.CreatedTime = DateTimeHelper.GetDateTime();
|
wplog.CreatedUserName = DataCapturePointCode;
|
wplog.UpdatedTime = DateTimeHelper.GetDateTime();
|
wplog.UpdatedUserName = Environment.MachineName+"自动" + Thread.CurrentThread.ManagedThreadId.ToString();
|
wplog.OnlineTime = DateTimeHelper.GetDateTime();
|
wplog.IsDeleted = false;
|
|
foreach (var col in colConfig.Where(o => o.DataCapturePointCode == DataCapturePointCode))
|
{
|
try
|
{
|
var valuecol = plcService.ReadValuePoint(col.DbNumber, col.Offset, col.DataCaptureColumnLength.Value, PLCManger.GetTypeForString(col.DataCaptureColumnType));
|
//wplog.GetType().GetProperty(col.DataCaptureColumnTabelName).SetValue(wplog, valuecol);//给动态字段赋值
|
var set = wplog.GetType().GetProperty(col.DataCaptureColumnTabelName);
|
if (set == null)
|
{
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $" {DataCapturePointCode}工序设备采集 读取工件码动态由于字段名{col.DataCaptureColumnTabelName}没找到,赋值【{WorkingProcedure ?? "空字符串"}】失败{wplog.Id}");
|
continue;
|
}
|
set.SetValue(wplog, valuecol);//给动态字段赋值
|
}
|
catch (Exception setex)
|
{
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $" {DataCapturePointCode}工序设备采集 读取工件码动态赋值【{WorkingProcedure ?? "空字符串"}】异常{wplog.Id}", setex);
|
}
|
}
|
//if((AlertStatus.Equals("") && wplog.IsCloseAlert.ToString().ToUpper().Equals("TRUE")) || !(AlertStatus.ToUpper().Equals(wplog.IsCloseAlert.ToString().ToUpper())))
|
//{//说明告警信息数据发生变更
|
// wplog.ischeckAlertStatus=true;
|
//}
|
//if ((MessageStatus.Equals("") && wplog.IsMessage.ToString().ToUpper().Equals("TRUE")) || !(MessageStatus.ToUpper().Equals(wplog.IsMessage.ToString().ToUpper())))
|
//{//说明告警信息数据发生变更
|
// wplog.ischeckMessageStatus = true;
|
//}
|
if(wplog.WorkingProcedure.Equals("OP05"))
|
{
|
if (fullBeatMonitorflag != wplog.FullMaterial)
|
{//堵料心跳变化
|
|
if (fullBeatDic.ContainsKey(wplog.FullMaterial))
|
{
|
fullBeatDic[wplog.FullMaterial] = DateTime.Now;
|
}
|
else
|
{
|
fullBeatDic.Add((bool)wplog.FullMaterial, DateTime.Now);
|
}
|
|
}
|
fullBeatMonitorflag = wplog.FullMaterial;
|
|
var maxfull = fullBeatDic.Max(kvp => kvp.Value);
|
if (maxfull == null || (DateTime.Now - maxfull).TotalSeconds > 10)
|
{//心跳10秒没有更新认为设备堵料
|
wplog.FullMaterial = true;
|
}
|
else
|
{
|
wplog.FullMaterial = false;
|
}
|
|
if (lackBeatMonitorflag != wplog.LackMaterial)
|
{//缺料心跳变化
|
|
if (lackBeatDic.ContainsKey(wplog.LackMaterial))
|
{
|
lackBeatDic[wplog.LackMaterial] = DateTime.Now;
|
}
|
else
|
{
|
lackBeatDic.Add((bool)wplog.LackMaterial, DateTime.Now);
|
}
|
|
}
|
lackBeatMonitorflag = wplog.LackMaterial;
|
|
var maxlack = lackBeatDic.Max(kvp => kvp.Value);
|
if (maxlack == null || (DateTime.Now - maxlack).TotalSeconds > 10)
|
{//心跳10秒没有更新认为设备堵料
|
wplog.LackMaterial = true;
|
}
|
else
|
{
|
wplog.LackMaterial = false;
|
}
|
|
|
}
|
|
AlertStatus = wplog.IsCloseAlert.ToString().ToUpper();
|
AlertStatus = wplog.IsMessage.ToString().ToUpper();
|
WorkPieceInfoManager.EquipmentMonitor(wplog, PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure));
|
|
threadStatusMonitor.Threadstatue = 1;
|
}
|
threadStatusMonitor.Remarks = $"abcdefg:{RandomHelper.GenerateRandomCode(4)}";
|
if (!_dataCaptureConfig.DataCaptureFrequency.HasValue || _dataCaptureConfig.DataCaptureFrequency < 10)
|
{
|
threadStatusMonitor.ThreadFrequency = 5000;
|
Thread.Sleep(5000);
|
}
|
else
|
{
|
threadStatusMonitor.ThreadFrequency = _dataCaptureConfig.DataCaptureFrequency.Value;
|
Thread.Sleep(_dataCaptureConfig.DataCaptureFrequency.Value);
|
}
|
|
}
|
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
|
{
|
threadStatusMonitor.ErrorMsg = $" {RandomHelper.GenerateRandomCode(4)} 读取{DataCapturePointCode}工序监控设备采集 出现异常,请查看日志!";
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"读取 {DataCapturePointCode}工序监控设备采集数据时异常:", ex);
|
}
|
catch (Exception ex)
|
{
|
threadStatusMonitor.ErrorMsg = $" {RandomHelper.GenerateRandomCode(4)} 读取{DataCapturePointCode}工序监控设备采集 出现异常,请查看日志!";
|
Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"读取 {DataCapturePointCode}工序监控设备采集数据时异常:", ex);
|
}
|
finally
|
{
|
WorkPieceInfoManager.ThreadMonitor(threadStatusMonitor);
|
}
|
}
|
}
|
else
|
{
|
threadStatusMonitor.ErrorMsg = "";
|
threadStatusMonitor.Threadcode = DataCapturePointCode;
|
threadStatusMonitor.Threadcname = DataCapturePointCname;
|
threadStatusMonitor.Threadendtime = DateTime.Now;
|
//threadStatusMonitor.Threadlastmodifytime = DateTime.Now;
|
threadStatusMonitor.Threadstatue = 0;
|
threadStatusMonitor.ThreadId = Thread.CurrentThread.ManagedThreadId.ToString();
|
threadStatusMonitor.Remarks = $" {RandomHelper.GenerateRandomCode(4)} {DataCapturePointCode}工序监控设备采集 不做校验,请确认配置信息!";
|
WorkPieceInfoManager.ThreadMonitor(threadStatusMonitor);
|
Log4NetHelper.WriteInfoLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"读{DataCapturePointCode}工序监控设备采集 不做校验,请确认是否配置异常");
|
}
|
}
|
|
}
|
}
|