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;
using TZONE.GPSSocketServer.Common;

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

        public override void ExecuteCommand(GPSSession session, BinaryRequestInfo requestInfo)
        {
            try
            {
                //4
                //@VEE,A571,123456789012345#
                string msg = Encoding.Default.GetString(requestInfo.Body, 0, requestInfo.Body.Length).Trim('\0');
                string[] s = msg.Split(',');
                tempdata model = new tempdata();
                model.imei = s[2].Substring(0, s[2].Length - 1);
                string _v = s[1];
                session.IMEI = model.imei;
                string version = OTA.versionBin(string.IsNullOrEmpty(session.product_model) ? "AVL" : session.product_model);
                string pn = OTA.packagenumberBin(string.IsNullOrEmpty(session.product_model) ? "AVL" : session.product_model);
                if (_v != version)
                {
                    //@VEP,A572,32#
                    string down = string.Format("@VEP,{0},{1}#", version, pn);
                    session.Send(down);
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_BIN_RUN);
                }
                else
                {
                    session.Send("@KPT#");
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_BIN_FAIL);
                }
            }
            catch (System.Exception ex)
            {
                log.Error(ex.Message);
            }
        }
    }
}
