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 ******************************************************************/
22 * @file simulator_resource_server.h
24 * @brief This file contains a class which represents a simulator resource that provides a set
25 * of functions for operating a resource and performing automation on attribute values.
28 #ifndef SIMULATOR_RESOURCE_SERVER_H_
29 #define SIMULATOR_RESOURCE_SERVER_H_
31 #include "simulator_resource_model.h"
32 #include "simulator_attribute_automation.h"
33 #include "simulator_error_codes.h"
35 class ResourceManager;
37 * @class SimulatorResourceServer
38 * @brief This class provides a set of functions for operating and automating a resource.
40 class SimulatorResourceServer
42 friend class ResourceManager;
45 typedef std::function<void (const std::string &uri, const SimulatorResourceModel &resModel)>
46 ResourceModelChangedCB;
48 SimulatorResourceServer();
51 * This method is used to set the resource URI.
53 * @param uri - Resource URI
55 * @return SimulatorResult
57 SimulatorResult setURI(const std::string &uri);
60 * This method is used to get the resource URI.
62 * @return Resource URI
64 std::string getURI() const;
67 * This method is used to set the resource type.
69 * @param resourceType - Resource Type
71 * @return SimulatorResult
73 SimulatorResult setResourceType(const std::string &resourceType);
76 * This method is used to get the resource URI.
78 * @return Resource Type
80 std::string getResourceType() const;
83 * This method is used to set the interface type of the resource.
85 * @param interfaceType - Interface Type of the resource
87 * @return SimulatorResult
89 SimulatorResult setInterfaceType(const std::string &interfaceType);
92 * This method is used to get the interface type of the resource.
94 * @return Interface type of the resource
96 std::string getInterfaceType() const;
99 * This method is used to set the name of the resource.
101 * @param name - Name of the resource
105 void setName(const std::string &name);
108 * This method is used to get the name of the resource.
110 * @return Resource name
112 std::string getName() const;
115 * This method is used to set whether resource can be observable or not.
117 * @param state - true for resource observable, otherwise false.
119 * @return SimulatorResult
121 SimulatorResult setObservable(bool state);
124 * This method is used to get the observable state of resource.
126 * @return bool - true if resource is observable, otherwise false.
128 bool isObservable() const;
131 * This method is used to start the attribute value automation for all attributes.
132 * Once started, values for the attributes will be selected randomly from their allowed range
133 * and the updated values will be notified to all the observers of the resource.
135 * @param id - Identifier for automation will be returned
137 * @return SimulatorResult
139 SimulatorResult startUpdateAutomation(AutomationType type,
140 updateCompleteCallback callback, int &id);
143 * This method is used to start the attribute value automation for a specific attribute.
144 * Once started, values for the attribute will be selected randomly from its allowed range
145 * and the updated value will be notified to all the observers of the resource.
147 * @param attrName - Name of the attribute to be automated
148 * @param id - Identifier for automation will be returned
150 * @return SimulatorResult
152 SimulatorResult startUpdateAutomation(const std::string &attrName, AutomationType type,
153 updateCompleteCallback callback, int &id);
156 * This method is used to get the Ids of all ongoing resource update automation .
158 * @return Ids as vector of int
160 std::vector<int> getResourceAutomationIds();
163 * This method is used to get the Ids of all ongoing attribute update automation .
165 * @return Ids as vector of int
167 std::vector<int> getAttributeAutomationIds();
170 * This method is used to stop the automation.
172 * @param id - Identifier for automation
174 void stopUpdateAutomation(const int id);
177 * This method is used to add a new attribute to the resource model.
179 * @param attrName - Name of the attribute
180 * @param attrValue - Value of the attribute
184 template <typename T>
185 void addAttribute(const std::string &attrName, const T &attrValue)
187 m_resModel.addAttribute(attrName, attrValue);
188 notifyListOfObservers();
192 * This method is used to set the value range of an attribute.
193 * This method is intended to be used for attributes whose values are numbers only.
195 * @param attrName - Name of the attribute
196 * @param min - Minimum value of the range
197 * @param max - Maximum value of the range
201 void setRange(const std::string &attrName, const int min, const int max);
204 * This method is used to set the allowed values of an attribute.
206 * @param attrName - Name of the attribute
207 * @param values - Allowed values
211 template <typename T>
212 void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
214 m_resModel.setAllowedValues(attrName, values);
218 * This method is used to set the update interval time for automation.
220 * @param attrName - Name of the attribute
221 * @param interval - Interval time in miliseconds for attribute value update automation
225 void setUpdateInterval(const std::string &attrName, int interval);
228 * This method is used to update the value of an attribute.
230 * @param attrName - Name of the attribute
231 * @param value - Value of the attribute
235 template <typename T>
236 void updateAttribute(const std::string &attrName, const T &value)
238 m_resModel.updateAttribute(attrName, value);
239 notifyListOfObservers();
243 * This method is used to update the attribute's value by taking the index of the value
244 * in the allowed values range.
246 * @param attrName - Name of the attribute
247 * @param allowedValueIndex - Index of the value in the allowed values range
251 void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
254 * This method is used to remove an attribute from the resource model.
256 * @param attName - Name of the attribute to be removed
260 void removeAttribute(const std::string &attName);
263 * This method is used to get the object of SimulatorResourceModel.
264 * Attributes of the resource are accessed using this object.
266 * @return SimulatorResourceModel - Resource model of the resource
268 SimulatorResourceModel getModel() const;
271 * This method is used to set the callback for receiving the notifications when the
272 * resource model changes.
274 * @param callback - Callback to be set for receiving the notifications.
276 * @return SimulatorResourceModel - Resource model of the resource
278 void setModelChangeCallback(ResourceModelChangedCB callback);
281 SimulatorResult start();
282 SimulatorResult stop();
283 OC::OCRepresentation getOCRepresentation();
284 bool modifyResourceModel(OC::OCRepresentation &ocRep);
285 OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request);
286 void notifyListOfObservers ();
288 SimulatorResourceModel m_resModel;
290 std::string m_resourceType;
291 std::string m_interfaceType;
293 ResourceModelChangedCB m_callback;
294 UpdateAutomationManager m_updateAutomationMgr;
296 OCResourceHandle m_resourceHandle;
297 OCResourceProperty m_property;
298 OC::ObservationIds m_interestedObservers;
301 typedef std::shared_ptr<SimulatorResourceServer> SimulatorResourceServerPtr;