Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / scene-manager / src / RemoteSceneAction.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 "RemoteSceneAction.h"
22
23 #include <cassert>
24
25 #include "SceneCommons.h"
26 #include "SceneMemberResourceRequestor.h"
27
28 namespace OIC
29 {
30     namespace Service
31     {
32
33         RemoteSceneAction::RemoteSceneAction(
34             SceneMemberResourceRequestor::Ptr requestor,
35             const std::string &sceneName, const RCSResourceAttributes &attrs)
36                 : m_sceneName{ sceneName }, m_attributes{ attrs }, m_requestor{ requestor }
37         {
38             assert(requestor);
39         }
40
41         RemoteSceneAction::RemoteSceneAction(
42             SceneMemberResourceRequestor::Ptr requestor, const std::string &sceneName,
43             const std::string &key, const RCSResourceAttributes::Value &value)
44                 : m_sceneName{ sceneName }, m_requestor{ requestor }
45         {
46             assert(requestor);
47             m_attributes[key] = value;
48         }
49
50         void RemoteSceneAction::resetExecutionParameter(const std::string &key,
51                                        const RCSResourceAttributes::Value &value,
52                                        ResetExecutionParameterCallback clientCB)
53         {
54             RCSResourceAttributes attr;
55             attr[key] = RCSResourceAttributes::Value(value);
56
57             resetExecutionParameter(attr, std::move(clientCB));
58         }
59
60         void RemoteSceneAction::resetExecutionParameter(const RCSResourceAttributes &attr,
61             ResetExecutionParameterCallback clientCB)
62         {
63             if (!clientCB)
64             {
65                 throw RCSInvalidParameterException{ "resetExecutionParameter : Callback is NULL" };
66             }
67
68             SceneMemberResourceRequestor::InternalAddSceneActionCallback internalCB
69                 = std::bind(&RemoteSceneAction::onExecutionParameterSet, this,
70                 std::placeholders::_1, attr, std::move(clientCB));
71
72             m_requestor->requestSceneActionCreation(
73                 m_sceneName, attr, internalCB);
74         }
75
76         RCSResourceAttributes RemoteSceneAction::getExecutionParameter() const
77         {
78             std::lock_guard< std::mutex > lock(m_attributeLock);
79             return m_attributes;
80         }
81
82         RCSRemoteResourceObject::Ptr RemoteSceneAction::getRemoteResourceObject() const
83         {
84             return m_requestor->getRemoteResourceObject();
85         }
86
87         void RemoteSceneAction::onExecutionParameterSet(int eCode, const RCSResourceAttributes &attr,
88             const ResetExecutionParameterCallback &clientCB)
89         {
90             int result = SCENE_CLIENT_BADREQUEST;
91             if (eCode == SCENE_RESPONSE_SUCCESS)
92             {
93                 std::lock_guard< std::mutex > lock(m_attributeLock);
94                 m_attributes = attr;
95                 result = SCENE_RESPONSE_SUCCESS;
96             }
97
98             clientCB(result);
99         }
100     }
101 }