using HighWayIot.Log4net;
using HighWayIot.Repository.domain;
using HighWayIot.Repository.service.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.TouchSocket
{
    public class BufferAnalysis
    {
        private static LogHelper logHelper = LogHelper.Instance;

        /// <summary>
        /// 心跳报文分析
        /// </summary>
        /// <param name="bytes"></param>
        public static void HeartbeatSocket(byte[] bytes)
        {
            if (int.TryParse(Encoding.ASCII.GetString(bytes, 4, 4), out int deviceno))
            {
                BaseHeartbeatServiceImpl sql = new BaseHeartbeatServiceImpl();
                if (sql.UpdateHeartbeatInfo(deviceno) == 0)
                {
                    RFIDHeartbeat heartbeat = new RFIDHeartbeat()
                    {
                        DeviceNo = deviceno,
                        BeatTime = DateTime.Now,
                    };
                    sql.AddHeartbeatInfo(heartbeat);
                }
            }
            else
            {
                logHelper.Error("心跳报文编号数值转换出现错误！");
            }
        }

        /// <summary>
        /// RFID发送设备状态
        /// </summary>
        /// <param name="bytes"></param>
        public static void RFIDStatusSocket(byte[] bytes)
        {
            if (int.TryParse(Encoding.ASCII.GetString(bytes, 4, 4), out int deviceno) &&
                int.TryParse(Encoding.ASCII.GetString(bytes, 9, 1), out int state))
            {
                BaseStateServiceImpl sql = new BaseStateServiceImpl();
                RFIDState rFIDState = new RFIDState()
                {
                    DeviceNo = deviceno,
                    DeviceState = state == 1 ? true : false,
                    LogTime = DateTime.Now,
                };
                sql.AddStateInfo(rFIDState);
            }
            else
            {
                logHelper.Error("设备状态报文编号数值转换出现错误！");
            }
        }

        /// <summary>
        /// RFID发送条码
        /// </summary>
        /// <param name="bytes"></param>
        public static void RFIDCodeSocket(byte[] bytes)
        {
            string readKind = Encoding.ASCII.GetString(bytes, 4, 2);
            if (int.TryParse(Encoding.ASCII.GetString(bytes, 7, 4), out int deviceno)) 
            {
                RFIDContent rFIDContent = new RFIDContent()
                {
                    DeviceNo = deviceno,
                    ReadKind = readKind,
                    LogTime = DateTime.Now,
                };
                string content = "";
                if (readKind == "NB" || readKind == "GR")
                {
                    content = Encoding.ASCII.GetString(bytes, 12, 16);
                }
                else if (readKind == "MR")
                {
                    content = Encoding.ASCII.GetString(bytes, 12, bytes.Length - 12 - 2); // 减去条码内容之前和之后内容的长度
                }
                rFIDContent.Content = content;
                BaseContentServiceImpl sql = new BaseContentServiceImpl();
                sql.AddContentInfo(rFIDContent);
            }
            else
            {
                logHelper.Error("条码发送报文编号数值转换出现错误！");
            }
        }

        /// <summary>
        /// 将一个数组拆成另外一个数组
        /// </summary>
        /// <param name="originbyte">原始数组，被拆分的数组</param>
        /// <param name="oringinstartindex">从原始数组第几个元素开始</param>
        /// <param name="destbytelength">目标数组的长度</param>
        /// <param name="destbytestartindex">目标数组开始的元素序号，默认为0</param>
        /// <returns></returns>
        public static byte[] SplitByteArray(byte[] originbyte, int oringinstartindex, int destbytelength, int destbytestartindex = 0)
        {
            byte[] result = new byte[destbytelength];
            System.Array.Copy(originbyte, oringinstartindex, result, destbytestartindex, destbytelength);
            return result;
        }
    }
}
