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 "RemoteSceneCollection.h"
23 #include "SceneCommons.h"
24 #include "RemoteSceneUtils.h"
25 #include "SceneCollectionResourceRequestor.h"
26 #include "OCPlatform.h"
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 }
38 // TODO: check pRequestor not null
41 void RemoteSceneCollection::addNewScene
42 (const std::string &name, AddNewSceneCallback clientCB)
44 if (m_remoteScenes.find(name) == m_remoteScenes.end())
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);
51 m_requestorPtr->requestSceneCreation(name, internalCB);
55 // TODO: throw duplication scene name exception
59 void RemoteSceneCollection::removeScene(const std::string &/* name */,
60 RemoveSceneCallback /* clientCB */)
65 std::map< const std::string, RemoteScene::Ptr >
66 RemoteSceneCollection::getRemoteScenes() const
68 return m_remoteScenes;
71 RemoteScene::Ptr RemoteSceneCollection::getRemoteScene(const std::string &sceneName) const
73 auto itr = m_remoteScenes.find(sceneName);
75 // TODO: throw unadded scene exception
76 return itr != m_remoteScenes.end() ? itr->second : nullptr;
79 void RemoteSceneCollection::setName(const std::string &name, SetNameCallback clientCB)
81 SceneCollectionResourceRequestor::InternalSetNameCallback internalCB
82 = std::bind(&RemoteSceneCollection::onNameSet, this,
83 std::placeholders::_1, name, std::move(clientCB));
85 m_requestorPtr->requestSetName(name, internalCB);
88 std::string RemoteSceneCollection::getName() const
93 std::string RemoteSceneCollection::getId() const
98 void RemoteSceneCollection::initializeRemoteSceneCollection(
99 const std::vector< RCSRepresentation > &MemberReps, const std::string &host)
103 for (const auto &itr : MemberReps)
105 RCSResourceAttributes attrs = itr.getAttributes();
107 for (const auto &mappingInfo :
108 attrs.at(SCENE_KEY_SCENEMAPPINGS).get
109 <std::vector< RCSResourceAttributes > >())
111 std::string sceneName
112 = mappingInfo.at(SCENE_KEY_SCENE).get< std::string >();
113 RemoteScene::Ptr pRemoteScene = nullptr;
115 auto remoteScene = m_remoteScenes.find(sceneName);
116 if (remoteScene == m_remoteScenes.end())
118 pRemoteScene = createRemoteSceneInstance(sceneName);
122 pRemoteScene = m_remoteScenes.at(sceneName);
125 pRemoteScene->addExistingRemoteSceneAction(
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))
136 catch (const std::exception &)
142 RemoteScene::Ptr RemoteSceneCollection::createRemoteSceneInstance
143 (const std::string &name)
145 RemoteScene::Ptr pNewRemoteScene
147 new RemoteScene(name, m_requestorPtr)
150 m_remoteScenes[name] = pNewRemoteScene;
152 return pNewRemoteScene;
155 void RemoteSceneCollection::onSceneAddedRemoved(const int &reqType,
156 const std::string &name, int eCode,
157 const AddNewSceneCallback &addCB, const RemoveSceneCallback &)
159 if (eCode == SCENE_RESPONSE_SUCCESS)
163 case SceneCollectionResourceRequestor::REQUEST_TYPE::ADD_SCENE:
164 addCB(createRemoteSceneInstance(name), SCENE_RESPONSE_SUCCESS);
167 case SceneCollectionResourceRequestor::REQUEST_TYPE::REMOVE_SCENE:
177 void RemoteSceneCollection::onNameSet(int eCode, const std::string &name,
178 const SetNameCallback &clientCB)
180 if (eCode == SCENE_RESPONSE_SUCCESS)