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_mngr.h"
22 #include "simulator_exceptions.h"
23 #include "simulator_logger.h"
26 #define TAG "UPDATE_AUTOMATION_MNGR"
28 UpdateAutomationMngr::UpdateAutomationMngr()
31 int UpdateAutomationMngr::startResourceAutomation(SimulatorSingleResource *resource,
32 AutomationType type, int interval, updateCompleteCallback callback)
36 OIC_LOG(ERROR, TAG, "Invalid callback!");
37 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
40 ResourceUpdateAutomationSP resourceAutomation(new ResourceUpdateAutomation(
41 m_id, resource, type, interval, callback,
42 std::bind(&UpdateAutomationMngr::automationCompleted, this, std::placeholders::_1)));
44 std::lock_guard<std::mutex> lock(m_lock);
45 resourceAutomation->start();
47 OIC_LOG_V(DEBUG, TAG, "Resource automation successfully started [id: %d]", m_id);
48 SIM_LOG(ILogger::INFO, "Resource automation successfully started [ id: " << m_id << " ]");
50 m_resourceUpdationList[m_id] = resourceAutomation;
54 int UpdateAutomationMngr::startAttributeAutomation(SimulatorSingleResource *resource,
55 const std::string &attrName, AutomationType type, int interval,
56 updateCompleteCallback callback)
60 OIC_LOG(ERROR, TAG, "Invalid callback!");
61 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
64 // Check the validity of attribute
65 SimulatorResourceModel::Attribute attribute;
66 if (false == resource->getAttribute(attrName, attribute))
68 OIC_LOG_V(ERROR, TAG, "Attribute:%s not present in resource!", attrName.c_str());
69 throw SimulatorException(SIMULATOR_ERROR, "Attribute is not present in resource!");
72 AttributeUpdateAutomationSP attributeAutomation(new AttributeUpdateAutomation(
73 m_id, resource, attribute, type, interval, callback,
74 std::bind(&UpdateAutomationMngr::automationCompleted, this, std::placeholders::_1)));
76 std::lock_guard<std::mutex> lock(m_lock);
77 attributeAutomation->start();
79 OIC_LOG_V(DEBUG, TAG, "Attribute automation successfully started [name: %s, id: %d]",
80 attrName.c_str(), m_id);
81 SIM_LOG(ILogger::INFO, "Automation for " << attrName << " attribute has successfully started [ id: "
85 m_attrUpdationList[m_id] = attributeAutomation;
89 std::vector<int> UpdateAutomationMngr::getResourceAutomationIds()
92 std::lock_guard<std::mutex> lock(m_lock);
93 for (auto &automation : m_resourceUpdationList)
94 ids.push_back(automation.first);
99 std::vector<int> UpdateAutomationMngr::getAttributeAutomationIds()
101 std::vector<int> ids;
102 std::lock_guard<std::mutex> lock(m_lock);
103 for (auto &automation : m_attrUpdationList)
104 ids.push_back(automation.first);
109 void UpdateAutomationMngr::stop(int id)
111 std::lock_guard<std::mutex> lock(m_lock);
112 if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
114 m_resourceUpdationList[id]->stop();
115 m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
118 else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
120 m_attrUpdationList[id]->stop();
121 m_attrUpdationList.erase(m_attrUpdationList.find(id));
126 void UpdateAutomationMngr::stopAll()
128 std::lock_guard<std::mutex> lock(m_lock);
129 std::for_each(m_resourceUpdationList.begin(),
130 m_resourceUpdationList.end(), [] (std::pair<int, ResourceUpdateAutomationSP> element)
132 element.second->stop();
134 m_resourceUpdationList.clear();
136 std::for_each(m_attrUpdationList.begin(),
137 m_attrUpdationList.end(), [] (std::pair<int, AttributeUpdateAutomationSP> element)
139 element.second->stop();
142 m_attrUpdationList.clear();
145 void UpdateAutomationMngr::automationCompleted(int id)
147 std::lock_guard<std::mutex> lock(m_lock);
148 if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
150 m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
152 else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
154 m_attrUpdationList.erase(m_attrUpdationList.find(id));