schangxiang@126.com
2024-09-10 429923d78ca4a016ab86adf30d189eb0e7774925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using iWare_SCADA_BusinessLogical.BLL;
using iWare_SCADA_BusinessLogical.Utils;
using iWare_SCADA_Model;
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>();
                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"))
                        {
 
                        }
                        //_dataCaptureConfig
                        if (plcService == null || !plcService.IsConnected)
                        {
                            SystemValue.lbl_Alert_HMIBeartBeatMonitor = $" {RandomHelper.GenerateRandomCode(4)} {_dataCaptureConfig.WorkingProcedure} PLC连接已断开,正在尝试打开!";
                            plcService.Close();
                            plcService.OpenService();
                            Thread.Sleep(100);
                            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);
                                    }
 
 
                                }
 
                                WorkPieceLogMiddle wplog = new WorkPieceLogMiddle();
                                wplog.WorkingProcedure = WorkingProcedure;
                                wplog.EquipmentID = _dataCaptureConfig.EquipmentID;
                                wplog.Remarks = "HMI";
                                wplog.MonitoringPoint = DataCapturePointCode;
                                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}数采心跳写入异常:{write.resMsg}");
                                    }
 
                                }
                            }
                            catch (Exception ex)
                            {
                                Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(WorkingProcedure), $"{WorkingProcedure}数采心跳写入异常:", 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}工序心跳 不做校验,请确认是否配置异常");
            }
        }
 
    }
}