领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using Bro.Common.Helper;
using Bro.Common.Interface;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Configuration;
using System.IO;
 
namespace Bro.UI.ViewModel
{
    public class InformationViewModel : ViewModelBase
    {
        #region Properties
        private IProcess processControl = null;
        public IProcess ProcessControl
        {
            get => processControl;
            set
            {
                if (processControl?.ProductionCode != value.ProductionCode)
                {
                    processControl = value;
                    ResetProcessControl();
                }
            }
        }
 
        private void ResetProcessControl()
        {
        }
 
        private int qty_OK = 0;
        private int qty_NG = 0;
        private int qty_All = 0;
 
        private float qty_ok_rate = 0;
        private float qty_ng_rate = 0;
 
        private float ct = 0;
        private int uph = 0;
 
        private DateTime? startTime = null;
        private TimeSpan runTime = new TimeSpan();
        private TimeSpan idleTime = new TimeSpan();
        private TimeSpan downTime = new TimeSpan();
        private TimeSpan availableTime = new TimeSpan();
        private int qty_OEE = 0;
        private int qty_OEE_OK = 0;
        private float oee = 0;
        private int idealUPH = 10000;
 
        public int Qty_OK
        {
            get => qty_OK;
            set
            {
                Set(ref qty_OK, value);
 
                Qty_All = Qty_OK + Qty_NG;
                Qty_OK_Rate = Qty_All == 0 ? 0 : (float)Qty_OK / (float)Qty_All;
                Qty_NG_Rate = Qty_All == 0 ? 0 : (float)Qty_NG / (float)Qty_All;
            }
        }
        public int Qty_NG
        {
            get => qty_NG;
            set
            {
                Set(ref qty_NG, value);
 
                Qty_All = Qty_OK + Qty_NG;
                Qty_OK_Rate = Qty_All == 0 ? 0 : (float)Qty_OK / (float)Qty_All;
                Qty_NG_Rate = Qty_All == 0 ? 0 : (float)Qty_NG / (float)Qty_All;
            }
        }
 
        public int Qty_All { get => qty_All; set => Set(ref qty_All, value); }
        public float Qty_OK_Rate { get => qty_ok_rate; set => Set(ref qty_ok_rate, value); }
        public float Qty_NG_Rate { get => qty_ng_rate; set => Set(ref qty_ng_rate, value); }
 
        public float CT { get => ct; set => Set(ref ct, value); }
        public int UPH { get => uph; set => Set(ref uph, value); }
 
        public DateTime? StartTime { get => startTime; set => Set(ref startTime, value); }
 
        public TimeSpan RunTime
        {
            get => runTime;
            set
            {
                Set(ref runTime, value);
                //GetAvailableTime();
            }
        }
 
        public TimeSpan IdleTime
        {
            get => idleTime;
            set
            {
                Set(ref idleTime, value);
                GetRunTime();
            }
        }
 
        public TimeSpan DownTime
        {
            get => downTime;
            set
            {
                Set(ref downTime, value);
                GetRunTime();
            }
        }
 
        private void GetRunTime()
        {
            //AvailableTime = RunTime - IdleTime - DownTime;
 
            RunTime = AvailableTime + IdleTime + DownTime;
        }
 
        public TimeSpan AvailableTime
        {
            get => availableTime;
            set
            {
                Set(ref availableTime, value);
                GetRunTime();
            }
        }
 
        public int Qty_OEE { get => qty_OEE; set => Set(ref qty_OEE, value); }
        public int Qty_OEE_OK { get => qty_OEE_OK; set => Set(ref qty_OEE_OK, value); }
        public float OEE { get => oee; set => Set(ref oee, value); }
        public int IdealUPH { get => idealUPH; set => Set(ref idealUPH, value); }
 
        #endregion
 
        #region Command
        public RelayCommand Cmmd_ClearQty { get; set; }
        public RelayCommand Cmmd_RefreshOEE { get; set; }
        public RelayCommand Cmmd_ClearOEE { get; set; }
        #endregion
 
