// ChangeFanRepresentaion is an observation function, // which notifies any changes to the resource to stack // via notifyObservers void *ChangeFanRepresentation (void *param) { FanResource *fanPtr = (FanResource *) param; // This function continuously monitors for the changes while (1) { sleep (5); if (gObservation) { // If under observation if there are any changes to the fan resource // we call notifyObservors // // For demostration we are changing the power value and notifying. fanPtr->m_power += 10; cout << "\nPower updated to : " << fanPtr->m_power << endl; cout << "Notifying observers with resource handle: " << fanPtr->getHandle() << endl; OCStackResult result = OC_STACK_OK; if (isListOfObservers) { std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse()); resourceResponse->setErrorCode(200); resourceResponse->setResourceRepresentation(fanPtr->get(), DEFAULT_INTERFACE); result = OCPlatform::notifyListOfObservers( fanPtr->getHandle(), fanPtr->m_interestedObservers, resourceResponse); } else { result = OCPlatform::notifyAllObservers(fanPtr->getHandle()); } if (OC_STACK_NO_OBSERVERS == result) { cout << "No More observers, stopping notifications" << endl; gObservation = 0; } } } return NULL; }
void * handleSlowResponse(void *param, std::shared_ptr< OCResourceRequest > pRequest) { // This function handles slow response case FanResource* fanPtr = (FanResource*) param; // Induce a case for slow response by using sleep std::cout << "SLOW response" << std::endl; sleep(10); auto pResponse = std::make_shared< OC::OCResourceResponse >(); pResponse->setRequestHandle(pRequest->getRequestHandle()); pResponse->setResourceHandle(pRequest->getResourceHandle()); pResponse->setResourceRepresentation(fanPtr->get()); pResponse->setErrorCode(200); pResponse->setResponseResult(OC_EH_OK); // Set the slow response flag back to false isSlowResponse = false; OCPlatform::sendResponse(pResponse); return NULL; }