1
schangxiang@126.com
2024-12-09 2ef8eda1ea4ef302d86dff34d722da0cce950eff
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
 
 
using iWareSda.Common;
 
using iWareModel;
using System;
using System.Collections.Generic;
using System.Threading;
using iWareCommon.Utils;
using iWareCommon.Common.Globle;
using iWareCommon;
using iWareSda.Model;
 
 
namespace iWareSda
{
    /// <summary>
    /// 每台堆垛机的实时信息
    /// </summary>
    public class SrmEntity : IDevice
    {
        public SrmEntity()
        {
            this.View = new SrmView();
        }
 
        #region OPC相关
 
        /// <summary>
        /// 写的DB块
        /// </summary>
        public SrmDBForWrite DBBlockForWrite { get; set; }
 
        /// <summary>
        /// 读的DB块
        /// </summary>
        public SrmDBForRead DBBlockForRead { get; set; }
 
        /// <summary>
        /// 读取PLC实例
        /// </summary>
        public PLCService plcService { get; set; }
 
        /// <summary>
        /// 视图对象
        /// </summary>
        public SrmView View { get; set; }
 
 
        #endregion
 
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData()
        {
 
 
        }
        public SrmEntity(string srmName, int deviceId, SrmDBForWrite _dbBlockForWrite, SrmDBForRead _dbBlockForRead, PLCService _plcService)
        {
            this.View = new SrmView();
            this.View.DeviceName = srmName;
            this.View.DeviceId = deviceId;
            plcService = _plcService;
            DBBlockForWrite = _dbBlockForWrite;
            DBBlockForRead = _dbBlockForRead;
        }
 
 
        #region 个性化方法
 
        /// <summary>
        /// 是否允许给堆垛机发送新任务
        /// </summary>
        /// <param name="srm"></param>
        /// <returns></returns>
        public bool IsAllowSendNewTaskToSrm()
        {
            Thread.Sleep(2000);//等待一秒钟 【EditBy shaocx,2022-03-26】
            if (this.View.ModeName == "自动" && this.View.R_AlarmCode == 0 && (this.View.R_WarningDBList == null || this.View.R_WarningDBList.Count == 0)
                && this.View.StateName == "空闲")
            {
                return true;
            }
            return false;
        }
 
 
        /// <summary>
        /// 重新格式化处理站点号
        /// </summary>
        /// <param name="sourcePlace"></param>
        /// <param name="toPlace"></param>
        public void ReSubStringPlace(ref string sourcePlace, ref string toPlace)
        {//1-01-03-04 转换为 01-03-04
            sourcePlace = sourcePlace.Substring(2, sourcePlace.Length - 2);
            toPlace = toPlace.Substring(2, toPlace.Length - 2);
        }
 
