using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Threading;
using ICommLayer;
using DBFactory;
using CommonClassLib;
namespace SimensCP5611
{
    /// <summary>
    /// 通过西门子的CP5611卡实现Profibus总线方式的通讯接口派生类
    /// 发送设备命令类
    /// 创建者：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)
        {
            try
            {
                int[] _word = new int[10];
                byte[] _byte = new byte[20];
                byte[] _temp = new byte[2];
                _word[0] = 1;
                _word[1] = Order;
                _word[2] = TaskIndex;
                _word[3] = StartX;
                _word[4] = StartY;
                _word[5] = StartZ;
                _word[6] = EndX;
                _word[7] = EndY;
                _word[8] = EndZ;
                //////////////////////////

                _word[9] = _word[0];
                for (int i = 1; i <= 8; i++)
                {
                    _word[9] = _word[9] ^ _word[i];
                }
                for (int h = 0; h <= 9; h++)
                {
                    _temp = CCarryConvert.GetByteFromInt16(Convert.ToUInt16(_word[h]));
                    _byte[(2 * h)] = _temp[0];
                    _byte[(2 * h + 1)] = _temp[1];
                }
                byte[] _RRword = new byte[12];
                int[] _Rword = new int[6];
                DataSet ds = dbo.ExceSQL("SELECT  F_DeviceIndex, F_DBWAddress,  F_DBWSendLength, F_DBWGetLength FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex);
                DataView dv = ds.Tables[0].DefaultView;
                if (ds.Tables[0].DefaultView.Count > 0)
                {

                    bool rr= CSimensCP5611.DBwrite_CP5611(1, 1, Convert.ToInt16(dv[0]["F_DBWAddress"]), Convert.ToInt16(dv[0]["F_DBWSendLength"])*2, _byte);
                    if (rr == false)
                    {
                        _commLayerError = "SimensCP5611.CSendDeviceOrder" + ":SendDeviceOrder---" + CSimensCP5611.ErrorsText;
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                    //Thread.Sleep(300);
                    //_RRword = CSimensCP5611.DB_Read_CP5611(1, 2, Convert.ToInt16(dv[0]["F_DBWAddress"]), Convert.ToInt16(dv[0]["F_DBWGetLength"]));
                    //if (_RRword == null)
                    //{
                    //    _commLayerError = "SimensCP5611.CSendDeviceOrder" + ":SendDeviceOrder---" + CSimensCP5611.ErrorsText;
                    //    return false;
                    //}
                }
                else
                {
                    return false;
                }
                //ds.Dispose();
                //for (int g = 0; g <= 5; g++)
                //{
                //    _Rword[g] = _RRword[(2 * g)] * 256 + _RRword[(2 * g + 1)];
                //}
                //int _RXor = 0;
                //if (_Rword != null)
                //{
                //    _RXor = _Rword[0];
                //    for (int i = 1; i <= 4; i++)
                //    {
                //        _RXor = _RXor ^ _Rword[i];
                //    }
                //    //_Rword[0] == 2 && 
                //    if (_Rword[2] == TaskIndex && _RXor == _Rword[5])//(正确反馈TaskIndex的消息应答)
                //    {
                //        return true;
                //    }
                //    else
                //        return false;
                //}
                //else
                //{
                //    return false;
                //}
            }
            catch (Exception ex)
            {
                _commLayerError = "SimensCP5611.CSendDeviceOrder" + ":SendDeviceOrder---" + ex.Message;
                return false;
            
            }

        
        }
        /// <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)
        {
            try
            {
                byte[] _byte = new byte[8];
                byte[] _temp = new byte[2];
                int[] _word = new int[4];
                _word[0] = 1;
                _word[1] = Order;
                _word[2] = TaskIndex;
                _word[3] = _word[0];
                for (int i = 1; i <= 2; i++)
                {
                    _word[3] = _word[3] ^ _word[i];
                }

                for (int h = 0; h <= 3; h++)
                {
                    _temp = CCarryConvert.GetByteFromInt16(Convert.ToUInt16(_word[h]));
                    _byte[(2 * h)] = _temp[0];
                    _byte[(2 * h + 1)] = _temp[1];
                }
                byte[] _RRword = new byte[8];
                int[] _Rword = new int[4];
                DataSet ds = dbo.ExceSQL("SELECT  F_DeviceIndex, F_DBWAddress,  F_DBWSendLength, F_DBWGetLength FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex);
                DataView dv = ds.Tables[0].DefaultView;
                if (ds.Tables[0].DefaultView.Count > 0)
                {
                     
                    bool rr= CSimensCP5611.DBwrite_CP5611(1, 1, Convert.ToInt16(dv[0]["F_DBWAddress"]), Convert.ToInt16(dv[0]["F_DBWSendLength"])*2, _byte);
                    if (rr == false)
                    {
                        _commLayerError = "SimensCP5611.CSendDeviceOrder" + ":SendDeviceOrder---" + CSimensCP5611.ErrorsText;
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                    //Thread.Sleep(300);
                    //_RRword = CSimensCP5611.DB_Read_CP5611(1, 2, Convert.ToInt16(dv[0]["F_DBWAddress"]), Convert.ToInt16(dv[0]["F_DBWGetLength"]));
                    //if (_RRword == null)
                    //{
                    //    _commLayerError = "SimensCP5611.CSendDeviceOrder" + ":SendDeviceOrder---" + CSimensCP5611.ErrorsText;
                    //    return false;
                    //}
                }
                else
                {
                    return false;
                }
                //ds.Dispose();
                
                //for (int g = 0; g <= 3; g++)
                //{
                //    _Rword[g] = _RRword[(2 * g)] * 256 + _RRword[(2 * g + 1)];
                //}
                //int _RXor = 0;
                //if (_Rword != null)
                //{
                //    _RXor = _Rword[0];
                //    for (int i = 1; i <= 2; i++)
                //    {
                //        _RXor = _RXor ^ _Rword[i];
                //    }
                //    if (_Rword[0] == 2 && _Rword[2] == TaskIndex && _RXor == _Rword[3])//PLC读出数据正确(正确反馈TaskIndex的消息应答)
                //    {
                //        return true;
                //    }
                //    else
                //        return false;
                //}
                //else
                //{
                //    return false;
                //}
            }
            catch (Exception ex)
            {
                _commLayerError = "SimensCP5611.CSendDeviceOrder" + ":SendDeviceOrder---" + ex.Message;
                return false;
            }
        }
    }
}
