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_attribute_automation.h"
22 #include "simulator_resource_server.h"
25 #define SLEEP_FOR(X) if (X > 0) std::this_thread::sleep_for(std::chrono::milliseconds(X));
27 AttributeUpdateAutomation::AttributeUpdateAutomation(
28 SimulatorResourceServer *resource, const std::string &attrName, updateCompleteCallback callback,
29 int automationId, std::function<void (const int)> finishedCallback, AutomationType type,
31 : m_resource(resource),
36 m_stopRequested(false),
37 m_updateInterval(interval),
39 m_finishedCallback(finishedCallback) {}
41 SimulatorResult AttributeUpdateAutomation::start()
44 return SIMULATOR_AUTOMATION_ALREADY_STARTED;
46 // Check the validity of attribute
47 SimulatorResourceModel resModel = m_resource->getModel();
48 if (false == resModel.getAttribute(m_attrName, m_attribute))
49 return SIMULATOR_ERROR;
51 if (m_updateInterval < 0)
53 m_updateInterval = m_attribute.getUpdateFrequencyTime();
54 if (0 > m_updateInterval)
58 m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this);
61 return SIMULATOR_SUCCESS;
64 void AttributeUpdateAutomation::stop()
66 m_stopRequested = true;
71 void AttributeUpdateAutomation::updateAttribute()
79 while (AutomationType::RECURRENT == m_type);
83 // Notify application through callback
85 m_callback(m_resource->getURI(), m_id);
86 if (m_finishedCallback && !m_stopRequested)
87 m_finishedCallback(m_id);
90 void AttributeUpdateAutomation::setAttributeValue()
92 if (0 == m_attribute.getValueType()) // For integer type values
96 m_attribute.getRange(min, max);
97 for (int value = min; value <= max; value++)
101 m_resource->updateAttribute(m_attribute.getName(), value);
102 SLEEP_FOR(m_updateInterval);
107 for (int index = 0; index < m_attribute.getAllowedValuesSize(); index++)
111 m_resource->updateAttributeFromAllowedValues(m_attribute.getName(), index);
112 SLEEP_FOR(m_updateInterval);
118 ResourceUpdateAutomation::ResourceUpdateAutomation(
119 SimulatorResourceServer *resource, updateCompleteCallback callback,
120 int automationId, std::function<void (const int)> finishedCallback, AutomationType type,
122 : m_resource(resource),
126 m_updateInterval(interval),
127 m_callback(callback),
128 m_finishedCallback(finishedCallback) {}
130 SimulatorResult ResourceUpdateAutomation::start()
132 if (true == m_status)
133 return SIMULATOR_AUTOMATION_ALREADY_STARTED;
135 m_resModel = m_resource->getModel();
136 std::map<std::string, SimulatorResourceModel::Attribute> attributes = m_resModel.getAttributes();
137 if (0 == attributes.size())
140 return SIMULATOR_ERROR;
144 for (auto & attribute : attributes)
146 AttributeUpdateAutomationPtr attributeAutomation = std::make_shared<AttributeUpdateAutomation>
147 (m_resource, attribute.first, nullptr, id,
148 std::bind(&ResourceUpdateAutomation::finished, this, std::placeholders::_1),
149 m_type, m_updateInterval);
150 m_attrUpdationList[id++] = attributeAutomation;
151 if (SIMULATOR_SUCCESS != attributeAutomation->start())
155 return SIMULATOR_ERROR;
160 return SIMULATOR_SUCCESS;
163 void ResourceUpdateAutomation::finished(int id)
165 if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
167 m_attrUpdationList.erase(m_attrUpdationList.find(id));
170 if (!m_attrUpdationList.size())
172 // Notify application through callback
174 m_callback(m_resource->getURI(), m_id);
175 if (m_finishedCallback)
176 m_finishedCallback(m_id);
179 void ResourceUpdateAutomation::stop()
181 // Stop all the attributes updation
182 for (auto & attrAutomation : m_attrUpdationList)
184 (attrAutomation.second)->stop();
187 m_attrUpdationList.clear();
191 UpdateAutomationManager::UpdateAutomationManager()
192 : m_automationId(0) {}
194 SimulatorResult UpdateAutomationManager::startResourceAutomation(SimulatorResourceServer *resource,
195 int &id, updateCompleteCallback callback, AutomationType type, int interval)
197 std::lock_guard<std::mutex> lock(m_mutex);
199 ResourceUpdateAutomationPtr resourceAutomation(new ResourceUpdateAutomation(
200 resource, callback, m_automationId,
201 std::bind(&UpdateAutomationManager::automationFinished, this, std::placeholders::_1),
203 SimulatorResult result = resourceAutomation->start();
204 if (SIMULATOR_SUCCESS != result)
210 m_resourceUpdationList[m_automationId] = resourceAutomation;
211 id = m_automationId++;
215 SimulatorResult UpdateAutomationManager::startAttributeAutomation(SimulatorResourceServer *resource,
216 const std::string &attrName, int &id, updateCompleteCallback callback, AutomationType type,
219 std::lock_guard<std::mutex> lock(m_mutex);
221 AttributeUpdateAutomationPtr attributeAutomation(new AttributeUpdateAutomation(
222 resource, attrName, callback, m_automationId,
223 std::bind(&UpdateAutomationManager::automationFinished, this, std::placeholders::_1),
225 SimulatorResult result = attributeAutomation->start();
226 if (SIMULATOR_SUCCESS != result)
232 m_attrUpdationList[m_automationId] = attributeAutomation;
233 id = m_automationId++;
237 std::vector<int> UpdateAutomationManager::getResourceAutomationIds()
239 std::vector<int> ids;
240 std::lock_guard<std::mutex> lock(m_mutex);
241 for (auto & automation : m_resourceUpdationList)
242 ids.push_back(automation.first);
247 std::vector<int> UpdateAutomationManager::getAttributeAutomationIds()
249 std::vector<int> ids;
250 std::lock_guard<std::mutex> lock(m_mutex);
251 for (auto & automation : m_attrUpdationList)
252 ids.push_back(automation.first);
257 void UpdateAutomationManager::stop(int automationId)
259 std::lock_guard<std::mutex> lock(m_mutex);
260 if (m_resourceUpdationList.end() != m_resourceUpdationList.find(automationId))
262 m_resourceUpdationList[automationId]->stop();
263 m_resourceUpdationList.erase(m_resourceUpdationList.find(automationId));
265 else if (m_attrUpdationList.end() != m_attrUpdationList.find(automationId))
267 m_attrUpdationList[automationId]->stop();
268 m_attrUpdationList.erase(m_attrUpdationList.find(automationId));
272 void UpdateAutomationManager::stopAll()
274 std::lock_guard<std::mutex> lock(m_mutex);
275 std::for_each(m_resourceUpdationList.begin(),
276 m_resourceUpdationList.end(), [] (std::pair<int, ResourceUpdateAutomationPtr> element)
278 element.second->stop();
280 m_resourceUpdationList.clear();
282 std::for_each(m_attrUpdationList.begin(),
283 m_attrUpdationList.end(), [] (std::pair<int, AttributeUpdateAutomationPtr> element)
285 element.second->stop();
287 m_attrUpdationList.clear();
290 void UpdateAutomationManager::automationFinished(int id)
292 std::lock_guard<std::mutex> lock(m_mutex);
293 if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
295 m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
297 else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
299 m_attrUpdationList.erase(m_attrUpdationList.find(id));