Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027 C12832 EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed
MQTTTransport.cpp@23:793b2898522e, 2014-02-28 (annotated)
- Committer:
- ansond
- Date:
- Fri Feb 28 02:52:04 2014 +0000
- Revision:
- 23:793b2898522e
- Parent:
- 22:f1002e5993c5
- Child:
- 30:5c670ec5d203
updates
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ansond | 0:ae2a45502448 | 1 | /* Copyright C2013 Doug Anson, MIT License |
| ansond | 0:ae2a45502448 | 2 | * |
| ansond | 0:ae2a45502448 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| ansond | 0:ae2a45502448 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
| ansond | 0:ae2a45502448 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| ansond | 0:ae2a45502448 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| ansond | 0:ae2a45502448 | 7 | * furnished to do so, subject to the following conditions: |
| ansond | 0:ae2a45502448 | 8 | * |
| ansond | 0:ae2a45502448 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| ansond | 0:ae2a45502448 | 10 | * substantial portions of the Software. |
| ansond | 0:ae2a45502448 | 11 | * |
| ansond | 0:ae2a45502448 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| ansond | 0:ae2a45502448 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| ansond | 0:ae2a45502448 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| ansond | 0:ae2a45502448 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| ansond | 0:ae2a45502448 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| ansond | 0:ae2a45502448 | 17 | */ |
| ansond | 0:ae2a45502448 | 18 | |
| ansond | 0:ae2a45502448 | 19 | #include "MQTTTransport.h" |
| ansond | 0:ae2a45502448 | 20 | |
| ansond | 4:11f00b499106 | 21 | // Endpoint Support |
| ansond | 0:ae2a45502448 | 22 | #include "MBEDEndpoint.h" |
| ansond | 0:ae2a45502448 | 23 | |
| ansond | 8:45f9a920e82c | 24 | // EmulatedResourceFactory support |
| ansond | 8:45f9a920e82c | 25 | #include "EmulatedResourceFactory.h" |
| ansond | 8:45f9a920e82c | 26 | |
| ansond | 7:f570eb3f38cd | 27 | // splitstring support |
| ansond | 7:f570eb3f38cd | 28 | #include "splitstring.h" |
| ansond | 0:ae2a45502448 | 29 | |
| ansond | 0:ae2a45502448 | 30 | // our transmitt instance |
| ansond | 15:e44d75d95b38 | 31 | MQTTTransport *_mqtt_instance = NULL; |
| ansond | 0:ae2a45502448 | 32 | |
| ansond | 0:ae2a45502448 | 33 | // MQTT callback to handle received messages |
| ansond | 0:ae2a45502448 | 34 | void _mqtt_message_handler(char *topic,char *payload,unsigned int length) { |
| ansond | 15:e44d75d95b38 | 35 | if (_mqtt_instance != NULL) _mqtt_instance->processMessage(_mqtt_instance->getEndpointNameFromTopic(topic),payload,length); |
| ansond | 0:ae2a45502448 | 36 | } |
| ansond | 0:ae2a45502448 | 37 | |
| ansond | 0:ae2a45502448 | 38 | // our MQTT client endpoint |
| ansond | 0:ae2a45502448 | 39 | PubSubClient _mqtt(MQTT_HOSTNAME,MQTT_HOSTPORT,_mqtt_message_handler); |
| ansond | 0:ae2a45502448 | 40 | |
| ansond | 0:ae2a45502448 | 41 | // default constructor |
| ansond | 15:e44d75d95b38 | 42 | MQTTTransport::MQTTTransport(ErrorHandler *error_handler,void *endpoint,MBEDToIOCResourceMap *map) : Transport(error_handler,endpoint) { |
| ansond | 0:ae2a45502448 | 43 | this->m_mqtt = NULL; |
| ansond | 15:e44d75d95b38 | 44 | _mqtt_instance = this; |
| ansond | 15:e44d75d95b38 | 45 | this->m_map = map; |
| ansond | 8:45f9a920e82c | 46 | this->initTopic(); |
| ansond | 0:ae2a45502448 | 47 | } |
| ansond | 0:ae2a45502448 | 48 | |
| ansond | 0:ae2a45502448 | 49 | // default destructor |
| ansond | 0:ae2a45502448 | 50 | MQTTTransport::~MQTTTransport() { |
| ansond | 0:ae2a45502448 | 51 | this->disconnect(); |
| ansond | 0:ae2a45502448 | 52 | } |
| ansond | 0:ae2a45502448 | 53 | |
| ansond | 8:45f9a920e82c | 54 | // init our topic |
| ansond | 8:45f9a920e82c | 55 | void MQTTTransport::initTopic() { |
| ansond | 8:45f9a920e82c | 56 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
| ansond | 8:45f9a920e82c | 57 | char *endpoint_name = endpoint->getEndpointName(); |
| ansond | 8:45f9a920e82c | 58 | memset(this->m_topic,0,MQTT_IOC_TOPIC_LEN+1); |
| ansond | 8:45f9a920e82c | 59 | sprintf(this->m_topic,MQTT_IOC_TOPIC,endpoint_name); |
| ansond | 8:45f9a920e82c | 60 | } |
| ansond | 8:45f9a920e82c | 61 | |
| ansond | 8:45f9a920e82c | 62 | // get our topic |
| ansond | 8:45f9a920e82c | 63 | char *MQTTTransport::getTopic() { return this->m_topic; } |
| ansond | 8:45f9a920e82c | 64 | |
| ansond | 15:e44d75d95b38 | 65 | // get the IOC <--> MBED resource map |
| ansond | 15:e44d75d95b38 | 66 | MBEDToIOCResourceMap *MQTTTransport::getMap() { return this->m_map; } |
| ansond | 15:e44d75d95b38 | 67 | |
| ansond | 6:34c07e145caa | 68 | // pull the endpoint name from the MQTT topic |
| ansond | 6:34c07e145caa | 69 | char *MQTTTransport::getEndpointNameFromTopic(char *topic) { |
| ansond | 6:34c07e145caa | 70 | memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1); |
| ansond | 8:45f9a920e82c | 71 | splitstring *tmp = new splitstring(topic); |
| ansond | 8:45f9a920e82c | 72 | vector<string> data = tmp->split('/'); |
| ansond | 8:45f9a920e82c | 73 | if (data.size() > 0) { |
| ansond | 8:45f9a920e82c | 74 | char *ep = (char *)data[data.size()-1].c_str(); |
| ansond | 23:793b2898522e | 75 | if (ep != NULL) { |
| ansond | 23:793b2898522e | 76 | if (strcmp(ep,"control") != 0) { |
| ansond | 23:793b2898522e | 77 | // just insert the name and let the parser determine if its for us or not... |
| ansond | 23:793b2898522e | 78 | strncpy(this->m_endpoint_name,ep,strlen(ep)); |
| ansond | 23:793b2898522e | 79 | } |
| ansond | 23:793b2898522e | 80 | else { |
| ansond | 23:793b2898522e | 81 | // this is a broadcast message - so we need to process it |
| ansond | 23:793b2898522e | 82 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
| ansond | 23:793b2898522e | 83 | char *endpoint_name = endpoint->getEndpointName(); |
| ansond | 23:793b2898522e | 84 | strcpy(this->m_endpoint_name,endpoint_name); |
| ansond | 23:793b2898522e | 85 | } |
| ansond | 23:793b2898522e | 86 | } |
| ansond | 8:45f9a920e82c | 87 | } |
| ansond | 8:45f9a920e82c | 88 | if (tmp != NULL) delete tmp; |
| ansond | 6:34c07e145caa | 89 | return this->m_endpoint_name; |
| ansond | 6:34c07e145caa | 90 | } |
| ansond | 6:34c07e145caa | 91 | |
| ansond | 0:ae2a45502448 | 92 | // process a MQTT Message |
| ansond | 6:34c07e145caa | 93 | void MQTTTransport::processMessage(char *message_name,char *payload, unsigned int len) { |
| ansond | 7:f570eb3f38cd | 94 | char *message_type = ""; |
| ansond | 7:f570eb3f38cd | 95 | char *message_verb = ""; |
| ansond | 7:f570eb3f38cd | 96 | char *message_value = ""; |
| ansond | 23:793b2898522e | 97 | |
| ansond | 6:34c07e145caa | 98 | // get our endpoint |
| ansond | 0:ae2a45502448 | 99 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
| ansond | 7:f570eb3f38cd | 100 | char *endpoint_name = endpoint->getEndpointName(); |
| ansond | 6:34c07e145caa | 101 | |
| ansond | 0:ae2a45502448 | 102 | // DEBUG |
| ansond | 8:45f9a920e82c | 103 | //this->logger()->log("Endpoint:[%s] Target: [%s]",endpoint_name,message_name); |
| ansond | 7:f570eb3f38cd | 104 | |
| ansond | 3:3db076b9d380 | 105 | // only respond if its for our node |
| ansond | 8:45f9a920e82c | 106 | if (strcmp(endpoint_name,message_name) == 0) { |
| ansond | 7:f570eb3f38cd | 107 | // format of the MQTT message: message_type:verb|Parameter_X:value|keyword:optional_data |
| ansond | 7:f570eb3f38cd | 108 | splitstring *tmp = new splitstring(payload); |
| ansond | 7:f570eb3f38cd | 109 | vector<string> data = tmp->split(':'); |
| ansond | 7:f570eb3f38cd | 110 | if (data.size() > 0) message_type = (char *)data[0].c_str(); |
| ansond | 7:f570eb3f38cd | 111 | if (data.size() > 1) message_verb = (char *)data[1].c_str(); |
| ansond | 7:f570eb3f38cd | 112 | if (data.size() > 2) message_value = (char *)data[2].c_str(); |
| ansond | 7:f570eb3f38cd | 113 | |
| ansond | 7:f570eb3f38cd | 114 | // DEBUG |
| ansond | 15:e44d75d95b38 | 115 | //this->logger()->log("Raw Payload: %s",payload); |
| ansond | 8:45f9a920e82c | 116 | //this->logger()->log("Type: %s Name: %s Verb: %s Value: %s",message_type,message_name,message_verb,message_value); |
| ansond | 8:45f9a920e82c | 117 | |
| ansond | 3:3db076b9d380 | 118 | // load endpoints |
| ansond | 15:e44d75d95b38 | 119 | if (message_type != NULL && strcmp(message_type,IOC_ENDPOINT_VERB) == 0) { // Endpoint |
| ansond | 15:e44d75d95b38 | 120 | if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_LOAD_ALL_VERB) == 0) { // load |
| ansond | 15:e44d75d95b38 | 121 | if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) { // all |
| ansond | 15:e44d75d95b38 | 122 | // load up our endpoints |
| ansond | 15:e44d75d95b38 | 123 | endpoint->loadEndpoints(); |
| ansond | 0:ae2a45502448 | 124 | } |
| ansond | 0:ae2a45502448 | 125 | } |
| ansond | 15:e44d75d95b38 | 126 | |
| ansond | 15:e44d75d95b38 | 127 | else if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) { // update |
| ansond | 15:e44d75d95b38 | 128 | if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) { // all |
| ansond | 15:e44d75d95b38 | 129 | // update our endpoints |
| ansond | 23:793b2898522e | 130 | endpoint->updateEndpoints(); |
| ansond | 15:e44d75d95b38 | 131 | } |
| ansond | 15:e44d75d95b38 | 132 | else { |
| ansond | 15:e44d75d95b38 | 133 | // update just our endpoint |
| ansond | 15:e44d75d95b38 | 134 | int index = -1; |
| ansond | 15:e44d75d95b38 | 135 | if (message_name != NULL) { |
| ansond | 15:e44d75d95b38 | 136 | index = endpoint->indexOfLight((char *)message_name); |
| ansond | 15:e44d75d95b38 | 137 | if (index >= 0) { |
| ansond | 15:e44d75d95b38 | 138 | if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) { |
| ansond | 15:e44d75d95b38 | 139 | // update our endpoint |
| ansond | 23:793b2898522e | 140 | endpoint->updateEndpoints(index); |
| ansond | 15:e44d75d95b38 | 141 | } |
| ansond | 3:3db076b9d380 | 142 | } |
| ansond | 3:3db076b9d380 | 143 | } |
| ansond | 3:3db076b9d380 | 144 | } |
| ansond | 3:3db076b9d380 | 145 | } |
| ansond | 0:ae2a45502448 | 146 | } |
| ansond | 3:3db076b9d380 | 147 | |
| ansond | 3:3db076b9d380 | 148 | // change a resource value |
| ansond | 13:25448d92c205 | 149 | if (message_type != NULL && strcmp(message_type,IOC_CHANGE_VERB) == 0) { |
| ansond | 8:45f9a920e82c | 150 | if (message_name != NULL) { |
| ansond | 3:3db076b9d380 | 151 | // destined for our lights? |
| ansond | 3:3db076b9d380 | 152 | int index = endpoint->indexOfLight((char *)message_name); |
| ansond | 3:3db076b9d380 | 153 | if (index >= 0) { |
| ansond | 3:3db076b9d380 | 154 | if (message_verb != NULL) { |
| ansond | 3:3db076b9d380 | 155 | // map the parameter to one of ours |
| ansond | 14:0a6497a380a4 | 156 | char *mapped_resource = this->mapIOCResourceToEndpointResource((char *)message_verb); |
| ansond | 3:3db076b9d380 | 157 | if (mapped_resource != NULL) { |
| ansond | 3:3db076b9d380 | 158 | if (message_value != NULL) { |
| ansond | 8:45f9a920e82c | 159 | EmulatedResourceFactory *factory = (EmulatedResourceFactory *)endpoint->getResources(index); |
| ansond | 8:45f9a920e82c | 160 | bool success = factory->setResourceValue(mapped_resource,message_value); |
| ansond | 8:45f9a920e82c | 161 | |
| ansond | 8:45f9a920e82c | 162 | // end the resource value back over MQTT |
| ansond | 8:45f9a920e82c | 163 | this->sendResult(message_name,message_verb,message_value,success); |
| ansond | 3:3db076b9d380 | 164 | } |
| ansond | 3:3db076b9d380 | 165 | } |
| ansond | 3:3db076b9d380 | 166 | } |
| ansond | 3:3db076b9d380 | 167 | } |
| ansond | 3:3db076b9d380 | 168 | } |
| ansond | 3:3db076b9d380 | 169 | } |
| ansond | 3:3db076b9d380 | 170 | |
| ansond | 3:3db076b9d380 | 171 | // get a resource value |
| ansond | 13:25448d92c205 | 172 | if (message_type != NULL && strcmp(message_type,IOC_REQUEST_VALUE_VERB) == 0) { |
| ansond | 3:3db076b9d380 | 173 | if (message_name != NULL) { |
| ansond | 3:3db076b9d380 | 174 | // destined for our lights? |
| ansond | 3:3db076b9d380 | 175 | int index = endpoint->indexOfLight((char *)message_name); |
| ansond | 3:3db076b9d380 | 176 | if (index >= 0) { |
| ansond | 3:3db076b9d380 | 177 | if (message_verb != NULL) { |
| ansond | 3:3db076b9d380 | 178 | // map the parameter to one of ours |
| ansond | 13:25448d92c205 | 179 | char *mapped_resource = this->mapIOCResourceToEndpointResource((char *)message_verb); |
| ansond | 3:3db076b9d380 | 180 | if (mapped_resource != NULL) { |
| ansond | 8:45f9a920e82c | 181 | EmulatedResourceFactory *factory = (EmulatedResourceFactory *)endpoint->getResources(index); |
| ansond | 8:45f9a920e82c | 182 | message_value = factory->getResourceValue((char *)mapped_resource); |
| ansond | 8:45f9a920e82c | 183 | bool success = false; if (message_value != NULL) success = true; |
| ansond | 8:45f9a920e82c | 184 | |
| ansond | 8:45f9a920e82c | 185 | // log resource get |
| ansond | 8:45f9a920e82c | 186 | if (success) this->logger()->log("Resource: %s (%s) Value: %s",message_verb,mapped_resource,message_value); |
| ansond | 3:3db076b9d380 | 187 | |
| ansond | 3:3db076b9d380 | 188 | // end the resource value back over MQTT |
| ansond | 8:45f9a920e82c | 189 | this->sendResult(message_name,message_verb,message_value,success); |
| ansond | 3:3db076b9d380 | 190 | } |
| ansond | 3:3db076b9d380 | 191 | } |
| ansond | 3:3db076b9d380 | 192 | } |
| ansond | 0:ae2a45502448 | 193 | } |
| ansond | 0:ae2a45502448 | 194 | } |
| ansond | 7:f570eb3f38cd | 195 | |
| ansond | 7:f570eb3f38cd | 196 | // clean up |
| ansond | 8:45f9a920e82c | 197 | if (tmp != NULL) delete tmp; |
| ansond | 0:ae2a45502448 | 198 | } |
| ansond | 3:3db076b9d380 | 199 | else { |
| ansond | 3:3db076b9d380 | 200 | // message not bound for our node |
| ansond | 3:3db076b9d380 | 201 | this->logger()->log("MQTT Message: %s not for us: %s... ignoring...",payload,endpoint_name); |
| ansond | 7:f570eb3f38cd | 202 | } |
| ansond | 0:ae2a45502448 | 203 | } |
| ansond | 0:ae2a45502448 | 204 | |
| ansond | 8:45f9a920e82c | 205 | // send result back to MQTT |
| ansond | 8:45f9a920e82c | 206 | void MQTTTransport::sendResult(char *endpoint_name,char *resource_name,char *value,bool success) { |
| ansond | 8:45f9a920e82c | 207 | if (this->m_connected == true) { |
| ansond | 8:45f9a920e82c | 208 | // send the response back to MQTT |
| ansond | 8:45f9a920e82c | 209 | this->logger()->log("Sending Response back to MQTT..."); |
| ansond | 8:45f9a920e82c | 210 | char message[MAX_MQTT_MESSAGE_LENGTH+1]; |
| ansond | 8:45f9a920e82c | 211 | memset(message,0,MAX_MQTT_MESSAGE_LENGTH+1); |
| ansond | 13:25448d92c205 | 212 | char *str_success = IOC_RESPONSE_OK; if (!success) str_success = IOC_RESPONSE_FAILED; |
| ansond | 13:25448d92c205 | 213 | sprintf(message,IOC_RESPONSE_TEMPLATE,IOC_RESPONSE_VERB,endpoint_name,resource_name,value,str_success); |
| ansond | 8:45f9a920e82c | 214 | bool sent = this->m_mqtt->publish(this->getTopic(),message,strlen(message)); |
| ansond | 8:45f9a920e82c | 215 | if (sent) { |
| ansond | 8:45f9a920e82c | 216 | this->logger()->log("Result sent successfully"); |
| ansond | 8:45f9a920e82c | 217 | this->logger()->blinkMQTTTransportTxLED(); |
| ansond | 8:45f9a920e82c | 218 | } |
| ansond | 8:45f9a920e82c | 219 | else { |
| ansond | 8:45f9a920e82c | 220 | this->logger()->log("Result send FAILED"); |
| ansond | 8:45f9a920e82c | 221 | } |
| ansond | 8:45f9a920e82c | 222 | } |
| ansond | 8:45f9a920e82c | 223 | else { |
| ansond | 8:45f9a920e82c | 224 | // unable to send the response |
| ansond | 8:45f9a920e82c | 225 | this->logger()->log("Unable to send response back to MQTT. Not connected."); |
| ansond | 8:45f9a920e82c | 226 | } |
| ansond | 0:ae2a45502448 | 227 | } |
| ansond | 0:ae2a45502448 | 228 | |
| ansond | 15:e44d75d95b38 | 229 | char *MQTTTransport::mapIOCResourceToEndpointResource(char *ioc_name) { return this->getMap()->iocNameToEndpointName(ioc_name); } |
| ansond | 0:ae2a45502448 | 230 | |
| ansond | 0:ae2a45502448 | 231 | char *MQTTTransport::makeID(char *id_template,char *buffer) { |
| ansond | 0:ae2a45502448 | 232 | srand(time(0)); |
| ansond | 0:ae2a45502448 | 233 | srand(rand()); |
| ansond | 0:ae2a45502448 | 234 | sprintf(buffer,id_template,rand()%MQTT_MAXID_VALUE); |
| ansond | 0:ae2a45502448 | 235 | return buffer; |
| ansond | 0:ae2a45502448 | 236 | } |
| ansond | 0:ae2a45502448 | 237 | |
| ansond | 0:ae2a45502448 | 238 | // connect up MQTT |
| ansond | 0:ae2a45502448 | 239 | bool MQTTTransport::connect() { |
| ansond | 0:ae2a45502448 | 240 | char mqtt_id[MQTT_ENDPOINT_IDLEN+1]; |
| ansond | 0:ae2a45502448 | 241 | memset(mqtt_id,0,(MQTT_ENDPOINT_IDLEN+1)); |
| ansond | 0:ae2a45502448 | 242 | if (this->m_connected == false) { |
| ansond | 0:ae2a45502448 | 243 | this->logger()->log("MQTT Init: %s:%d...",MQTT_HOSTNAME,MQTT_HOSTPORT); |
| ansond | 0:ae2a45502448 | 244 | this->m_mqtt = &_mqtt; |
| ansond | 0:ae2a45502448 | 245 | if (this->m_mqtt != NULL) { |
| ansond | 0:ae2a45502448 | 246 | char *id = this->makeID(MQTT_ENDPOINT_ID,mqtt_id); |
| ansond | 0:ae2a45502448 | 247 | this->logger()->log("MQTT Connect: ID: %s...",id); |
| ansond | 0:ae2a45502448 | 248 | if (this->m_mqtt->connect(id)) { |
| ansond | 8:45f9a920e82c | 249 | this->logger()->log("MQTT Subscribe: Topic: %s...",this->getTopic()); |
| ansond | 8:45f9a920e82c | 250 | if (this->m_mqtt->subscribe(this->getTopic())) { |
| ansond | 22:f1002e5993c5 | 251 | if (this->m_mqtt->subscribe(MQTT_IOC_ANNOUNCE_TOPIC)) { |
| ansond | 22:f1002e5993c5 | 252 | this->logger()->log("MQTT CONNECTED."); |
| ansond | 22:f1002e5993c5 | 253 | this->m_connected = true; |
| ansond | 22:f1002e5993c5 | 254 | } |
| ansond | 22:f1002e5993c5 | 255 | else { |
| ansond | 22:f1002e5993c5 | 256 | this->logger()->log("MQTT Subscribe: Topic(ANNOUNCE): %s FAILED",this->getTopic()); |
| ansond | 22:f1002e5993c5 | 257 | this->logger()->turnLEDRed(); |
| ansond | 22:f1002e5993c5 | 258 | this->m_connected = false; |
| ansond | 22:f1002e5993c5 | 259 | } |
| ansond | 0:ae2a45502448 | 260 | } |
| ansond | 0:ae2a45502448 | 261 | else { |
| ansond | 8:45f9a920e82c | 262 | this->logger()->log("MQTT Subscribe: Topic: %s FAILED",this->getTopic()); |
| ansond | 0:ae2a45502448 | 263 | this->logger()->turnLEDRed(); |
| ansond | 7:f570eb3f38cd | 264 | this->m_connected = false; |
| ansond | 0:ae2a45502448 | 265 | } |
| ansond | 0:ae2a45502448 | 266 | } |
| ansond | 0:ae2a45502448 | 267 | else { |
| ansond | 0:ae2a45502448 | 268 | this->logger()->log("MQTT Connect: ID: %s FAILED",id); |
| ansond | 0:ae2a45502448 | 269 | this->logger()->turnLEDRed(); |
| ansond | 7:f570eb3f38cd | 270 | this->m_connected = false; |
| ansond | 0:ae2a45502448 | 271 | } |
| ansond | 0:ae2a45502448 | 272 | } |
| ansond | 0:ae2a45502448 | 273 | else { |
| ansond | 0:ae2a45502448 | 274 | this->logger()->log("MQTT Unable to allocate new instance"); |
| ansond | 0:ae2a45502448 | 275 | this->logger()->turnLEDRed(); |
| ansond | 7:f570eb3f38cd | 276 | this->m_connected = false; |
| ansond | 0:ae2a45502448 | 277 | } |
| ansond | 0:ae2a45502448 | 278 | } |
| ansond | 0:ae2a45502448 | 279 | else { |
| ansond | 0:ae2a45502448 | 280 | this->logger()->log("MQTT already connected (OK)"); |
| ansond | 0:ae2a45502448 | 281 | } |
| ansond | 0:ae2a45502448 | 282 | return this->m_connected; |
| ansond | 0:ae2a45502448 | 283 | } |
| ansond | 0:ae2a45502448 | 284 | |
| ansond | 0:ae2a45502448 | 285 | // disconnect from MQTT |
| ansond | 0:ae2a45502448 | 286 | bool MQTTTransport::disconnect() { |
| ansond | 0:ae2a45502448 | 287 | if (this->m_mqtt != NULL) { |
| ansond | 8:45f9a920e82c | 288 | this->logger()->log("MQTT Unsubscribing from: %s...",this->getTopic()); |
| ansond | 8:45f9a920e82c | 289 | this->m_mqtt->unsubscribe(this->getTopic()); |
| ansond | 22:f1002e5993c5 | 290 | this->m_mqtt->unsubscribe(MQTT_IOC_ANNOUNCE_TOPIC); |
| ansond | 0:ae2a45502448 | 291 | this->logger()->log("MQTT Disconnecting..."); |
| ansond | 0:ae2a45502448 | 292 | this->m_mqtt->disconnect(); |
| ansond | 0:ae2a45502448 | 293 | } |
| ansond | 0:ae2a45502448 | 294 | else { |
| ansond | 0:ae2a45502448 | 295 | this->logger()->log("MQTT already disconnected (OK)"); |
| ansond | 0:ae2a45502448 | 296 | } |
| ansond | 0:ae2a45502448 | 297 | this->m_connected = false; |
| ansond | 0:ae2a45502448 | 298 | return true; |
| ansond | 0:ae2a45502448 | 299 | } |
| ansond | 0:ae2a45502448 | 300 | |
| ansond | 0:ae2a45502448 | 301 | // check transport and process stuff |
| ansond | 0:ae2a45502448 | 302 | void MQTTTransport::checkAndProcess() { |
| ansond | 0:ae2a45502448 | 303 | if (this->m_mqtt != NULL && this->m_connected == true) { |
| ansond | 0:ae2a45502448 | 304 | this->m_mqtt->loop(); |
| ansond | 0:ae2a45502448 | 305 | this->logger()->blinkMQTTTransportRxLED(); |
| ansond | 0:ae2a45502448 | 306 | } |
| ansond | 0:ae2a45502448 | 307 | } |