        /// <summary>
        /// 给堆垛机发送指令
        /// </summary>
        /// <param name="deviceId">堆垛机设备号</param>
        /// <param name="taskId">任务号</param>
        /// <param name="sourcePlace">起始位</param>
        /// <param name="toPlace">目标位</param>
        /// <param name="command">任务类型</param>
        /// <param name="msg">错误消息</param>
        /// <returns></returns>
        public bool SendTask(ESrmCmd srmCmd, int deviceId, int taskId, string sourcePlace, string toPlace, out string msg)
        {
            try
            {
                msg = "";
                if (!CanDoTask(deviceId, out msg))
                {
                    return false;
                }
 
 
                //Z 排 X 列 Y层
                //01-02-03
                int sourceZ = 0, sourceX = 0, sourceY = 0;
                if (srmCmd == ESrmCmd.库内搬运 || srmCmd == ESrmCmd.入库 || srmCmd == ESrmCmd.出库)
                {
                    var arr_source = sourcePlace.Split('-');
                    //int sourceZ = int.Parse(sourcePlace.Substring(0, 2));
                    //int sourceX = int.Parse(sourcePlace.Substring(2, 4));
                    //int sourceY = int.Parse(sourcePlace.Substring(3, 2));
                    sourceZ = int.Parse(arr_source[0]);
                    sourceX = int.Parse(arr_source[1]);
                    sourceY = int.Parse(arr_source[2]);
                    ConvertPlaceToSrm(deviceId, ref srmCmd, true, sourcePlace, ref sourceZ, ref sourceX, ref sourceY);
                }
 
 
                var arr_to = toPlace.Split('-');
                //int toZ = int.Parse(toPlace.Substring(0, 1));
                //int toX = int.Parse(toPlace.Substring(1, 2));
                //int toY = int.Parse(toPlace.Substring(3, 2));
                int toZ = int.Parse(arr_to[0]);
                int toX = int.Parse(arr_to[1]);
                int toY = int.Parse(arr_to[2]);
                ConvertPlaceToSrm(deviceId, ref srmCmd, false, toPlace, ref toZ, ref toX, ref toY);
 
                //发送指令
                var ss = this.plcService;
 
                ////只有一台堆垛机,不需要写
                //var constValue = 1;
                //MessageModel b1 = this.plcService.WriteValuePoint( this.DBBlockForWrite.W_SrmNo, constValue, this.View.W_SrmNo);
                //if (!b1.result)
                //{
                //    msg = string.Format("向堆垛机{0}发送任务{1},写入设备编号" + constValue + "失败,返回结果:" + b1.resMsg, this.View.DeviceName, taskId);
                //    return false;
                //}
 
                MessageModel b2 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_TaskNo, taskId, this.View.W_TaskNo);
                if (!b2.result)
                {
                    msg = string.Format("向堆垛机{0}发送任务{1},写入任务号" + taskId + "失败,返回结果:" + b2.resMsg, this.View.DeviceName, taskId);
                    return false;
                }
 
                //MessageModel b60 = this.plcService.WriteValuePoint( this.DBBlockForWrite.W_PalletType, constValue, this.View.W_PalletType);
                //if (!b60.result)
                //{
                //    msg = string.Format("向堆垛机{0}发送任务{1},写入托盘类型" + constValue + "失败,返回结果:" + b60.resMsg, this.View.DeviceName, taskId);
                //    return false;
                //}
 
                if (srmCmd == ESrmCmd.库内搬运 || srmCmd == ESrmCmd.入库 || srmCmd == ESrmCmd.出库)
                {
                    MessageModel b5 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_SourcePosZ, sourceZ, this.View.W_SourcePosZ);
                    if (!b5.result)
                    {
                        msg = string.Format("向堆垛机{0}发送任务{1},写入sourceZ失败,返回结果:" + b5.resMsg, this.View.DeviceName, taskId);
                        return false;
                    }
 
                    MessageModel b4 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_SourcePosX, sourceX, this.View.W_SourcePosX);
                    if (!b4.result)
                    {
                        msg = string.Format("向堆垛机{0}发送任务{1},写入sourceX失败,返回结果:" + b4.resMsg, this.View.DeviceName, taskId);
                        return false;
                    }
                    MessageModel b3 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_SourcePosY, sourceY, this.View.W_SourcePosY);
                    if (!b3.result)
                    {
                        msg = string.Format("向堆垛机{0}发送任务{1},写入sourceY失败,返回结果:" + b3.resMsg, this.View.DeviceName, taskId);
                        return false;
                    }
                }
 
 
                MessageModel b8 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_DestinationPosZ, toZ, this.View.W_DestinationPosZ);
                if (!b8.result)
                {
                    msg = string.Format("向堆垛机{0}发送任务{1},写入toZ失败,返回结果:" + b8.resMsg, this.View.DeviceName, taskId);
                    return false;
                }
                MessageModel b7 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_DestinationPosX, toX, this.View.W_DestinationPosX);
                if (!b7.result)
                {
                    msg = string.Format("向堆垛机{0}发送任务{1},写入toX失败,返回结果:" + b7.resMsg, this.View.DeviceName, taskId);
                    return false;
                }
                MessageModel b6 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_DestinationPosY, toY, this.View.W_DestinationPosY);
                if (!b6.result)
                {
                    msg = string.Format("向堆垛机{0}发送任务{1},写入toY失败,返回结果:" + b6.resMsg, this.View.DeviceName, taskId);
                    return false;
                }
 
                short command = (short)srmCmd;
                MessageModel b10 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_Command, command, this.View.W_Command);
                if (!b10.result)
                {
                    msg = string.Format("向堆垛机{0}发送任务{1},写入command失败,返回结果:" + b10.resMsg, this.View.DeviceName, taskId);
                    return false;
                }
                MessageModel b9 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_Stb, 1, this.View.W_Stb);
                if (!b9.result)
                {
                    msg = string.Format("向堆垛机{0}发送任务{1},写入W_Stb失败,返回结果:" + b9.resMsg, this.View.DeviceName, taskId);
                    return false;
                }
 
                //重置如下状态
                //不需要重置 【EditBy shaocx,2022-04-09】
                //this.plcService.WriteValuePoint( this.DBBlockForWrite.W_EmergencyStop, 0, this.View.W_EmergencyStop);
                //this.plcService.WriteValuePoint( this.DBBlockForWrite.W_CancleAlarm, 0, this.View.W_CancleAlarm);
                //this.plcService.WriteValuePoint( this.DBBlockForWrite.W_TaskFinishConfirm, 0, this.View.W_TaskFinishConfirm);
                //this.plcService.WriteValuePoint( this.DBBlockForWrite.W_Ack, 0, this.View.W_Ack);
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                Log4NetHelper.WriteErrorLog(Srm_CacheEntity.curLogType, "发送任务出现异常:" + ex.Message, ex);
                return false;
            }
        }
 
 
        /// <summary>
        /// 堆垛机上的站点转换
        /// </summary>
        /// <param name="isSource"></param>
        /// <param name="place"></param>
        /// <param name="z"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        private void ConvertPlaceToSrm(int deviceId, ref ESrmCmd srmCmd, bool isSource, string place, ref int z, ref int x, ref int y)
        {
            if (deviceId == (int)EDevice.四号堆垛机)
            {
                if (place == "02-01-01")
                {//入口
                    x = 801;
                    y = 0;
                    z = 0;
 
                    if (isSource && srmCmd == ESrmCmd.库内搬运)
                    {//如果是起点是入库口
                        srmCmd = ESrmCmd.入库;
                    }
                }
                else if (place == "01-01-01")
                {//出口
                    x = 901;
                    y = 0;
                    z = 0;
 
                    if (isSource == false && srmCmd == ESrmCmd.库内搬运)
                    {//如果是目标点是出口
                        srmCmd = ESrmCmd.出库;
                    }
                }
            }
            else
            {
                if (place == "01-01-01")
                {//入口
                    x = 801;
                    y = 0;
                    z = 0;
 
                    if (isSource && srmCmd == ESrmCmd.库内搬运)
                    {//如果是起点是入库口
                        srmCmd = ESrmCmd.入库;
                    }
                }
                else if (place == "02-01-01")
                {//出口
                    x = 901;
                    y = 0;
                    z = 0;
 
                    if (isSource == false && srmCmd == ESrmCmd.库内搬运)
                    {//如果是目标点是出口
                        srmCmd = ESrmCmd.出库;
                    }
                }
            }
        }
 
 
        /// <summary>
        /// 任务删除
        /// </summary>
        /// <param name="srmNo"></param>
        /// <param name="taskId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendDelTask(int deviceId, out string msg)
        {
            try
            {
                msg = "";
 
                if (!CanDeleteTask(deviceId, out msg))
                {
                    return false;
                }
 
                MessageModel b3 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_Command, (int)ESrmCmd.任务删除, this.View.W_Command);
                if (!(b3.result))
                {
                    msg = string.Format("向堆垛机{0}发送删除任务失败,返回结果:" + b3.resMsg, this.View.DeviceName);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(Srm_CacheEntity.curLogType, "任务删除出现异常:" + ex.Message, ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// 堆垛机解警
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool ReleaseAlert(out string msg)
        {
            try
            {
                msg = "";
 
                MessageModel b1 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_CancleAlarm, 1, this.View.W_CancleAlarm);
 
                if (!(b1.result))
                {
                    msg = string.Format("向堆垛机{0}发送解警指令失败,返回结果:" + b1.resMsg, this.View.DeviceName);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(Srm_CacheEntity.curLogType, "发送解警指令异常:" + ex.Message, ex);
                throw ex;
            }
        }
 
 
        /// <summary>
        /// 堆垛机急停
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendEStop(out string msg)
        {
            try
            {
                msg = "";
 
                MessageModel b1 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_EmergencyStop, 1, this.View.W_EmergencyStop);
 
                if (!(b1.result))
                {
                    msg = string.Format("向堆垛机{0}发送急停指令失败,返回结果:" + b1.resMsg, this.View.DeviceName);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(Srm_CacheEntity.curLogType, "发送急停指令异常:" + ex.Message, ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// 堆垛机任务确认
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool ConfirmTaskFinish(out string msg)
        {
            try
            {
                msg = "";
 
                MessageModel b1 = this.plcService.WriteValuePoint(this.DBBlockForWrite.W_TaskFinishConfirm, 1, this.View.W_TaskFinishConfirm);
 
                if (!(b1.result))
                {
                    msg = string.Format("向堆垛机{0}发送任务确认指令失败,返回结果:" + b1.resMsg, this.View.DeviceName);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(Srm_CacheEntity.curLogType, "发送任务确认指令异常:" + ex.Message, ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// 堆垛机继续任务
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool ContinueTask(out string msg)
        {
            try
            {
                msg = "";
 
                //var dbNumber = Srm_CacheEntity.W_DBHeader;
                //FunctionReturnEntity b1 = this.S7.WriteValuePoint( this.DBBlock.con, 1);
 
                //if (!(b1.result))
                //{
                //    msg = string.Format("向堆垛机{0}发送解警指令失败,返回结果:" + b1.resMsg, this.DeviceName);
                //    return false;
                //}
                return true;
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(Srm_CacheEntity.curLogType, "堆垛机继续任务异常:" + ex.Message, ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// 是否可以下发任务
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CanDoTask(int deviceId, out string msg)
        {
            msg = "";
            //在不在线不需要校验,不在线的话
            if (this.View.R_Mode != 1)
            {
                msg = string.Format("堆垛机{0}非自动模式", this.View.DeviceName);
                return false;
            }
 
            if (this.View.R_Alarm == 1)
            {
                msg = string.Format("堆垛机{0}报警中", this.View.DeviceName);
                return false;
            }
 
            if (this.View.R_LiftFull == 1)
            {
                msg = string.Format("堆垛机{0}叉齿有货", this.View.DeviceName);
                return false;
            }
 
            if (this.View.R_PosZ != 0)
            {
                msg = string.Format("堆垛机{0}叉齿不在原点", this.View.DeviceName);
                return false;
            }
            //*/
 
            if (this.View.R_State != (int)ESrmState.空闲)
            {
                msg = string.Format("堆垛机{0}非空闲", this.View.DeviceName);
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 是否可以删除任务
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private bool CanDeleteTask(int deviceId, out string msg)
        {
            msg = "";
            //在不在线不需要校验,不在线的话
            if (this.View.R_Mode != 1)
            {
                msg = string.Format("堆垛机{0}非自动模式", this.View.DeviceName);
                return false;
            }
            return true;
        }
 
 
        #endregion
 
        public void SetPropertyValueForRead()
        {
            var r_dbBlock = this.DBBlockForRead;
            SdaHelper.SetPropertyValueForDB<SrmDBForRead, SrmView>(r_dbBlock, this.View, this.plcService, "");
        }
 
 
        public void SetPropertyValueForWrite()
        {
            var w_dbBlock = this.DBBlockForWrite;
            SdaHelper.SetPropertyValueForDB<SrmDBForWrite, SrmView>(w_dbBlock, this.View, this.plcService, "");
        }
 
 
 
        /// <summary>
        /// 是否有心跳
        /// </summary>
        /// <returns></returns>
        public bool IsHaveHeatBeat()
        {
            try
            {
                var r_dbBlock = this.DBBlockForRead;
                short value = Convert.ToInt16(this.plcService.ReadValuePoint(r_dbBlock.R_HandShake, typeof(short)));
                //约定 等待2秒
                Thread.Sleep(2000);
                short value_next = Convert.ToInt16(this.plcService.ReadValuePoint(r_dbBlock.R_HandShake, typeof(short)));
                if (value == value_next)
                {//没有心跳
                    return false;
                }
                else
                {//有心跳
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
 
 
        /// <summary>
        /// 组合 起始点
        /// </summary>
        /// <returns></returns>
        public string GetSrmSourcePlace()
        {
            if (this.View.W_SourcePosZ != 0)
            {
                return this.View.W_SourcePosZ.ToString("00") + "-" + this.View.W_SourcePosX.ToString("00") + "-" + this.View.W_SourcePosY.ToString("00");
            }
 
            return GetPlaceForInOutStation(this.View.W_SourcePosX);
        }
        /// <summary>
        /// 组合 目标点
        /// </summary>
        /// <returns></returns>
        public string GetSrmToPlace()
        {
            if (this.View.W_DestinationPosZ != 0)
            {
                return this.View.W_DestinationPosZ.ToString("00") + "-" + this.View.W_DestinationPosX.ToString("00") + "-" + this.View.W_DestinationPosY.ToString("00");
            }
            return GetPlaceForInOutStation(this.View.W_DestinationPosX);
        }
 
        /// <summary>
        /// 单独获取出入口站点名
        /// </summary>
        /// <param name="xVaue"></param>
        /// <returns></returns>
        private string GetPlaceForInOutStation(short xVaue)
        {
            if (xVaue == 801)
            {
                if (this.View.DeviceId == (int)EDevice.四号堆垛机)
                {
                    return "02-01-01";
                }
                else
                {
                    return "01-01-01";
                }
            }
            else if (xVaue == 901)
            {
                if (this.View.DeviceId == (int)EDevice.四号堆垛机)
                {
                    return "01-01-01";
                }
                else
                {
                    return "02-01-01";
                }
            }
            return "";
        }
 
 
        #region 获取当前报警信息
 
 
 
        
 
 
        #endregion
    }
}