Common.cs 8.5 KB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Agv
{


    public class Common
    {
        //包开始字节
        public static byte[] headPack = new byte[] { 0xAB, 0xBA };
        public static int headSize = 6;//包头识别符2+包头长度4
        /// <summary>
        /// 节点信息解析方式:默认解析单个
        /// </summary>
        public static bool EnDecodeSingleNode = true;
        public static bool CheckHeadChar(byte[] head, out int starIdx)
        {
            starIdx = -1;
            for (int i = 0; i < head.Length - headPack.Length+1; i++)
            {
                int j = 0;
                for (j = 0; j < headPack.Length; j++)
                {
                    if (!head[i + j].Equals(headPack[j]))
                        break;
                }
                if (j == headPack.Length)
                {
                    starIdx = i;
                    return true;
                }

            }
            return false;
        }
        public static string HexBuff(byte[] buff)
        {
            string s = "";
            if (buff == null) return s;

            for (int i = 0; i < buff.Length; i++)
                s += buff[i].ToString("X2") + " ";
            return s;
        }
        public static string HexBuffWithoutSpace(byte[] buff)
        {
            string s = "";
            if (buff == null) return s;

            for (int i = 0; i < buff.Length; i++)
                s += buff[i].ToString("X2");
            return s;
        }
        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="node">单个节点</param>
        /// <returns></returns>
        public static byte[] Encode(Node node)
        {
            try
            {
                string json = JsonHelper.SerializeObject(node);
                log.debug(string.Format("Encode[{0}]", json));
                byte[] body = Encoding.UTF8.GetBytes(json);
                byte[] bodySize = IntToByteArray(body.Length);
                byte[] buff = headPack.Concat(bodySize).Concat(body).ToArray();
                return buff;
            }
            catch (Exception ex)
            {
                log.error("Encode Error", ex);
                return null;
            }
        }

        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="nodes">多个节点</param>
        /// <returns></returns>
        public static byte[] Encode(List<Node> nodes)
        {
            try
            {
                string json = JsonHelper.SerializeObject(nodes);
                log.debug(string.Format("Encode[{0}]", json));
                byte[] body = Encoding.UTF8.GetBytes(json);
                byte[] bodySize = IntToByteArray(body.Length);
                byte[] buff = headPack.Concat(bodySize).Concat(body).ToArray();
                log.debug(string.Format("headPack:{0}", Common.HexBuff(Common.headPack)));
                log.debug(string.Format("bodysize:{0}[{1}]", Common.HexBuff(bodySize), body.Length));
                log.debug(string.Format("body:{0}", Common.HexBuff(body)));
                return buff;
            }
            catch (Exception ex)
            {
                log.error("Encode Error", ex);
                return null;
            }
        }

        /// <summary>
        /// 解码单个节点
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        public static Node Decode(byte[] buff)
        {
            try
            {
                string json = System.Text.Encoding.UTF8.GetString(buff);
                log.debug(string.Format("Decode[{0}]", json));
                Node node = JsonHelper.DeserializeJsonToObject<Node>(json);

                return node;
            }
            catch (Exception ex)
            {
                log.error("Decode Error", ex);
                return null;
            }
        }
        /// <summary>
        /// 解码多个节点
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        public static List<Node> DecodeNodes(byte[] buff)
        {
            try
            {
                string json = System.Text.Encoding.UTF8.GetString(buff);
                log.debug(string.Format("DecodeNodes[{0}]", json));
                List<Node> node = JsonHelper.DeserializeJsonToList<Node>(json);

                return node;
            }
            catch (Exception ex)
            {
                log.error("Decode Error", ex);
                return null;
            }
        }

        //将数字转换成字节数组
        //由数字创建字节数组
        public static byte[] IntToByteArray(Int32 src)
        {
            //创建内存流MemoryStream,stream作为存放 二进制数据 的缓存
            using (MemoryStream stream = new MemoryStream())
            {
                //创建一个BinaryWriter来写二进制数据到stream
                using (BinaryWriter write = new BinaryWriter(stream))
                {
                    write.Write(src);//将十进制数字src写到stream中,
                    return stream.ToArray();//将写到stream中的二进制数据转为字节数组

                }
            }
        }
    }

    /// <summary>
    /// 客户端的动作
    /// </summary>
    public enum ClientAction : byte
    {
        /// <summary>
        /// 没有动作
        /// </summary>
        None,
        /// <summary>
        /// 完成(服务器发送)
        /// </summary>
        CloseDoor,
        /// <summary>
        /// 可以进入料架
        /// </summary>
        MayEnter,
        /// <summary>
        /// 可以流出料架
        /// </summary>
        MayLeave,
        /// <summary>
        /// 需要进入料架
        /// </summary>
        NeedEnter,
        /// <summary>
        /// 需要流出料架
        /// </summary>
        NeedLeave,
        /// <summary>
        /// 完成进入料架
        /// </summary>
        FinishEnter,
        /// <summary>
        /// 完成流出料架
        /// </summary>
        FinishLeave,
        /// <summary>
        /// 到达包装料仓门口(服务器发送)
        /// </summary>
        Arrive,
        /// <summary>
        /// 小车准备好(服务器发送)
        /// </summary>
        Ready,
        /// <summary>
        /// 只能入料不能出料(仅包装料仓)
        /// </summary>
        EnterShelf,
        /// <summary>
        /// 不允许
        /// </summary>
        MayNot,
        /// <summary>
        /// 需要进入流出料架
        /// </summary>
        NeedEnterLeave,
        /// <summary>
        /// 可以停靠
        /// </summary>
        MayDock,
        /// <summary>
        /// 不可以进入料架
        /// </summary>
        MayNotEnter,
        /// <summary>
        /// 不可以流出料架
        /// </summary>
        MayNotLeave,
        /// <summary>
        /// 不可以停靠
        /// </summary>
        MayNotDock,
        /// <summary>
        /// 小车准备好送出料架(服务器发送)
        /// </summary>
        ReadyEnter,
        /// <summary>
        /// 小车准备好进入料架(服务器发送)
        /// </summary>
        ReadyLeave,

        /// <summary>
        /// 完成(服务器发送)
        /// </summary>
        Complete,

    }

    /// <summary>
    /// 客户端的优先级
    /// </summary>
    public enum ClientLevel : byte
    {
        /// <summary>
        /// 低
        /// </summary>
        Low,
        /// <summary>
        /// 中等
        /// </summary>
        Middle,
        /// <summary>
        /// 高
        /// </summary>
        High,

        None,
    }

    /// <summary>
    /// 客户端的料架
    /// </summary>
    public enum ClientShelf
    {
        /// <summary>
        /// 没有架子
        /// </summary>
        None,
        /// <summary>
        /// 空架子
        /// </summary>
        Empty,
        /// <summary>
        /// 满料架子
        /// </summary>
        Full
    }

    /// <summary>
    /// 类型
    /// </summary>
    public enum ClientType
    {
        /// <summary>
        /// 无
        /// </summary>
        None,
        /// <summary>
        /// 紧急料
        /// </summary>
        Urgent,
        /// <summary>
        /// 包装料
        /// </summary>
        Pack,
        /// <summary>
        /// 分盘料
        /// </summary>
        Cut
    }

}