using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Threading;
using Microsoft.VisualBasic;
using DBFactory;
using ICommLayer;

namespace SimensSerialPort
{
    /// <summary>
    /// 通过西门子无线电台的自由口方式的通讯接口派生类
    /// 发送设备命令类
    /// 创建者：Richard.Liu
    /// </summary>
    public class CSendDeviceOrder: ISendDeviceOrder
    {
        string _commLayerError;

        public string CommLayerError
        {
            get { return _commLayerError; }
            set { _commLayerError = value; }
        }
        DBOperator dbo = new DBOperator();
        public CSendDeviceOrder()
        {
            dbo.Open();
        }
        //~CSendDeviceOrder()
        //{
        //    dbo.Close();
        //}
        /// <summary>
        /// 发送堆垛机命令
        /// </summary>
        /// <param name="MessageIndex">消息编号</param>
        /// <param name="TaskIndex">任务编号</param>
        /// <param name="Order">堆垛机命令字</param>
        ///单叉单进伸：
        ///1-复位 
        ///2-将取 
        ///3-将送
        ///4-取货
        ///5-放货
        ///6-取放货
        ///双叉单进伸：
        ///1-复位 
        ///2-将取 
        ///3-将送
        ///4-左叉取货
        ///5-左叉放货
        ///6-左叉取放货
        ///14-右叉取货
        ///15-右叉放货
        ///16-右叉取放货
        ///24-双叉取货
        ///25-双叉放货
        ///26-双叉取放货
        ///单叉双进伸：
        ///1-复位 
        ///2-将取 
        ///3-将送
        ///4-近位取货
        ///5-近位放货
        ///6-近位取放货
        ///14-远位取货
        ///15-远位放货
        ///16-远位取放货
        /// <param name="DeviceIndex">设备编号</param>
        /// <param name="StartX">起始x坐标，排-沿轨道方向</param>
        /// <param name="StartY">起始y坐标，层-高度方向</param>
        /// <param name="StartZ">起始z坐标，列-面向堆垛机操作面板，1-左侧，2-右侧</param>
        /// <param name="EndX">目标x坐标，排-沿轨道方向</param>
        /// <param name="EndY">目标y坐标，层-高度方向</param>
        /// <param name="EndZ">目标z坐标，列-面向堆垛机操作面板，1-左侧，2-右侧</param>
        /// <returns>发送堆垛机命令是否成功</returns>
        public bool SendDeviceOrder(int MessageIndex, int TaskIndex, int Order, int DeviceIndex, int StartX, int StartY, int StartZ, int EndX, int EndY, int EndZ)
        {
            CSimensSerialPort ssp; DataView dv;
            byte[] _Sdata = new byte[16];
            try
            {
                
                byte WXor = 0;
                _Sdata[0] = 16;
                _Sdata[1] = Convert.ToByte(MessageIndex);
                _Sdata[2] = Convert.ToByte(TaskIndex >> 8);//高八位
                _Sdata[3] = Convert.ToByte(TaskIndex & 255);//低八位
                _Sdata[4] = Convert.ToByte(Order);
                _Sdata[5] = Convert.ToByte(DeviceIndex);
                //_Sdata[6] = 0;
                //_Sdata[7] = 0;
                //_Sdata[8] = Convert.ToByte(StartX);
                //_Sdata[9] = Convert.ToByte(StartY);
                //_Sdata[10] = Convert.ToByte(StartZ);
                //_Sdata[11] = Convert.ToByte(EndX);
                _Sdata[6] = Convert.ToByte(StartX);
                _Sdata[7] = Convert.ToByte(StartY);
                _Sdata[8] = Convert.ToByte(StartZ);
                _Sdata[9] = Convert.ToByte(EndX);
                _Sdata[10] = Convert.ToByte(EndY);
                _Sdata[11] = Convert.ToByte(EndZ);
                _Sdata[12] = 0;
                _Sdata[13] = 0;
                _Sdata[14] = 0;
                WXor = _Sdata[0];
                for (int i = 1; i <= 14; i++)
                {
                    WXor = Convert.ToByte(Convert.ToInt16(WXor) ^ Convert.ToInt16(_Sdata[i]));
                }
                _Sdata[15] = WXor;
                //根据DeviceIndex,得到串口号和初始设置

                dv = dbo.ExceSQL("SELECT  F_DeviceIndex, F_CommSettings,  F_SerialPort   FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex).Tables[0].DefaultView;
                
                if (dv.Count > 0)
                {

                    ssp = new CSimensSerialPort(Convert.ToInt16(dv[0]["F_SerialPort"]), Convert.ToString(dv[0]["F_CommSettings"]));

                }
                else
                {
                    return false;
                }
               
                if (ssp.InitCom() == true)
                {
                    //CommonClassLib.CCarryConvert.WriteDarkCasket(this.ToString(), "WriteCom", _Sdata);
                    if (ssp.WriteCom(_Sdata) == true)
                    {
                        ssp.CloseCom();
                        return true;

                        //Thread.Sleep(300);
                        //byte[] _GetData = ssp.ReadCom(16);
                        ////CommonClassLib.CCarryConvert.WriteDarkCasket(this.ToString(), "ReadCom", _GetData);
                        //if (_GetData != null)
                        //{
                        //    if ((_GetData[0] == 8) && (_GetData[1] == 240) && (_GetData[2] == MessageIndex) &&
                        //        ((_GetData[3] * Math.Pow(2, 8) + _GetData[4]) == TaskIndex) &&
                        //        (_GetData[5] == DeviceIndex) && (_GetData[6] == 170))
                        //    {
                        //        byte GXor = 0;
                        //        GXor = _GetData[0];
                        //        for (int i = 1; i <= 6; i++)
                        //        {
                        //            GXor = Convert.ToByte(Convert.ToInt16(GXor) ^ Convert.ToInt16(_GetData[i]));
                        //        }
                        //        if (GXor == _GetData[7])
                        //        {
                        //            ssp.CloseCom();
                        //            return true;
                        //        }
                        //        else
                        //        {
                        //            ssp.CloseCom();
                        //            return false;
                        //        }
                        //    }
                        //    else
                        //    {
                        //        ssp.CloseCom();
                        //        return false;
                        //    }
                        //}
                        //else
                        //{
                        //    ssp.CloseCom();
                        //    _commLayerError = this.ToString() + ":SendDeviceOrder---" + ssp.ComError;
                        //    return false;
                        //}
                    }
                    else
                    {
                        ssp.CloseCom();
                        _commLayerError = this.ToString() + ":SendDeviceOrder---" + ssp.ComError;
                        return false;
                    }
                }

                else
                {
                    ssp.CloseCom();
                    _commLayerError = this.ToString() + ":SendDeviceOrder---" + ssp.ComError;
                    return false;
                }
            }
            catch (Exception ex)
            {
                _commLayerError = this.ToString() + ":SendDeviceOrder---" + ex.Message;
                return false;
            }
            finally
            {
                ssp = null;
                dv = null;
                _Sdata = null;
            }
        }
        /// <summary>
        /// 发送堆垛机以外的其它设备命令
        /// </summary>
        /// <param name="MessageIndex">消息编号</param>
        /// <param name="TaskIndex">任务编号</param>
        /// <param name="Order">命令字</param>
        /// 穿梭车命令字： 
        ///1-复位 
        ///2-左接货
        ///3-左送货
        ///4-右接货
        ///5-右送货
        ///6-停止
        ///7-运动到1#位置
        ///8-运动到2#位置
        ///     …
        ///输送机、台车命令字：
        ///1-入库（朝向库）
        ///        2-出库（背向库）
        ///        3-送货
        ///        4-停止
        /// <param name="DeviceIndex">设备编号</param>
        /// <returns>发送其它设备命令是否成功</returns>
        public bool SendDeviceOrder(int MessageIndex, int TaskIndex, int Order, int DeviceIndex, int ArrowDeviceIndex)
        {
            byte[] _Sdata = new byte[16]; DataView dv; CSimensSerialPort ssp;
            try
            {
                byte WXor = 0;
                _Sdata[0] = 16;
                _Sdata[1] = Convert.ToByte(MessageIndex);
                _Sdata[2] = Convert.ToByte(TaskIndex >> 8);//高八位
                _Sdata[3] = Convert.ToByte(TaskIndex & 255);//低八位
                _Sdata[4] = Convert.ToByte(Order);
                _Sdata[5] = Convert.ToByte(DeviceIndex);
                _Sdata[6] = 0;
                _Sdata[7] = 0;
                _Sdata[8] = 0;
                _Sdata[9] = 0;
                _Sdata[10] = 0;
                _Sdata[11] = 0;
                _Sdata[12] = 0;
                _Sdata[13] = 0;
                _Sdata[14] = 0;
                WXor = _Sdata[0];
                for (int i = 1; i <= 14; i++)
                {
                    WXor = Convert.ToByte(Convert.ToInt16(WXor) ^ Convert.ToInt16(_Sdata[i]));
                }
                _Sdata[15] = WXor;
                //根据DeviceIndex,得到串口号和初始设置
                dv = dbo.ExceSQL("SELECT  F_DeviceIndex, F_CommSettings,  F_SerialPort   FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex).Tables[0].DefaultView;

                if (dv.Count > 0)
                {

                    ssp = new CSimensSerialPort(Convert.ToInt16(dv[0]["F_SerialPort"]), Convert.ToString(dv[0]["F_CommSettings"]));

                }
                else
                {
                    return false;
                }

                if (ssp.InitCom() == true)
                {
                    //CommonClassLib.CCarryConvert.WriteDarkCasket(this.ToString(), "WriteCom", _Sdata);
                    if (ssp.WriteCom(_Sdata) == true)
                    {
                        ssp.CloseCom();
                        return true;

                        //Thread.Sleep(300);
                        //byte[] _GetData = ssp.ReadCom(16);
                        ////CommonClassLib.CCarryConvert.WriteDarkCasket(this.ToString(), "ReadCom", _GetData);
                        //if (_GetData != null)
                        //{
                        //    if ((_GetData[0] == 8) && (_GetData[1] == 240) && (_GetData[2] == MessageIndex) &&
                        //        ((_GetData[3] * Math.Pow(2, 8) + _GetData[4]) == TaskIndex) &&
                        //        (_GetData[5] == DeviceIndex) && (_GetData[6] == 170))
                        //    {
                        //        byte GXor = 0;
                        //        GXor = _GetData[0];
                        //        for (int i = 1; i <= 6; i++)
                        //        {
                        //            GXor = Convert.ToByte(Convert.ToInt16(GXor) ^ Convert.ToInt16(_GetData[i]));
                        //        }
                        //        if (GXor == _GetData[7])
                        //        {
                        //            ssp.CloseCom();
                        //            return true;
                        //        }
                        //        else
                        //        {
                        //            ssp.CloseCom();
                        //            return false;
                        //        }
                        //    }
                        //    else
                        //    {
                        //        ssp.CloseCom();
                        //        return false;
                        //    }
                        //}
                        //else
                        //{
                        //    ssp.CloseCom();
                        //    return false;
                        //}
                    }
                    else
                    {
                        ssp.CloseCom();
                        return false;
                    }
                }

                else
                {
                    ssp.CloseCom();
                    return false;
                }
            }
            catch (Exception ex)
            {
                _commLayerError = this.ToString() + ":SendDeviceOrder---" + ex.Message;
                return false;
            }
            finally
            {
                ssp = null;
                dv = null;
                _Sdata = null;
            }
        

        }
        public bool SendDeviceOrder(int MessageIndex, int TaskIndex, int Order, int DeviceIndex, int StartX, int StartY, int StartZ, int EndX, int EndY, int EndZ, string DoubleFork)
        {
            return true;

        }
        public bool SendDeviceOrder(int DeviceIndex, string TobaccoCode, bool HaveFirstProjectCode, bool IfSmallTobacco, int Count)
        {
            return true;
        }
        public bool WriteDBData(StringBuilder [] ItemNames, StringBuilder [] ItemValues,int deviceindex)
        {
            return true;
        }
    }
}