        System.Threading.Timer _availableTimer = null;
        System.Threading.Timer _idleTimer = null;
        System.Threading.Timer _downTimer = null;
        System.Threading.Timer _checkIdleTimer = null;
        System.Threading.Timer _calculateTimer = null;
 
        private RunState? currentState = null;
        public RunState? CurrentState
        {
            get => currentState;
            set
            {
                if (value != null)
                {
                    if (currentState != value.Value)
                    {
                        switch (value.Value)
                        {
                            case RunState.Stop:
                                _availableTimer.Change(-1, -1);
                                _idleTimer.Change(-1, -1);
                                _downTimer.Change(-1, -1);
                                break;
                            case RunState.Running:
                                _idleTimer.Change(-1, -1);
                                _downTimer.Change(-1, -1);
                                _availableTimer.Change(0, 1000);
                                break;
                            case RunState.Idle:
                                _availableTimer.Change(-1, -1);
                                _downTimer.Change(-1, -1);
                                _idleTimer.Change(0, 1000);
                                break;
                            case RunState.Down:
                                _idleTimer.Change(-1, -1);
                                _availableTimer.Change(-1, -1);
                                _downTimer.Change(0, 1000);
                                break;
                        }
 
                        currentState = value;
                    }
                }
            }
        }
 
        int _idleThreshold = 30;
 
        public InformationViewModel(IProcess control)
        {
            string idealUPHStr = ConfigurationManager.AppSettings["IdealUPH"];
            if (!string.IsNullOrWhiteSpace(idealUPHStr) && int.TryParse(idealUPHStr, out int idealUPHSetting))
            {
                IdealUPH = idealUPHSetting;
            }
 
            string idleThresholdStr = ConfigurationManager.AppSettings["IdleThreshold"];
            if (!string.IsNullOrWhiteSpace(idleThresholdStr) && int.TryParse(idleThresholdStr, out int idleTTemp))
            {
                _idleThreshold = idleTTemp;
            }
            else
            {
                _idleThreshold = 30;
            }
 
            ProcessControl = control;
 
            ProcessControl.OnProcessStateChanged -= OnProcessStateChanged;
            ProcessControl.OnProcessStateChanged += OnProcessStateChanged;
 
            ProcessControl.OnUpdateResult = UpdateResultOK;
            ProcessControl.OnUpdateCT = UpdateCT;
 
            Cmmd_ClearQty = new RelayCommand(OnClearQty);
            Cmmd_RefreshOEE = new RelayCommand(RefreshOEE);
            Cmmd_ClearOEE = new RelayCommand(ClearOEE);
 
            _availableTimer = new System.Threading.Timer(AvailableTimerCallBack, null, -1, -1);
            _idleTimer = new System.Threading.Timer(IdleTimerCallBack, null, -1, -1);
            _downTimer = new System.Threading.Timer(DownTimerCallBack, null, -1, -1);
            _checkIdleTimer = new System.Threading.Timer(CheckIdle, null, -1, -1);
 
            _calculateTimer = new System.Threading.Timer(Calculation, null, 1000 * 60, 1000 * 60);
 
            ReadNumRecords();
        }
 
        /// <summary>
        /// 每分钟计算OEE,平均UPH
        /// </summary>
        /// <param name="state"></param>
        private void Calculation(object state)
        {
            if (AvailableTime.TotalHours != 0)
            {
                UPH = (int)(Qty_OEE / AvailableTime.TotalHours);
            }
 
            if (IdealUPH == 0 || RunTime.TotalSeconds == 0 || Qty_OEE == 0)
            {
                OEE = 0;
            }
            else
            {
                OEE = (float)(AvailableTime.TotalSeconds / RunTime.TotalSeconds) * ((float)UPH / (float)IdealUPH) * ((float)Qty_OEE_OK / (float)Qty_OEE);
            }
        }
 
