/// <summary> /// 向Ap发送Main模块过来的消息 /// </summary> /// <param name="MainMsg"></param> private void Send2ap_RecvMainMsg(InterModuleMsgStruct MainMsg) { AsyncUserToken apToKen = MyDeviceList.FindByApInfo(MainMsg.ApInfo); if (apToKen == null) { OnOutputLog(LogInfoType.WARN, string.Format("在线AP列表中找不到Ap[{0}:{1}]设备({2})!", MainMsg.ApInfo.IP, MainMsg.ApInfo.Port.ToString(), MainMsg.ApInfo.Fullname)); return; } MsgId2App msgId2App = new MsgId2App(); msgId2App.id = ApMsgIdClass.addNormalMsgId(); msgId2App.AppInfo = MainMsg.AppInfo; if (!MyDeviceList.AddMsgId2App(apToKen, msgId2App)) { OnOutputLog(LogInfoType.EROR, string.Format("添加消息Id到设备列表出错!")); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, string.Format("添加消息Id到设备列表出错!")); return; } if (!SendMsg2Ap(apToKen, msgId2App.id, MainMsg.Body)) { Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, string.Format("封装向AP发送的XML消息({0})出错!", MainMsg.Body.type)); } }
/// <summary> /// 向AP发送透传消息 /// </summary> /// <param name="appToKen"></param> /// <param name="ApInfo"></param> /// <param name="TypeKeyValue"></param> protected void OnSendTransparentMsg2Main(AsyncUserToken appToKen, Ap_Info_Struct ApInfo, Msg_Body_Struct TypeKeyValue) { MessageType mt = MessageType.MSG_JSON; MessageBody mb = new MessageBody(); InterModuleMsgStruct msg = new InterModuleMsgStruct(); msg.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); msg.MsgType = MsgType.TRANSPARENT.ToString(); msg.AppInfo.User = appToKen.User; msg.AppInfo.Group = appToKen.Group; msg.AppInfo.Domain = appToKen.Domain; msg.AppInfo.Ip = appToKen.IPAddress.ToString(); msg.AppInfo.Port = appToKen.Port; msg.AppInfo.Type = DeviceType; msg.ApInfo = ApInfo; string sData = GetMsgStringValueInList("transparent_msg", TypeKeyValue); //sData = Regex.Replace(sData, "<\\s*id\\s*>.+<\\s*/\\s*id\\s*>", string.Format("<id>{0}</id>", ApMsgIdClass.addTransparentMsgId())); Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("transparent_msg", sData); TypeKeyValue.dic = dic; msg.Body = TypeKeyValue; mb.bJson = JsonConvert.SerializeObject(msg); OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给MainController模块!", TypeKeyValue.type), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), LogCategory.S); AppManager.sendMsg_2_MainController(mt, mb); }
/// <summary> /// 发送消息到MainContrller模块 /// </summary> /// <param name="appToKen">App连接信息</param> /// <param name="ApInfo">Ap信息</param> /// <param name="TypeKeyValue">发送内容</param> protected void OnSendMsg2Main(AsyncUserToken appToKen, Ap_Info_Struct ApInfo, Msg_Body_Struct TypeKeyValue) { MessageType mt = MessageType.MSG_JSON; MessageBody mb = new MessageBody(); InterModuleMsgStruct msg = new InterModuleMsgStruct(); msg.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); msg.MsgType = MsgType.CONFIG.ToString(); msg.AppInfo.User = appToKen.User; msg.AppInfo.Group = appToKen.Group; msg.AppInfo.Domain = appToKen.Domain; msg.AppInfo.Ip = appToKen.IPAddress.ToString(); msg.AppInfo.Port = appToKen.Port; msg.AppInfo.Type = DeviceType; msg.ApInfo = ApInfo; msg.Body = TypeKeyValue; mb.bJson = JsonConvert.SerializeObject(msg); OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给MainController模块!", TypeKeyValue.type), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), LogCategory.S); AppManager.sendMsg_2_MainController(mt, mb); }
/// <summary> /// 向APP返回通用错误消息 /// </summary> /// <param name="appToken">app信息</param> /// <param name="type">app发送过来的消息类型</param> /// <param name="str">错误描述</param> protected void Send2APP_GeneralError(AsyncUserToken appToken, string type, string str) { MessageType mt = MessageType.MSG_JSON; MessageBody mb = new MessageBody(); InterModuleMsgStruct msg = new InterModuleMsgStruct(); msg.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); msg.MsgType = MsgStruct.MsgType.CONFIG.ToString(); msg.ApInfo = null; msg.AppInfo.Type = string.Empty; msg.AppInfo.Ip = appToken.IPAddress.ToString(); msg.AppInfo.Port = appToken.Port; msg.AppInfo.Group = appToken.Group; msg.AppInfo.User = appToken.User; msg.AppInfo.Domain = appToken.Domain; Msg_Body_Struct TypeKeyValue = new Msg_Body_Struct(AppMsgType.general_error_result, "ErrStr", str, "RecvType", type); msg.Body = TypeKeyValue; mb.bJson = JsonConvert.SerializeObject(msg); OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给MainController模块!", TypeKeyValue.type), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), LogCategory.S); ApManager.sendMsg_2_MainController(mt, mb); }
protected void Send2APP_GeneralError(Ap_Info_Struct apInfo, App_Info_Struct appInfo, string type, string str) { MessageType mt = MessageType.MSG_JSON; MessageBody mb = new MessageBody(); InterModuleMsgStruct msg = new InterModuleMsgStruct(); msg.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); msg.MsgType = MsgStruct.MsgType.CONFIG.ToString(); msg.ApInfo = apInfo; msg.AppInfo = appInfo; Msg_Body_Struct TypeKeyValue = new Msg_Body_Struct(AppMsgType.general_error_result, "ErrStr", str, "RecvType", type); msg.Body = TypeKeyValue; mb.bJson = JsonConvert.SerializeObject(msg); OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给MainController模块!", TypeKeyValue.type), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), LogCategory.S); ApManager.sendMsg_2_MainController(mt, mb); //OnReceiveMainMsg(mt, mb); }
/// <summary> /// 检测数所库保存的Ap上下线状态是否正确,若不正确,向其发送正状态 /// </summary> /// <param name="status">数据库保存状态</param> /// <param name="apToKen">ap信息</param> protected void Send2main_OnOffLineCheck(string status, Ap_Info_Struct ApInfo) { string MainControllerStatus = OffLine; AsyncUserToken apToKen = MyDeviceList.FindByIpPort(ApInfo.IP, ApInfo.Port); if (apToKen == null) { apToKen = MyDeviceList.FindByFullname(ApInfo.Fullname); } if (apToKen == null) { MainControllerStatus = OffLine; } else { MainControllerStatus = apToKen.MainControllerStatus; } OnOutputLog(LogInfoType.DEBG, "保存的状态为:" + MainControllerStatus + ";接收到状态为:" + status); if (String.Compare(MainControllerStatus, status, true) != 0) { Msg_Body_Struct TypeKeyValue = new Msg_Body_Struct(Main2ApControllerMsgType.OnOffLine, "AllOnLineNum", MyDeviceList.GetCount().ToString(), "Status", MainControllerStatus, "mode", apToKen.Mode, "timestamp", DateTime.Now.ToLocalTime().ToString()); //向Main模块发消息 MessageType mt = MessageType.MSG_JSON; MessageBody mb = new MessageBody(); InterModuleMsgStruct msg = new InterModuleMsgStruct(); msg.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); msg.MsgType = MsgStruct.MsgType.NOTICE.ToString(); msg.ApInfo = ApInfo; App_Info_Struct appInfo = new App_Info_Struct(); appInfo.Ip = AllDevice; msg.AppInfo = appInfo; msg.Body = TypeKeyValue; mb.bJson = JsonConvert.SerializeObject(msg); OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给MainController模块!", TypeKeyValue.type), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), LogCategory.S); ApManager.sendMsg_2_MainController(mt, mb); } }
/// <summary> /// 透传MainController模块过来的消息给设备 /// </summary> /// <param name="MainMsg"></param> private void Send2ap_TransparentMsg(InterModuleMsgStruct MainMsg) { AsyncUserToken apToKen = MyDeviceList.FindByApInfo(MainMsg.ApInfo); if (apToKen == null) { OnOutputLog(LogInfoType.WARN, string.Format("在线AP列表中找不到Ap[{0}:{1}]设备({2})!", MainMsg.ApInfo.IP, MainMsg.ApInfo.Port.ToString(), MainMsg.ApInfo.Fullname)); return; } string sendMsg = GetMsgStringValueInList("transparent_msg", MainMsg.Body); if (string.IsNullOrEmpty(sendMsg)) { OnOutputLog(LogInfoType.EROR, string.Format("透传XML消息为空!")); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, string.Format("透传XML消息为空!")); return; } if (!Regex.IsMatch(sendMsg, "<\\s*id\\s*>.+<\\s*/\\s*id\\s*>")) { OnOutputLog(LogInfoType.EROR, string.Format("透传的XML消息中没有id项!")); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, string.Format("透传的XML消息中没有id项!")); return; } UInt16 id = ApMsgIdClass.addTransparentMsgId(); sendMsg = Regex.Replace(sendMsg, "<\\s*id\\s*>.+<\\s*/\\s*id\\s*>", string.Format("<id>{0}</id>", id)); MsgId2App msgId2App = new MsgId2App(); msgId2App.id = id; msgId2App.AppInfo = MainMsg.AppInfo; if (!MyDeviceList.AddMsgId2App(apToKen, msgId2App)) { OnOutputLog(LogInfoType.EROR, string.Format("添加消息Id到设备列表出错!")); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, string.Format("添加消息Id到设备列表出错!")); return; } SendMsg2Ap(apToKen, MainMsg.Body.type, sendMsg); }
/// <summary> /// 向AP删除黑白名单 /// </summary> /// <param name="token">AP信息</param> private void Send2ap_imsi_list_delconfig(InterModuleMsgStruct MainMsg) { AsyncUserToken apToKen = MyDeviceList.FindByApInfo(MainMsg.ApInfo); if (apToKen == null) { OnOutputLog(LogInfoType.WARN, string.Format("在线AP列表中找不到Ap[{0}:{1}]设备({2})!", MainMsg.ApInfo.IP, MainMsg.ApInfo.Port.ToString(), MainMsg.ApInfo.Fullname)); return; } Msg_Body_Struct TypeKeyValue = new Msg_Body_Struct(ApMsgType.imsi_list_delconfig); int i = 0; foreach (Name_DIC_Struct n_dicList in MainMsg.Body.n_dic) { Dictionary <string, object> dic = n_dicList.dic; String bwFlag = GetMsgStringValueInList("bwFlag", dic); if (!bwFlag.Equals("black") && !bwFlag.Equals("white")) { OnOutputLog(LogInfoType.WARN, "删除黑白名单参数错误。bwFlag字段错误!"); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, "删除黑白名单参数错误。bwFlag字段错误!"); continue; } if (bwFlag.Equals("white")) { String imsi = GetMsgStringValueInList("imsi", dic); if (imsi.Equals(String.Empty)) { OnOutputLog(LogInfoType.WARN, "删除白名单参数错误。imsi字段错误!"); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, "删除白名单参数错误。imsi字段错误!"); continue; } Name_DIC_Struct n_dic = new Name_DIC_Struct("name" + i++); n_dic.dic.Add("whiteimsi/imsi", imsi); TypeKeyValue.n_dic.Add(n_dic); } if (bwFlag.Equals("black")) { String imsi = GetMsgStringValueInList("imsi", dic); if (imsi.Equals(String.Empty)) { OnOutputLog(LogInfoType.WARN, "删除黑名单参数错误。imsi字段错误!"); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, "删除黑名单参数错误。imsi字段错误!"); continue; } Name_DIC_Struct n_dic = new Name_DIC_Struct("name" + i++); n_dic.dic.Add("blackimsi/imsi", imsi); TypeKeyValue.n_dic.Add(n_dic); } } if (i == 0) { OnOutputLog(LogInfoType.EROR, "删除黑名单参数错误。没有要发送的名单!"); Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, "删除黑名单参数错误。没有要发送的名单!"); return; } if (!SendMsg2Ap(apToKen, 0, TypeKeyValue)) { Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type, "封装XML消息(imsi_list_delconfig)出错!"); } }
/// <summary> /// 接收到MainController模块消息处理线程 /// </summary> /// <param name="o"></param> public void ReceiveMainMsgThread(object o) { bool noMsg = false; int hNum = 0; int count = 0; long upTime = 0; string str = string.Empty; while (true) { try { if (DeviceType != null && ((DateTime.Now.Ticks - upTime) / 10000000) > 60) { upTime = DateTime.Now.Ticks; //报到线程状态 FrmMainController.write_monitor_status("MAIN_2_" + DeviceType + "_HANDLE"); } if (noMsg) { Thread.Sleep(10); } else { if (hNum >= 100) { hNum = 0; //Thread.Sleep(10); } } lock (AppManager.mutex_Main2App_Msg) { if (AppManager.rMain2AppMsgQueue.Count <= 0) { noMsg = true; hNum = 0; continue; } else { noMsg = false; hNum++; str = AppManager.rMain2AppMsgQueue.Dequeue(); } count = AppManager.rMain2AppMsgQueue.Count; } if (AppManager.handleMain2AppContrMsgNum == System.UInt32.MaxValue) { AppManager.handleMain2AppContrMsgNum = 0; } else { AppManager.handleMain2AppContrMsgNum++; } //解析收到的消息 InterModuleMsgStruct MainMsg = null; try { MainMsg = JsonConvert.DeserializeObject <InterModuleMsgStruct>(str); } catch (Exception) { OnOutputLog(LogInfoType.EROR, "解析收到的Main模块消息出错!", LogCategory.I); OnOutputLog(LogInfoType.INFO, string.Format("共处理Main2App消息条数:{0},当前队列消息条数:{1}!", AppManager.handleMain2AppContrMsgNum, count)); continue; } if ((MainMsg.AppInfo.Ip.Equals(MsgStruct.AllDevice)) || (MainMsg.AppInfo.Type.Equals(DeviceType))) { OnOutputLog(LogInfoType.INFO, string.Format("接收到MainController消息({0})。", MainMsg.Body.type)); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", str)); } AppInnerType flag; if ((!MainMsg.AppInfo.Ip.Equals(MsgStruct.AllDevice)) && (!(Enum.TryParse <AppInnerType>(MainMsg.AppInfo.Type, true, out flag)))) { if (string.IsNullOrEmpty(MainMsg.AppInfo.Type)) { MainMsg.AppInfo.Type = "空"; } OnOutputLog(LogInfoType.EROR, "收到MainController模块消息中APP类型错误!收到类型为:" + MainMsg.AppInfo.Type); continue; } if (MainMsg.Body.type == ApMsgType.scanner) { recvMainImsiMsgNum++; } if (ReceiveMainData != null && MainMsg != null) { ReceiveMainData(MainMsg); } OnOutputLog(LogInfoType.INFO, string.Format("共处理Main2App消息条数:{0},当前队列消息条数:{1}!", AppManager.handleMain2AppContrMsgNum, count)); } catch (Exception e) { OnOutputLog(LogInfoType.EROR, string.Format("线程[ReceiveMainMsgThread]出错。错误码:{0}", e.Message)); } } }
/// <summary> /// 发送消息给MainController模块 /// </summary> /// <param name="msgId">Ap返回的消息Id</param> /// <param name="msgType">Main模块消息类型</param> /// <param name="apToKen">Ap信息</param> /// <param name="TypeKeyValue">消息内容</param> public void OnSendMsg2Main(UInt16 msgId, MsgStruct.MsgType msgType, AsyncUserToken apToKen, Msg_Body_Struct TypeKeyValue) { MessageType mt = MessageType.MSG_JSON; MessageBody mb = new MessageBody(); InterModuleMsgStruct msg = new InterModuleMsgStruct(); msg.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); msg.MsgType = msgType.ToString(); msg.ApInfo.SN = apToKen.Sn; msg.ApInfo.Fullname = apToKen.FullName; msg.ApInfo.IP = apToKen.IPAddress.ToString(); msg.ApInfo.Port = apToKen.Port; msg.ApInfo.Type = DeviceType; App_Info_Struct appInfo = new App_Info_Struct(); if ((msgType == MsgType.NOTICE) || (msgId == 0)) { appInfo.Ip = AllDevice; } else { //通过消息id查找AppInfo AsyncUserToken ListToken = MyDeviceList.FindByIpPort(apToKen.IPAddress.ToString(), apToKen.Port); if (ListToken == null) { String str = string.Format( "未找到设备[{0}:{1}]消息ID({2})对应的APP信息,不发送消息给MainController模块!", apToKen.IPAddress.ToString(), apToKen.Port, msgId); OnOutputLog(LogInfoType.EROR, str); return; } HashSet <MsgId2App> msgId2App = new HashSet <MsgId2App>(); msgId2App = ListToken.msgId2App; foreach (MsgId2App x in msgId2App) { if (x.id == msgId) { appInfo = x.AppInfo; break; } } //删除保存的消息Id列表(目前实现的方法是120秒后自动删除) //MyDeviceList.RemoveMsgId2App(apToKen, msgId); } if (string.IsNullOrEmpty(appInfo.Ip)) { String str = string.Format( "未找到设备[{0}:{1}]消息ID({2})对应的APP信息,不发送消息给MainController模块!", apToKen.IPAddress.ToString(), apToKen.Port, msgId); OnOutputLog(LogInfoType.EROR, str); return; } msg.AppInfo = appInfo; msg.Body = TypeKeyValue; mb.bJson = JsonConvert.SerializeObject(msg); //当设备状态为在线时再发送消息给Main模块 if ((String.Compare(MyDeviceList.GetMainControllerStatus(apToKen), OnLine, true) != 0) && (!TypeKeyValue.type.Equals(Main2ApControllerMsgType.OnOffLine))) { OnOutputLog(LogInfoType.WARN, string.Format("设备[{0}:{1}]在线状态为:{2},OnLine状态才向Main模块发送消息{3}!", apToKen.IPAddress.ToString(), apToKen.Port.ToString(), MyDeviceList.GetMainControllerStatus(apToKen), TypeKeyValue.type)); if (TypeKeyValue.type == ApMsgType.scanner) { noSendMainImsiNum++; } return; } if (TypeKeyValue.type == ApMsgType.scanner) { sendMainImsiMsgNum++; } OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给MainController模块!", TypeKeyValue.type), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), LogCategory.S); ApManager.sendMsg_2_MainController(mt, mb); }