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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System.Collections.Generic;
using System.ComponentModel;
using VirtualConveyor.Cache.Entity;
using VirtualConveyor.Common.Entity;
using VirtualConveyor.Conveyor.EnumType;
using System;
using System.Linq;
 
namespace VirtualConveyor.Conveyor.Entity
{
   public class GateEntity: BindableBase
   {
 
       /// <summary>
       /// 拥有更多该输送点的输送机
       /// </summary>
       public ConveyorEntity Conveyor { get; private set; }
 
       /// <summary>
       /// 端口名称
       /// </summary>
       public string Name { get; private set; }
 
 
       /// <summary>
       /// 4允许放货
       /// </summary>
       private bool wPutDownLicense = true;
 
       public bool WPutDownLicense
       {
           get { return wPutDownLicense; }
           set { SetProperty(ref wPutDownLicense, value); }
       }
 
       /// <summary>
       /// 4允许取货
       /// </summary>
       private bool wPickUpLicense = true;
 
       public bool WPickUpLicense
       {
           get { return wPickUpLicense; }
           set { SetProperty(ref wPickUpLicense, value); }
       }
              
       /// <summary>
       /// 到位信号
       /// </summary>
       private bool wOccupied = true;
 
       public bool WOccupied
       {
           get { return wOccupied; }
           set { SetProperty(ref wOccupied, value); }
       }
 
       /// <summary>
       /// 7滚动开始
       /// </summary>
       private bool wStartRotation = true;
 
       public bool WStartRotation
       {
           get { return wStartRotation; }
           set { SetProperty(ref wStartRotation, value); }
       }
 
       /// <summary>
       /// 8手动允许取货(输送机空闲)
       /// </summary>
       private bool wPickUpLicenseManual = true;
 
       public bool WPickUpLicenseManual
       {
           get { return wPickUpLicenseManual; }
           set { SetProperty(ref wPickUpLicenseManual, value); }
       }
 
       /// <summary>
       /// 9滚料完成
       /// </summary>
       private bool wLayingOffComplete = true;
 
       public bool WLayingOffComplete
       {
           get { return wLayingOffComplete; }
           set { SetProperty(ref wLayingOffComplete, value); }
       }
 
 
       /// <summary>
       /// 10人工抽样结果
       /// </summary>
       private int wCheckResult = -1;
 
       public int WCheckResult
       {
           get { return wCheckResult; }
           set { SetProperty(ref wCheckResult, value); }
       }
 
       /// <summary>
       /// 任务号
       /// </summary>
       private string wtaskId;
 
       public string WTaskId
       {
           get { return wtaskId; }
           set { SetProperty(ref wtaskId, value); }
       }
       /// <summary>
       /// 任务号
       /// </summary>
       private string taskId;
 
       public string TaskId
       {
           get { return taskId; }
           set { SetProperty(ref taskId, value); }
       }
 
   
       /// <summary>
       /// 5放货完成
       /// </summary>
       private bool putSymbol;
 
       public bool PutSymbol
       {
           get { return putSymbol; }
           set { SetProperty(ref putSymbol, value); }
       }
 
       /// <summary>
       /// 6取货完成
       /// </summary>
       private bool pickSymbol;
 
       public bool PickSymbol
       {
           get { return pickSymbol; }
           set { SetProperty(ref pickSymbol, value); }
       }
 
       /// <summary>
       /// 7手动放货完成
       /// </summary>
       private bool putSymbolManual;
 
       public bool PutSymbolManual
       {
           get { return putSymbolManual; }
           set { SetProperty(ref putSymbolManual, value); }
       }
 
 
       /// <summary>
       /// WCS需要写入的OPC地址
       /// </summary>
       public OpcWriteItem OpcWriteItems { get; set; }
 
       /// <summary>
       /// WCS需要读取的OPC地址
       /// </summary>
       public OpcReadItem OpcReadItems { get; set; }
 
 
       private bool flag = false;
       