        private void ClearOEE()
        {
            AvailableTime = IdleTime = DownTime = new TimeSpan();
            Qty_OEE_OK = Qty_OEE = 0;
 
            if (ProcessControl.ProcessState == EnumHelper.DeviceState.DSOpen)
            {
                StartTime = DateTime.Now;
            }
            else
            {
                StartTime = null;
            }
        }
 
        private void DownTimerCallBack(object state)
        {
            DownTime = DownTime.Add(new TimeSpan(0, 0, 1));
        }
 
        private void IdleTimerCallBack(object state)
        {
            IdleTime = IdleTime.Add(new TimeSpan(0, 0, 1));
        }
 
        private void AvailableTimerCallBack(object state)
        {
            AvailableTime = AvailableTime.Add(new TimeSpan(0, 0, 1));
        }
 
        private void OnProcessStateChanged(EnumHelper.DeviceState state)
        {
            if (state == EnumHelper.DeviceState.DSOpen)
            {
                StartTime = DateTime.Now;
                CurrentState = RunState.Idle; ;
            }
            else
            {
                CurrentState = RunState.Stop;
            }
 
            if (state == EnumHelper.DeviceState.DSClose)
            {
                SaveNumRecord();
            }
        }
 
        private void CheckIdle(object state)
        {
            var idleBack = new TimeSpan(0, 0, _idleThreshold);
            IdleTime = IdleTime.Add(idleBack);
            AvailableTime = AvailableTime.Subtract(idleBack);
            CurrentState = RunState.Idle;
        }
 
        private void UpdateResultOK(DateTime dt, int result)
        {
            CurrentState = RunState.Running;
            _checkIdleTimer.Change(_idleThreshold * 1000, -1); //限定时间内没有上传Load认为进入空闲时间
 
            if (result == 1)
            {
                Qty_OK++;
                Qty_OEE_OK++;
 
                //if (_lastCTTime != null)
                //{
                //    CT = (float)(dt - _lastCTTime.Value).TotalSeconds;
                //}
 
                //_lastCTTime = dt;
            }
            else
            {
                Qty_NG++;
            }
 
            Qty_OEE++;
        }
 
        //DateTime? _lastCTTime = null;
        private void UpdateCT(float ctTime)
        {
            //if (_lastCTTime != null)
            //{
            //    CT = (float)(dt - _lastCTTime.Value).TotalSeconds;
            //}
 
            //_lastCTTime = dt;
 
            CT = ctTime;
        }
 
        private void RefreshOEE()
        {
            Calculation(null);
        }
 
        private void OnClearQty()
        {
            Qty_OK = Qty_NG = 0;
        }
 
        string numRecordFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Statistic.json");
        static object numLock = new object();
        private void SaveNumRecord()
        {
            lock (numLock)
            {
                using (StreamWriter writer = new StreamWriter(numRecordFile, false, System.Text.Encoding.UTF8))
                {
                    string dataStr = JsonConvert.SerializeObject(new { Qty_OK, Qty_NG });
                    writer.Write(dataStr);
                    writer.Flush();
                }
            }
        }
 
        private void ReadNumRecords()
        {
            if (File.Exists(numRecordFile))
            {
                lock (numLock)
                {
                    using (StreamReader reader = new StreamReader(numRecordFile, System.Text.Encoding.UTF8))
                    {
                        string dataStr = reader.ReadToEnd();
 
                        JObject data = JsonConvert.DeserializeObject<JObject>(dataStr);
 
                        if (data != null)
                        {
                            Qty_OK = data.Value<int>("Qty_OK");
                            Qty_NG = data.Value<int>("Qty_NG");
                        }
                    }
                }
            }
        }
 
        public override void Cleanup()
        {
            SaveNumRecord();
            base.Cleanup();
        }
    }
 
    public enum RunState
    {
        Stop,
        Running,
        Idle,
        Down,
    }
}