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_server_types.h"
32 #include "simulator_resource_model.h"
33 #include "simulator_exceptions.h"
35 enum class ObservationStatus : unsigned char
49 * @class SimulatorResourceServer
50 * @brief This class provides a set of functions for operating and automating a resource.
52 class SimulatorResourceServer
56 * Callback method for receiving notifications when resource model gets changed.
58 * @param uri - Resource URI
59 * @param resModel - Resource model
61 typedef std::function<void (const std::string &uri, const SimulatorResourceModel &resModel)>
62 ResourceModelChangedCB;
65 * Callback method for receiving notifications when observer is registered/unregistered
68 * @param uri - Resource URI
69 * @param state - OBSERVE_REGISTER if observer is registered, otherwise OBSERVE_UNREGISTER.
70 * @param observerInfo - Information about observer.
72 typedef std::function<void (const std::string &uri, ObservationStatus state, const ObserverInfo &observerInfo)>
75 SimulatorResourceServer();
77 virtual ~SimulatorResourceServer() {};
80 * API to get the resource URI.
82 * @return Resource URI
84 std::string getURI() const;
87 * API to get the resource URI.
89 * @return Resource Type
91 std::string getResourceType() const;
94 * API to get the interface type of the resource.
96 * @return Interface type of the resource
98 std::string getInterfaceType() const;
101 * API to get the name of the resource.
103 * @return Resource name
105 std::string getName() const;
108 * API to add a new attribute to the resource model.
110 * @param attribute - Attribute to be add to model.
112 void addAttribute(SimulatorResourceModel::Attribute &attribute);
115 * API to set the value range of an attribute.
116 * This method is intended to be used for attributes whose values are numbers only.
118 * @param attrName - Name of the attribute
119 * @param min - Minimum value of the range
120 * @param max - Maximum value of the range
122 void setRange(const std::string &attrName, const int min, const int max);
125 * API to set the allowed values of an attribute.
127 * @param attrName - Name of the attribute
128 * @param values - Allowed values
130 template <typename T>
131 void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
133 m_resModel.setAllowedValues(attrName, values);
137 * API to set the update interval time for automation.
139 * @param attrName - Name of the attribute
140 * @param interval - Interval time in miliseconds for attribute value update automation
142 void setUpdateInterval(const std::string &attrName, int interval);
145 * API to update the value of an attribute.
147 * @param attrName - Name of the attribute
148 * @param value - Value of the attribute
150 template <typename T>
151 void updateAttributeValue(const std::string &attrName, const T &value)
153 m_resModel.updateAttribute(attrName, value);
155 // Notify all the subscribers
160 * API to update the attribute's value by taking the index of the value
161 * in the allowed values range.
163 * @param attrName - Name of the attribute
164 * @param allowedValueIndex - Index of the value in the allowed values range
166 void updateFromAllowedValues(const std::string &attrName, unsigned int index);
169 * API to remove an attribute from the resource model.
171 * @param attName - Name of the attribute to be removed
173 void removeAttribute(const std::string &attName);
176 * API to get the object of SimulatorResourceModel.
177 * Attributes of the resource are accessed using this object.
179 * @return Resource model of the resource.
181 SimulatorResourceModel getModel() const;
184 * API to get the observable state of resource.
186 * @return bool - true if resource is observable, otherwise false.
188 virtual bool isObservable() const = 0;
191 * API to start the attribute value automation for all attributes.
192 * Once started, values for the attributes will be selected randomly from their allowed range
193 * and the updated values will be notified to all the observers of the resource.
195 * @param type - Automation type.
196 * @param callback - Callback to get notifiy when update automation is finished.
197 * @param id - Identifier for automation.
199 * @return ID representing update automation session.
200 * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
202 virtual int startUpdateAutomation(AutomationType type,
203 updateCompleteCallback callback) = 0;
206 * This method is used to start the attribute value automation for a specific attribute.
207 * Once started, values for the attribute will be selected randomly from its allowed range
208 * and the updated value will be notified to all the observers of the resource.
210 * @param attrName - Name of the attribute to be automated.
211 * @param type - Automation type.
212 * @param callback - Callback to get notifiy when update automation is finished.
213 * @param id - Identifier for automation.
215 * @return ID representing update automation session.
216 * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
218 virtual int startUpdateAutomation(const std::string &attrName, AutomationType type,
219 updateCompleteCallback callback) = 0;
222 * API to get the Ids of all ongoing resource update automation .
224 * @return vector of resource automation ids.
226 virtual std::vector<int> getResourceAutomationIds() = 0;
229 * API to get the Ids of all ongoing attribute update automation .
231 * @return vector of attribute automation ids.
233 virtual std::vector<int> getAttributeAutomationIds() = 0;
236 * API to stop the resource/attribute automation.
238 * @param id - Identifier for automation.
240 virtual void stopUpdateAutomation(const int id) = 0;
243 * API to set the callback for receiving the notifications when the
244 * resource model changes.
246 * @param callback - Callback to be set for receiving the notifications.
248 virtual void setModelChangeCallback(ResourceModelChangedCB callback) = 0;
251 * API to set the callback for receiving the notifications when
252 * observer is registered or unregistered with resource.
254 * @param callback - Callback to be set for receiving the notifications.
256 virtual void setObserverCallback(ObserverCB callback) = 0;
259 * API to get observers which are registered with resource.
261 * @return vector of ObserverInfo.
263 virtual std::vector<ObserverInfo> getObserversList() = 0;
266 * API to notify current resource model to specific observer.
268 * NOTE: API throws @SimulatorException exception.
270 virtual void notify(uint8_t id) = 0;
273 * API to notify all registered observers.
275 * NOTE: API throws @SimulatorException exception.
277 virtual void notifyAll() = 0;
282 std::string m_resourceType;
283 std::string m_interfaceType;
284 SimulatorResourceModel m_resModel;
287 typedef std::shared_ptr<SimulatorResourceServer> SimulatorResourceServerSP;