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 The settings of the singleton mode * * @param [in] n/A * @param [out] n/A * @return void * @note */+ (mqttframework *) Getmqttframeinstanc e;/** * @brief send a message to the MQTT server temporarily unused to expand * * @param [in] n/A * @param [out] n/A * @return void * @note */+ (void) publishmes Sage: (NSString *) message Withtopictype: (NSString *) topictype;/** * @brief The result of the request processing * * @param [in] n/A * @param [out] n/A * @return void * @note */-(void) Requestresult: (NSString *) topic Withdata: (ID) resultdata;/** * @brief define MQTT Message subject * * @par AM [in] n/A * @param [out] n/A * @return void * @note */-(void) Producttheme: (NSString *) theme;/** * @brief re-connect MQTT server * @ 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 Get the handle of the module controller single example * @param [in] n/A * @param [out] n/A * @return void * @note */static Mqttframewo RK *mqttinstance = nil;+ (mqttframework*) getmqttframeinstance{@synchronized (self) {if (Mqttinstance==nil) { Mqttinstance=[[self Alloc]init]; }}return mqttinstance;} #pragma--mark businessmoduleprotocol delegate-(int) Initbusinessmodule: (businessmoduleinfo*) info{info.business Moduleidex = @ "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]; [Mosqsethost:mqtt_url];//[Mosq connect];//[mosq subscribe:@ "nanode/red_led"];//[mosq subscribe:@ "nanode/green_led"];/ /[Mosq subscribe:@ "Icombo"];//[mosq subscribe:@ "IPhone"]; 1. Assemble XML work//2. Send a request message///This formally calls the MQTT request NSString *sendstr = [[NSString alloc] initwithdata:[xmladept makemqttx Ml:messagetype withdictionary: (Nsdictionary *) messagevector] encoding:nsutf8stringencoding]; /**************** remove last line break ********************/int index = sendstr.length-1; SENDSTR = [Sendstr substringtoindex:index]; /*********************end*************************///Asiformdatarequest *formdatarequest = [ASIFormDataRequest Requestwithurl:nil]; NSString *messageex = [Formdatarequest encodeurl:sendstr];//nsstring *messageex = [Formdatarequest encodeURL:@ "my Home"]; [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 (@ "send business broadcast to listener"); [Businessframeworks_ broadcastbusinessnotifyex:topic withinparam:resultdata];} /** * @brief define MQTT Message Subject * * @param [in] n/A * @param [out] n/A * @return void * @note */-(void) Producttheme: (NSString *) th eme{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 re-connect MQTT server * * @param [in] n/A * @param [out] n/A * @return void * @note */-(void) reconnectmqtt{Appdelegat E *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 (@ "Connect local Mosquito Proxy server return code:%d connection mqtt successful ", code); } else {NSLog (@ "Connection to MQTT Server failed"); }}-(void) Diddisconnect{nslog (@ "Mqtt disconnect!");} Handles message-(void) didreceivemessage for each topic: (nsstring*) message topic: (nsstring*) Topic{nslog (@ "%@ =%@", topic, message); Deal with diffrent message here://if Else Branch handles parsing of the//1.xml//the received message is processed and then broadcast out the recipient will handle their business according to the subject 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 callback after messageid-(void) Didpublish: (Nsuinteger) messageid{NSLog (@ "Send messageId =%d", messageId);} Customizing a Theme-(void) Didsubscribe: (Nsuinteger) messageId Grantedqos: (nsarray*) qos{NSLog (@ "messageId =%d Grantedqos =%@", Messageid,qos);} Cancel a topic-(void) Didunsubscribe: (Nsuinteger) messageid{NSLog (@ "Unsubscibe messageId =%d", messageId);} /** * @brief send a message to the MQTT server external interface * * @param [in] n/A * @param [out] n/A * @return void * @note */+ (void) PublishMessage: (NS String *) message Withtopictype: (NSString *) topictype{appdelegate *app = (appdelegate *) [[UIApplication Sharedapplic Ation] delegate]; Mqttclient *mosq = [app mosquittoclient]; [Mosq publishstring:message Totopic:topictype Retain:no]; The retain type of the message is set to no so that it is not retained in the message buffer pool}-(void) dealloc{[super Dealloc];} @end