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_single_resource_impl.h"
23 #include "attribute_generator.h"
24 #include "simulator_exceptions.h"
25 #include "simulator_logger.h"
28 #define ATAG "ATTRIBUTE_AUTOMATION"
29 #define RTAG "RESOURCE_AUTOMATION"
31 #define SLEEP_FOR(X) if (X > 0) std::this_thread::sleep_for(std::chrono::milliseconds(X));
33 AttributeUpdateAutomation::AttributeUpdateAutomation(int id, SimulatorSingleResource *resource,
34 const SimulatorResourceModel::Attribute &attribute, AutomationType type, int interval,
35 updateCompleteCallback callback, std::function<void (const int)> finishedCallback)
36 : m_resource(resource),
39 m_stopRequested(false),
40 m_updateInterval(interval),
41 m_attributeGen(attribute),
43 m_finishedCallback(finishedCallback),
46 if (m_updateInterval < 0)
52 void AttributeUpdateAutomation::start()
54 m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this);
57 void AttributeUpdateAutomation::stop()
59 m_stopRequested = true;
64 void AttributeUpdateAutomation::updateAttribute()
66 SimulatorSingleResourceImpl *resourceImpl =
67 dynamic_cast<SimulatorSingleResourceImpl *>(m_resource);
76 SimulatorResourceModel::Attribute attribute;
77 while (!m_stopRequested && true == m_attributeGen.next(attribute))
79 if (false == m_resource->updateAttributeValue(attribute))
81 OC_LOG_V(ERROR, ATAG, "Failed to update the attribute![%s]", attribute.getName().c_str());
84 resourceImpl->notifyApp();
86 SLEEP_FOR(m_updateInterval);
89 m_attributeGen.reset();
91 catch (SimulatorException &e)
96 while (!m_stopRequested && AutomationType::RECURRENT == m_type);
100 OC_LOG_V(DEBUG, ATAG, "Attribute:%s automation is completed!", m_attrName.c_str());
101 SIM_LOG(ILogger::INFO, "Automation of " << m_attrName << " attribute is completed.");
104 // Notify application through callback
106 m_callback(m_resource->getURI(), m_id);
108 if (m_finishedCallback && !m_stopRequested)
109 m_finishedCallback(m_id);
112 ResourceUpdateAutomation::ResourceUpdateAutomation(int id, SimulatorSingleResource *resource,
113 AutomationType type, int interval, updateCompleteCallback callback,
114 std::function<void (const int)> finishedCallback)
115 : m_resource(resource),
118 m_stopRequested(false),
119 m_updateInterval(interval),
120 m_callback(callback),
121 m_finishedCallback(finishedCallback),
124 void ResourceUpdateAutomation::start()
126 std::vector<SimulatorResourceModel::Attribute> attributes;
127 for (auto &attributeEntry : m_resource->getResourceModel().getAttributes())
129 attributes.push_back(attributeEntry.second);
132 if (0 == attributes.size())
134 OC_LOG(ERROR, RTAG, "Resource has zero attributes!");
135 throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!");
138 m_thread = new std::thread(&ResourceUpdateAutomation::updateAttributes, this, attributes);
141 void ResourceUpdateAutomation::stop()
143 m_stopRequested = true;
148 void ResourceUpdateAutomation::updateAttributes(
149 std::vector<SimulatorResourceModel::Attribute> attributes)
151 SimulatorSingleResourceImpl *resourceImpl =
152 dynamic_cast<SimulatorSingleResourceImpl *>(m_resource);
159 AttributeCombinationGen attrCombGen(attributes);
160 SimulatorResourceModel resModel;
161 while (!m_stopRequested && attrCombGen.next(resModel))
163 for (auto &attributeEntry : resModel.getAttributes())
165 resourceImpl->updateAttributeValue(attributeEntry.second);
168 resourceImpl->notifyApp();
169 SLEEP_FOR(m_updateInterval);
172 while (!m_stopRequested && AutomationType::RECURRENT == m_type);
174 if (!m_stopRequested)
176 SIM_LOG(ILogger::INFO, "Resource update automation complete [id: " << m_id << "]");
179 // Notify application
181 m_callback(m_resource->getURI(), m_id);
183 if (m_finishedCallback && !m_stopRequested)
184 m_finishedCallback(m_id);