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.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_H_
29 #define SIMULATOR_RESOURCE_H_
31 #include "simulator_resource_model.h"
32 #include "simulator_attribute_automation.h"
33 #include "simulator_error_codes.h"
35 class ResourceManager;
37 * @class SimulatorResource
38 * @brief This class provides a set of functions for operating and automating a resource.
40 class SimulatorResource
42 friend class ResourceManager;
45 typedef std::function<void (const std::string &uri, const SimulatorResourceModel &resModel)>
46 ResourceModelChangedCB;
49 * This method is used to set the resource URI.
51 * @param uri - Resource URI
55 void setURI(const std::string &uri);
58 * This method is used to get the resource URI.
60 * @return Resource URI
62 std::string getURI() const;
65 * This method is used to set the resource type.
67 * @param resourceType - Resource Type
71 void setResourceType(const std::string &resourceType);
74 * This method is used to get the resource URI.
76 * @return Resource Type
78 std::string getResourceType() const;
81 * This method is used to set the interface type of the resource.
83 * @param interfaceType - Interface Type of the resource
87 void setInterfaceType(const std::string &interfaceType);
90 * This method is used to get the interface type of the resource.
92 * @return Interface type of the resource
94 std::string getInterfaceType() const;
97 * This method is used to set the name of the resource.
99 * @param name - Name of the resource
103 void setName(const std::string &name);
106 * This method is used to get the name of the resource.
108 * @return Resource name
110 std::string getName() const;
113 * This method is used to start the attribute value automation for all attributes.
114 * Once started, values for the attributes will be selected randomly from their allowed range
115 * and the updated values will be notified to all the observers of the resource.
117 * @param id - Identifier for automation will be returned
119 * @return SimulatorResult
121 SimulatorResult startUpdateAutomation(AutomationType type, int &id);
124 * This method is used to start the attribute value automation for a specific attribute.
125 * Once started, values for the attribute will be selected randomly from its allowed range
126 * and the updated value will be notified to all the observers of the resource.
128 * @param attrName - Name of the attribute to be automated
129 * @param id - Identifier for automation will be returned
131 * @return SimulatorResult
133 SimulatorResult startUpdateAutomation(const std::string &attrName, AutomationType type, int &id);
136 * This method is used to stop the automation.
138 * @param id - Identifier for automation
140 void stopUpdateAutomation(const int id);
143 * This method is used to add a new attribute to the resource model.
145 * @param attrName - Name of the attribute
146 * @param attrValue - Value of the attribute
150 template <typename T>
151 void addAttribute(const std::string &attrName, const T &attrValue)
153 m_resModel.addAttribute(attrName, attrValue);
154 notifyListOfObservers();
158 * This method is used to set the value range of an attribute.
159 * This method is intended to be used for attributes whose values are numbers only.
161 * @param attrName - Name of the attribute
162 * @param min - Minimum value of the range
163 * @param max - Maximum value of the range
167 void setRange(const std::string &attrName, const int min, const int max);
170 * This method is used to set the allowed values of an attribute.
172 * @param attrName - Name of the attribute
173 * @param values - Allowed values
177 template <typename T>
178 void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
180 m_resModel.setAllowedValues(attrName, values);
184 * This method is used to set the update interval time for automation.
186 * @param attrName - Name of the attribute
187 * @param interval - Interval time in miliseconds for attribute value update automation
191 void setUpdateInterval(const std::string &attrName, int interval);
194 * This method is used to update the value of an attribute.
196 * @param attrName - Name of the attribute
197 * @param value - Value of the attribute
201 template <typename T>
202 void updateAttribute(const std::string &attrName, const T &value)
204 m_resModel.updateAttribute(attrName, value);
205 notifyListOfObservers();
209 * This method is used to update the attribute's value by taking the index of the value
210 * in the allowed values range.
212 * @param attrName - Name of the attribute
213 * @param allowedValueIndex - Index of the value in the allowed values range
217 void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
220 * This method is used to remove an attribute from the resource model.
222 * @param attName - Name of the attribute to be removed
226 void removeAttribute(const std::string &attName);
229 * This method is used to get the object of SimulatorResourceModel.
230 * Attributes of the resource are accessed using this object.
232 * @return SimulatorResourceModel - Resource model of the resource
234 SimulatorResourceModel getModel() const;
237 * This method is used to set the callback for receiving the notifications when the
238 * resource model changes.
240 * @param callback - Callback to be set for receiving the notifications.
242 * @return SimulatorResourceModel - Resource model of the resource
244 void setModelChangeCallback(ResourceModelChangedCB callback);
247 OCResourceHandle getHandle() const;
248 OC::OCRepresentation getOCRepresentation();
249 bool modifyResourceModel(OC::OCRepresentation &ocRep, SimulatorResourceModel::UpdateType type);
250 OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request);
251 void notifyListOfObservers ();
253 SimulatorResourceModel m_resModel;
254 OCResourceHandle m_resourceHandle;
256 std::string m_resourceType;
257 std::string m_interfaceType;
259 OC::ObservationIds m_interestedObservers;
260 ResourceModelChangedCB m_callback;
261 UpdateAutomationManager m_updateAutomationMgr;
264 typedef std::shared_ptr<SimulatorResource> SimulatorResourcePtr;