DBStoreManager.cs 38.5 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
using log4net; 
using OnlineStore.Common;
using OnlineStore.DeviceLibrary.doubleStore;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
 
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    public class DBStoreManager
    {
        public static string PreMsg = "";
        //三个灯状态,需要发给Lib
        private static int RedLedStatus = 0;
        private static int GreenLedStatus = 0;
        private static int YellowLedStatus = 0;
        private static IO_VALUE lastFuwei = IO_VALUE.LOW;

        public static bool isUseMiddleAxis = true;
        public static StoreRunStatus storeRunStatus = StoreRunStatus.Wait;
        public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static Dictionary<int, DB_LA_BoxBean> StoreBoxMap = new Dictionary<int, DB_LA_BoxBean>();
        public static Dictionary<int, DB_LA_LineBean> StoreLineMap = new Dictionary<int, DB_LA_LineBean>();
        public static Dictionary<int, DB_LA_Store_Config> configMap = new Dictionary<int, DB_LA_Store_Config>();
        public static List<string> DIONameList = new List<string>();

        public static string CID = "";
        public static DB_LA_LINE_Config lineConfig = null;
        private static bool IsConServer = true;
        public static string StoreName = "";
        private static bool isInSuddenDown = false; 

        private static System.Timers.Timer serverConTimer = new System.Timers.Timer();
        private static System.Timers.Timer mainTimer = new System.Timers.Timer();
        /// <summary>
        /// 灯闪烁定时器
        /// </summary>
        private static System.Timers.Timer ledProcessTimer = null;

        public static string WarnMsg = "";
        /// <summary>
        /// 开始运行
        /// </summary>
        public static bool IsRun = false;
         
        /// <summary>
        /// 获取当前报警信息
        /// </summary>
        public static string GetWarMsg()
        {
            if (!WarnMsg.Equals(""))
            {
                return WarnMsg;
            }
            foreach (DB_LA_BoxBean box in StoreBoxMap.Values)
            {
                if (!box.WarnMsg.Equals(""))
                {
                    return box.WarnMsg;
                }
            }

            foreach (DB_LA_LineBean line in StoreLineMap.Values)
            {
                if (!line.WarnMsg.Equals(""))
                {
                    return line.WarnMsg;
                }
            }
            return "";
        }
        public static void InitTimer()
        {
            serverConTimer = new System.Timers.Timer();
            serverConTimer.Interval = 1000;
            serverConTimer.Elapsed += server_connect_timer_Tick;
            serverConTimer.AutoReset = true;
            serverConTimer.Enabled = false;

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

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

        public static bool InitDBStore()
        {
            isUseMiddleAxis = true;
            int storecount = ConfigAppSettings.GetIntValue(Setting_Init.store_count);
            string appPath = Application.StartupPath;

            string lineCID = ConfigAppSettings.GetValue(Setting_Init.Store_CID);
            CID = lineCID;
            string linefilePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_ConfigPath);
            string storeType = ConfigAppSettings.GetValue(Setting_Init.Store_Type);
            int storeCount = ConfigAppSettings.GetIntValue(Setting_Init.store_count);

            StoreBoxMap = new Dictionary<int, DB_LA_BoxBean>();
            StoreLineMap = new Dictionary<int, DB_LA_LineBean>();
            configMap = new Dictionary<int, DB_LA_Store_Config>();
            DIONameList = new List<string>();
            LogUtil.info(LOGGER, "配置的料仓 类型=" + storeType + ",开始加载料仓配置");

            lineConfig = (DB_LA_LINE_Config)CSVConfigReader.LoadConfig(0, lineCID, storeType, linefilePath);
            foreach (string str in lineConfig.DIODeviceNameList)
            {
                if (!DIONameList.Contains(str))
                {
                    DIONameList.Add(str);
                }
            }
            StoreName = (" DBSTORE_" + lineCID + " ").ToUpper();
            //加载位置
            string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_Position_Config);

            bool isload = false;
            if (File.Exists(positionConfigFile))
            {
                LogUtil.info(LOGGER, "加载位置文件:" + positionConfigFile);
                isload = true;
                CSVPositionReader<DoublePosition>.ReloadCSVFile(positionConfigFile);
            }
            if (storeType == StoreType.RC_DB_LINE)
            {
                for (int i = 1; i <= storeCount; i++)
                {
                    string addStr = "_" + i;
                    if (!isload)
                    {
                        //位置配置
                        positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_Position_Config, addStr);
                        CSVPositionReader<DoublePosition>.AddCSVFile(positionConfigFile);
                        LogUtil.info(LOGGER, "加载位置文件:" + positionConfigFile);
                    }
                    string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_ConfigPath, addStr);
                    DB_LA_Store_Config config = (DB_LA_Store_Config)CSVConfigReader.LoadConfig(i, CID, StoreType.RC_DB_LA, filePath);

                    foreach (string str in config.DIODeviceNameList)
                    {
                        if (!DIONameList.Contains(str))
                        {
                            DIONameList.Add(str);
                        }
                    }
                    DB_LA_BoxBean boxBean = new DB_LA_BoxBean(lineConfig, config);
                    DB_LA_LineBean lineBean = new DB_LA_LineBean(config);
                    StoreBoxMap.Add(i, boxBean);
                    StoreLineMap.Add(i, lineBean);
                    configMap.Add(i, config);
                }
                LogUtil.info(LOGGER, "加载手动料仓完成!");
            }
            else
            {
                LogUtil.error(LOGGER, "没有配置手动料仓,启动失败!");
                Application.Exit();
                return false;
            }
            DBLineControl.CurrWidth = lineConfig.LineWidth_DefaultWidth / 10;
            InitTimer();
            //连接设备
            KNDManager.ConnectionKND(DIONameList);
            //OpenShuoKe();
            //默认三个灯都亮
            KNDIOMove(Store_IO_Type.Alarm_HddLed, IO_VALUE.LOW);
            KNDIOMove(Store_IO_Type.AutoRun_HddLed, IO_VALUE.HIGH);
            KNDIOMove(Store_IO_Type.RunSign_HddLed, IO_VALUE.HIGH);
            GreenLedStatus = 1;
            //mainTimer.Enabled = true;
            int isAuto = ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun);
            if (isAuto == 1)
            {
                mainTimer.Enabled = true;
            }
            return true;
        }
        public static bool OpenModbus()
        {
            ////打开电钢端口号
            //Parity parity = (Parity)lineConfig.Modbus_PortParity;
            //StopBits bit = (StopBits)lineConfig.Modbus_StopBits;
            //ErrorCode returnCode = ModbusBean.instance.PortOpen(lineConfig.Modbus_PortName, lineConfig.Modbus_PortBaudrate, parity, bit);
            //if (returnCode.Equals(ErrorCode.ERROR_NONE) || DeviceControl.isNoDeviceDebug)
            //{
            //    LogUtil.info(StoreName + "打开电钢串口【" + lineConfig.Modbus_PortName + "】成功");
            //    return true;
            //}
            //else
            //{
                //LogUtil.info(StoreName + "打开电钢串口【" + lineConfig.Modbus_PortName + "】失败,错误码:" + returnCode);
                //LogUtil.error(LOGGER, StoreName + "电钢打开失败,启动失败!");
                return false;
            //}
        }
        public static bool OpenShuoKe()
        {
            //打开硕科步进驱动器端口
            Parity parity = (Parity)lineConfig.LineWidth_PortParity;
            StopBits bit = (StopBits)lineConfig.LineWidth_StopBits;
            bool result = ShuoKeControls.InitPort(lineConfig.LineWidth_PortName, lineConfig.LineWidth_PortBaudrate, lineConfig.LineWidth_PortParity, 8, bit);
            if (result)
            {
                LogUtil.info(StoreName + "打开硕科步进控制器【" + lineConfig.LineWidth_PortName + "】成功");
                return true;
            }
            else
            {
                LogUtil.error(LOGGER, StoreName + "打开硕科步进控制器【" + lineConfig.LineWidth_PortName + "】失败,启动失败!");
                return false;
            }
        }

        public static bool IsCanStart()
        {
            if (DeviceControl.isNoDeviceDebug)
            {
                return true;
            }
            //KNDManager.ConnectionKND(DIONameList);
            //判断io模块是否连接上
            foreach (string dio in DIONameList)
            {
                if (!KNDManager.IsConnection(dio))
                {
                    LogUtil.error(LOGGER, StoreName + "启动失败,IO模块【" + dio + "】未连接上!");
                    return false;
                }
            }
            if (KNDIOValue(Store_IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
            {
                LogUtil.error(LOGGER, StoreName + "启动失败,总急停未开!");
                return false;
            }
            if (KNDIOValue(Store_IO_Type.LINE_Suddown).Equals(IO_VALUE.LOW))
            {
                LogUtil.error(LOGGER, StoreName + "启动失败,流水线急停未开!");
                return false;
            }
            if (OpenModbus() == false)
            {
                return false;
            }
            if (ShuoKeControls.isRun == false && !OpenShuoKe())
            {
                LogUtil.error(LOGGER, StoreName + "启动失败,硕科步进驱动器串口未打卡!");
                return false;
            }

            return true;
        }
        public static bool StartRun()
        { 
            mainTimer.Enabled = false;
            //判断急停是否开着
            if (!IsCanStart())
            {
                return false;
            }
            //启动温湿度服务器
            HumitureServer.StartTemperateServer(lineConfig.TemperateServer_Port);
            KNDIOMove(Store_IO_Type.AutoRun_HddLed, IO_VALUE.HIGH);
            KNDIOMove(Store_IO_Type.RunSign_HddLed, IO_VALUE.LOW);
            KNDIOMove(Store_IO_Type.Alarm_HddLed, IO_VALUE.LOW);
            GreenLedStatus = 1;
            RedLedStatus = 0;
            YellowLedStatus = 0;

            foreach (DB_LA_BoxBean bean in StoreBoxMap.Values)
            {
                if (bean.IsDebug)
                {
                    LogUtil.error(LOGGER, bean.StoreName + "调试模式,不需要启动!");
                }
                else if (!bean.StartRun() && DeviceControl.isNoDeviceDebug == false)
                {
                    LogUtil.error(LOGGER, bean.StoreName + "启动失败,直接关闭所有!");
                    StopRun();
                    return false;
                }
            }
            //启动流水线
            foreach (DB_LA_LineBean line in DBStoreManager.StoreLineMap.Values)
            {
                if (DBLineControl.TheLineIdList.Contains(line.StoreID))
                {
                    line.StartRun();
                }
                else
                {
                    LogUtil.info(line.StoreName + "调试状态暂不启动!");
                }
            }

            IsRun = true;
            storeRunStatus = StoreRunStatus.Runing;
            LogUtil.info(LOGGER, StoreName + "启动成功,时间:" + DateTime.Now.ToString() + "!");
            //timersTimer.Enabled = true;
            if (IsConServer)
            {
                serverConTimer.Enabled = true;
            }
            mainTimer.Enabled = true;
            ledProcessTimer.Enabled = true;

            //写入状态
            //FileGenerated.writeSGIFile(DBLineControl.MachineId, "0000", "0", "0", 1, 0);
            //FileGenerated.writeSGIFile(DBLineControl.MachineId, GetLightStr(), "0", "0", 0, 0);
            //FileGenerated.writeSTSFile(DBLineControl.MachineId, 1, 0);
            return true;
        }

        /// <summary>
        /// 停止运行
        /// </summary>
        public static void StopRun()
        {
            foreach (DB_LA_BoxBean bean in StoreBoxMap.Values)
            {
                if (bean.IsDebug == false)
                {
                    bean.StopRun();
                }
            }
            foreach (DB_LA_LineBean bean in StoreLineMap.Values)
            {
                bean.StopRun();
            }
            storeRunStatus = StoreRunStatus.Wait;
            //timersTimer.Enabled = false;
            ledProcessTimer.Enabled = false;
            serverConTimer.Enabled = false;
            mainTimer.Enabled = false;
            IsRun = false;
            //CloseLed();
            KNDManager.CloseAllDO();
            KNDManager.CloseAllConnection();
            //ModbusBean.instance.PortClose();
            ShuoKeControls.ClosePort();
            HumitureServer.StopTemperateServer();
            //写入状态
            //FileGenerated.writeSGIFile(DBLineControl.MachineId, "0", "0", "0", 0, 0);
            //FileGenerated.writeSTSFile(DBLineControl.MachineId, 0, 0);
            LogUtil.info(LOGGER, StoreName + "停止运行,时间" + DateTime.Now.ToShortTimeString() + "!");
        }

        /// <summary>
        /// 定时处理,监听信号,监听IO
        /// </summary>  
        protected static void mainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DateTime time = DateTime.Now;
            if (storeRunStatus.Equals(StoreRunStatus.Wait))
            {
                //取新的Io状态
                IO_VALUE autoSingle = KNDIOValue(Store_IO_Type.AutoRun_Single);
                if (ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun) == 1)
                {
                    //收到复位信号后启动
                    if (autoSingle.Equals(IO_VALUE.HIGH) && lastFuwei.Equals(IO_VALUE.LOW))
                    {
                        //没有启动时收到复位按钮,相当于启动按钮
                        LogUtil.info(LOGGER, StoreName + "没有启动时收到复位按钮,相当于启动按钮,开始调用启动方法!");
                        StartRun();
                    }
                    lastFuwei = autoSingle;
                    return;
                }
                lastFuwei = autoSingle;
            }
            //判断急停 
            else if (storeRunStatus >= StoreRunStatus.HomeMoving)
            {
                if (KNDIOValue(Store_IO_Type.SuddenStop_BTN).Equals(IO_VALUE.HIGH))
                {
                    foreach (DB_LA_BoxBean bean in StoreBoxMap.Values)
                    {
                        if (!bean.IsDebug)
                        {
                            bean.TimerProcess();
                        }
                    }
                    foreach (DB_LA_LineBean bean in StoreLineMap.Values)
                    {
                        if (DBLineControl.TheLineIdList.Contains(bean.StoreID) && bean.storeRunStatus >= StoreRunStatus.HomeMoving)
                        {
                            bean.timer_Elapsed(null, null);
                        }
                    }

                    //判断复位信号
                    if (KNDIOValue(Store_IO_Type.Reset_BTN).Equals(IO_VALUE.HIGH))
                    {
                        if (storeRunStatus.Equals(StoreRunStatus.Reset) && (isInSuddenDown == false))
                        {
                            LOGGER.Error(StoreName + "收到复位信号,当前已经在复位中,不进行处理");
                        }
                        else
                        {
                            LOGGER.Error(StoreName + "收到复位信号,开始启动复位");
                            isInSuddenDown = false;
                            bool isNeedCon = false;
                            foreach (string name in DIONameList)
                            {
                                if (!KNDManager.IsConnection(name))
                                {
                                    LOGGER.Error(StoreName + "收到复位信号,重连IO模块【" + name + "】");
                                    KNDManager.ConnectionKND(name);
                                    isNeedCon = true;
                                }
                            }
                            //OpenShuoKe();
                            if (isNeedCon)
                            {
                                Thread.Sleep(1000);
                            }
                            foreach (DB_LA_BoxBean bean in StoreBoxMap.Values)
                            {
                                if (isInSuddenDown || (!bean.alarmType.Equals(StoreAlarmType.NoAirCheck)))
                                {
                                    bean.Reset();
                                }
                                else
                                {
                                    LOGGER.Error(StoreName + "收到复位信号," + bean.StoreName + "没有报警不需要复位");
                                }
                            }
                            foreach (DB_LA_LineBean bean in StoreLineMap.Values)
                            {
                                if (DBLineControl.TheLineIdList.Contains(bean.StoreID) && bean.storeRunStatus >= StoreRunStatus.HomeMoving)
                                {
                                    bean.Reset();
                                }
                            }
                            storeRunStatus = StoreRunStatus.Reset;
                        }
                    }

                    if (storeRunStatus.Equals(StoreRunStatus.Reset))
                    {
                        ResetProcess();
                    }
                    else if (storeRunStatus.Equals(StoreRunStatus.HomeMoving))
                    {
                        ReturnHomeProcess();
                    }
                }
                else
                {
                    if (!isInSuddenDown)
                    {
                        isInSuddenDown = true;
                        storeRunStatus = StoreRunStatus.Runing;
                        //TODO 收到急停信号,停止所有运行
                        LOGGER.Error(StoreName + "收到急停信号,停止所有运行");
                        foreach (DB_LA_BoxBean bean in StoreBoxMap.Values)
                        {
                            bean.StopMove();
                            bean.Alarm(StoreAlarmType.SuddenStop, "1", StoreName + "收到急停信号,停止所有运行", 0);
                        }
                        foreach (DB_LA_LineBean bean in StoreLineMap.Values)
                        {
                            bean.StopMove();
                        }
                        //ShuoKeControls.ClosePort();
                    }
                    //判断复位信号
                    if (KNDIOValue(Store_IO_Type.Reset_BTN).Equals(IO_VALUE.HIGH))
                    {
                        LOGGER.Error(StoreName + "急停中收到复位信号,不处理");
                    }
                }
            }
        }

        private static void ReturnHomeProcess()
        {
            bool isEnd = true;
            foreach (DB_LA_BoxBean bean in StoreBoxMap.Values)
            {
                if (bean.IsDebug == false && bean.storeRunStatus >= StoreRunStatus.HomeMoving && bean.storeRunStatus.Equals(StoreRunStatus.HomeMoving) && bean.alarmType.Equals(StoreAlarmType.None))
                {
                    isEnd = false;
                    break;
                }
            }
            foreach (DB_LA_LineBean bean in StoreLineMap.Values)
            {
                if (DBLineControl.TheLineIdList.Contains(bean.StoreID) && bean.storeRunStatus >= StoreRunStatus.HomeMoving && bean.storeRunStatus.Equals(StoreRunStatus.HomeMoving))
                {
                    isEnd = false;
                    break;
                }
            }
            if (isEnd)
            {
                storeRunStatus = StoreRunStatus.Runing;
                LOGGER.Error(StoreName + "全部原点返回完成");
            }
        }

        private static void ResetProcess()
        {
            bool isEnd = true;
            foreach (DB_LA_BoxBean bean in StoreBoxMap.Values)
            {
                if (bean.IsDebug == false && bean.storeRunStatus >= StoreRunStatus.HomeMoving && bean.storeRunStatus.Equals(StoreRunStatus.Reset) && bean.alarmType.Equals(StoreAlarmType.None))
                {
                    isEnd = false; break;
                }
            }
            foreach (DB_LA_LineBean bean in StoreLineMap.Values)
            {
                if (DBLineControl.TheLineIdList.Contains(bean.StoreID) && bean.storeRunStatus >= StoreRunStatus.HomeMoving && bean.storeRunStatus.Equals(StoreRunStatus.HomeMoving))
                {
                    isEnd = false;
                    break;
                }
            }
            if (isEnd)
            {
                storeRunStatus = StoreRunStatus.Runing;
                LOGGER.Error(StoreName + "全部复位完成");
            }
        }

        public static IO_VALUE GetSingle(string ioType)
        {
            return KNDIOValue(ioType);
        }
        /// <summary>
        /// 关闭所有灯
        /// </summary>
        public static void CloseLed()
        {
            KNDIOMove(Store_IO_Type.Alarm_HddLed, IO_VALUE.LOW);
            Thread.Sleep(100);
            KNDIOMove(Store_IO_Type.RunSign_HddLed, IO_VALUE.LOW);
            Thread.Sleep(100);
            KNDIOMove(Store_IO_Type.AutoRun_HddLed, IO_VALUE.LOW);
        }
        public static void KNDIOMove(string ioType, IO_VALUE ioValue)
        {
            if (lineConfig.StoreDOList.ContainsKey(ioType))
            {
                ConfigIO configIo = lineConfig.StoreDOList[ioType];
                KNDManager.WriteSingleDO(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr(), ioValue);
                //UpdateDoValue(ioType, ioValue);
            }
            else
            {
                LogUtil.error(LOGGER, StoreName + "没有DO=" + ioType);
            }
        }
        public static IO_VALUE KNDIOValue(string ioType)
        {
            IO_VALUE ioValue = IO_VALUE.LOW;
            if (lineConfig.StoreDIList.ContainsKey(ioType))
            {
                ConfigIO configIo = lineConfig.StoreDIList[ioType];
                ioValue = KNDManager.GetDIValue(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr());
                //UpdateDoValue(ioType, ioValue);
            }
            else if (lineConfig.StoreDOList.ContainsKey(ioType))
            {
                ConfigIO configIo = lineConfig.StoreDOList[ioType];
                ioValue = KNDManager.GetDOValue(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr());
            }
            else
            {
                LogUtil.error(LOGGER, StoreName + "没有DO=" + ioType);
            }
            return ioValue;
        }
        public static string GetLightStr()
        {
            return "0" + GreenLedStatus.ToString() + YellowLedStatus.ToString() + RedLedStatus.ToString();
        }
        /// <summary>
        /// 灯定时处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void LedProcess(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                bool isNotScanCode = false;
                bool isNeedAlarmLed = false;
                bool isInOutMoveing = false;
                //报警灯
                if (isInSuddenDown || DeviceControl.TrayErrorMsg != "")
                {
                    isNeedAlarmLed = true;
                }
                foreach (DB_LA_BoxBean box in StoreBoxMap.Values)
                {
                    //if (box.IsDebug == false)
                    //{
                        if (box.isNotScanCode)
                        {
                            isNotScanCode = true;
                        }
                        if (!box.alarmType.Equals(StoreAlarmType.None) || box.isInSuddenDown)
                        {
                            isNeedAlarmLed = true;
                        }
                        if (box.storeStatus.Equals(StoreStatus.InStoreEnd) || box.storeStatus.Equals(StoreStatus.InStoreExecute)
                 || box.storeStatus.Equals(StoreStatus.OutStoreExecute) || box.storeStatus.Equals(StoreStatus.OutStoreBoxEnd))
                        {
                            isInOutMoveing = true;
                        }
                    //}
                }
                //黄灯
                if (storeRunStatus.Equals(StoreRunStatus.HomeMoving) || storeRunStatus.Equals(StoreRunStatus.Reset))
                {
                    //开机执行中时黄灯闪烁
                    if (KNDIOValue(Store_IO_Type.RunSign_HddLed).Equals(IO_VALUE.HIGH))
                    {
                        KNDIOMove(Store_IO_Type.RunSign_HddLed, IO_VALUE.LOW);
                    }
                    else
                    {
                        KNDIOMove(Store_IO_Type.RunSign_HddLed, IO_VALUE.HIGH);
                    }
                    YellowLedStatus = 1;
                }
                else
                {
                    //如果有一个料仓未扫到码,则黄灯亮

                    if (isNotScanCode)
                    {
                        YellowLedStatus = 2;
                        if (KNDIOValue(Store_IO_Type.RunSign_HddLed).Equals(IO_VALUE.LOW))
                        {
                            KNDIOMove(Store_IO_Type.RunSign_HddLed, IO_VALUE.HIGH);
                        }
                    }
                    else
                    {
                        YellowLedStatus = 0;
                        if (KNDIOValue(Store_IO_Type.RunSign_HddLed).Equals(IO_VALUE.HIGH))
                        {
                            KNDIOMove(Store_IO_Type.RunSign_HddLed, IO_VALUE.LOW);
                        }
                    }
                }
                //路灯亮
                //出入库执行中,绿灯闪烁
                if (isInOutMoveing)
                {
                    GreenLedStatus = 1;
                }
                else
                {
                    GreenLedStatus = 2;
                }
                if (isInOutMoveing && KNDIOValue(Store_IO_Type.AutoRun_HddLed).Equals(IO_VALUE.HIGH))
                {
                    KNDIOMove(Store_IO_Type.AutoRun_HddLed, IO_VALUE.LOW);
                }
                else
                {
                    if (KNDIOValue(Store_IO_Type.AutoRun_HddLed).Equals(IO_VALUE.LOW))
                    {
                        KNDIOMove(Store_IO_Type.AutoRun_HddLed, IO_VALUE.HIGH);
                    }
                }

                if (isNeedAlarmLed)
                {
                    RedLedStatus = 2;
                    if (KNDIOValue(Store_IO_Type.Alarm_HddLed).Equals(IO_VALUE.LOW))
                    {
                        KNDIOMove(Store_IO_Type.Alarm_HddLed, IO_VALUE.HIGH);
                    }
                }
                else
                {
                    RedLedStatus = 1;
                    if (KNDIOValue(Store_IO_Type.Alarm_HddLed).Equals(IO_VALUE.HIGH))
                    {
                        KNDIOMove(Store_IO_Type.Alarm_HddLed, IO_VALUE.LOW);
                    }
                }
            }
            catch (Exception ex)
            {
                LOGGER.Error(StoreName + "灯处理定时器出错:", ex);
            }
            Thread.Sleep(50);
        }
        #region 与服务器通信定时器,每1秒向服务器通知一次状态,同时执行出库操作
        private static bool isInProcess = false;
        public static void server_connect_timer_Tick(object sender, EventArgs e)
        {
            if (isInProcess)
            {
                return;
            }
            isInProcess = true;
            try
            {
                SendLineStatus(-1, "", StoreStatus.StoreOnline);
            }
            catch (Exception ex)
            {
                LOGGER.Error("定时给服务器发送消息出错:", ex);
            }
            isInProcess = false;
        }

        /// <summary>
        /// 获取整个料仓的状态
        /// </summary> 
        public static Operation getLineBoxStatus(int storeId, string posId, StoreStatus storeStatus)
        {
            //构建发送给服务器的对象
            Operation lineOperation = new Operation();
            lineOperation.msg = "";
            lineOperation.alarmList = new List<AlarmInfo>();
            lineOperation.cid = CID;
            lineOperation.seq = ConfigAppSettings.nextSeq();
            lineOperation.status = 1;
            if (WarnMsg != "")
            {
                lineOperation.status = (int)StoreStatus.Warning;
                lineOperation.msg = WarnMsg;
            }
            else if (IsRun)
            {
                lineOperation.status = (int)StoreStatus.StoreOnline;
            }
            lineOperation.status = (int)StoreStatus.StoreOnline;
            //循环加入单台料仓BOX的状态
            foreach (DB_LA_BoxBean box in StoreBoxMap.Values)
            {
                //if (box.StoreID != DBLineControl.TheLineId)
                //{
                //    continue;
                //}

                BoxStatus boxStatus = new BoxStatus();
                boxStatus.boxId = box.StoreID;

                //状态
                boxStatus.status = (int)box.storeStatus;

                if (box.IsDebug)
                {
                    boxStatus.status = (int)StoreStatus.Debugging;
                }
                boxStatus.msg = box.WarnMsg;
                lineOperation.msg = AddMsg(box.WarnMsg, lineOperation.msg);
                box.WarnMsg = "";
                //状态
                boxStatus.status = (int)box.storeStatus;
                if (box.IsDebug)
                {
                    boxStatus.status = (int)StoreStatus.Debugging;
                }
                else if (box.storeStatus.Equals(StoreStatus.OutStoreBoxEnd) || box.storeStatus.Equals(StoreStatus.InStoreEnd))
                {
                    boxStatus.data.Add(ParamDefine.posId, box.lastPosId);
                }
                else if (!box.lastPosId.Equals(""))
                {
                    boxStatus.data.Add(ParamDefine.posId, box.lastPosId);
                    boxStatus.status = (int)box.lastPosIdStatus;
                    if (box.lastPosId != "")
                    {
                        LogUtil.info(LOGGER, "给服务器发送出入库完成消息:" + box.StoreName + ",status【" + box.lastPosIdStatus + "】posId【" + box.lastPosId + "】");
                    }
                    box.lastPosId = "";
                }

                //判断是否需要修改状态
                if (storeId.Equals(box.StoreID))
                {
                    boxStatus.data = new Dictionary<string, string>();
                    boxStatus.data.Add(ParamDefine.posId, posId);
                    boxStatus.status = (int)storeStatus;
                    if (box.lastPosId != "")
                    {
                        LogUtil.info(LOGGER, "给服务器发送出入库完成消息:" + box.StoreName + ",status【" + storeStatus + "】posId【" + posId + "】");
                    }
                }
                //温湿度
                ASTemperateParam param = HumitureServer.GetTemperateParam(box.Config.Temperate_ServerAddress);
                if (param != null)
                {
                    boxStatus.humidity = param.Humidity.ToString();
                    boxStatus.temperature = param.Temperate.ToString();
                }
                lineOperation.boxStatus.Add(box.StoreID, boxStatus);
                if (box.storeStatus.Equals(StoreStatus.InStoreEnd) || box.storeStatus.Equals(StoreStatus.InStoreExecute)
                    || box.storeStatus.Equals(StoreStatus.OutStoreExecute) || box.storeStatus.Equals(StoreStatus.OutStoreBoxEnd))
                {
                    //isInOutMoveing = true;
                }
                if (!box.alarmType.Equals(StoreAlarmType.None))
                {
                    lineOperation.alarmList.Add(box.alarmInfo);
                }

            }
            PreMsg = lineOperation.msg;
            return lineOperation;
        }
        private static string AddMsg(string needSendMsg, string msg)
        {
            if (msg.Equals(""))
            {
                return needSendMsg;
            }
            if (needSendMsg.Equals(""))
            {
                needSendMsg = msg;
            }
            else
            {
                needSendMsg = needSendMsg + "  。" + msg;
            }
            return needSendMsg;
        }
        public static void SendLineStatus(int sStoreId, string sPosId, StoreStatus storeStatus)
        {
            DateTime time = DateTime.Now;
            //构建发送给服务器的对象
            Operation lineOperation = getLineBoxStatus(sStoreId, sPosId, storeStatus);

            string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
            Operation resultOperation = HttpHelper.Post(StoreManager.GetPostApi(server), lineOperation, false);
            //发送状态信息到服务器 
            if (resultOperation == null || (resultOperation.op != 2))
            {
                //判断服务端是否返回出库操作
                return;
            }

            Dictionary<string, string> data = resultOperation.data;
            if (data != null && data.ContainsKey(ParamDefine.posId)
                   && data.ContainsKey(ParamDefine.plateH) && data.ContainsKey(ParamDefine.plateW))
            {
                string posIdStr = data[ParamDefine.posId];
                string plateWStr = data[ParamDefine.plateW];
                string plateHStr = data[ParamDefine.plateH];

                LogUtil.info(LOGGER, "收到服务器出库消息:poaIs=" + posIdStr + ",platew=" + plateWStr + ",plateh=" + plateHStr);
                char splitChar = '|';
                string[] posIdArray = posIdStr.Split(splitChar);
                string[] plateWArray = plateWStr.Split(splitChar);
                string[] plateHArray = plateHStr.Split(splitChar);
                int index = -1;
                foreach (string posId in posIdArray)
                {
                    index++;
                    string plateW = plateWArray[index];
                    string plateH = plateHArray[index];
                    string[] posArray = posId.Split('#');
                    if (posArray.Length != 2)
                    {
                        WarnMsg = StoreName + "出库格式错误:库位号【" + posId + "】";
                        LogUtil.error(LOGGER, "收到服务器出库命令:库位号【" + posId + "】格式错误");
                        continue;
                    }
                    int storeId = int.Parse(posArray[0]);

                    //根据发送的posId获取位置列表
                    DoublePosition position = CSVPositionReader<DoublePosition>.GetPositon(posId);
                    if (position == null || !(StoreBoxMap.ContainsKey(storeId)))
                    {
                        //出入库没有找到服务器发送的库位,需要打印日志方便查询原因
                        WarnMsg = StoreName + "出库未找库位:【" + posId + "】";
                        LogUtil.error(LOGGER, "收到服务器出库命令:未找到【" + posId + "】的库位信息");
                        continue;
                    }
                    //找到对应的料仓出库
                    DB_LA_BoxBean boxBean = StoreBoxMap[storeId];

                    //TODO:判断BOX是否处于可以入库状态,如果调试或急停中,需要返回给服务器; 
                    if (boxBean.CanReviceOutStore(posId))
                    {
                        //如果当前正在出入库中,需要记录下来,等待空闲时执行
                        //判断是否已经收过此出库信息
                        bool isReviceInfo = boxBean.IsReviceOutPosId(posId);
                        if (!isReviceInfo)
                        {
                            if (boxBean.CanStarInOut())
                            {
                                LogUtil.info(LOGGER, boxBean + " 收到服务器出库命令:开始出库【" + posId + "】 ");
                                boxBean.StartOutStoreMove(new InOutStoreParam("", posId, plateH, plateW));
                            }
                            else
                            {
                                //把当前出库信息与Store绑定
                                FixtureCodeInfo code = new FixtureCodeInfo(0, "", posId, plateW, plateH);
                                LogUtil.info(LOGGER, boxBean + " 收到服务器出库命令:需要出库【" + posId + "】,写入排队列表中等待 !");
                                boxBean.AddWaitOutInfo(code);
                            }
                        }
                        else
                        {
                            LogUtil.info(LOGGER, boxBean.StoreName + " 收到服务器出库命令 出库库位重复:【" + posId + "】 出库失败 !");
                        }
                    }
                }
            }
            TimeSpan span = DateTime.Now - time;
            if (span.TotalMilliseconds > 10)
            {
                LogUtil.info(StoreName + "执行TimerProcess 共处理了【" + span.TotalMilliseconds + "】毫秒");
            }
        }
        #endregion

    }
}