Ejemplo n.º 1
0
 protected string format(string format, params object[] args)
 {
     return(DataHandler.format(format, args));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 线程的执行过程
        /// </summary>
        private void ThreadHandlePorc(Object state)
        {
            var gpsHandlerInterval = int.Parse(ConfigurationManager.AppSettings["FETCHING_ADDRESS_INTERVAL"]);
            var stat = (int)state;
            AsyncUserDataBuffer obj;
            IridiumData         iridium;
            // 处理数据的Handler
            DataHandler _handler = new DataHandler();

            _handler.OnUnhandledMessage += new EventHandler <UIEventArgs>(DataHandler_OnUnhandledMessage);
            _handler.OnIridiumSend      += new EventHandler <IridiumDataEvent>(DataHandler_OnIridiumSend);
            _handler.OnTrackerChating   += new EventHandler <TrackerChatEvent>(DataHandler_OnTrackerChating);
            _handler.Server              = _server;
            int timer = 0, sleeper = 10, gpsHandler = 0;

            while (true)
            {
                Thread.Sleep(sleeper);
                timer += sleeper;

                // 服务已停止时跳出循环
                if (HasServiceStoped)
                {
                    break;
                }

                obj = null;
                if (!_dataPool.TryDequeue(out obj))
                {
                    obj = null;
                }

                // 没有取到数据时继续循环
                if (null != obj)
                {
                    try
                    {
                        string message = "";
                        switch (obj.DataType)
                        {
                        case AsyncUserDataType.ClientConnected:
                            message = format("{0}client {1}:{2} connected. [{3}]", DateTime(obj.ReceiveTime), obj.IP, obj.Port, obj.PackageType);
                            break;

                        case AsyncUserDataType.ClientDisconnected:
                            message = format("{0}client {1}:{2} disconnected. [{3}]", DateTime(obj.ReceiveTime), obj.IP, obj.Port, obj.PackageType);
                            break;

                        case AsyncUserDataType.ReceivedData:
                            message = format("{0}received data(length: {1}): {2} [{3}]", DateTime(obj.ReceiveTime),
                                             (null == obj.Buffer ? 0 : obj.Buffer.Length), CustomConvert.GetHex(obj.Buffer), obj.PackageType);
                            break;
                        }

                        // 处理数据
                        if (null != obj.Buffer)
                        {
                            _handler.HandleData(obj);
                        }

                        if (!string.IsNullOrEmpty(message))
                        {
                            HandleDisplayMessage(message);
                        }
                    }
                    finally
                    {
                        _server.RecycleBuffer(obj);
                    }
                }
                else
                {
                    if (stat == 0)
                    {
                        try
                        {
                            // 处理未处理的SMS信息
                            if (_handler.CanCheckSMSData())
                            {
                                _handler.CheckSMSData();
                            }
                        }
                        catch (Exception sms)
                        { HandleDisplayMessage(format("{0} Cannot handle CheckSMSData: {1}, Trace: {2}", Now, sms.Message, sms.StackTrace)); }
                    }
                }

                // 查找是否有铱星数据待处理
                iridium = null;
                if (!_iridiumPool.TryDequeue(out iridium))
                {
                    iridium = null;
                }
                if (null != iridium)
                {
                    try
                    {
                        // 处理铱星数据
                        _handler.HandleIridiumData(iridium);
                    }
                    catch (Exception iri)
                    {
                        HandleDisplayMessage(format("{0}Cannot handle HandleIridiumData: {1} Trace: {2}{3}{4}", Now, iri.Message, Environment.NewLine, iri.StackTrace, Environment.NewLine));
                    }
                }
                // 只有第一个线程有权处理命令后面的数据
                if (stat > 0)
                {
                    timer = 0;
                    continue;
                }
                // 处理TCP下发送的命令
                if (timer % 1000 == 0)
                {
                    // 每秒钟增加1
                    gpsHandler++;

                    lock (Locker)
                    {
                        try
                        {
                            // 每5秒处理一次未处理的excel请求
                            if (gpsHandler % 5 == 0)
                            {
                                // 首先处理tms的work分配
                                if (!_handler.HandleWebRequestExcel2pdf())
                                {
                                    // 处理导出work time到excel请求
                                    if (!_handler.HandleWebRequestWorkTime2Excel())
                                    {
                                        // 处理导出设备列表或终端列表到excel的请求
                                        _handler.HandleWebRequestEquipmentsOrTerminalsToExcel();
                                    }
                                }
                            }

                            // 每2秒处理一次未发的命令
                            if (gpsHandler % 2 == 0)
                            {
                                try
                                {
                                    _handler.CheckGSMCommand();
                                    _handler.CheckIridiumCommand();
                                    _handler.CheckTrackerChat();
                                }
                                catch (Exception e)
                                {
                                    HandleDisplayMessage(format("{0}Can not handle CheckTcpCommand/CheckIridiumCommand: {1}, Trace: {2}", Now, e.Message, e.StackTrace));
                                }
                            }

                            // 10秒处理一次获取GPS地理位置
                            if (gpsHandler % gpsHandlerInterval == 0)
                            {
                                gpsHandler = 0;
                                // 处理一次定位信息
                                if (stat == 0)
                                {
                                    _handler.HandleGpsAddress();
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            HandleDisplayMessage(format("{0} Cannot handle CheckCommand: {1}, Trace: {2}", Now, e.Message, e.StackTrace));
                        }
                    }
                }
                // 检测是否应该处理旧链接,每分钟处理一次
                if (timer % 2000 == 0)
                {
                    timer = 0;
                    lock (Locker)
                    {
                        if (_handler.CanClearOlderLinks)
                        {
                            try
                            {
                                _handler.HandleOlderClients();
                            }
                            catch (Exception e)
                            { HandleDisplayMessage(format("{0} Cannot handle OlderClients: {1}, Trace: {2}", Now, e.Message, e.StackTrace)); }
                        }
                    }
                }
            }
        }