[IOT-1065][IOT-1069] RC - Tizen: sample application fix
[platform/upstream/iotivity.git] / service / scene-manager / src / SceneListResourceRequestor.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "SceneListResourceRequestor.h"
22 #include "RemoteSceneUtils.h"
23 #include "OCPlatform.h"
24
25 namespace OIC
26 {
27     namespace Service
28     {
29
30         SceneListResourceRequestor::SceneListResourceRequestor(
31             RCSRemoteResourceObject::Ptr listResource)
32                 : m_sceneListResource{ listResource }
33         {
34             SCENE_CLIENT_ASSERT_NOT_NULL(listResource);
35         }
36
37         void SceneListResourceRequestor::requestSceneCollectionCreation(
38             const std::string &name, InternalCreateSceneCollectionCallback internalCB)
39         {
40             RCSResourceAttributes attrs;
41             attrs[SCENE_KEY_NAME] = name;
42
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()));
48
49             RCSQueryParams queryParams;
50             queryParams.setResourceInterface(LINK_BATCH);
51
52             m_sceneListResource->set(queryParams, std::move(attrs), std::move(setRequestCB));
53         }
54
55         void SceneListResourceRequestor::requestSetName
56         (const std::string &name, InternalSetNameCallback internalCB)
57         {
58             RCSResourceAttributes attrs;
59             attrs[SCENE_KEY_NAME] = name;
60
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()));
65
66             RCSQueryParams queryParams;
67             queryParams.setResourceInterface(SCENE_CLIENT_REQ_IF);
68
69             m_sceneListResource->set(queryParams, std::move(attrs), std::move(setRequestCB));
70         }
71
72         void SceneListResourceRequestor::requestGet(
73             const std::string &ifType, RCSRemoteResourceObject::GetCallback cb)
74         {
75             RCSQueryParams params;
76             params.setResourceInterface(ifType);
77
78             m_sceneListResource->get(params, cb);
79         }
80
81         RCSRemoteResourceObject::Ptr SceneListResourceRequestor::getRemoteResourceObject() const
82         {
83             return m_sceneListResource;
84         }
85
86         void SceneListResourceRequestor::onSceneCollectionCreated(
87             const RCSRepresentation &rep, int eCode,
88             const std::string &name, const InternalCreateSceneCollectionCallback &cb,
89             SceneListResourceRequestor::wPtr ptr)
90         {
91             SceneListResourceRequestor::Ptr listPtr = ptr.lock();
92
93             if (listPtr)
94             {
95                 listPtr->onSceneCollectionCreated_impl(std::move(rep), eCode, name, std::move(cb));
96             }
97         }
98
99         void SceneListResourceRequestor::onSceneCollectionCreated_impl(
100             const RCSRepresentation &rep, int eCode,
101             const std::string &name, const InternalCreateSceneCollectionCallback &internalCB)
102         {
103             int result = SCENE_CLIENT_BADREQUEST;
104             std::string link, id;
105
106             if (eCode == OC_STACK_OK)
107             {
108                 try
109                 {
110                     RCSResourceAttributes attrs = rep.getAttributes();
111
112                     if (attrs.at(SCENE_KEY_NAME).get< std::string >().compare(name) == 0)
113                     {
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;
117                     }
118                 }
119                 catch (const std::exception &e)
120                 {
121                     SCENE_CLIENT_PRINT_LOG(e.what());
122                     result = SCENE_SERVER_INTERNALSERVERERROR;
123                 }
124             }
125
126             internalCB(link, id, name, result);
127         }
128
129         void SceneListResourceRequestor::onNameSet(const RCSRepresentation &rep, int eCode,
130             const std::string &name, const InternalSetNameCallback &internalCB,
131             SceneListResourceRequestor::wPtr ptr)
132         {
133             SceneListResourceRequestor::Ptr listPtr = ptr.lock();
134
135             if (listPtr)
136             {
137                 listPtr->onNameSet_impl(std::move(rep), eCode, name, std::move(internalCB));
138             }
139         }
140
141         void SceneListResourceRequestor::onNameSet_impl(
142             const RCSRepresentation &rep, int eCode, const std::string &name,
143             const InternalSetNameCallback &internalCB)
144         {
145             int result = SCENE_CLIENT_BADREQUEST;
146             if (eCode == OC_STACK_OK)
147             {
148                 if (rep.getAttributes().at(SCENE_KEY_NAME).get< std::string >() == name)
149                 {
150                     result = SCENE_RESPONSE_SUCCESS;
151                 }
152
153             }
154
155             internalCB(result);
156         }
157
158     }
159 }