void RCSSeparateResponse::set() { assert(m_request.getOCRequest()); auto resObj = m_request.getResourceObject().lock(); if (!resObj) { throw RCSBadRequestException{ "ResourceObject is unspecified(or destroyed)!" }; } if (m_done) throw RCSBadRequestException{ "The response is already set!" }; auto ocRequest = m_request.getOCRequest(); auto response = std::make_shared< OC::OCResourceResponse >(); response->setRequestHandle(ocRequest->getRequestHandle()); response->setResourceHandle(ocRequest->getResourceHandle()); response->setResponseResult(OC_EH_OK); // TODO the response should be different by the request interface. response->setResourceRepresentation( RCSRepresentation::toOCRepresentation(resObj->toRepresentation())); invokeOCFunc(OC::OCPlatform::sendResponse, response); m_done = true; }
OCEntityHandlerResult AbstractResource::resourceEntityHandler(shared_ptr<OCResourceRequest> Request) { //Set Default result OCEntityHandlerResult result = OC_EH_ERROR; //Check if it is a request if (Request) { //Get reqeuest information string requestType = Request->getRequestType(); int requestFlag = Request->getRequestHandlerFlag(); if (requestFlag & RequestHandlerFlag::RequestFlag) { //create response auto Response = std::make_shared<OC::OCResourceResponse>(); Response->setRequestHandle(Request->getRequestHandle()); Response->setResourceHandle(Request->getResourceHandle()); //Handle get if (requestType == "GET") { handleGet(Response); if (OCPlatform::sendResponse(Response) == OC_STACK_OK) { result = OC_EH_OK; } } else { Response->setResponseResult(OC_EH_ERROR); OCPlatform::sendResponse(Response); cerr << "Unsupported request type" << endl; return result; } } /*if (requestFlag & RequestHandlerFlag::ObserverFlag) { ObservationInfo observationInfo = Request->getObservationInfo(); if (ObserveAction::ObserveRegister == observationInfo.action) { cout << "Starting observer for temperature sensor" << endl; m_temperatureObservers.push_back(observationInfo.obsId); m_temperatureObserverLoop->start(); } else if (ObserveAction::ObserveUnregister == observationInfo.action) { m_temperatureObservers.erase( remove(m_temperatureObservers.begin(), m_temperatureObservers.end(), observationInfo.obsId), m_temperatureObservers.end()); } }*/ } return result; }
OCEntityHandlerResult SensorResource::entityHandler(std::shared_ptr<OCResourceRequest> request) { std::cout << "EH of sensor resource invoked " << std::endl; OCEntityHandlerResult ehResult = OC_EH_ERROR; if(request) { int requestFlag = request->getRequestHandlerFlag(); std::cout << "In entity handler for sensors, URI is : " << request->getResourceUri() << std::endl; if(requestFlag & RequestHandlerFlag::RequestFlag) { auto pResponse = std::make_shared<OC::OCResourceResponse>(); pResponse->setRequestHandle(request->getRequestHandle()); pResponse->setResourceHandle(request->getResourceHandle()); if(request->getRequestType() == "GET") { // Return all registered sensors std::cout<< " Sensors Get Request"<<std::endl; pResponse->setErrorCode(200); pResponse->setResourceRepresentation(get(), ""); if(OC_STACK_OK == OCPlatform::sendResponse(pResponse)) { ehResult = OC_EH_OK; } } else if(request->getRequestType() == "PUT") { // Register new sensor address std::cout << " Sensors Put Request"<<std::endl; put(request->getResourceRepresentation()); pResponse->setErrorCode(200); pResponse->setResourceRepresentation(get(),""); if(OC_STACK_OK == OCPlatform::sendResponse(pResponse)) { ehResult = OC_EH_OK; if (!m_sensorMap.empty()) { ChangeSensorRepresentation(); } } } else { std::cout <<" Sensors unsupported request type " << request->getRequestType() << std::endl; pResponse->setResponseResult(OC_EH_ERROR); OCPlatform::sendResponse(pResponse); ehResult = OC_EH_ERROR; } } if (requestFlag & RequestHandlerFlag::ObserverFlag) { ObservationInfo observationInfo = request->getObservationInfo(); if (ObserveAction::ObserveRegister == observationInfo.action) { cout << "Starting observer for registered sensors" << endl; m_interestedObservers.push_back(observationInfo.obsId); m_observation = 1; } else if (ObserveAction::ObserveUnregister == observationInfo.action) { m_interestedObservers.erase(std::remove( m_interestedObservers.begin(), m_interestedObservers.end(), observationInfo.obsId), m_interestedObservers.end()); } ehResult = OC_EH_OK; } } return ehResult; }
const var EnviJSONRPC::getResponseWithResult(const var responseResult) { setResponseResult (responseResult); return (response); }