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.DT
{
    public class DataHandle : CommandBase<GPSSession, BinaryRequestInfo>
    {
        public override string Name
        {
            get
            {
                return "24-54";//$T
            }
        }

        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public override void ExecuteCommand(GPSSession session, BinaryRequestInfo requestInfo)
        {
            try
            {
                string dt = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.FFF");
                
                //245400D63836393434343030353438343835327C32303133303532303130333932377C32323332333233384E313134303338303433457C267C10000054100000631000006710000061100000441000007510000047100000501000 00451000005510000079100000601000007110000076100000111000004810000072100000491000007410000046100000681000005910000058100000561000006410000077100000161000006910000062100000571000006610 000052100000700D000002ED0001020D00000110000073100000787C394333450D0A

                //imei
                string imei = Encoding.Default.GetString(requestInfo.Body, 4, 15).Trim('\0');

                tempdata model = new tempdata();
                model.server_time = dt;
                model.imei = imei;
                model.data_body = requestInfo.Body;
                if (IMEIFilter.GetIMEIContains(ref model))
                {
                    if (string.IsNullOrEmpty(model.product_model))
                    {
                        session.Send("$$ NO MODEL");
                        session.Close();
                        return;
                    }
                    session.IMEI = model.imei;
                    model.EventTempData += model_EventTempData;
                    model.ExecuteEvent();
                }
                else
                {
                    session.Send("$$ NO IMEI");
                    session.Close();
                    return;
                }
                
                DownData(session, session.IMEI);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                log.Error("ERROR:" + Encoding.Default.GetString(requestInfo.Body, 0, requestInfo.Body.Length).Trim('\0') + " ---- " + BitConverter.ToString(requestInfo.Body, 0, requestInfo.Body.Length).Replace("-", ""));
#endif
                log.Error(ex.Message);
            }
        }


        public void model_EventTempData(tempdata _temp)
        {
            string typename = "TZONE." + _temp.product_model + ".DT.AnalyzeHandle";
            String assemblyName = "TZONE." + _temp.product_model + @".DT.dll";
            Assembly ass = Assembly.LoadFrom(assemblyName);
            Type type = ass.GetType(typename);//必须使用名称空间+类名称
            System.Reflection.MethodInfo method = type.GetMethod("AnalyzeData");//方法的名称
            object obj = ass.CreateInstance(typename);//必须使用名称空间+类名称;
            method.Invoke(obj, new object[] { _temp }); //实例方法的调用

        }

        private void DownData(GPSSession session, string imei)
        {
            if (GlobalVariables.GLOBAL_ON_LINE_DOWN.Contains(imei))
            {
                if (session.TrySend(GlobalVariables.GLOBAL_ON_LINE_DOWN[imei].ToString()))
                {
                    GlobalVariables.GLOBAL_ON_LINE_DOWN.Remove(imei);
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_OK);//指令执行成功
                }
                else
                {
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_FAIL);//指令执行失败
                }
            }
        }

    }
}
