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.AVTD
{
    public class DataHandle : CommandBase<GPSSession, BinaryRequestInfo>
    {
        public override string Name
        {
            get
            {
                return "40-56-54-44";//@VTD 0x40, 0x56, 0x54, 0x44
            }
        }
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public override void ExecuteCommand(GPSSession session, BinaryRequestInfo requestInfo)
        {
            try
            {
                //1
                //@VTD,0501,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];
                if (IMEIFilter.GetIMEIContains(ref model))
                {
                    if (string.IsNullOrEmpty(model.product_model))
                    {
                        session.Send("@KPT#");
                        DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_FW_FAIL);
                        return;
                    }
                    session.IMEI = model.imei;
                    session.product_model = model.product_model;
                }
                string version = OTA.version(string.IsNullOrEmpty(session.product_model) ? "AVL" : session.product_model);
                //string type = OTA.type(model.product_model);
                string pn = OTA.packagenumber(string.IsNullOrEmpty(session.product_model) ? "AVL" : session.product_model);
                if (_v != version)
                {
                    //@VER,0503,708#
                    string down = string.Format("@VER,{0},{1}#", version, pn);
                    session.Send(down);
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_FW_RUN);
                }
                else
                {
                    session.Send("@KPT#");
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_FW_FAIL);
                }

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