Mqtt协议IOS端移植2
MqttFramework.h
#import <Foundation/Foundation.h> #import "MQTTClient.h" #import "BusinessModuleProtocol.h" #import "BusinessFramework.h" #import "AppDelegate.h" @interface MqttFramework : NSObject<MQTTDelegate,BusinessModuleProtocol> { BusinessFramework *businessFrameworks_; } /** * @brief 单例模式的设置 * * @param [in] N/A * @param [out] N/A * @return void * @note */ +(MqttFramework *)getMQttFrameInstance; /** * @brief 给mqttserver发送消息 临时未做使用 待扩展 * * @param [in] N/A * @param [out] N/A * @return void * @note */ +(void)publishMessage:(NSString *)message withTopicType:(NSString *)topicType; /** * @brief 请求的结果处理 * * @param [in] N/A * @param [out] N/A * @return void * @note */ -(void)requestResult:(NSString *)topic withData:(id)resultData; /** * @brief 定义mqtt消息主题 * * @param [in] N/A * @param [out] N/A * @return void * @note */ -(void)productTheme:(NSString *)theme; /** * @brief 重连mqttserver * * @param [in] N/A * @param [out] N/A * @return void * @note */ -(void)reconnectMqtt; @end
MqttFramework.m
#import "MqttFramework.h" #import "MQTTClient.h" #import "XmlAdept.h" #import "ASIFormDataRequest.h" @implementation MqttFramework /** * @brief 得到模块控制器的句柄单例 * * @param [in] N/A * @param [out] N/A * @return void * @note */ static MqttFramework *mqttInstance = nil; +(MqttFramework*)getMQttFrameInstance { @synchronized(self) { if(mqttInstance==nil) { mqttInstance=[[self alloc]init]; } } return mqttInstance; } #pragma --mark BusinessModuleProtocol delegate - (int)initBusinessModule:(BusinessModuleInfo*)info { info.businessModuleIdEX = @"MqttFrameWork";// businessFrameworks_ = info.businessFramework; return 0; } - (int)callBusinessProcessEX:(NSString *)capabilityId withMessageType:(NSString *)MessageType WithMessage:(id)messageVector { NSLog(@"capabilityId = %@ topic = %@ messageVector = %@",capabilityId,MessageType,messageVector); AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; MQTTClient *mosq = [app mosquittoClient]; // [mosq setHost:Mqtt_URL]; // [mosq connect]; // [mosq subscribe:@"nanode/red_led"]; // [mosq subscribe:@"nanode/green_led"]; // [mosq subscribe:@"iCombo"]; // [mosq subscribe:@"iPhone"]; //1.组装xml工作 //2.发送请求消息 //在此正式调用mqtt请求 NSString *sendStr = [[NSString alloc] initWithData:[XmlAdept makeMqttXML:MessageType withDictionary:(NSDictionary *)messageVector] encoding:NSUTF8StringEncoding]; // /****************去掉最后换行符********************/ int index = sendStr.length-1; sendStr = [sendStr substringToIndex:index]; // /*********************end*************************/ // ASIFormDataRequest *formDataRequest = [ASIFormDataRequest requestWithURL:nil]; //NSString *messageEx = [formDataRequest encodeURL:sendStr]; // NSString *messageEx = [formDataRequest encodeURL:@"我家"]; //[mosq publishString:sendStr toTopic:@"iCombo" retain:YES]; // NSLog(@"%@",messageEx); NSString *string = [NSString stringWithFormat:@"**%@**",sendStr]; NSLog(@"senderMesg = %@",string); [mosq publishString:sendStr toTopic:@"iCombo" retain:NO]; return 0; } -(void)requestResult:(NSString *)topic withData:(id)resultData { NSLog(@"发送业务广播 给监听者"); [businessFrameworks_ broadcastBusinessNotifyEX:topic withInParam:resultData]; } /** * @brief 定义mqtt消息主题 * * @param [in] N/A * @param [out] N/A * @return void * @note */ -(void)productTheme:(NSString *)theme { AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; MQTTClient *mosq = [app mosquittoClient]; NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; if ([[ud objectForKey:@"host"] isEqualToString:@"no"]) { [mosq setHost:Mqtt_URL]; } else { [mosq setHost:[ud stringForKey:@"host"]]; } //[mosq setHost:Mqtt_URL]; [mosq connect]; [mosq subscribe:theme]; } /** * @brief 重连mqttserver * * @param [in] N/A * @param [out] N/A * @return void * @note */ -(void)reconnectMqtt { AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; MQTTClient *mosq = [app mosquittoClient]; NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; if ([ud stringForKey:@"host"]) { [mosq setHost:[ud stringForKey:@"host"]]; } else { [mosq setHost:Mqtt_URL]; } [mosq reconnect]; } #pragma --mark mosquittoclientDelegate - (void) didConnect:(NSUInteger)code { if (code == 0) { NSLog(@"连接本地mosquito代理server返回码为:%d 连接mqtt成功",code); } else { NSLog(@"连接Mqttserver失败"); } } - (void) didDisconnect { NSLog(@"mqtt disconnect!"); } //处理各个主题相应的message - (void) didReceiveMessage: (NSString*)message topic:(NSString*)topic { NSLog(@"%@ => %@", topic, message); //deal with diffrent message here: //if else 分支处理 //1.xml的解析工作 //将收到的消息处理后然后广播出去 接收者会依据主题处理自己的业务 NSArray *resultArr = [[NSArray alloc]initWithObjects:message, nil]; NSArray *list = [XmlAdept mqttParseMessageNode:resultArr]; NSString *type = [[list objectAtIndex:0] objectForKey:@"type"]; [self requestResult:type withData:list]; } //publish后回调的messageId - (void) didPublish: (NSUInteger)messageId { NSLog(@"send messageId = %d",messageId); } //定制一个主题 - (void) didSubscribe: (NSUInteger)messageId grantedQos:(NSArray*)qos { NSLog(@"messageId = %d grantedQos = %@",messageId,qos); } //取消一个主题 - (void) didUnsubscribe: (NSUInteger)messageId { NSLog(@"unsubscibe messageId = %d",messageId); } /** * @brief 给mqttserver发送消息 对外接口 * * @param [in] N/A * @param [out] N/A * @return void * @note */ +(void)publishMessage:(NSString *)message withTopicType:(NSString *)topicType { AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; MQTTClient *mosq = [app mosquittoClient]; [mosq publishString:message toTopic:topicType retain:NO]; //消息的retain类型设置为NO让其不在消息缓冲池中保留 } -(void)dealloc { [super dealloc]; } @end