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 AttributeUpdateAutomation::AttributeUpdateAutomation(
32 int id, std::shared_ptr<SimulatorSingleResourceImpl> resource, const std::string &name,
33 AutoUpdateType type, int interval,
34 const SimulatorSingleResource::AutoUpdateCompleteCallback &callback,
35 std::function<void (const int)> finishedCallback)
39 m_updateInterval(interval),
40 m_stopRequested(false),
43 m_finishedCallback(finishedCallback),
46 if (m_updateInterval < 0)
50 AttributeUpdateAutomation::~AttributeUpdateAutomation()
56 void AttributeUpdateAutomation::start()
58 SimulatorResourceAttribute attribute;
59 if (false == m_resource->getAttribute(m_attrName, attribute))
61 OIC_LOG(ERROR, ATAG, "Attribute is not present in resource!");
62 throw SimulatorException(SIMULATOR_ERROR, "Attribute is not present in resource!");
65 m_thread.reset(new std::thread(&AttributeUpdateAutomation::updateAttribute, this, attribute));
68 void AttributeUpdateAutomation::stop()
71 std::lock_guard<std::mutex> lock(m_lock);
72 m_stopRequested = true;
75 m_condVariable.notify_one();
80 void AttributeUpdateAutomation::updateAttribute(SimulatorResourceAttribute attribute)
82 std::unique_lock<std::mutex> lock(m_lock);
83 std::chrono::system_clock::time_point now;
85 AttributeGenerator attributeGen(attribute);
90 SimulatorResourceAttribute attribute;
91 while (!m_stopRequested && true == attributeGen.next(attribute))
93 if (false == m_resource->updateAttributeValue(attribute))
97 now = std::chrono::system_clock::now();
98 m_condVariable.wait_until(lock, now + std::chrono::milliseconds(m_updateInterval),
99 [this] { return m_stopRequested; });
102 attributeGen.reset();
104 catch (SimulatorException &e)
109 while (!m_stopRequested && AutoUpdateType::REPEAT == m_type);
111 if (!m_stopRequested)
113 OIC_LOG_V(DEBUG, ATAG, "Attribute:%s automation is completed!", m_attrName.c_str());
114 SIM_LOG(ILogger::INFO, "Attribute automation completed [Name: \"" << m_attrName
115 << "\", id: " << m_id <<"].");
119 SIM_LOG(ILogger::INFO, "Attribute automation stopped [Name: \"" << m_attrName
120 << "\", id: " << m_id <<"].");
123 // Notify application through callback
125 m_callback(m_resource->getURI(), m_id);
127 if (m_finishedCallback && !m_stopRequested)
129 std::thread notifyManager(m_finishedCallback, m_id);
130 notifyManager.detach();
134 ResourceUpdateAutomation::ResourceUpdateAutomation(
135 int id, std::shared_ptr<SimulatorSingleResourceImpl> resource, AutoUpdateType type, int interval,
136 const SimulatorSingleResource::AutoUpdateCompleteCallback &callback,
137 std::function<void (const int)> finishedCallback)
140 m_updateInterval(interval),
141 m_stopRequested(false),
142 m_resource(resource),
143 m_callback(callback),
144 m_finishedCallback(finishedCallback),
147 if (m_updateInterval < 0)
148 m_updateInterval = 0;
151 ResourceUpdateAutomation::~ResourceUpdateAutomation()
153 if (!m_stopRequested)
157 void ResourceUpdateAutomation::start()
159 std::vector<SimulatorResourceAttribute> attributes;
160 for (auto &attributeEntry : m_resource->getAttributes())
162 attributes.push_back(attributeEntry.second);
165 if (0 == attributes.size())
167 OIC_LOG(ERROR, RTAG, "Resource has zero attributes!");
168 throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!");
171 m_thread.reset(new std::thread(&ResourceUpdateAutomation::updateAttributes, this, attributes));
174 void ResourceUpdateAutomation::stop()
177 std::lock_guard<std::mutex> lock(m_lock);
178 m_stopRequested = true;
181 m_condVariable.notify_one();
186 void ResourceUpdateAutomation::updateAttributes(
187 std::vector<SimulatorResourceAttribute> attributes)
189 std::unique_lock<std::mutex> lock(m_lock);
190 std::chrono::system_clock::time_point now;
194 AttributeCombinationGen attrCombGen(attributes);
195 SimulatorResourceModel newResModel;
196 while (!m_stopRequested && attrCombGen.next(newResModel))
198 SimulatorResourceModel updatedResModel;
199 m_resource->updateResourceModel(newResModel, updatedResModel);
202 now = std::chrono::system_clock::now();
203 m_condVariable.wait_until(lock, now + std::chrono::milliseconds(m_updateInterval),
204 [this] { return m_stopRequested; });
207 while (!m_stopRequested && AutoUpdateType::REPEAT == m_type);
209 if (!m_stopRequested)
211 OIC_LOG_V(DEBUG, RTAG, "Resource update automation complete [id: %d]!", m_id);
212 SIM_LOG(ILogger::INFO, "Resource automation completed [URI: \"" << m_resource->getURI()
213 << "\", id: " << m_id << "].");
217 SIM_LOG(ILogger::INFO, "Resource automation stopped [URI: \"" << m_resource->getURI()
218 << "\", id: " << m_id <<"].");
221 // Notify application
223 m_callback(m_resource->getURI(), m_id);
225 if (m_finishedCallback)
227 std::thread notifyManager(m_finishedCallback, m_id);
228 notifyManager.detach();