mqtt specific components for the impact mbed endpoint library
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp
Diff: MBEDEndpoint.cpp
- Revision:
- 7:8a4a61202b36
- Parent:
- 6:2db2c7e75ad9
- Child:
- 10:d96c57c19611
--- a/MBEDEndpoint.cpp Thu Mar 27 18:14:05 2014 +0000 +++ b/MBEDEndpoint.cpp Fri Mar 28 16:24:22 2014 +0000 @@ -63,7 +63,7 @@ if (success) this->logger()->turnLEDYellow(); if (success)this->m_map = new MBEDToIOCResourceMap(error_handler); if (success) success = this->initializeTransports(); - if (success) success = this->initializeEndpointPersonality(); + if (success) success = this->initializePersonalities(); if (success) this->logger()->turnLEDOrange(); this->logger()->lcdStatusOnly(true); if (!success) closedown(2); @@ -73,7 +73,7 @@ MBEDEndpoint::~MBEDEndpoint() { bool success = true; if (success) this->logger()->turnLEDYellow(); - if (success) success = this->closeEndpointPersonality(); + if (success) success = this->closePersonalities(); if (success) success = this->closeTransports(); if (success) success = this->closeEthernet(); if (success) this->logger()->turnLEDBlue(); @@ -104,10 +104,10 @@ memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1); // look at Light#0 to determine the IOC linkage ID... - char *ioc = this->m_lights[0]->getResourceFactory()->getResourceValue(IOC_LINKAGE_RESOURCE); + char *ioc = this->m_personalities[0]->getResourceFactory()->getResourceValue(EXTERNAL_LINKAGE_RESOURCE); // color our LED depending on whether we have IOC linkage or not... - if (ioc == NULL || strcmp(ioc,IOC_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange(); + if (ioc == NULL || strcmp(ioc,EXTERNAL_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange(); else this->logger()->turnLEDGreen(); sprintf(this->m_lcd_status,"Node: %s\nGW IP: %s\nIOC Link: %s",this->getEndpointName(),this->getGWAddress(),ioc); @@ -115,7 +115,7 @@ } // initialize our personality - bool MBEDEndpoint::initializeEndpointPersonality() { + bool MBEDEndpoint::initializePersonalities() { #ifdef LIGHT_PERSONALITY return this->initializeLights(); #else @@ -125,23 +125,23 @@ // initialize the Lights bool MBEDEndpoint::initializeLights() { - int index = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX); + int index = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX); this->logger()->log("Initializing Lights..."); - for(int i=0;i<NUM_LIGHTS;++i) { - this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this); - this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i])); - this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i])); + for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i) { + this->m_personalities[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this); + this->m_personalities[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_personalities[i])); + this->m_personalities[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_personalities[i])); } return true; } - // does the input name match any of our light resources? - int MBEDEndpoint::indexOfLight(char *name) { + // does the input name match any of our personality instances? + int MBEDEndpoint::indexOfPersonality(char *name) { bool found = false; int index = -1; - for(int i=0;i<NUM_LIGHTS && !found;++i) { - if (strcmp(this->m_lights[i]->getName(),name) == 0) { + for(int i=0;i<NUM_PERSONALITY_INSTANCES && !found;++i) { + if (strcmp(this->m_personalities[i]->getName(),name) == 0) { found = true; index = i; } @@ -152,7 +152,7 @@ // get a specific resources ResourceFactory *MBEDEndpoint::getResources(int index) { - if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources(); + if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->m_personalities[index]->getResourceFactory(); return NULL; } @@ -170,9 +170,9 @@ // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...) void MBEDEndpoint::initEndpointName() { - this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX); - memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1); - sprintf(this->m_endpoint_name,LIGHT_NAME,this->m_instance_id); + this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX); + memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1); + sprintf(this->m_endpoint_name,PERSONALITY_NAME,this->m_instance_id); } // get our endpoint name @@ -242,16 +242,16 @@ return success; } - // load up all endpoints into the IOC - bool MBEDEndpoint::loadEndpoints() { + // load up all personality instances into the IOC + bool MBEDEndpoint::loadPersonalities() { bool success = true; - this->logger()->log("Loading All Endpoints to IOC..."); - for(int i=0;i<NUM_LIGHTS && success;++i) success = this->loadEndpoint(this->m_lights[i]); + this->logger()->log("Loading All Personalities into the IOC..."); + for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->loadPersonality(this->m_personalities[i]); return success; } - // load up our endpoint to the IOC - bool MBEDEndpoint::loadEndpoint(Light *light) { + // load up our personality to the IOC + bool MBEDEndpoint::loadPersonality(Personality *instance) { bool success = false; char result[IOC_RESULT_LEN+1]; char payload[IOC_PAYLOAD_LEN+1]; @@ -261,15 +261,18 @@ memset(payload,0,IOC_PAYLOAD_LEN+1); // DEBUG - this->logger()->log("Building Payload for Light: %s...",light->getName()); + this->logger()->log("Building Payload for Personality(%s): %s...",instance->getType(),instance->getName()); - // build the payload - char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light); + // build the personality payload + char *data = NULL; +#ifdef LIGHT_PERSONALITY + data = this->iocEndpoint()->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance); +#endif // issue the request if (data != NULL && strlen(data) > 0) { // DEBUG - this->logger()->log("Sending Light: %s to the IOC...",light->getName()); + this->logger()->log("Sending Personality(%s): %s to the IOC...",instance->getType(),instance->getName()); // load success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN); @@ -278,37 +281,37 @@ // DEBUG //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName()); - // update the IOC ID if found - if (success) this->iocEndpoint()->saveLightID(light,result); + // update the External ID if found + if (success) this->iocEndpoint()->saveExternalID(instance,result); // DEBUG - if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),light->getIOCID()); + if (success) this->logger()->log("Personality(%s): %s IOC ID=%d sent successfully",instance->getType(),instance->getName(),instance->getExternalID()); // DEBUG if (!success) { - if (light != NULL) this->logger()->log("Light: %s send FAILED",light->getName()); - else this->logger()->log("Light: send FAILED (NULL LIGHT)"); + if (instance != NULL) this->logger()->log("Personality(%s): %s send FAILED",instance->getType(),instance->getName()); + else this->logger()->log("Personality send FAILED: NULL Personality instance"); } // return our status return success; } - // update all endpoints to the IOC - bool MBEDEndpoint::updateEndpoints() { + // update all personality instances into the IOC + bool MBEDEndpoint::updatePersonalities() { bool success = true; - for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i); + for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->updatePersonality(i); return success; } - // update all endpoints to the IOC - bool MBEDEndpoint::updateEndpoints(int index) { - if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]); + // update all personality instances to the IOC + bool MBEDEndpoint::updatePersonality(int index) { + if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->updatePersonality(this->m_personalities[index]); return false; } - // update our endpoint with the IOC - bool MBEDEndpoint::updateEndpoint(Light *light) { + // update our ith personality instance with the IOC + bool MBEDEndpoint::updatePersonality(Personality *instance) { bool success = false; char result[IOC_RESULT_LEN+1]; char payload[IOC_PAYLOAD_LEN+1]; @@ -318,40 +321,35 @@ memset(payload,0,IOC_PAYLOAD_LEN+1); // build the payload - char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light); - + char *data = NULL; + +#ifdef LIGHT_PERSONALITY + data = this->iocEndpoint()->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance); +#endif + // issue the request if (data != NULL && strlen(data) > 0) { // DEBUG - this->logger()->log("Updating Light: %s at the IOC...",light->getName()); + this->logger()->log("Updating Personality(%s): %s at the IOC...",instance->getType(),instance->getName()); // update - success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(light->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN); + success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(instance->getExternalID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN); } // DEBUG - if (success) this->logger()->log("Update of Endpoint to IOC successful"); - else this->logger()->log("Update of Endpoint to IOC FAILED"); + if (success) this->logger()->log("Update of Personality instance to IOC successful"); + else this->logger()->log("Update of Personality instance to IOC FAILED"); // return our status return success; } - // close down our endpoint personality - bool MBEDEndpoint::closeEndpointPersonality() { - #ifdef LIGHT_PERSONALITY - return this->closeLights(); - #else - return NULL; - #endif - } - - // close down the Lights - bool MBEDEndpoint::closeLights() { + // close down our personalities + bool MBEDEndpoint::closePersonalities() { bool success = true; - this->logger()->log("Closing down Lights..."); - for(int i=0;i<NUM_LIGHTS;++i) - if (this->m_lights[i] != NULL) delete this->m_lights[i]; + this->logger()->log("Closing down Personalities..."); + for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i) + if (this->m_personalities[i] != NULL) delete this->m_personalities[i]; return success; }