MsgRecevie.cs 6.63 KB
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Common;
using XingYe_ACS.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace XingYe_ACS.Business
{
    public class MsgRecevie
    {
        /// <summary>
        /// 指令解析
        /// </summary>
        /// <param name="socketOpt">客户端对象</param>
        public static void TcpTranslate(SocketOpt socketOpt)
        {
            try
            {
                ClientMsg clientMsg = null;
                byte[] dataRecv = socketOpt.recData;
                clientMsg = AgvHeartRecevie(dataRecv);//无错误时msgClientErr=0;
                #region 注释功能码
                //switch (dataRecv[1])
                //{
                //    case 0:
                //        clientMsg = AgvHeartRecevie(dataRecv);//无错误时msgClientErr=0;
                //        break;
                //    default:
                //        App.ExFile.MessageLog("MsgException", string.Format("Tcp指令功能码无定义:FuncCode[{0}]", dataRecv[1].ToString()));
                //        socketOpt.sendData = GetSendData.GetRepeatData();
                //        break;
                //}
                #endregion
                if (clientMsg.clientErr != 0)
                {
                    if (clientMsg.clientErr != EnumMsg.ErrMsg.不作处理)
                    {
                        App.ExFile.MessageLog("MsgException", "客户端信息出错:" + clientMsg.clientErr.ToString()
                            + "=名称=" + clientMsg.strAddress + "=任务号=" + clientMsg.intTaskNo + "=动作状态=" + clientMsg.actState);
                    }
                    return;
                }
                else if (App.SingleControlList.Exists(a => a.agvNo == clientMsg.strAddress))
                {
                    List<ClientMotion> listMotion = new List<ClientMotion>();
                    SingleControl singleControl = App.SingleControlList.Find(a => a.agvNo == clientMsg.strAddress);
                    socketOpt.sendData = GetSendData.GetNewActionData(singleControl.agvAction, null);

                    if (singleControl.agvAction == 200)
                    {
                        if (clientMsg.intState < (int)EnumMsg.State.手动无码) return;
                        else
                        {
                            singleControl.agvAction = 0;
                        }
                    }
                    else if (singleControl.agvAction == 209 && clientMsg.intState < (int)EnumMsg.State.离线)
                    {
                        App.SingleControlList.RemoveAll(a => a.agvNo == clientMsg.strAddress);
                    }
                }
                else
                {
                    Config_ClientControl clientControl = App.ClientWork.Find(a => a.strClientIp == ((IPEndPoint)socketOpt.clientSocket.RemoteEndPoint).Address.ToString());
                    if (clientControl.strAgvNo == "" || clientControl.strAgvNo == null)
                    {
                        clientControl.strAgvNo = clientMsg.strAddress;
                        //将小车IP地址更新到agv表中
                        UpdateManage.UpdateAgvIP(clientControl);
                    }
                    MsgManage.TcpManage(clientMsg, socketOpt);
                }
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("TcpTranslate", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 获取Agv心跳信息
        /// </summary>
        /// <param name="dataRecv">心跳数据报文</param>
        /// <returns>Agv心跳信息</returns>
        public static ClientMsg AgvHeartRecevie(byte[] dataRecv)
        {
            try
            {
                //客户端发送数据对象
                ClientMsg clientMsg = new ClientMsg();
                clientMsg.intDataLength = dataRecv[0];

                clientMsg.strAddress = Encoding.ASCII.GetString(dataRecv, 1, 10).Replace("\0", "");
                clientMsg.msgAgv = App.AgvList.FirstOrDefault(a => a.strAgvNo == clientMsg.strAddress);

                if (clientMsg.msgAgv == null)
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.小车号不存在;
                    return clientMsg;
                }
                if (!clientMsg.msgAgv.isEnable)
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                    return clientMsg;
                }
                clientMsg.msgAgv.agvHeart = DateTime.Now;
                clientMsg.intCheckCode = ComnMethod.checkCode(dataRecv, clientMsg.intDataLength + 12);  //校验
                if (clientMsg.intCheckCode != dataRecv[30])
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.校验位错误;
                    clientMsg.msgAgv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, clientMsg.clientErr.ToString());
                    return clientMsg;
                }
                clientMsg.msgAgv.strStopReason = "";
                clientMsg.strBarcode = BitConverter.ToUInt32(dataRecv, 11).ToString();
                clientMsg.intVoltage = dataRecv[15];
                #region 记录AGV电量显示
                if (ComnMethod.DiffTime(DateTime.Parse(clientMsg.msgAgv.logTime.ToString())) > 10)
                {
                    clientMsg.msgAgv.logTime = DateTime.Now;
                    App.ExFile.AgvVoltageLog("agvVoltage" + clientMsg.msgAgv.strAgvNo, clientMsg.intVoltage.ToString());
                }
                #endregion
                clientMsg.intState = dataRecv[16];
                clientMsg.agvDirection = (EnumMsg.OriType)dataRecv[17];
                clientMsg.dialDirection = (EnumMsg.OriType)dataRecv[18];
                clientMsg.intHeightState = dataRecv[19];
                clientMsg.ErrorList = new int[] { dataRecv[20], dataRecv[21], dataRecv[22] };
                clientMsg.intRunDistance = BitConverter.ToUInt16(dataRecv, 23);
                string strTaskNo = BitConverter.ToUInt32(dataRecv, 25).ToString();
                if (string.IsNullOrEmpty(strTaskNo))
                    clientMsg.intTaskNo = 0;
                else
                    clientMsg.intTaskNo = int.Parse(strTaskNo);
                clientMsg.actState = (EnumMsg.ActState)dataRecv[29];
               // clientMsg.isCarry = dataRecv[31];
                return clientMsg;
            }
            catch (Exception Ex)
            {
                throw new Exception("AgvHeartRecevie:" + Ex.Message.Replace(Environment.NewLine, ""));
            }
        }
    }
}