void *ChangeLightRepresentation(void *param) { cout << "ChangeLigthRepresentation Enter\n"; while (1) { cout << "pthread_cond_wait\n"; pthread_cond_wait(&m_cond, &m_mutex); cout << "pthread_cond_start\n"; if (g_Observation) { cout << endl; cout << "========================================================" << endl; cout << "HUMTepm updated to : " << myResource.m_temp << endl; cout << "Notifying observers with resource handle: " << myResource.getHandle() << endl; cout << endl; cout << "========================================================" << endl; cout << "Send data : \n"; cout << "Attribute Name: Temp\tvalue: " << myResource.m_temp << endl; cout << "Attribute Name: Humid\tvalue: " << myResource.m_humid << endl; OCStackResult result = OCPlatform::notifyAllObservers(myResource.getHandle()); cout << "Notify Success\n"; if (OC_STACK_NO_OBSERVERS == result) { cout << "No More observers, stopping notifications" << endl; g_Observation = 0; } } cout << "ChangeLigthRepresentation Out\n"; } return NULL; }
int main() { PlatformConfig cfg { OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0", 0, OC::QualityOfService::LowQos }; OCPlatform::Configure(cfg); int number = 0; try { startPresence(30); myResource.createResource(); signal(SIGINT, handleSigInt); while (true) { bool end = false; cout << endl; cout << "========================================================" << endl; cout << "1. Temp is up" << endl; cout << "2. Temp is down" << endl; cout << "3. This Program will be ended." << endl; cout << "========================================================" << endl; cin >> number; if(std::cin.fail()) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "Invalid input type, please try again" << std::endl; continue; } switch (number) { case 1: { cout << "Temp is up!" << endl; myResource.m_temp += 10; pthread_cond_signal(&m_cond); cout << "ChangeLightRepresentation Done!" << endl; break; } case 2: { cout << "Temp is down!" << endl; myResource.m_temp -= 10; pthread_cond_signal(&m_cond); cout << "ChangeLightRepresentation Done!" << endl; break; } case 3: { cout << "Bye!" << endl; stopPresence(); end = true; quitProcess(); break; } default: { cout << "Invalid input. Please try again." << endl; break; } } if (end == true) { break; } } } catch (exception &e) { cout << "main exception : " << e.what() << endl; } }
OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request) { cout << "Sample Provider entityHandler\n"; OCEntityHandlerResult ehResult = OC_EH_ERROR; if (request) { cout << "flag : request\n"; std::string requestType = request->getRequestType(); int requestFlag = request->getRequestHandlerFlag(); if (requestFlag == RequestHandlerFlag::RequestFlag) { cout << "\t\trequestFlag : Request\n"; auto pResponse = std::make_shared<OC::OCResourceResponse>(); pResponse->setRequestHandle(request->getRequestHandle()); pResponse->setResourceHandle(request->getResourceHandle()); if (requestType == "GET") { cout << "\t\trequestType : GET\n"; pResponse->setErrorCode(200); pResponse->setResponseResult(OC_EH_OK); pResponse->setResourceRepresentation(myResource.get()); if (OC_STACK_OK == OCPlatform::sendResponse(pResponse)) { ehResult = OC_EH_OK; } } else if (requestType == "PUT") { cout << "\t\t\trequestType : PUT\n"; OCRepresentation rep = request->getResourceRepresentation(); myResource.put(rep); if (pResponse) { pResponse->setErrorCode(200); pResponse->setResourceRepresentation(myResource.get()); } if (OC_STACK_OK == OCPlatform::sendResponse(pResponse)) { ehResult = OC_EH_OK; } else { cout << "put request Error\n"; } } else if (requestType == "POST") { cout << "\t\t\trequestType : POST\n"; } else if (requestType == "DELETE") { cout << "\t\trequestType : DELETE\n"; cout << "DeviceResource Delete Request" << std::endl; if (myResource.deleteDeviceResource() == OC_STACK_OK) { cout << "\tSuccess DELETE\n"; pResponse->setErrorCode(200); pResponse->setResponseResult(OC_EH_RESOURCE_DELETED); ehResult = OC_EH_OK; } else { pResponse->setResponseResult(OC_EH_ERROR); ehResult = OC_EH_ERROR; } OCPlatform::sendResponse(pResponse); } } if (requestFlag & RequestHandlerFlag::ObserverFlag) { pthread_t threadId; ObservationInfo observationInfo = request->getObservationInfo(); if (ObserveAction::ObserveRegister == observationInfo.action) { myResource.m_interestedObservers.push_back(observationInfo.obsId); } else if (ObserveAction::ObserveUnregister == observationInfo.action) { myResource.m_interestedObservers.erase(std::remove( myResource.m_interestedObservers.begin(), myResource.m_interestedObservers.end(), observationInfo.obsId), myResource.m_interestedObservers.end()); } cout << request->getResourceUri() << endl; cout << request->getResourceRepresentation().getUri() << endl; cout << "========================================================" << endl; cout << "Receive ObserverFlag : Start Observe\n"; cout << "========================================================" << endl; g_Observation = 1; cout << "\t\trequestFlag : Observer\n"; static int startedThread = 0; if (!startedThread) { cout << "\t\tpthrerad_create\n"; pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL); startedThread = 1; } ehResult = OC_EH_OK; } } else { std::cout << "Request invalid" << std::endl; } return ehResult; }
int main() { PlatformConfig cfg { OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0", 0, OC::QualityOfService::LowQos }; OCPlatform::Configure(cfg); int number = 0; try { myResource.createResource(); while (true) { bool end = false; cout << endl; cout << "========================================================" << endl; cout << "1. Temp is up" << endl; cout << "2. Temp is down" << endl; cout << "3. This Program will be ended." << endl; cout << "========================================================" << endl; cin >> number; switch (number) { case 1: { cout << "Temp is up!" << endl; myResource.m_temp += 10; pthread_cond_signal(&m_cond); cout << "ChangeLightRepresentation Done!" << endl; break; } case 2: { cout << "Temp is down!" << endl; myResource.m_temp -= 10; pthread_cond_signal(&m_cond); cout << "ChangeLightRepresentation Done!" << endl; break; } case 3: { cout << "Bye!" << endl; end = true; break; } default: { cout << "You type wrong number. Try again!" << endl; break; } } if (end == true) { break; } } } catch (exception &e) { cout << "main exception : " << e.what() << endl; } }
OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request , std::shared_ptr< OCResourceResponse > response) { cout << "Sample Provider entityHandler\n"; if (request) { cout << "flag : request\n"; std::string requestType = request->getRequestType(); int requestFlag = request->getRequestHandlerFlag(); if (requestFlag == RequestHandlerFlag::InitFlag) { cout << "\t\trequestFlag : Init\n"; } if (requestFlag == RequestHandlerFlag::RequestFlag) { cout << "\t\trequestFlag : Request\n"; if (requestType == "GET") { cout << "\t\trequestType : GET\n"; try { if (response) { OCRepresentation rep = myResource.get(); cout << rep.getJSONRepresentation() << endl; response->setErrorCode(200); response->setResourceRepresentation(rep, DEFAULT_INTERFACE); } else { cout << "response is null\n"; } } catch (exception &e) { cout << e.what() << endl; } } else if (requestType == "PUT") { cout << "\t\t\trequestType : PUT\n"; OCRepresentation rep = request->getResourceRepresentation(); myResource.put(rep); if (response) { response->setErrorCode(200); response->setResourceRepresentation(myResource.get()); } } else if (requestType == "POST") { } else if (requestType == "DELETE") { } } else if (requestFlag & RequestHandlerFlag::ObserverFlag) { pthread_t threadId; cout << request->getResourceUri() << endl; cout << request->getResourceRepresentation().getUri() << endl; cout << "========================================================" << endl; cout << "Receive ObserverFlag : Start Observe\n"; cout << "========================================================" << endl; g_Observation = 1; cout << "\t\trequestFlag : Observer\n"; static int startedThread = 0; if (!startedThread) { cout << "\t\tpthrerad_create\n"; pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL); startedThread = 1; } } } else { std::cout << "Request invalid" << std::endl; } return OC_EH_OK; }