/// <summary> /// 接收MainController发来的消息,存贮到消息队列中 /// </summary> /// <param name="mt"></param> /// <param name="mb"></param> public static void MessageDelegate_For_MainController(MessageType mt, MessageBody mb) { int count = 0; Xml_codec.StaticOutputLog(LogInfoType.INFO, "AppContr收到Main侧消息。", "APPContr", LogCategory.R); Xml_codec.StaticOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), "APPContr", LogCategory.R); lock (mutex_Main2App_Msg) { rMain2AppMsgQueue.Enqueue(mb.bJson); if (recvMain2AppContrMsgNum == System.UInt32.MaxValue) { recvMain2AppContrMsgNum = 0; } else { recvMain2AppContrMsgNum++; } count = rMain2AppMsgQueue.Count; } string outStr = string.Format("ApContr共收到设备消息条数:{0},当前队列消息条数:{1}!", recvMain2AppContrMsgNum, count); Xml_codec.StaticOutputLog(LogInfoType.INFO, outStr, "APContr", LogCategory.R); }
private void CheckMsgBuff() { while (true) { try { HashSet <AsyncUserToken> RemoveList = new HashSet <AsyncUserToken>(); lock (locker1) { foreach (AsyncUserToken x in connList) { TimeSpan timeSpan = DateTime.Now - x.EndMsgTime; if (x.Buffer.Count > 0 && timeSpan.TotalSeconds > 10) //大于10秒认为设备不会再回消息了 { RemoveList.Add(x); } } foreach (AsyncUserToken x in RemoveList) { byte[] rev = x.Buffer.GetRange(0, x.Buffer.Count).ToArray(); string da = System.Text.Encoding.Default.GetString(rev).Trim(); if (!string.IsNullOrEmpty(da)) { Xml_codec.StaticOutputLog(LogInfoType.EROR, string.Format("清除设备[{0}:{1}]缓存消息。", x.IPAddress.ToString(), x.Port), "DeviceList"); Xml_codec.StaticOutputLog(LogInfoType.DEBG, string.Format("清除消息内容:\n{0}", da), "DeviceList"); } rev = null; da = null; x.Buffer.Clear(); x.Buffer.TrimExcess(); } connList.TrimExcess(); RemoveList.Clear(); RemoveList.TrimExcess(); } Thread.Sleep(10000); } catch (Exception e) { Xml_codec.StaticOutputLog(LogInfoType.EROR, string.Format("线程[CheckMsgBuff]出错。错误码:{0}", e.Message), "DeviceList"); } } }
static private void CheckImsiList() { HashSet <string> RemovList = new HashSet <string>(); while (true) { try { //去重功能关闭 if (DataController.RemoveDupMode != 1) { Thread.Sleep(3000); continue; } lock (locker1) { foreach (KeyValuePair <string, DateTime> kvp in dic) { //Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); TimeSpan timeSpan = DateTime.Now - kvp.Value; if (timeSpan.TotalMinutes > DataController.RemoveDupTimeLength) { RemovList.Add(kvp.Key); } } } //删除超时的imsi foreach (string x in RemovList) { del(x); } //Console.WriteLine("\n\n当前缓存Imsi数量: {0}", GetCount()); RemovList.Clear(); RemovList.TrimExcess(); Thread.Sleep(3000); } catch (Exception e) { Xml_codec.StaticOutputLog(LogInfoType.EROR, string.Format("线程[CheckImsiList]出错。错误码:{0}", e.Message), "ImsiRemoverDup"); } } }
private void CheckMsgId() { while (true) { try { lock (locker1) { foreach (AsyncUserToken x in connList) { HashSet <MsgId2App> RemoveList = new HashSet <MsgId2App>(); foreach (MsgId2App y in x.msgId2App) { TimeSpan timeSpan = DateTime.Now - y.AddTime; if (timeSpan.TotalSeconds > 30) //大于30秒认为设备不会再回消息了 { RemoveList.Add(y); } } foreach (MsgId2App y in RemoveList) { x.msgId2App.Remove(y); } x.msgId2App.TrimExcess(); RemoveList.Clear(); RemoveList.TrimExcess(); } } Thread.Sleep(10000); } catch (Exception e) { Xml_codec.StaticOutputLog(LogInfoType.EROR, string.Format("线程[CheckMsgId]出错。错误码:{0}", e.Message), "DeviceList"); } } }
/// <summary> /// 根据apinfo查询apToKen信息。首先通过ip+Prot查询,若查不到,再通过fullname查询 /// </summary> /// <param name="ApInfo">apinfo信息</param> /// <returns>apToKen信息,若未找到,返回null</returns> public AsyncUserToken FindByApInfo(Ap_Info_Struct ApInfo) { AsyncUserToken apToKen = this.FindByIpPort(ApInfo.IP, ApInfo.Port); if (apToKen == null) { Xml_codec.StaticOutputLog(LogInfoType.DEBG, string.Format("在线AP列表中找不到Ap[{0}:{1}]设备,通过FullName重新查询设备!", ApInfo.IP, ApInfo.Port.ToString()), "DeviceList"); apToKen = this.FindByFullname(ApInfo.Fullname); } if (apToKen == null) { Xml_codec.StaticOutputLog(LogInfoType.WARN, string.Format("在线AP列表中找不到Ap[{0}:{1}],FullName:{2}。无法向AP发送消息!", ApInfo.IP, ApInfo.Port.ToString(), ApInfo.Fullname), "DeviceList"); } return(apToKen); }
/// <summary> /// 处理收到的AP消息 /// </summary> /// <param name="apToKen"></param> /// <param name="msg"></param> private void HandleApMsg(AsyncUserToken apToKen, string msg) { //解析AP发过来的消息 UInt16 msgId = 0; Msg_Body_Struct msgBody = null; try { msgBody = Xml_codec.DecodeApXmlMessage(msg, ref msgId); } catch (Exception) { OnOutputLog(LogInfoType.EROR, string.Format("解析收到的Ap[{0}:{1}]消息出错!", apToKen.IPAddress.ToString(), apToKen.Port.ToString())); return; } if (msgBody == null) { OnOutputLog(LogInfoType.EROR, "收到消息格式错误!"); OnOutputLog(LogInfoType.DEBG, "出错消息内容:" + msg); return; } if (msgBody.type == ApMsgType.device_test_request) { Send2ap_device_test_response(apToKen); return; } if (msgBody.type != ApMsgType.status_response) { OnOutputLog(LogInfoType.INFO, string.Format("处理AP[{0}:{1}]消息({2}),消息Id={3}!", apToKen.IPAddress.ToString(), apToKen.Port, msgBody.type, msgId)); } //处理透传消息 if (msgId >= ApMsgIdClass.MIN_TRANSPARENT_MSG_ID && msgId <= ApMsgIdClass.MAX_TRANSPARENT_MSG_ID) { if (msgBody.type != ApMsgType.get_general_para_response) //参数上报消息不透传 { Msg_Body_Struct body = new Msg_Body_Struct(Main2ApControllerMsgType.transparent_msg_response, "transparent_msg", msg); OnSendMsg2Main(msgId, MsgStruct.MsgType.TRANSPARENT, apToKen, body); return; } } //心跳消息处理 if (msgBody.type == ApMsgType.status_response) { //OnOutputLog(LogInfoType.INFO, "收到心跳消息"); heartbeatMsgNum++; //UInt32 oldDetail = apToKen.Detail; UInt32 detail = 0; string sDetail = GetMsgStringValueInList("detail", msgBody); if (!string.IsNullOrEmpty(sDetail)) { detail = Convert.ToUInt32(sDetail, 16); } //Byte oldApReadySt = apToKen.ApReadySt; Byte ApReadySt = 5; string sApReadySt = GetMsgStringValueInList("addStatus", msgBody); if (!string.IsNullOrEmpty(sApReadySt)) { ApReadySt = Convert.ToByte(sApReadySt); } apToKen.version = GetMsgStringValueInList("version", msgBody); apToKen.Mode = GetMsgStringValueInList("mode", msgBody); apToKen.Sn = GetMsgStringValueInList("sn", msgBody); apToKen.FullName = GetMsgStringValueInList("fullname", msgBody); if (string.IsNullOrEmpty(apToKen.FullName)) //兼容老Agent拼写错误 { apToKen.FullName = GetMsgStringValueInList("fullaname", msgBody); } apToKen.Id = GetMsgStringValueInList("id", msgBody); //apToKen.Detail = detail; int i = MyDeviceList.add(apToKen); Send2main_OnOffLine(OnLine, i, apToKen); //判断是周期心跳,还是上线心跳 if ((detail & (int)AP_STATUS_LTE.OnLine) > 0) //上线 { //OnOutputLog(LogInfoType.DEBG, "上线消息"); if (OnLine.Equals(MyDeviceList.GetMainControllerStatus(apToKen))) { Send2ap_status_request(apToKen); Thread.Sleep(1000); Send2ap_get_general_para_request(apToKen); } else { OnOutputLog(LogInfoType.DEBG, "MainController未回复上线成功消息!"); } } else //周期心跳 { //OnOutputLog(LogInfoType.DEBG, "周期心跳消息"); } //发送状态改变 Send2ap_ApStatusChange_LTE(apToKen, detail, ApReadySt); } else if (msgBody.type == ApMsgType.get_general_para_response) { Msg_Body_Struct SendMsgBody = msgBody; SendMsgBody.type = Main2ApControllerMsgType.ReportGenPara; // 向Main模块发消息 OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody); } else if (msgBody.type == ApMsgType.set_general_para_response) { Msg_Body_Struct SendMsgBody = msgBody; SendMsgBody.type = Main2ApControllerMsgType.SetGenParaRsp; // 向Main模块发消息 OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody); } else if (msgBody.type == ApMsgType.imsi_list_config_result) { Msg_Body_Struct SendMsgBody = msgBody; SendMsgBody.type = Main2ApControllerMsgType.app_add_bwlist_response; // 向Main模块发消息 OnSendMsg2Main(msgId, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody); } else if (msgBody.type == ApMsgType.imsi_list_delconfig_result) { Msg_Body_Struct SendMsgBody = msgBody; SendMsgBody.type = Main2ApControllerMsgType.app_del_bwlist_response; // 向Main模块发消息 OnSendMsg2Main(msgId, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody); } else if (msgBody.type == ApMsgType.scanner) { imsiMsgNum++; recvApImsiMsgNum++; string imsi = GetMsgStringValueInList("imsi", msgBody); if (!string.IsNullOrEmpty(imsi)) { //IMSI去重处理 if (DataController.RemoveDupMode == 1) { //有该IMSI,去重,不用上报 if (ImsiRemoveDup.isExist(imsi)) { OnOutputLog(LogInfoType.INFO, string.Format("Imsi[{0}]在{1}时被抓到过,不再上报!", imsi, ImsiRemoveDup.GetTime(imsi))); return; } ImsiRemoveDup.add(imsi); } Msg_Body_Struct body = new Msg_Body_Struct(msgBody.type, msgBody.dic); OnOutputLog(LogInfoType.DEBG, string.Format("上报imsi:{0}", imsi)); OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, body); } } else { Msg_Body_Struct body = new Msg_Body_Struct(msgBody.type, msgBody.dic); OnSendMsg2Main(msgId, MsgStruct.MsgType.CONFIG, apToKen, body); } msgBody = null; }