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
using Autofac;
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using Bro.Device.AuboRobot;
using Bro.Device.SeerAGV;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading;
 
namespace A032.Process
{
    public enum TaskStatus
    {
        Available = 1,
        Running = 2,
        Warning = 3,
    }
 
    public enum TaskAvailableLevel
    {
        Robot = 1,
        AGV = 2,
        Both = 3,
    }
 
    public class AGVTaskModel
    {
        public TaskAvailableLevel Level { get; set; } = TaskAvailableLevel.Robot;
 
        public string MethodName { get; set; }
 
        public IOperationConfig OpConfig { get; set; }
 
        public IDevice Device { get; set; }
 
        //public int Priority { get; set; } = 10;
 
        public AGVTaskModel() { }
 
        public AGVTaskModel(TaskAvailableLevel level, string methodName, IOperationConfig opConfig = null, IDevice device = null)
        {
            Level = level;
            MethodName = methodName;
            OpConfig = opConfig ?? new OperationConfigBase();
            Device = device;
        }
    }
 
    public class AGVBindUnit : IComplexDisplay
    {
        #region 可编辑项目
        [Category("设备绑定")]
        [Description("绑定Id")]
        [Browsable(false)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
 
        [Category("设备绑定")]
        [Description("绑定AGV")]
        [TypeConverter(typeof(AGVDeviceConverter))]
        public string AGVId { get; set; }
 
        [Category("设备绑定")]
        [Description("绑定Robot")]
        [TypeConverter(typeof(RobotDeviceConverter))]
        public string RobotId { get; set; }
 
        [Category("设备绑定")]
        [Description("绑定Camera")]
        [TypeConverter(typeof(CameraDeviceConverter))]
        public string CameraId { get; set; }
 
        public string GetDisplayText()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                ProcessConfig config = scope.Resolve<ProcessConfig>();
 
                string agv = config.AGVConfigCollection.FirstOrDefault(u => u.ID == AGVId)?.Name ?? "未绑定AGV";
                string robot = config.RobotConfigCollection.FirstOrDefault(u => u.ID == RobotId)?.Name ?? "未绑定机器人";
                string camera = config.CameraConfigCollection.FirstOrDefault(u => u.ID == CameraId)?.Name ?? "未绑定相机";
 
                return agv + "-" + robot + "-" + camera;
            }
        }
        #endregion
 
        [Browsable(false)]
        [JsonIgnore]
        public Action<AGVTaskModel> OnMethodInvoke { get; set; }
 
        public string AGVDest { get; set; } = "";
 
        private TaskStatus agvStatus = TaskStatus.Available;
        [Browsable(false)]
        [JsonIgnore]
        public TaskStatus AGVStatus
        {
            get => agvStatus;
            set
            {
                agvStatus = value;
                //InvokeTaskCheck();
            }
        }
 
        private TaskStatus robotStatus = TaskStatus.Available;
        [Browsable(false)]
        [JsonIgnore]
        public TaskStatus RobotStatus
        {
            get => robotStatus;
            set
            {
                robotStatus = value;
                //InvokeTaskCheck();
            }
        }
 
        [Browsable(false)]
        [JsonIgnore]
        public TaskStatus UnitStatus
        {
            get
            {
                if (AGVStatus == TaskStatus.Warning || RobotStatus == TaskStatus.Warning)
                {
                    return TaskStatus.Warning;
                }
                else
                {
                    if (AGVStatus == TaskStatus.Available && RobotStatus == TaskStatus.Available)
                    {
                        return TaskStatus.Available;
                    }
                }
 
                return TaskStatus.Running;
            }
        }
 
        [Browsable(false)]
        [JsonIgnore]
        public SeerAGVDriver AGV { get; set; } = null;
        [Browsable(false)]
        [JsonIgnore]
        public AuboRobotDriver Robot { get; set; } = null;
        [Browsable(false)]
        [JsonIgnore]
        public CameraBase Camera { get; set; } = null;
 
        //[Browsable(false)]
        //[JsonIgnore]
        //public ObservableCollection<AGVTaskModel> TaskList { get; set; } = new ObservableCollection<AGVTaskModel>();
 
        [Browsable(false)]
        [JsonIgnore]
        public int CurrentEmptyTray { get; set; }
        [Browsable(false)]
        [JsonIgnore]
        public int CurrentFullTray { get; set; }
        [Browsable(false)]
        [JsonIgnore]
        public bool IsFullTrayFull { get; set; }
        [Browsable(false)]
        [JsonIgnore]
        public bool IsFullTrayEmpty { get; set; }
        [Browsable(false)]
        [JsonIgnore]
        public bool IsEmptyTrayEmpty { get; set; }
 
