using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using log4net;
using System.Reflection;
using TZONE.Model;

namespace TZONE.GPSSocketServer.Command.AREP
{
    public class DataHandle : CommandBase<GPSSession, BinaryRequestInfo>
    {
        public override string Name
        {
            get
            {
                return "40-52-45-50";//@REP 0x40, 0x52, 0x45, 0x50,
            }
        }
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public override void ExecuteCommand(GPSSession session, BinaryRequestInfo requestInfo)
        {
            try
            {
                //5
                //@REP,A572,008#
                string msg = Encoding.Default.GetString(requestInfo.Body, 0, requestInfo.Body.Length).Trim('\0');
                string[] s = msg.Split(',');
                string number = s[2].Substring(0, s[2].Length - 1);
                string data = OTA.PackageItemDataBin(string.IsNullOrEmpty(session.product_model) ? "AVL" : session.product_model, number);
                byte[] b = TZONE.GPSSocketServer.Common.CRC.GetStringToToHexByte(data);
                if (b.Length > 0)
                    session.Send(b, 0, b.Length);
                else
                    session.Send(string.Format("@ERR,{0}#", number));

            }
            catch (System.Exception ex)
            {
                log.Error(ex.Message);
            }
        }
    }
}
