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

namespace TZONE.GPSSocketServer.Command.DU
{
    public class DataHandle : CommandBase<GPSSession, BinaryRequestInfo>
    {
        public override string Name
        {
            get
            {
                return "24-55";//$U
            }
        }
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public override void ExecuteCommand(GPSSession session, BinaryRequestInfo requestInfo)
        {
            try
            {
                tempdata model = new tempdata();
                model.data_body = requestInfo.Body;
                byte[] bt0 = new byte[15];//15位的机器IMEI号
                Array.Copy(requestInfo.Body, 2, bt0, 0, bt0.Length);
                model.imei = System.Text.Encoding.ASCII.GetString(bt0);
                if (IMEIFilter.GetIMEIContains(ref model))
                {
                    if (string.IsNullOrEmpty(model.product_model))
                    {
                        session.Send("$U NO MODEL");
                        session.Close();
                        return;
                    }
                    session.IMEI = model.imei;
                    model.EventTempData += model_EventTempData;
                    model.ExecuteEvent();
                }
                else
                {
                    session.Send("$U 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 + ".DU.AnalyzeHandle";
            String assemblyName = "TZONE." + _temp.product_model + @".DU.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);//指令执行失败
                }
            }
        }

    }
}
