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 "SceneCollection.h"
23 #include "uuid/uuid.h"
33 unsigned int numSceneCollection = 0;
35 const std::string PREFIX_SCENE_COLLECTION_URI = "/a/sceneCollection/";
36 const std::string LAST_SCENE = "lastScene";
37 const std::string SCENE_VALUES = "sceneValues";
38 const std::string SCENE_COLLECTION_NAME = "n";
39 const std::string SCENE_COLLECTION_ID = "id";
40 const std::string SCENE_RTS = "rts";
41 const std::string SCENE_RTS_VALUE = "oic.r.scenemember";
42 const std::string SCENE_PAYLOAD_LINK = "link";
44 const std::string SCENE_COLLECTION_RT = "oic.wk.scenecollection";
47 std::string OICGenerateUUIDStr()
49 uint8_t uuid[UUID_SIZE] = { 0, };
50 char uuidStr[UUID_STRING_SIZE] = { 0, };
51 if(RAND_UUID_OK == OCGenerateUuid(uuid))
53 if(RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
55 return std::string(uuidStr);
62 SceneCollection::SceneCollection()
63 : m_Id(), m_Uri(PREFIX_SCENE_COLLECTION_URI + std::to_string(numSceneCollection++))
65 m_Id = OICGenerateUUIDStr();
68 // TODO handle uuid creation fail.
71 m_sceneCollectionResourcePtr = RCSResourceObject::Builder(m_Uri, SCENE_COLLECTION_RT,
72 OC_RSRVD_INTERFACE_DEFAULT).setDiscoverable(true).setObservable(true).build();
74 m_sceneCollectionResourcePtr->setAttribute(LAST_SCENE, "");
75 m_sceneCollectionResourcePtr->setAttribute(SCENE_VALUES, std::vector<std::string>());
76 m_sceneCollectionResourcePtr->setAttribute(SCENE_COLLECTION_NAME, "");
77 m_sceneCollectionResourcePtr->setAttribute(SCENE_COLLECTION_ID, m_Id);
78 m_sceneCollectionResourcePtr->setAttribute(SCENE_RTS, SCENE_RTS_VALUE);
81 m_sceneCollectionResourcePtr->setSetRequestHandler(
83 &SceneCollection::setRequestHandler, this,
84 std::placeholders::_1, std::placeholders::_2));
87 const std::vector< Scene::Ptr >& SceneCollection::getSceneList()
92 void SceneCollection::setName(const std::string& sceneCollectionName)
94 m_sceneCollectionResourcePtr->setAttribute(SCENE_COLLECTION_NAME, sceneCollectionName);
97 std::string SceneCollection::getName() const
99 return m_sceneCollectionResourcePtr->
100 getAttributeValue(SCENE_COLLECTION_NAME).get<std::string>();
103 std::string SceneCollection::getUri() const
108 std::string SceneCollection::getId() const
113 Scene::Ptr SceneCollection::addScene(const std::string& sceneName)
115 Scene::Ptr m_scene = std::make_shared < Scene > (sceneName);
116 m_SceneList.push_back(m_scene);
120 bool SceneCollection::removeScene(const Scene::Ptr& it)
123 // remove scene Name list
126 SceneMemberObject::Ptr SceneCollection::addSceneMember(
127 const RCSRemoteResourceObject::Ptr& it)
129 auto newSceneMemberObj = std::make_shared < SceneMemberObject > (it);
130 m_SceneMemberObjectList.push_back(newSceneMemberObj);
131 return newSceneMemberObj;
134 bool SceneCollection::removeSceneMember(SceneMemberObject::Ptr it)
138 const std::vector< SceneMemberObject::Ptr >& SceneCollection::getSceneMemberList()
140 return m_SceneMemberObjectList;
144 SceneCollection::setRequestHandler(
145 const RCSRequest & request, RCSResourceAttributes & attributes)
147 if(attributes.contains(SCENE_PAYLOAD_LINK))
149 return createSceneMemberRequestHandler(request, attributes);
152 if(attributes.contains(SCENE_VALUES))
154 return createSceneRequestHandler(request, attributes);
157 if(attributes.contains(LAST_SCENE))
159 return executeSceneRequestHandler(request, attributes);
163 return RCSSetResponse::create(attributes, (int)OC_STACK_ERROR)
164 .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
168 SceneCollection::createSceneMemberRequestHandler(
169 const RCSRequest &, RCSResourceAttributes & attributes)
171 std::string request_key = attributes.at(SCENE_PAYLOAD_LINK).get<std::string>();
172 // TODO create scene member
174 attributes.at(SCENE_COLLECTION_ID) = m_Id;
176 return RCSSetResponse::create(attributes)
177 .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
181 SceneCollection::createSceneRequestHandler(
182 const RCSRequest &, RCSResourceAttributes & attributes)
184 std::vector<std::string> request_key
185 = attributes.at(SCENE_VALUES).get<std::vector<std::string>>();
187 std::vector<std::string> newScene;
188 std::vector<std::string>::iterator iter = request_key.begin();
189 std::vector<Scene::Ptr>::iterator foundScene;
193 bool operator()(Scene::Ptr scene) const { return scene->getName() == name; }
197 while(iter != request_key.end())
199 FindSceneName fScene;
201 foundScene = std::find_if(m_SceneList.begin(), m_SceneList.end(), fScene);
202 if(foundScene == m_SceneList.end())
204 newScene.push_back(*iter);
209 for(unsigned int i = 0; i < newScene.size(); ++i)
212 newScene[i]; //Scene Value
217 return RCSSetResponse::create(attributes)
218 .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
222 SceneCollection::executeSceneRequestHandler(
223 const RCSRequest &, RCSResourceAttributes & attributes)
225 std::string request_key = attributes.at(LAST_SCENE).get<std::string>();
226 m_sceneCollectionResourcePtr->setAttribute(LAST_SCENE, request_key);
228 // TODO execute scene
231 return RCSSetResponse::create(attributes)
232 .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
234 } /* namespace Service */
235 } /* namespace OIC */