1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 "SceneMemberResourceRequestor.h"
22 #include "RemoteSceneUtils.h"
29 SceneMemberResourceRequestor::SceneMemberResourceRequestor(
30 RCSRemoteResourceObject::Ptr memberResource, const std::string &id)
31 : m_id{ id }, m_sceneMemberResource{ memberResource }
33 SCENE_CLIENT_ASSERT_NOT_NULL(memberResource);
36 void SceneMemberResourceRequestor::requestSceneActionCreation(
37 const std::string &sceneName, const RCSResourceAttributes &attr,
38 InternalAddSceneActionCallback internalCB)
40 RCSResourceAttributes attributesToSet;
41 std::vector< RCSResourceAttributes > vecSceneMappings;
43 for (const auto &itr : attr)
45 RCSResourceAttributes sceneMappingAttrs;
46 sceneMappingAttrs[SCENE_KEY_SCENE] = sceneName;
47 sceneMappingAttrs[SCENE_KEY_MEMBERPROPERTY] = itr.key();
48 sceneMappingAttrs[SCENE_KEY_MEMBERVALUE] = itr.value();
50 vecSceneMappings.push_back(sceneMappingAttrs);
53 attributesToSet[SCENE_KEY_SCENEMAPPINGS] = vecSceneMappings;
55 RCSRemoteResourceObject::RemoteAttributesSetCallback setRequestCB
56 = std::bind(&SceneMemberResourceRequestor::onSceneActionCreated,
57 std::placeholders::_1, std::placeholders::_2,
58 sceneName, attr, std::move(internalCB),
59 SceneMemberResourceRequestor::wPtr(shared_from_this()));
61 m_sceneMemberResource->setRemoteAttributes(
62 std::move(attributesToSet), std::move(setRequestCB));
65 void SceneMemberResourceRequestor::requestGet(
66 const std::string &ifType, RCSRemoteResourceObject::GetCallback cb)
68 RCSQueryParams params;
69 params.setResourceInterface(ifType);
71 m_sceneMemberResource->get(params, cb);
74 void SceneMemberResourceRequestor::setRemoteResourceObject(
75 RCSRemoteResourceObject::Ptr target)
77 m_remoteResource = target;
80 RCSRemoteResourceObject::Ptr SceneMemberResourceRequestor::getRemoteResourceObject() const
82 return m_remoteResource;
85 void SceneMemberResourceRequestor::onSceneActionCreated(
86 const RCSResourceAttributes &attrs, int eCode, const std::string &sceneName,
87 const RCSResourceAttributes &requestedAttrs, const InternalAddSceneActionCallback &cb,
88 SceneMemberResourceRequestor::wPtr ptr)
90 SceneMemberResourceRequestor::Ptr member = ptr.lock();
94 member->onSceneActionCreated_impl(
95 std::move(attrs), eCode, sceneName, std::move(requestedAttrs), std::move(cb));
99 void SceneMemberResourceRequestor::onSceneActionCreated_impl(
100 const RCSResourceAttributes &attrs, int eCode, const std::string &sceneName,
101 const RCSResourceAttributes &requestedAttrs,
102 const InternalAddSceneActionCallback &internalCB)
105 int result = SCENE_CLIENT_BADREQUEST;
107 if (eCode == OC_STACK_OK)
112 = attrs.at(SCENE_KEY_SCENEMAPPINGS).get
113 < std::vector< RCSResourceAttributes > >();
115 // check if the SCENE_MAPPINGS contains requested scene action
116 int uncreatedActionNum = requestedAttrs.size();
117 for (const auto &itr : mappings)
119 if (itr.at(SCENE_KEY_SCENE).get< std::string >().compare(sceneName) == 0)
122 = itr.at(SCENE_KEY_MEMBERPROPERTY).get< std::string >();
124 if (requestedAttrs.contains(key)
125 && requestedAttrs.at(key) == itr.at(SCENE_KEY_MEMBERVALUE))
127 uncreatedActionNum--;
131 if (uncreatedActionNum == 0)
133 result = SCENE_RESPONSE_SUCCESS;
138 catch (const std::exception &e)
140 SCENE_CLIENT_PRINT_LOG(e.what());
141 result = SCENE_SERVER_INTERNALSERVERERROR;