/// <summary> /// 发送消息到App /// </summary> /// <param name="appToKen">App信息</param> /// <param name="TypeKeyValue">消息内容</param> protected void SendMsg2App(AsyncUserToken appToKen, Msg_Body_Struct TypeKeyValue) { if (appToKen == null) { OnOutputLog(LogInfoType.WARN, string.Format("目的设备信息为NULL!")); return; } if (TypeKeyValue.type == ApMsgType.scanner) { sendAppImsiMsgNum++; } DeviceServerMsgStruct msgStruct = new DeviceServerMsgStruct(); msgStruct.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); msgStruct.ApInfo.IP = MsgStruct.NullDevice; msgStruct.Body = TypeKeyValue; string strJosn = JsonConvert.SerializeObject(msgStruct); if (-1 == strJosn.IndexOf(AppMsgType.app_heartbeat_response)) { OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给APP[{1}:{2}]!", TypeKeyValue.type, appToKen.IPAddress, appToKen.Port), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", strJosn), LogCategory.S); } byte[] buff = System.Text.Encoding.Default.GetBytes(strJosn); MySocket.SendMessage(appToKen, buff); }
/// <summary> /// 重载收到Main模块消息处理 /// </summary> /// <param name="str">消息内容</param> public void OnReceiveMainMsg(MsgStruct.InterModuleMsgStruct MainMsg) { //在此处理MainController发送过来的信息 if (MainMsg == null || MainMsg.Body == null) { OnOutputLog(LogInfoType.EROR, "收到MainController模块消息内容为空!"); return; } if (MainMsg.AppInfo.Ip == null || MainMsg.AppInfo.Type == null) { OnOutputLog(LogInfoType.EROR, "收到MainController模块消息AppInfo内容错误!"); return; } if ((!MainMsg.AppInfo.Ip.Equals(MsgStruct.AllDevice)) && (!MainMsg.AppInfo.Type.Equals(MODE_NAME))) { //OnOutputLog(LogInfoType.DEBG, "收到MainController模块消息,不是本模块消息!"); return; } OnOutputLog(LogInfoType.INFO, string.Format("处理MainController消息。消息类型:{0}。", MainMsg.Body.type)); DeviceServerMsgStruct deviceServerMsgStruct = new DeviceServerMsgStruct(); deviceServerMsgStruct.Version = MainMsg.Version; deviceServerMsgStruct.ApInfo = MainMsg.ApInfo; deviceServerMsgStruct.Body = MainMsg.Body; SendMsg2App(MainMsg.AppInfo, deviceServerMsgStruct); return; }
/// <summary> /// 发送消息到App /// </summary> /// <param name="appInfo">Ap信息</param> /// <param name="stdeviceServerMsgStructr">消息内容</param> protected void SendMsg2App(App_Info_Struct appInfo, DeviceServerMsgStruct deviceServerMsgStruct) { try { if ((string.IsNullOrEmpty(appInfo.Ip)) || (appInfo.Ip == MsgStruct.NullDevice)) { OnOutputLog(LogInfoType.INFO, string.Format("目的设备为Null,不向App发送信息!")); return; } if (deviceServerMsgStruct.Body.type == ApMsgType.scanner) { sendAppImsiMsgNum++; } string strJosn = JsonConvert.SerializeObject(deviceServerMsgStruct); byte[] buff = System.Text.Encoding.Default.GetBytes(strJosn); if (appInfo.Ip == MsgStruct.AllDevice) { OnOutputLog(LogInfoType.INFO, string.Format("目的设备为All,向所有App发送信息!")); //HashSet<AsyncUserToken> toKenList = MyDeviceList.GetConnList(); AsyncUserToken[] toKenList = MyDeviceList.GetConnListToArray(); if (toKenList.Length > 0) { foreach (AsyncUserToken appToKen in toKenList) { OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给APP[{1}:{2}]!", deviceServerMsgStruct.Body.type, appToKen.IPAddress, appToKen.Port), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", strJosn), LogCategory.S); MySocket.SendMessage(appToKen, buff); } } } else { AsyncUserToken appToKen = MyDeviceList.FindByIpPort(appInfo.Ip, appInfo.Port); if (appToKen == null) { OnOutputLog(LogInfoType.WARN, string.Format("设备列表中未找到该App设备[{0}:{1}]信息!", appInfo.Ip, appInfo.Port)); return; } OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给APP[{1}:{2}]!", deviceServerMsgStruct.Body.type, appToKen.IPAddress, appToKen.Port), LogCategory.S); OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", strJosn), LogCategory.S); MySocket.SendMessage(appToKen, buff); } buff = null; strJosn = null; } catch (Exception ee) { OnOutputLog(LogInfoType.EROR, "发送消息到App出错。出错原因:" + ee.Message, LogCategory.I); } return; }
/// <summary> /// 处理收到的App侧消息 /// </summary> /// <param name="appToKen"></param> /// <param name="AppMsg"></param> private void HandleAppMsg(AsyncUserToken appToKen, string msg) { ////解析收到的消息 DeviceServerMsgStruct AppMsg = null; try { AppMsg = JsonConvert.DeserializeObject <DeviceServerMsgStruct>(msg); } catch (Exception) { OnOutputLog(LogInfoType.EROR, "解析收到的App消息出错。JSON格式错误!"); SendErrorMsg2App(appToKen, null, "解析收到的App消息出错。JSON格式错误!"); return; } if (AppMsg == null) { OnOutputLog(LogInfoType.EROR, "收到消息格式错误!"); SendErrorMsg2App(appToKen, null, "收到消息格式错误!"); OnOutputLog(LogInfoType.DEBG, "出错消息内容:" + msg); return; } Msg_Body_Struct msgBody = AppMsg.Body; if (msgBody == null) { OnOutputLog(LogInfoType.EROR, "消息内容为NULL!"); SendErrorMsg2App(appToKen, null, "消息内容为NULL!"); return; } if (msgBody.type == ApMsgType.device_test_request) { Send2App_device_test_response(appToKen); return; } if (msgBody.type != ApMsgType.status_response) { OnOutputLog(LogInfoType.INFO, string.Format("处理APP[{0}:{1}]消息({2})!", appToKen.IPAddress.ToString(), appToKen.Port, msgBody.type)); } //心跳消息 if (AppMsg.Body.type == AppMsgType.app_heartbeat_request) { //OnOutputLog(LogInfoType.INFO, "收到心跳消息"); //if (GetMsgIntValueInList(MsgStruct.AllNum, msgBody) != msgBody.dic.Count()) //{ // OnOutputLog(LogInfoType.EROR, string.Format("收到消息里键值对数[{0}]与校验值[{1}]不一至。", // msgBody.dic.Count(), GetMsgIntValueInList(MsgStruct.AllNum, msgBody))); // return; //} appToKen.User = (string)GetMsgStringValueInList("User", msgBody); appToKen.Group = (string)GetMsgStringValueInList("Group", msgBody); appToKen.Domain = (string)GetMsgStringValueInList("Domain", msgBody); MyDeviceList.add(appToKen); Msg_Body_Struct TypeKeyValue = new Msg_Body_Struct(AppMsgType.app_heartbeat_response, "timestamp", DateTime.Now.ToLocalTime().ToString()); SendMsg2App(appToKen, TypeKeyValue); } else if (AppMsg.Body.type == AppMsgType.transparent_msg_request) { //透传消息 OnSendTransparentMsg2Main(appToKen, AppMsg.ApInfo, AppMsg.Body); } else { //将消息转发给Main模块 OnSendMsg2Main(appToKen, AppMsg.ApInfo, AppMsg.Body); } }