1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "simulator_resource.h"
22 #include "simulator_attribute_automation.h"
24 void SimulatorResource::setRange(const std::string &attrName, const int min, const int max)
26 m_resModel.setRange(attrName, min, max);
29 void SimulatorResource::setUpdateInterval(const std::string &attrName, int interval)
31 m_resModel.setUpdateInterval(attrName, interval);
34 void SimulatorResource::removeAttribute(const std::string &attrName)
36 m_resModel.removeAttribute(attrName);
37 notifyListOfObservers();
40 void SimulatorResource::setURI(const std::string &uri)
45 std::string SimulatorResource::getURI() const
50 void SimulatorResource::setResourceType(const std::string &resourceType)
52 m_resourceType = resourceType;
55 std::string SimulatorResource::getResourceType() const
57 return m_resourceType;
60 void SimulatorResource::setInterfaceType(const std::string &interfaceType)
62 m_interfaceType = interfaceType;
65 std::string SimulatorResource::getInterfaceType() const
67 return m_interfaceType;
70 OCResourceHandle SimulatorResource::getHandle() const
72 return m_resourceHandle;
75 void SimulatorResource::setName(const std::string &name)
80 std::string SimulatorResource::getName() const
85 void SimulatorResource::updateAttributeFromAllowedValues(const std::string &attrName,
86 const int allowedValueIndex)
88 m_resModel.updateAttributeFromAllowedValues(attrName, allowedValueIndex);
89 notifyListOfObservers();
92 SimulatorResourceModel SimulatorResource::getModel() const
97 void SimulatorResource::setModelChangeCallback(ResourceModelChangedCB callback)
99 m_callback = callback;
102 SimulatorResult SimulatorResource::startUpdateAutomation(AutomationType type, int &id)
104 return m_updateAutomationMgr.startResourceAutomation(this, id, type);
107 SimulatorResult SimulatorResource::startUpdateAutomation(const std::string &attrName,
108 AutomationType type, int &id)
110 return m_updateAutomationMgr.startAttributeAutomation(this, attrName, id, type);
113 void SimulatorResource::stopUpdateAutomation(const int id)
115 m_updateAutomationMgr.stop(id);
118 OC::OCRepresentation SimulatorResource::getOCRepresentation()
120 return m_resModel.getOCRepresentation();
123 bool SimulatorResource::modifyResourceModel(OC::OCRepresentation &ocRep,
124 SimulatorResourceModel::UpdateType type)
126 return m_resModel.update(ocRep, type);
129 OCEntityHandlerResult SimulatorResource::entityHandler(std::shared_ptr<OC::OCResourceRequest>
132 OCEntityHandlerResult errCode = OC_EH_ERROR;
138 if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag())
140 auto response = std::make_shared<OC::OCResourceResponse>();
141 response->setRequestHandle(request->getRequestHandle());
142 response->setResourceHandle(request->getResourceHandle());
144 if ("GET" == request->getRequestType())
146 response->setErrorCode(200);
147 response->setResponseResult(OC_EH_OK);
148 response->setResourceRepresentation(getOCRepresentation());
150 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
155 else if ("PUT" == request->getRequestType())
157 OC::OCRepresentation rep = request->getResourceRepresentation();
158 if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::PUT))
160 response->setErrorCode(200);
161 response->setResponseResult(OC_EH_OK);
162 response->setResourceRepresentation(getOCRepresentation());
166 response->setErrorCode(400);
167 response->setResponseResult(OC_EH_ERROR);
170 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
175 else if ("POST" == request->getRequestType())
177 OC::OCRepresentation rep = request->getResourceRepresentation();
178 if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::POST))
180 response->setErrorCode(200);
181 response->setResponseResult(OC_EH_OK);
182 response->setResourceRepresentation(getOCRepresentation());
186 response->setErrorCode(400);
187 response->setResponseResult(OC_EH_ERROR);
190 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
195 else if ("DELETE" == request->getRequestType())
197 OC::OCRepresentation rep = request->getResourceRepresentation();
198 if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::DELETE))
200 response->setErrorCode(200);
201 response->setResponseResult(OC_EH_OK);
202 response->setResourceRepresentation(getOCRepresentation());
206 response->setErrorCode(400);
207 response->setResponseResult(OC_EH_ERROR);
210 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
217 response->setResponseResult(OC_EH_ERROR);
218 OC::OCPlatform::sendResponse(response);
219 errCode = OC_EH_ERROR;
223 if (OC::RequestHandlerFlag::ObserverFlag & request->getRequestHandlerFlag())
225 OC::ObservationInfo observationInfo = request->getObservationInfo();
226 if (OC::ObserveAction::ObserveRegister == observationInfo.action)
228 m_interestedObservers.push_back(observationInfo.obsId);
230 else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
232 m_interestedObservers.erase(std::remove(m_interestedObservers.begin(),
233 m_interestedObservers.end(),
234 observationInfo.obsId),
235 m_interestedObservers.end());
243 void SimulatorResource::notifyListOfObservers ()
247 if (m_interestedObservers.size())
249 std::shared_ptr<OC::OCResourceResponse> resourceResponse =
250 {std::make_shared<OC::OCResourceResponse>()};
252 resourceResponse->setErrorCode(200);
253 resourceResponse->setResponseResult(OC_EH_OK);
254 resourceResponse->setResourceRepresentation(getOCRepresentation(), OC::DEFAULT_INTERFACE);
256 OC::OCPlatform::notifyListOfObservers(getHandle(), m_interestedObservers, resourceResponse);
260 m_callback(getURI(),getModel());