using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DBFactory;
using CommonClassLib;
using Microsoft.VisualBasic;
using System.Threading;
using CommonLib;
namespace WcfControlMonitorLib
{
    /// <summary>
    /// Creator:Richard.liu
    /// 任务拆分类
    /// 选取调度任务时以保障设备利用率最大化为原则:1、每个路径端头的入库站台要有一个任务;
    /// 2、每个堆垛机要有一个任务(以优先方式决定先执行哪种任务);
    /// 3、组合优先方式时,在调度任务完成时调整已拆分任务优先级为最高级9,进行优先执行。
    /// 
    /// 把调度任务分解成多个监控搬运任务
    /// 分解原则:把一个调度任务按照出入库路径的设备索引排出多个步骤的设备指令
    /// </summary>


    //20091107增加对五个调度策略SelectTask_ManPRI();SelectTask_OnlyIn();SelectTask_InSubjoinOut();
    //SelectTask_OnlyOut();SelectTask_OutSubjoinIn();的优先级相同时根据下达时间的升序执行order by FTASKLEVEL desc,FBEGTIME asc

    public static  class CDisassembleTask
    {
        public static event CDataSourceChangeEventHandler DataChange;
        public static void OnDataChange(object sender, CDataChangeEventArgs e)
        {
            if (DataChange != null)
            {
                DataChange(sender, e);
            }
        }
        public static event RefreshMonitorEventHandler RefreshMonitor;
        public static void OnRefreshMonitor(RefreshMonitorEventArgs e)
        {
            if (RefreshMonitor != null)
            {
                RefreshMonitor(e);
            }
        }
        static string stackUcode = "35000,35001,35002,35003,45001";
        static StringBuilder sql = new StringBuilder();
        static Thread mythread;
        static bool exitThread = false;//20091107
        private static void BeginListen()
        {//20091107
            while (!exitThread )
            {
                try
                {
                    MyTaskIntoSteps();

                }
                catch (Exception ex)
                {
                    DisassembleTaskError =ex.StackTrace+ ex.Message;
                }
            }
        }
        public static void EndListen()
        {//20091107
            exitThread = true;

            if (mythread != null)
            {

                dbo.Close();
                
                mythread.Abort();
                mythread = null;
            }
        }
        public static void StartListen()
        {
            exitThread = false;
            mythread = new Thread(new ThreadStart(BeginListen));
            mythread.IsBackground = true;
            mythread.Start();
        }
        static string _DisassembleTaskError="";
        public static string DisassembleTaskError
        {
            get { return _DisassembleTaskError; }
            set { _DisassembleTaskError = value;
            RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", DisassembleTaskError);
            OnRefreshMonitor(rme);
            }
        }
        static DBOperator dbo =new DBOperator();//20130926
        static DBOperator dboM = CStaticClass.dboM; //20130510
        static CCommonFunction ccf = new CCommonFunction();

        static  Model.MDevice devinfo;

        /// <summary>
        /// 拆分调度任务,不包括手工任务(直接生成存入调度T_Monitor_Task表)
        /// </summary>
        public static void MyTaskIntoSteps()
        {
            try
            {
                int Pri = ccf.SelectTaskPriPrecept();
                switch (Pri)
                {
                    case 0://按照调度任务优先级
                        SelectTask_ManPRI();
                        break;
                    case 1://单纯入库优先
                        SelectTask_OnlyIn();
                        break;
                    case 12://入库优先携带附带运行条件的出库任务
                        SelectTask_InSubjoinOut();
                        break;
                    case 2://单纯出库优先
                        SelectTask_OnlyOut();
                        break;
                    case 21://出库优先携带附带运行条件的入库任务
                        SelectTask_OutSubjoinIn();
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.MyTaskIntoSteps时发生错误:" +ex.StackTrace+ ex.Message;
            }
        }
        /// <summary>
        /// 按照调度任务优先级拆分任务
        /// </summary>
        static void SelectTask_ManPRI()
        {
            DataView dvM = new DataView();
            DataView dv = new DataView();
            DataView dv1 = new DataView();
            try
            {
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("select * from T_Manage_Task where ").Append(
                    " FIntoStepOK='0' order by FTASKLEVEL desc,F_ManageTaskKindIndex asc,FBEGTIME asc, FID asc ");

                dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                dv = new DataView();//20091107
                dv1 = new DataView();//20091107
                Model.MDevice deviceInfo;
                for (int i = 0; i < dvM.Count; i++)
                {


                    if (dvM[i]["FCONTROLTASKTYPE"].ToString() == "1")
                    //如果是入库任务:判断起点设备是否有调度命令在执行或者被锁定
                    {
                        //sql = "SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task" +
                        //    " where F_ManageTASKKINDINDEX=" + dvM[i]["F_ManageTASKKINDINDEX"] + " and F_DeviceIndex=" + dvM[i]["FSTARTDEVICE"] + " and F_Status>=1";
                        //dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
                        //if (dv.Count > 0)
                        //{
                        //    continue;
                        //}
                        //被锁定
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dvM[i]["FSTARTDEVICE"]).Append(" and F_LockedState>0");

                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;//20091107
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                    }

                    if (dvM[i]["FCONTROLTASKTYPE"].ToString() == "2")
                    //如果是出库任务:判断堆垛机是否有调度命令在执行或者被锁定
                    {
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task").Append(
                            " where F_ManageTASKKINDINDEX=").Append(dvM[i]["F_ManageTASKKINDINDEX"]).Append(
                            " and F_DeviceIndex=").Append(dvM[i]["FSTACK"]).Append(" and F_Status>0 ");

                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            continue;
                        }
                        //被锁定20091009
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dvM[i]["FSTACK"]).Append(" and (F_LockedState>0 or F_ManTaskReserve>0)");

                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;//20091107
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                        //20091128
                        //判断将要被拆分的出库任务是否在已拆分的队列中有终点相同的出库任务,没有就找一个拆分
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dvM[i]["FENDDEVICE"]).Append(") and (FSTACK = ").Append(
                            dvM[i]["FSTACK"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc,FBEGTIME asc");

                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                        //20101028增加出库关联
                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvM[i]["FSTACK"]));
                        if (deviceInfo != null)
                        {
                            if (deviceInfo.IfCorrelDoubleFork == "1")
                            {
                                GetOutDoubleForkTask(dvM[i]);
                            }
                        }
                    }
                    //选择最短调度路径并且判断此路径上是否有设备发生故障
                    int routeid = MinRouteID(Convert.ToInt32(dvM[i]["FSTARTDEVICE"]), Convert.ToInt32(dvM[i]["FENDDEVICE"]), dvM[i]["FUseAwayFork"]);
                    if (routeid == -1)
                    {
                        if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                        {//20091107
                            GetUsableDestination(dvM[i]);
                        }
                        continue;
                    }


