Beispiel #1
0
        /// <summary>
        /// 将Json字符串转为预警结果信息
        /// </summary>
        /// <param name="jsonTxt">Socket传送过来的Json字符串</param>
        /// <returns></returns>
        static public UpdateWarningResultMessage Convert2WarningResultMsg(string jsonTxt)
        {
            UpdateWarningResultMessage warningResult = (UpdateWarningResultMessage)JsonConvert.DeserializeObject <UpdateWarningResultMessage>(jsonTxt);

            return(warningResult);
        }
Beispiel #2
0
        /// <summary>
        /// 处理来自服务端的数据
        /// </summary>
        /// <returns></returns>
        public void HandleReceivedData()
        {
            byte[] recvData = new byte[ClientSocket.MAX_RECV_LEN];
            while (true)
            {
                try
                {
                    if (_socketHandler.Connected == false)
                    {
                        StartReconnectThread();
                        return;
                    }
                    if (_socketHandler.Poll(-1, SelectMode.SelectRead))
                    {
                        int    recvDataLen = _socketHandler.Receive(recvData);
                        string strDatas    = System.Text.Encoding.Default.GetString(recvData, 0, recvDataLen);
                        //_log.Log("Received data: " + strDatas + " Length: " + recvDataLen.ToString());
                        if (strDatas == null || strDatas == "")
                        {
                            continue;
                        }
                        //通过Json先获取命令ID,然后根据不同ID将其转换为相应的实体类型
                        JObject    jo    = (JObject)JsonConvert.DeserializeObject(strDatas);
                        COMMAND_ID cmdId = COMMAND_ID.UNDEFINED;
                        if (jo[ConstSocketStr.COMMADN_ID] != null)
                        {
                            if (!Enum.TryParse <COMMAND_ID>(jo[ConstSocketStr.COMMADN_ID].ToString(), out cmdId))
                            {
                                Log.Error("无法解析Socket命令-->" + strDatas);
                                continue;
                            }
                        }
                        else
                        {
                            Log.Debug("无法解析Socket命令-->" + strDatas);
                            continue;
                        }
                        //更新预警结果
                        if (cmdId == COMMAND_ID.NTFY_WARNING_RESULT ||
                            cmdId == COMMAND_ID.WARNING_RESULT_SINGLE)
                        {
                            #region //test
                            //_log.Log("COMMAND 'NTFY_WARNING_RESULT ' RECEIVED!");
                            //UpdateWarningDataMsg warningResult = (UpdateWarningDataMsg)JsonConvert.DeserializeObject<UpdateWarningDataMsg>(strDatas);
                            //_log.Log("tunnelID: " + warningResult.TunnelId +
                            //    ", commandID: " + warningResult.CommandId.ToString() +
                            //    ", DateTime: " + warningResult.DTime);
                            #endregion

                            UpdateWarningResultMessage resultMsg = JsonParser.Convert2WarningResultMsg(strDatas);
                            //调用处理事件
                            OnMsgUpdateWarningResult(resultMsg);
                        }
                        else
                        {
                            Log.Debug("未处理命令:" + cmdId.ToString() + " --> " + strDatas);
                            //未定义命令类型
                            if (cmdId == COMMAND_ID.UNDEFINED)
                            {
                            }
                            //同步时间
                            if (cmdId == COMMAND_ID.SYNC_DATETIME)
                            {
                            }
                            //心跳检测
                            if (cmdId == COMMAND_ID.KEEP_ALIVE)
                            {
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    Log.Error("Error in Function HandleReceivedData: " + ex.Message);
                }
            }
        }