SceneManager init Code
[platform/upstream/iotivity.git] / service / scene-manager / src / SceneCollection.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 "SceneCollection.h"
22
23 #include "uuid/uuid.h"
24 #include "octypes.h"
25 #include "ocrandom.h"
26
27 namespace OIC
28 {
29     namespace Service
30     {
31         namespace
32         {
33             unsigned int numSceneCollection = 0;
34
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";
43
44             const std::string SCENE_COLLECTION_RT = "oic.wk.scenecollection";
45
46
47             std::string OICGenerateUUIDStr()
48             {
49                 uint8_t uuid[UUID_SIZE] = { 0, };
50                 char uuidStr[UUID_STRING_SIZE] = { 0, };
51                 if(RAND_UUID_OK == OCGenerateUuid(uuid))
52                 {
53                     if(RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
54                     {
55                         return std::string(uuidStr);
56                     }
57                 }
58                 return "";
59             }
60         }
61
62         SceneCollection::SceneCollection()
63         : m_Id(), m_Uri(PREFIX_SCENE_COLLECTION_URI + std::to_string(numSceneCollection++))
64         {
65             m_Id = OICGenerateUUIDStr();
66             if(m_Id == "")
67             {
68                 // TODO handle uuid creation fail.
69             }
70
71             m_sceneCollectionResourcePtr = RCSResourceObject::Builder(m_Uri, SCENE_COLLECTION_RT,
72                     OC_RSRVD_INTERFACE_DEFAULT).setDiscoverable(true).setObservable(true).build();
73
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);
79
80
81             m_sceneCollectionResourcePtr->setSetRequestHandler(
82                     std::bind(
83                             &SceneCollection::setRequestHandler, this,
84                             std::placeholders::_1, std::placeholders::_2));
85         }
86
87         const std::vector< Scene::Ptr >& SceneCollection::getSceneList()
88         {
89             return m_SceneList;
90         }
91
92         void SceneCollection::setName(const std::string& sceneCollectionName)
93         {
94             m_sceneCollectionResourcePtr->setAttribute(SCENE_COLLECTION_NAME, sceneCollectionName);
95         }
96
97         std::string SceneCollection::getName() const
98         {
99             return m_sceneCollectionResourcePtr->
100                     getAttributeValue(SCENE_COLLECTION_NAME).get<std::string>();
101         }
102
103         std::string SceneCollection::getUri() const
104         {
105             return m_Uri;
106         }
107
108         std::string SceneCollection::getId() const
109         {
110             return m_Id;
111         }
112
113         Scene::Ptr SceneCollection::addScene(const std::string& sceneName)
114         {
115             Scene::Ptr m_scene = std::make_shared < Scene > (sceneName);
116             m_SceneList.push_back(m_scene);
117             return m_scene;
118         }
119
120         bool SceneCollection::removeScene(const Scene::Ptr& it)
121         {
122             // remove scene
123             // remove scene Name list
124         }
125
126         SceneMemberObject::Ptr SceneCollection::addSceneMember(
127                 const RCSRemoteResourceObject::Ptr& it)
128         {
129             auto newSceneMemberObj = std::make_shared < SceneMemberObject > (it);
130             m_SceneMemberObjectList.push_back(newSceneMemberObj);
131             return newSceneMemberObj;
132         }
133
134         bool SceneCollection::removeSceneMember(SceneMemberObject::Ptr it)
135         {
136         }
137
138         const std::vector< SceneMemberObject::Ptr >& SceneCollection::getSceneMemberList()
139         {
140             return m_SceneMemberObjectList;
141         }
142
143         RCSSetResponse
144         SceneCollection::setRequestHandler(
145                 const RCSRequest & request, RCSResourceAttributes & attributes)
146         {
147             if(attributes.contains(SCENE_PAYLOAD_LINK))
148             {
149                 return createSceneMemberRequestHandler(request, attributes);
150             }
151
152             if(attributes.contains(SCENE_VALUES))
153             {
154                 return createSceneRequestHandler(request, attributes);
155             }
156
157             if(attributes.contains(LAST_SCENE))
158             {
159                 return executeSceneRequestHandler(request, attributes);
160             }
161
162
163             return RCSSetResponse::create(attributes, (int)OC_STACK_ERROR)
164             .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
165         }
166
167         RCSSetResponse
168         SceneCollection::createSceneMemberRequestHandler(
169                 const RCSRequest &, RCSResourceAttributes & attributes)
170         {
171             std::string request_key = attributes.at(SCENE_PAYLOAD_LINK).get<std::string>();
172             // TODO create scene member
173
174             attributes.at(SCENE_COLLECTION_ID) = m_Id;
175
176             return RCSSetResponse::create(attributes)
177             .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
178         }
179
180         RCSSetResponse
181         SceneCollection::createSceneRequestHandler(
182                 const RCSRequest &, RCSResourceAttributes & attributes)
183         {
184             std::vector<std::string> request_key
185                 = attributes.at(SCENE_VALUES).get<std::vector<std::string>>();
186
187             std::vector<std::string> newScene;
188             std::vector<std::string>::iterator iter = request_key.begin();
189             std::vector<Scene::Ptr>::iterator foundScene;
190
191             struct FindSceneName
192             {
193                 bool operator()(Scene::Ptr scene) const { return scene->getName() == name; }
194                 std::string name;
195             };
196
197             while(iter != request_key.end())
198             {
199                 FindSceneName fScene;
200                 fScene.name = *iter;
201                 foundScene = std::find_if(m_SceneList.begin(), m_SceneList.end(), fScene);
202                 if(foundScene == m_SceneList.end())
203                 {
204                     newScene.push_back(*iter);
205                 }
206                 iter++;
207             }
208
209             for(unsigned int i = 0; i < newScene.size(); ++i)
210             {
211                 // TODO create scene
212                 newScene[i]; //Scene Value
213             }
214
215
216             // success
217             return RCSSetResponse::create(attributes)
218             .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
219         }
220
221         RCSSetResponse
222         SceneCollection::executeSceneRequestHandler(
223                 const RCSRequest &, RCSResourceAttributes & attributes)
224         {
225             std::string request_key = attributes.at(LAST_SCENE).get<std::string>();
226             m_sceneCollectionResourcePtr->setAttribute(LAST_SCENE, request_key);
227
228             // TODO execute scene
229
230             // success
231             return RCSSetResponse::create(attributes)
232             .setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
233         }
234     } /* namespace Service */
235 } /* namespace OIC */
236