       /// <summary>
       /// 构造函数
       /// </summary>
       /// <param name="conveyor">调用该货口的输送机</param>
       public GateEntity(ConveyorEntity conveyor,string name) 
       {
           this.Conveyor = conveyor;
           this.Name = name;
           switch(name)
           {
               //输送机Conveyor1
               //起始点
               case "gate11":
                   OpcReadItems = new OpcReadItem
                   {
 
                       TaskId = "S7:[@LOCALSERVER]DB1,STRING164.254", //任务号                  
                       PutSymbol = "S7:[@LOCALSERVER]DB1,X682.0",//放货完成
                       PutSymbolManual = "S7:[@LOCALSERVER]DB1,X682.2"//手动放货完成
                   };
                   OpcWriteItems = new OpcWriteItem
                   {
                       PutDownLicense = "S7:[@LOCALSERVER]DB1,X684.0",//允许放货
                       Occupied = "S7:[@LOCALSERVER]DB1,X684.1",//到位信号
                       StartRotation = "S7:[@LOCALSERVER]DB1,X688.0",//滚动开始
                       PickUpLicenseManual = "S7:[@LOCALSERVER]DB1,X688.1"//允许手动放货
                   };
                   break;
                 
               //目标点立库入库口
               case "gate12":
                   OpcReadItems = new OpcReadItem
                   {
                       PickSymbol = "S7:[@LOCALSERVER]DB1,X952.1"//取货完成
                   };
                   OpcWriteItems = new OpcWriteItem
                   {
                       TaskId = "S7:[@LOCALSERVER]DB1,STRING164.254", //任务号
                       PickUpLicense = "S7:[@LOCALSERVER]DB1,X1466.0",//允许取货
                       Occupied = "S7:[@LOCALSERVER]DB1,X1466.1",//到位信号
                       CheckResult = "S7:[@LOCALSERVER]DB1,INT1988"//人工检验结果
                   };
                   break;
 
               //输送机conveyor2
               //起始点立库出库口
               case "gate21":
                   OpcReadItems = new OpcReadItem
                   {
                       TaskId = "S7:[@LOCALSERVER]DB1,STRING1990.254", //任务号                  
                       PutSymbol = "S7:[@LOCALSERVER]DB1,X2250.0",//放货完成 S7:[@LOCALSERVER]DB1,X2250.1
                   };
                   OpcWriteItems = new OpcWriteItem
                   {
                       PutDownLicense = "S7:[@LOCALSERVER]DB1,X2508.1",//允许放货
                       Occupied = "S7:[@LOCALSERVER]DB1,X2508.0",//到位信号
                       StartRotation = "S7:[@LOCALSERVER]DB1,X2508.2",//滚动开始
                   };
                   break;
 
               //目标点
               case "gate22":
                   OpcReadItems = new OpcReadItem
                   {
                       PickSymbol = "S7:[@LOCALSERVER]DB1,X2766.0"//取货完成
                   };
                   OpcWriteItems = new OpcWriteItem
                   {
                       TaskId = "S7:[@LOCALSERVER]DB1,STRING1990.254", //任务号
                       PickUpLicense = "S7:[@LOCALSERVER]DB1,X3018.1",//允许取货
                       Occupied = "S7:[@LOCALSERVER]DB1,X3018.0",//到位信号
                   };
                   break;
               
           }
           
           this.PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);
 
           var items = new List<string>();
           var values = new List<object>();
 
           if (!string.IsNullOrEmpty(this.OpcReadItems.TaskId)) { items.Add(this.OpcReadItems.TaskId); values.Add(0); }
           if (!string.IsNullOrEmpty(this.OpcReadItems.PutSymbol)) { items.Add(this.OpcReadItems.PutSymbol); values.Add(false); }
           if (!string.IsNullOrEmpty(this.OpcReadItems.PickSymbol)) { items.Add(this.OpcReadItems.PickSymbol); values.Add(false); }
           if (!string.IsNullOrEmpty(this.OpcReadItems.PutSymbolManual)) { items.Add(this.OpcReadItems.PutSymbolManual); values.Add(false); }
 
           CacheEntity.OpcWcfServiceClient.WriteValues(items.ToArray(), values.ToArray());
 
       }
 