        [Browsable(false)]
        [JsonIgnore]
        public bool IsFullTrayTaskAssigned { get; set; }
 
        [Browsable(false)]
        [JsonIgnore]
        public bool IsEmptyTrayTaskAssigned { get; set; }
 
        //[Browsable(false)]
        //[JsonIgnore]
        //public ManualResetEvent FullTrayFullHandle { get; set; } = new ManualResetEvent(false);
 
        //[Browsable(false)]
        //[JsonIgnore]
        //public ManualResetEvent FullTrayEmptyHandle { get; set; } = new ManualResetEvent(false);
 
        public ManualResetEvent RobotIOHandle { get; set; } = new ManualResetEvent(false);
 
        public AGVBindUnit()
        {
            //TaskList.CollectionChanged += TaskList_CollectionChanged;
        }
 
        object agvStatusLock = new object();
        public bool SetAGVStatus(TaskStatus status)
        {
            lock (agvStatusLock)
            {
                switch (status)
                {
                    case TaskStatus.Available:
                        break;
                    case TaskStatus.Running:
                        if (AGVStatus == TaskStatus.Available)
                        {
                            AGVStatus = status;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    case TaskStatus.Warning:
                        break;
                    default:
                        break;
                }
 
                AGVStatus = status;
                return true;
            }
        }
        //private void TaskList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        //{
        //    if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        //    {
        //        InvokeTaskCheck();
        //    }
        //}
 
        //private void InvokeTaskCheck()
        //{
        //    lock (taskLock)
        //    {
        //        for (int i = 0; i < TaskList.Count; i++)
        //        {
        //            var task = TaskList[i];
 
        //            bool isAgvOK = false;
        //            bool isRobotOK = false;
 
        //            if ((task.Level & TaskAvailableLevel.AGV) == TaskAvailableLevel.AGV)
        //            {
        //                isAgvOK = AGVStatus == TaskStatus.Available;
        //            }
        //            else
        //            {
        //                isAgvOK = true;
        //            }
 
        //            if ((task.Level & TaskAvailableLevel.Robot) == TaskAvailableLevel.Robot)
        //            {
        //                isRobotOK = RobotStatus == TaskStatus.Available;
        //            }
        //            else
        //            {
        //                isRobotOK = true;
        //            }
 
        //            if (isRobotOK && isAgvOK)
        //            {
        //                OnMethodInvoke?.Invoke(TaskList[i]);
        //                TaskList.RemoveAt(i);
        //                break;
        //            }
        //        }
        //    }
        //}
 
        //object taskLock = new object();
        //public void AddTask(AGVTaskModel task)
        //{
        //    lock (taskLock)
        //    {
        //        TaskList.Add(task);
        //    }
        //}
 
        //public void ClearTask()
        //{
        //    lock (taskLock)
        //    {
        //        TaskList.Clear();
        //    }
        //}
 
        //public AGVTaskModel GetTask(int index)
        //{
        //    lock (taskLock)
        //    {
        //        return TaskList[index];
        //    }
        //}
    }
 
    public class AGVDeviceConverter : ComboBoxItemTypeConvert
    {
        public override void GetConvertHash()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                ProcessConfig config = scope.Resolve<ProcessConfig>();
 
                config.AGVConfigCollection.ForEach(plc =>
                {
                    _hash[plc.ID] = plc.Name;
                });
            }
        }
    }
 
    public class RobotDeviceConverter : ComboBoxItemTypeConvert
    {
        public override void GetConvertHash()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                ProcessConfig config = scope.Resolve<ProcessConfig>();
 
                config.RobotConfigCollection.ForEach(robot =>
                {
                    _hash[robot.ID] = robot.Name;
                });
            }
        }
    }
 
    public class CameraDeviceConverter : ComboBoxItemTypeConvert
    {
        public override void GetConvertHash()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                ProcessConfig config = scope.Resolve<ProcessConfig>();
 
                config.CameraConfigCollection.ForEach(camera =>
                {
                    _hash[camera.ID] = camera.Name;
                });
            }
        }
    }
 
    public class PLCDeviceConverter : ComboBoxItemTypeConvert
    {
        public override void GetConvertHash()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                _hash[""] = "未指定";
 
                ProcessConfig config = scope.Resolve<ProcessConfig>();
 
                config.PLCConfigCollection.ForEach(plc =>
                {
                    _hash[plc.ID] = plc.Name;
                });
            }
        }
    }
}