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 = std::make_shared<std::thread>(&AttributeUpdateAutomation::updateAttribute, this);
58 void AttributeUpdateAutomation::stop()
60 m_stopRequested = true;
63 void AttributeUpdateAutomation::updateAttribute()
65 SimulatorSingleResourceImpl *resourceImpl =
66 dynamic_cast<SimulatorSingleResourceImpl *>(m_resource);
75 SimulatorResourceModel::Attribute attribute;
76 while (!m_stopRequested && true == m_attributeGen.next(attribute))
80 if (false == m_resource->updateAttributeValue(attribute))
82 OC_LOG_V(ERROR, ATAG, "Failed to update the attribute![%s]", attribute.getName().c_str());
86 catch(SimulatorException &e) {}
88 resourceImpl->notifyApp();
90 SLEEP_FOR(m_updateInterval);
93 m_attributeGen.reset();
95 catch (SimulatorException &e)
100 while (!m_stopRequested && AutomationType::RECURRENT == m_type);
102 if (!m_stopRequested)
104 OC_LOG_V(DEBUG, ATAG, "Attribute:%s automation is completed!", m_attrName.c_str());
105 SIM_LOG(ILogger::INFO, "Automation of " << m_attrName << " attribute is completed.");
108 // Notify application through callback
110 m_callback(m_resource->getURI(), m_id);
112 if (m_finishedCallback)
113 m_finishedCallback(m_id);
116 ResourceUpdateAutomation::ResourceUpdateAutomation(int id, SimulatorSingleResource *resource,
117 AutomationType type, int interval, updateCompleteCallback callback,
118 std::function<void (const int)> finishedCallback)
119 : m_resource(resource),
122 m_stopRequested(false),
123 m_updateInterval(interval),
124 m_callback(callback),
125 m_finishedCallback(finishedCallback),
128 void ResourceUpdateAutomation::start()
130 std::vector<SimulatorResourceModel::Attribute> attributes;
131 for (auto &attributeEntry : m_resource->getResourceModel().getAttributes())
133 attributes.push_back(attributeEntry.second);
136 if (0 == attributes.size())
138 OC_LOG(ERROR, RTAG, "Resource has zero attributes!");
139 throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!");
142 m_thread = std::make_shared<std::thread>(&ResourceUpdateAutomation::updateAttributes, this, attributes);
146 void ResourceUpdateAutomation::stop()
148 m_stopRequested = true;
151 void ResourceUpdateAutomation::updateAttributes(
152 std::vector<SimulatorResourceModel::Attribute> attributes)
154 SimulatorSingleResourceImpl *resourceImpl =
155 dynamic_cast<SimulatorSingleResourceImpl *>(m_resource);
162 AttributeCombinationGen attrCombGen(attributes);
163 SimulatorResourceModel resModel;
164 while (!m_stopRequested && attrCombGen.next(resModel))
166 for (auto &attributeEntry : resModel.getAttributes())
170 resourceImpl->updateAttributeValue(attributeEntry.second);
172 catch(SimulatorException &e) {}
175 resourceImpl->notifyApp();
176 SLEEP_FOR(m_updateInterval);
179 while (!m_stopRequested && AutomationType::RECURRENT == m_type);
181 if (!m_stopRequested)
183 SIM_LOG(ILogger::INFO, "Resource update automation complete [id: " << m_id << "]");
186 // Notify application
188 m_callback(m_resource->getURI(), m_id);
190 if (m_finishedCallback)
191 m_finishedCallback(m_id);