       /// <summary>
       /// GateEntity实体属性变化时触发的事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
       {
           switch (e.PropertyName)
           {
 
               case "WPickUpLicense":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.PickUpLicense))
                   {
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.PickUpLicense, this.WPickUpLicense);
                   }
                   break;
               case "WPutDownLicense":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.PutDownLicense))
                   {
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.PutDownLicense, this.WPutDownLicense);
                   }
                   break;
               case "WOccupied":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.Occupied))
                   {
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.Occupied, this.WOccupied);
                       if(WOccupied)
                       {
                           if(!string.IsNullOrEmpty(this.OpcReadItems.PickSymbol))
                           {
                               CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcReadItems.PickSymbol,false);
                           }
                       }
 
                   }
                   break;
               case "WTaskId":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.TaskId))
                   {
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.TaskId, this.TaskId);
                   }
                   break;
 
               case "WStartRotation":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.StartRotation))
                   { 
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.StartRotation, this.WStartRotation); 
                   }
                   break;
               case "WPickUpLicenseManual":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.PickUpLicenseManual))
                   { 
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.PickUpLicenseManual, this.WPickUpLicenseManual); 
                   }
                   break;
               case "WLayingOffComplete":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.LayingOffComplete))
                   {
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.LayingOffComplete, this.WLayingOffComplete);
                   }
                   break;
               case "WCheckResult":
                   if (!string.IsNullOrEmpty(this.OpcWriteItems.CheckResult))
                   {
                       CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.CheckResult, this.WCheckResult);
                   }
                   break;
           }
       }
 
       /// <summary>
       /// 接收任务
       /// </summary>
       /// <returns></returns>
       public bool ReceiveTask(out string msg)
       {
           msg = "";
           var toPort = "";
 
           switch(Name)
           {
               case "gate11":
                   toPort = "gate12";
                   break;
               case "gate21":
                   toPort = "gate22";
                   break;
           }
           var task = new TaskEntity
           {
 
               TaskId = TaskId,
               FromPort = Name,
               ToPort = toPort,
               Status = (int)ETaskStatus.未开始
           };
 
           if(!string.IsNullOrEmpty(OpcWriteItems.StartRotation))
           {
               WStartRotation = false;
           }
           Conveyor.Tasks.Add(task);
           
           return true;
       }
 
 
       public bool StartTask(out string msg)
       {
           msg = "";
 
           if (!PutSymbol || !PutSymbolManual)
           {
               msg = string.Format("端口{0}没有手动开始标志", Name);
               return false;
           }
 
           var task = Conveyor.Tasks.FirstOrDefault(x => x.FromPort == Name && x.Status == (int)ETaskStatus.未开始) ;
 
           if (task == null)
           {
               msg = string.Format("端口{0}没有找到未开始的任务", Name);
               return false;
           }
 
           task.Status = (int)ETaskStatus.执行中;
 
           if (!string.IsNullOrEmpty(OpcReadItems.PutSymbol))
           {
               CacheEntity.OpcWcfServiceClient.WriteValue(OpcReadItems.PutSymbol, false);
           }
 
           if (!string.IsNullOrEmpty(OpcReadItems.PutSymbolManual))
           {
               CacheEntity.OpcWcfServiceClient.WriteValue(OpcReadItems.PutSymbolManual, false);
           }
 
           PutSymbol = false;
           PutSymbolManual = false;
 
           flag = false;
 
           return true;
       }
 
 
       public bool FinishTask(out string msg)
       {
           msg = "";
 
           if (!WOccupied)
           {
               msg = string.Format("端口{0}没有到位", Name);
               return false;
           }
 
           var name = Name;
 
           switch(Name)
           {
               case "gate11":
                   name = "gate12";
                   break;
               case "gate21":
                   name = "gate22";
                   break;
           }
 
           var task = Conveyor.Tasks.FirstOrDefault(x => x.ToPort == name && x.Status == (int)ETaskStatus.执行中);
 
           if (task == null)
           {
               msg = string.Format("端口{0}没有找到待完成的任务", name);
               return false;
           }
 
           task.Status = (int)ETaskStatus.已结束;
 
           return true;
       }
    }
}