add scene manager remote interface class implementation
[platform/upstream/iotivity.git] / service / scene-manager / src / RemoteSceneCollection.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 "RemoteSceneCollection.h"
22
23 #include "SceneCommons.h"
24 #include "RemoteSceneUtils.h"
25 #include "SceneCollectionResourceRequestor.h"
26 #include "OCPlatform.h"
27
28 namespace OIC
29 {
30     namespace Service
31     {
32
33         RemoteSceneCollection::RemoteSceneCollection
34         (std::shared_ptr< SceneCollectionResourceRequestor > pRequestor,
35          const std::string &id, const std::string &name)
36             : m_id{ id }, m_name{ name }, m_requestorPtr{ pRequestor }
37         {
38             // TODO: check pRequestor not null
39         }
40
41         void RemoteSceneCollection::addNewScene
42         (const std::string &name, AddNewSceneCallback clientCB)
43         {
44             if (m_remoteScenes.find(name) == m_remoteScenes.end())
45             {
46                 SceneCollectionResourceRequestor::InternalSceneRequestCallback internalCB
47                     = std::bind(&RemoteSceneCollection::onSceneAddedRemoved, this,
48                                 std::placeholders::_1, std::placeholders::_2,
49                                 std::placeholders::_3, std::move(clientCB), nullptr);
50
51                 m_requestorPtr->requestSceneCreation(name, internalCB);
52             }
53             else
54             {
55                 // TODO: throw duplication scene name exception
56             }
57         }
58
59         void RemoteSceneCollection::removeScene(const std::string &/* name */,
60                                                 RemoveSceneCallback /* clientCB */)
61         {
62
63         }
64
65         std::map< const std::string, RemoteScene::Ptr >
66         RemoteSceneCollection::getRemoteScenes() const
67         {
68             return m_remoteScenes;
69         }
70
71         RemoteScene::Ptr RemoteSceneCollection::getRemoteScene(const std::string &sceneName) const
72         {
73             auto itr = m_remoteScenes.find(sceneName);
74
75             // TODO: throw unadded scene exception
76             return itr != m_remoteScenes.end() ? itr->second : nullptr;
77         }
78
79         void RemoteSceneCollection::setName(const std::string &name, SetNameCallback clientCB)
80         {
81             SceneCollectionResourceRequestor::InternalSetNameCallback internalCB
82                 = std::bind(&RemoteSceneCollection::onNameSet, this,
83                             std::placeholders::_1, name, std::move(clientCB));
84
85             m_requestorPtr->requestSetName(name, internalCB);
86         }
87
88         std::string RemoteSceneCollection::getName() const
89         {
90             return m_name;
91         }
92
93         std::string RemoteSceneCollection::getId() const
94         {
95             return m_id;
96         }
97
98         void RemoteSceneCollection::initializeRemoteSceneCollection(
99             const std::vector< RCSRepresentation > &MemberReps, const std::string &host)
100         {
101             try
102             {
103                 for (const auto &itr : MemberReps)
104                 {
105                     RCSResourceAttributes attrs = itr.getAttributes();
106
107                     for (const auto &mappingInfo :
108                          attrs.at(SCENE_KEY_SCENEMAPPINGS).get
109                          <std::vector< RCSResourceAttributes > >())
110                     {
111                         std::string sceneName
112                             = mappingInfo.at(SCENE_KEY_SCENE).get< std::string >();
113                         RemoteScene::Ptr pRemoteScene = nullptr;
114
115                         auto remoteScene = m_remoteScenes.find(sceneName);
116                         if (remoteScene == m_remoteScenes.end())
117                         {
118                             pRemoteScene = createRemoteSceneInstance(sceneName);
119                         }
120                         else
121                         {
122                             pRemoteScene = m_remoteScenes.at(sceneName);
123                         }
124
125                         pRemoteScene->addExistingRemoteSceneAction(
126                             host + itr.getUri(),
127                             attrs.at(SCENE_KEY_PAYLOAD_LINK).get< RCSResourceAttributes >().
128                             at(SCENE_KEY_HREF).get< std::string >(),
129                             attrs.at(SCENE_KEY_ID).get< std::string >(),
130                             mappingInfo.at(SCENE_KEY_MEMBERPROPERTY).get< std::string >(),
131                             RCSResourceAttributes::Value(mappingInfo.at(SCENE_KEY_MEMBERVALUE))
132                         );
133                     }
134                 }
135             }
136             catch (const std::exception &)
137             {
138                 // error
139             }
140         }
141
142         RemoteScene::Ptr RemoteSceneCollection::createRemoteSceneInstance
143         (const std::string &name)
144         {
145             RemoteScene::Ptr pNewRemoteScene
146             {
147                 new RemoteScene(name, m_requestorPtr)
148             };
149
150             m_remoteScenes[name] = pNewRemoteScene;
151
152             return pNewRemoteScene;
153         }
154
155         void RemoteSceneCollection::onSceneAddedRemoved(const int &reqType,
156                 const std::string &name, int eCode,
157                 const AddNewSceneCallback &addCB, const RemoveSceneCallback &)
158         {
159             if (eCode == SCENE_RESPONSE_SUCCESS)
160             {
161                 switch (reqType)
162                 {
163                     case SceneCollectionResourceRequestor::REQUEST_TYPE::ADD_SCENE:
164                         addCB(createRemoteSceneInstance(name), SCENE_RESPONSE_SUCCESS);
165                         break;
166
167                     case SceneCollectionResourceRequestor::REQUEST_TYPE::REMOVE_SCENE:
168                         break;
169                 }
170             }
171             else
172             {
173                 // error
174             }
175         }
176
177         void RemoteSceneCollection::onNameSet(int eCode, const std::string &name,
178                                               const SetNameCallback &clientCB)
179         {
180             if (eCode == SCENE_RESPONSE_SUCCESS)
181                 m_name = name;
182
183             clientCB(eCode);
184         }
185
186     }
187 }