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.h"
25 #define SLEEP_FOR(X) if (X > 0) std::this_thread::sleep_for(std::chrono::milliseconds(X));
27 AttributeUpdateAutomation::AttributeUpdateAutomation(
28 SimulatorResource *resource, const std::string &attrName, AutomationType type, int interval)
29 : m_resource(resource),
33 m_stopRequested(false),
34 m_updateInterval(interval) {}
36 SimulatorResult AttributeUpdateAutomation::start()
39 return SIMULATOR_AUTOMATION_ALREADY_STARTED;
41 // Check the validity of attribute
42 SimulatorResourceModel resModel = m_resource->getModel();
43 if (false == resModel.getAttribute(m_attrName, m_attribute))
44 return SIMULATOR_ERROR;
46 if (m_updateInterval < 0)
48 m_updateInterval = m_attribute.getUpdateFrequencyTime();
49 if (0 > m_updateInterval)
53 m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this);
56 return SIMULATOR_SUCCESS;
59 void AttributeUpdateAutomation::stop()
61 m_stopRequested = true;
66 void AttributeUpdateAutomation::updateAttribute()
74 while (AutomationType::RECURRENT == m_type);
79 void AttributeUpdateAutomation::setAttributeValue()
81 if (0 == m_attribute.getValueType()) // For integer type values
85 m_attribute.getRange(min, max);
86 for (int value = min; value <= max; value++)
88 m_resource->updateAttribute(m_attribute.getName(), value);
89 SLEEP_FOR(m_updateInterval);
94 for (int index = 0; index < m_attribute.getAllowedValuesSize(); index++)
96 m_resource->updateAttributeFromAllowedValues(m_attribute.getName(), index);
97 SLEEP_FOR(m_updateInterval);
103 ResourceUpdateAutomation::ResourceUpdateAutomation(
104 SimulatorResource *resource, AutomationType type, int interval)
105 : m_resource(resource),
108 m_updateInterval(interval) {}
110 SimulatorResult ResourceUpdateAutomation::start()
112 if (true == m_status)
113 return SIMULATOR_AUTOMATION_ALREADY_STARTED;
115 m_resModel = m_resource->getModel();
116 std::map<std::string, SimulatorResourceModel::Attribute> attributes = m_resModel.getAttributes();
117 if (0 == attributes.size())
120 return SIMULATOR_ERROR;
123 for (auto & attribute : attributes)
125 AttributeUpdateAutomationPtr attributeAutomation = std::make_shared<AttributeUpdateAutomation>
126 (m_resource, attribute.first, m_type, m_updateInterval);
127 m_attrUpdationList.push_back(attributeAutomation);
128 if (SIMULATOR_SUCCESS != attributeAutomation->start())
132 return SIMULATOR_ERROR;
137 return SIMULATOR_SUCCESS;
140 void ResourceUpdateAutomation::stop()
142 // Stop all the attributes updation
143 for (auto & attrAutomation : m_attrUpdationList)
145 attrAutomation->stop();
148 m_attrUpdationList.clear();
152 UpdateAutomationManager::UpdateAutomationManager()
153 : m_automationId(0) {}
155 SimulatorResult UpdateAutomationManager::startResourceAutomation(SimulatorResource *resource,
156 int &id, AutomationType type, int interval)
158 ResourceUpdateAutomationPtr resoureceAutomation(new ResourceUpdateAutomation(resource, type,
160 SimulatorResult result = resoureceAutomation->start();
161 if (SIMULATOR_SUCCESS != result)
167 std::lock_guard<std::mutex> lock(m_mutex);
168 m_resourceUpdationList[m_automationId++] = resoureceAutomation;
169 id = m_automationId - 1;
173 SimulatorResult UpdateAutomationManager::startAttributeAutomation(SimulatorResource *resource,
174 const std::string &attrName, int &id, AutomationType type, int interval)
176 AttributeUpdateAutomationPtr attributeAutomation(new AttributeUpdateAutomation(resource, attrName,
178 SimulatorResult result = attributeAutomation->start();
179 if (SIMULATOR_SUCCESS != result)
185 std::lock_guard<std::mutex> lock(m_mutex);
186 m_attrUpdationList[m_automationId++] = attributeAutomation;
187 id = m_automationId - 1;
191 void UpdateAutomationManager::stop(int automationId)
193 std::lock_guard<std::mutex> lock(m_mutex);
194 if (m_resourceUpdationList.end() != m_resourceUpdationList.find(automationId))
196 m_resourceUpdationList[automationId]->stop();
197 m_resourceUpdationList.erase(m_resourceUpdationList.find(automationId));
199 else if (m_attrUpdationList.end() != m_attrUpdationList.find(automationId))
201 m_attrUpdationList[automationId]->stop();
202 m_attrUpdationList.erase(m_attrUpdationList.find(automationId));
206 void UpdateAutomationManager::stopAll()
208 std::lock_guard<std::mutex> lock(m_mutex);
209 std::for_each(m_resourceUpdationList.begin(),
210 m_resourceUpdationList.end(), [] (std::pair<int, ResourceUpdateAutomationPtr> element)
212 element.second->stop();
214 m_resourceUpdationList.clear();
216 std::for_each(m_attrUpdationList.begin(),
217 m_attrUpdationList.end(), [] (std::pair<int, AttributeUpdateAutomationPtr> element)
219 element.second->stop();
221 m_attrUpdationList.clear();