using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.Linq;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// Creator:Richard.liu and Jack.Ma
    /// 设备实时状态显示
    /// </summary>
    public partial class FrmDeviceStatus : Form
    {
        #region 变量定义
        Model.MDevice _device = null;
        int _deviceIndex;
        DataView dv;
        string sqlstr = string.Empty;
        DBFactory.DBOperator dbo = CStaticClass.dbo;
        DBFactory.DBOperator dboMan = CStaticClass.dboM;
        /// <summary>
        /// 设置要显示的设备索引
        /// </summary>
        public int DeviceIndex
        {
            set { _deviceIndex = value;}
        }
        private static FrmDeviceStatus _formInstance;

        public static FrmDeviceStatus FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmDeviceStatus();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        #endregion

        #region 构造函数/初始化
        public FrmDeviceStatus()
        {
            InitializeComponent();
            _formInstance = this;
        }

        private void FrmDeviceStatus_Load(object sender, EventArgs e)
        {
            //StringBuilder sql=new StringBuilder();
            //sql.Append("SELECT F_LaneGateDeviceIndex from T_Base_Lane_Gate where F_LaneGateDeviceIndex=").Append(_deviceIndex);
            //DataView  dvlane = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
            //if (dvlane.Count > 0)
            //{
            //    sql = new StringBuilder();
            //    sql.Append("SELECT F_DeviceIndex from T_Base_PLC_Ask   where F_DeviceIndex=").Append(_deviceIndex);
            //    dvlane = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
            //    if (dvlane.Count > 0)
            //    {
            //        this.btCancelDoubleFork.Visible = true;
            //    }
            //    else
            //    {
            //        this.btCancelDoubleFork.Visible = false;
            //    }
            //    this.btLogicHaveGoods.Visible = true;
            //    this.btLogicNoneGoods.Visible = true;
            //    textBox1.Visible = true;
            //}
            //else
            //{
            //    this.btCancelDoubleFork.Visible = false;
            //    this.btLogicHaveGoods.Visible = false;
            //    this.btLogicNoneGoods.Visible = false;
            //    textBox1.Visible = false;
            //}
            btnRefresh_Click(null, EventArgs.Empty);
            
        }
        #endregion

        #region 更新设备状态
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                tabControl_Device.SelectedIndex = 0;//默认选择首页
                _device = Model.CGetInfo.GetDeviceInfo(_deviceIndex);
                //20100108
                if (_device == null)
                {
                    MessageBox.Show("您选择的设备在数据库没有记录!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;

                }
                if ((_device.DeviceIndex >= 11001 && _device.DeviceIndex <= 11003)
                    || (_device.DeviceIndex >= 12001 && _device.DeviceIndex <= 12003)
                    || (_device.DeviceIndex >= 13001 && _device.DeviceIndex <= 13007) 
                    || (_device.DeviceIndex >= 15001 && _device.DeviceIndex <= 15010) 
                    || (_device.DeviceIndex >= 17001 && _device.DeviceIndex <= 17002))//变位机和探伤机显示交互信号页面
                {
                    //tabPage_signal.Parent = tabControl_Device;
                    tabPage_signal.Parent = null;
                }
                else
                {
                    tabPage_signal.Parent=null;
                }
                if (_device.DeviceKind == 1)
                {
                    button1.Visible = true;
                }
                else
                {
                    button1.Visible = false;
                }
          
                #region 显示逻辑有无按钮
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT F_LaneGateDeviceIndex from T_Base_Lane_Gate where F_LaneGateDeviceIndex=").Append(_deviceIndex);
                DataView dvlane = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                if (dvlane.Count > 0)
                {
                    sql = new StringBuilder();
                    sql.Append("SELECT F_DeviceIndex from T_Base_PLC_Ask   where F_DeviceIndex=").Append(_deviceIndex);
                    dvlane = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    if (dvlane.Count > 0)
                    {
                        this.btCancelDoubleFork.Visible = true;
                    }
                    else
                    {
                        this.btCancelDoubleFork.Visible = false;
                    }
                    this.btLogicHaveGoods.Visible = true;
                    this.btLogicNoneGoods.Visible = true;
                    textBox1.Visible = true;
                    //add for CATL YB2 申请补码 增加站台 2220173-时代吉利
                    if (_device.DeviceIndex == 11703 || _device.DeviceIndex == 11706 || _device.DeviceIndex == 11709 || _device.DeviceIndex == 11411 || _device.DeviceIndex == 11414 || _device.DeviceIndex == 11418
                         || _device.DeviceIndex == 31703 || _device.DeviceIndex == 31706 || _device.DeviceIndex == 31709 || _device.DeviceIndex == 31411 || _device.DeviceIndex == 31414 || _device.DeviceIndex == 31417)
                    {
                        gb12002.Visible = true;

                    }
                    else
                    {
                        gb12002.Visible = false;
                    }

                    if (_device.DeviceKind == 2 || _device.DeviceKind == 9)
                    {
                        dv = dbo.ExceSQL("select F_DeviceIndex from T_Base_Device  where F_HaveGoods >0 and F_DeviceIndex =" + this._deviceIndex).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            lblGoods.Text = "逻辑有物";
                        }
                        else
                        {
                            lblGoods.Text = "逻辑无物";
                        }
                    }
                    else
                    {
                        lblGoods.Text = "-";
                    }


                }
                else
                {
                    this.btCancelDoubleFork.Visible = false;
                    this.btLogicHaveGoods.Visible = false;
                    this.btLogicNoneGoods.Visible = false;
                    textBox1.Visible = false;
                    //add for CATL ly5 2220173-时代吉利
                    if (_device.DeviceIndex == 12431 || _device.DeviceIndex == 12442 || _device.DeviceIndex == 12516 || _device.DeviceIndex == 12640 || _device.DeviceIndex == 12701 || _device.DeviceIndex == 22438
                        || _device.DeviceIndex == 32433 || _device.DeviceIndex == 32443 || _device.DeviceIndex == 32516 || _device.DeviceIndex == 32640 || _device.DeviceIndex == 32701 || _device.DeviceIndex == 42443)
                    //if (true)
                    {
                        gb12002.Visible = true;
                    }
                    else
                    {
                        gb12002.Visible = false;
                    }
                    lblGoods.Text = "-";
                    //button1.Visible = false;
                } 
                #endregion

                //设备索引
                this.lblDeviceIndex.Value = _device.DeviceIndex;
                //设备名称
                this.lblDeviceName.Text = _device.DeviceName;
                //设备指令索引
                this.lblTaskIndex.Text = _device.TaskNo.ToString();

                #region 显示目标位置和托盘条码
                sqlstr = string.Format("SELECT F_MonitorIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2,F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6, F_TxtParam FROM T_Monitor_Task WHERE (F_MonitorIndex ={0})", _device.TaskNo);
                dv = dbo.ExceSQL(sqlstr).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    int order = Convert.ToInt32(dv[0]["F_DeviceCommandIndex"]);
                    string info = string.Empty;

                    int Z = Convert.ToInt32(dv[0]["F_NumParam1"]);
                    int X = Convert.ToInt32(dv[0]["F_NumParam2"]);
                    int Y = Convert.ToInt32(dv[0]["F_NumParam3"]);
                    int Z1 = Convert.ToInt32(dv[0]["F_NumParam4"]);
                    int X1 = Convert.ToInt32(dv[0]["F_NumParam5"]);
                    int Y1 = Convert.ToInt32(dv[0]["F_NumParam6"]);

                    if (_device.DeviceKind == 1)
                    {
                        switch (order)
                        {
                            case 1://回原点
                                info = string.Format("回原点:01列01层");
                                break;
                            case 2://将取
                                info = string.Format("将取:{0:D2}排-{1:D2}列-{2:D2}层", Z, X, Y);
                                break;
                            case 3://将送
                                info = string.Format("将送:{0:D2}排-{1:D2}列-{2:D2}层", Z1, X1, Y1);
                                break;
                            case 4://取
                                info = string.Format("取货:{0:D2}排-{1:D2}列-{2:D2}层", Z, X, Y);

                                break;
                            case 5://送
                                info = string.Format("送货:{0:D2}排-{1:D2}列-{2:D2}层", Z1, X1, Y1);
                                break;
                            case 6:
                                info = string.Format("取送货:{0:D2}排-{1:D2}列-{2:D2}层 到 {3:D2}排-{4:D2}列-{5:D2}层", Z, X, Y, Z1, X1, Y1);
                                break;

                        }


                    }
                    else
                        if (_device.DeviceKind == 2)
                        {
                            switch (order)
                            {
                                case 1://正转
                                    info = string.Format("正转");
                                    break;
                                case 2://反转
                                    info = string.Format("反转");
                                    break;
                                case 6://送出
                                    info = string.Format("{0}送出到{1}", Z,Z1);
                                    break;
                                case 8://上升
                                    info = string.Format("上升");
                                    break;
                                case 9://下降
                                    info = string.Format("下降");
                                    break;


                            }
                        }
                        else
                            if (_device.DeviceKind == 4)
                            {
                                switch (order)
                                {
                                    case 7://运动
                                        info = string.Format("运动:{0}", Z);
                                        break;
                                    case 8://取
                                        info = string.Format("取货:{0}", Z);
                                        break;
                                    case 9://送
                                        info = string.Format("送货:{0}", Z);
                                        break;


                                }
                            }


                    this.lblArrowLocation.Text = info;

                    this.lblBarcode.Text = dv[0]["F_TxtParam"].ToString();

                }
                else
                {
                    //20140109
                    if (_device.DeviceKind == 2)
                    {
                        // add for CATL 动作完成状态
                        if (_device.RunState == 5 && _device.SplitByte_0 == 1)
                        {
                            //条码号
                            object bcode = dbo.GetSingle(string.Format("SELECT F_PALLETBARCODE FROM T_Base_Device WHERE (F_DeviceIndex = {0})", _device.DeviceIndex));
                            if (bcode != null)
                            {
                                this.lblBarcode.Text = bcode.ToString();
                            }
                            else
                            {
                                this.lblBarcode.Text = "";

                            }

                        }
                        else
                        {
                            //条码号
                            this.lblBarcode.Text = "";
                        }
                    }
                    else
                    {
                        //条码号
                        this.lblBarcode.Text = "";
                    }
                    //目标位置

                    this.lblArrowLocation.Text = "";
                } 
                #endregion
                if (_device.DeviceKind == 31)
                {

                    this.lblTaskIndex.Text = GetMonitorTaskIndexFromBarCode(new StringBuilder(_device.Barcode)).ToString();
                    this.lblBarcode.Text = _device.Barcode;
                    this.lblBarcode.Enabled = true;
                }
                else
                {
                    this.lblBarcode.Enabled = true;
                }

                //设备状态
                switch (_device.RunState)
                {
                    case 0:
                        lblDeviceStatus.Text = "空闲";
                        break;
                    case 1:
                        lblDeviceStatus.Text = "运行";
                        break;
                    case 2:
                        lblDeviceStatus.Text = "故障";
                        break;
                    case 3:
                        lblDeviceStatus.Text = "离线";
                        break;
                    case 4:
                        lblDeviceStatus.Text = "停用";
                        break;
                    case 5://20090920
                        lblDeviceStatus.Text = "动作完成";
                        break;
                }


                if (_device.ErrorCode >= 30)
                {
                    Model.MError err = Model.CGetInfo.GetErrorInfo(Convert.ToInt32(_device.DeviceKind.ToString() + _device.ErrorCode.ToString()));
                    if (err != null)
                    {
                        this.lblErrorName.Text = err.ErrorName + ",代码:" + _device.ErrorCode.ToString();
                    }
                    else
                    {
                        this.lblErrorName.Text = _device.ErrorCode.ToString();
                    }
                }
                else
                {
                    this.lblErrorName.Text = "";
                }
                if (_device.DeviceKind == 1)
                {
                    int xcoor = _device.XCoor;
                    if (_device.DeviceIndex==23035)//20180609
                    {
                        xcoor -= 1488;
                    }
                    if (_device.DeviceIndex == 31006)//20181023
                    {
                        xcoor -= 3800;
                    }
                    this.lblNowX.Text = string.Format("{0:D2}列", _device.XCoor);
                    this.lblNowY.Text = string.Format("{0:D2}层",   _device.YCoor);
                    //this.lblNowY.Text = _device.YCoor.ToString();
                }
                else
                if (_device.DeviceKind == 4)
                {
                    this.lblNowX.Text = string.Format("{0}mm",  _device.XCoor);
                    this.lblNowY.Text = "";
                }
                else
                if (_device.DeviceKind == 2|| _device.DeviceKind == 9)
                {
                    if (_device.XCoor > 0)
                    {
                        this.lblNowX.Text = string.Format("{0}列", _device.XCoor);
                    }
                    else
                    {
                        this.lblNowX.Text = "";
                    }
                    this.lblNowY.Text = "";
                }
                else
                {
                    this.lblNowX.Text = "";
                    this.lblNowY.Text = "";
                }
                cB_Empty.Enabled = false;
                label_staus.Text = "设备状态:";
                
                label_X.Text = "当前X坐标:";
                label_Y.Text = "当前Y坐标:";
                if (_device.DeviceKind == 7)
                {
                    dv = dbo.ExceSQL(string.Format("SELECT F_BarCode ,F_Time,F_TempFirstProject,F_FirstProject  FROM T_Base_PLC_Ask WHERE (F_DeviceIndex = {0})", _device.DeviceIndex)).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        this.lblBarcode.Text = dv[0]["F_BarCode"].ToString();

                        label_staus.Text = "扫描时间:";
                        //add for catl YB2
                        label_X.Text = "数量";//高度
                        label_Y.Text = "备用";//重量

                        lblDeviceStatus.Text = dv[0]["F_Time"].ToString();
                        lblNowX.Text = dv[0]["F_FirstProject"].ToString();


                        lblNowY.Text = dv[0]["F_TempFirstProject"].ToString();


                        cB_Empty.Checked = (dv[0]["F_TempFirstProject"].ToString()=="0"?false:true);//20170215
                        if (_device.DeviceIndex.ToString().Length<=5)
                        {
                        //this.lblBarcode.Enabled = true;
                        cB_Empty.Enabled = true;
                        }
                    }
                    lblErrorName.Text = "";
                    //lblDeviceStatus.Text = "";
                }
                if (_device.DeviceKind == 35)//出库申请
                {
                    dv = dbo.ExceSQL(string.Format("SELECT F_BarCode ,F_Time,F_TempFirstProject,F_FirstProject  FROM T_Base_PLC_Ask WHERE (F_DeviceIndex = {0})", _device.DeviceIndex)).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        this.lblBarcode.Text = dv[0]["F_BarCode"].ToString();
                        lblDeviceStatus.Text = dv[0]["F_Time"].ToString();
                        label_X.Text = "申请出库数量:";


                        lblNowX.Text = dv[0]["F_FirstProject"].ToString();
                        //cB_Empty.Checked = (dv[0]["F_TempFirstProject"].ToString() == "0" ? false : true);//20170215
                        if (_device.DeviceIndex.ToString().Length <= 5)
                        {
                            //this.lblBarcode.Enabled = true;
                            cB_Empty.Enabled = true;
                        }
                    }
                    lblErrorName.Text = "";
                    //lblDeviceStatus.Text = "";
                }
                //是否逻辑有物
                //if (_device.DeviceKind == 2 || _device.DeviceKind == 9)
                //{
                //    dv = dbo.ExceSQL("select F_DeviceIndex from T_Base_Device  where F_HaveGoods >0 and F_DeviceIndex =" + this._deviceIndex).Tables[0].DefaultView;
                //    if (dv.Count > 0)
                //    {
                //        lblGoods.Text = "逻辑有物";
                //    }
                //    else
                //    {
                //        lblGoods.Text = "逻辑无物";
                //    }
                //    }
                //else
                //{
                //    lblGoods.Text = "";
                //}
                //20120417被预约的调度任务
                dv = dbo.ExceSQL(string.Format("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskKindIndex = 4 and F_MonitorIndex={0})", Convert.ToInt32(this.lblTaskIndex.Text))).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    lblManTask.Text = "手工任务";
                }
                else
                {
                    dv = dbo.ExceSQL(string.Format("select F_ManTaskReserve from T_Base_Device  where F_ManTaskReserve>0 and F_DeviceIndex ={0}", this._deviceIndex)).Tables[0].DefaultView;
                    if (dv.Count > 0)
                    {
                        int mankind = Convert.ToInt32(dv[0][0].ToString().Substring(0, 1));
                        int fid = Convert.ToInt32(dv[0][0].ToString().Substring(1));
                        lblManTask.Text = fid.ToString();
                        dv = dbo.ExceSQL(string.Format("SELECT F_TxtParam FROM T_Monitor_Task WHERE (F_ManageTaskKindIndex = {0} and F_ManageTaskIndex={1})", mankind, fid)).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            lblBarcode.Text = dv[0][0].ToString(); ;
                        }

                    }
                    else
                    {
                        lblManTask.Text = "";
                    }
                }
                dv = dbo.ExceSQL(string.Format("SELECT F_DeviceIndex,F_PalletBarcodeLength,F_BarCode FROM T_Base_PLC_Ask WHERE (F_Askkind = 4 OR F_Askkind = 13 OR F_Askkind = 14) AND (F_BindingDevice = {0})", this._deviceIndex)).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    // add for CATL YB2
                    //gbBarcode.Visible = true;
                   // CStaticClass.WcfControl.BeginGetReadedAndReportedBarcode(this._deviceIndex, new AsyncCallback(CallbackGetReadedAndReportedBarcode), null);
                }
                else
                {
                    gbBarcode.Visible = false;
                }
                //if (_device.Dbw1Address!=-1)
                if (_device.DeviceKind == 1)
                {
                    btnReset.Visible = true;
                    btnAnswer.Visible = true;
                    button1.Visible = true;
                }
                else
                {
                    btnReset.Visible = false;
                    btnAnswer.Visible = true;
                    button1.Visible = false;
                    
                }

                //获取光电信息
                #region 显示光电信息
                labelValue0.Text = _device.SplitByte_0.ToString();
                labelValue1.Text = _device.SplitByte_1.ToString();
                labelValue2.Text = _device.SplitByte_2.ToString();
                labelValue3.Text = _device.SplitByte_3.ToString();
                labelValue4.Text = _device.SplitByte_4.ToString();
                labelValue5.Text = _device.SplitByte_5.ToString();
                labelValue6.Text = _device.SplitByte_6.ToString();
                labelValue7.Text = _device.SplitByte_7.ToString();
                if (_device.DeviceIndex == 11001 ||_device.DeviceIndex == 11003)//两端的堆垛机
                {
                    labelsignal0.Text = "有货(.0):";
                    labelsignal1.Text = "防碰近探测信号(.1):";
                    labelsignal2.Text = "防碰远探测信号(.2):";
                    labelsignal3.Text = "自动停车(.3):";
                    labelsignal4.Text = "";
                    labelsignal5.Text = "";
                    labelsignal6.Text = "";
                    labelsignal7.Text = "";

                    labelValue4.Text = "";
                    labelValue5.Text = "";
                    labelValue6.Text = "";
                    labelValue7.Text = "";

                }
                if (_device.DeviceIndex == 11002)//中间的堆垛机
                {
                    labelsignal0.Text = "有货(.0):";
                    labelsignal1.Text = "防碰近探测信号(1#堆垛机)(.1):";
                    labelsignal2.Text = "防碰远探测信号(1#堆垛机)(.2):";
                    labelsignal3.Text = "防碰近探测信号(3#堆垛机)(.3):";
                    labelsignal4.Text = "防碰远探测信号(3#堆垛机)(.4):";
                    labelsignal5.Text = "自动停车(.5):";
                    labelsignal6.Text = "";
                    labelsignal7.Text = "";

                    labelValue6.Text = "";
                    labelValue7.Text = "";

                }
                if (_device.DeviceIndex >= 12001 && _device.DeviceIndex <= 12003)//变位机
                {
                    if (_device.DeviceIndex==12002)
                    {
                        labelsignal0.Text = "输送机货物检测(.0):";
                        labelsignal1.Text = "输送机高位检测(.1):";
                        labelsignal2.Text = "输送机低位检测(.2):";

                    }
                    else
                    {
                        labelsignal0.Text = "输送机货物检测(.0):";
                        labelsignal1.Text = "输送机允许穿梭车取货(.1):";
                        labelsignal2.Text = "输送机允许穿梭车送货(.2):";
                    }
                    labelsignal3.Text = "";
                    labelsignal4.Text = "";
                    labelsignal5.Text = "";
                    labelsignal6.Text = "";
                    labelsignal7.Text = "";

                    labelValue3.Text = "";
                    labelValue4.Text = "";
                    labelValue5.Text = "";
                    labelValue6.Text = "";
                    labelValue7.Text = "";
                }
                if (_device.DeviceIndex >= 13001 && _device.DeviceIndex <= 13007)//台车
                {

                    labelsignal0.Text = "台车货物检测(.0):";
                    labelsignal1.Text = "台车里位检测(.1):";
                    labelsignal2.Text = "台车外位检测(.2):";


                    labelsignal3.Text = "";
                    labelsignal4.Text = "";
                    labelsignal5.Text = "";
                    labelsignal6.Text = "";
                    labelsignal7.Text = "";

                    labelValue3.Text = "";
                    labelValue4.Text = "";
                    labelValue5.Text = "";
                    labelValue6.Text = "";
                    labelValue7.Text = "";
                }
                if (_device.DeviceIndex >= 15001 && _device.DeviceIndex <= 15009)//变位机
                {
                    labelsignal0.Text = "变位机允许堆垛机动作(.0):";
                    labelsignal1.Text = "变位机货物检测(.1):";
                    labelsignal2.Text = "变位机开始动作(.2):";
                    labelsignal3.Text = "堆垛机允许变位机动作(.3):";
                    labelsignal4.Text = "堆垛机放货完成(.4):";
                    labelsignal5.Text = "堆垛机取货完成(.5):";
                    labelsignal6.Text = "";
                    labelsignal7.Text = "";

                    labelValue6.Text = "";
                    labelValue7.Text = "";
                   
                }
                if (_device.DeviceIndex == 15010)//变位机
                {
                    labelsignal0.Text = "变位机允许输送机动作(.0):";
                    labelsignal1.Text = "变位机开始动作(.1):";
                    labelsignal2.Text = "变位机复位取货完成(.2):";
                    labelsignal3.Text = "变位机申请取货(.3):";
                    labelsignal4.Text = "变位机申请放货(.4):";
                    labelsignal5.Text = "输送机允许变位机动作(.5):";
                    labelsignal6.Text = "输送机放货完成(.6):";
                    labelsignal7.Text = "输送机取货完成(.7):";
                }
                if  (_device.DeviceIndex >= 17001 && _device.DeviceIndex <= 17002)//探伤机显示交互信号页面
                {
                    labelsignal0.Text = "探伤机允许堆垛机动作(.0):";
                    labelsignal1.Text = "探伤机高位检测(.1):";
                    labelsignal2.Text = "探伤机低位检测(.2):";
                    labelsignal3.Text = "探伤机申请取空托盘(.3):";
                    labelsignal4.Text = "变位机申请取实托盘(.4):";
                    labelsignal5.Text = "堆垛机允许探伤机动作(.5):";
                    labelsignal6.Text = "堆垛机放货完成(.6):";
                    labelsignal7.Text = "堆垛机取货完成(.7):";
                }



                #endregion

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void CallbackGetReadedAndReportedBarcode(IAsyncResult ar)
        {
            string errortext,ReadedBarcode, ReportedBarcode;
            try
            {
                bool rt=CStaticClass.WcfControl.EndGetReadedAndReportedBarcode(out ReadedBarcode, out ReportedBarcode, out errortext, ar);
                if (rt ==true)
                {
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        lblReadedBarcode.Text = ReadedBarcode;
                        lblReportedBarcode.Text = ReportedBarcode;
                    }));
                }
                else
                {
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        lblReadedBarcode.Text ="";
                        lblReportedBarcode.Text = "";
                    }));
                    if (errortext != "")
                    {
                        MessageBox.Show(errortext, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch(Exception ex)//20170115
            {
                throw ex;
            }
        }
        #endregion

        #region 关闭窗体
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要把设备:" + this._deviceIndex + "设成逻辑无物吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            try
            {
                CStaticClass.WcfControl.BeginSetLogicHaveGoods(false, this._deviceIndex, new AsyncCallback(SetLogicHaveGoodsCallBack), null);
                btnRefresh_Click(sender, e);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("您确认要把设备:" + this._deviceIndex + "设成逻辑有物吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            try
            {
                CStaticClass.WcfControl.BeginSetLogicHaveGoods(true, this._deviceIndex, new AsyncCallback(SetLogicHaveGoodsCallBack), null);
                btnRefresh_Click(sender, e);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要向设备:" + this._deviceIndex + "发送【完成应答】吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            string errtext = string.Empty;
            try
            {
                CStaticClass.WcfControl.BeginSendDeviceReset(this._deviceIndex, 2, new AsyncCallback(SendDeviceResetCallBack), 2);
                CommonClassLib.CCarryConvert.WriteDarkCasket("设备状态描述", "完成应答", "设备:" + this._deviceIndex.ToString(), ",应答");
            }
            catch(Exception ex)
            {
                      
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        void SetLogicHaveGoodsCallBack(IAsyncResult ar)
        {
            string errtext = string.Empty;

            try
            {
                if (CStaticClass.WcfControl.EndSetLogicHaveGoods(out errtext, ar) == false)
                {
                    MessageBox.Show(errtext, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        void SendDeviceResetCallBack(IAsyncResult ar)
        {
            string errtext = string.Empty;


            try
            {
                if (CStaticClass.WcfControl.EndSendDeviceReset(out errtext, ar) == false)
                {
                    MessageBox.Show(errtext, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (ar.AsyncState != null)
                    {
                        //if ((((int)ar.AsyncState) == 4)&&(_device.DeviceKind==4))
                        //{
                        //    CStaticClass.WcfControl.BeginSendDeviceReset(_deviceIndex, 2, new AsyncCallback(SendDeviceResetCallBack), null);
                        //}
                        if (((int)ar.AsyncState) == 2 || ((int)ar.AsyncState) == 4)
                        {
                            CStaticClass.WcfControl.BeginSendDeviceReset(_deviceIndex, 0, new AsyncCallback(SendDeviceResetCallBack), null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

               throw ex;
            }
        }
        private void lblDeviceIndex_ValueChanged(object sender, EventArgs e)
        {
            _deviceIndex =int.Parse( lblDeviceIndex.Value.ToString());
        }

        private void FrmDeviceStatus_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要向设备:" + this._deviceIndex + "发送【设备复位】吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            string errtext = string.Empty;
            try
            {
                CStaticClass.WcfControl.BeginSendDeviceReset(this._deviceIndex, 4, new AsyncCallback(SendDeviceResetCallBack), 4);
                CommonClassLib.CCarryConvert.WriteDarkCasket("设备状态描述", "故障复位", "设备:" + this._deviceIndex.ToString(), ",复位");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        int GetMonitorTaskIndexFromBarCode(StringBuilder BarCode)
        {//20110216
            string bc = BarCode.ToString();

            DataView dv = dbo.ExceSQL(string.Format("SELECT T_Monitor_Task.F_MonitorIndex FROM T_Base_Device,T_Monitor_Task WHERE  T_Base_Device.F_DeviceIndex = T_Monitor_Task.F_DeviceIndex and ((T_Base_Device.F_DeviceKindIndex = 13) OR (T_Base_Device.F_DeviceKindIndex = 31)) and (F_TxtParam = '{0}') and F_Status>0 order by F_MonitorIndex asc", bc)).Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return Convert.ToInt32(dv[0]["F_MonitorIndex"]);
            }
            else
            {
                return -1;
            }

        }

       
        void WriteDBDataCallback(IAsyncResult ar)
        {
            string errtext;

            try
            {
                if (CStaticClass.WcfControl.EndWriteDBData(out errtext, ar) == false)
                {
                    MessageBox.Show("操作提示", errtext, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        private void lblBarcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == '\r')
                {
                    if (MessageBox.Show("您确认要修改设备【" + this._deviceIndex + "】记录的【条码】吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                    {
                        return;
                    }
                    //int type = 1;//1的asc码默认为实物托盘,有货
                    //if(cB_Empty.Checked==false)//空托盘
                    //{
                    //    type = 0;//0的asc码
                    //}
                    //string barcode = string.Format("{0}{1}",type, lblBarcode.Text);
                    CStaticClass.WcfControl.BeginWriteDBData(_deviceIndex, "DB2", lblBarcode.Text, new AsyncCallback(WriteDBDataCallback), null);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btCancelDoubleFork_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要取消【" + this._deviceIndex + "】的双叉关联吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            StringBuilder sql = new StringBuilder() ;
            sql.Append("update T_Base_PLC_Ask set F_TaskIndex=0,F_Remark='' where F_DeviceIndex=").Append(_deviceIndex);
            dbo.ExecuteSql(sql.ToString());

        }

        private void btnSolutions_Click(object sender, EventArgs e)
        {//20151120
            try
            {
                if (_device != null)
                {
                    if (_device.ErrorCode >= 30)
                    {
                        FrmDeviceErrorSolutions.FormInstance.Show();
                        FrmDeviceErrorSolutions.FormInstance.comboBox1.SelectedValue = _device.DeviceKind;
                        FrmDeviceErrorSolutions.FormInstance.textBox1.Text = _device.ErrorCode.ToString();
                        FrmDeviceErrorSolutions.FormInstance.button1_Click(sender, e);

                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //add for CATL YB2
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要向设备:" + this._deviceIndex + "发送【解锁命令】吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            string errtext = string.Empty;
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("update T_Base_Device set F_ManTaskReserve=0,F_LockedState= 0  where F_DeviceIndex=").Append(_deviceIndex);
                dbo.ExecuteSql(sql.ToString());

                ////每次禁用后重新更新设备状态 内存的
                //Model.MDevice temp_device = null;
                //temp_device = Model.CGetInfo.GetDeviceInfo(_deviceIndex);
                //Model.CGetInfo.SetModifyPathDeviceInfo(temp_device);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btLayer2_Click(object sender, EventArgs e)
        {
            if (tbBarcode.Text.Trim().Length < 5) //|| tbBarcode.Text.Trim().Length != 5 || tbBarcode.Text.Trim().Length != 11
            {
                MessageBox.Show("请输入正确条码!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            StringBuilder dtime = new StringBuilder(DateTime.Now.ToString("u"));
            // 给管理申请时候更新 申请时间
            dtime.Remove(dtime.Length - 1, 1);
            int apptype = 0;
            DataView dvapp = dbo.ExceSQL(string.Format("select F_MANAGEASKKIND from T_BASE_PLC_ASK where F_BINDINGDEVICE={0}", this._deviceIndex)).Tables[0].DefaultView;
            if (dvapp.Count > 0)
            {
                apptype = Convert.ToInt32(dvapp[0]["F_MANAGEASKKIND"]);
            }
            string applyStation = "CW";
            string warehouse = "3";
            string barcode = tbBarcode.Text.ToString();
            if (tbBarcode.Text.Trim().Length > 5 && !tbBarcode.Text.Trim().ToString().Contains('|')) //|| tbBarcode.Text.Trim().Length != 5 || tbBarcode.Text.Trim().Length != 11
            {
                MessageBox.Show("请输入正确条码!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string[] barcodeSinnal = barcode.Split('|');
            for (int i = 0; i < barcodeSinnal.Length; i++)
            {
                DataView dvbcApply = new DataView();
                dvbcApply = dboMan.ExceSQL(string.Format("SELECT DEVICE_CODE,STOCK_BARCODE FROM IO_CONTROL_APPLY WHERE APPLY_TASK_STATUS<2  and (STOCK_BARCODE = '{0}')",
                                        barcodeSinnal[i].ToString())).Tables[0].DefaultView;
                if (dvbcApply.Count > 0)
                {
                    MessageBox.Show("当前条码已存在申请!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                DataView dvbc = new DataView();
                dvbc = dboMan.ExceSQL(string.Format("select CONTROL_ID from IO_CONTROL  where CONTROL_STATUS<>999 and  STOCK_BARCODE = '{0}'",
                barcodeSinnal[i].ToString())).Tables[0].DefaultView;//管理系统有可能还没处理已经报告完成的任务
                if (dvbc.Count > 0)
                {
                    MessageBox.Show("当前条码已经生成控制任务!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                DataView dvbcc = new DataView();
                dvbcc = dbo.ExceSQL(string.Format("select fid from T_Manage_Task  where FPALLETBARCODE = '{0}'",
                                    barcodeSinnal[i].ToString())).Tables[0].DefaultView;
                if (dvbcc.Count > 0)
                {
                    MessageBox.Show("当前条码存在调度任务!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            //2220173-时代吉利 For JL 阴极库
            if (_device.DeviceIndex == 12431  || _device.DeviceIndex == 12442 || _device.DeviceIndex == 12516 || _device.DeviceIndex == 12640 || _device.DeviceIndex == 12701 || _device.DeviceIndex == 22438)
            {
                applyStation = _device.DeviceIndex.ToString();
                warehouse = "2";
            }
            //2220173-时代吉利 For JL 阳极库
            else if (_device.DeviceIndex == 32433 || _device.DeviceIndex == 32443 || _device.DeviceIndex == 32516 || _device.DeviceIndex == 32640 || _device.DeviceIndex == 32701 || _device.DeviceIndex == 42443)
            {
                applyStation = _device.DeviceIndex.ToString();
                warehouse = "1";
            }
            //2220173-时代吉利
            if (this._deviceIndex == 11709)
            {
                warehouse = "2";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "11709";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "11709|11708";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 11706)
            {
                warehouse = "2";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "11706";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "11706|11705";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 11703)
            {
                warehouse = "2";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "11703";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "11703|11702";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 11411)
            {
                warehouse = "2";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "11411";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "11411|11410";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 11418)
            {
                warehouse = "2";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "11418";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "11418|11417";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 11414)
            {
                warehouse = "2";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "11414";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "11414|11413";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 31411)
            {
                warehouse = "1";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "31411";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "31411|31410";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 31414)
            {
                warehouse = "1";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "31414";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "31414|31413";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 31417)
            {
                warehouse = "1";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "31417";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "31417|31416";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 31703)
            {
                warehouse = "1";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "31703";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "31703|31702";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 31706)
            {
                warehouse = "1";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "31706";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "31706|31705";
                }
            }//2220173-时代吉利
            else if (this._deviceIndex == 31709)
            {
                warehouse = "1";
                // 判断条码长度如果为5默认为一个站台申请
                if (tbBarcode.Text.Trim().Length == 5)
                {
                    applyStation = "31709";
                }
                else if (tbBarcode.Text.Trim().Length == 11 && tbBarcode.Text.Trim().ToString().Contains('|'))
                {
                    applyStation = "31709|31708";
                }
            }
            if (applyStation ==  "CW")
            {
                 MessageBox.Show("请输入正确条码!严谨工作态度!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 return;
            }
            // 补码时候需要判断当前站台存不存在申请 两种申请 双码申请 单码申请、  防止补码站台选错
            string[] stationCodeApply = applyStation.Split('|');
            DataView dvbcApplyStations = new DataView();
            DataView dvbcControlStation = new DataView();
            // 条码申请
            if (stationCodeApply.Length >0)
            {
                for (int k = 0; k < stationCodeApply.Length; k++)
                {
                    // 判断补码位置是否有货  无货 补码失败
                      Model.MDevice devinfoGarDQ;
                   //判断终点设备是否有物
                   //ceshi
                    //devinfoGarDQ = Model.CGetInfo.GetDeviceInfo( Convert.ToInt32(stationCodeApply[0]));
                    //if (devinfoGarDQ.SplitByte_0 !=1)
                    //{
                    //    MessageBox.Show("请选对站台补码!补码站台无货!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //    return;
                    //}
                    dvbcApplyStations = dboMan.ExceSQL(string.Format("SELECT DEVICE_CODE  FROM IO_CONTROL_APPLY WHERE APPLY_TASK_STATUS<2 and DEVICE_CODE='{0}'", stationCodeApply[0].ToString())).Tables[0].DefaultView;
                    if (dvbcApplyStations.Count > 0)
                    {
                        MessageBox.Show("当前站台存在申请!请确认是否选错站台!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    // 判断起点是当前申请站台 终点是货位的控制任务是否存在
                    dvbcControlStation = dboMan.ExceSQL(string.Format("select CONTROL_ID from IO_CONTROL  where CONTROL_STATUS<>999 and  START_DEVICE_CODE = '{0}' and length(END_DEVICE_CODE)=8 ",
               stationCodeApply[0].ToString())).Tables[0].DefaultView;
                    if (dvbcControlStation.Count > 0)
                    {
                        MessageBox.Show("当前站台存在控制任务!请确认是否选错站台!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            // 需要判断是否存在对应的双条码申请
           
            if (stationCodeApply.Length >1)
            {
                dvbcApplyStations = dboMan.ExceSQL(string.Format("SELECT DEVICE_CODE  FROM IO_CONTROL_APPLY WHERE APPLY_TASK_STATUS<2 and DEVICE_CODE='{0}'",
                                            applyStation)).Tables[0].DefaultView;
                if (dvbcApplyStations.Count > 0)
                {
                    MessageBox.Show("当前站台存在申请!请确认是否选错站台!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

            }
            else
            {//2220173-时代吉利
                string judgeStation = "GarbELE";
                switch (this._deviceIndex )
                {
                    case 11703:
                        judgeStation = "11703|11702";
                        break;
                    case 11706:
                        judgeStation = "11706|11705";
                        break;
                    case 11709:
                        judgeStation = "11709|11708";
                        break;
                    case 11411:
                        judgeStation = "11411|11410";
                        break;
                    case 11414:
                        judgeStation = "11414|11413";
                        break;
                    case 11418:
                        judgeStation = "11418|11417";
                        break;
                    case 31703:
                        judgeStation = "31703|31702";
                        break;
                    case 31706:
                        judgeStation = "31706|31705";
                        break;
                    case 31709:
                        judgeStation = "31709|31708";
                        break;
                    case 31411:
                        judgeStation = "31411|31410";
                        break;
                    case 31414:
                        judgeStation = "31414|31413";
                        break;
                    case 31417:
                        judgeStation = "31417|11416";
                        break;
                }
                dvbcApplyStations = dboMan.ExceSQL(string.Format("SELECT DEVICE_CODE  FROM IO_CONTROL_APPLY WHERE APPLY_TASK_STATUS<2 and DEVICE_CODE='{0}'",
                                            judgeStation)).Tables[0].DefaultView;
                if (dvbcApplyStations.Count > 0)
                {
                    MessageBox.Show("当前站台存在申请!请确认是否选错站台!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

            }
        
            try
            {
                int row = 0;
                string remake = "补码";
                object[] ob = new object[7] { apptype, applyStation, barcode, 0, dtime, remake, warehouse };
                row = dboMan.ExecuteSql(string.Format("INSERT INTO IO_CONTROL_APPLY ( CONTROL_APPLY_ID,CONTROL_APPLY_TYPE,DEVICE_CODE, STOCK_BARCODE, APPLY_TASK_STATUS, CREATE_TIME,CONTROL_APPLY_REMARK,WAREHOUSE_CODE)VALUES ( IO_CONTROL_APPLY_SEQ.Nextval,{0},'{1}','{2}',{3},'{4}','{5}','{6}')",ob));
               
                string sql = "";
                sql = "UPDATE T_BASE_PLC_ASK SET F_BARCODE ='" + barcode + "' WHERE F_BINDINGDEVICE =" + this._deviceIndex ;
                dbo.ExceSQL(sql);
                if (row == 1)
                {
                    object[] ob1 = new object[7] { apptype, applyStation, barcode, 0, dtime, remake, warehouse.ToString() };
                    dbo.ExecuteSql(string.Format("INSERT INTO IO_CONTROL_APPLY_WCS (CONTROL_APPLY_ID,CONTROL_APPLY_TYPE,DEVICE_CODE, STOCK_BARCODE, APPLY_TASK_STATUS, CREATE_TIME,CONTROL_APPLY_PARA01,WAREHOUSE_CODE)VALUES (IO_CONTROL_APPLY_WCS_SEQ.Nextval,{0},'{1}','{2}',{3},'{4}','{5}','{6}')",
                   ob1));
                    CommonClassLib.CCarryConvert.WriteDarkCasket("ClientBacorde", "客户端补码", barcode.ToString().ToUpper(), "补码站台和账户:" + this._deviceIndex.ToString() + "|" + CStaticClass.UserID);

                    MessageBox.Show("补码成功!", "操作提示:");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "补码异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ButtonClear_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要向设备:" + this._deviceIndex + "发送【清除条码】吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            StringBuilder sql = new StringBuilder();
            sql.Append("update T_Base_PLC_Ask set F_BARCODE='' where F_DeviceIndex=").Append(this._deviceIndex);
            dbo.ExecuteSql(sql.ToString());
        }
    }
}