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 "SceneListResourceRequestor.h"
22 #include "RemoteSceneUtils.h"
23 #include "OCPlatform.h"
30 SceneListResourceRequestor::SceneListResourceRequestor(
31 RCSRemoteResourceObject::Ptr listResource)
32 : m_sceneListResource{ listResource }
34 SCENE_CLIENT_ASSERT_NOT_NULL(listResource);
37 void SceneListResourceRequestor::requestSceneCollectionCreation(
38 const std::string &name, InternalCreateSceneCollectionCallback internalCB)
40 RCSResourceAttributes attrs;
41 attrs[SCENE_KEY_NAME] = name;
43 RCSRemoteResourceObject::SetCallback setRequestCB
44 = std::bind(&SceneListResourceRequestor::onSceneCollectionCreated,
45 std::placeholders::_2, std::placeholders::_3,
46 name, std::move(internalCB),
47 SceneListResourceRequestor::wPtr(shared_from_this()));
49 RCSQueryParams queryParams;
50 queryParams.setResourceInterface(LINK_BATCH);
52 m_sceneListResource->set(queryParams, std::move(attrs), std::move(setRequestCB));
55 void SceneListResourceRequestor::requestSetName
56 (const std::string &name, InternalSetNameCallback internalCB)
58 RCSResourceAttributes attrs;
59 attrs[SCENE_KEY_NAME] = name;
61 RCSRemoteResourceObject::SetCallback setRequestCB
62 = std::bind(&SceneListResourceRequestor::onNameSet,
63 std::placeholders::_2, std::placeholders::_3, name, std::move(internalCB),
64 SceneListResourceRequestor::wPtr(shared_from_this()));
66 RCSQueryParams queryParams;
67 queryParams.setResourceInterface(SCENE_CLIENT_REQ_IF);
69 m_sceneListResource->set(queryParams, std::move(attrs), std::move(setRequestCB));
72 void SceneListResourceRequestor::requestGet(
73 const std::string &ifType, RCSRemoteResourceObject::GetCallback cb)
75 RCSQueryParams params;
76 params.setResourceInterface(ifType);
78 m_sceneListResource->get(params, cb);
81 RCSRemoteResourceObject::Ptr SceneListResourceRequestor::getRemoteResourceObject() const
83 return m_sceneListResource;
86 void SceneListResourceRequestor::onSceneCollectionCreated(
87 const RCSRepresentation &rep, int eCode,
88 const std::string &name, const InternalCreateSceneCollectionCallback &cb,
89 SceneListResourceRequestor::wPtr ptr)
91 SceneListResourceRequestor::Ptr listPtr = ptr.lock();
95 listPtr->onSceneCollectionCreated_impl(std::move(rep), eCode, name, std::move(cb));
99 void SceneListResourceRequestor::onSceneCollectionCreated_impl(
100 const RCSRepresentation &rep, int eCode,
101 const std::string &name, const InternalCreateSceneCollectionCallback &internalCB)
103 int result = SCENE_CLIENT_BADREQUEST;
104 std::string link, id;
106 if (eCode == OC_STACK_OK)
110 RCSResourceAttributes attrs = rep.getAttributes();
112 if (attrs.at(SCENE_KEY_NAME).get< std::string >().compare(name) == 0)
114 link = attrs.at(SCENE_KEY_PAYLOAD_LINK).get< std::string >();
115 id = attrs.at(SCENE_KEY_ID).get< std::string >();
116 result = SCENE_RESPONSE_SUCCESS;
119 catch (const std::exception &e)
121 SCENE_CLIENT_PRINT_LOG(e.what());
122 result = SCENE_SERVER_INTERNALSERVERERROR;
126 internalCB(link, id, name, result);
129 void SceneListResourceRequestor::onNameSet(const RCSRepresentation &rep, int eCode,
130 const std::string &name, const InternalSetNameCallback &internalCB,
131 SceneListResourceRequestor::wPtr ptr)
133 SceneListResourceRequestor::Ptr listPtr = ptr.lock();
137 listPtr->onNameSet_impl(std::move(rep), eCode, name, std::move(internalCB));
141 void SceneListResourceRequestor::onNameSet_impl(
142 const RCSRepresentation &rep, int eCode, const std::string &name,
143 const InternalSetNameCallback &internalCB)
145 int result = SCENE_CLIENT_BADREQUEST;
146 if (eCode == OC_STACK_OK)
148 if (rep.getAttributes().at(SCENE_KEY_NAME).get< std::string >() == name)
150 result = SCENE_RESPONSE_SUCCESS;