LineBean.cs 38.0 KB
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 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 流水线自动料仓-流水线类
    /// </summary>
    public partial class LineBean : KTK_Store
    {
      
        /// <summary>
        /// 是否没有扫到码
        /// </summary>
        public bool isNotScanCode = false;

        /// <summary>
        /// 灯闪烁定时器
        /// </summary>
        private System.Timers.Timer ledProcessTimer = null; 
        /// <summary>
        /// 分出一个线程,专门处理急停,报警,气压检测,工单信号检测等处理
        /// </summary>
        private System.Timers.Timer IoCheckTimer = null;
     
        /// <summary>
        /// 流水线下所有的移载装置
        /// </summary>
        public Dictionary<int, MoveEquip> MoveEquipMap { get; set; }

        public Dictionary <int,FeedingEquip> FeedingEquipMap { get; set; }
        public Dictionary<int, ProvidingEquip> ProvidingEquipMap { get; set; }

        /// <summary>
        /// 流水线配置
        /// </summary>
        public Line_Config Config { get; set; }


        private bool canStart = false;
        public LineBean(Line_Config lineConfig, Dictionary<int, MoveEquip_Config> configList,Dictionary<int,FeedingEquip_Config> feedMap,Dictionary<int,ProvidingEquip_Config> providMap)
        {
            if (lineConfig.IOSingle_TimerOut <= 0)
            {
                lineConfig.IOSingle_TimerOut = 5000;
            }
            Init();
            InitTimer();
            this.Config = lineConfig;
            this.DeviceID = lineConfig.Id;
            SW12_MoveInfo = new LineMoveInfo(DeviceID,"横移轨道-12");
            SW34_MoveInfo = new LineMoveInfo(DeviceID,"横移轨道-34");
            MoveInfo = new LineMoveInfo(DeviceID, "流水线-Move ");
            Name = (" 流水线_" + Config.CID + " ").ToUpper();


            MoveEquipMap = new Dictionary<int, MoveEquip>();
            FeedingEquipMap = new Dictionary<int, FeedingEquip>();
            ProvidingEquipMap = new Dictionary<int, ProvidingEquip>();

            List<string> ioList = new List<string>();

            AddDeviceName(ioList, Config.IOIPList);
            foreach (MoveEquip_Config config in configList.Values)
            {
                MoveEquip equip = new MoveEquip(lineConfig.CID, config);

                AddDeviceName(ioList, config.IOIPList);
                MoveEquipMap.Add(config.Id, equip);
            }
            foreach (FeedingEquip_Config config in feedMap.Values)
            {
                FeedingEquip equip = new FeedingEquip(lineConfig.CID, config);

                AddDeviceName(ioList, config.IOIPList);
                FeedingEquipMap.Add(config.Id, equip);
            }
            foreach (ProvidingEquip_Config config in providMap.Values)
            {
                ProvidingEquip equip = new ProvidingEquip(lineConfig.CID, config);

                AddDeviceName(ioList, config.IOIPList);
                ProvidingEquipMap.Add(config.Id, equip);
            }
            IOManager.Init();
            //先初始化设备

            //初始化摄像机配置  
            CodeManager.LoadConfig(ConfigAppSettings.GetValue(Setting_Init.CameraName), ConfigAppSettings.GetValue(Setting_Init.CodeType));

            Task.Factory.StartNew(delegate
            {
                IOManager.instance.ConnectionIOList(ioList);
                Thread.Sleep(10);
                AIManager.ConnectionIP(Config.AIDevice_IP);

                addLastDI(IO_Type.Airpressure_Check, IOValue(IO_Type.Airpressure_Check));
                addLastDI(IO_Type.SuddenStop_BTN, IOValue(IO_Type.SuddenStop_BTN));
                addLastDI(IO_Type.Reset_BTN, IOValue(IO_Type.Reset_BTN));
                addLastDI(IO_Type.Start_BTN, IOValue(IO_Type.Start_BTN));

                mainTimer.Enabled = true;
                IoCheckTimer.Enabled = true;
                canStart = true;
            });
        }

        /// <summary>--
        /// BOX出入库完成事件
        /// </summary> 
        public void boxBean_OutStoreEnd(int storeId, InOutParam param)
        {
            MoveEquip moveEquip = MoveEquipMap[storeId];
            string posId = param != null ? param.PosId : "";
            if (moveEquip.IsDebug)
            {
                LogUtil.error( param.ToStr() + "  已经完成出库," + moveEquip.Name + "当前正在调试中,不继续操作!");
                return;
            }

            //料仓出库完成,移栽装置开始出库检测
            if (moveEquip.IsDebug == false && moveEquip.MoveInfo.MoveType == LineMoveType.None && moveEquip.runStatus == LineRunStatus.Runing)
            {
                LogUtil.info( param.ToStr() + "  已经完成出库,开始" + moveEquip.Name + "出库!");
                moveEquip.StartOutStoreMove(param);
            }
            else
            {
                LogUtil.info( param.ToStr() + "已经完成出库," + moveEquip.Name + "正在忙碌中!");
            }
        }

        private void AddDeviceName(List<string> targetList, List<string> list)
        {
            foreach (string str in list)
            {
                if (!targetList.Contains(str))
                {
                    targetList.Add(str);
                }
            }
        }
        public void InitTimer()
        {

            ledProcessTimer = new System.Timers.Timer();
            ledProcessTimer.Interval = 1000;
            ledProcessTimer.Elapsed += LedProcess;
            ledProcessTimer.AutoReset = true;
            ledProcessTimer.Enabled = false;

            IoCheckTimer = new System.Timers.Timer();
            IoCheckTimer.Interval = 200;
            IoCheckTimer.Elapsed += IoCheckTimerProcess;
            IoCheckTimer.AutoReset = true;
            IoCheckTimer.Enabled = false;
        }

        /// <summary>
        /// 开始运行
        /// </summary>
        public override bool StartRun()
        {
            IsScanCode = false;
            if (!canStart)
            {
                SetWarnMsg("启动失败:设备未初始化完成");
                return false;
            }
            if (MoveEquipMap == null)
            {
                SetWarnMsg("启动失败:未加载到移栽信息");
                return false;
            }
            if (IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
            {
                SetWarnMsg("启动失败:急停未开"); 
            }
            else if (IOValue(IO_Type.Airpressure_Check).Equals(IO_VALUE.LOW))
            {
                SetWarnMsg("启动失败:没有气压信号"); 
            }
            else
            {
                LogUtil.info( Name + "开始启动,启动时间:" + StartTime.ToString());
                runStatus = LineRunStatus.HomeMoving;
                StartTime = DateTime.Now;

                LineServer.StartServer(ConfigAppSettings.GetIntValue(Setting_Init.TCPServerPort));
                RHomeOp();

                foreach (MoveEquip moveEquip in this.MoveEquipMap.Values)
                {
                    if (moveEquip.IsDebug)
                    {
                        moveEquip.OpenStopCylinder();
                    }
                    else
                    {
                        moveEquip.StartRun();
                    }
                }

                ledProcessTimer.Enabled = true;
                IoCheckTimer.Enabled = true;
                mainTimer.Enabled = true;
                return true;
            }
            return false;
        } 
        private void RHomeOp()
        {

            alarmType = LineAlarmType.None;
            mainTimer.Enabled = false;
            IoCheckTimer.Enabled = false;
            isInprocess = false;
            isInSuddenDown = false;
            isNoAirCheck = false;
            alarmType = LineAlarmType.None;
            TrayManager.TrayErrorMsg = "";
            WarnMsg = "";
            preTrayNum = 0;
            PreIsHasProcess = true;

            if (TrayManager.ErrorStoreId > 0)
            {
                if (MoveEquipMap.ContainsKey(TrayManager.ErrorStoreId))
                {
                    MoveEquipMap[TrayManager.ErrorStoreId].preTrayNum = 0;
                }
            }
            TrayManager.ErrorStoreId = -1;

            IOMove(IO_Type.DriveMotor_Run, IO_VALUE.LOW);
            //上料气缸下降
            CylinderMove(null, IO_Type.InStore_TopCylinder_Up, IO_Type.InStore_TopCylinder_Down);

            //阻挡都上升
            IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.LOW);
            IOMove(IO_Type.StopCylinder_Down2, IO_VALUE.LOW);
            //横移轨道下降
            SideWayReset();
        }
        /// <summary>
           /// 收到复位按钮进行 复位,只复位有报警的轴
           /// </summary>
        public override bool Reset()
        {
            mainTimer.Enabled = false;
            IoCheckTimer.Enabled = false;
            bool isNeedAllReset = false;
            if (isInSuddenDown || isNoAirCheck)
            {
                TrayManager.LineNeedEmptyTrayNum = 0;
                isNeedAllReset = true;
                LogUtil.error( Name + "收到复位信号,在急停中或没有气压中,强制所有设备复位,清理出库需要的托盘数量~");
            }
            else if ((runStatus == LineRunStatus.HomeMoving || runStatus == LineRunStatus.Reset) && alarmType.Equals(LineAlarmType.None))
            {
                LogUtil.error( Name + "收到复位信号,已经在复位或原点返回中,且当前无报警,不处理复位");
                return false;
            }

            //停止运动
            MoveInfo.EndMove(); 

            runStatus = LineRunStatus.Reset;

            //重置通用处理
            RHomeOp();

            foreach (MoveEquip equip in MoveEquipMap.Values)
            {
                //调试状态不再重置
                if (!equip.IsDebug)
                {
                    if (isNeedAllReset || TrayManager.ErrorStoreId.Equals(equip.DeviceID) || (!equip.alarmType.Equals(LineAlarmType.None)))
                    {
                        LogUtil.info( Name + "收到复位信号," + equip.Name + " 需要复位");
                        equip.Reset();
                    }
                    else
                    {
                        LogUtil.info( Name + "收到复位信号," + equip.Name + " 正常无报警,不需要复位");
                    }
                }
                else
                {
                    LogUtil.info( Name + "收到复位信号," + equip.Name + " 调试中,需要下降阻挡气缸");
                    equip.OpenStopCylinder();
                }
            }
            IoCheckTimer.Enabled = true;
            mainTimer.Enabled = true;
            return true;
        }
        /// <summary>
        /// 停止运行
        /// </summary>
        public override void StopRun()
        {
            IoCheckTimer.Enabled = false; 
            mainTimer.Enabled = false;
            ledProcessTimer.Enabled = false;
             
            //停止运行时,把阻挡气缸上升
            StopMove();
            runStatus = LineRunStatus.Wait;

            LineServer.StopServer();
            TimeSpan span = DateTime.Now - StartTime;
            LogUtil.info( Name + ",停止运行,总运行时间:" + span.ToString());
        }

        public override void Alarm(LineAlarmType alarmType, string alarmDetial, string alarmMsg, LineMoveType storeMoveType)
        {
            if (this.alarmType.Equals(alarmType))
            {
                return;
            }
            SaveAlarmInfo(alarmType, alarmDetial, alarmMsg, storeMoveType);
            this.alarmType = alarmType;
            if (alarmType == LineAlarmType.SuddenStop)
            {
                isInSuddenDown = true;
            }
            else if (alarmType.Equals(LineAlarmType.NoAirCheck))
            {
                isNoAirCheck = true;
            }

            if (alarmType == LineAlarmType.SuddenStop || alarmType.Equals(LineAlarmType.NoAirCheck))
            { 
                LogUtil.error(WarnMsg);
                StopMove();
            }
            IOMove(IO_Type.Alarm_HddLed, IO_VALUE.HIGH); 
        }

        #region 休眠处理
        private DateTime LastNoOperateTime = DateTime.Now;
        public bool IsSleep = false;
        private bool PreIsHasProcess = true;
        private bool isHasProcess()
        {
            if (isInSuddenDown || isNoAirCheck)
            {
                return true;
            }
            if (!runStatus.Equals(LineRunStatus.Runing))
            {
                return true;
            }
            if (TrayManager.IsHasFull())
            {
                return true;
            }
            //判断是否有操作
            if (IOValue(IO_Type.InStore_TrayCheck1).Equals(IO_VALUE.HIGH) || IOValue(IO_Type.InStore_TrayCheck2).Equals(IO_VALUE.HIGH))
            {
                return true;
            }
            if (SW12_MoveInfo.MoveType.Equals(LineMoveType.None).Equals(false))
            {
                return true;
            }
            if (SW34_MoveInfo.MoveType.Equals(LineMoveType.None).Equals(false))
            {
                return true;
            }
            foreach (MoveEquip move in MoveEquipMap.Values)
            {
                if (!move.IsDebug && (!move.runStatus.Equals(LineRunStatus.Runing)))
                {
                    return true;
                }
            }
            return false;

        }
        private void UpdateSleep(bool isS)
        {
            if (isS != IsSleep)
            {
                if (isS)
                {
                    LogUtil.info( Name + "***********更新为休眠状态");
                    IOMove(IO_Type.DriveMotor_Run, IO_VALUE.LOW);
                }
                else
                {
                    LogUtil.info( Name + "***********更新为工作状态");
                    IOMove(IO_Type.DriveMotor_Run, IO_VALUE.HIGH);
                }
            }
            IsSleep = isS;
        }
        private void SleepProcess()
        {
            //休眠处理
            //判断所有的盘的都是空盘
            bool isHasO = isHasProcess();

            if (!isHasO)
            {
                if (IsSleep)
                {
                    isNotScanCode = false;
                    return;
                }
                if (PreIsHasProcess.Equals(isHasO))
                {
                    //判断时间是否需要休眠
                    TimeSpan span = DateTime.Now - LastNoOperateTime;
                    if (span.TotalSeconds > Config.Sleep_MSeconds)
                    {
                        LogUtil.info( Name + "***********已经【" + span.TotalSeconds + "】秒没有操作了,开始进入休眠状态");
                        UpdateSleep(true);
                    }
                }
                else
                {
                    PreIsHasProcess = isHasO;
                    LastNoOperateTime = DateTime.Now;
                }
            }
            else
            {
                PreIsHasProcess = isHasO;
                UpdateSleep(false);
            }
        }
        #endregion

        #region 灯光处理 
        private void LedProcess(object sender, ElapsedEventArgs e)
        {
            try
            {
                DateTime time = DateTime.Now;
                SleepProcess();

                //黄灯
                if (isNotScanCode || runStatus.Equals(LineRunStatus.HomeMoving) || runStatus.Equals(LineRunStatus.Reset))
                {
                    //开机执行中时黄灯闪烁
                    if (IsDoValue(IO_Type.RunSign_HddLed, IO_VALUE.HIGH))
                    {
                        IOMove(IO_Type.RunSign_HddLed, IO_VALUE.LOW);
                    }
                    else
                    {
                        IOMove(IO_Type.RunSign_HddLed, IO_VALUE.HIGH);
                    }
                }
                //休眠状态黄灯常亮
                else if (IsSleep)
                {
                    if (IsDoValue(IO_Type.RunSign_HddLed, IO_VALUE.LOW))
                    {
                        IOMove(IO_Type.RunSign_HddLed, IO_VALUE.HIGH);
                    }
                }
                else
                {
                    if (IsDoValue(IO_Type.RunSign_HddLed, IO_VALUE.HIGH))
                    {
                        IOMove(IO_Type.RunSign_HddLed, IO_VALUE.LOW);
                    }
                }

                bool isNeedAlarmLed = false;
                bool isInOut = false;
                if (alarmType.Equals(LineAlarmType.None).Equals(false) || isNoAirCheck || isInSuddenDown || TrayManager.TrayErrorMsg != "")
                {
                    isNeedAlarmLed = true;
                }

                foreach (MoveEquip moveEquip in MoveEquipMap.Values)
                {
                    if (!moveEquip.alarmType.Equals(LineAlarmType.None))
                    {
                        isNeedAlarmLed = true;
                    }
                    if (moveEquip.MoveInfo.MoveType.Equals(LineMoveType.InStore) || moveEquip.MoveInfo.MoveType.Equals(LineMoveType.OutStore))
                    {
                        isInOut = true;
                    }
                }

                //忙碌中,判断是否有移栽在出入库执行,绿灯闪烁
                if (isInOut)
                {
                    if (IsDoValue(IO_Type.AutoRun_HddLed, IO_VALUE.LOW))
                    {
                        IOMove(IO_Type.AutoRun_HddLed, IO_VALUE.HIGH);
                    }
                    else
                    {
                        IOMove(IO_Type.AutoRun_HddLed, IO_VALUE.LOW);
                    }
                }
                else if (IsDoValue(IO_Type.AutoRun_HddLed, IO_VALUE.HIGH))
                {
                    IOMove(IO_Type.AutoRun_HddLed, IO_VALUE.LOW);
                }

                //报警中 ,红灯闪烁
                if (isNeedAlarmLed)
                {
                    if (IsDoValue(IO_Type.Alarm_HddLed, IO_VALUE.LOW))
                    {
                        IOMove(IO_Type.Alarm_HddLed, IO_VALUE.HIGH);
                    }
                    else
                    {
                        IOMove(IO_Type.Alarm_HddLed, IO_VALUE.LOW);
                    }
                }
                else if (IsDoValue(IO_Type.Alarm_HddLed, IO_VALUE.HIGH))
                {
                    IOMove(IO_Type.Alarm_HddLed, IO_VALUE.LOW);
                }

            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "灯处理定时器出错:"+ ex.ToString());
            }
            Thread.Sleep(5);
        } 
        #endregion

        private void IoCheckTimerProcess(object sender, ElapsedEventArgs e)
        {
            try
            {
                DateTime time = DateTime.Now;
                if (this.runStatus >= LineRunStatus.HomeMoving)
                {

                    //取新的Io状态
                    IO_VALUE fuweiValue = IOValue(IO_Type.Reset_BTN);
                    IO_VALUE suddenBtn = IOValue(IO_Type.SuddenStop_BTN);
                    IO_VALUE airCheck = IOValue(IO_Type.Airpressure_Check);

                    IO_VALUE lastAir = DILastValueMap[IO_Type.Airpressure_Check];
                    IO_VALUE lastFuwei = DILastValueMap[IO_Type.Reset_BTN];

                    addLastDI(IO_Type.Airpressure_Check, airCheck);
                    addLastDI(IO_Type.SuddenStop_BTN, suddenBtn);
                    addLastDI(IO_Type.Reset_BTN, fuweiValue);

                    //气压检测按钮灭三秒以上才算关闭
                    IO_VALUE airValue = CheckAir(airCheck, lastAir);

                    //急停按钮和气压检测按钮需要一起使用  
                      if (suddenBtn .Equals( IO_VALUE.LOW))
                    {
                        Alarm(LineAlarmType.SuddenStop, "1", WarnMsg, LineMoveType.None);
                    }
                    if (fuweiValue.Equals(IO_VALUE.HIGH) && (!fuweiValue.Equals(lastFuwei)))
                    {
                        if (suddenBtn.Equals(IO_VALUE.LOW))
                        {
                            SetWarnMsg("收到复位信号但是急停未开,不处理复位");
                        }
                        else if (airValue.Equals(IO_VALUE.LOW))
                        {
                            SetWarnMsg("收到复位信号但是未检测到气压信号,不处理复位");
                        }
                        else
                        {
                            LogUtil.info( Name + "收到复位信号,开始复位!");
                            Reset();
                        }
                    }

                    //    FeederProcess();

                    //如果驱动电机正转过程中,驱动电机INV1状态 驱动电机INV2状态 有信号,需要报警
                    if (IOValue(IO_Type.DriveMotor_Run).Equals(IO_VALUE.HIGH))
                    {
                        if (IOValue(IO_Type.DriveMotor_INV1).Equals(IO_VALUE.LOW))
                        {
                            WarnMsg = "驱动电机INV1状态异常";
                            LogUtil.error("驱动电机正转过程中,DriveMotor_INV1信号LOW,需要报警", 300);
                            Alarm(LineAlarmType.SuddenStop, "1", WarnMsg, LineMoveType.None);

                        }
                        else if (IOValue(IO_Type.DriveMotor_INV2).Equals(IO_VALUE.LOW))
                        {
                            WarnMsg = "驱动电机INV2状态异常";
                            LogUtil.error("驱动电机正转过程中,DriveMotor_INV2信号LOW,需要报警", 301);
                            Alarm(LineAlarmType.SuddenStop, "1", WarnMsg, LineMoveType.None);
                        }
                    } 
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "定时检测报警出错:" + ex.ToString());
            }
            Thread.Sleep(1);
        }
        /// <summary>
        /// 定时处理,监听信号,监听IO
        /// </summary> 
        protected override void mainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                DateTime time = DateTime.Now;
                if (runStatus.Equals(LineRunStatus.Wait))
                {
                    //取新的Io状态
                    IO_VALUE fuweiValue = IOValue(IO_Type.Start_BTN);
                    IO_VALUE lastFuwei = DILastValueMap[IO_Type.Start_BTN];
                    addLastDI(IO_Type.Start_BTN, fuweiValue);
                    bool isAutoStart = ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun) == 1;

                    //收到复位信号后启动
                    if (isAutoStart && fuweiValue.Equals(IO_VALUE.HIGH) && lastFuwei.Equals(IO_VALUE.LOW))
                    {
                        //没有启动时收到复位按钮,相当于启动按钮
                        LogUtil.info( Name + "没有启动时收到复位按钮,相当于启动按钮,开始调用启动方法!");
                        bool isOk = StartRun();
                        if (!isOk)
                        {
                            LogUtil.error("启动失败,等待下次启动");
                            mainTimer.Enabled = true;
                        }
                    }
                    return;
                }
                //判断急停 
                else if (this.runStatus >= LineRunStatus.HomeMoving)
                {
                    foreach (MoveEquip moveEquip in this.MoveEquipMap.Values)
                    {
                        if (!moveEquip.IsDebug)
                        {
                            moveEquip.TimerProcess();
                        }
                    }

                    switch (runStatus)
                    {
                        case LineRunStatus.Busy:
                            BusyMoveProcess();
                            break;
                        case LineRunStatus.HomeMoving:
                            ResetProcess();
                            break;
                        case LineRunStatus.Reset:
                            ResetProcess();
                            break;
                        case LineRunStatus.Runing:
                            if ((isInSuddenDown.Equals(false) && isNoAirCheck.Equals(false)))
                            {
                                //出入库定时检测处理
                                InOutTimerProcess();

                                //清理超时异常
                                IOTimeOutProcess();
                            }
                            break;
                        default: break;
                    }

                    SideWayTimerProcess();

                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "主定时器出错:" + ex.ToString());
            }
            Thread.Sleep(1);
        } 
        /// <summary>
        /// 气压检测处理
        /// </summary> 
        private IO_VALUE CheckAir(IO_VALUE airCheck, IO_VALUE lastAir)
        {
            IO_VALUE airValue = IO_VALUE.HIGH;
            if (airCheck == IO_VALUE.LOW && (!isInSuddenDown))
            {
                //判断是否持续了3秒
                if (lastAir == IO_VALUE.LOW)
                {
                    if ((DateTime.Now - lastAirCloseTime).TotalSeconds > Config.AirCheckSeconds)
                    {
                        SetWarnMsg("持续"+Config.AirCheckSeconds+"秒未检测到气压信号");
                        //SendAlarmCode(0, LineAlarm.NoAirCheck);
                        airValue = IO_VALUE.LOW;
                        Alarm(LineAlarmType.NoAirCheck, "2", WarnMsg, LineMoveType.None);
                    }
                }
                else
                {
                    lastAirCloseTime = DateTime.Now;
                    isNoAirCheck = false;
                }
            }
            else
            {
                isNoAirCheck = false;
            }
            return airValue;
        }

        #region 扫码枪代码

        /// <summary>
        /// 是否已经扫码
        /// </summary>
        private bool IsScanCode = false;
        private string CodeMsg = "";
        private static string ACCode = "";
        public  void GetCameraCode()
        {
            if (IsInScan())
            {
                LogUtil.info("上次扫码还未执行完毕,请稍后!");
                return;
            }
            Task.Factory.StartNew(delegate
            {

                IsScanCode = true;
                LastScanTime = DateTime.Now;

             
                DateTime date = DateTime.Now;
                //   IOMove(IO_Type.CameraLight_Power, IO_VALUE.HIGH);
              
                List<string> codeList = CodeManager.CameraScan();
                if (codeList.Count <= 0)
                {
                    codeList = CodeManager.CameraScan();
                } 
                List<string> list = new List<string>();
                string outMsg = "";
                string message = "";
                int height = GetHeight();
                int width = GetWidth();
                //= 1 + 123.4x100.5 - 7x12 = CODE  
                foreach (string str in codeList)
                {
                    if (list.Contains(str.Trim()))
                    {
                        continue;
                    }
                    list.Add(str.Trim());
                    //string code = "=1+0x0-" + width + "x" + height + "=" + str.Trim();
                    string code = width + "x" + height + "%3D" + str.Trim();
                    message = message + code + spiltStr;
                }
                if (!outMsg.Equals(""))
                {
                    CodeMsg = "盘尺寸错误,清理二维码【" + message + "】";
                    LogUtil.error("盘尺寸错误,清理二维码【" + message + "】");
                    message = "";
                }
                //  KNDIOMove(IO_Type.CameraLight_Power, IO_VALUE.LOW);
                onCodeReceived( message,height,width);
                IsScanCode = false;
            });
        }

        private string spiltStr = "%23%23";

        private DateTime LastScanTime = DateTime.Now;
        private bool IsInScan()
        {
            if (!IsScanCode)
            {
                return false;
            }
            TimeSpan span = DateTime.Now - LastScanTime;
            if (span.TotalSeconds > 60)
            {
                //大于60秒表示超时了,可以重新开始扫码
                return false;
            }
            return true;
        }
        private void onCodeReceived(string message,int height,int width)
        {
            try
            {
                message = ScanCodeManager.ReplaceCode(message);
                if (String.IsNullOrEmpty(message))
                {
                    isNotScanCode = true;
                    LogUtil.info( Name + "没有收到二维码信息,请重新放入料盘");
                    return;
                }

                isNotScanCode = false;
                if (runStatus.Equals(LineRunStatus.Wait))
                {
                    LogUtil.info( Name + "收到二维码<< " + message + ",暂未开启,不需要发送服务器");
                    return;
                }
                //http://localhost/myproject/service/store/emptyPosForPutin
                //                参数:cids: 多个 cid
                //code: 条码内容
                string server = ConfigAppSettings.GetValue(Setting_Init.http_server) + "?cids=" + LineServer.GetAllCID() + "&code=%3D" + message; 
                LogUtil.info( Name + "收到二维码<< " + message + ",获取入库PosID:"+server);

                //发送扫码内容到服务器进行入库操作
                //  Operation operation =LineServer.GetInStoreOperation(message); 
                //   LineGetPosOp op = new LineGetPosOp(LineServer.GetAllCID(), message);

                string resultStr = HttpHelper.Post(server, "");
                LineOperation serverResult = JsonHelper.DeserializeJsonToObject<LineOperation>(resultStr);
                if (serverResult == null)
                {
                    LogUtil.info( Name + "二维码【" + message + "】没有收到服务器反馈!");
                    return;
                }
                else if ((!string.IsNullOrEmpty(serverResult.msg)) || serverResult.result.Equals(0).Equals(false))
                {
                    //如果有提示消息,直接显示提示
                    LogUtil.error( "服务器反馈   二维码【" + message + "】  :" + serverResult.msg);
                    return;
                }
              
                if (!serverResult.pos.Equals(""))
                { 
                    string posId = serverResult.pos;
                    int plateW = width;
                    int plateH =height;
                    string[] posArray = posId.Split('#');
                    if (!(posArray.Length == 2))
                    {
                        WarnMsg = Name + "入库库位格式错误:二维码【" + message + "】库位【" + posId + "】";
                        LogUtil.error( "服务器反馈   入库库位格式错误:二维码【" + message + "】库位【" + posId + "】");
                        return;
                    }

                    //判断盘是否过高(7*8的盘需要判断,如果盘过高,不让盘通过,直接显示报警信息) 
                    int storeId = int.Parse(posArray[0]);
                    //根据库位号查找移栽
                    MoveEquip moveEquip = MoveEquipMap[storeId];

                    //取盘号
                    string wareNum = serverResult.barcode ;
                    int trayCode = TrayManager.GetNum(0);
                    LogUtil.info( "更新盘空满信息,托盘号【" + trayCode + "】,是否有料盘【" + true + "】,出库入库【" + 1 + "】");
                    TrayManager.UpdateFixtureValue(trayCode, true, 1);

                    //TODO:判断BOX是否处于可以入库状态,如果调试或急停中,需要返回给服务器;
                    if (LineServer.BoxCanInStore(moveEquip.DeviceID))
                    {
                        InOutParam param = new InOutParam(trayCode, wareNum, posId, plateH, plateW);
                        //  判断PosID是否已经在入库或者在排队列表中,如果已经存在,加入列表失败                         
                        if (IsReviceInPosId(moveEquip, posId))
                        {
                            WarnMsg = moveEquip.Name + "入库库位重复: " + param.ToStr() + " ,入库失败!";
                            moveEquip.WarnMsg = WarnMsg;
                            LogUtil.info( "收到服务器入库命令  " + WarnMsg);
                            return;
                        }

                        lock (moveEquip.waitInListLock)
                        {
                            //  InOutParam param = new InOutParam(trayCode,wareNum, posId, plateH, plateW );
                            StartInStoreMove(param);
                            //如果当前正在出入库中,需要记录下来,等待空闲时执行
                            LogUtil.info( moveEquip.Name + " 收到服务器入库命令: " + param.ToStr() + " ,写入排队列表中等待!");
                            moveEquip.waitInStoreList.Add(param);
                        }
                        LineServer.CheckInStorePos(storeId, param);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name +" "+ ex.ToString());
            }
        }
  

        /// <summary>
        ///是否已经接收到过出库信息
        ///</summary> 
        private bool IsReviceInPosId(MoveEquip moveEquip, string posId)
        {
            bool isReviceInfo = false;
            if (moveEquip.MoveInfo.MoveType.Equals(LineMoveType.InStore) && moveEquip.MoveInfo.MoveParam != null && moveEquip.MoveInfo.MoveParam.PosId.Equals(posId))
            {
                isReviceInfo = true;
            }
            if (!isReviceInfo && moveEquip.waitInStoreList.Count > 0)
            {
                foreach (InOutParam inout in moveEquip.waitInStoreList)
                {
                    if (inout.PosId.Equals(posId) && (!inout.WareCode.Equals("")))
                    {
                        isReviceInfo = true;
                        break;
                    }
                }
            }
            return isReviceInfo;
        }
        #endregion


        public override void StopMove()
        {
            IsScanCode = false;

            foreach (MoveEquip store in this.MoveEquipMap.Values)
            {
                if (!store.IsDebug)
                {
                    store.StopRun();
                }
                else
                {
                    store.CloseCylinderStop();
                }
            }
            MoveInfo.EndMove();

            IOMove(IO_Type.DriveMotor_Run, IO_VALUE.LOW);
            IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.LOW);
            IOMove(IO_Type.StopCylinder_Down2, IO_VALUE.LOW);

            SideWayStop();
        }

        protected override void ResetProcess()
        {
            bool isOk = true;
            //判断是否所有的已经返回完成

            foreach (MoveEquip moveEquip in this.MoveEquipMap.Values)
            {
                if ((moveEquip.runStatus.Equals(LineRunStatus.HomeMoving) || moveEquip.runStatus.Equals(LineRunStatus.Reset)) && moveEquip.IsDebug.Equals(false))
                {
                    if (moveEquip.alarmType.Equals(LineAlarmType.None))
                    {
                        isOk = false;
                        break;
                    }
                    else
                    {
                        WarnMsg = moveEquip.Name + "在复位过程中报警,需要重新复位";
                    }
                }
            }
            if (!ResetSingleISOk())
            {
                isOk = false;
            }
            if (isOk)
            {
                PreIsHasProcess = false;
                //打开流水线
                IOMove(IO_Type.DriveMotor_Run, IO_VALUE.HIGH);
                //所有原点重置完成
                runStatus = LineRunStatus.Runing;
                LogUtil.info( Name + "所有设备重置完成,打开流水线,开始运转!");
            }
        }
        private bool ResetSingleISOk()
        {
            //气缸需要到位 
            if (IOValue(IO_Type.SidesWay1_TopCylinder_Down).Equals(IO_VALUE.HIGH) &&
                IOValue(IO_Type.SidesWay2_TopCylinder_Down).Equals(IO_VALUE.HIGH) &&
                IOValue(IO_Type.SidesWay3_TopCylinder_Down).Equals(IO_VALUE.HIGH) &&
                    IOValue(IO_Type.SidesWay4_TopCylinder_Down).Equals(IO_VALUE.HIGH) &&
                    IOValue(IO_Type.SidesWay4_TopCylinder_Up).Equals(IO_VALUE.LOW) &&
                    IOValue(IO_Type.SidesWay4_TopCylinder_Up).Equals(IO_VALUE.LOW) &&
                    IOValue(IO_Type.SidesWay4_TopCylinder_Up).Equals(IO_VALUE.LOW) &&
                    IOValue(IO_Type.SidesWay4_TopCylinder_Up).Equals(IO_VALUE.LOW) &&
                    IOValue(IO_Type.InStore_TopCylinder_Down).Equals(IO_VALUE.HIGH) &&
                    IOValue(IO_Type.InStore_TopCylinder_Up).Equals(IO_VALUE.LOW))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        private void SetWarnMsg(string msg)
        {
            WarnMsg = msg;
            LogUtil.error(Name + WarnMsg);
        }
    }
}