schangxiang@126.com
2024-08-26 350e1111aa34534fa6074072142deaf319f8d6b4
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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}工序监控设备采集 不做校验,请确认是否配置异常");
            }
        }
 
    }
}