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 "resource_update_automation.h"
22 #include "simulator_resource_server_impl.h"
23 #include "simulator_exceptions.h"
24 #include "simulator_logger.h"
27 #define ATAG "ATTRIBUTE_AUTOMATION"
28 #define RTAG "RESOURCE_AUTOMATION"
30 #define SLEEP_FOR(X) if (X > 0) std::this_thread::sleep_for(std::chrono::milliseconds(X));
32 AttributeUpdateAutomation::AttributeUpdateAutomation(int id, SimulatorResourceServer *resource,
33 const std::string &attrName, AutomationType type, int interval,
34 updateCompleteCallback callback, std::function<void (const int)> finishedCallback)
35 : m_resource(resource),
39 m_stopRequested(false),
40 m_updateInterval(interval),
42 m_finishedCallback(finishedCallback),
45 void AttributeUpdateAutomation::start()
47 // Check the validity of attribute
48 SimulatorResourceModel resModel = m_resource->getModel();
49 if (false == resModel.getAttribute(m_attrName, m_attribute))
51 OC_LOG_V(ERROR, ATAG, "Attribute:%s not present in resource!", m_attrName.c_str());
52 throw SimulatorException(SIMULATOR_ERROR, "Attribute is not present in resource!");
55 if (m_updateInterval < 0)
57 m_updateInterval = m_attribute.getUpdateFrequencyTime();
58 if (0 > m_updateInterval)
62 m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this);
65 void AttributeUpdateAutomation::stop()
67 m_stopRequested = true;
72 void AttributeUpdateAutomation::updateAttribute()
80 catch (SimulatorException &e)
87 while (AutomationType::RECURRENT == m_type);
91 OC_LOG_V(DEBUG, ATAG, "Attribute:%s automation is completed!", m_attrName.c_str());
92 SIM_LOG(ILogger::INFO, "Automation of " << m_attrName << " attribute is completed.");
95 // Notify application through callback
97 m_callback(m_resource->getURI(), m_id);
99 if (m_finishedCallback && !m_stopRequested)
100 m_finishedCallback(m_id);
103 void AttributeUpdateAutomation::setAttributeValue()
105 SimulatorResourceServerImpl *resourceImpl =
106 dynamic_cast<SimulatorResourceServerImpl *>(m_resource);
110 if (SimulatorResourceModel::Attribute::ValueType::INTEGER ==
111 m_attribute.getValueType()) // For integer type values
116 m_attribute.getRange(min, max);
117 for (int value = min; value <= max; value++)
121 resourceImpl->updateAttributeValue(m_attribute.getName(), value);
122 resourceImpl->notifyApp();
123 SLEEP_FOR(m_updateInterval);
128 for (int index = 0; index < m_attribute.getAllowedValuesSize(); index++)
132 resourceImpl->updateFromAllowedValues(m_attribute.getName(), index);
133 resourceImpl->notifyApp();
134 SLEEP_FOR(m_updateInterval);
139 ResourceUpdateAutomation::ResourceUpdateAutomation(int id, SimulatorResourceServer *resource,
140 AutomationType type, int interval, updateCompleteCallback callback,
141 std::function<void (const int)> finishedCallback)
142 : m_resource(resource),
145 m_updateInterval(interval),
146 m_callback(callback),
147 m_finishedCallback(finishedCallback) {}
149 void ResourceUpdateAutomation::start()
151 m_resModel = m_resource->getModel();
152 std::map<std::string, SimulatorResourceModel::Attribute> attributes = m_resModel.getAttributes();
153 if (0 == attributes.size())
155 OC_LOG(ERROR, RTAG, "Resource has zero attributes!");
156 throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!");
160 for (auto & attribute : attributes)
162 AttributeUpdateAutomationSP attributeAutomation(new AttributeUpdateAutomation(
163 id, m_resource, attribute.first, m_type, m_updateInterval, nullptr,
164 std::bind(&ResourceUpdateAutomation::finished, this, std::placeholders::_1)));
166 m_attrUpdationList[id++] = attributeAutomation;
169 attributeAutomation->start();
171 catch (SimulatorException &e)
179 void ResourceUpdateAutomation::finished(int id)
181 if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
183 m_attrUpdationList.erase(m_attrUpdationList.find(id));
186 if (!m_attrUpdationList.size())
188 // Notify application through callback
190 m_callback(m_resource->getURI(), m_id);
192 if (m_finishedCallback)
193 m_finishedCallback(m_id);
197 void ResourceUpdateAutomation::stop()
199 // Stop all the attributes updation
200 for (auto & attrAutomation : m_attrUpdationList)
202 (attrAutomation.second)->stop();
205 m_attrUpdationList.clear();