领胜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
using Bro.Common.Factory;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.UI.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Media;
 
namespace Bro.UI.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        #region IDialogService
        public IDialogCoordinator Dialog
        {
            get
            {
                return SimpleIoc.Default.GetInstance<IDialogCoordinator>("Main");
            }
        }
        #endregion
 
        #region Login
        private string _welcomeTitle = string.Empty;
        public string WelcomeTitle
        {
            get
            {
                return _welcomeTitle;
            }
            set
            {
                Set(ref _welcomeTitle, value);
            }
        }
 
        private string userName = "";
        public string UserName
        {
            get => userName;
            set => Set(ref userName, value);
        }
        #endregion
 
        public MainViewModel(LoginInfo info)
        {
            InitialCmmd();
 
            Initial();
 
            if (!string.IsNullOrWhiteSpace(info.ProductionCode))
            {
                CurrentCode = info.ProductionCode;
            }
 
            if (!string.IsNullOrWhiteSpace(info.UserName))
            {
                UserName = info.UserName;
            }
 
            LogList.CollectionChanged += LogList_CollectionChanged;
        }
 
        private void Initial()
        {
            WelcomeTitle = ConfigurationManager.AppSettings["Title"];
 
            if (string.IsNullOrWhiteSpace(WelcomeTitle))
            {
                WelcomeTitle = "T047";
            }
 
            WelcomeTitle += " V" + Assembly.GetExecutingAssembly().GetName().Version.ToString();
 
            CodesSource = SettingHelper.GetProductionCodes();
 
            //if (CodesSource == null || CodesSource.Count == 0)
            //{
            //    CodesSource = new List<string>() { "Default" };
            //}
 
            //CurrentCode = CodesSource[0];
        }
 
        #region ProductionCode
        private List<string> codeSource = new List<string>();
        public List<string> CodesSource
        {
            get => codeSource;
            set => Set(ref codeSource, value);
        }
 
        private string currentCode = "";
        public string CurrentCode
        {
            get => currentCode;
            set
            {
                if (currentCode != value)
                {
                    Set(ref currentCode, value);
                    LoadProdution(value);
                    //ProcessOperation(LoadProdution, "Loading", "Load Failed");
                }
            }
        }
 
        private bool isProcessStopped = true;
        public bool IsProcessStopped
        {
            get => isProcessStopped;
            set => Set(ref isProcessStopped, value);
        }
        #endregion
 
        #region Process
        public IProcess ProcessControl { get; set; }
 
        string _processCode = "";
 
        private List<IDevice> deviceList = null;
        public List<IDevice> DeviceList
        {
            get => deviceList;
            set => Set(ref deviceList, value);
        }
 
        private void LoadProcessCode()
        {
            var systemProcessCodes = ProcessFactory.GetProcessCodes();
            var avaiableProcessCodes = SettingHelper.GetProcessCodes();
 
            List<string> pCodes = new List<string>();
 
            if (avaiableProcessCodes.Count > 0)
            {
                pCodes = avaiableProcessCodes.Intersect(systemProcessCodes).ToList();
            }
            else
            {
                pCodes = systemProcessCodes;
            }
 
            if (!(pCodes.Count == 1 && pCodes[0] == ""))
            {
                pCodes.Remove("");
            }
 
            _processCode = pCodes[0];
        }
 
        private void LoadProdution(string productionCode)
        {
            LoadProcessCode();
            if (productionCode == "Default")
            {
                productionCode = "";
            }
 
            ProcessControl = ProcessFactory.CreateStationProcess(_processCode, productionCode, out string msg);
 
            if (!string.IsNullOrWhiteSpace(msg))
            {
                throw new ProcessException($"Process创建失败,{msg}", null, ExceptionLevel.Fatal);
            }
 
            ProcessControl.InitialProcess();
 
            //SimpleIoc.Default.Unregister<ProcessControl>("ProcessControl");
            //SimpleIoc.Default.Register(() => ProcessControl, "ProcessControl", true);
 
            SimpleIoc.Default.Unregister<RunningViewModel>();
            SimpleIoc.Default.Register<RunningViewModel>(() =>
            {
                RunningViewModel vm = new RunningViewModel(ProcessControl);
                return vm;
            }, true);
 
            SimpleIoc.Default.Unregister<InformationViewModel>();
            SimpleIoc.Default.Register<InformationViewModel>(() =>
            {
                InformationViewModel vm = new InformationViewModel(ProcessControl);
                return vm;
            }, true);
 
            Messenger.Default.Send<string>("", "ProcessDone");
 
            ProcessControl.OnProcessStateChanged -= OnProcessStateChanged;
            ProcessControl.OnProcessStateChanged += OnProcessStateChanged;
 
            ProcessControl.OnLog -= OnProcessLog;
            ProcessControl.OnLog += OnProcessLog;
 
            //using (var scope = GlobalVar.Container.BeginLifetimeScope())
            //{
            //    DeviceList = scope.Resolve<List<IDevice>>();
            //}
 
            DeviceList = ProcessControl.DeviceCollection;
 
            LogList.Clear();
        }
 
        private void LoadProdution()
        {
            LoadProdution(CurrentCode);
        }
 
        private void OnProcessStateChanged(EnumHelper.DeviceState state)
        {
            IsProcessStopped = (state != EnumHelper.DeviceState.DSOpen);
        }
        #endregion
 
        #region Operation
        public RelayCommand Cmmd_Run { get; set; }
        public RelayCommand Cmmd_Stop { get; set; }
        public RelayCommand Cmmd_Load { get; set; }
 
        private void InitialCmmd()
        {
            Cmmd_Run = new RelayCommand(OnCmmdRun);
            Cmmd_Stop = new RelayCommand(OnCmmdStop);
            Cmmd_Load = new RelayCommand(OnCmmdLoad);
        }
 
        private async void OnCmmdLoad()
        {
            await ProcessOperation(ProcessControl.InitialProcess, "Loading", "Load Failed");
        }
 
        private async void OnCmmdStop()
        {
            await ProcessOperation(ProcessControl.Close, "Stopping", "Stop Failed");
        }
 
        //int i = 0;
        private async void OnCmmdRun()
        {
            await ProcessOperation(ProcessControl.Open, "Starting", "Start Failed");
 
            //SimpleIoc.Default.GetInstance<RunningViewModel>().Images[0].Shapes.Add(new Bro.Common.ImageCanvas.Shapes.ComplexPointIndicator()
            //{
            //    PointInfo = new Bro.Common.Model.ComplexPoint()
            //    {
            //        ImagePoint = new Bro.Common.Model.DirectionAidPoint() { X = 300 + i * 200, Y = 300 + i * 200 },
            //        PlatPoint = new Bro.Common.Model.DirectionAidPoint() { X = 300, Y = 200, IsAvailable = false, },
            //    },
            //    Angle = 30,
            //    Index = 10,
            //});
            //i++;
        }
 
        private async Task ProcessOperation(Action action, string desc, string errorHint)
        {
            var controller = await Dialog.ShowProgressAsync(this, desc, $"Process is {desc}...");
            controller.SetProgressBarForegroundBrush(new SolidColorBrush(Color.FromRgb(10, 22, 33)));
            controller.SetIndeterminate();
 
            await Task.Run(() =>
            {
                action.Invoke();
                //Thread.Sleep(1000);
            }).ContinueWith((t) =>
            {
                controller.CloseAsync();
                if (t.Exception != null)
                {
                    Dialog.ShowMessageAsync(this, errorHint, t.Exception.GetExceptionMessage());
                }
            });
        }
        #endregion
 
        #region Log
        private ObservableCollection<string> logList = new ObservableCollection<string>();
        public ObservableCollection<string> LogList
        {
            get => logList;
            set => Set(ref logList, value);
        }
 
        private void LogList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RaisePropertyChanged("LogList");
        }
 
        private async void OnProcessLog(DateTime logTime, string prefix, string msg)
        {
            await Task.Run(() =>
            {
                string log = logTime.ToString("HH:mm:ss.fff") + "\t" + prefix;
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    log += ("\r\n" + msg);
                }
 
                App.Current.Dispatcher.Invoke(() =>
                {
                    LogList.Add(log);
 
                    if (LogList.Count > 200)
                    {
                        //LogList = new ObservableCollection<string>(LogList.Skip(100));
                        //LogList.CollectionChanged += LogList_CollectionChanged;
                        int i = 100;
                        while (i > 0)
                        {
                            LogList.RemoveAt(0);
                            i--;
                        }
                    }
                });
            });
        }
        #endregion
 
        public override void Cleanup()
        {
            base.Cleanup();
 
            if (ProcessControl.ProcessState == EnumHelper.DeviceState.DSOpen)
            {
                ProcessControl.Close();
            }
        }
    }
}