                    if (CreateMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt64(dvM[i]["FID"]), routeid, dvM[i], 0) > 0)
                    {
                        //分解完成,修改T_Manage_Task表FIntoStepOK=1
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dvM[i]["FID"]
                        ).Append(" and F_ManageTaskKindIndex=").Append(dvM[i]["F_ManageTaskKindIndex"]);
                        dbo.ExceSQL(sql.ToString());
                        //20100323
                        //20101028增加出库关联
                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvM[i]["FSTACK"]));
                        if (deviceInfo != null)
                        {
                            if (deviceInfo.IfCorrelDoubleFork == "1")
                            {
                                CreateRelativeMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt32(dvM[i]["FID"]));
                            }
                        }
                    }

                }

            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.SelectTask_ManPRI时发生错误:" +ex.StackTrace+ ex.Message;
            }
            finally
            {
                dvM.Dispose() ;
                dv.Dispose();
                dv1.Dispose();
            }
        }
        /// <summary>
        /// 单纯拆分入库任务,没有入库任务时再拆分出库任务
        /// </summary>
        static void SelectTask_OnlyIn()
        {
            DataView dvM = new DataView(); DataView dv = new DataView(); DataView dv1 = new DataView();
            try
            {
                Model.MDevice deviceInfo;
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("select * from T_Manage_Task where ").Append(
                    " FIntoStepOK='0' order by FCONTROLTASKTYPE asc, F_ManageTaskKindIndex asc,FBEGTIME asc, FID asc ");

                dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                for (int i = 0; i < dvM.Count; i++)
                {


                    if (dvM[i]["FCONTROLTASKTYPE"].ToString() == "1")
                    //如果是入库任务:判断起点设备是否有调度命令在执行或者被锁定
                    {
                        //sql = "SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task" +
                        //    " where F_ManageTASKKINDINDEX=" + dvM[i]["F_ManageTASKKINDINDEX"] + " and F_DeviceIndex=" + dvM[i]["FSTARTDEVICE"] + " and F_Status>0";
                        //dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
                        //if (dv.Count > 0)
                        //{
                        //    continue;
                        //}
                        //被锁定
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dvM[i]["FSTARTDEVICE"]).Append(" and F_LockedState>0");

                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                    }

                    if (dvM[i]["FCONTROLTASKTYPE"].ToString() == "2")
                    //如果是出库任务:判断堆垛机是否有调度命令在执行或者被锁定
                    {
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task").Append(
                            " where F_ManageTASKKINDINDEX=").Append(dvM[i]["F_ManageTASKKINDINDEX"]).Append(
                            " and F_DeviceIndex=").Append(dvM[i]["FSTACK"]).Append(" and F_Status>0");

                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            continue;
                        }
                        //被锁定20091009
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dvM[i]["FSTACK"]).Append(" and (F_LockedState>0 or F_ManTaskReserve>0)");

                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                        ////20091128
                        //判断将要被拆分的出库任务是否在已拆分的队列中有终点相同的出库任务,没有就找一个拆分
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dvM[i]["FENDDEVICE"]).Append(") and (FSTACK = ").Append(
                            dvM[i]["FSTACK"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc,FBEGTIME asc");
                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                        //20101028增加出库关联
                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvM[i]["FSTACK"]));
                        if (deviceInfo != null)
                        {
                            if (deviceInfo.IfCorrelDoubleFork == "1")
                            {
                                GetOutDoubleForkTask(dvM[i]);
                            }
                        }
                    }
                    //选择最短调度路径并且判断此路径上是否有设备发生故障
                    int routeid = MinRouteID(Convert.ToInt32(dvM[i]["FSTARTDEVICE"]), Convert.ToInt32(dvM[i]["FENDDEVICE"]), dvM[i]["FUseAwayFork"]);
                    if (routeid == -1)
                    {
                        if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                        {//20091107
                            GetUsableDestination(dvM[i]);
                        }
                        continue;
                    }


                    if (CreateMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt64(dvM[i]["FID"]), routeid, dvM[i], 0) > 0)
                    {
                        //分解完成,修改T_Manage_Task表FIntoStepOK=1
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dvM[i]["FID"]
                        ).Append(" and F_ManageTaskKindIndex=").Append(dvM[i]["F_ManageTaskKindIndex"]);
                        dbo.ExceSQL(sql.ToString());
                        //20100323
                        //20101028增加出库关联
                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvM[i]["FSTACK"]));
                        if (deviceInfo != null)
                        {
                            if (deviceInfo.IfCorrelDoubleFork == "1")
                            {
                                CreateRelativeMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt32(dvM[i]["FID"]));
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.SelectTask_OnlyIn时发生错误:" +ex.StackTrace+ ex.Message;
            }
            finally
            {
                dv.Dispose();
                dv1.Dispose();
                dvM.Dispose();
            }
        }
        /// <summary>
        /// 入库优先附带符合运行条件的出库任务
        /// 具体实现:判断T_Manage_Task表中是否有此堆垛机任务正在执行:如果没有,则挑选一个入库任务;
        /// 如果有堆垛机正在执行入库任务,并且队列中没有等待执行的堆垛机出库任务,挑选一个此堆垛机的出库任务优先级设为9;
        /// 
        /// </summary>
        static void SelectTask_InSubjoinOut()
        {
            DataView dvM = new DataView();
            DataView dv = new DataView();//20091107
            DataView dv12 = new DataView();
            DataView dvv = new DataView();
            DataView dv0 = new DataView();
            DataView dv1 = new DataView();
            DataView dv00 = new DataView();
            try
            {
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("SELECT F_StackIndex FROM T_Base_StackInfo");

                dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                Model.MDevice deviceInfo;
                int routeid = -1;
                for (int i = 0; i < dvM.Count; i++)
                {
                    //首先选择此堆垛机所有入库任务,判断那个能执行
                    #region 首先选择此堆垛机所有入库任务,判断那个能执行
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTATUS = 0) AND (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 1) AND (FIntoStepOK = '0') order by FTASKLEVEL desc,FBEGTIME asc");

                    dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;//20091107
                    for (int j = 0; j < dv.Count; j++)//有这个堆垛机的入库任务等待拆分
                    {
                        //选择最短调度路径并且判断此路径上是否有设备发生故障
                        routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"]);
                        if (routeid == -1)
                        {
                            if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                            {//20091107
                                GetUsableDestination(dv[j]);
                            }
                            continue;
                        }
                        if (CreateMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[j]["FID"]), routeid, dv[j], 0) > 0)
                        {
                            //分解完成,修改T_Manage_Task表FIntoStepOK=1
                            //20101124
                            sql.Remove(0, sql.Length);
                            sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dv[j]["FID"]
                           ).Append(" and F_ManageTaskKindIndex=").Append(dv[j]["F_ManageTaskKindIndex"]);
                            dbo.ExceSQL(sql.ToString());
                            //20100323
                            //20101028增加出库关联
                            deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[j]["FSTACK"]));
                            if (deviceInfo != null)
                            {
                                if (deviceInfo.IfCorrelDoubleFork == "1")
                                {
                                    CreateRelativeMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[j]["FID"]));
                                }
                            }
                        }
                    }

                    #endregion

                    #region 然后给正在执行的入库堆垛机任务配上一个出库任务


                    //然后给正在执行的入库堆垛机任务配上一个出库任务 
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT T_Monitor_Task.F_MonitorIndex FROM T_Monitor_Task ,T_Manage_Task WHERE ").Append(
                        " (T_Monitor_Task.F_ManageTaskIndex = T_Manage_Task.FID AND ").Append(
                        " T_Monitor_Task.F_ManageTASKKINDINDEX = T_Manage_Task.F_ManageTaskKindIndex)").Append(
                        " And  (T_Monitor_Task.F_Status = 1 OR T_Monitor_Task.F_Status = 2) AND  F_DeviceCommandIndex=5 and ").Append(
                        "(T_Monitor_Task.F_DeviceIndex = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (T_Manage_Task.FCONTROLTASKTYPE = 1)");

                    dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv.Count > 0)//有这个堆垛机的入库任务在执行
                    {
                        #region 有这个堆垛机的入库任务在执行准备携带出库任务
                        //20091013
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(
                            ") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc,FBEGTIME asc");

                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count <= 0)//没有此堆垛机的出库任务在排队
                        {
                            #region 挑选此堆垛机的出库任务

                            //20101124
                            sql.Remove(0, sql.Length);
                            sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') order by FTASKLEVEL desc,FBEGTIME asc");

                            dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv.Count > 0)//挑选此堆垛机的出库任务
                            {

                                routeid = MinRouteID(Convert.ToInt32(dv[0]["FSTARTDEVICE"]), Convert.ToInt32(dv[0]["FENDDEVICE"]), dv[0]["FUseAwayFork"]);
                                if (routeid == -1)
                                {
                                    if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                                    {//20091107
                                        GetUsableDestination(dv[0]);
                                    }
                                    continue;
                                }
                                //20101028增加出库关联
                                deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[0]["FSTACK"]));
                                if (deviceInfo != null)
                                {
                                    if (deviceInfo.IfCorrelDoubleFork == "1")
                                    {
                                        GetOutDoubleForkTask(dv[0]);
                                    }
                                }
                                if (CreateMonitor(Convert.ToInt32(dv[0]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[0]["FID"]), routeid, dv[0], 0) > 0)
                                {
                                    //分解完成,修改T_Manage_Task表FIntoStepOK=1
                                    int level = Convert.ToInt32(dv[0]["FTASKLEVEL"]) == 100 ? 100 : 9;
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update T_Manage_Task set FIntoStepOK='1',FTASKLEVEL=").Append(level).Append(" where  FID=").Append(dv[0]["FID"]
                                    ).Append(" and F_ManageTaskKindIndex=").Append(dv[0]["F_ManageTaskKindIndex"]);
                                    dbo.ExceSQL(sql.ToString());
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update dbo.T_Monitor_Task set F_MonitorTaskLevel=9 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                                        dv[0]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dv[0]["FID"]);
                                    dbo.ExceSQL(sql.ToString());
                                    //20100323
                                    //20101028增加出库关联
                                    if (deviceInfo != null)
                                    {
                                        if (deviceInfo.IfCorrelDoubleFork == "1")
                                        {
                                            CreateRelativeMonitor(Convert.ToInt32(dv[0]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[0]["FID"]));
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {//20091014
                            //20101124
                            sql.Remove(0, sql.Length);
                            //20130710sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(
                            //    ") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') order by FTASKLEVEL desc,FBEGTIME asc");
                            sql.Append("SELECT MIN(FID) AS FID,FTASKLEVEL,F_ManageTaskKindIndex, FENDDEVICE, FSTARTDEVICE, FSTACK,  FUseAwayFork,FENDUCODE FROM T_Manage_Task WHERE (FSTACK = ")
                                .Append(dvM[i]["F_StackIndex"]).Append(")  AND (FSTATUS = 0) AND  (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') GROUP BY F_ManageTaskKindIndex,FENDDEVICE,FTASKLEVEL,FSTARTDEVICE,FSTACK, FUseAwayFork,FENDUCODE ORDER BY FTASKLEVEL DESC");

                            dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv.Count > 0)//挑选此堆垛机的出库任务
                            {
                                //20091102
                                for (int j = 0; j < dv.Count; j++)
                                {
                                    if (stackUcode.IndexOf(dvM[i]["F_StackIndex"].ToString()) >= 0)
                                    {//20120309 有此堆垛机的出库任务而且FENDUCODE终点相同在排队就不拆分了
                                        sql.Remove(0, sql.Length);
                                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDUCODE=").Append(dv[j]["FENDUCODE"]).Append(
                                            ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");

                                    }
                                    else
                                    {//20110110有此堆垛机的出库任务而且终点相同在排队就不拆分了
                                        //20101124
                                        sql.Remove(0, sql.Length);
                                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dv[j]["FENDDEVICE"]).Append(
                                            ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");

                                    }
                                    dv12 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                    if (dv12.Count > 0)
                                    {
                                        continue;
                                    }

                                    routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"]);
                                    if (routeid == -1)
                                    {
                                        if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                                        {//20091107
                                            GetUsableDestination(dv[j]);
                                        }
                                        continue;
                                    }
                                    //20101028增加出库关联
                                    deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[j]["FSTACK"]));
                                    if (deviceInfo != null)
                                    {
                                        if (deviceInfo.IfCorrelDoubleFork == "1")
                                        {
                                            GetOutDoubleForkTask(dv[j]);
                                        }
                                    }
                                    if (CreateMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[j]["FID"]), routeid, dv[j], 0) > 0)
                                    {
                                        //分解完成,修改T_Manage_Task表FIntoStepOK=1
                                        int level = Convert.ToInt32(dv[j]["FTASKLEVEL"]) == 100 ? 100 : 9;
                                        //20101124
                                        sql.Remove(0, sql.Length);
                                        sql.Append("update T_Manage_Task set FIntoStepOK='1',FTASKLEVEL=").Append(level).Append(" where  FID=").Append(dv[j]["FID"]
                                        ).Append(" and F_ManageTaskKindIndex=").Append(dv[j]["F_ManageTaskKindIndex"]);
                                        dbo.ExceSQL(sql.ToString());
                                        //20101124
                                        sql.Remove(0, sql.Length);
                                        sql.Append("update T_Monitor_Task set F_MonitorTaskLevel=9 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                                            dv[j]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dv[j]["FID"]);
                                        dbo.ExceSQL(sql.ToString());
                                        //20100323
                                        //20101028增加出库关联
                                        if (deviceInfo != null)
                                        {
                                            if (deviceInfo.IfCorrelDoubleFork == "1")
                                            {
                                                CreateRelativeMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[j]["FID"]));
                                            }
                                        }
                                    }

                                }
                            }
                            else
                            {
                                //20091107
                                //20101124
                                sql.Remove(0, sql.Length);
                                sql.Append("select * from T_Manage_Task  WHERE (FSTACK = ").Append(
                                    dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0)");
                                dvv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                for (int aa = 0; aa < dvv.Count; aa++)
                                {
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update T_Manage_Task set FTASKLEVEL=9 WHERE (FTASKLEVEL<>100) and  F_ManageTASKKINDINDEX=").Append(
                                                dvv[aa]["F_ManageTaskKindIndex"]).Append(" and FID=").Append(dvv[aa]["FID"]);
                                    dbo.ExceSQL(sql.ToString());
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update T_Monitor_Task set F_MonitorTaskLevel=9 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                                                dvv[aa]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dvv[aa]["FID"]);
                                    dbo.ExceSQL(sql.ToString());
                                }
                            }
                        }
                        #endregion
                    }
                    else//没有此堆垛机任务
                    {
                        #region 没有此堆垛机任务
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FIntoStepOK = '0')  order by FTASKLEVEL desc, FCONTROLTASKTYPE asc,FBEGTIME asc");

                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        //20091102
                        for (int j = 0; j < dv.Count; j++)
                        //if (dv.Count > 0)//随便挑选一个此堆垛机的任务
                        {
                            if (dv[j]["FCONTROLTASKTYPE"].ToString() == "2")
                            //如果是出库任务:判断堆垛机是否有调度命令在执行或者被锁定
                            {
                                if (stackUcode.IndexOf(dvM[i]["F_StackIndex"].ToString()) >= 0)
                                {//20120309 有此堆垛机的出库任务而且FENDUCODE终点相同在排队就不拆分了
                                    sql.Remove(0, sql.Length);
                                    sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDUCODE=").Append(dv[j]["FENDUCODE"]).Append(
                                        ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");

                                }
                                else
                                {//20110110有此堆垛机的出库任务而且终点相同在排队就不拆分了
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dv[j]["FENDDEVICE"]).Append(
                                        ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");

                                }
                                dv12 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                if (dv12.Count > 0)
                                {
                                    continue;
                                }
                                //20101124
                                sql.Remove(0, sql.Length);
                                sql.Append("SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task").Append(
                                    " where F_DeviceIndex=").Append(dv[j]["FSTACK"]).Append(" and F_Status>0 ");
                                dv0 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;//20091107
                                if (dv0.Count > 0)
                                {
                                    continue;
                                }
                                //被锁定20091009
                                //20101124
                                sql.Remove(0, sql.Length);
                                sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dv[j]["FSTACK"]).Append(" and (F_LockedState>0 or F_ManTaskReserve>0)");

                                dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;//220091007
                                if (dv1.Count > 0)
                                {
                                    continue;
                                }
                                //20101028增加出库关联

                                deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[j]["FSTACK"]));
                                if (deviceInfo != null)
                                {
                                    if (deviceInfo.IfCorrelDoubleFork == "1")
                                    {
                                        GetOutDoubleForkTask(dv[j]);
                                    }
                                }
                            }
                            routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"]);
                            if (routeid == -1)
                            {
                                if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                                {//20091107
                                    GetUsableDestination(dv[j]);
                                }
                                continue;
                            }
                            if (CreateMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[j]["FID"]), routeid, dv[j], 0) > 0)
                            {
                                //分解完成,修改T_Manage_Task表FIntoStepOK=1
                                //20101124
                                sql.Remove(0, sql.Length);
                                sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dv[j]["FID"]
                                ).Append(" and F_ManageTaskKindIndex=").Append(dv[j]["F_ManageTaskKindIndex"]);
                                dbo.ExceSQL(sql.ToString());
                                //20100323
                                //20101028增加出库关联
                                deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[j]["FSTACK"]));
                                if (deviceInfo != null)
                                {
                                    if (deviceInfo.IfCorrelDoubleFork == "1")
                                    {
                                        CreateRelativeMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[j]["FID"]));
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                    #endregion
                }
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("SELECT * FROM T_Manage_Task WHERE ((FSTACK <= 0) or (FCONTROLTASKTYPE=3)) AND (FIntoStepOK = '0')  order by FTASKLEVEL desc, FCONTROLTASKTYPE asc,FBEGTIME asc");

                dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                for (int i = 0; i < dvM.Count; i++)//挑选没有堆垛机参与的任务
                {
                    //选择最短调度路径并且判断此路径上是否有设备发生故障
                    routeid = MinRouteID(Convert.ToInt32(dvM[i]["FSTARTDEVICE"]), Convert.ToInt32(dvM[i]["FENDDEVICE"]), dvM[i]["FUseAwayFork"]);
                    if (routeid == -1)
                    {
                        if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                        {//20091107
                            GetUsableDestination(dvM[i]);
                        }
                        continue;
                    }
                    if (CreateMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt64(dvM[i]["FID"]), routeid, dvM[i], 0) > 0)
                    {
                        //分解完成,修改T_Manage_Task表FIntoStepOK=1
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dvM[i]["FID"]
                        ).Append(" and F_ManageTaskKindIndex=").Append(dvM[i]["F_ManageTaskKindIndex"]);
                        dbo.ExceSQL(sql.ToString());
                        //20100323
                        //20101028增加出库关联
                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvM[i]["FSTACK"]));
                        if (deviceInfo != null)
                        {
                            if (deviceInfo.IfCorrelDoubleFork == "1")
                            {
                                CreateRelativeMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt32(dvM[i]["FID"]));
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.SelectTask_InSubjoinOut时发生错误:" +ex.StackTrace+ ex.Message;
            }
            finally
            {
                //20091107
                dvM.Dispose();
                dv.Dispose();
                dv12.Dispose();
                dvv.Dispose();
                dv0.Dispose();
                dv1.Dispose();
                dv00.Dispose();
            }
        }
        /// <summary>
        /// 单纯拆分出库任务,没有出库任务时再拆分入库任务
        /// </summary>
        static void SelectTask_OnlyOut()
        {
            DataView dvM = new DataView(); DataView dv = new DataView(); DataView dv1 = new DataView();
            try
            {

                
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("select * from T_Manage_Task where ").Append(
                    " FIntoStepOK='0' order by FCONTROLTASKTYPE decs, F_ManageTaskKindIndex asc,FBEGTIME asc, FID asc ");
                dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                Model.MDevice deviceInfo;
                for (int i = 0; i < dvM.Count; i++)
                {


                    if (dvM[i]["FCONTROLTASKTYPE"].ToString() == "1")
                    //如果是入库任务:判断起点设备是否有调度命令在执行或者被锁定
                    {
                        //sql = "SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task" +
                        //    " where F_ManageTASKKINDINDEX=" + dvM[i]["F_ManageTASKKINDINDEX"] + " and F_DeviceIndex=" + dvM[i]["FSTARTDEVICE"] + " and F_Status>0";
                        //dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
                        //if (dv.Count > 0)
                        //{
                        //    continue;
                        //}
                        //被锁定
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dvM[i]["FSTARTDEVICE"]).Append(" and F_LockedState>0");
                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                    }

                    if (dvM[i]["FCONTROLTASKTYPE"].ToString() == "2")
                    //如果是出库任务:判断堆垛机是否有调度命令在执行或者被锁定
                    {
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task").Append(
                            " where F_ManageTASKKINDINDEX=").Append(dvM[i]["F_ManageTASKKINDINDEX"]
                            ).Append(" and F_DeviceIndex=").Append(dvM[i]["FSTACK"]).Append(" and F_Status>0 ");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            continue;
                        }
                        //被锁定20091009
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dvM[i]["FSTACK"]).Append(" and (F_LockedState>0 or F_ManTaskReserve>0)");
                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                        //20091128
                        //判断将要被拆分的出库任务是否在已拆分的队列中有终点相同的出库任务,没有就找一个拆分
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dvM[i]["FENDDEVICE"]).Append(") and (FSTACK = ").Append(
                            dvM[i]["FSTACK"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc,FBEGTIME asc");
                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                        //20101028增加出库关联
                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvM[i]["FSTACK"]));
                        if (deviceInfo != null)
                        {
                            if (deviceInfo.IfCorrelDoubleFork == "1")
                            {
                                GetOutDoubleForkTask(dvM[i]);
                            }
                        }
                    }
                    //选择最短调度路径并且判断此路径上是否有设备发生故障
                    int routeid = MinRouteID(Convert.ToInt32(dvM[i]["FSTARTDEVICE"]), Convert.ToInt32(dvM[i]["FENDDEVICE"]), dvM[i]["FUseAwayFork"]);
                    if (routeid == -1)
                    {
                        if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                        {//20091107
                            GetUsableDestination(dvM[i]);
                        }
                        continue;
                    }
                    if (CreateMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt64(dvM[i]["FID"]), routeid, dvM[i], 0) > 0)
                    {
                        //分解完成,修改T_Manage_Task表FIntoStepOK=1
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dvM[i]["FID"]
                        ).Append(" and F_ManageTaskKindIndex=").Append(dvM[i]["F_ManageTaskKindIndex"]);
                        dbo.ExceSQL(sql.ToString());
                        //20100323
                        //20101028增加出库关联
                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dvM[i]["FSTACK"]));
                        if (deviceInfo.IfCorrelDoubleFork == "1")
                        {
                            CreateRelativeMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt32(dvM[i]["FID"]));
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.SelectTask_OnlyOut时发生错误:" +ex.StackTrace+ ex.Message;
            }
            finally
            {
                dv.Dispose();
                dv1.Dispose();
                dvM.Dispose();
            }
        }
        /// <summary>
        /// 出库优先附带符合运行条件的入库任务
        /// 具体实现:在收到出库任务的完成时,挑选一个可以执行的入库任务把优先级设成最高9
        /// </summary>
        static void SelectTask_OutSubjoinIn()
        {
            DataView dvM = new DataView();
            DataView dv = new DataView();
            DataView dv112 = new DataView();
            DataView dv0 = new DataView();
            DataView dv1 = new DataView();
            try
            {
                Model.MDevice deviceInfo;
                //20101124
                int routeid = -1;
                sql.Remove(0, sql.Length);
                sql.Append("SELECT * FROM T_Manage_Task WHERE ((FSTACK <= 0) or (FCONTROLTASKTYPE=3) or (FCONTROLTASKTYPE=5) OR (FCONTROLTASKTYPE=6)) AND (FIntoStepOK = '0')  order by FTASKLEVEL desc, FCONTROLTASKTYPE asc,FBEGTIME asc");
                dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                for (int i = 0; i < dvM.Count; i++)//挑选没有堆垛机参与的任务
                {
                    //选择最短调度路径并且判断此路径上是否有设备发生故障
                    #region update by lyj20180423
                    // routeid = MinRouteID(Convert.ToInt32(dvM[i]["FSTARTDEVICE"]), Convert.ToInt32(dvM[i]["FENDDEVICE"]), dvM[i]["FUseAwayFork"]);
                    routeid = MinRouteID(Convert.ToInt32(dvM[i]["FSTARTDEVICE"]), Convert.ToInt32(dvM[i]["FENDDEVICE"]), dvM[i]["FUseAwayFork"], dvM[i]["FSTARTCELL"].ToString(), dvM[i]["FENDCELL"].ToString(), Convert.ToInt32(dvM[i]["FCONTROLTASKTYPE"]));
                    #endregion

                    if (routeid == -1)
                    {
                        if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                        {//20091107
                            GetUsableDestination(dvM[i]);
                        }
                        continue;
                    }
                    if (CreateMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt64(dvM[i]["FID"]), routeid, dvM[i], 0) > 0)
                    {
                        //分解完成,修改T_Manage_Task表FIntoStepOK=1
                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dvM[i]["FID"]
                        ).Append(" and F_ManageTaskKindIndex=").Append(dvM[i]["F_ManageTaskKindIndex"]);
                        dbo.ExceSQL(sql.ToString());

                        //晟碟 添加以下四行代码,移库优先级置为10,(因任务依次按优先级、拆分时间优先执行)
                        sql.Remove(0, sql.Length);
                        sql.Append("update T_Monitor_Task set F_MonitorTaskLevel=10 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                           dvM[i]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dvM[i]["FID"]);
                        dbo.ExceSQL(sql.ToString());
                        //20100323
                        CreateRelativeMonitor(Convert.ToInt32(dvM[i]["F_ManageTaskKindIndex"]), Convert.ToInt32(dvM[i]["FID"]));





                    }
                }
                sql.Remove(0, sql.Length);
                sql.Append("SELECT F_StackIndex FROM T_Base_StackInfo");
                dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                
                for (int i = 0; i < dvM.Count; i++)
                {//20090928
                    #region 首先选择此堆垛机所有入库任务,判断那个能执行
                  

                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTATUS = 0) AND (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 1) AND (FIntoStepOK = '0') order by FTASKLEVEL desc,FBEGTIME asc");
                    dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    for (int j = 0; j < dv.Count; j++)//有这个堆垛机的入库任务等待拆分
                    {
                        //选择最短调度路径并且判断此路径上是否有设备发生故障
                        #region update by lyj20180423
                        // routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"])
                        routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"], dv[j]["FSTARTCELL"].ToString(), dv[j]["FENDCELL"].ToString(), Convert.ToInt32(dv[j]["FCONTROLTASKTYPE"]));
                        #endregion
                        if (routeid == -1)
                        {
                            if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                            {//20091107
                                GetUsableDestination(dv[j]);  
                            }
                            continue;
                        }
                        if (CreateMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[j]["FID"]), routeid, dv[j], 0) > 0)
                        {
                            //分解完成,修改T_Manage_Task表FIntoStepOK=1
                            //20101124
                            sql.Remove(0, sql.Length);
                            sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dv[j]["FID"]
                            ).Append(" and F_ManageTaskKindIndex=").Append(dv[j]["F_ManageTaskKindIndex"]);
                            dbo.ExceSQL(sql.ToString());
                            //20100323
                            //20101028增加出库关联
                            deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[j]["FSTACK"]));
                            if (deviceInfo != null)
                            {
                                if (deviceInfo.IfCorrelDoubleFork == "1")
                                {
                                    CreateRelativeMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[j]["FID"]));
                                }
                            }
                        }
                    }

                    #endregion
                    //20091005
                    #region 然后给正在执行的入库堆垛机任务配上一个出库任务


                    //然后给正在执行的入库堆垛机任务配上一个出库任务 
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT T_Monitor_Task.F_MonitorIndex FROM T_Monitor_Task ,T_Manage_Task WHERE ").Append(
                        " (T_Monitor_Task.F_ManageTaskIndex = T_Manage_Task.FID AND ").Append(
                        " T_Monitor_Task.F_ManageTASKKINDINDEX = T_Manage_Task.F_ManageTaskKindIndex)").Append(
                        " And  (T_Monitor_Task.F_Status = 1 OR T_Monitor_Task.F_Status = 2) AND F_DeviceCommandIndex=5 and ").Append(
                        "(T_Monitor_Task.F_DeviceIndex = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (T_Manage_Task.FCONTROLTASKTYPE = 1)");
                    dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv.Count > 0)//有这个堆垛机的入库任务在执行
                    {
                        #region 有这个堆垛机的入库任务在执行

                        //20101124
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc,FBEGTIME asc");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count <= 0)//没有此堆垛机的出库任务在排队或者执行
                        {
                            #region 挑选此堆垛机的出库任务

                            //20101124
                            sql.Remove(0, sql.Length);
                            sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') order by FTASKLEVEL desc,FBEGTIME asc");
                            dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv.Count > 0)//挑选此堆垛机的出库任务
                            {
                                #region update by lyj20180423
                                // routeid = MinRouteID(Convert.ToInt32(dv[0]["FSTARTDEVICE"]), Convert.ToInt32(dv[0]["FENDDEVICE"]), dv[0]["FUseAwayFork"]);
                                routeid = MinRouteID(Convert.ToInt32(dv[0]["FSTARTDEVICE"]), Convert.ToInt32(dv[0]["FENDDEVICE"]), dv[0]["FUseAwayFork"], dv[0]["FSTARTCELL"].ToString(), dv[0]["FENDCELL"].ToString(), Convert.ToInt32(dv[0]["FCONTROLTASKTYPE"]));
                                #endregion
                               
                                if (routeid == -1)
                                {
                                    if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                                    {//20091107
                                        GetUsableDestination(dv[0]);
                                    }
                                    continue;
                                }
                                //20101028增加出库关联
                                deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[0]["FSTACK"]));
                                if (deviceInfo != null)
                                {
                                    if (deviceInfo.IfCorrelDoubleFork == "1")
                                    {
                                        GetOutDoubleForkTask(dv[0]);
                                    }
                                }
                                if (CreateMonitor(Convert.ToInt32(dv[0]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[0]["FID"]), routeid, dv[0], 0) > 0)
                                {
                                    //分解完成,修改T_Manage_Task表FIntoStepOK=1
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dv[0]["FID"]
                                    ).Append(" and F_ManageTaskKindIndex=").Append(dv[0]["F_ManageTaskKindIndex"]);
                                    dbo.ExceSQL(sql.ToString());
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update dbo.T_Monitor_Task set F_MonitorTaskLevel=9 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                                        dv[0]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dv[0]["FID"]);
                                    dbo.ExceSQL(sql.ToString());
                                    //20100323
                                    //20101028增加出库关联
                                    deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[0]["FSTACK"]));
                                    if (deviceInfo != null)
                                    {
                                        if (deviceInfo.IfCorrelDoubleFork == "1")
                                        {
                                            CreateRelativeMonitor(Convert.ToInt32(dv[0]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[0]["FID"]));
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            //20101124
                            sql.Remove(0, sql.Length);
                            //20130710sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(
                            //    ") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') order by FTASKLEVEL desc,FBEGTIME asc");
                            sql.Append("SELECT MIN(FID) AS FID,FTASKLEVEL,F_ManageTaskKindIndex, FENDDEVICE, FSTARTDEVICE, FSTACK,  FUseAwayFork,FENDUCODE,FStartLayer  FROM T_Manage_Task WHERE (FSTACK = ")
                                .Append(dvM[i]["F_StackIndex"]).Append(")  AND (FSTATUS = 0) AND  (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') GROUP BY F_ManageTaskKindIndex,FENDDEVICE,FTASKLEVEL,FSTARTDEVICE,FSTACK, FUseAwayFork,FENDUCODEF,StartLayer ORDER BY FTASKLEVEL DESC");

                            dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv.Count > 0)//挑选此堆垛机的出库任务
                            {

                                //20091102
                                for (int j = 0; j < dv.Count; j++)
                                {

                                    //sql.Append("SELECT F_StackIndex FROM T_Base_StackInfo");
                                    //dvM = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                    if (stackUcode.IndexOf(dvM[i]["F_StackIndex"].ToString()) >= 0)
                                    {//20120309 有此堆垛机的出库任务而且FENDUCODE终点相同在排队就不拆分了
                                        sql.Remove(0, sql.Length);
                                        sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDUCODE=").Append(dv[j]["FENDUCODE"]).Append(
                                            ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");
                                    }
                                    else
                                    {//20110110有此堆垛机的出库任务而且终点相同在排队就不拆分了
                                        //20101124
                                        sql.Remove(0, sql.Length);
                                        //sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dv[j]["FENDDEVICE"]).Append(
                                        //    ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");
                                        sql.Append("SELECT * FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dv[j]["FENDDEVICE"]).Append(
                                          ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') order by FTASKLEVEL desc");//晟碟同层出库拆分3条就不拆分
                                    }

                                    dv112 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                                    //晟碟根据终点取得限制的数量
                                    sql.Clear();
                                    sql.Append("SELECT * FROM T_Base_TaskCount2  WHERE  F_DeviceIndex_End = ").Append(Convert.ToInt32(dv[j]["FENDDEVICE"])).Append(" and F_DeviceIndex_Start =").Append( Convert.ToInt32(dv[j]["FSTARTDEVICE"]));
                                    dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                    if (dv1.Count > 0)
                                    {
                                        if (dv1[0]["F_StackIndex"].ToString() != dvM[i]["F_StackIndex"].ToString())
                                        {
                                            continue;
                                        }
                                        //晟碟
                                        if (dv112.Count >= Convert.ToInt32(dv1[0]["F_MaxCount"]))
                                        {
                                            continue;
                                        }
                                    }

                                    #region update by lyj20180423
                                    //  routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"]);
                                    routeid = MinRouteID(Convert.ToInt32(dv[0]["FSTARTDEVICE"]), Convert.ToInt32(dv[0]["FENDDEVICE"]), dv[0]["FUseAwayFork"], dv[0]["FSTARTCELL"].ToString(), dv[0]["FENDCELL"].ToString(), Convert.ToInt32(dv[0]["FCONTROLTASKTYPE"]));
                                    #endregion
                                       
                                        if (routeid == -1)
                                        {
                                            if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                                            {//20091107
                                                GetUsableDestination(dv[j]);
                                            }
                                            continue;
                                        }
                                        //20101028增加出库关联
                                        deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[j]["FSTACK"]));
                                        if (deviceInfo != null)
                                        {
                                            if (deviceInfo.IfCorrelDoubleFork == "1")
                                            {
                                                GetOutDoubleForkTask(dv[j]);
                                            }
                                        }
                                        if (CreateMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[j]["FID"]), routeid, dv[j], 0) > 0)
                                        {
                                            //分解完成,修改T_Manage_Task表FIntoStepOK=1
                                            int level = Convert.ToInt32(dv[j]["FTASKLEVEL"]) == 100 ? 100 : 9;
                                            //20101124
                                            sql.Remove(0, sql.Length);
                                            sql.Append("update T_Manage_Task set FIntoStepOK='1',FTASKLEVEL=").Append(level).Append(" where  FID=").Append(dv[j]["FID"]
                                            ).Append(" and F_ManageTaskKindIndex=").Append(dv[j]["F_ManageTaskKindIndex"]);
                                            dbo.ExceSQL(sql.ToString());
                                            //20101124
                                            sql.Remove(0, sql.Length);
                                            sql.Append("update T_Monitor_Task set F_MonitorTaskLevel=9 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                                                dv[j]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dv[j]["FID"]);
                                           dboM.ExceSQL(sql.ToString());

                                        #region   拆分成功后修改IOControl表对应任务的状态 lzm20250311
                                        sql.Remove(0, sql.Length);
                                        sql.Append("update IO_CONTROL set CONTROL_STATUS=10 where MANAGE_ID=").Append(dv[j]["FID"]);
                                        dbo.ExceSQL(sql.ToString());
                                        #endregion
                                        //20100323
                                        //20101028增加出库关联
                                        if (deviceInfo != null)
                                            {
                                                if (deviceInfo.IfCorrelDoubleFork == "1")
                                                {
                                                    CreateRelativeMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[j]["FID"]));
                                                }
                                            }
                                        }
                                    

                                }
                            }
                            else
                            {
                                //20101124
                                sql.Remove(0, sql.Length);
                                sql.Append("select * from T_Manage_Task  WHERE (FSTACK = ").Append(
                                    dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0)");
                                DataView dvv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                for (int aa = 0; aa < dvv.Count; aa++)
                                {
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update T_Manage_Task set FTASKLEVEL=9 WHERE (FTASKLEVEL<>100) and  F_ManageTASKKINDINDEX=").Append(
                                                dvv[aa]["F_ManageTaskKindIndex"]).Append(" and FID=").Append(dvv[aa]["FID"]);
                                    dbo.ExceSQL(sql.ToString());
                                    //20101124
                                    sql.Remove(0, sql.Length);
                                    sql.Append("update T_Monitor_Task set F_MonitorTaskLevel=9 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                                                dvv[aa]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dvv[aa]["FID"]);
                                    dbo.ExceSQL(sql.ToString());
                                }
                            }
                        }
                        #endregion
                    }
                    #endregion
                    //20091005
                    else
                    {

                        //首先选择此堆垛机一个出库任务
                        #region 首先选择此堆垛机一个出库任务

                        //判断堆垛机是否有调度命令在执行或者被锁定
                        //20130620
                        sql.Remove(0, sql.Length);

                        sql.Append("SELECT F_ManageTASKKINDINDEX, F_DeviceIndex, F_Status FROM T_Monitor_Task").Append(
                            " where F_DeviceIndex=").Append(dvM[i]["F_StackIndex"]).Append(" and F_Status>0 ");
                        dv0 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv0.Count > 0)
                        {
                            continue;
                        }
                        //被锁定20130620
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_DeviceIndex, F_LockedState FROM T_Base_Device where F_DeviceIndex=").Append(dvM[i]["F_StackIndex"]).Append(" and (F_LockedState>0 or F_ManTaskReserve>0)");
                        dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv1.Count > 0)
                        {
                            continue;
                        }
                        sql.Remove(0, sql.Length);
                        //20130710sql.Append("SELECT * FROM T_Manage_Task WHERE (FSTATUS = 0) AND (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') order by FTASKLEVEL desc,FBEGTIME asc");

                        #region update by lyj20180424 加入控制类型和起始cell
                        //sql.Append("SELECT MIN(FID) AS FID,FTASKLEVEL,F_ManageTaskKindIndex, FENDDEVICE, FSTARTDEVICE, FSTACK,  FUseAwayFork,FENDUCODE FROM T_Manage_Task WHERE (FSTACK = ")
                        //        .Append(dvM[i]["F_StackIndex"]).Append(")  AND (FSTATUS = 0) AND  (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') GROUP BY F_ManageTaskKindIndex,FENDDEVICE,FTASKLEVEL,FSTARTDEVICE,FSTACK, FUseAwayFork,FENDUCODE ORDER BY FTASKLEVEL DESC");

                        sql.Append("SELECT MIN(FID) AS FID,FTASKLEVEL,F_ManageTaskKindIndex, FENDDEVICE, FSTARTDEVICE, FSTACK,  FUseAwayFork,FENDUCODE,FCONTROLTASKTYPE,FSTARTCELL,FENDCELL,FStartLayer FROM T_Manage_Task WHERE (FSTACK = ")
                             .Append(dvM[i]["F_StackIndex"]).Append(")  AND (FSTATUS = 0) AND  (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '0') GROUP BY F_ManageTaskKindIndex,FENDDEVICE,FTASKLEVEL,FSTARTDEVICE,FSTACK, FUseAwayFork,FENDUCODE,FCONTROLTASKTYPE,FSTARTCELL,FENDCELL,FStartLayer ORDER BY FTASKLEVEL DESC, FID asc");
                        #endregion
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        for (int j = 0; j < dv.Count; j++)
                        //有这个堆垛机的出库任务等待拆分
                        {
                            if (stackUcode.IndexOf(dvM[i]["F_StackIndex"].ToString()) >= 0)
                            {//20120309 有此堆垛机的出库任务而且FENDUCODE终点相同在排队就不拆分了
                                sql.Remove(0, sql.Length);
                                sql.Append("SELECT FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FENDUCODE=").Append(dv[j]["FENDUCODE"]).Append(
                                    ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");

                            }
                            else
                            {//20110110有此堆垛机的出库任务而且终点相同在排队就不拆分了
                             //20101124

                                
                                sql.Remove(0, sql.Length);
                                //sql.Append("SELECT * FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dv[j]["FENDDEVICE"]).Append(
                                //    ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1') AND (FStatus = 0) order by FTASKLEVEL desc");
                                sql.Append("SELECT * FROM T_Manage_Task WHERE (FENDDEVICE=").Append(dv[j]["FENDDEVICE"]).Append(
                                    ") and (FSTACK = ").Append(dvM[i]["F_StackIndex"]).Append(") AND (FCONTROLTASKTYPE = 2) AND (FIntoStepOK = '1')  order by FTASKLEVEL desc");//晟碟同层出库拆分3条就不拆分

                            }
                            dv112 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                            sql.Clear();
                            sql.Append("SELECT * FROM T_Base_TaskCount2  WHERE  F_DeviceIndex_End = ").Append(Convert.ToInt32(dv[j]["FENDDEVICE"])).Append(" and F_DeviceIndex_Start =").Append(Convert.ToInt32(dv[j]["FSTARTDEVICE"]));
                            dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                            if (dv1.Count > 0)
                            {
                              
                                if (dv1[0]["F_StackIndex"].ToString() != dvM[i]["F_StackIndex"].ToString()) {
                                    continue;
                                }
                                //lzm
                                if (dv112.Count >= Convert.ToInt32(dv1[0]["F_MaxCount"]))
                                {
                                    continue;
                                }
                            }

                            //选择最短调度路径并且判断此路径上是否有设备发生故障
                            #region update by lyj20180423
                            //   routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"]);
                            routeid = MinRouteID(Convert.ToInt32(dv[j]["FSTARTDEVICE"]), Convert.ToInt32(dv[j]["FENDDEVICE"]), dv[j]["FUseAwayFork"], dv[j]["FSTARTCELL"].ToString(), dv[j]["FENDCELL"].ToString(), Convert.ToInt32(dv[j]["FCONTROLTASKTYPE"]));
                            #endregion
                           
                            if (routeid == -1)
                            {
                                if (CStaticClass.DeviceErrorAutoModifyRoutePath == "1")
                                {//20091107
                                    GetUsableDestination(dv[j]);
                                }
                                continue;
                            }
                            //20101028增加出库关联
                            deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(dv[j]["FSTACK"]));
                            if (deviceInfo != null)
                            {
                                if (deviceInfo.IfCorrelDoubleFork == "1")
                                {
                                    GetOutDoubleForkTask(dv[j]);
                                }
                            }
                            if (CreateMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt64(dv[j]["FID"]), routeid, dv[j], 0) > 0)
                            {
                                //分解完成,修改T_Manage_Task表FIntoStepOK=1
                                //20101124
                                sql.Remove(0, sql.Length);
                                sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dv[j]["FID"]
                                ).Append(" and F_ManageTaskKindIndex=").Append(dv[j]["F_ManageTaskKindIndex"]);
                                dbo.ExceSQL(sql.ToString());
                                //20161219 添加以下四行代码,出库任务优先级默认是9,解决出库任务不执行的问题(因任务依次按优先级、拆分时间优先执行)
                                sql.Remove(0, sql.Length);
                                sql.Append("update T_Monitor_Task set F_MonitorTaskLevel=9 where F_MonitorTaskLevel<>100 and F_ManageTASKKINDINDEX=").Append(
                                    dv[j]["F_ManageTaskKindIndex"]).Append(" and F_ManageTaskIndex=").Append(dv[j]["FID"]);
                                dbo.ExceSQL(sql.ToString());
                                //20161219 添加以上四行代码
                                //20100323
                                //20101028增加出库关联
                                if (deviceInfo != null)
                                {
                                    if (deviceInfo.IfCorrelDoubleFork == "1")
                                    {
                                        CreateRelativeMonitor(Convert.ToInt32(dv[j]["F_ManageTaskKindIndex"]), Convert.ToInt32(dv[j]["FID"]));
                                    }
                                }
                            }
                        }

                        #endregion
                    }


                }
                //20101124
                

            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.SelectTask_OutSubjoinIn时发生错误:" + ex.StackTrace + ex.Message;
            }
            finally
            {
                dv.Dispose();
                dv0.Dispose();
                dv1.Dispose();
                dv112.Dispose();
                dvM.Dispose();
            }
        }
        /// <summary>
        /// 根据调度任务的起点和终点设备索引查找路径最短的RouteIDSub
        /// </summary>
        /// <param name="startdevice">起点设备索引</param>
        /// <param name="enddevice">终点设备索引</param>
        /// <returns></returns>
        public static int MinRouteID(int startdevice,int enddevice)
        {
            DataView dv = new DataView(); DataView dvd = new DataView(); DataView dv1 = new DataView(); DataView dv2 = new DataView();
            List<double> minroute=new List<double>() ;
            try
            {
                int retrouteidsub =GetSameStartDeviceEnddeviceRouteID(startdevice, enddevice);
                if (retrouteidsub > 0)
                {
                    return retrouteidsub;
                }
                //选择最短调度路径并且判断此路径上是否有设备发生故障
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                    "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                    " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                    " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append(" and F_Status=1");
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                //0,routIDSub;1,路径最小值
                if (dv.Count == 0)
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
                for (int i = 0; i < dv.Count; i++)
                {
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT F_RouteIDSub, F_LockedState,T_Base_Device.F_DeviceIndex FROM T_Base_Device,T_Base_Route_Device WHERE ").Append(
                        " T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex  and (T_Base_Route_Device.F_RouteIDSub = ").Append(
                        dv[i]["F_RouteIDSub"]).Append(") AND  (T_Base_Device.F_LockedState = - 1 or T_Base_Device.F_ErrorCode>=30)");//20121203
                    dvd = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dvd.Count > 0)
                    {
                        int dvin = Convert.ToInt32(dvd[0]["F_DeviceIndex"]);
                        if ((dvin >= 35001 && dvin <= 35003) || (dvin == 45001))
                        {//20120207机器人地位无法取代,不判断是否被禁用

                        }
                        else
                        {
                            continue;
                        }
                    }

                    List<double> route = new List<double>();//0,routIDSub;1,步长;2,路径使用频率;3,路径设备任务数
                    //路径步长(设备总数)最短(权重0.3),路径使用频率最小(权重0.3)的路径,路径设备任务数(权重0.3)

                    route.Add(Convert.ToDouble(dv[i]["F_RouteIDSub"]));//RouteIDSub
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("select count(F_DeviceIndex) as steps from T_Base_Route_Device where F_RouteIDSub="
                        ).Append(dv[i]["F_RouteIDSub"]);
                    dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv1.Count > 0)
                    {
                        route.Add(Convert.ToDouble(dv1[0]["steps"]));//步长
                        //route.Add(0);
                    }
                    else
                    {
                        continue;
                    }
                    //路径使用频率:路径执行调度任务的数量
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(DISTINCT F_ManageTaskIndex) AS ManCount FROM T_Monitor_Task GROUP BY F_RouteID HAVING (F_RouteID = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        int a = -1;
                        if (int.TryParse(dv2[0]["ManCount"].ToString(), out a) == true)
                        {
                            route.Add(Convert.ToDouble(dv2[0]["ManCount"]));//路径使用频率
                        }
                        else
                        {
                            route.Add(0);//路径使用频率
                        }
                    }
                    else
                    {
                        route.Add(0);//路径使用频率
                    }
                    //路径设备任务数
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(T_Monitor_Task.F_MonitorIndex) AS mtask FROM T_Monitor_Task ,T_Base_Route_Device where  (T_Monitor_Task.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex) and  (T_Base_Route_Device.F_RouteIDSub = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        int a = -1;
                        if (int.TryParse(dv2[0]["mtask"].ToString(), out a) == true)
                        {
                            route.Add(Convert.ToDouble(dv2[0]["mtask"]));//路径设备任务数
                        }
                        else
                        {
                            route.Add(0);//路径设备任务数
                        }
                    }
                    else
                    {
                        route.Add(0);//路径设备任务数
                    }

                    //求最短路径((路径步长*0.3+路径使用频率*0.3+设备占有数*0.3)的最小值)
                    if (minroute.Count == 0)
                    {
                        minroute.Add(route[0]);
                        minroute.Add(route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                    }
                    else
                    {
                        if (minroute[1] > (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3))
                        {
                            minroute[0] = route[0];
                            minroute[1] = (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                        }
                    }
                }
                if (minroute.Count > 0)
                {
                    return Convert.ToInt32(minroute[0]);
                }
                else
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.MinRouteID时发生错误:" +ex.StackTrace+ ex.Message;
                return -1;
            }
            finally
            {
                dv.Dispose();
                dv1.Dispose();
                dv2.Dispose();
                dvd.Dispose();
                minroute = null;
            }

        }
        /// <summary>
        /// 20091005
        /// 根据调度任务的起点和终点设备索引查找路径最短的RouteIDSub
        /// </summary>
        /// <param name="startdevice">起点设备索引</param>
        /// <param name="enddevice">终点设备索引</param>
        /// /// <param name="enddevice">当前设备索引</param>
        /// <returns></returns>
        public static int MinRouteID(int startdevice, int enddevice,int nowdevice)
        {
            DataView dv = new DataView(); DataView dvd = new DataView();
            DataView dv1 = new DataView(); DataView dv2 = new DataView();
            List<double> minroute = new List<double>();//0,routIDSub;1,路径最小值
            try
            {
                int retrouteidsub = GetSameStartDeviceEnddeviceRouteID(startdevice, enddevice);
                if (retrouteidsub > 0)
                {
                    return retrouteidsub;
                }
                //选择最短调度路径并且判断此路径上是否有设备发生故障
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                    "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                    " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                    " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append(" and F_Status=1 and T_Base_Device.F_DeviceIndex =").Append(nowdevice);
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                if (dv.Count == 0)
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
                for (int i = 0; i < dv.Count; i++)
                {
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT F_RouteIDSub, F_LockedState,T_Base_Device.F_DeviceIndex FROM T_Base_Device,T_Base_Route_Device WHERE ").Append(
                        " T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex  and (T_Base_Route_Device.F_RouteIDSub = ").Append(
                        dv[i]["F_RouteIDSub"]).Append(") AND  (T_Base_Device.F_LockedState = - 1 or T_Base_Device.F_ErrorCode>=30)");//20121203
                    dvd = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dvd.Count > 0)
                    {
                        int dvin = Convert.ToInt32(dvd[0]["F_DeviceIndex"]);
                        if ((dvin >= 35001 && dvin <= 35003) || (dvin == 45001))
                        {//20120207机器人地位无法取代,不判断是否被禁用

                        }
                        else
                        {
                            continue;
                        }
                    }

                    List<double> route = new List<double>();//0,routIDSub;1,步长;2,路径使用频率;3,路径设备任务数
                    //路径步长(设备总数)最短(权重0.3),路径使用频率最小(权重0.3)的路径,路径设备任务数(权重0.3)

                    route.Add(Convert.ToDouble(dv[i]["F_RouteIDSub"]));//RouteIDSub
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("select count(F_DeviceIndex) as steps from T_Base_Route_Device where F_RouteIDSub="
                        ).Append(dv[i]["F_RouteIDSub"]);
                    dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv1.Count > 0)
                    {
                        route.Add(Convert.ToDouble(dv1[0]["steps"]));//步长
                        //route.Add(0);
                    }
                    else
                    {
                        continue;
                    }
                    //路径使用频率:路径执行调度任务的数量

                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(DISTINCT F_ManageTaskIndex) AS ManCount FROM T_Monitor_Task GROUP BY F_RouteID HAVING (F_RouteID = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["ManCount"].ToString(), out a) == true)
                        {
                            route.Add(Convert.ToDouble(dv2[0]["ManCount"]));//路径使用频率
                        }
                        else
                        {
                            route.Add(0);//路径使用频率
                        }
                    }
                    else
                    {
                        route.Add(0);//路径使用频率
                    }
                    //路径设备任务数
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(T_Monitor_Task.F_MonitorIndex) AS mtask FROM T_Monitor_Task ,T_Base_Route_Device where  (T_Monitor_Task.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex) and  (T_Base_Route_Device.F_RouteIDSub = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["mtask"].ToString(), out a) == true)
                        {
                            route.Add(Convert.ToDouble(dv2[0]["mtask"]));//路径设备任务数
                        }
                        else
                        {
                            route.Add(0);//路径设备任务数
                        }
                    }
                    else
                    {
                        route.Add(0);//路径设备任务数
                    }

                    //求最短路径((路径步长*0.3+路径使用频率*0.3+设备占有数*0.3)的最小值)
                    if (minroute.Count == 0)
                    {
                        minroute.Add(route[0]);
                        minroute.Add(route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                    }
                    else
                    {
                        if (minroute[1] > (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3))
                        {
                            minroute[0] = route[0];
                            minroute[1] = (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                        }
                    }
                }
                if (minroute.Count > 0)
                {
                    return Convert.ToInt32(minroute[0]);
                }
                else
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.MinRouteID时发生错误:" +ex.StackTrace+ ex.Message;
                return -1;
            }
            finally
            {
                dv.Dispose();
                dv1.Dispose();
                dv2.Dispose();
                dvd.Dispose();
            }

        }
        /// <summary>
        /// 根据调度任务的起点和终点设备索引查找路径最短的RouteIDSub
        /// </summary>
        /// <param name="startdevice">起点设备索引</param>
        /// <param name="enddevice">终点设备索引</param>
        /// <param name="AllStatus">不考虑路径状态:true和false都可以</param>
        /// <returns></returns>
        public static int MinRouteID(int startdevice, int enddevice, bool AllStatus, object useAwayfork)
        {
            //20101124
            DataView dv = new DataView();
            DataView dv1 = new DataView(); DataView dv2 = new DataView();
            StringBuilder dff =new StringBuilder();
            if (useAwayfork.ToString() == "-")
            {
                dff .Append( "F_UseAwayFork<>'n'");
            }
            else
            {
                dff .Append( "(F_UseAwayFork='" ).Append( useAwayfork ).Append( "' or F_UseAwayFork='-' )");//20101028
            }
            //20101124
            try
            {
                //选择最短调度路径并且判断此路径上是否有设备发生故障
                //20101124
                sql.Remove(0, sql.Length);
                sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                    "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                    " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                    " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append(" and ").Append(dff.ToString());
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                List<double> minroute = new List<double>();//0,routIDSub;1,路径最小值
                if (dv.Count == 0)
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
                for (int i = 0; i < dv.Count; i++)
                {


                    List<double> route = new List<double>();//0,routIDSub;1,步长;2,路径使用频率;3,路径设备任务数
                    //路径步长(设备总数)最短(权重0.3),路径使用频率最小(权重0.3)的路径,路径设备任务数(权重0.3)

                    route.Add(Convert.ToDouble(dv[i]["F_RouteIDSub"]));//RouteIDSub
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("select count(F_DeviceIndex) as steps from T_Base_Route_Device where F_RouteIDSub="
                        ).Append(dv[i]["F_RouteIDSub"]);
                    dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv1.Count > 0)
                    {
                        route.Add(Convert.ToDouble(dv1[0]["steps"]));//步长
                        //route.Add(0);
                    }
                    else
                    {
                        continue;
                    }
                    //路径使用频率:路径执行调度任务的数量

                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(DISTINCT F_ManageTaskIndex) AS ManCount FROM T_Monitor_Task GROUP BY F_RouteID HAVING (F_RouteID = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["ManCount"].ToString(), out a) == true)
                        {
                            route.Add(Convert.ToDouble(dv2[0]["ManCount"]));//路径使用频率
                        }
                        else
                        {
                            route.Add(0);//路径使用频率
                        }
                    }
                    else
                    {
                        route.Add(0);//路径使用频率
                    }
                    //路径设备任务数
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(T_Monitor_Task.F_MonitorIndex) AS mtask FROM T_Monitor_Task ,T_Base_Route_Device where  (T_Monitor_Task.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex) and  (T_Base_Route_Device.F_RouteIDSub = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["mtask"].ToString(), out a) == true)
                        {
                            route.Add(Convert.ToDouble(dv2[0]["mtask"]));//路径设备任务数
                        }
                        else
                        {
                            route.Add(0);//路径设备任务数
                        }
                    }
                    else
                    {
                        route.Add(0);//路径设备任务数
                    }

                    //求最短路径((路径步长*0.3+路径使用频率*0.3+设备占有数*0.3)的最小值)
                    if (minroute.Count == 0)
                    {
                        minroute.Add(route[0]);
                        minroute.Add(route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                    }
                    else
                    {
                        if (minroute[1] > (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3))
                        {
                            minroute[0] = route[0];
                            minroute[1] = (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                        }
                    }
                }
                if (minroute.Count > 0)
                {
                    return Convert.ToInt32(minroute[0]);
                }
                else
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.MinRouteID时发生错误:" +ex.StackTrace+ ex.Message;
                return -1;
            }
            finally
            {
                dv.Dispose();
                dv1.Dispose();
                dv2.Dispose();
            }

        }
        /// <summary>
        /// 20100702
        /// 根据调度任务的起点和终点设备索引查找路径最短的RouteIDSub 
        /// </summary>
        /// <param name="startdevice">起点设备索引</param>
        /// <param name="enddevice">终点设备索引</param>
        /// <param name="useAwayfork">是否使用远货叉</param>
        /// <returns></returns>
        //public static int MinRouteID(int startdevice, int enddevice, object useAwayfork,string FSTARTCELL,string FENDCELL,int tasktype)
          public static int MinRouteID(int startdevice, int enddevice, object useAwayfork)
      
        {
            //20101124
            DataView dv = new DataView(); DataView dvd = new DataView();
            DataView dv1 = new DataView(); DataView dv2 = new DataView();
            StringBuilder dff = new StringBuilder();
            if (useAwayfork.ToString() == "-")
            {
                dff.Append( "F_UseAwayFork<>'n'");
            }
            else
            {
                dff .Append( "(F_UseAwayFork='" ).Append( useAwayfork).Append( "' or F_UseAwayFork='-' )");//20101028
            }
            #region 工位堆垛机的路径优选20120207
            //过桥输送机的选择是临近取货位优先
            //35000双顶升机的选择是小列(1排6列;2排26列)选择顶升机32038

            //if (startdevice == 35000 && (enddevice == 32025 || enddevice == 10100 || enddevice == 10200 || enddevice == 10300))
            //{

            //    dff.Append(" and T_Base_Route_Device.F_DeviceIndex=32038");
            //}
            //else
            //{

            //}
            #endregion
            //20101124
            try
            {
                int retrouteidsub = GetSameStartDeviceEnddeviceRouteID(startdevice, enddevice);
                if (retrouteidsub > 0)
                {
                    return retrouteidsub;
                }
                //选择最短调度路径并且判断此路径上是否有设备发生故障
                //20101124

               //int conveyorindex= conveyorNumFromPosition(FSTARTCELL, FENDCELL, startdevice, enddevice, tasktype);

                sql.Remove(0, sql.Length);
                sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                    "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                    " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                    " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append("  and ").Append(dff.ToString()).Append(" and F_Status=1 ");//and T_Base_Route_Device.F_DeviceIndex=12018
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                List<double> minroute = new List<double>();//0,routIDSub;1,路径最小值
                if (dv.Count == 0)
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
                for (int i = 0; i < dv.Count; i++)
                {
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT F_RouteIDSub,F_LockedState,T_Base_Device.F_DeviceIndex FROM T_Base_Device,T_Base_Route_Device WHERE ").Append(
                        " T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex  and (T_Base_Route_Device.F_RouteIDSub = ").Append(
                        dv[i]["F_RouteIDSub"]).Append(") AND  (T_Base_Device.F_LockedState = - 1 or T_Base_Device.F_ErrorCode>=30)");//20121203
                    dvd = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dvd.Count > 0)
                    {
                        int dvin = Convert.ToInt32(dvd[0]["F_DeviceIndex"]);
                        if ((dvin >= 35001 && dvin <= 35003) || (dvin == 45001))
                        {//20120207机器人地位无法取代,不判断是否被禁用

                        }
                        else
                        {
                            continue;
                        }
                    }

                    List<double> route = new List<double>();//0,routIDSub;1,步长;2,路径使用频率;3,路径设备任务数
                    //路径步长(设备总数)最短(权重0.3),路径使用频率最小(权重0.3)的路径,路径设备任务数(权重0.3)

                    route.Add(Convert.ToDouble(dv[i]["F_RouteIDSub"]));//RouteIDSub
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("select count(F_DeviceIndex) as steps from T_Base_Route_Device where F_RouteIDSub="
                        ).Append(dv[i]["F_RouteIDSub"]);
                    dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv1.Count > 0)
                    {
                        route.Add(Convert.ToDouble(dv1[0]["steps"]));//步长
                        //route.Add(0);
                    }
                    else
                    {
                        continue;
                    }
                    //路径使用频率:路径执行调度任务的数量
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(DISTINCT F_ManageTaskIndex) AS ManCount FROM T_Monitor_Task GROUP BY F_RouteID HAVING (F_RouteID = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["ManCount"].ToString(), out a) == true)
                        {
                            route.Add(a);//路径使用频率
                        }
                        else
                        {
                            route.Add(0);//路径使用频率
                        }
                    }
                    else
                    {
                        route.Add(0);//路径使用频率
                    }
                    //路径设备任务数
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(T_Monitor_Task.F_MonitorIndex) AS mtask FROM T_Monitor_Task ,T_Base_Route_Device where (T_Monitor_Task.F_deviceindex = T_Base_Route_Device.F_deviceindex) and  (T_Monitor_Task.F_RouteID = T_Base_Route_Device.F_RouteIDSub) and  (T_Base_Route_Device.F_RouteIDSub = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["mtask"].ToString(), out a) == true)
                        {
                            route.Add(a);//路径设备任务数
                        }
                        else
                        {
                            route.Add(0);//路径设备任务数
                        }
                    }
                    else
                    {
                        route.Add(0);//路径设备任务数
                    }

                    //求最短路径((路径步长*0.3+路径使用频率*0.3+设备占有数*0.3)的最小值)
                    if (minroute.Count == 0)
                    {
                        minroute.Add(route[0]);
                        minroute.Add(route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                    }
                    else
                    {
                        if (minroute[1] > (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3))
                        {
                            minroute[0] = route[0];
                            minroute[1] = (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                        }
                    }
                }
                if (minroute.Count > 0)
                {
                    return Convert.ToInt32(minroute[0]);
                }
                else
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.MinRouteID时发生错误:" +ex.StackTrace+ ex.Message;
                return -1;
            }
            finally
            {
                dv.Dispose();
                dv1.Dispose();
                dv2.Dispose();
                dvd.Dispose();
            }


        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Mankind">调度任务类型索引</param>
        /// <param name="ManFID">io_control索引</param>
        /// <param name="routeIDSub">调度路径的子路径编号</param>
        /// <param name="drv">调度任务行视图</param>
        /// <returns></returns>
        public static int CreateMonitor(int Mankind,long ManFID, int routeIDSub,DataRowView drv,int status)
        {
            //关联设备属于需要同步执行的设备组
            //穿梭车、堆垛机、AGV都属于关键设备需要单独调度,
            //所以此类设备是路径分段命令的分割点,
            //举例:【成品入库路径】
            //1)输送机输送分段控制
            //2)输送机出库(取关联);
            //3)RGV将取+RGV取货(取关联)+RGV将送+RGV送货(送关联);
            //4)输送机入库(送关联)
            //5)输送机输送分段控制
            //6)堆垛机取送货(将取+取货或者将送+送货);
            //7)条码扫描仪
            //12)虚拟关键设备
            DataView dvRoute = new DataView(); DataView dvs = new DataView();
            DataView dvahead = new DataView();//add by lyj 20171026
            int tasktype = 0;
            try
            {
                drv = dbo.ExceSQL(string.Format("SELECT * FROM T_Manage_Task   WHERE (FID = {0}) AND (F_ManageTaskKindIndex = {1})", ManFID, Mankind)).Tables[0].DefaultView[0];//20130620
                //20110104
                if (ccf.GetMonitorIndex(ManFID, Mankind) >= 29000)
                {

                    ccf.UpdateMonitorIndex(1);
                }


                //if (isOrNotAllowDisaemble(drv) == false) //20171106 add by lyj 控制出入库 晟碟注释
                //{

                //    return 0;
                //}

                #region  add by lyj 20171026
                tasktype = Convert.ToInt32(drv["FCONTROLTASKTYPE"]);

                #endregion
                //20121203
                //化成A回库和去倒箔间的包含12068的任务分别不能超过六个
                //化成B出库包含12019的不能超过4个;
                //化成B去倒箔间和回库的任务包含12034等待执行的不能超过3个
                //立库到化成B倒箔间任务每个巷道最多两个
                //if (ControlTaskAmountUnDisassemble(drv) == true) return 0; 晟碟注释

                sql.Remove(0, sql.Length);
                sql.Append( "SELECT T_Base_Route_Device.F_DeviceIndex,F_DeviceOrder, T_Base_Route_Device.F_SerialNumber, " ).Append(
                    "T_Base_Device.F_DeviceKindIndex,F_UnControl FROM " ).Append(
                    "T_Base_Device,T_Base_Route_Device where " ).Append(
                    "T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex and F_RouteIDSub=" ).Append(
                    routeIDSub + " order by F_SerialNumber asc ");
                dvRoute = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvRoute.Count == 0) return 0;
                dbo.TransBegin(IsolationLevel.ReadCommitted);
                int CurSerialNumber = 0;
                List<int> PriorDevice0 = new List<int>(), PriorKeyDevice0 = new List<int>();
                StringBuilder AheadDetect =new StringBuilder();//20101124
                StringBuilder RunningLock = new StringBuilder();//20101124
                StringBuilder AheadTrigger = new StringBuilder();//20101124
                string uncontrol = string.Empty;
                for (int i = 0; i < dvRoute.Count; i++)
                {
                    AheadDetect.Remove(0,AheadDetect.Length);
                    RunningLock.Remove(0,RunningLock.Length);
                    AheadTrigger.Remove(0,AheadTrigger.Length);
                    uncontrol= (dvRoute[i]["F_UnControl"] + "").ToString();

                    List<int> PriorDevice = new List<int>(), NextDevice = new List<int>(),
                        CurDevice = new List<int>(), NextKeyDevice = new List<int>();//0设备所引;1设备类型;2:路径序号;3:设备命令

                    #region 20210326_上海晟碟,关键设备的下一个关键设备
                    //List<int> SecondNextKeyDevice = new List<int>();
                    #endregion

                    //防止输送机设备组重复查找关键设备
                    if (Convert.ToInt32(dvRoute[i]["F_SerialNumber"]) <= CurSerialNumber) continue;

                    CurDevice.Add(Convert.ToInt32(dvRoute[i]["F_DeviceIndex"]));
                    CurDevice.Add(Convert.ToInt32(dvRoute[i]["F_DeviceKindIndex"]));
                    CurDevice.Add(Convert.ToInt32(dvRoute[i]["F_SerialNumber"]));
                    CurDevice.Add(Convert.ToInt32(dvRoute[i]["F_DeviceOrder"]));
                    NextKeyDevice = GetNextKeyDevice(routeIDSub, Convert.ToInt32(dvRoute[i]["F_SerialNumber"]));
                    if (NextKeyDevice.Count > 0)//找到关键设备:0设备所引;1设备类型;2:路径序号;3:设备命令
                    {
                        
                        CurSerialNumber = NextKeyDevice[2];
                        NextDevice = GetNextDevice(routeIDSub, NextKeyDevice[2]);
                        PriorDevice = GetPriorDevice(routeIDSub, NextKeyDevice[2]);

                        #region 20210326_上海晟碟,关键设备的下一个关键设备赋值

                        //for (int j = CurSerialNumber + 1; j < dvRoute.Count; j++)
                        //{
                        //    SecondNextKeyDevice = GetNextKeyDevice(routeIDSub, j);
                        //    if (SecondNextKeyDevice.Count > 0)
                        //        break;

                        //}
                        
                        #endregion

                        int TriggerTaskNo = 0;
                        int[] Coor = new int[6] { 0, 0, 0, 0, 0, 0 };
                        //20101118
                        string tw = string.Empty; 
                        int mainTask = 0; int Rgvorder = 0;
                        int lifterUP=0,snextq = 0, snexts = 0;
                        switch (NextKeyDevice[1])
                        {

                            case 1:

                                #region 堆垛机
                                if (NextKeyDevice[3] == -1) continue;
                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                #region 3、4楼的巷道的同一U型移库操作//20111020
                                if (NextDevice.Count > 0 && PriorDevice.Count > 0)
                                {
                                    if (Convert.ToInt32(drv["FSTARTUCODE"]) > 0 && Convert.ToInt32(drv["FENDUCODE"]) == Convert.ToInt32(drv["FSTARTUCODE"]))
                                    {//直接插入高端设备U型内部的送出指令
                                        int arrowdev = Convert.ToInt32(drv["FENDCELL"]);
                                        CurDevice[3] = 6;
                                        CurDevice[0] = Convert.ToInt32(drv["FSTARTCELL"]);
                                        #region 运行提前检测光电组
                                        AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                        //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                        tw = GetSendOutDetect(CurDevice[0]);
                                        if (tw != "")
                                        {//20101124
                                            AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                        }
                                        else
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);
                                        }



                                        #endregion
                                        int udev = Convert.ToInt32(drv["FSTARTUCODE"]);
                                        AheadTrigger.Remove(0, AheadTrigger.Length);
                                        if (DeviceAndOrderExitInMonitor(Mankind, ManFID, udev, CurDevice[3], arrowdev) == false)
                                        {

                                            #region 生成高端输送机命令
                                            int mindex = ccf.GetMonitorIndex(ManFID, Mankind);//任务ID
                                            TriggerTaskNo = mindex;
                                            //20101124
                                            sql.Remove(0, sql.Length);
                                            sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                " F_AheadDetect,F_TxtParam,F_UseAwayFork)").Append(
                                            "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                ).Append(",").Append(udev).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append("," + arrowdev
                                                ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"]).Append("')");
                                            dbo.ExceSQL(sql.ToString());


                                            #endregion
                                        }
                                        continue;
                                    }
                                }
                                #endregion
                                #region 生成一个设备命令并且获得取送货坐标
                                string Uzxy = string.Empty;//20111020
                                StringBuilder GetGDDetect = new StringBuilder("");
                                StringBuilder SendGDDetect = new StringBuilder("");
                                int SendUdevConveyor = 0;
                                //堆垛机取货:
                                if (PriorDevice.Count > 0)
                                {

                                    #region 插入高端U型线控制//20111020
                                    if (Convert.ToInt32(drv["FSTARTUCODE"]) > 0)
                                    {//直接插入高端设备U型内部的送出指令
                                        int arrowdev = 0;
                                        GetUAreaOutputDevice(NextKeyDevice[0], Convert.ToInt32(drv["FSTARTUCODE"]), out arrowdev, out Uzxy);
                                        if (arrowdev > 0)
                                        {
                                            GetGDDetect.Clear();
                                            GetGDDetect.Append("D-").Append(arrowdev).Append(".0;D-").Append(arrowdev).Append(".2");
                                            CurDevice[3] = 6;
                                            CurDevice[0] = Convert.ToInt32(drv["FSTARTCELL"]);
                                            if (arrowdev != CurDevice[0])
                                            {
                                                int udev = Convert.ToInt32(drv["FSTARTUCODE"]);
                                                #region 运行提前检测光电组
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                tw = GetSendOutDetect(CurDevice[0]);
                                                if (tw != "")
                                                {//20101124
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                #endregion

                                                AheadTrigger.Remove(0, AheadTrigger.Length);
                                                if (DeviceAndOrderExitInMonitor(Mankind, ManFID, udev, CurDevice[3], arrowdev) == false)
                                                {

                                                    #region 生成高端输送机命令
                                                    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                    TriggerTaskNo = mindex;
                                                    //20101124
                                                    sql.Remove(0, sql.Length);
                                                    sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                        " F_AheadDetect,F_TxtParam,F_UseAwayFork)").Append(
                                                    "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                        ).Append(",").Append(udev).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append("," + arrowdev
                                                        ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"]).Append("')");
                                                    dbo.ExceSQL(sql.ToString());


                                                    #endregion
                                                }
                                            }
                                        }
                                    }
                                    #endregion
                                    //如果PriorDevice是输送机则生成输送机命令【CurDevice-->PriorDevice目标设备,出库:2】,
                                    //收到输送机PriorDevice的PLC上报的运行状态则提前触发堆垛机将取命令,然后是取送货命令,获取堆垛机取坐标
                                    #region 前一设备是输送机
                                    if (uncontrol != "1")
                                    {//20091107
                                        if (PriorDevice[1] == 2)
                                        {
                                            int arrowdev;
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;

                                            }

                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                                #region 运行提前检测光电组

                                                //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                tw = GetBindingDeviceIndex(CurDevice[0]);
                                                if (tw != "")
                                                {//20101118
                                                    //20101124
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                if (tw != "")
                                                {//20101118
                                                    AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                }

                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;

                                                #region 运行提前检测光电组
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                tw = GetSendOutDetect(CurDevice[0]);
                                                if (tw != "")
                                                {//20101124
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }

                                                //20161221
                                                //20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                if (nextConveyor.Count > 0)//20110331
                                                {
                                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));
                                                    #region lzm20250225 取消重点输送线校验
                                                    //tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                    //if (tw != "")
                                                    //{//20101124
                                                    //    AheadDetect.Append(";D").Append(tw.ToString());
                                                    //}
                                                    //tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                    //if (tw != "")
                                                    //{//20101124
                                                    //    AheadDetect.Append(";D").Append(tw.ToString());
                                                    //}
                                                    #endregion
                                                    ////20090803检测设备是否空闲idle
                                                    //AheadDetect += ";" + "I" + nextConveyor[0].ToString();

                                                }


                                                #endregion
                                            }
                                            AheadTrigger.Remove(0, AheadTrigger.Length);
                                            AheadTrigger.Append(PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                #region 运行时锁定设备组

                                                #endregion

                                                #region 生成输送机命令

                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                //20101124
                                                #region update by lyj20180424 穿梭板入库不生成输送机命令
                                                //if (NextKeyDevice[0]!=10000)
                                                //{
                                                sql.Remove(0, sql.Length);
                                                sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                    " F_AheadDetect,F_TxtParam,F_UseAwayFork)").Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                    ).Append(",").Append(CurDevice[0]).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append("," + arrowdev
                                                    ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"]).Append("')");
                                                dbo.ExceSQL(sql.ToString());
                                                //}
                                                //sql.Remove(0, sql.Length);
                                                //sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                //    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                //    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                //    " F_AheadDetect,F_TxtParam,F_UseAwayFork)").Append(
                                                //"VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                //    ).Append(",").Append(CurDevice[0]).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append("," + arrowdev
                                                //    ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"]).Append("')");
                                                //dbo.ExceSQL(sql.ToString());
                                                #endregion

                                                #endregion
                                            }

                                        }
                                    }
                                    #endregion

                                    #region 前一设备是条码扫描器等,不载物的关键设备

                                    if ((PriorDevice[1] == 7) || (PriorDevice[1] == 12))
                                    {
                                        int arrowdev;
                                        List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                        if (newcur != null)
                                        {
                                            CurDevice = newcur;
                                            PriorDevice = PriorDevice0;
                                            //输送机的起点设备和终点设备相同时,目标设备索引置为零
                                            devinfo = Model.CGetInfo.GetDeviceInfo(CurDevice[0]);
                                            if (devinfo.UnControl != "1")
                                            {
                                                arrowdev = 0;
                                                #region 运行提前检测光电组
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                tw = GetBindingDeviceIndex(CurDevice[0]);
                                                if (tw != "")
                                                {//20101124
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                                AheadTrigger.Remove(0, AheadTrigger.Length);//20101124
                                                AheadTrigger.Append(PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                                if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                                {
                                                    #region 运行时锁定设备组

                                                    #endregion

                                                    #region 生成输送机命令
                                                    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                    TriggerTaskNo = mindex;
                                                    sql.Remove(0, sql.Length);//20101124
                                                    sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                        " F_AheadDetect,F_TxtParam)").Append(
                                                    "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                        ).Append(",").Append(CurDevice[0]).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append(",").Append(arrowdev
                                                        ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                                    dbo.ExceSQL(sql.ToString());
                                                    #endregion
                                                }
                                            }
                                        }

                                    }
                                    #endregion

                                    //如果PriorDevice是固定站台不生成命令,堆垛机生成取送货命令,获取堆垛机取坐标
                                    //如果PriorDevice是巷道,PriorDevice不拆分命令,获取堆垛机取坐标,拆分堆垛机命令取送货
                                    #region 获得取送货坐标


                                    int[] Coor0_2 = new int[6] { 0, 0, 0, 0, 0, 0 };
                                    //如果NextDevice是巷道,NextDevice不拆分命令,获取堆垛机送坐标,拆分堆垛机命令取送货
                                    if ((PriorDevice[1] == 7) || (PriorDevice[1] == 12))
                                    {
                                        PriorDevice = PriorDevice0;
                                    }
                                    if (PriorDevice[1] == 10)
                                    {
                                        #region 插入高端U型线控制//20111020
                                        if ((Convert.ToInt32(drv["FSTARTUCODE"]) > 0) && (Uzxy != "-"))
                                        {
                                            char[] cc = new char[1] { '-' };
                                            string[] sp = Uzxy.Split(cc);
                                            for (int ip = 0; ip < sp.Length; ip++)
                                            {
                                                Coor0_2[ip] = Convert.ToInt32(sp[ip]);
                                            }
                                        }
                                        #endregion
                                        else
                                            Coor0_2 = GetStackCoordinateFromManage(Mankind, ManFID, true);
                                    }
                                    else if (PriorDevice[1] == 2)
                                    {
                                        //如果NextDevice是输送机则取送货命令,获取堆垛机送坐标

                                        //如果NextDevice是固定站台则取送货命令,获取堆垛机送坐标
                                        Coor0_2 = GetStackCoordinateFromLaneGate(NextKeyDevice[0], PriorDevice[0], true);
                                    }

                                    Coor[0] = Coor0_2[0];
                                    Coor[1] = Coor0_2[1];
                                    Coor[2] = Coor0_2[2];
                                    #endregion
                                }

                                //堆垛机送货:
                                if (NextDevice.Count > 0)
                                {
                                    int[] Coor3_5 = new int[6] { 0, 0, 0, 0, 0, 0 };
                                    //如果NextDevice是巷道,NextDevice不拆分命令,获取堆垛机送坐标,拆分堆垛机命令取送货
                                    if (NextDevice[1] == 10 || (NextDevice[0] == 14005 || NextDevice[0] == 14006))  //20210201_上海晟碟,如果下一个设备为19004提升机,获取终点货位与巷道方式相同
                                    {

                                        if (Convert.ToInt32(drv["FENDUCODE"]) > 0)
                                        {
                                            #region 插入高端U型线控制//20111020
                                            char[] cc = new char[1] { '-' }; int curdev = 0;
                                            GetUAreaInputDevice(NextKeyDevice[0], Convert.ToInt32(drv["FENDUCODE"]), out curdev, out Uzxy);
                                            if (curdev > 0)
                                            {
                                                SendUdevConveyor = curdev;
                                                SendGDDetect.Clear();
                                                SendGDDetect.Append("D").Append(curdev).Append(".0;D-").Append(curdev).Append(".2");
                                                string[] sp = Uzxy.Split(cc);
                                                for (int ip = 0; ip < sp.Length; ip++)
                                                {
                                                    Coor3_5[3 + ip] = Convert.ToInt32(sp[ip]);
                                                }
                                            }
                                            #endregion
                                        }

                                        else
                                        {
                                            Coor3_5 = GetStackCoordinateFromManage(Mankind, ManFID, false);
                                        }
                                    }
                                    else if (NextDevice[1] == 2)
                                    {
                                        //如果NextDevice是输送机则取送货命令,获取堆垛机送坐标

                                        //如果NextDevice是固定站台则取送货命令,获取堆垛机送坐标
                                        Coor3_5 = GetStackCoordinateFromLaneGate(NextKeyDevice[0], NextDevice[0], false);
                                    }

                                    Coor[3] = Coor3_5[3];
                                    Coor[4] = Coor3_5[4];
                                    Coor[5] = Coor3_5[5];
                                }
                                #endregion

                                #region 20210125_上海晟碟,穿梭板换层任务拆分 民航二所

                                //检测下一个设备是否为19005(换层提升机),如果是,改变拆分逻辑为:
                                //1.提升机取货至穿梭板所在层
                                //2.穿梭板运动至提升机
                                //3.提升机送货至目标层
                                //4.穿梭板运动至目标货位
                                //因为该提升机是由其他两个提货的提升机共同完成的,提前检测需要添加其他两个提升机空闲和无货
                                //19004执行完取货需要加锁,防止其他两个提升机运动
                                if (NextDevice[0] == 14005 || NextDevice[0] == 14006)
                                {
                                    //RunningLock.Remove(0, RunningLock.Length);
                                    //RunningLock.Append("19003;19005");  //锁定两个提升机
                                    int x_c_p = 0;//民航二所
                                    if (NextDevice[0] == 14005)
                                    {

                                        x_c_p = 2;
                                    }
                                    else if (NextDevice[0] == 14006)
                                    {

                                        x_c_p = 31;
                                    }
                                    #region 穿梭板运动到第2列,1表示穿梭板运动 14006运动到第 31列

                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    tw = GetBindingDeviceIndex(NextDevice[0]);
                                    if (tw != string.Empty)
                                    {
                                        AheadDetect.Append("D").Append(tw);//19005提升机无货
                                    }
                                    AheadTrigger.Remove(0, AheadTrigger.Length);
                                    AheadTrigger.Append(NextKeyDevice[0].ToString()); //上海晟碟穿梭版到第一列触发提升机 

                                    //AheadDetect.Append(";").Append(GetBeDetectedDevices(NextDevice[0]));//19005提升机空闲
                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(NextKeyDevice[0]));//穿梭版空闲

                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], 6, 0) ==
                                        false)
                                    {
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                        TriggerTaskNo = mindex;//上海晟碟穿梭版到第2列触发提升机取车 

                                        sql.Remove(0, sql.Length);//20101124
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,")
                                            .Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,")
                                            .Append(
                                                " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork,F_PriorMonitor,F_AgvTask,F_AGVNextTask)")
                                            .Append("VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",")
                                            .Append(mindex).Append(",").Append(drv["FTASKLEVEL"]).Append(",")
                                            .Append(NextKeyDevice[0]).Append(",1,").Append(routeIDSub).Append(",")
                                            .Append(status).Append(",2").Append(",").Append(x_c_p)
                                            .Append(",").Append(Coor[2]).Append(",").Append(Coor[3]).Append(",")
                                            .Append(Coor[4]).Append(",").Append(Coor[5]).Append(",'")
                                            .Append(AheadDetect.ToString()).Append("','").Append(RunningLock.ToString())
                                            .Append("','").Append(drv["FPALLETBARCODE"]).Append("','")
                                            .Append(drv["FUseAwayFork"]).Append("',").Append(lifterUP).Append(",")
                                            .Append(snextq).Append(",").Append(snexts).Append(")");
                                        dbo.ExceSQL(sql.ToString());


                                    }


                                    #endregion

                                    #region 提升机取车

                                    Rgvorder = 4;

                                    #region 运行提前检测光电组
                                    AheadDetect.Remove(0, AheadDetect.Length);

                                    #region 20210201_提升机取车提前检测,检测两个提升机是否空闲,检测是否有正在执行的其他换层任务

                                    //if (NextDevice[0] == 14005)
                                    //{
                                    //    AheadDetect.Append(GetBeDetectedDevices(14003)).Append(";").Append(GetBeDetectedDevices(14004));
                                    //    AheadDetect.Append(";D14003.0;D14004.0;");
                                    //    AheadDetect.Append(";D14005.0");//14005无货
                                    //    AheadDetect.Append(GetBeDetectedDevices(NextDevice[0]));

                                    //}
                                    //else if (NextDevice[0] == 14006)
                                    //{
                                    //    AheadDetect.Append(GetBeDetectedDevices(14001)).Append(";").Append(GetBeDetectedDevices(14002));
                                    //    AheadDetect.Append(";D14001.0;D14002.0;");
                                    //    AheadDetect.Append(";D14006.0");//14006无货
                                    //    AheadDetect.Append(GetBeDetectedDevices(NextDevice[0]));
                                    //}
                                    #endregion


                                    #endregion

                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], Rgvorder, 0) == false)
                                    {
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);


                                        sql.Remove(0, sql.Length);
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,")
                                            .Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,")
                                            .Append(
                                                " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_TxtParam)")
                                            .Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",")
                                            .Append(mindex).Append(",").Append(drv["FTASKLEVEL"]).Append(",")
                                            .Append(NextDevice[0]).Append(",").Append(Rgvorder)
                                            .Append(",").Append(routeIDSub).Append(",").Append(status).Append(",")
                                            .Append(Coor[2]).Append(",0,0,0").Append(",0,0,'")
                                            .Append(AheadDetect.ToString()).Append("','")
                                            .Append(drv["FPALLETBARCODE"]).Append("')");
                                        dbo.ExceSQL(sql.ToString());
                                        //sql.Remove(0, sql.Length);
                                        //sql.Append("update T_Monitor_Task set F_Associate=").Append(mindex).Append(" where F_MonitorIndex=").Append(mainTask);
                                        //dbo.ExceSQL(sql.ToString());
                                        if (AheadTrigger != null)
                                        {
                                            AheadTrigger.Remove(0, AheadTrigger.Length);
                                            AheadTrigger.Append(NextKeyDevice[0]).Append("-").Append(mindex);

                                            sql.Clear();
                                            sql.Append("update T_Monitor_Task set F_AheadTrigger='").Append(AheadTrigger).Append("' where F_MonitorIndex=").Append(TriggerTaskNo);
                                            dbo.ExceSQL(sql.ToString());

                                        }

                                    }
                                    #endregion

                                    #region 穿梭板运动到提升机,1表示穿梭板运动 民航二所两个提升机
                                    AheadTrigger.Remove(0, AheadTrigger.Length);
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    tw = GetBindingDeviceIndex(NextDevice[0]);
                                    if (tw != string.Empty)
                                    {
                                        AheadDetect.Append("D").Append(tw);//19005提升机无货
                                    }

                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(NextDevice[0]));//19005提升机空闲
                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(NextKeyDevice[0]));//穿梭版空闲

                                    int x_c = 0;//民航二所
                                    if (NextDevice[0] == 14005)
                                    {
                                        AheadDetect.Append(";D-14005.1"); //D-14005.1为1表明提升机叉子伸到位
                                        x_c = 1;
                                    }
                                    else if (NextDevice[0] == 14006)
                                    {
                                        AheadDetect.Append(";D-14006.1");
                                        x_c = 32;
                                    }


                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], 6, 0) ==
                                        false)
                                    {
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);

                                        sql.Remove(0, sql.Length);//20101124
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,")
                                            .Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,")
                                            .Append(
                                                " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork,F_PriorMonitor,F_AgvTask,F_AGVNextTask)")
                                            .Append("VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",")
                                            .Append(mindex).Append(",").Append(drv["FTASKLEVEL"]).Append(",")
                                            .Append(NextKeyDevice[0]).Append(",1,").Append(routeIDSub).Append(",")
                                            .Append(status).Append(",2").Append(",").Append(x_c)
                                            .Append(",").Append(Coor[2]).Append(",").Append(Coor[3]).Append(",")
                                            .Append(Coor[4]).Append(",").Append(Coor[5]).Append(",'")
                                            .Append(AheadDetect.ToString()).Append("','").Append(RunningLock.ToString())
                                            .Append("','").Append(drv["FPALLETBARCODE"]).Append("','")
                                            .Append(drv["FUseAwayFork"]).Append("',").Append(lifterUP).Append(",")
                                            .Append(snextq).Append(",").Append(snexts).Append(")");
                                        dbo.ExceSQL(sql.ToString());
                                    }

                                    #endregion

                                    #region 提升机送车
                                    Rgvorder = 5;

                                    #region 运行提前检测光电组
                                    AheadDetect.Remove(0, AheadDetect.Length);

                                    #region 20210201_提升机取车提前检测,检测两个提升机是否空闲,检测是否有正在执行的其他换层任务

                                    AheadDetect.Append(GetBeDetectedDevices(NextDevice[0]));//提升机空闲
                                    tw = GetBindingDeviceIndex(NextDevice[0]);
                                    if (tw != string.Empty)
                                    {
                                        AheadDetect.Append(";D-").Append(tw);//19005提升机有货
                                    }

                                    //AheadDetect.Append(";K19005");
                                    #endregion


                                    #endregion

                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], Rgvorder, 0) == false)
                                    {
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                        sql.Remove(0, sql.Length);
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,")
                                            .Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,")
                                            .Append(
                                                " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_TxtParam)")
                                            .Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",")
                                            .Append(mindex).Append(",").Append(drv["FTASKLEVEL"]).Append(",")
                                            .Append(NextDevice[0]).Append(",").Append(Rgvorder)
                                            .Append(",").Append(routeIDSub).Append(",").Append(status).Append(",")
                                            .Append(Coor[5]).Append(",0,0,0,").Append("0,0,'")
                                            .Append(AheadDetect.ToString()).Append("','")
                                            .Append(drv["FPALLETBARCODE"]).Append("')");
                                        dbo.ExceSQL(sql.ToString());
                                        //sql.Remove(0, sql.Length);
                                        //sql.Append("update T_Monitor_Task set F_Associate=").Append(mindex).Append(" where F_MonitorIndex=").Append(mainTask);
                                        //dbo.ExceSQL(sql.ToString());

                                    }
                                    #endregion

                                    #region 穿梭板运动
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    AheadDetect.Append("D-").Append(NextDevice[0]).Append(".0");
                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(NextDevice[0]));
                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(NextKeyDevice[0]));//穿梭板空闲

                                    int mindex1 = ccf.GetMonitorIndex(ManFID, Mankind);
                                    int x_c_S = 0;//民航二所
                                    if (NextDevice[0] == 14005)
                                    {
                                        AheadDetect.Append(";D-14005.1"); //D-14005.1为1表明提升机叉子伸到位
                                        x_c_S = 2;
                                    }
                                    else if (NextDevice[0] == 14006)
                                    {
                                        AheadDetect.Append(";D-14006.1");
                                        x_c_S = 31;
                                    }
                                    sql.Remove(0, sql.Length);//20101124
                                    sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,")
                                        .Append(
                                            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,")
                                        .Append(
                                            " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork,F_PriorMonitor,F_AgvTask,F_AGVNextTask)")
                                        .Append("VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",")
                                        .Append(mindex1).Append(",").Append(drv["FTASKLEVEL"]).Append(",")
                                        .Append(NextKeyDevice[0]).Append(",1,").Append(routeIDSub).Append(",")
                                        .Append(status).Append(",").Append(2).Append(",").Append(x_c_S)
                                        .Append(",").Append(Coor[5]).Append(",").Append(2).Append(",")
                                        .Append(2).Append(",").Append(Coor[5]).Append(",'")
                                        .Append(AheadDetect.ToString()).Append("','").Append(RunningLock.ToString())
                                        .Append("','").Append(drv["FPALLETBARCODE"]).Append("','")
                                        .Append(drv["FUseAwayFork"]).Append("',").Append(lifterUP).Append(",")
                                        .Append(snextq).Append(",").Append(snexts).Append(")");
                                    dbo.ExceSQL(sql.ToString());
                                    #endregion
                                }
                                #endregion

                                #region 202110330_上海晟碟,充电任务 民航二所

                                else if (tasktype == 6)  //如果是充电任务 运行到2列
                                {
                                    int mindex1 = ccf.GetMonitorIndex(ManFID, Mankind);
                                    sql.Remove(0, sql.Length);//20101124
                                    sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,")
                                        .Append(
                                            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,")
                                        .Append(
                                            " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork,F_PriorMonitor,F_AgvTask,F_AGVNextTask)")
                                        .Append("VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",")
                                        .Append(mindex1).Append(",").Append(drv["FTASKLEVEL"]).Append(",")
                                        .Append(NextKeyDevice[0]).Append(",1,").Append(routeIDSub).Append(",")
                                        .Append(status).Append(",").Append(Coor[3]).Append(",").Append(2)
                                        .Append(",").Append(Coor[5]).Append(",").Append(Coor[3]).Append(",")
                                        .Append(2).Append(",").Append(Coor[5]).Append(",'")
                                        .Append(AheadDetect.ToString()).Append("','").Append(RunningLock.ToString())
                                        .Append("','").Append(drv["FPALLETBARCODE"]).Append("','")
                                        .Append(drv["FUseAwayFork"]).Append("',").Append(lifterUP).Append(",")
                                        .Append(snextq).Append(",").Append(snexts).Append(")");
                                    dbo.ExceSQL(sql.ToString());
                                }

                                #endregion

                                else
                                {
                                    #region 生成堆垛机指令:将取+取货+送货

                                    #region 取货

                                    //取货
                                    if (PriorDevice.Count > 0)
                                    {
                                        if (PriorDevice[1] == 10)
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);
                                        }
                                        RunningLock.Remove(0, RunningLock.Length);//20101124
                                        AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                        if (PriorDevice[1] == 10) //晟碟
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);


                                            //AheadDetect.Append("M").Append(PriorDevice[0].ToString());//出库及移库 取货都要检测 双升货位 
                                            if (NextDevice.Count > 0)
                                            {
                                                if (NextDevice[1] == 10)
                                                {
                                                    //AheadDetect.Append(";").Append("J").Append(PriorDevice[0].ToString());//移库的终点 相当于又要入库 

                                                }
                                            }


                                        }

                                        if (PriorDevice[1] == 2)
                                        {//取货做提前检测
                                            #region 运行时锁定设备组
                                            RunningLock.Append(PriorDevice[0].ToString());
                                            #endregion
                                            #region 运行提前检测光电组
                                            AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                                                      //20101011
                                            string[] cdi = ccf.GetLanewayDeviceInfoFromStackDev(NextKeyDevice[0], PriorDevice[0]);
                                            if (cdi != null)
                                            {//顶升机构被堆垛机取货时检测在顶升高位,有货
                                                //AheadDetect.Append("D-").Append(cdi[1]).Append(";D-").Append(cdi[3]);
                                                AheadDetect.Append(";D-").Append(cdi[3]);
                                            }
                                            //20101011
                                            else
                                            {
                                                tw = GetBindingDeviceIndex(PriorDevice[0]);
                                                if (tw != "")
                                                {//20101118
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(PriorDevice[0]));
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                            }



                                            //20090803检测设备是否空闲idle
                                            AheadDetect.Append(";").Append("I").Append(PriorDevice[0].ToString());
                                          //  AheadDetect.Append(";R").Append(PriorDevice[0].ToString());//20140109
                                            #endregion

                                            if (NextDevice.Count > 0)//晟碟
                                            {
                                                if (NextDevice[1] == 10) //站台间移库不加J
                                                {
                                                    ////AheadDetect.Append(";J").Append(NextKeyDevice[0].ToString());//双升取货就得检查
                                                }
                                            }

                                        }
                                        else
                                        {
                                            AheadDetect.Append(GetGDDetect);
                                        }
                                    }
                                    if (NextDevice.Count > 0)
                                    {
                                        if (NextDevice[1] == 2)
                                        {


                                            //20101011
                                            string[] cdi = ccf.GetLanewayDeviceInfoFromStackDev(NextKeyDevice[0], NextDevice[0]);
                                            if (cdi != null)
                                            {///20120906检测被堆垛机送货的顶升机构在顶升高位,无物,近巷道5和远巷道6也无物
                                                //AheadDetect.Append(";D-").Append(cdi[1]).Append(";D").Append(cdi[3]);
                                                //if (cdi[5] != "")
                                                //{
                                                //    AheadDetect.Append(";D").Append(cdi[5]);
                                                //}
                                                //if (cdi[6] != "")
                                                //{
                                                //    AheadDetect.Append(";D").Append(cdi[6]);
                                                //}
                                            }
                                            else
                                            {
                                                //tw = GetBindingDeviceIndex(NextDevice[0]);
                                                //if (tw != "")
                                                //{
                                                //    AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                //}
                                                //tw = GetBindingDeviceIndexOut(NextDevice[0]);
                                                //if (tw != "")
                                                //{
                                                //    AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                //}
                                            }

                                            tw = GetBindingDeviceIndex(NextDevice[0]);
                                            if (tw != "")
                                            {
                                                AheadDetect.Append(";D").Append(tw.ToString()).Append(";");//.Append(GetBeDetectedDevices(NextDevice[0]));//20101124
                                            }
                                            //20101124检测被堆垛机送货的输送机空闲
                                            AheadDetect.Append(";").Append("I").Append(NextDevice[0].ToString()).Append(";");
                                                //.Append("N").Append(NextDevice[0].ToString());
                                            //20140309AheadDetect.Append(";R").Append(NextDevice[0].ToString());
                                            ////20120405
                                            //string r2 = "12002,42002,32002,12010,42010,32010,12018,42018,32018";

                                            //if (r2.IndexOf(NextDevice[0].ToString()) >= 0)
                                            //{
                                            //    AheadDetect.Append(";D").Append(Convert.ToInt32(NextDevice[0] - 1).ToString()).Append(".0;N").Append(Convert.ToInt32(NextDevice[0] - 1).ToString()); ;
                                            //}
                                        }
                                        else
                                        {
                                            AheadDetect.Append(";").Append(SendGDDetect);
                                        }
                                    }
                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(NextKeyDevice[0]));//20101124
                                   AheadDetect.Append(";D").Append(GetBindingDeviceIndex(NextKeyDevice[0]));
                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], 4, 0) ==
                                        false)
                                    {
                                        lifterUP = ccf.GetMonitorIndex(ManFID, Mankind);//升降机顶升
                                        snextq = ccf.GetMonitorIndex(ManFID, Mankind);
                                        snexts = ccf.GetMonitorIndex(ManFID, Mankind);

                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);

                                        sql.Remove(0, sql.Length);//20101124
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,").Append(
                                                " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork,F_PriorMonitor,F_AgvTask,F_AGVNextTask)").Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                            ).Append(",").Append(NextKeyDevice[0]).Append(",2,").Append(routeIDSub).Append(",").Append(status).Append(",").Append(Coor[0]).Append(",").Append(Coor[1]
                                            ).Append(",").Append(Coor[2]).Append(",").Append(Coor[3]).Append(",").Append(Coor[4]).Append(",").Append(Coor[5])
                                            .Append(",'").Append(AheadDetect.ToString()).Append("','").Append(RunningLock.ToString()).Append("','").Append(drv["FPALLETBARCODE"])
                                            .Append("','").Append(drv["FUseAwayFork"]).Append("',").Append(lifterUP).Append(",").Append(snextq).Append(",").Append(snexts).Append(")");
                                        dbo.ExceSQL(sql.ToString());
                                    }


                                    #endregion

                                    #region 送货
                                    RunningLock.Remove(0, RunningLock.Length);//20101124
                                    AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                    if (NextDevice.Count > 0)
                                    {
                                        if (NextDevice[1] == 2)
                                        {
                                            //送货做提前检测+运行时锁定
                                            ////20120405
                                            //string r2 = "12002,42002,32002,12010,42010,32010,12018,42018,32018";

                                            #region 运行时锁定设备组

                                            //sql.Remove(0, sql.Length);//20101124
                                            //sql.Append("SELECT F_LaneGateDeviceIndex FROM T_Base_Lane_Gate WHERE (F_LaneGateDeviceIndex =").Append(NextDevice[0]).Append(") AND (F_RunLock = '1')");
                                            //DataView dvlane = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                            ////20101124
                                            //if (dvlane.Count > 0)
                                            //{
                                            //    RunningLock.Append(NextDevice[0].ToString());//20101124
                                            //    //20120405
                                            //    if (r2.IndexOf(NextDevice[0].ToString()) >= 0)
                                            //    {
                                            //        RunningLock.Append(NextDevice[0].ToString()).Append(";").Append((NextDevice[0] - 1).ToString());
                                            //    }
                                            //}
                                            //else
                                            //{
                                            //    RunningLock.Remove(0, RunningLock.Length);//20101124
                                            //}

                                            #endregion

                                            #region 运行提前检测光电组

                                            AheadDetect.Remove(0, AheadDetect.Length); //20101124
                                            //20101011
                                            string[] cdi =
                                                ccf.GetLanewayDeviceInfoFromStackDev(NextKeyDevice[0], NextDevice[0]);
                                            if (cdi != null)
                                            {
                                                //20120906检测被堆垛机送货的顶升机构在顶升高位,无物,近巷道5和远巷道6也无物
                                                //AheadDetect.Append(";D-").Append(cdi[1]).Append(";D").Append(cdi[3]);
                                                //if (cdi[5] != "")
                                                //{
                                                //    AheadDetect.Append(";D").Append(cdi[5]);
                                                //}

                                                //if (cdi[6] != "")
                                                //{
                                                //    AheadDetect.Append(";D").Append(cdi[6]);
                                                //}
                                            }
                                            else
                                            {
                                                //AheadDetect.Remove(0, AheadDetect.Length); //20101124
                                                //tw = GetBindingDeviceIndexOut(NextDevice[0]);
                                                //if (tw != "")
                                                //{
                                                //    AheadDetect.Append(";D").Append(tw.ToString()).Append(";")
                                                //        .Append(GetBeDetectedDevices(NextDevice[0])); //20101124
                                                //}
                                                //else
                                                //{
                                                //    AheadDetect.Remove(0, AheadDetect.Length);
                                                //}
                                            }

                                            tw = GetBindingDeviceIndex(NextDevice[0]);
                                            if (tw != "")
                                            {
                                                AheadDetect.Append(";D").Append(tw.ToString()).Append(";");
                                                // .Append(GetBeDetectedDevices(NextDevice[0]));
                                            }
                                            ////20090803检测设备是否空闲idle
                                            //AheadDetect.Append(";").Append("I").Append(NextDevice[0].ToString());//20101124
                                            ////20090918给堆垛机送货的输送机增加是否逻辑有物的判断
                                            //AheadDetect.Append(";").Append("N").Append(NextDevice[0].ToString());//20101124

                                            //20101124检测被堆垛机送货的输送机空闲
                                            AheadDetect.Append(";").Append("I").Append(NextDevice[0].ToString());
                                            //  .Append(";").Append("N").Append(NextDevice[0].ToString())
                                            ////20120405
                                            //if (r2.IndexOf(NextDevice[0].ToString()) >= 0)
                                            //{
                                            //    AheadDetect.Append(";D").Append(Convert.ToInt32(NextDevice[0] - 1).ToString()).Append(".0;N").Append(Convert.ToInt32(NextDevice[0] - 1).ToString()); ;
                                            //}

                                            #endregion
                                        }
                                        else if (NextDevice[1] == 10)// 晟碟 入库和移库送货都加双升检测
                                        {
                                            //AheadDetect.Append("J").Append(PriorDevice[0].ToString());// 入库和移库送货都加双升检测
                                        }
                                        else
                                        {
                                            AheadDetect.Append(";").Append(SendGDDetect);
                                        }

                                        AheadDetect.Append(";")
                                            .Append(GetBeDetectedDevices(NextKeyDevice[0])); //20101124
                                        AheadDetect.Append(";D-").Append(GetBindingDeviceIndex(NextKeyDevice[0]));
                                        //送货
                                        if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], 5, 0) ==
                                            false)
                                        {

                                            int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                            //mindex = ccf.GetMonitorIndex(ManFID, Mankind);//20110510
                                            sql.Remove(0, sql.Length); //20101124
                                            sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,")
                                                .Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,")
                                                .Append(
                                                    " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork,F_PriorMonitor,F_AGVTask,F_AGVNextTask)")
                                                .Append(
                                                    "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",")
                                                .Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                ).Append(",").Append(NextKeyDevice[0]).Append(",3,").Append(routeIDSub)
                                                .Append(",").Append(status).Append(",").Append(Coor[0]).Append(",")
                                                .Append(Coor[1]
                                                ).Append(",").Append(Coor[2]).Append(",").Append(Coor[3]).Append(",")
                                                .Append(Coor[4]).Append(",")
                                                .Append(Coor[5]).Append(",'").Append(AheadDetect.ToString())
                                                .Append("','").Append(RunningLock.ToString()).Append("','")
                                                .Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"])
                                                .Append("',").Append(lifterUP).Append(",").Append(snextq).Append(",")
                                                .Append(snexts).Append(")");
                                            dbo.ExceSQL(sql.ToString());
                                            //20101124
                                        }
                                    }

                                    #endregion

                                    #endregion
                                }

                                #region 插入高端U型线控制//20111020
                                if (NextDevice.Count > 0 && Convert.ToInt32(drv["FENDUCODE"]) > 0)
                                {//直接插入高端设备U型内部的送出指令
                                    int arrowdev = Convert.ToInt32(drv["FENDCELL"]);
                                    CurDevice[3] = 6;
                                    int curdev = 0; int udev = Convert.ToInt32(drv["FENDUCODE"]);
                                    GetUAreaInputDevice(NextKeyDevice[0], Convert.ToInt32(drv["FENDUCODE"]), out curdev, out Uzxy);
                                    if (curdev > 0)
                                    {
                                        CurDevice[0] = curdev;
                                        #region 运行提前检测光电组
                                        AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                        //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                        tw = GetSendOutDetect(CurDevice[0]);
                                        if (tw != "")
                                        {//20101124
                                            AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                        }
                                        else
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);
                                        }



                                        #endregion

                                        AheadTrigger.Remove(0, AheadTrigger.Length);
                                        if (DeviceAndOrderExitInMonitor(Mankind, ManFID, udev, CurDevice[3], arrowdev) == false)
                                        {

                                            #region 生成高端输送机命令
                                            int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                            TriggerTaskNo = mindex;
                                            //20101124
                                            sql.Remove(0, sql.Length);
                                            sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                " F_AheadDetect,F_TxtParam,F_UseAwayFork)").Append(
                                            "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                ).Append(",").Append(udev).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append("," + arrowdev
                                                ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"]).Append("')");
                                            dbo.ExceSQL(sql.ToString());


                                            #endregion
                                        }
                                    }
                                }
                                #endregion
                                //修改T_Manage_Task的FLANEWAY,FSTACK
                                sql.Remove(0, sql.Length);//20101124
                                sql.Append("SELECT F_LaneDeviceIndex,F_StackIndex  FROM T_Base_LaneInfo where F_StackIndex=").Append(NextKeyDevice[0]);
                                dvs = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                //20101124
                                int laneway = 0;
                                if (dvs.Count > 0)
                                {
                                    laneway = Convert.ToInt32(dvs[0]["F_LaneDeviceIndex"]);

                                }//20101124
                                sql.Remove(0, sql.Length);
                                sql.Append("update T_Manage_Task set FLANEWAY=").Append(laneway).Append(",FSTACK=").Append(NextKeyDevice[0]).Append(" where (FLANEWAY=-1) and (FSTACK=-1) and (F_ManageTaskKindIndex=").Append(Mankind).Append(") and FID=").Append(ManFID);
                                dbo.ExceSQL(sql.ToString());
                                //20101124
                                break;

                            #endregion
                            case 2://输送机(关键点:例如十字路口(有前一个和后一个设备)或者路径终点(没有后一个设备))
                                #region 输送机
                                if (NextDevice.Count == 0)//终点输送机
                                {
                                    if (uncontrol != "1")
                                    {//20091107
                                        List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                        if (newcur != null)
                                        {
                                            CurDevice = newcur;
                                        }
                                        int arrowdev;
                                        if (CurDevice[0] == NextKeyDevice[0])
                                        {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                            arrowdev = 0;

                                            #region 运行提前检测光电组
                                            if ((CurDevice[3] == 1) || ((CurDevice[3] == 2)))
                                            {
                                                //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                tw = GetBindingDeviceIndex(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect .Append( "D-" ).Append( tw.ToString() ).Append( ";" ).Append( GetBeDetectedDevices(CurDevice[0]));//20101124
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect .Append( ";D" ).Append( tw.ToString());//20101124
                                                }

                                            }
                                            //else if (CurDevice[3] == 2)
                                            //{
                                            //    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                            //    if (tw != "")
                                            //    {
                                            //        AheadDetect = "D-" + tw.ToString() + ";" + GetBeDetectedDevices(CurDevice[0]);//20101118
                                            //    }
                                            //    else
                                            //    {
                                            //        AheadDetect.Remove(0, AheadDetect.Length);
                                            //    }
                                            //}
                                            ////20090803检测设备是否空闲idle
                                            //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                            #endregion
                                        }
                                        else
                                        {
                                            arrowdev = NextKeyDevice[0];
                                            CurDevice[3] = 6;
                                            #region 运行提前检测光电组
                                            //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物

                                            AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                            string[] cdi = ccf.GetOwnCorrel_DeviceInfo(CurDevice[0]);
                                            if (null != cdi)
                                            {//20100406检测被堆垛机送货的顶升机构在顶升高位,空闲
                                                AheadDetect.Append(";D-").Append(cdi[1]).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118
                                            }
                                            //20101011
                                            else
                                            {
                                                tw = GetSendOutDetect(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101124
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                //20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                //20161221
                                                List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                if (nextConveyor.Count > 0)//20110331
                                                {
                                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));

                                                    tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                    }
                                                    tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                    }
                                                    ////20090803检测设备是否空闲idle
                                                    //AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                }
                                            }
                                            
                                            #endregion
                                        }
                                        if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                        {
                                            #region 运行时锁定设备组

                                            #endregion

                                            #region 生成输送机命令
                                            int mindex = ccf.GetMonitorIndex(ManFID, Mankind);

                                            sql.Remove(0, sql.Length);
                                            sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                                " F_AheadDetect,F_TxtParam)" ).Append(
                                            "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                                ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( CurDevice[3] ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( arrowdev
                                                ).Append( ",'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                            dbo.ExceSQL(sql.ToString());
                                            #endregion
                                        }

                                    }
                                }
                                
                                else//20110104与AGV交互的顶升机或者路径十字路口
                                {
                                    int arrowdev;
                                    #region 前一设备是输送机

                                    if (PriorDevice[1] == 2)
                                    {
                                        if (uncontrol != "1")
                                        {//20091107
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;
                                            }


                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                tw = GetBindingDeviceIndex(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101124
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                tw = GetSendOutDetect(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101124
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();

                                                List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                if (nextConveyor.Count > 0)//20110331
                                                {
                                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));//20110331

                                                    tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                    }
                                                    tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                    }
                                                    ////20090803检测设备是否空闲idle
                                                    //AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                }
                                                #endregion
                                            }
                                            AheadTrigger.Remove(0, AheadTrigger.Length);
                                            AheadTrigger.Append(PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                #region 运行时锁定设备组
                                                RunningLock.Clear();//20120420


                                                #endregion

                                                #region 生成输送机命令
                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                sql.Remove(0, sql.Length);//20101124
                                                sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                    " F_AheadDetect,F_TxtParam,F_RunningLock)").Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                    ).Append(",").Append(CurDevice[0]).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append(",").Append(arrowdev
                                                    ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(RunningLock).Append("')");
                                                dbo.ExceSQL(sql.ToString());
                                                #endregion
                                            }

                                        }


                                    }

                                    #endregion

                                    #region 顶升机前一输送机PriorDevice[0]空闲,有物:允许顶升机下降
                                    //arrowdev = 0;
                                    //#region 运行提前检测光电组
                                    ////CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                    //AheadDetect.Remove(0, AheadDetect.Length);//20101124

                                    //tw = GetBindingDeviceIndexOut(PriorDevice[0]);
                                    //if (tw != "")
                                    //{
                                    //    AheadDetect.Append(";D-").Append(tw.ToString());//20101124
                                    //}
                                    //AheadDetect.Append(";I").Append( PriorDevice[0].ToString());
                                    //#endregion
                                    //AheadTrigger.Remove(0, AheadTrigger.Length);

                                    //if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], NextKeyDevice[3], 0) == false)
                                    //{
                                    //    #region 运行时锁定设备组

                                    //    #endregion

                                    //    #region 生成输送机命令
                                    //    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                    //    TriggerTaskNo = mindex;
                                    //    sql.Remove(0, sql.Length);//20101124
                                    //    sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                    //        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                    //        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                    //        " F_AheadDetect,F_TxtParam)").Append(
                                    //    "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                    //        ).Append(",").Append(NextKeyDevice[0]).Append(",").Append(NextKeyDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(NextKeyDevice[0]).Append(",").Append(arrowdev
                                    //        ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                    //    dbo.ExceSQL(sql.ToString());
                                    //    #endregion
                                    //}
                                    #endregion
                                }
                                #endregion
                                break;
                            case 4:
                                #region RGV
                                //RGV取货:
                                if (PriorDevice.Count > 0)
                                {
                                    //如果PriorDevice是输送机则生成输送机命令【CurDevice-->PriorDevice目标设备,出库:2】,
                                    //收到输送机PriorDevice的PLC上报的运行状态则提前触发RGV运行命令(7),然后是取送货命令,获取堆垛机取坐标
                                    #region 前一设备是输送机

                                    if (PriorDevice[1] == 2)
                                    {
                                        if ((uncontrol != "1") && (CurDevice[1] == 2))
                                        {//20091107
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;
                                            }
                                            int arrowdev;

                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                tw = GetBindingDeviceIndex(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    // AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101124
                                                    AheadDetect.Append("D-").Append(tw.ToString());
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                AheadDetect.Remove(0, AheadDetect.Length);//20101124
                                                tw = GetBindingDeviceIndex(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101124
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();

                                                //List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                //if (nextConveyor.Count > 0)//20110331
                                                //{
                                                //    AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));
                                                //    tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                //    if (tw != "")
                                                //    {
                                                //        AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                //    }
                                                //    tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                //    if (tw != "")
                                                //    {
                                                //        AheadDetect.Append(";D").Append(tw.ToString());//20101124
                                                //    }
                                                //    ////20090803检测设备是否空闲idle
                                                //    //AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                //}
                                                #endregion
                                            }
                                            AheadTrigger.Remove(0, AheadTrigger.Length);
                                            AheadTrigger.Append(PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                #region 运行时锁定设备组

                                                #endregion

                                                #region 生成输送机命令
                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                sql.Remove(0, sql.Length);//20101124
                                                sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                    " F_AheadDetect,F_TxtParam)").Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                    ).Append(",").Append(CurDevice[0]).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append(",").Append(arrowdev
                                                    ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                                dbo.ExceSQL(sql.ToString());
                                                #endregion
                                            }

                                        }
                                        


                                    }
                                    else
                                    {//20111020
                                        List<int> newPrior = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                        if (newPrior != null)
                                        {
                                            PriorDevice = newPrior;
                                        }
                                    }
                                    #endregion

                                    #region 获得RGV运行的接货位的设备索引
                                    Coor[0] = PriorDevice[0];
                                    Coor[1] = 0;
                                    Coor[2] = 0;
                                    Coor[3] = 0;
                                    Coor[4] = 0;
                                    Coor[5] = 0;
                                    #endregion

                                    #region RGV的接货(被关联的任务)
                                    Rgvorder = 4; //GetRGVOrder(PriorDevice[0], true);
                                    
                                    #region 运行提前检测光电组
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    #region 目标输送机运行提前检测光电组
                                    if (NextDevice.Count > 0)
                                    {
                                        if (NextDevice[0] == 13003 || NextDevice[0] == 13004)//民航二所出库对接站台
                                        {
                                            tw = GetBindingDeviceIndex(NextDevice[0]);
                                           // AheadDetect.Append(";D-").Append(tw.ToString()).Append(";");
                                        }
                                        else
                                        {
                                            //民航二所入库取时候不用判断目标 提升机只用来入库 目标放不下去也得取上来  加快节奏
                                            //tw = GetBindingDeviceIndex(NextDevice[0]);//目标输送机入口无探物,能运行
                                            //if (tw != "")
                                            //{
                                            //    AheadDetect.Append(";D").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(NextDevice[0]));//20101124
                                            //}
                                            //20111215
                                            tw = GetBindingDeviceIndexOut(NextDevice[0]);//目标输送机出口无探物,能运行
                                            if (tw != "")
                                            {
                                                AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                            }
                                            //20090803检测设备是否空闲idle
                                            //AheadDetect.Append(";").Append("I").Append(NextDevice[0].ToString());//20101124
                                            //AheadDetect.Append(";I").Append(NextDevice[0]);//.Append(";L").Append(NextDevice[0]);  //20160421
                                        }
                                    }
                                    #endregion
                                    #region 取货输送机运行提前检测光电组
                                    if (PriorDevice[0] == 13001 || PriorDevice[0] == 13002)//民航二所入库对接站台
                                    {
                                        tw = GetBindingDeviceIndex(PriorDevice[0]);
                                       // AheadDetect.Append(";D-").Append(tw.ToString()).Append(";");
                                    }
                                    else
                                    {
                                        tw = GetBindingDeviceIndex(PriorDevice[0]);
                                        if (tw != "")
                                        {
                                            AheadDetect.Append(";D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(PriorDevice[0]));
                                        }
                                        //else
                                        //{
                                        //    AheadDetect.Remove(0, AheadDetect.Length);
                                        //}
                                        //20090803检测设备是否空闲idle
                                        // AheadDetect.Append(";").Append("I").Append(PriorDevice[0].ToString());//20101124
                                    }
                                        #endregion
                                        tw = GetBindingDeviceIndex(NextKeyDevice[0]);
                                        if (tw != "")
                                        {
                                            AheadDetect.Append(";D").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(NextKeyDevice[0]));
                                        }
                                       // AheadDetect.Append(";I").Append(NextKeyDevice[0]);
                                    
                                    #endregion

                                    

                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], Rgvorder, 0) == false)
                                    {
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                        sql.Remove(0, sql.Length);
                                        sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2," ).Append(
                                            " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_TxtParam)" ).Append(
                                        "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                            ).Append( "," ).Append( NextKeyDevice[0] ).Append( "," ).Append( Rgvorder).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( PriorDevice[0] ).Append( ",0" ).Append(
                                            ",0,0,0,0,'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                        dbo.ExceSQL(sql.ToString());
                                        sql.Remove(0, sql.Length);
                                        sql.Append("update T_Monitor_Task set F_Associate=" ).Append( mindex ).Append( " where F_MonitorIndex=" ).Append( mainTask);
                                        dbo.ExceSQL(sql.ToString());

                                    }
                                    //20101124
                                    #endregion

                                    #region RGV是条码绑定设备需要条码比对
                                    //int barcodedev = GetBarcodeDeviceFromBindDevice(NextKeyDevice[0]);
                                    //if (barcodedev > 0)
                                    //{
                                    //    #region 生成条码比对命令
                                    //    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, barcodedev, 1, 0) == false)
                                    //    {
                                    //        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                    //        sql.Remove(0, sql.Length);
                                    //        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                    //            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                    //            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                    //            " F_AheadDetect,F_TxtParam)").Append(
                                    //        "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                    //            ).Append(",").Append(barcodedev).Append(",").Append(1).Append(",").Append(ccf.GetRouteIDsub(barcodedev)).Append(",").Append(status).Append(",0,0,'").Append("").Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                    //        dbo.ExceSQL(sql.ToString());
                                    //    }
                                    //    #endregion
                                    //}
                                    #endregion
                                }

                                //获得RGV运行的送货位的设备索引:
                                if (NextDevice.Count > 0)
                                {
                                    Coor[0] = NextDevice[0];
                                    Coor[1] = 0;
                                    Coor[2] = 0;
                                    Coor[3] = 0;
                                    Coor[4] = 0;
                                    Coor[5] = 0;

                                    #region RGV的送货
                                
                                Rgvorder =5;// GetRGVOrder(NextDevice[0], false);
                                
                                    

                                #region 运行提前检测光电组


                                AheadDetect .Remove(0,AheadDetect.Length);
                                    if (NextDevice[0] == 13003 || NextDevice[0] == 13004)//民航二所出库对接站台
                                    {
                                        tw = GetBindingDeviceIndex(NextDevice[0]);
                                        //AheadDetect.Append(";D-").Append(tw.ToString()).Append(";");
                                    }
                                    else
                                    {

                                        tw = GetBindingDeviceIndex(NextDevice[0]);//目标输送机入口无探物,能运行
                                        if (tw != "")
                                        {
                                            AheadDetect.Append(";D").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(NextDevice[0]));//20101118
                                        }
                                        //else
                                        //{
                                        //    AheadDetect.Remove(0, AheadDetect.Length);
                                        //}
                                        //20111215
                                        //tw = GetBindingDeviceIndexOut(NextDevice[0]);//目标输送机出口无探物,能运行
                                        //if (tw != "")
                                        //{
                                        //    AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                        //}
                                        //AheadDetect.Append(";I").Append(NextDevice[0]).Append(";L").Append(NextDevice[0]);  //20160421
                                    }
                                tw = GetBindingDeviceIndex(NextKeyDevice[0]);//RGV有探物,能运行
                                if (tw != "")
                                {
                                    AheadDetect .Append(";" ).Append( "D-" ).Append( tw.ToString() ).Append( ";" ).Append( GetBeDetectedDevices(NextKeyDevice[0]));//20101118
                                }
                              //  AheadDetect.Append(";I").Append(NextKeyDevice[0]);
                                #endregion

                                #region 20210201_上海晟碟,如果是穿梭板取送货提升机,添加K19004,判断是否有19004的换层任务,如果有,先不执行

                                //if (NextKeyDevice[0] == 19003 || NextKeyDevice[0] == 19004)
                                //{
                                //    AheadDetect.Append(";K19005");
                                //}

                                #endregion

                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], Rgvorder, 0) == false)
                                {
                                    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                    sql.Remove(0, sql.Length);
                                    sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2," ).Append(
                                        " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_TxtParam)" ).Append(
                                    "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                        ).Append( "," ).Append( NextKeyDevice[0] ).Append( "," ).Append( Rgvorder ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( NextDevice[0] ).Append( ",0" ).Append(
                                        ",0,0,0,0,'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                    dbo.ExceSQL(sql.ToString());
                                    mainTask = mindex;
                                }

                                #endregion

                                }

                                break;



                                #endregion
                            case 6:
                                #region AGV

                                #region 生成一个设备命令并且获得取送货坐标
                                //AGV取货:
                                if (PriorDevice.Count > 0)
                                {
                                    #region 前一设备是输送机
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    if (PriorDevice[1] == 2)
                                    {
                                        if (uncontrol != "1")
                                        {//20091107
                                            bool IfConveyor = false;
                                            if (PriorKeyDevice0.Count > 0)
                                            {
                                                if (PriorKeyDevice0[1] != 2)//20110104前一关键设备不是类输送机
                                                {
                                                    IfConveyor = false;
                                                }
                                                else
                                                {
                                                    IfConveyor = true;
                                                }
                                            }
                                            else
                                            {
                                                IfConveyor = false;
                                            }
                                            if (IfConveyor == false)//20110104前一关键设备不是类输送机
                                            {
                                                List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                                if (newcur != null)
                                                {
                                                    CurDevice = newcur;
                                                }
                                                int arrowdev;
                                                if (CurDevice[0] == PriorDevice[0])
                                                {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                    arrowdev = 0;
                                                    #region 运行提前检测光电组
                                                    //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                    #region 威派格注释
                                                    //tw = GetBindingDeviceIndex(CurDevice[0]);
                                                    //if (tw != "")
                                                    //{
                                                    //    AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118


                                                    //}
                                                    //else
                                                    //{
                                                    //    AheadDetect.Remove(0, AheadDetect.Length);
                                                    //} 
                                                    #endregion
                                                    tw = GetSendOutDetect(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118
                                                    }
                                                    ////20090803检测设备是否空闲idle
                                                    //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                    if (CStaticClass.AGVAllowSingleFork == "0")//20100710
                                                    {
                                                        #region 隔壁输送机空闲无物
                                                        //奇数加一;偶数减一
                                                        if ((CurDevice[0] % 2) == 0)
                                                        {
                                                            AheadDetect.Append(";D").Append(GetBindingDeviceIndexOut(CurDevice[0] - 1).ToString());//20101118
                                                        }
                                                        else
                                                        {
                                                            AheadDetect.Append(";D").Append(GetBindingDeviceIndexOut(CurDevice[0] + 1).ToString());//20101118
                                                        }
                                                        #endregion
                                                    }
                                                    #endregion
                                                }
                                                else
                                                {
                                                    arrowdev = PriorDevice[0];
                                                    CurDevice[3] = 6;
                                                    #region 运行提前检测光电组
                                                    //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                    tw = GetSendOutDetect(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118


                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }
                                                    ////20090803检测设备是否空闲idle
                                                    //AheadDetect += ";" + "I" + CurDevice[0].ToString();

                                                    List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                    if (nextConveyor.Count > 0)//20110331
                                                    {
                                                        AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));
                                                        tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                        }
                                                        tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                        }

                                                        AheadDetect.Append(";").Append("N").Append(nextConveyor[0].ToString());
                                                        ////20090803检测设备是否空闲idle
                                                        //AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                    }
                                                    
                                                    #region 22034\22035
                                                    if (arrowdev == 22036)//20101203
                                                    {
                                                        AheadDetect.Append(";D").Append("22036.0");
                                                    }
                                                    if (arrowdev == 22037)//20101203
                                                    {
                                                        AheadDetect.Append(";D").Append("22037.0");
                                                    }
                                                    #endregion
                                                    #endregion
                                                }
                                            
                                                if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                                {
                                                    #region 运行时锁定设备组

                                                    #endregion

                                                    #region 生成输送机命令
                                                    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                    TriggerTaskNo = mindex;
                                                    sql.Remove(0, sql.Length);
                                                    sql.Append( "INSERT INTO T_Monitor_Task " ).Append(
                                                        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                                        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                                        " F_AheadDetect,F_TxtParam)" ).Append(
                                                    "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                                        ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( CurDevice[3] ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( arrowdev
                                                        ).Append( ",'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                                    dbo.ExceSQL(sql.ToString());
                                                    #endregion
                                                }
                                                #region 顶升机下降20110210
                                                //AheadDetect.Remove(0, AheadDetect.Length);
                                                //AheadDetect.Append("I").Append(Convert.ToString(arrowdev-2)).Append(";I").Append(Convert.ToString(arrowdev));
                                                //tw = ccf.GetBindingDeviceIndexOut(arrowdev - 2);
                                                //if (tw != "")
                                                //{
                                                //    AheadDetect .Append( ";D-" ).Append( tw.ToString());
                                                //}
                                                //AheadDetect.Append(";D").Append(arrowdev.ToString()).Append(".2");//20110331
                                                //if (DeviceAndOrderExitInMonitor(Mankind, ManFID, arrowdev, 8, 0) == false)
                                                //{
                                                //    #region 运行时锁定设备组

                                                //    #endregion

                                                //    #region 生成输送机命令
                                                //    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                    
                                                //    sql.Remove(0, sql.Length);
                                                //    sql.Append( "INSERT INTO T_Monitor_Task " ).Append(
                                                //        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                                //        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                                //        " F_AheadDetect,F_TxtParam)" ).Append(
                                                //    "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                                //        ).Append( "," ).Append( arrowdev ).Append( "," ).Append( 8 ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status 
                                                //        ).Append( "," ).Append( arrowdev ).Append( "," ).Append( 0
                                                //        ).Append( ",'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                                //    dbo.ExceSQL(sql.ToString());
                                                //    #endregion
                                                //}
                                                #endregion
                                            }
                                        }
                                    }

                                    #endregion

                                    int[] Coor0_2 = new int[6] { 0, 0, 0, 0, 0, 0 };
                                    //如果PriorDevice是AGV通道,PriorDevice不拆分命令,获取送坐标,拆分AGV命令取、送货
                                    if (PriorDevice[1] == 11)
                                    {
                                        Coor0_2 =GetAGVAddressFromManage (Mankind, ManFID, true);
                                    }
                                    else
                                    {
                                        //如果NextDevice是输送机则取送货命令,获取送坐标

                                        //如果NextDevice是固定站台则取送货命令,获取送坐标
                                        Coor0_2[1] =PriorDevice[0];
                                    }
                                    Coor[0] = Coor0_2[0];
                                    Coor[1] = Coor0_2[1];//站台设备索引
                                    Coor[2] = Coor0_2[2];
                                }
                                if (NextDevice.Count > 0)
                                {
                                    int[] Coor3_5 = new int[6] { 0, 0, 0, 0, 0, 0 };
                                    //如果NextDevice是AGV通道,NextDevice不拆分命令,获取送坐标,拆分AGV命令取、送货
                                    if (NextDevice[1] == 11)
                                    {
                                        Coor3_5 = GetAGVAddressFromManage(Mankind, ManFID, false);
                                    }
                                    else
                                    {
                                        //如果NextDevice是输送机则取送货命令,获取送坐标

                                        //如果NextDevice是固定站台则取送货命令,获取送坐标
                                        Coor3_5[4] =NextDevice[0];
                                    }
                                    Coor[3] = Coor3_5[3];
                                    Coor[4] = Coor3_5[4];//站台设备索引
                                    Coor[5] = Coor3_5[5];
                                }
                                #endregion

                                #region 生成AGV指令:取送货//20110104
                                //转换调度任务类型和AGV任务类型
                                int agvorder = 0;
                                int station;
                                #region AGV取货
                                if (PriorDevice.Count > 0)
                                {
                                    agvorder = 2;//AGV取货
                                    RunningLock.Remove(0, RunningLock.Length) ;
                                    AheadDetect.Remove(0, AheadDetect.Length);

                                    station = Coor[1];
                                    ;
                                    if (CStaticClass.GetDeviceKindIdx(station) == 2)
                                    {//20100714
                                        #region 运行时锁定设备组
                                        RunningLock.Append(station.ToString());
                                        //取货时锁定隔壁输送机
                                        if (CStaticClass.AGVAllowSingleFork == "0")
                                        {
                                            if ((PriorDevice[0] % 2) == 0)
                                            {
                                                RunningLock .Append( ";" ).Append( (PriorDevice[0] - 1).ToString());
                                            }
                                            else
                                            {
                                                RunningLock.Append( ";" ).Append( (PriorDevice[0] + 1).ToString());
                                            }
                                        }
                                        #endregion
                                        if (station == 12001)
                                        {
                                            AheadDetect.Append("D-").Append(GetBindingDeviceIndex(station).ToString()).Append(";I").Append(station.ToString());
                                        }
                                        else
                                        if (station == 12002)
                                        {
                                            AheadDetect.Append("D-12002.0").Append(";I").Append(station.ToString());
                                        }
                                        else
                                        {
                                            AheadDetect.Append("D-").Append(GetBindingDeviceIndexOut(station).ToString()).Append(";I").Append(station.ToString());
                                        }
                                            #region 增加检测顶升是否在低位20100710
                                        //AheadDetect .Append( ";D-" ).Append( PriorDevice[0].ToString() ).Append( ".2");
                                        #endregion
                                    }
                                    
                                    if (NextDevice.Count > 0)
                                    {
                                        station = Coor[4];

                                   
                                      //if (CStaticClass.GetDeviceKindIdx(station) == 2)
                                      //  {//20100714
                                      //      if (station == 12002)
                                      //      {
                                      //          AheadDetect.Append(";D12002.0;D12002.1;D12002.2").Append(";I").Append(station.ToString()).Append(";N").Append(station.ToString());
                                      //      }
                                      //      else
                                      //      {
                                      //          AheadDetect.Append(";D").Append(GetBindingDeviceIndex(station).ToString()).Append(";I").Append(station.ToString()).Append(";N").Append(station.ToString());
                                      //      }
                                      //  }
                                        
                                    }
                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], agvorder, 0) == false)
                                    {//20091107
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                        mindex = ccf.GetMonitorIndex(ManFID, Mankind);//20110104
                                        mindex = ccf.GetMonitorIndex(ManFID, Mankind);//20110104
                                        sql.Remove(0, sql.Length);
                                        sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2," ).Append(
                                            " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork)" ).Append(
                                        "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                            ).Append( "," ).Append( NextKeyDevice[0] ).Append( ",").Append(agvorder).Append("," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( Coor[0] ).Append( "," ).Append( Coor[1]
                                            ).Append( "," ).Append( Coor[2] ).Append( "," ).Append( Coor[3] ).Append( "," ).Append( Coor[4] ).Append( "," ).Append( Coor[5] ).Append( ",'" )
                                            .Append( AheadDetect.ToString() ).Append( "','" ).Append( RunningLock.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "','" ).Append( drv["FUseAwayFork"] ).Append( "')");
                                        dbo.ExceSQL(sql.ToString());
                                        
                                    }


                                }
                                #endregion

                                #region AGV送货
                                if (NextDevice.Count > 0)
                                {
                                    agvorder = 3;//AGV送货
                                    RunningLock.Remove(0, RunningLock.Length);
                                    AheadDetect.Remove(0, AheadDetect.Length);

                                    station = Coor[4];

                                   
                                    if (CStaticClass.GetDeviceKindIdx(station) == 2)
                                    {//20100714
                                        
                                        agvorder = 1;//AGV运动

                                        if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], agvorder, 0) == false)
                                        {
                                            int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                            mindex = ccf.GetMonitorIndex(ManFID, Mankind);//20110104
                                            mindex = ccf.GetMonitorIndex(ManFID, Mankind);//20110104
                                            sql.Remove(0, sql.Length);
                                            sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,").Append(
                                                " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork)").Append(
                                            "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                ).Append(",").Append(NextKeyDevice[0]).Append(",").Append(agvorder).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(Coor[0]).Append(",").Append(Coor[4]
                                                ).Append(",").Append(Coor[2]).Append(",").Append(Coor[3]).Append(",").Append(Coor[4]).Append(",").Append(Coor[5]).Append(",'")
                                                .Append(AheadDetect.ToString()).Append("','").Append(RunningLock.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"]).Append("')");
                                            dbo.ExceSQL(sql.ToString());
                                        }
                                        if (station == 12001)
                                        {
                                            AheadDetect.Append(";D12001.0;D12001.1").Append(";I").Append(station.ToString()).Append(";N").Append(station.ToString());
                                        }
                                        else
                                        if (station == 12002)
                                        {
                                            AheadDetect.Append(";D12002.0;D12002.1;D12002.2").Append(";I").Append(station.ToString()).Append(";N").Append(station.ToString());
                                        }
                                        else
                                        {
                                            AheadDetect.Append(";D").Append(GetBindingDeviceIndex(station).ToString()).Append(";I").Append(station.ToString()).Append(";N").Append(station.ToString());
                                        }
                                        AheadDetect.Append(";L").Append(station.ToString());//20160421
                                        RunningLock.Append(station.ToString());
                                    }

                                    agvorder = 3;//AGV送货
                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], agvorder, 0) == false)
                                    {//20091107
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                        mindex = ccf.GetMonitorIndex(ManFID, Mankind);//20110104
                                        mindex = ccf.GetMonitorIndex(ManFID, Mankind);//20110104
                                        sql.Remove(0, sql.Length);
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,").Append(
                                            " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_RunningLock,F_TxtParam,F_UseAwayFork)").Append(
                                        "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                            ).Append(",").Append(NextKeyDevice[0]).Append(",").Append(agvorder).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(Coor[0]).Append(",").Append(Coor[1]
                                            ).Append(",").Append(Coor[2]).Append(",").Append(Coor[3]).Append(",").Append(Coor[4]).Append(",").Append(Coor[5]).Append(",'")
                                            .Append(AheadDetect.ToString()).Append("','").Append(RunningLock.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("','").Append(drv["FUseAwayFork"]).Append("')");
                                        dbo.ExceSQL(sql.ToString());

                                    }


                                }
                                #endregion
                                #endregion

                                #endregion
                                break;
                            case 7:
                                #region 条码扫描仪(托盘条码比对1)
                                if (PriorDevice.Count > 0)
                                {
                                    //如果PriorDevice是输送机则生成输送机命令【CurDevice-->PriorDevice目标设备,出库:2】,
                                    //收到输送机PriorDevice的PLC上报的运行状态则提前触发RGV运行命令(7),然后是取送货命令,获取堆垛机取坐标
                                    #region 前一设备是输送机
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    if (PriorDevice[1] == 2)
                                    {
                                        if ((uncontrol != "1") && (CurDevice[1]==2))
                                        {//20091107
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;
                                            }
                                            int arrowdev;
                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                                #region 运行提前检测光电组
                                                if (CurDevice[3] == 1)
                                                {
                                                    //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                    tw = GetBindingDeviceIndex(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect .Append( "D-" ).Append( tw.ToString() ).Append( ";" ).Append( GetBeDetectedDevices(CurDevice[0]));//20101118
                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }
                                                    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect .Append( ";D" + tw.ToString());//20101118
                                                    }

                                                }
                                                else if (CurDevice[3] == 2)
                                                {
                                                    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118
                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }
                                                    tw = GetBindingDeviceIndex(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D" + tw.ToString());//20101118
                                                    }
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                string[] cdi = ccf.GetOwnCorrel_DeviceInfo(CurDevice[0] );
                                                if (null != cdi)
                                                {//20100406检测被堆垛机送货的顶升机构在顶升高位,空闲
                                                    AheadDetect.Append(";D-").Append(cdi[1]).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118
                                                }
                                                //20101011
                                                else
                                                {
                                                    tw = GetSendOutDetect(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                                        
                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }

                                                    List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                    if (nextConveyor.Count > 0)//20110331
                                                    {
                                                        AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));
                                                        tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                        if ((tw != "")&&(nextConveyor[0]!=12304)&&(nextConveyor[0]!=12310))//20130510
                                                        {
                                                            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                        }
                                                        tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                        }
                                                        ////20090803检测设备是否空闲idle
                                                        //AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                    }

                                                }
                                                
                                                #endregion
                                            }
                                            AheadDetect.Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                            AheadTrigger.Remove(0, AheadTrigger.Length);
                                            AheadTrigger .Append( PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                

                                                #region 生成输送机命令
                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                sql.Remove(0, sql.Length);
                                                sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                                    " F_AheadDetect,F_TxtParam)" ).Append(
                                                "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                                    ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( CurDevice[3] ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( arrowdev
                                                    ).Append( ",'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                                dbo.ExceSQL(sql.ToString());
                                                #endregion


                                            }
                                        }
                                    }

                                    #endregion
                                    #region 运行提前检测光电组
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    tw = GetBindingDeviceIndexOut(PriorDevice[0]);
                                    if (tw != "")
                                    {
                                        AheadDetect.Append("D-").Append(tw.ToString());
                                    }
                                    else
                                    {
                                        AheadDetect.Remove(0, AheadDetect.Length);
                                    }
                                    #endregion
                                }
                                

                                #region 生成条码比对命令
                                if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], NextKeyDevice[3], 0) == false)
                                {
                                    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                    sql.Remove(0, sql.Length);
                                    sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                        " F_AheadDetect,F_TxtParam)" ).Append(
                                    "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                        ).Append( "," ).Append( NextKeyDevice[0] ).Append( "," ).Append( NextKeyDevice[3] ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( ",0,0,'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                    dbo.ExceSQL(sql.ToString());
                                }
                                #endregion


                                #endregion
                                break;
                            case 8://20130510
                            case 17://20161221 拆垛机器人
                                #region 拆垛机器人、称重设备(拆垛、称重命令1)
                                if (PriorDevice.Count > 0)
                                {
                                    //如果PriorDevice是输送机则生成输送机命令【CurDevice-->PriorDevice目标设备,出库:2】,
                                    //收到输送机PriorDevice的PLC上报的运行状态则提前触发RGV运行命令(7),然后是取送货命令,获取堆垛机取坐标
                                    #region 前一设备是输送机
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    if (PriorDevice[1] == 2)
                                    {
                                        if ((uncontrol != "1") && (CurDevice[1] == 2))
                                        {//20091107
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;
                                            }
                                            int arrowdev;
                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                                #region 运行提前检测光电组
                                                if (CurDevice[3] == 1)
                                                {
                                                    //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                    tw = GetBindingDeviceIndex(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118
                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }
                                                    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D" + tw.ToString());//20101118
                                                    }

                                                }
                                                else if (CurDevice[3] == 2)
                                                {
                                                    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append("D-").Append(tw.ToString()).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118
                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }
                                                    tw = GetBindingDeviceIndex(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D" + tw.ToString());//20101118
                                                    }
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                string[] cdi = ccf.GetOwnCorrel_DeviceInfo(CurDevice[0]);
                                                if (null != cdi)
                                                {//20100406检测被堆垛机送货的顶升机构在顶升高位,空闲
                                                    AheadDetect.Append(";D-").Append(cdi[1]).Append(";").Append(GetBeDetectedDevices(CurDevice[0]));//20101118
                                                }
                                                //20101011
                                                else
                                                {
                                                    tw = GetSendOutDetect(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append("D-").Append(tw.ToString());//20101118
                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }

                                                    List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                    if (nextConveyor.Count > 0)//20110331
                                                    {
                                                        AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));
                                                        tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                        }
                                                        tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                        }
                                                        ////20090803检测设备是否空闲idle
                                                        //AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                    }

                                                }

                                                #endregion
                                            }
                                            AheadDetect.Append(";").Append(GetBeDetectedDevices(CurDevice[0]));
                                            //AheadTrigger.Remove(0, AheadTrigger.Length);
                                            //AheadTrigger.Append(PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                #region 运行时锁定设备组
                                                //if ((CurDevice[0] == 32037) || (CurDevice[0] == 32050))
                                                //{
                                                //    RunningLock.Clear();
                                                //    RunningLock.Append(Convert.ToInt32(CurDevice[0] - 1));
                                                //}
                                                #endregion

                                                #region 生成输送机命令
                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                sql.Remove(0, sql.Length);
                                                sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                    " F_AheadDetect,F_TxtParam)").Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                    ).Append(",").Append(CurDevice[0]).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append(",").Append(arrowdev
                                                    ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                                dbo.ExceSQL(sql.ToString());
                                                #endregion


                                            }
                                        }
                                    }

                                    #endregion
                                    #region 运行提前检测光电组
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(PriorDevice[0]));

                                    tw = GetBindingDeviceIndexOut(PriorDevice[0]);
                                    if (tw != "")
                                    {
                                        AheadDetect.Append(";D-").Append(tw.ToString());//20101118
                                    }
                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(NextKeyDevice[0]));//20101124
                                    #endregion
                                    #region 注释
                                    //if (CurDevice[0] == 12304 || CurDevice[0] == 12310)
                                    //{//20130510  16006,16008
                                    //    int dev = GetBarcodeDeviceFromBindDevice(CurDevice[0]);
                                    //    #region 生成条码比对命令1
                                    //    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, dev, 1, 0) == false)
                                    //    {
                                    //        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                    //        sql.Remove(0, sql.Length);
                                    //        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                    //            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                    //            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                    //            " F_AheadDetect,F_TxtParam)").Append(
                                    //        "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                    //            ).Append(",").Append(dev).Append(",").Append(1).Append(",").Append(ccf.GetRouteIDsub(dev)).Append(",").Append(status).Append(",0,0,'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                    //        dbo.ExceSQL(sql.ToString());
                                    //    }
                                    //    #endregion
                                    //} 
                                    #endregion

                                }


                                #region 生成拆垛称重命令1
                                if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], NextKeyDevice[3], 0) == false)
                                {
                                    //内筒或箱体的类型写入F_NumParam1
                                    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                    sql.Remove(0, sql.Length);
                                    sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                        " F_AheadDetect,F_TxtParam)").Append(
                                    "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                        ).Append(",").Append(NextKeyDevice[0]).Append(",").Append(NextKeyDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",0,").Append(drv["GOODS_NUM"]).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                    dbo.ExceSQL(sql.ToString());
                                }
                                #endregion


                                #endregion
                                break;
                            case 12:
                                #region 虚拟关键设备(只作为路径分割点)
                                if (PriorDevice.Count > 0)
                                {
                                    //如果PriorDevice是输送机则生成输送机命令【CurDevice-->PriorDevice目标设备,出库:2】,
                                    //收到输送机PriorDevice的PLC上报的运行状态则提前触发RGV运行命令(7),然后是取送货命令,获取堆垛机取坐标
                                    #region 前一设备是输送机
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    if (PriorDevice[1] == 2)
                                    {
                                        if (uncontrol != "1")
                                        {//20091107
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;
                                            }
                                            int arrowdev;
                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                                #region 运行提前检测光电组
                                                //20101011
                                                string[] cdi = ccf.GetOwnCorrel_DeviceInfo(CurDevice[0]);
                                                if ((null!= cdi)&&(CurDevice[3] == 2))
                                                {//20100406检测被堆垛机送货的顶升机构在顶升高位,空闲
                                                    AheadDetect .Append( ";D-" ).Append( cdi[1] ).Append( ";" ).Append( GetBeDetectedDevices(CurDevice[0]));//20101118
                                                }
                                                //20101011
                                                else
                                                {
                                                    if (CurDevice[3] == 1)
                                                    {
                                                        //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                        tw = GetBindingDeviceIndex(CurDevice[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect .Append( "D-" ).Append( tw.ToString() ).Append( ";" ).Append( GetBeDetectedDevices(CurDevice[0]));//20101118
                                                        }
                                                        else
                                                        {
                                                            AheadDetect.Remove(0, AheadDetect.Length);
                                                        }
                                                        tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect .Append( ";D" ).Append( tw.ToString());//20101118
                                                        }
                                                    }
                                                    else if ((CurDevice[3] == 2) )//20110510反转
                                                    {
                                                        //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                        #region 注释 by lyj 20171113
                                                        //  tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                        #endregion
                                                        tw = GetSendOutDetect(CurDevice[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect .Append( "D-" ).Append( tw.ToString() ).Append( ";" ).Append( GetBeDetectedDevices(CurDevice[0]));//20101118
                                                        }
                                                        tw = GetBindingDeviceIndex(CurDevice[0]);
                                                        if (tw != "")
                                                        {
                                                            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                        }
                                                    }
                                                
                                                }
                                                //else if (CurDevice[3] == 2)
                                                //{
                                                //    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                //    if (tw != "")
                                                //    {
                                                //        AheadDetect = "D-" + tw.ToString() + ";" + GetBeDetectedDevices(CurDevice[0]);//20101118
                                                //    }
                                                //    else
                                                //    {
                                                //        AheadDetect.Remove(0, AheadDetect.Length);
                                                //    }
                                                //}
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                tw = GetBindingDeviceIndex(CurDevice[0]);
                                                if (tw != "")
                                                {//20091107
                                                    AheadDetect .Append( "D-" ).Append( tw.ToString() ).Append( ";" ).Append( GetBeDetectedDevices(CurDevice[0]));//20101118
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();


                                                //List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                //if (nextConveyor.Count > 0)//20110331
                                                //{
                                                //    AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));
                                                //    tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                //    if (tw != "")
                                                //    {
                                                //        AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                //    }
                                                //    tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                //    if (tw != "")
                                                //    {
                                                //        AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                //    }
                                                //    //20090803检测设备是否空闲idle
                                                //    AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                //}
                                                #endregion
                                            }

                                            //#region 20210326_上海晟碟,如果是出入库提升机还要判断下一个输送线是否有货

                                            //if (NextDevice[0] == 19003 || NextDevice[0] == 19005)
                                            //{
                                            //    if (SecondNextKeyDevice.Count > 0)
                                            //    {
                                            //        int iNextDevice = GetPriorDevice(routeIDSub, SecondNextKeyDevice[2])[0];
                                            //        tw = GetBindingDeviceIndex(iNextDevice);
                                            //        if (tw != "")
                                            //        {
                                            //            AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                            //        }
                                            //    }
                                                
                                            //}

                                            //#endregion


                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                #region 运行时锁定设备组

                                                #endregion

                                                #region 生成输送机命令
                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                sql.Remove(0, sql.Length);
                                                sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                                    " F_AheadDetect,F_TxtParam)" ).Append(
                                                "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                                    ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( CurDevice[3] ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( arrowdev
                                                    ).Append( ",'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                                dbo.ExceSQL(sql.ToString());
                                                #endregion


                                            }

                                        }
                                    }
                                    #endregion

                                }
                                #endregion
                                break;
                            case 30:
                                #region  20100617和管理交互虚拟设备,1:出库托盘确认
                                AheadDetect.Remove(0, AheadDetect.Length);
                                if (PriorDevice.Count > 0)
                                {
                                    //如果PriorDevice是输送机则生成输送机命令【CurDevice-->PriorDevice目标设备,出库:2】,
                                    //收到输送机PriorDevice的PLC上报的运行状态则提前触发RGV运行命令(7),然后是取送货命令,获取堆垛机取坐标
                                    #region 前一设备是输送机

                                    if (PriorDevice[1] == 2)
                                    {
                                        if (uncontrol != "1")
                                        {//20091107
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;
                                            }
                                            int arrowdev;
                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                                #region 运行提前检测光电组
                                                if ((CurDevice[3] == 1) || (CurDevice[3] == 2))
                                                {
                                                    //CurDevice的F_BindingDevice里存的光电开关的设备所引。CurDevice有探物
                                                    tw = GetBindingDeviceIndex(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect .Append( "D-" ).Append( tw.ToString() ).Append( ";" ).Append( GetBeDetectedDevices(CurDevice[0]));//20101118
                                                    }
                                                    else
                                                    {
                                                        AheadDetect.Remove(0, AheadDetect.Length);
                                                    }
                                                    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect .Append( ";D" ).Append( tw.ToString());//20101118
                                                    }

                                                }
                                                //else if (CurDevice[3] == 2)
                                                //{
                                                //    tw = GetBindingDeviceIndexOut(CurDevice[0]);
                                                //    if (tw != "")
                                                //    {
                                                //        AheadDetect = "D-" + tw.ToString() + ";" + GetBeDetectedDevices(CurDevice[0]);//20101118
                                                //    }
                                                //    else
                                                //    {
                                                //        AheadDetect.Remove(0, AheadDetect.Length);
                                                //    }
                                                //}
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();
                                                #endregion
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;
                                                #region 运行提前检测光电组
                                                //CurDevice的F_SendOutDetect里存的光电开关的设备所引。CurDevice有探物
                                                tw = GetSendOutDetect(CurDevice[0]);
                                                if (tw != "")
                                                {
                                                    AheadDetect .Append( "D-" ).Append( tw.ToString());//20101118
                                                }
                                                else
                                                {
                                                    AheadDetect.Remove(0, AheadDetect.Length);
                                                }
                                                ////20090803检测设备是否空闲idle
                                                //AheadDetect += ";" + "I" + CurDevice[0].ToString();

                                                List<int> nextConveyor = GetCurConveyorNextDevice(routeIDSub, CurDevice[2]);
                                                if (nextConveyor.Count > 0)//20110331
                                                {
                                                    AheadDetect.Append(";").Append(GetBeDetectedDevices(nextConveyor[0]));
                                                    tw = GetBindingDeviceIndex(nextConveyor[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                    }
                                                    tw = GetBindingDeviceIndexOut(nextConveyor[0]);
                                                    if (tw != "")
                                                    {
                                                        AheadDetect.Append(";D").Append(tw.ToString());//20101118
                                                    }

                                                    ////20090803检测设备是否空闲idle
                                                    //AheadDetect += ";" + "I" + nextConveyor[0].ToString();
                                                }
                                                #endregion
                                            }
                                            AheadTrigger.Remove(0, AheadTrigger.Length);
                                            AheadTrigger .Append( PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                #region 运行时锁定设备组

                                                #endregion

                                                #region 生成输送机命令
                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                sql.Remove(0, sql.Length);
                                                sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                                    " F_AheadDetect,F_TxtParam)" ).Append(
                                                "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                                    ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( CurDevice[3] ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( "," ).Append( CurDevice[0] ).Append( "," ).Append( arrowdev
                                                    ).Append( ",'" ).Append( AheadDetect.ToString() ).Append( "','" ).Append( drv["FPALLETBARCODE"] ).Append( "')");
                                                dbo.ExceSQL(sql.ToString());
                                                #endregion


                                            }
                                        }
                                    }

                                    #endregion

                                }
                                #region 运行提前检测光电组

                                //tw = GetBindingDeviceIndexOut(PriorDevice[0]);
                                //if (tw != "")
                                //{
                                //    AheadDetect = "D-" + tw.ToString();//20101118
                                //}
                                //else
                                //{
                                //    AheadDetect.Remove(0, AheadDetect.Length);
                                //}
                                #endregion

                                #region 生成和管理交互虚拟设备的【出库确认】命令
                                if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], NextKeyDevice[3], 0) == false)
                                {
                                    int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                    sql.Remove(0, sql.Length);
                                    sql .Append( "INSERT INTO T_Monitor_Task " ).Append(
                                        "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel," ).Append(
                                        " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4," ).Append(
                                        " F_AheadDetect,F_TxtParam)" ).Append(
                                    "VALUES (" ).Append( ManFID ).Append( "," ).Append( Mankind ).Append( "," ).Append( mindex ).Append( "," ).Append( drv["FTASKLEVEL"]
                                        ).Append( "," ).Append( NextKeyDevice[0] ).Append( "," ).Append( NextKeyDevice[3] ).Append( "," ).Append( routeIDSub ).Append( "," ).Append( status ).Append( ",0,0,'" ).Append( AheadDetect ).Append( "','" ).Append( drv["FPALLETBARCODE"] + "')");
                                    dbo.ExceSQL(sql.ToString());
                                }
                                #endregion


                                #endregion
                                break;
                            #region  add by lyj20180424双叉提升机类型
                            case 40:
                                #region 双叉提升机取货
                                //双叉提升机取货:
                                if (PriorDevice.Count > 0)
                                {
                                    //如果PriorDevice是输送机则生成输送机命令【CurDevice-->PriorDevice目标设备,出库:2】,
                                    //收到输送机PriorDevice的PLC上报的运行状态则提前触发RGV运行命令(7),然后是取送货命令,获取堆垛机取坐标
                                    #region 前一设备是输送机

                                    if (PriorDevice[1] == 2)
                                    {
                                        if ((uncontrol != "1") && (CurDevice[1] == 2))
                                        {//20091107
                                            List<int> newcur = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                            if (newcur != null)
                                            {
                                                CurDevice = newcur;
                                            }
                                            int arrowdev;

                                            if (CurDevice[0] == PriorDevice[0])
                                            {//输送机的起点设备和终点设备相同时,目标设备索引置为零
                                                arrowdev = 0;
                                              
                                            }
                                            else
                                            {
                                                arrowdev = PriorDevice[0];
                                                CurDevice[3] = 6;
                                               
                                            }
                                            sql.Remove(0, sql.Length);
                                            sql.Append("select * from T_Base_AheadDetect where F_DeviceIndex=").Append(CurDevice[0]).Append("and F_TaskType=").Append(tasktype).Append(" and F_CommondIndex=6").Append(" and F_GoalDeviceIndex=").Append(PriorDevice[0]);
                                            dvahead = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                                            if (dvahead.Count > 0)
                                            {
                                                AheadDetect.Remove(0, AheadDetect.Length);
                                                AheadDetect.Append(dvahead[0]["F_AheadDetect"].ToString());
                                            }


                                            AheadTrigger.Remove(0, AheadTrigger.Length);
                                            AheadTrigger.Append(PriorDevice[0].ToString()); //+ "-" + (被触发的调度任务号);//此设备的运行状态(货物当前位置=PriorDevice[0])触发堆垛机的将取
                                            if (DeviceAndOrderExitInMonitor(Mankind, ManFID, CurDevice[0], CurDevice[3], 0) == false)
                                            {
                                                #region 运行时锁定设备组

                                                #endregion

                                                #region 生成输送机命令
                                                int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                                TriggerTaskNo = mindex;
                                                sql.Remove(0, sql.Length);//20101124
                                                sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                                    "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                                    " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam4,").Append(
                                                    " F_AheadDetect,F_TxtParam)").Append(
                                                "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                                    ).Append(",").Append(CurDevice[0]).Append(",").Append(CurDevice[3]).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(CurDevice[0]).Append(",").Append(arrowdev
                                                    ).Append(",'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                                dbo.ExceSQL(sql.ToString());
                                                #endregion
                                            }

                                        }



                                    }
                                    else
                                    {//20111020
                                        List<int> newPrior = NewCurDevice(PriorDevice0, PriorKeyDevice0);
                                        if (newPrior != null)
                                        {
                                            PriorDevice = newPrior;
                                        }
                                    }
                                    #endregion

                                    #region 获得RGV运行的接货位的设备索引
                                    Coor[0] = PriorDevice[0];
                                    Coor[1] = 0;
                                    Coor[2] = 0;
                                    Coor[3] = 0;
                                    Coor[4] = 0;
                                    Coor[5] = 0;
                                    #endregion

                                    #region RGV的接货(被关联的任务)
                                    Rgvorder = 4; //GetRGVOrder(PriorDevice[0], true);

                                    #region 运行提前检测光电组
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    #region 提前检测从数据库里取 add by lyj20180424
                                    if (tasktype == 1)
                                    {

                                        sql.Remove(0, sql.Length);
                                        sql.Append("select * from T_Base_AheadDetect where F_DeviceIndex=").Append(NextKeyDevice[0]).Append("and F_TaskType=1 and F_CommondIndex=4").Append(" and F_GoalDeviceIndex=").Append(PriorDevice[0]);
                                        dvahead = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                                        if (dvahead.Count > 0)
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);
                                            AheadDetect.Append(dvahead[0]["F_AheadDetect"].ToString());
                                        }

                                    }
                                    else if (tasktype == 2)
                                    {
                                        int goaldevice=12037;
                                        sql.Remove(0, sql.Length);
                                        //sql.Append("select * from T_Base_AheadDetect where F_DeviceIndex=").Append(NextKeyDevice[0]).Append("and F_TaskType=2 and F_CommondIndex=4").Append(" and F_GoalDeviceIndex=").Append(NextDevice[0]); 

                                        sql.Append("select * from T_Base_AheadDetect where F_DeviceIndex=").Append(NextKeyDevice[0]).Append("and F_TaskType=2 and F_CommondIndex=4").Append(" and F_GoalDeviceIndex=").Append(PriorDevice[0]); 
                                        dvahead = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                                        if (dvahead.Count > 0)
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);
                                            AheadDetect.Append(dvahead[0]["F_AheadDetect"].ToString());
                                        }

                                    }
                                   
                                    #endregion
                                    #endregion

                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], Rgvorder, 0) == false)
                                    {
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                        sql.Remove(0, sql.Length);
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,").Append(
                                            " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_TxtParam)").Append(
                                        "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                            ).Append(",").Append(NextKeyDevice[0]).Append(",").Append(Rgvorder).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(PriorDevice[0]).Append(",0").Append(
                                            ",0,").Append(PriorDevice[0]).Append(",0,0,'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                        dbo.ExceSQL(sql.ToString());
                                        sql.Remove(0, sql.Length);
                                        sql.Append("update T_Monitor_Task set F_Associate=").Append(mindex).Append(" where F_MonitorIndex=").Append(mainTask);
                                        dbo.ExceSQL(sql.ToString());

                                    }
                                    //20101124
                                    #endregion

                                  
                                }

                                //获得RGV运行的送货位的设备索引:
                                if (NextDevice.Count > 0)
                                {
                                    Coor[0] = NextDevice[0];
                                    Coor[1] = 0;
                                    Coor[2] = 0;
                                    Coor[3] = 0;
                                    Coor[4] = 0;
                                    Coor[5] = 0;

                                    #region RGV的送货

                                    Rgvorder = 5;// GetRGVOrder(NextDevice[0], false);



                                    #region 运行提前检测光电组
                                    AheadDetect.Remove(0, AheadDetect.Length);
                                    #region 送货从数据库里取 add by lyj 20171026
                                    if (tasktype == 1)
                                    {

                                        sql.Remove(0, sql.Length);
                                        sql.Append("select * from T_Base_AheadDetect where F_DeviceIndex=").Append(NextKeyDevice[0]).Append("and F_TaskType=1 and F_CommondIndex=5").Append(" and F_GoalDeviceIndex=").Append(NextDevice[0]);
                                        dvahead = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                                        if (dvahead.Count > 0)
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);
                                            AheadDetect.Append(dvahead[0]["F_AheadDetect"].ToString());
                                        }

                                    }
                                    else if (tasktype == 2)
                                    {
                                      
                                        sql.Remove(0, sql.Length);
                                      
                                       // sql.Append("select * from T_Base_AheadDetect where F_DeviceIndex=").Append(NextKeyDevice[0]).Append("and F_TaskType=2 and F_CommondIndex=5").Append(" and F_GoalDeviceIndex=").Append(NextDevice[0]);
                                        sql.Append("select * from T_Base_AheadDetect where F_DeviceIndex=").Append(NextKeyDevice[0]).Append("and F_TaskType=2 and F_CommondIndex=5").Append(" and F_GoalDeviceIndex=").Append(NextDevice[0]); 
                                        dvahead = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                                        if (dvahead.Count > 0)
                                        {
                                            AheadDetect.Remove(0, AheadDetect.Length);
                                            AheadDetect.Append(dvahead[0]["F_AheadDetect"].ToString());
                                        }

                                    }
                                  
                                    #endregion
                                    #endregion

                                    if (DeviceAndOrderExitInMonitor(Mankind, ManFID, NextKeyDevice[0], Rgvorder, 0) == false)
                                    {
                                        int mindex = ccf.GetMonitorIndex(ManFID, Mankind);
                                        sql.Remove(0, sql.Length);
                                        sql.Append("INSERT INTO T_Monitor_Task ").Append(
                                            "(F_ManageTaskIndex, F_ManageTASKKINDINDEX, F_MonitorIndex,F_MonitorTaskLevel,").Append(
                                            " F_DeviceIndex, F_DeviceCommandIndex, F_RouteID, F_Status,F_NumParam1, F_NumParam2,").Append(
                                            " F_NumParam3, F_NumParam4,F_NumParam5, F_NumParam6,F_AheadDetect,F_TxtParam)").Append(
                                        "VALUES (").Append(ManFID).Append(",").Append(Mankind).Append(",").Append(mindex).Append(",").Append(drv["FTASKLEVEL"]
                                            ).Append(",").Append(NextKeyDevice[0]).Append(",").Append(Rgvorder).Append(",").Append(routeIDSub).Append(",").Append(status).Append(",").Append(NextDevice[0]).Append(",0").Append(
                                            ",0,").Append(NextDevice[0]).Append(",0,0,'").Append(AheadDetect.ToString()).Append("','").Append(drv["FPALLETBARCODE"]).Append("')");
                                        dbo.ExceSQL(sql.ToString());
                                        mainTask = mindex;
                                    }

                                    #endregion

                                }

                                break;



                                #endregion
                            #endregion
                            default:
                                break;
                        }
                    }
                    else
                    {

                    }
                    PriorDevice0 = PriorDevice;
                    PriorKeyDevice0 = NextKeyDevice;
                    //20091107
                    PriorDevice = null;
                    NextKeyDevice = null;
                    CurDevice = null;
                    NextKeyDevice = null;
                }
                //20120820
                string dtime = DateTime.Now.ToString("u");
                dtime = dtime.Substring(0, dtime.Length - 1);
                dbo.ExecuteSql(string.Format("update T_Monitor_Task set F_SplitTime= '{0}' where F_ManageTASKKINDINDEX={1} and F_ManageTASKINDEX={2}",dtime,Mankind,ManFID));//20120616
                dbo.TransCommit();
                //20091107
                PriorDevice0 = null;
                PriorKeyDevice0 = null;

                CDataChangeEventArgs cea = new CDataChangeEventArgs(null, null);
                OnDataChange("CreateMonitor",cea);
                return 1;
            }
            catch (Exception ex)
            {
                
                DisassembleTaskError = "ControlSystem.CDisassembleTask.CreateMonitor时发生错误:" + ex.StackTrace+ex.Message ;
                RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", DisassembleTaskError);
                OnRefreshMonitor(rme);
                dbo.TransRollback();

                return 0;
            }
            finally
            {
                //dvRoute.Dispose();
                //dvs.Dispose();

            }
        }
        /// <summary>
        /// 特殊任务需要控制任务数量,不进行拆分,返回值true就不拆分
        /// </summary>
        /// <param name="drv"></param>
        /// <returns></returns>
        public static bool ControlTaskAmountUnDisassemble(DataRowView drv)
        {//20121203
            DataView dv = new DataView();
            try
            {
                if (Convert.ToInt32(drv["FSTARTDEVICE"]) == 19001 && Convert.ToInt32(drv["FENDDEVICE"]) != 19001)
                {//化成A回库和去倒箔间的包含12068的任务分别不能超过六个(12076,12083,18001,18002...18005)
                    sql.Clear();
                    sql.Append("SELECT count(distinct FID) as taskamount FROM T_Monitor_Task,T_Manage_Task WHERE  T_Monitor_Task.F_ManageTASKKINDINDEX = T_Manage_Task.F_ManageTaskKindIndex")
                        .Append("  AND T_Monitor_Task.F_ManageTaskIndex = T_Manage_Task.FID and T_Monitor_Task.F_DeviceIndex = 12068 and FSTARTDEVICE=19001 ");
                    if (Convert.ToInt32(drv["FENDDEVICE"]) == 12076 || Convert.ToInt32(drv["FENDDEVICE"]) == 12083)
                    {
                        sql.Append(" and (FENDDEVICE=12076 or FENDDEVICE=12083) ");
                    }
                    else
                    {
                        sql.Append(" and (FENDDEVICE<>12076 and FENDDEVICE<>12083 and FENDDEVICE<>19001) ");
                    }
                    object ob = dbo.GetSingle(sql.ToString());
                    if (ob != null)
                    {
                        if (Convert.ToInt32(ob) > 6)
                        {
                            return true;
                        }
                    }
                }
                else if (Convert.ToInt32(drv["FSTARTDEVICE"]) != 19002 &&  Convert.ToInt32(drv["FENDDEVICE"]) == 19002  )
                {//化成B出库包含12019的不能超过4个;
                    sql.Clear();
                    sql.Append("SELECT count(distinct FID) as taskamount FROM T_Monitor_Task,T_Manage_Task WHERE  T_Monitor_Task.F_ManageTASKKINDINDEX = T_Manage_Task.F_ManageTaskKindIndex")
                    .Append("  AND T_Monitor_Task.F_ManageTaskIndex = T_Manage_Task.FID and T_Monitor_Task.F_DeviceIndex = 12019 ");
                    sql.Append(" and (FENDDEVICE=19002 and FSTARTDEVICE<>19002) ");
                    object ob = dbo.GetSingle(sql.ToString());
                    if (ob != null)
                    {
                        if (Convert.ToInt32(ob) > 4)
                        {
                            return true;
                        }
                    }
                }
                else if (Convert.ToInt32(drv["FSTARTDEVICE"]) == 19002 &&  Convert.ToInt32(drv["FENDDEVICE"]) != 19002  )
                {//化成B去倒箔间和回库的任务包含12034等待执行的不能超过3个
                    sql.Clear();
                    sql.Append("SELECT count(distinct FID) as taskamount FROM T_Monitor_Task,T_Manage_Task WHERE  T_Monitor_Task.F_ManageTASKKINDINDEX = T_Manage_Task.F_ManageTaskKindIndex")
                        .Append("  AND T_Monitor_Task.F_ManageTaskIndex = T_Manage_Task.FID and T_Monitor_Task.F_DeviceIndex = 12034 and f_status=0 and FSTARTDEVICE=19002 ");
                    if (Convert.ToInt32(drv["FENDDEVICE"]) == 12042 || Convert.ToInt32(drv["FENDDEVICE"]) == 12051)
                    {
                        sql.Append(" and (FENDDEVICE=12042 or FENDDEVICE=12051) ");
                    }
                    else
                    {
                        sql.Append(" and (FENDDEVICE<>12042 and FENDDEVICE<>12051 and FENDDEVICE<>19002) ");
                    }
                    object ob = dbo.GetSingle(sql.ToString());
                    if (ob != null)
                    {
                        if (Convert.ToInt32(ob) > 3)
                        {
                            return true;
                        }
                    }
                }
                else if (Convert.ToInt32(drv["FSTARTDEVICE"]) >= 18001 && Convert.ToInt32(drv["FSTARTDEVICE"]) <= 18005 && (Convert.ToInt32(drv["FENDDEVICE"]) == 12042 || Convert.ToInt32(drv["FENDDEVICE"]) == 12051))
                {//立库到化成B倒箔间任务每个巷道最多两个
                    sql.Clear();
                    sql.Append("SELECT count(distinct FID) as taskamount FROM T_Monitor_Task,T_Manage_Task WHERE  T_Monitor_Task.F_ManageTASKKINDINDEX = T_Manage_Task.F_ManageTaskKindIndex")
                        .Append("  AND T_Monitor_Task.F_ManageTaskIndex = T_Manage_Task.FID  and (FENDDEVICE=12042 or FENDDEVICE=12051) and FSTARTDEVICE=").Append(Convert.ToInt32(drv["FSTARTDEVICE"]));
                   
                    object ob = dbo.GetSingle(sql.ToString());
                    if (ob != null)
                    {
                        if (Convert.ToInt32(ob) >=2)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "拆分任务时,判断是否二楼检定任务是否超过最大缓存8垛:" + ex.Message;
                return false;
            }
        }
        /// <summary>
        /// 查找路径运行方向的反方向的最近的设备,返回值:索引0:设备所引;索引1:设备类型;2:路径序号;3:设备命令
        /// </summary>
        /// <param name="CurRouteID">路径ID</param>
        /// <param name="CurSerialNumber">路径上的序号</param>
        /// <returns></returns>
        public static List<int> GetPriorDevice(int CurRouteIDSub, int CurSerialNumber)
        {
            
            List<int> keyDevice = new List<int>();
            DataView dvRoute = new DataView();
            try
            {
                //后续设备最近的设备
                sql.Remove(0, sql.Length);
                sql.Append( "SELECT T_Base_Route_Device.F_DeviceIndex,F_DeviceOrder, T_Base_Route_Device.F_SerialNumber, " ).Append(
                        "T_Base_Device_Command.F_DeviceCommandIndex,T_Base_Device.F_DeviceKindIndex FROM T_Base_Device_Command " ).Append(
                        ",T_Base_Device,T_Base_Route_Device where T_Base_Device_Command.F_DeviceKindIndex = T_Base_Device.F_DeviceKindIndex" ).Append(
                        " and T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex and F_RouteIDSub=" ).Append(
                        CurRouteIDSub ).Append( " and F_SerialNumber < " ).Append( CurSerialNumber ).Append( "  order by F_SerialNumber desc ");
                dvRoute = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvRoute.Count > 0)
                {
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceIndex"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceKindIndex"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_SerialNumber"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceOrder"]));
                }
                return keyDevice;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format("ControlSystem.CDisassembleTask.GetPriorDevice:{0}" , ex.StackTrace+ex.Message );
                return null;
            }
            finally
            {
                keyDevice=null;
                dvRoute.Dispose() ;
                
            }
        }
        /// <summary>
        /// 对于长串输送机组,需要判断当前设备的下一个设备是否具备发送状态
        /// </summary>
        /// <param name="CurRouteIDSub">当前调度的子路径</param>
        /// <param name="CurSerialNumber">输送机组的当前输送机对应的路径序号</param>
        /// <returns></returns>
        static List<int> GetCurConveyorNextDevice(int CurRouteIDSub, int CurSerialNumber)
        {
            List<int> CurDeviceNextDev = new List<int>();//20130510 return CurDeviceNextDev;//20120906嘉兴特殊
            DataView dvRoute = new DataView(); 
            try
            {
                //找到前一个设备CurDevice的下一个设备
                sql.Remove(0, sql.Length );
                sql.Append("SELECT T_Base_Route_Device.F_DeviceIndex,F_DeviceOrder, T_Base_Route_Device.F_SerialNumber, " ).Append(
                        "T_Base_Device_Command.F_DeviceCommandIndex,T_Base_Device.F_DeviceKindIndex FROM T_Base_Device_Command " ).Append(
                        ",T_Base_Device,T_Base_Route_Device where T_Base_Device_Command.F_DeviceKindIndex = T_Base_Device.F_DeviceKindIndex" ).Append(
                        " and T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex and F_RouteIDSub=" ).Append(
                        CurRouteIDSub ).Append( " and F_SerialNumber > " ).Append( CurSerialNumber ).Append( " and T_Base_Device.F_DeviceKindIndex=2 order by F_SerialNumber asc ");
                dvRoute = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvRoute.Count > 0)
                {
                    CurDeviceNextDev.Add(Convert.ToInt32(dvRoute[0]["F_DeviceIndex"]));
                    CurDeviceNextDev.Add(Convert.ToInt32(dvRoute[0]["F_DeviceKindIndex"]));
                    CurDeviceNextDev.Add(Convert.ToInt32(dvRoute[0]["F_SerialNumber"]));
                    CurDeviceNextDev.Add(Convert.ToInt32(dvRoute[0]["F_DeviceOrder"]));
                }
                return CurDeviceNextDev;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format("ControlSystem.CDisassembleTask.GetCurConveyorNextDevice:{0}" ,ex.StackTrace+ex.Message );
                return null;
            }
            finally
            {
                CurDeviceNextDev=null;
                dvRoute.Dispose();
                
            }
        }
        /// <summary>
        /// 查找路径方向上最近的关键设备,返回值:索引0:设备所引;索引1:设备类型;2:路径序号;3:设备命令
        /// </summary>
        /// <param name="CurRouteID">路径ID</param>
        /// <param name="CurSerialNumber">路径上的序号</param>
        /// <returns></returns>
        static List<int> GetNextKeyDevice(int CurRouteIDSub, int CurSerialNumber)
        {
            List<int> keyDevice = new List<int>(); DataView dvRoute = new DataView(); DataView dv = new DataView();
            try
            {
                //首先判断本设备是否为关键设备
                sql.Remove(0, sql.Length);
                sql.Append("SELECT F_SerialNumber, T_Base_Device.F_DeviceIndex,F_DeviceOrder,F_DeviceKindIndex FROM T_Base_Device," ).Append(
                    "T_Base_Route_Device  where T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex " ).Append(
                    " and F_RouteIDSub=" ).Append( CurRouteIDSub ).Append( " and F_SerialNumber=" ).Append( CurSerialNumber
                    ).Append( " and F_KeyDevice = '1'");
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    keyDevice.Add(Convert.ToInt32(dv[0]["F_DeviceIndex"]));
                    keyDevice.Add(Convert.ToInt32(dv[0]["F_DeviceKindIndex"]));
                    keyDevice.Add(Convert.ToInt32(dv[0]["F_SerialNumber"]));
                    keyDevice.Add(Convert.ToInt32(dv[0]["F_DeviceOrder"]));
                    return keyDevice;
                }
                //然后依次判断后续设备最近的关键设备
                sql.Remove(0, sql.Length);
                sql .Append( "SELECT T_Base_Route_Device.F_DeviceIndex,F_DeviceOrder, T_Base_Route_Device.F_SerialNumber, " ).Append(
                        "T_Base_Device_Command.F_DeviceCommandIndex,T_Base_Device.F_DeviceKindIndex FROM T_Base_Device_Command " ).Append(
                        ",T_Base_Device,T_Base_Route_Device where T_Base_Device_Command.F_DeviceKindIndex = T_Base_Device.F_DeviceKindIndex" ).Append(
                        " and T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex and F_RouteIDSub=" ).Append(
                        CurRouteIDSub ).Append( " and F_SerialNumber > " ).Append( CurSerialNumber ).Append( " and T_Base_Device.F_KeyDevice = '1' order by F_SerialNumber asc ");
                dvRoute = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvRoute.Count > 0)
                {
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceIndex"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceKindIndex"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_SerialNumber"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceOrder"]));

                }
                return keyDevice;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format("ControlSystem.CDisassembleTask.GetNextKeyDevice:{0}" ,ex.StackTrace+ex.Message );
                return null;
            }
            finally
            {
                keyDevice=null;
                dvRoute.Dispose();
                dv.Dispose();
                
            }
        }
        /// <summary>
        /// 查找路径方向上下一个设备,返回值:索引0:设备所引;索引1:设备类型;2:路径序号;3:设备命令
        /// </summary>
        /// <param name="CurRouteID">路径ID</param>
        /// <param name="CurSerialNumber">路径上的序号</param>
        /// <returns></returns>
        public static List<int> GetNextDevice(int CurRouteIDSub, int CurSerialNumber)
        {
            List<int> keyDevice = new List<int>(); DataView dvRoute = new DataView();
            try
            {
                //然后依次判断后续设备最近的设备
                sql.Remove(0, sql.Length);
                sql .Append( "SELECT T_Base_Route_Device.F_DeviceIndex,F_DeviceOrder, T_Base_Route_Device.F_SerialNumber, " ).Append(
                        "T_Base_Device_Command.F_DeviceCommandIndex,T_Base_Device.F_DeviceKindIndex FROM T_Base_Device_Command " ).Append(
                        ",T_Base_Device,T_Base_Route_Device where T_Base_Device_Command.F_DeviceKindIndex = T_Base_Device.F_DeviceKindIndex" ).Append(
                        " and T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex and F_RouteIDSub=" ).Append(
                        CurRouteIDSub ).Append( " and F_SerialNumber > " ).Append( CurSerialNumber ).Append( "  order by F_SerialNumber asc ");
                dvRoute = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvRoute.Count > 0)
                {
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceIndex"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceKindIndex"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_SerialNumber"]));
                    keyDevice.Add(Convert.ToInt32(dvRoute[0]["F_DeviceOrder"]));

                }
                return keyDevice;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format( "ControlSystem.CDisassembleTask.GetNextDevice:{0}" ,ex.StackTrace+ex.Message );
                return null;
            }
            finally
            {
                keyDevice=null;
                dvRoute.Dispose();
                
            }
        }
        /// <summary>
        ///  获得AGV坐标
        /// </summary>
        /// <param name="Mankind">调度任务类型</param>
        /// <param name="ManFID">调度任务索引</param>
        /// <param name="IOType">搬运任务类型:1,入库(送坐标);2:出库(取坐标);3:移库(取和送坐标)</param>
        /// <returns></returns>
        static int[] GetAGVAddressFromManage(int Mankind, long ManFID, DataRowView drv)
        {
            int[] stackCoo = new int[6] { 0, 0, 0, 0, 0, 0 };//取排-列-层(Z-X-Y);送排-列-层(Z-X-Y)
            
            char[] cc = new char[1] { '-' };
            string[] split = new string[3];
            DataView dv = new DataView();
            try
            {
                switch (Convert.ToInt32(drv["FCONTROLTASKTYPE"]))
                {
                    case 1://入库,取坐标:机台站台;送坐标:路径下一个设备
                        sql.Remove(0, sql.Length);
                        sql .Append( "SELECT FSTARTCELL,FSTARTDEVICE,FENDDEVICE  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            stackCoo[0] = 0;
                            stackCoo[1] =Convert.ToInt32(dv[0]["FSTARTCELL"]);
                            stackCoo[2] = 0;

                            stackCoo[3] = 0;
                            stackCoo[4] = 0;
                            stackCoo[5] = 0;
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得AGV坐标!";
                            return null;
                        }
                        break;

                    case 2://出库:取坐标在路径上一个设备取;送坐标:机台站台
                        sql.Remove(0, sql.Length);
                        sql .Append( "SELECT FENDCELL,FENDDEVICE  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            stackCoo[0] = 0;
                            stackCoo[1] = 0;
                            stackCoo[2] = 0;

                            stackCoo[3] = 0;
                            stackCoo[4] = Convert.ToInt32(dv[0]["FENDCELL"]);
                            stackCoo[5] = 0;
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得AGV坐标!";
                            return null;
                        }
                        break;
                    
                    default:
                        break;
                    case 3://移库,取坐标:机台站台;送坐标:路径下一个设备
                        sql.Remove(0, sql.Length);
                        sql .Append( "SELECT FSTARTCELL,FSTARTDEVICE,FENDCELL,FENDDEVICE  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            stackCoo[0] = 0; int a = 0;
                            if (int.TryParse(dv[0]["FSTARTCELL"].ToString(),out a)==true)
                            {
                                stackCoo[1] = Convert.ToInt32(dv[0]["FSTARTCELL"]);
                            }
                            else
                            {
                                stackCoo[1] = 0;
                            }
                            stackCoo[2] = 0;

                            stackCoo[3] = 0; 
                            if(int.TryParse (dv[0]["FENDCELL"].ToString(),out a)==true)
                            {
                            stackCoo[4] = Convert.ToInt32(dv[0]["FENDCELL"]);
                            }
                            else
                            {
                                stackCoo[4] = 0;
                            }
                            stackCoo[5] = 0;
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得AGV坐标!";
                            return null;
                        }
                        break;
                }


                return stackCoo;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format( "ControlSystem.CDisassembleTask.GetAGVAddressFromManage:{0}" , ex.StackTrace+ex.Message );
                return null;
            }
            finally
            {
                stackCoo = null;
                
               cc = null ;
               split =null ;
               dv.Dispose() ;
            }
            
        }
        /// <summary>
        /// 20101219根据是否是在AGV通道取货,判断坐标
        /// </summary>
        /// <param name="Mankind"></param>
        /// <param name="ManFID"></param>
        /// <param name="IfGetGoods"></param>
        /// <returns></returns>
        static int[] GetAGVAddressFromManage(int Mankind, long ManFID,bool IfGetGoods)
        {
            int[] stackCoo = new int[6] { 0, 0, 0, 0, 0, 0 };//取排-列-层(Z-X-Y);送排-列-层(Z-X-Y)
            
            char[] cc = new char[1] { '-' };
            string[] split = new string[3];
            DataView dv = new DataView();
            try
            {
                switch (IfGetGoods)
                {
                    case true://在AGV通道取货,取坐标:机台站台;送坐标:路径下一个设备
                        sql.Remove(0, sql.Length);
                        sql .Append( "SELECT FSTARTCELL,FSTARTDEVICE,FENDDEVICE  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            stackCoo[0] = 0;
                            stackCoo[1] =Convert.ToInt32(dv[0]["FSTARTCELL"]);
                            stackCoo[2] = 0;

                            stackCoo[3] = 0;
                            stackCoo[4] = 0;
                            stackCoo[5] = 0;
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得AGV坐标!";
                            return null;
                        }
                        break;

                    case false://向AGV通道送货:取坐标在路径上一个设备取;送坐标:机台站台
                        sql.Remove(0, sql.Length);
                        sql .Append( "SELECT FENDCELL,FENDDEVICE  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            stackCoo[0] = 0;
                            stackCoo[1] = 0;
                            stackCoo[2] = 0;

                            stackCoo[3] = 0;
                            stackCoo[4] = Convert.ToInt32(dv[0]["FENDCELL"]);
                            stackCoo[5] = 0;
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得AGV坐标!";
                            return null;
                        }
                        break;
                    
                    default:
                        break;
                   
                }


                return stackCoo;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format( "ControlSystem.CDisassembleTask.GetAGVAddressFromManage:{0}" ,ex.StackTrace+ex.Message );
                return null;
            }
            finally
            {
                stackCoo = null;
                
               cc = null ;
               split =null ;
               dv.Dispose();
            }
            
        }
        /// <summary>
        /// 在表T_Base_AGV_Gate获得AGV地址
        /// </summary>
        /// <param name="devindex">AGV地址对应的设备索引</param>
        /// <returns></returns>
        static int GetAGVAddressFromAgvGate(int devindex)
        {
            DataView dv = new DataView();
            try
            {
                sql.Remove(0, sql.Length);
                sql.Append("select F_Address from T_Base_AGV_Gate where F_AGVGateDeviceIndex=" ).Append( devindex);
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["F_Address"]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format( "ControlSystem.CDisassembleTask.GetAGVAddressFromAgvGate:{0}" ,ex.StackTrace+ex.Message );
                return -1;
            }
            finally
            {
                dv.Dispose();
            }
        }
        /// <summary>
        ///  获得堆垛机坐标取排-列-层(Z-X-Y);送排-列-层(Z-X-Y)
        /// </summary>
        /// <param name="Mankind">调度任务类型</param>
        /// <param name="ManFID">调度任务索引</param>
        /// <param name="IOType">搬运任务类型:1,入库(送坐标);2:出库(取坐标);3:移库(取和送坐标)</param>
        /// <returns></returns>
        static int[] GetStackCoordinateFromManage(int Mankind, long ManFID,DataRowView drv)
        {
            int[] stackCoo = new int[6] { 0, 0, 0, 0, 0, 0 };//取排-列-层(Z-X-Y);送排-列-层(Z-X-Y)
            char[] cc = new char[1] {'-' };
            string[] split = new string[3];
            DataView dv = new DataView();
            try
            {
                sql.Remove(0, sql.Length);
                switch (Convert.ToInt32(drv["FCONTROLTASKTYPE"]))
                {
                    case 1://入库,送坐标
                        sql .Append( "SELECT FENDCELL  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            split = dv[0]["FENDCELL"].ToString().Split(cc);
                            stackCoo[3] = Convert.ToInt32(split[0]);
                            stackCoo[4] = Convert.ToInt32(split[1]);
                            stackCoo[5] = Convert.ToInt32(split[2]);
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得堆垛机坐标!";
                            return null;
                        }
                        break;

                    case 2://出库,取坐标
                        sql.Append( "SELECT FSTARTCELL  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            split = dv[0]["FSTARTCELL"].ToString().Split(cc);
                            stackCoo[0] = Convert.ToInt32(split[0]);
                            stackCoo[1] = Convert.ToInt32(split[1]);
                            stackCoo[2] = Convert.ToInt32(split[2]);
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得堆垛机坐标!";
                            return null;
                        }
                        break;
                    case 3:
                        sql .Append( "SELECT FSTARTCELL,FENDCELL  FROM T_Manage_Task WHERE (FID = " ).Append( ManFID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( Mankind ).Append( ")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            if (dv[0]["FSTARTCELL"].ToString() != "-")
                            {
                                split = dv[0]["FSTARTCELL"].ToString().Split(cc);
                                stackCoo[0] = Convert.ToInt32(split[0]);
                                stackCoo[1] = Convert.ToInt32(split[1]);
                                stackCoo[2] = Convert.ToInt32(split[2]);
                            }
                            if (dv[0]["FENDCELL"].ToString() != "-")
                            {
                                split = dv[0]["FENDCELL"].ToString().Split(cc);
                                stackCoo[3] = Convert.ToInt32(split[0]);
                                stackCoo[4] = Convert.ToInt32(split[1]);
                                stackCoo[5] = Convert.ToInt32(split[2]);
                            }
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得堆垛机坐标!";
                            return null;
                        }
                        break;
                    default:
                        break;
                }
                return stackCoo;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage调度任务的坐标有误!"+ex.StackTrace+ex.Message ;
                return null;
            }
            
            finally{
                stackCoo = null;
                
                cc = null ;
                split=null ;
                dv.Dispose() ;

            }
        }
        /// <summary>
        /// 20101219根据是否在巷道取货,判断坐标
        /// </summary>
        /// <param name="Mankind"></param>
        /// <param name="ManFID"></param>
        /// <param name="IfGetGoods"></param>
        /// <returns></returns>
        static int[] GetStackCoordinateFromManage(int Mankind, long ManFID,bool IfGetGoods)
        {
            int[] stackCoo = new int[6] { 0, 0, 0, 0, 0, 0 };//取排-列-层(Z-X-Y);送排-列-层(Z-X-Y)
            char[] cc = new char[1] { '-' };
            string[] split = new string[3];
            DataView dv = new DataView();
            try
            {
                sql.Remove(0, sql.Length);
                switch (IfGetGoods)
                {
                    case false://向巷道送货,送坐标
                        sql.Append("SELECT FENDCELL  FROM T_Manage_Task WHERE (FID = ").Append(ManFID).Append(") AND (F_ManageTaskKindIndex = ").Append(Mankind).Append(")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            split = dv[0]["FENDCELL"].ToString().Split(cc);
                            stackCoo[3] = Convert.ToInt32(split[0]);
                            stackCoo[4] = Convert.ToInt32(split[1]);
                            stackCoo[5] = Convert.ToInt32(split[2]);
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得堆垛机坐标!";
                            return null;
                        }
                        break;

                    case true://在巷道取货,取坐标
                        sql.Append("SELECT FSTARTCELL  FROM T_Manage_Task WHERE (FID = ").Append(ManFID).Append(") AND (F_ManageTaskKindIndex = ").Append(Mankind).Append(")");
                        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            split = dv[0]["FSTARTCELL"].ToString().Split(cc);
                            stackCoo[0] = Convert.ToInt32(split[0]);
                            stackCoo[1] = Convert.ToInt32(split[1]);
                            stackCoo[2] = Convert.ToInt32(split[2]);
                        }
                        else
                        {
                            DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage时没能从调度任务获得堆垛机坐标!";
                            return null;
                        }
                        break;
                    
                    default:
                        break;
                }
                return stackCoo;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromManage调度任务的坐标有误!" +ex.StackTrace+ ex.Message;
                return null;
            }

            finally
            {
                stackCoo = null;

                cc = null;
                split = null;
                dv.Dispose();

            }
        }
       /// <summary>
        /// 获得巷道的出入口处设备对应的堆垛机坐标取排-列-层(Z-X-Y);送排-列-层(Z-X-Y)
       /// </summary>
       /// <param name="devIndex">设备索引</param>
       /// <param name="ifGet">是否取货</param>
       /// <returns></returns>
        static int[] GetStackCoordinateFromLaneGate(int stackIndex,int devIndex,bool ifGet)
        {
            char[] cc = new char[1] { '-' };//20100305
            string[] split;
            int[] rt = new int[6] { 0,0,0,0,0,0};
            DataView dv = new DataView();
            int LaneWay = ccf.GetLaneWayFromLaneInfo(stackIndex);

            try
            {
                sql.Remove(0, sql.Length);
                #region  注释 by lyj 20171117
                //   sql.Append("select * from T_Base_Lane_Gate where F_LaneGateDeviceIndex=" ).Append( devIndex ).Append( " and F_LaneIndex=").Append(LaneWay);
                #endregion
                sql.Append("select * from T_Base_Lane_Gate where F_LaneGateDeviceIndex=").Append(devIndex);
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    if (ifGet == true)//取坐标
                    {
                        //20100305
                        split = dv[0]["F_ZXY"].ToString().Split(cc);

                        rt[0] = Convert.ToInt32(split[0]);//排Z
                        rt[1] = Convert.ToInt32(split[1]);//列X
                        rt[2] = Convert.ToInt32(split[2]);//层Y
                        //20100305
                    }
                    else//送坐标
                    {
                        //20100305
                        split = dv[0]["F_ZXY"].ToString().Split(cc);

                        rt[3] = Convert.ToInt32(split[0]);//排Z
                        rt[4] = Convert.ToInt32(split[1]);//列X
                        rt[5] = Convert.ToInt32(split[2]);//层Y
                        //20100305
                    }

                }
                return rt;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetStackCoordinateFromLaneGate:" +ex.StackTrace+ ex.Message;
                return null;
            }
            finally
            {
                cc =null ;
                split=null ;
                rt =null ;
                dv.Dispose() ;
            }
        }
         /// <summary>
        /// 获得设备的入库口探物光电的设备索引值
        /// </summary>
        /// <param name="devinx">设备索引</param>
        /// <returns></returns>
        /// 20101118
        static string GetBindingDeviceIndex(int devinx)
        {
            
            try
            {
               devinfo = Model.CGetInfo.GetDeviceInfo(devinx);
               
               if (devinfo.BindingDevice == null) return "";
               return devinfo.BindingDevice;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetBindingDeviceIndex:" +ex.StackTrace+ ex.Message;
                return "";
            }
            finally
            {
                devinfo = null;
            }
            
        }
        /// <summary>
        /// 获得输送机设备的出库口探物光电的设备索引值
        /// </summary>
        /// <param name="devinx">设备索引</param>
        /// <returns></returns>
        /// 20101118
        static string GetBindingDeviceIndexOut(int devinx)
        {
            
            try
            {
                devinfo = Model.CGetInfo.GetDeviceInfo(devinx);
                if (devinfo.BindingDeviceOut == null)return  "";
                return devinfo.BindingDeviceOut;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetBindingDeviceIndexOut:" +ex.StackTrace+ ex.Message;
                return "";
            }
            finally
            {
                devinfo = null;
            }
            
        }
        /// <summary>
        ///被临近设备运行前检测的光电信号对应的设备索引组(故障,正转,反转,上升,下降等),
        /// 索引之间使用分号“;”隔开,索引前面加负号“-”表示需要检测值为“1”的信号
        /// </summary>
        /// <param name="devinx">设备索引</param>
        /// <returns></returns>
        static string GetBeDetectedDevices(int devinx)
        {
            
            try
            {
                devinfo = Model.CGetInfo.GetDeviceInfo(devinx);
                return devinfo.BeDetected;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetBeDetectedDevices:" +ex.StackTrace+ ex.Message;
                return null ;
            }
            finally
            {
                devinfo = null;
            }
            
        }
        /// <summary>
        /// 获取输送机执行送出命令时,需要提前检测的光电开关设备索引
        /// </summary>
        /// <param name="devinx">设备索引</param>
        /// <returns></returns>
        /// 20101118
        static string  GetSendOutDetect(int devinx)
        {
            
            try
            {
                devinfo = Model.CGetInfo.GetDeviceInfo(devinx);
                if (devinfo.SendOutDetect == null)return  "";
                return devinfo.SendOutDetect;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetSendOutDetect:" +ex.StackTrace+ ex.Message;
                return "";
            }
            finally
            {
                devinfo = null;
            }
            
        }
       

        /// <summary>
        /// 获取设备是否逻辑有货
        /// </summary>
        /// <param name="devinx">设备索引</param>
        /// <returns></returns>
        static string GetIfHaveGoods(int devinx)
        {


            DataView db = new DataView();
            try
            {
                sql.Remove(0, sql.Length);
                sql .Append( "SELECT F_HaveGoods FROM T_Base_Device WHERE (F_DeviceIndex = " ).Append( devinx ).Append( ") and F_HaveGoods is not null");
               db = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (db.Count > 0)
                {
                    return db[0]["F_HaveGoods"].ToString();
                }
                else
                    return "";
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetIfHaveGoods:" +ex.StackTrace+ ex.Message;
                return "";
            }
                
            finally
            {
                db.Dispose();
                
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="devidx">RGV地址的设备索引号</param>
        /// <param name="IfGet">是否是接货</param>
        /// <returns></returns>
        static int GetRGVOrder(int devidx,bool IfGet)
        {//(2-左接货3-左送货)或者(4-右接货5-右送货)
            int order = 0;
            DataView dv = new DataView();
            try
            {
                sql.Remove(0, sql.Length);
                sql.Append("SELECT F_IFChannelLeft, F_RGVGateDeviceIndex" ).Append(
                " FROM T_Base_RGV_Gate WHERE (F_RGVGateDeviceIndex = " ).Append( devidx ).Append( ")");
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    if (dv[0]["F_IFChannelLeft"].ToString() == "1")
                    {
                        if (IfGet == true)
                        {
                            order = 2;
                        }
                        else
                        {
                            order = 3;
                        }
                    }
                    else
                    {
                        if (IfGet == true)
                        {
                            order = 4;
                        }
                        else
                        {
                            order = 5;
                        }
                    }
                }
                return order;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetRGVOrder:" +ex.StackTrace + ex.Message;
                return 0;
            }
            finally
            {
                dv.Dispose();
            }
        }
        /// <summary>
        /// 获得AGV的前一个设备信息
        /// 20090625
        /// </summary>
        /// <param name="CurRouteIDSub">路径子编号</param>
        /// <param name="CurSerialNumber">AGV设备的路径序号</param>
        /// <returns></returns>
        static List<int> GetAGVPriorDevice(int CurRouteIDSub, int CurSerialNumber)
        {
            DataView dvRoute = new DataView();
            List<int> Device = new List<int>();
            try
            {
                sql.Remove(0, sql.Length);
               sql .Append( "SELECT T_Base_Route_Device.F_DeviceIndex,F_DeviceOrder, T_Base_Route_Device.F_SerialNumber, " ).Append(
                        "T_Base_Device_Command.F_DeviceCommandIndex,T_Base_Device.F_DeviceKindIndex FROM T_Base_Device_Kind,T_Base_Device_Command " ).Append(
                        ",T_Base_Device,T_Base_Route_Device where T_Base_Device_Command.F_DeviceKindIndex = T_Base_Device.F_DeviceKindIndex" ).Append(
                        " and T_Base_Device_Kind.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex and F_RouteIDSub=" ).Append(
                        CurRouteIDSub ).Append( " and F_SerialNumber < " ).Append( CurSerialNumber ).Append( " and F_GoodsMoveKindIndex=1 order by F_SerialNumber desc ");
                dvRoute = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvRoute.Count > 0)
                {
                    Device.Add(Convert.ToInt32(dvRoute[0]["F_DeviceIndex"]));
                    Device.Add(Convert.ToInt32(dvRoute[0]["F_DeviceKindIndex"]));
                    Device.Add(Convert.ToInt32(dvRoute[0]["F_SerialNumber"]));
                    Device.Add(Convert.ToInt32(dvRoute[0]["F_DeviceOrder"]));
                }
                return Device;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.GetAGVPriorDevice:" +ex.StackTrace+ ex.Message;
                return null;
            }
            finally
            {
                Device=null ;
                dvRoute.Dispose();
            }
        
        }
        /// <summary>
        /// 判断调度表T_Monitor_Task是否存在调度任务的设备和命令
        /// </summary>
        /// <param name="Mankind">调度任务类型</param>
        /// <param name="ManFID">调度任务索引</param>
        /// <param name="DeviceIndex">设备所引</param>
        /// <param name="Order">设备命令</param>
        /// <returns></returns>
        public static bool DeviceAndOrderExitInMonitor(int Mankind, long ManFID, int DeviceIndex,int Order,int ArrowAddress)
        {
            DataView dv = new DataView();
            try
            {
                if (Order == -1) return true;
                sql.Remove(0, sql.Length);
                switch (ccf.GetDeviceKindIdx(DeviceIndex))
                {
                    case 1://堆垛机
                        sql .Append( "SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = " ).Append( ManFID ).Append( ")" ).Append(
                        " AND (F_ManageTASKKINDINDEX = " ).Append( Mankind ).Append( ") AND (F_DeviceIndex = " ).Append( DeviceIndex ).Append( ")" ).Append(
                        " AND (F_DeviceCommandIndex = " ).Append( Order ).Append( ")");
                        break;
                    case 2://输送机
                        sql .Append( "SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = " ).Append( ManFID ).Append( ")" ).Append(
                        " AND (F_ManageTASKKINDINDEX = " ).Append( Mankind ).Append( ") AND (F_DeviceIndex = " ).Append( DeviceIndex ).Append( ")" ).Append(
                        " AND (F_DeviceCommandIndex = " ).Append( Order ).Append( ")");
                        break;
                    case 4://RGV
                        sql .Append( "SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = " ).Append( ManFID ).Append( ")" ).Append(
                        " AND (F_ManageTASKKINDINDEX = " ).Append( Mankind ).Append( ") AND (F_DeviceIndex = " ).Append( DeviceIndex ).Append( ")" ).Append(
                        " AND (F_DeviceCommandIndex = " ).Append( Order ).Append( ") and F_NumParam1=" ).Append( ArrowAddress );
                        break;
                    case 6://AGV
                        sql .Append("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = " ).Append( ManFID ).Append( ")" ).Append(
                        " AND (F_ManageTASKKINDINDEX = " ).Append( Mankind ).Append( ") AND (F_DeviceIndex = " ).Append( DeviceIndex ).Append( ")" ).Append(
                        " AND (F_DeviceCommandIndex = " ).Append( Order ).Append( ")");
                        break;
                    default:
                        sql .Append( "SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = " ).Append( ManFID ).Append( ")" ).Append(
                        " AND (F_ManageTASKKINDINDEX = " ).Append( Mankind ).Append( ") AND (F_DeviceIndex = " ).Append( DeviceIndex ).Append( ")" ).Append(
                        " AND (F_DeviceCommandIndex = " ).Append( Order ).Append( ")");
                        break;
                }
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.DeviceAndOrderExitInMonitor:" +ex.StackTrace+ ex.Message;
                return false;
            }
            finally
            {
                dv.Dispose();
                
            }
        }
        /// <summary>
        /// 找到不能移动托盘的关键设备的前一个设备:例如,条码扫描器前一个位置(运动反方向)的输送机
        /// </summary>
        /// <param name="prirorDevice">关键设备的前一个设备</param>
        /// <param name="prirorKeydevice">关键设备</param>
        /// <returns></returns>
        static List<int> NewCurDevice(List<int> prirorDevice, List<int> prirorKeydevice)
        {//0设备所引;1设备类型;2:路径序号;3:设备命令
            DataView dv = new DataView();
            try
            {
                if ((prirorKeydevice.Count > 0) && (prirorDevice.Count > 0))
                {
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT F_GoodsMoveKindIndex, F_DeviceKindIndex FROM T_Base_Device_Kind WHERE " ).Append(
                        "(F_DeviceKindIndex = " ).Append( prirorKeydevice[1] ).Append( ") and (F_GoodsMoveKindIndex=2)");
                    dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        if (Model.CGetInfo.GetDeviceInfo(prirorDevice[0]).UnControl != "1")
                        {
                            return prirorDevice;
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.NewCurDevice:" +ex.StackTrace+ ex.Message;
                return null;
            }
            finally
            {
                prirorDevice=null;
                prirorKeydevice=null;
                dv.Dispose();
            }
        }
        /// <summary>
        /// 获取可用的目标位置:入库的巷道或者出库站台;同时修改调度任务为不可拆分状态
        /// </summary>
        /// <param name="dr">T_Manage_Task表的行记录</param>
        /// <returns></returns>
        public static void GetUsableDestination(DataRowView dr)
        {
            DataView dvIO = new DataView(); DataTable dtd = new DataTable();
            try
            {
                dr = dbo.ExceSQL(string.Format("SELECT * FROM T_Manage_Task   WHERE (FID = {0}) AND (F_ManageTaskKindIndex = {1})", Convert.ToInt32(dr["FID"]), Convert.ToInt32(dr["F_ManageTaskKindIndex"]))).Tables[0].DefaultView[0];//20130620
                
                //只修申请改调度任务的路径,不修改调度自动任务路径
                if (dr["F_ManageTaskKindIndex"].ToString() != "1") return;


                string adviceDev = string.Empty;
                //20100610查找一个路径可用,任务数最少的终点设备
                Dictionary<int, int> advDev = new Dictionary<int, int>();
                object ob;

                //20091107
                sql.Remove(0, sql.Length);
                sql.Append("select CONTROL_ID, CONTROL_STATUS from IO_Control where (CONTROL_ID = ").Append(Convert.ToInt32(dr["FID"])).Append(") AND ((CONTROL_STATUS=").Append(Model.CGeneralFunction.TASKALTERROUTEAPPLY).Append(") or (CONTROL_STATUS=").Append(Model.CGeneralFunction.TASKALTERROUTEREPLY).Append("))");
                dvIO = dboM.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvIO.Count <= 0)
                {
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT F_TASKKIND, F_DESTINATION FROM T_Base_Destination WHERE (F_Warehouse='").Append(dr["FENDWAREHOUSE"]).Append("') and  (F_TASKKIND = ").Append(dr["FCONTROLTASKTYPE"]).Append(") and (F_AbendStation=1) and (F_DESTINATION=").Append(dr["FENDDEVICE"]).Append(")");
                    dtd = dbo.ExceSQL(sql.ToString()).Tables[0];
                    if (dtd.Rows.Count > 0)//原来终点是应急站台那么还分配这个站台,不需要改变
                    {
                        adviceDev = dtd.Rows[0]["F_DESTINATION"].ToString();
                        //20091005
                        return;
                    }
                    else
                    {
                        //查找可用目标位置
                        sql.Remove(0, sql.Length);
                        sql.Append("SELECT F_TASKKIND, F_DESTINATION FROM T_Base_Destination WHERE (F_Warehouse='").Append(dr["FENDWAREHOUSE"]).Append("') and  (F_TASKKIND = ").Append(dr["FCONTROLTASKTYPE"]).Append(") and (F_DESTINATION <>").Append(dr["FENDDEVICE"]).Append(") and (F_AbendStation=0)");
                        DataTable dt = dbo.ExceSQL(sql.ToString()).Tables[0];
                        if (dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                if (CDisassembleTask.MinRouteID(Convert.ToInt32(dr["FSTARTDEVICE"]), Convert.ToInt32(dt.Rows[i]["F_DESTINATION"]), dr["FUseAwayFork"]) != -1)
                                {
                                    //20100610统计到终点设备正在执行的任务数
                                    sql.Remove(0, sql.Length);
                                    sql.Append("SELECT count(FENDDEVICE) as counts FROM T_Manage_Task where FENDDEVICE='").Append(dt.Rows[i]["F_DESTINATION"]).Append("' and fstatus>0");
                                    ob = dbo.GetSingle(sql.ToString());
                                    advDev.Add(Convert.ToInt32(dt.Rows[i]["F_DESTINATION"]), Convert.ToInt32(ob));

                                    //20100610  adviceDev = dt.Rows[i]["F_DESTINATION"].ToString();
                                    //break;
                                }
                            }
                            if (advDev.Count == 0)//20100610
                            {

                                return;


                            }
                            else
                            {//20100610
                                int mincount = 9999;
                                foreach (int aaa in advDev.Keys)
                                {
                                    if (advDev[aaa] < mincount)
                                    {
                                        mincount = advDev[aaa];
                                        adviceDev = aaa.ToString();
                                    }
                                }
                                if (adviceDev == "")
                                {

                                    return;

                                }
                            }
                        }
                        else
                        {
                            return;
                        }
                    }


                    //DataView DV = dboM.ExceSQL("select IO_CONTROL_APPLY_SEQ.NEXTVAL FROM DUAL").Tables[0].DefaultView;
                    //20101108int fid = dboM.GetManageTableIndex("IO_CONTROL_APPLY");
                    string dtime = DateTime.Now.ToString("u");
                    dtime = dtime.Substring(0, dtime.Length - 1);
                    //20091128
                    dboM.TransBegin();
                    try
                    {
                        sql.Remove(0, sql.Length);
                        sql.Append(" update IO_CONTROL set CONTROL_STATUS=").Append(Model.CGeneralFunction.TASKALTERROUTEAPPLY).Append(" where CONTROL_ID=").Append(dr["FID"]);
                        dboM.ExceSQL(sql.ToString());
                        //向管理申请修改任务//
                        int appid = dboM.GetManageTableIndex("IO_CONTROL_APPLY", true);//CONTROL_APPLY_ID
                  
                        sql.Remove(0, sql.Length);
                        sql.Append("INSERT INTO IO_CONTROL_APPLY (CONTROL_APPLY_ID, CONTROL_ID,CONTROL_APPLY_TYPE,WAREHOUSE_CODE, STOCK_BARCODE, DEVICE_CODE, APPLY_TASK_STATUS, CREATE_TIME, ").Append(
                                " CONTROL_APPLY_REMARK)").Append(
                        "VALUES (").Append(appid).Append(",").Append(dr["FID"] + ",2,'" + dr["FENDWAREHOUSE"]).Append("','").Append(dr["FPALLETBARCODE"]).Append("','").Append(adviceDev).Append("',0,'").Append(dtime).Append("',null)");
                        dboM.ExceSQL(sql.ToString());
                        dboM.TransCommit();
                        //修改调度任务为不可拆分状态
                        sql.Remove(0, sql.Length);
                        sql.Append("update T_Manage_Task set  FIntoStepOK='-' where (FID = ").Append(Convert.ToInt32(dr["FID"])).Append(") AND (F_ManageTASKKINDINDEX =1)");
                        dbo.ExceSQL(sql.ToString());
                        CommonClassLib.CCarryConvert.WriteDarkCasket("CControl.GetUsableDestination", "拆分任务,向管理申请改道", "管理任务:" + dr["FID"].ToString(), "条码:" + dr["FPALLETBARCODE"].ToString().ToUpper());


                    }
                    catch (Exception ex)
                    {
                        DisassembleTaskError = string.Format("拆分任务,向管理申请改道时:{0}", ex.StackTrace+ex.Message );
                        dboM.TransRollback();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dvIO.Dispose();
                dtd.Dispose();
            }
            
        }

        public static void  CreateRelativeMonitor(int manKind, int FID)
        {//20120906

            //if (DoubleForkSingleCell(manKind, FID) == true) return;
            int relativeFID = -1;
            DataView dv=new DataView() ; DataView dvs=new DataView() ;
            try
            {
                sql.Remove(0, sql.Length);
                sql.Append("SELECT F_RELATIVECONTORLID FROM T_Manage_Task WHERE (FID = " ).Append( FID ).Append( ") AND (F_ManageTaskKindIndex = " ).Append( manKind ).Append( ") and (F_RELATIVECONTORLID<>-1)");
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    relativeFID = Convert.ToInt32(dv[0]["F_RELATIVECONTORLID"]);
                }
                else
                {
                    return ;
                    
                }
                //多叉关联调度任务在此拆分20100323
                //获取多叉关联任务的路径和任务信息,递归调用CreateMonitor
                

                if (relativeFID != -1)
                {
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT T_Manage_Task.* FROM T_Manage_Task WHERE (F_RELATIVECONTORLID = ").Append(relativeFID).Append(") AND (F_ManageTaskKindIndex = ").Append(manKind).Append(") and (FIntoStepOK=0)");
                    dvs = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    for (int j = 0; j < dvs.Count;j++ )
                    {
                        int minrid = MinRouteID(Convert.ToInt32(dvs[j]["FSTARTDEVICE"]), Convert.ToInt32(dvs[j]["FENDDEVICE"]), dvs[j]["FUseAwayFork"]);
                        if (minrid != -1)
                        {
                            if (CreateMonitor(manKind, Convert.ToInt32(dvs[j]["FID"]), minrid, dvs[j], 0) > 0)
                            {
                                //分解完成,修改T_Manage_Task表FIntoStepOK=1
                                sql.Remove(0, sql.Length);
                                sql.Append("update T_Manage_Task set FIntoStepOK='1' where FID=").Append(dvs[j]["FID"]
                                ).Append(" and F_ManageTaskKindIndex=").Append(dvs[0]["F_ManageTaskKindIndex"]);
                                dbo.ExceSQL(sql.ToString());
                                CDataChangeEventArgs cea = new CDataChangeEventArgs(null, null);
                                OnDataChange("CreateRelativeMonitor", cea);
                            }
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "CreateRelativeMonitor,"+ex.StackTrace+ex.Message ;
            }
            finally
            {
                dv.Dispose();
                dvs.Dispose();
            }
        }
        /// <summary>
        /// 找到一个双叉最合适货位,修改关联关系
        /// </summary>
        /// <param name="dr"></param>
        public static void GetOutDoubleForkTask(DataRowView dr)
        {//sqlser的 SUBSTRING(express1,1开始,2);C#的.Substring(0开始, 2)
            DataView dv = new DataView();
            try
            {
                //20170104 出库任务数量限制,不能关联出库任务
                if (ccf.GetTaskCountsCanDisassembleToEndDevice(Convert.ToInt32(dr["FID"]), Convert.ToInt32(dr["FENDDEVICE"])) == false) return;
                dr = dbo.ExceSQL(string.Format("SELECT * FROM T_Manage_Task   WHERE (FID = {0}) AND (F_ManageTaskKindIndex = {1})", Convert.ToInt32(dr["FID"]), Convert.ToInt32(dr["F_ManageTaskKindIndex"]))).Tables[0].DefaultView[0];//20130620
                
                if (dr["F_RELATIVECONTORLID"].ToString() != "-1") return;


                int forwlimitX = 0, backlimitX = 0;
                sql.Remove(0, sql.Length);
                sql.Append("SELECT F_ForwardLimitX, F_BackLimitX FROM T_Base_LaneInfo WHERE (F_LaneDeviceIndex = ").Append(dr["FLANEWAY"]).Append(")");
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    forwlimitX = Convert.ToInt32(dv[0]["F_ForwardLimitX"]);
                    backlimitX = Convert.ToInt32(dv[0]["F_BackLimitX"]);
                }
                else
                    return;
                int forkamount = 1;
                sql.Clear();
                sql.Append("SELECT F_ForkAmount FROM T_Base_StackInfo WHERE  F_StackIndex = ").Append(Convert.ToInt32(dr["FSTACK"]));
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    forkamount = Convert.ToInt32(dv[0]["F_ForkAmount"])-1;
                }
                else
                    return;
                StringBuilder sqladd = new StringBuilder(" ");
                if (forwlimitX.ToString() == dr["FStartCol"].ToString())
                {
                    sqladd.Append(" and FStartCol <>").Append(forwlimitX).Append(" ");
                }
                else if (backlimitX.ToString() == dr["FStartCol"].ToString())
                {
                    sqladd.Append(" and FStartCol <>").Append(backlimitX).Append(" ");
                }
                else
                {
                    sqladd.Append(" ");
                }
                //列的速度是层的速度的3倍,所以层的差值要乘以三(提升一层的时间相当于移动三列的时间)
                sql.Remove(0, sql.Length);
                if (CStaticClass.DBFactory == "OracleDBFactory")
                {//20151120调度系统oracle的特殊语句
                    sql.Append("SELECT ").Append(" F_ManageTaskKindIndex, FID, MIN(POWER(FStartCol  - ").Append(dr["FStartCol"]).Append(", 2) + POWER((FStartLayer - ").Append(
                     dr["FStartLayer"]).Append(") * 3, 2)) AS Expr1 FROM T_Manage_Task WHERE (FIntoStepOK = '0') AND rownum<=").Append(forkamount).Append(
                     " and (FSTATUS = 0 and FUseAwayFork<>'3') AND (FCONTROLTASKTYPE = ").Append(dr["FCONTROLTASKTYPE"]).Append(") AND (FLOGIC_AREA = '").Append(dr["FLOGIC_AREA"]).Append(
                     "') AND (FCONTORL_BATCH = '").Append(dr["FCONTORL_BATCH"]).Append("') AND (FLANEWAY = ").Append(dr["FLANEWAY"]).Append(
                     ") AND (F_RELATIVECONTORLID = - 1) and F_ManageTaskKindIndex=").Append(dr["F_ManageTaskKindIndex"]).Append(" and FID<>").Append(dr["fid"]).Append(
                     sqladd.ToString()).Append("  GROUP BY F_ManageTaskKindIndex, FID order by Expr1 asc");
                }
                else
                {//20151120调度系统SQLServer的特殊语句
                    sql.Append("SELECT TOP ").Append(forkamount).Append(" F_ManageTaskKindIndex, FID, MIN(POWER(FStartCol  - ").Append(dr["FStartCol"]).Append(", 2) + POWER((FStartLayer - ").Append(
                     dr["FStartLayer"]).Append(") * 3, 2)) AS Expr1 FROM T_Manage_Task WHERE (FIntoStepOK = '0') AND ").Append(
                     "(FSTATUS = 0 and FUseAwayFork<>'3') AND (FCONTROLTASKTYPE = ").Append(dr["FCONTROLTASKTYPE"]).Append(") AND (FLOGIC_AREA = '").Append(dr["FLOGIC_AREA"]).Append(
                     "') AND (FCONTORL_BATCH = '").Append(dr["FCONTORL_BATCH"]).Append("') AND (FLANEWAY = ").Append(dr["FLANEWAY"]).Append(
                     ") AND (F_RELATIVECONTORLID = - 1) and F_ManageTaskKindIndex=").Append(dr["F_ManageTaskKindIndex"]).Append(" and FID<>").Append(dr["fid"]).Append(
                     sqladd.ToString()).Append("  GROUP BY F_ManageTaskKindIndex, FID order by Expr1 asc");
                }
                
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {
                    sql.Remove(0, sql.Length);
                    sql.Append("update T_Manage_Task set F_RELATIVECONTORLID=").Append(dr["fid"]).Append(" where FID=").Append(dv[i]["FID"]).Append(" and F_ManageTaskKindIndex=").Append(dr["F_ManageTaskKindIndex"]);
                    dbo.ExecuteSql(sql.ToString());
                    
                }
                sql.Remove(0, sql.Length);
                sql.Append("update T_Manage_Task set F_RELATIVECONTORLID=").Append(dr["fid"]).Append(" where FID=").Append(dr["fid"]).Append(" and F_ManageTaskKindIndex=").Append(dr["F_ManageTaskKindIndex"]);
                dbo.ExecuteSql(sql.ToString());
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv.Dispose();
            }
        }

        /// <summary>
        /// 20110412是否存在不同的逻辑区域未通过条码分道
        /// </summary>
        /// <param name="Area">逻辑区域</param>
        /// <returns></returns>
        public static bool IfExitElseAreaTask(int mankind,int FID)
        {
            DataView dv = new DataView();
            try
            {
                object ob = dbo.GetSingle(string.Format("select FLOGIC_AREA from T_Manage_Task where F_ManageTaskKindIndex={0} and FID={1} and FCONTROLTASKTYPE=2 and FLOGIC_AREA>'20000'", mankind, FID));
                if (ob != null)
                {
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT F_NumParam4 FROM T_Manage_Task ,T_Monitor_Task  where F_NumParam4=12030 and T_Manage_Task.F_ManageTaskKindIndex = T_Monitor_Task.F_ManageTASKKINDINDEX  AND T_Manage_Task.FID = T_Monitor_Task.F_ManageTaskIndex and FCONTROLTASKTYPE=2 and T_Monitor_Task.F_ManageTaskKindIndex=1 and FLOGIC_AREA>'20000'  and FLOGIC_AREA <>'").Append(ob.ToString()).Append("'");//and FSTATUS>020110412
                    dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        return true;

                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format("拆分任务时,判断是否存在同一逻辑区域且不同批号的出库任务:{0}", ex.StackTrace+ex.Message );
                return false;
            }
            finally
            {
                dv.Dispose();
            }
        }

        static bool GetUAreaOutputDevice(int StackIndex,int UCode,out int OutputUdevice,out string  OutputUZxy)
        {
            DataView dv = new DataView();
            OutputUdevice = 0;
            OutputUZxy = "-";
            try
            {
                dv = dbo.ExceSQL(string.Format("SELECT DISTINCT  F_OutputUDevice, F_OutputUZxy FROM T_Base_UArea,ST_CELL where T_Base_UArea.F_UCODE = ST_CELL.FUCODE and (T_Base_UArea.F_UCODE = {0}) AND (ST_CELL.FStack = {1})", UCode, StackIndex)).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    OutputUdevice = Convert.ToInt32(dv[0]["F_OutputUDevice"]);
                    OutputUZxy = dv[0]["F_OutputUZxy"].ToString();
                }
                return true;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format("CDisassembleTask.GetUAreaOutputDevice错误:{0}", ex.StackTrace+ex.Message );
                return false;
            }
            finally
            {
                dv.Dispose();
            }

        }
        static bool GetUAreaInputDevice(int StackIndex,int UCode, out int InputUdevice, out string InputUZxy)
        {
            DataView dv = new DataView();
            InputUdevice = 0;
            InputUZxy = "-";
            try
            {
                dv = dbo.ExceSQL(string.Format("SELECT DISTINCT  F_InputUDevice, F_InputUZxy FROM T_Base_UArea,ST_CELL where T_Base_UArea.F_UCODE = ST_CELL.FUCODE and (T_Base_UArea.F_UCODE = {0}) AND (ST_CELL.FStack = {1})", UCode, StackIndex)).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    InputUdevice = Convert.ToInt32(dv[0]["F_InputUDevice"]);
                    InputUZxy = dv[0]["F_InputUZxy"].ToString();
                }
                return true;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = string.Format("CDisassembleTask.GetUAreaOutputDevice错误:{0}", ex.StackTrace+ex.Message );
                return false;
            }
            finally
            {
                dv.Dispose();
            }

        }
        static bool DoubleForkSingleCell(int manKind,int  FID)
        {
            DataView dv = new DataView();
            try
            {
                dv = dbo.ExceSQL(string.Format("SELECT T_Manage_Task.FID FROM T_Manage_Task ,ST_CELL WHERE T_Manage_Task.FSTARTWAREHOUSE = ST_CELL.FWAREHOUSE AND T_Manage_Task.FSTARTCELL = ST_CELL.FCELLCODE and ST_CELL.FDoubleFork = '3'  AND T_Manage_Task.FID = {1} AND T_Manage_Task.F_ManageTaskKindIndex = {0}", manKind, FID)).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return true;
                }
                else
                {
                    dv = dbo.ExceSQL(string.Format("SELECT T_Manage_Task.FID FROM T_Manage_Task ,ST_CELL WHERE T_Manage_Task.FENDWAREHOUSE = ST_CELL.FWAREHOUSE AND T_Manage_Task.FENDCELL = ST_CELL.FCELLCODE and ST_CELL.FDoubleFork = '3'  AND T_Manage_Task.FID = {1} AND T_Manage_Task.F_ManageTaskKindIndex = {0}", manKind, FID)).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv.Dispose();
            }
        }
        static int GetBarcodeDeviceFromBindDevice(int barcodeBindDevice)
        {
            DataView ob = new DataView();
            try
            {
                ob = dbo.ExceSQL(string.Format("SELECT F_DeviceIndex FROM T_Base_PLC_Ask WHERE (F_BindingDevice = {0})", barcodeBindDevice)).Tables[0].DefaultView;
                if (ob.Count > 0)
                {
                    return Convert.ToInt32(ob[0][0]);
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ob.Dispose();
            }
        }
        static int  GetSameStartDeviceEnddeviceRouteID(int startdevice, int enddevice)
        {
            DataView dv=new DataView();
            try
            {
                sql.Remove(0, sql.Length);
                sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                    "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                    " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                    " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append(" and F_StartDevice=F_EndDevice and (F_StartDevice=30100 or F_StartDevice=30200 or F_StartDevice=40100)");
                dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0][0]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv.Dispose();
            }
        }



        /// <summary>
        /// 是否允许任务拆分 ,同一个站台有入库任务出库任务不拆分 20171106 by lyj add 
        /// </summary>
        /// <param name="drv"></param>
        /// <returns></returns>


        /*  
         * 方法名:isOrNotAllowDisaemble
         * 作  者:LYJ  
         * 方法功能:是否允许任务拆分 ,同一个站台有入库任务出库任务不拆分
         * 参数介绍:
         * 返 回 值:返回true表示拆分,false表示不拆分
         * 完成日期:2018/01/15
         * 维护人员:
         * 维护日期:
         * 维护原因:
         * 当前版本:1.0 
         * 前继版本:1.0beta  
         */
        public static bool isOrNotAllowDisaemble(DataRowView drv)
        {
            DataView dvoutin=new DataView();;
            int tasktype = 0;
            int startdevice = 0;
            int enddevice = 0;
            Model.MDevice devinfoion;
            string intask = string.Empty;
            string outtask = string.Empty;
            try
            {
                tasktype = Convert.ToInt32(drv["FCONTROLTASKTYPE"]);

                startdevice = Convert.ToInt32(drv["FSTARTDEVICE"]);

                enddevice = Convert.ToInt32(drv["FENDDEVICE"]);

                #region  add by lyj20180621穿梭板不控制出入库数量
                if (tasktype == 1)
                {
                    if (enddevice == 18005)
                    {
                        return true;
                    }

                }
                else if (tasktype == 2)
                {
                    if (startdevice == 18005)
                    {
                        return true;
                    }
                }

                #endregion
                if (tasktype == 1)
                {


                    outtask = "select * from T_Manage_Task where FCONTROLTASKTYPE=2 and FENDDEVICE =" + startdevice;

                    sql.Remove(0, sql.Length);

                    sql.Append(outtask);

                    dvoutin = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                    if (dvoutin.Count > 0)
                    {
                        return false;
                    }

                }
                else if (tasktype == 2 )
                {


                    intask = "select * from T_Manage_Task where FCONTROLTASKTYPE=1 and FSTARTDEVICE =" + enddevice;

                    //sql.Remove(0, sql.Length);

                    sql.Append(intask);

                    dvoutin = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                    if (dvoutin.Count > 0)
                    {
                        return false;
                    }
                    devinfoion = Model.CGetInfo.GetDeviceInfo(enddevice);


                    if (devinfoion.SplitByte_0==1)
                    {

                        return false;
                    }
                  

                   

                }
                #region  拣选任务的控制,每次只拆分一个拣选任务  20160303 add by lyj
                //else if (tasktype == 2 && Convert.ToInt32(drv["FChooseTask"]) == 1)
                //{

                //    #region  update by lyj 20160525 站台占用拣选任务不拆分
                //    checkStationStatus = "select * from T_Base_Device  where F_StationStatus=1 and F_DeviceIndex=" + enddevice;

                //    sql.Remove(0, sql.Length);

                //    sql.Append(checkStationStatus);

                //    dvoutin = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                //    if (dvoutin.Count > 0)
                //    {
                //        return false;
                //    }
                //    #endregion

                //    #region update by lyj 20160525 针对同一个站台每次有一个拣选任务拆分
                //    choosetask = "select * from T_Manage_Task where FCONTROLTASKTYPE=2 and FChooseTask=1 and FIntoStepOK=1 and FENDDEVICE =" + enddevice;

                //    sql.Remove(0, sql.Length);

                //    sql.Append(choosetask);

                //    dvoutin = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                //    if (dvoutin.Count > 0)
                //    {
                //        return false;
                //    }

                //    #endregion

                //    #region add by lyj 20160525 有入库任务时拣选任务不拆分
                //    if (startdevice < 18004)
                //    {

                //        if (enddevice == 52107 || enddevice == 62101 || enddevice == 72101)
                //        {
                //            intask = "select * from T_Manage_Task where FCONTROLTASKTYPE=1 and FSTARTDEVICE in ('52107','62101','72101')";

                //        }
                //        else
                //        {

                //            intask = "select * from T_Manage_Task where FCONTROLTASKTYPE=1 and FSTARTDEVICE =" + enddevice;
                //        }

                //    }
                //    else
                //    {
                //        if (enddevice == 52008 || enddevice == 62001 || enddevice == 72001)
                //        {
                //            intask = "select * from T_Manage_Task where FCONTROLTASKTYPE=1 and FSTARTDEVICE in ('52008','62001','72001')";

                //        }
                //        else
                //        {
                //            intask = "select * from T_Manage_Task where FCONTROLTASKTYPE=1 and FSTARTDEVICE =" + enddevice;
                //        }

                //    }

                //    sql.Remove(0, sql.Length);

                //    sql.Append(intask);

                //    dvoutin = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;

                //    if (dvoutin.Count > 0)
                //    {
                //        return false;
                //    }
                //    #endregion

                //    updateStationStatus = "update T_Base_Device set F_StationStatus=1 where F_DeviceIndex=" + enddevice;

                //    sql.Remove(0, sql.Length);

                //    sql.Append(updateStationStatus);

                //    dbo.ExceSQL(sql.ToString());
                //}
                #endregion
                return true;

            }
            catch (Exception eiad)
            {
                return false;
            }

        }




        /*  
         * 方法名:conveyorNumFromPosition
         * 作  者:LYJ  
         * 方法功能:根据起始位置找到合适的输送机编号
         * 参数介绍:
         * 返 回 值:输送机编号
         * 完成日期:2018/04/23
         * 维护人员:
         * 维护日期:
         * 维护原因:
         * 当前版本:1.0 
         * 前继版本:1.0beta  
         */
        public static int conveyorNumFromPosition(string FSTARTCELL, string FENDCELL, int startdevice, int enddevice,int tasktype)
        {

          
            string sy = "";//表示层
            int iy = 0;//表示层
            int deviceindex = 0;
            DataView dvconveyor = new DataView(); ;
            string sqlconveyor = string.Empty;
            try
            {

                if(tasktype==1&&(startdevice==18001||enddevice==18001))
                {

                    if(FENDCELL!=null)
                    {
                        if (FENDCELL.Length == 8)
                        {
                            sy = FENDCELL.Substring(6);
                            if(sy!="")
                            {
                                iy = Convert.ToInt32(sy);
                                sqlconveyor = "select * from T_Base_conveyorNumFromPosition where FTASKTYPE=1 and F_Station ="+ startdevice + " and FLAYER=" + iy;
                                sql.Remove(0, sql.Length);

                                sql.Append(sqlconveyor);

                                dvconveyor = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                if (dvconveyor.Count>0)
                                {
                                    deviceindex = Convert.ToInt32(dvconveyor[0]["FCONVEYORNUM"]);
                                }
                                
                            }
                            
                        }
                    }

                }
                else if (tasktype == 2 && (startdevice == 18001 || enddevice == 18001)) 
                {
                    if (FSTARTCELL != null)
                    {
                        if (FSTARTCELL.Length == 8)
                        {
                            sy = FSTARTCELL.Substring(6);
                            if (sy != "")
                            {
                                iy = Convert.ToInt32(sy);
                                sqlconveyor = "select * from T_Base_conveyorNumFromPosition where FTASKTYPE=2  and F_Station =" + enddevice + " and FLAYER=" + iy;
                                sql.Remove(0, sql.Length);

                                sql.Append(sqlconveyor);

                                dvconveyor = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                                if (dvconveyor.Count > 0)
                                {
                                    deviceindex = Convert.ToInt32(dvconveyor[0]["FCONVEYORNUM"]);
                                }

                            }

                        }
                    }
                }










                return deviceindex;
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.MinRouteID时发生错误:" + ex.Message;
                return 0;
            }

        }


        /// <summary>
        /// 20100702
        /// 根据调度任务的起点和终点设备索引查找路径最短的RouteIDSub  update by lyj20180423
        /// </summary>
        /// <param name="startdevice">起点设备索引</param>
        /// <param name="enddevice">终点设备索引</param>
        /// <param name="useAwayfork">是否使用远货叉</param>
        /// <returns></returns>
        // public static int MinRouteID(int startdevice, int enddevice, object useAwayfork)
        public static int MinRouteID(int startdevice, int enddevice, object useAwayfork,string FSTARTCELL,string FENDCELL,int tasktype)
       
        {
            //20101124
            DataView dv = new DataView(); DataView dvd = new DataView();
            DataView dv1 = new DataView(); DataView dv2 = new DataView();
            StringBuilder dff = new StringBuilder();
            if (useAwayfork.ToString() == "-")
            {
                dff.Append("F_UseAwayFork<>'n'");
            }
            else
            {
                dff.Append("(F_UseAwayFork='").Append(useAwayfork).Append("' or F_UseAwayFork='-' )");//20101028
            }
            #region 工位堆垛机的路径优选20120207
            //过桥输送机的选择是临近取货位优先
            //35000双顶升机的选择是小列(1排6列;2排26列)选择顶升机32038

            //if (startdevice == 35000 && (enddevice == 32025 || enddevice == 10100 || enddevice == 10200 || enddevice == 10300))
            //{

            //    dff.Append(" and T_Base_Route_Device.F_DeviceIndex=32038");
            //}
            //else
            //{

            //}
            #endregion
            //20101124
            try
            {
                int retrouteidsub = GetSameStartDeviceEnddeviceRouteID(startdevice, enddevice);
                if (retrouteidsub > 0)
                {
                    return retrouteidsub;
                }
                //选择最短调度路径并且判断此路径上是否有设备发生故障
                //20101124
                #region  add by lyj20180424 找到与层匹配的输送机
                //if ((tasktype == 1 || tasktype == 2)&&(startdevice==18001||enddevice==18001))
                //{
                //    int conveyorindex = conveyorNumFromPosition(FSTARTCELL, FENDCELL, startdevice, enddevice, tasktype);

                //    sql.Remove(0, sql.Length);
                //    sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                //        "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                //        " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                //        " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append("  and ").Append(dff.ToString()).Append(" and F_Status=1 and T_Base_Route_Device.F_DeviceIndex=").Append(conveyorindex);//and T_Base_Route_Device.F_DeviceIndex=12018
                //}
                //else 
                //{
                //    sql.Remove(0, sql.Length);
                //    sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                //        "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                //        " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                //        " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append("  and ").Append(dff.ToString()).Append(" and F_Status=1 ");//and T_Base_Route_Device.F_DeviceIndex=12018
                //}
                sql.Remove(0, sql.Length);
            sql.Append("SELECT distinct(T_Base_Route_Device.F_RouteIDSub) FROM T_Base_Device,T_Base_Route_Device,").Append(
                "T_Base_Route where T_Base_Route_Device.F_RouteID = T_Base_Route.F_RouteID and ").Append(
                " T_Base_Route_Device.F_DeviceIndex = T_Base_Device.F_DeviceIndex and ").Append(
                " F_StartDevice=").Append(startdevice).Append(" and F_EndDevice=").Append(enddevice).Append("  and ").Append(dff.ToString()).Append(" and F_Status=1 ");
            #endregion
            dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                List<double> minroute = new List<double>();//0,routIDSub;1,路径最小值
                if (dv.Count == 0)
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
                for (int i = 0; i < dv.Count; i++)
                {
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT F_RouteIDSub,F_LockedState,T_Base_Device.F_DeviceIndex FROM T_Base_Device,T_Base_Route_Device WHERE ").Append(
                        " T_Base_Device.F_DeviceIndex = T_Base_Route_Device.F_DeviceIndex  and (T_Base_Route_Device.F_RouteIDSub = ").Append(
                        dv[i]["F_RouteIDSub"]).Append(") AND  (T_Base_Device.F_LockedState = - 1 or T_Base_Device.F_ErrorCode>=30)");//20121203
                    dvd = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dvd.Count > 0)
                    {
                        int dvin = Convert.ToInt32(dvd[0]["F_DeviceIndex"]);
                        if ((dvin >= 35001 && dvin <= 35003) || (dvin == 45001))
                        {//20120207机器人地位无法取代,不判断是否被禁用

                        }
                        else
                        {
                            continue;
                        }
                    }

                    List<double> route = new List<double>();//0,routIDSub;1,步长;2,路径使用频率;3,路径设备任务数
                    //路径步长(设备总数)最短(权重0.3),路径使用频率最小(权重0.3)的路径,路径设备任务数(权重0.3)

                    route.Add(Convert.ToDouble(dv[i]["F_RouteIDSub"]));//RouteIDSub
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("select count(F_DeviceIndex) as steps from T_Base_Route_Device where F_RouteIDSub="
                        ).Append(dv[i]["F_RouteIDSub"]);
                    dv1 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv1.Count > 0)
                    {
                        route.Add(Convert.ToDouble(dv1[0]["steps"]));//步长
                        //route.Add(0);
                    }
                    else
                    {
                        continue;
                    }
                    //路径使用频率:路径执行调度任务的数量
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(DISTINCT F_ManageTaskIndex) AS ManCount FROM T_Monitor_Task GROUP BY F_RouteID HAVING (F_RouteID = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["ManCount"].ToString(), out a) == true)
                        {
                            route.Add(a);//路径使用频率
                        }
                        else
                        {
                            route.Add(0);//路径使用频率
                        }
                    }
                    else
                    {
                        route.Add(0);//路径使用频率
                    }
                    //路径设备任务数
                    //20101124
                    sql.Remove(0, sql.Length);
                    sql.Append("SELECT COUNT(T_Monitor_Task.F_MonitorIndex) AS mtask FROM T_Monitor_Task ,T_Base_Route_Device where (T_Monitor_Task.F_deviceindex = T_Base_Route_Device.F_deviceindex) and  (T_Monitor_Task.F_RouteID = T_Base_Route_Device.F_RouteIDSub) and  (T_Base_Route_Device.F_RouteIDSub = ").Append(dv[i]["F_RouteIDSub"]).Append(")");
                    dv2 = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dv2.Count > 0)
                    {
                        double a = 0;
                        if (double.TryParse(dv2[0]["mtask"].ToString(), out a) == true)
                        {
                            route.Add(a);//路径设备任务数
                        }
                        else
                        {
                            route.Add(0);//路径设备任务数
                        }
                    }
                    else
                    {
                        route.Add(0);//路径设备任务数
                    }

                    //求最短路径((路径步长*0.3+路径使用频率*0.3+设备占有数*0.3)的最小值)
                    if (minroute.Count == 0)
                    {
                        minroute.Add(route[0]);
                        minroute.Add(route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                    }
                    else
                    {
                        if (minroute[1] > (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3))
                        {
                            minroute[0] = route[0];
                            minroute[1] = (route[1] * 0.3 + route[2] * 0.3 + route[3] * 0.3);
                        }
                    }
                }
                if (minroute.Count > 0)
                {
                    return Convert.ToInt32(minroute[0]);
                }
                else
                {
                    RefreshMonitorEventArgs rme = new RefreshMonitorEventArgs("tsStatus", "起点设备:" + startdevice + "和终点设备:" + enddevice + "之间路径不可用!");
                    OnRefreshMonitor(rme);
                    return -1;
                }
            }
            catch (Exception ex)
            {
                DisassembleTaskError = "ControlSystem.CDisassembleTask.MinRouteID时发生错误:" + ex.StackTrace + ex.Message;
                return -1;
            }
            finally
            {
                dv.Dispose();
                dv1.Dispose();
                dv2.Dispose();
                dvd.Dispose();
            }


        }

    }

}