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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include <condition_variable>
26 #include "OCPlatform.h"
27 #include "RCSDiscoveryManager.h"
28 #include "RCSRemoteResourceObject.h"
29 #include "RCSAddress.h"
30 #include "RemoteSceneList.h"
33 using namespace OIC::Service;
35 constexpr int CREATE_REMOTE_SCENE_LIST = 1;
37 constexpr int CREATE_REMOTE_SCENE_COLLECTION = 1;
38 constexpr int SHOW_REMOTE_SCENE_COLLECTION = 2;
40 constexpr int CREATE_REMOTE_SCENE = 1;
41 constexpr int CREATE_REMOTE_SCENE_ACTION = 1;
43 constexpr int EXECUTE_REMOTE_SCENE = 1;
45 constexpr int SCENE_RESULT_OK = 200;
47 constexpr int numCreatedSceneAction = 2;
48 static int numRecvSceneActionCreationResp = 0;
50 typedef std::function< void() > Run;
53 const std::string scene_name = "Night mode";
54 const std::string relativetUri = OC_RSRVD_WELL_KNOWN_URI;
55 const std::vector<std::string> resourceTypes{ "oic.wk.scenelist", "core.light", "core.fan" };
58 std::mutex g_discoverymtx;
59 std::condition_variable g_cond;
61 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> g_discoveryTask;
63 RCSRemoteResourceObject::Ptr g_foundListResource;
64 RCSRemoteResourceObject::Ptr g_foundLightResource;
65 RCSRemoteResourceObject::Ptr g_foundFanResource;
67 RemoteSceneList::Ptr g_sceneList;
68 RemoteSceneCollection::Ptr g_sceneCollection;
69 RemoteScene::Ptr g_scene;
71 void displaySceneList();
72 void runCreateRemoteSceneList();
73 void runRemoteSceneCollection();
74 void runCreateRemoteScene();
75 void runCreateRemoteSceneAction();
76 void runExecuteCreatedRemoteScene();
77 void runExecuteExistingRemoteScene();
79 // Scene Manager Remote API sample ---
80 void onRemoteSceneListCreated(RemoteSceneList::Ptr remoteSceneList, int eCode)
82 std::cout << __func__ << " - error code : " << eCode << std::endl;
84 if (eCode == SCENE_RESULT_OK)
86 g_sceneList = std::move(remoteSceneList);
87 g_currentRun = runRemoteSceneCollection;
91 std::cout << "Create Remote scene list failed." << std::endl;
92 g_currentRun = runCreateRemoteSceneList;
97 void onRemoteSceneCollectionCreated(RemoteSceneCollection::Ptr remoteSceneCol, int eCode)
99 std::cout << __func__ << " - error code : " << eCode << std::endl;
101 if (eCode == SCENE_RESULT_OK)
103 g_sceneCollection = remoteSceneCol;
104 g_currentRun = runCreateRemoteScene;
108 std::cout << "Create Remote scene collection failed." << std::endl;
109 g_currentRun = runRemoteSceneCollection;
115 void onRemoteSceneCreated(RemoteScene::Ptr remoteScene, int eCode)
117 std::cout << __func__ << " - error code : " << eCode << std::endl;
119 if (eCode == SCENE_RESULT_OK)
121 g_scene = remoteScene;
123 g_currentRun = runCreateRemoteSceneAction;
127 std::cout << "Create Remote scene failed." << std::endl;
128 g_currentRun = runCreateRemoteScene;
134 void onRemoteSceneActionCreated(RemoteSceneAction::Ptr, int eCode)
136 std::cout << __func__ << " - error code : " << eCode << std::endl;
138 if (eCode == SCENE_RESULT_OK)
140 g_currentRun = runExecuteCreatedRemoteScene;
144 std::cout << "Create Remote scene action failed." << std::endl;
145 g_currentRun = runCreateRemoteSceneAction;
148 numRecvSceneActionCreationResp++;
150 if(numCreatedSceneAction == numRecvSceneActionCreationResp)
154 void onRemoteSceneExecuted(const std::string &sceneName, int eCode)
156 std::cout << __func__ << " - scene name : " << sceneName
157 << ", error code : " << eCode << std::endl;
159 if (eCode != SCENE_RESULT_OK)
161 std::cout << "Execute scene failed." << std::endl;
167 // --- Scene Manager Remote API sample
169 void createRemoteSceneList()
171 if (g_foundListResource)
173 RemoteSceneList::createInstance(g_foundListResource, onRemoteSceneListCreated);
177 std::cout << "Scene List Resource is not discovered." << std::endl;
182 void createRemoteSceneCollection()
184 if (!g_sceneList) return;
186 g_sceneList->addNewSceneCollection(onRemoteSceneCollectionCreated);
189 void showRemoteSceneCollection()
191 if (!g_sceneList) return;
193 if (g_sceneList->getRemoteSceneCollections().size() == 0) return;
195 g_sceneCollection = g_sceneList->getRemoteSceneCollections().at(0);
197 if( g_sceneCollection->getRemoteScenes().size() == 0) return;
199 g_scene = g_sceneCollection->getRemoteScenes().begin()->second;
202 void createRemoteScene()
204 if (!g_sceneCollection) return;
206 g_sceneCollection->addNewScene(scene_name, onRemoteSceneCreated);
209 void createRemoteSceneAction(
210 RemoteScene::Ptr scene, RCSRemoteResourceObject::Ptr member,
211 const std::string &key, const std::string &value)
215 g_scene->addNewSceneAction(member, key, RCSResourceAttributes::Value(value),
216 onRemoteSceneActionCreated);
220 void createRemoteSceneActions()
222 createRemoteSceneAction(g_scene, g_foundLightResource, "power", "on");
223 createRemoteSceneAction(g_scene, g_foundFanResource, "speed", "50");
232 g_scene->execute(onRemoteSceneExecuted);
233 std::cout << "\n\t'" << g_scene->getName() << "' is executed!\n" << std::endl;
237 // --- Scene Manager Remote API sample
239 void configurePlatform()
241 PlatformConfig config
243 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos
245 OCPlatform::Configure(config);
248 int processUserInput(int min, int max)
256 if (!std::cin.fail())
258 if (input == max + 1) exit(0);
259 if (min <= input && input <= max) return input;
263 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
265 throw std::runtime_error("Invalid Input, please try again");
268 void displaySceneList()
270 if (!g_sceneList) return;
272 std::cout << "\t" << g_sceneList->getName() << "(SceneList)" << std::endl;
274 if (!g_sceneCollection) return;
276 std::cout << "\t\t |_ _ _ " << g_sceneCollection->getId() << " (SceneCollection)" << std::endl;
278 for( const auto &it_scene : g_sceneCollection->getRemoteScenes() )
280 std::cout << "\t\t\t |_ _ _ " << it_scene.first << " (Scene)" << std::endl;
282 auto sceneActionList = it_scene.second->getRemoteSceneActions();
283 for (const auto &it : sceneActionList)
285 auto attr = it->getExecutionParameter();
286 for (const auto &att : attr)
288 std::cout << "\t\t\t \t\t|_ _ _ ";
289 std::cout << it->getRemoteResourceObject()->getUri() << " : ";
290 std::cout << att.key() << " - " << att.value().toString() << std::endl;
296 void displayClear(Run runFunc)
298 auto ret = std::system("/usr/bin/clear");
301 std::cout << "clear error!" << std::endl;
303 g_currentRun = runFunc;
306 void displayCreateRemoteSceneListMenu()
308 std::cout << "========================================================\n";
309 std::cout << CREATE_REMOTE_SCENE_LIST << ". Create a RemoteSceneList \n";
310 std::cout << CREATE_REMOTE_SCENE_LIST + 1 << ". Quit \n";
311 std::cout << "========================================================\n";
314 void displayRemoteSceneCollectionMenu()
316 std::cout << "======================================================== \n";
317 std::cout << CREATE_REMOTE_SCENE_COLLECTION << ". Create a RemoteSceneCollection \n";
318 std::cout << SHOW_REMOTE_SCENE_COLLECTION << ". Show existing RemoteSceneCollection \n";
319 std::cout << SHOW_REMOTE_SCENE_COLLECTION + 1 << ". Quit \n";
320 std::cout << "======================================================== \n";
323 void displayRemoteSceneCreationMenu()
325 std::cout << "========================================================\n";
326 std::cout << CREATE_REMOTE_SCENE << ". Create a RemoteScene \n";
327 std::cout << CREATE_REMOTE_SCENE + 1 << ". Quit \n";
328 std::cout << "========================================================\n";
331 void displayRemoteSceneActionCreationMenu()
333 std::cout << "======================================================== \n";
334 std::cout << CREATE_REMOTE_SCENE_ACTION << ". Create RemoteSceneActions \n";
335 std::cout << CREATE_REMOTE_SCENE_ACTION + 1 << ". Quit \n";
336 std::cout << "======================================================== \n";
339 void displayExecuteCreatedRemoteSceneCreationMenu()
341 std::cout << "========================================================\n";
342 std::cout << EXECUTE_REMOTE_SCENE << ". Execute RemoteScene \n";
343 std::cout << EXECUTE_REMOTE_SCENE + 1 << ". Quit \n";
344 std::cout << "========================================================\n";
347 void displayExecuteExistingRemoteSceneCreationMenu()
349 std::cout << "========================================================\n";
350 std::cout << EXECUTE_REMOTE_SCENE << ". Execute a first RemoteScene \n";
351 std::cout << EXECUTE_REMOTE_SCENE + 1 << ". Quit \n";
352 std::cout << "========================================================\n";
355 void runExecuteExistingRemoteScene()
359 displayExecuteExistingRemoteSceneCreationMenu();
363 int command = processUserInput(EXECUTE_REMOTE_SCENE, EXECUTE_REMOTE_SCENE);
366 case EXECUTE_REMOTE_SCENE:
368 displayClear(runExecuteExistingRemoteScene);
371 } catch (std::exception &e)
373 std::cout << e.what() << std::endl;
378 void runExecuteCreatedRemoteScene()
382 displayExecuteCreatedRemoteSceneCreationMenu();
386 int command = processUserInput(EXECUTE_REMOTE_SCENE, EXECUTE_REMOTE_SCENE);
389 case EXECUTE_REMOTE_SCENE:
391 displayClear(runExecuteCreatedRemoteScene);
394 } catch (std::exception &e)
396 std::cout << e.what() << std::endl;
401 void runCreateRemoteSceneAction()
405 displayRemoteSceneActionCreationMenu();
409 int command = processUserInput(CREATE_REMOTE_SCENE_ACTION, CREATE_REMOTE_SCENE_ACTION);
412 case CREATE_REMOTE_SCENE_ACTION:
413 createRemoteSceneActions();
414 displayClear(runExecuteCreatedRemoteScene);
417 } catch (std::exception &e)
419 std::cout << e.what() << std::endl;
424 void runCreateRemoteScene()
428 displayRemoteSceneCreationMenu();
432 int command = processUserInput(CREATE_REMOTE_SCENE, CREATE_REMOTE_SCENE);
435 case CREATE_REMOTE_SCENE:
437 displayClear(runCreateRemoteSceneAction);
440 } catch (std::exception &e)
442 std::cout << e.what() << std::endl;
447 void runRemoteSceneCollection()
451 displayRemoteSceneCollectionMenu();
455 int command = processUserInput(CREATE_REMOTE_SCENE_COLLECTION, SHOW_REMOTE_SCENE_COLLECTION);
458 case CREATE_REMOTE_SCENE_COLLECTION:
459 createRemoteSceneCollection();
460 displayClear(runCreateRemoteScene);
462 case SHOW_REMOTE_SCENE_COLLECTION:
463 showRemoteSceneCollection();
464 displayClear(runExecuteExistingRemoteScene);
468 } catch (std::exception &e)
470 std::cout << e.what() << std::endl;
475 void runCreateRemoteSceneList()
477 displayCreateRemoteSceneListMenu();
481 int command = processUserInput(CREATE_REMOTE_SCENE_LIST, CREATE_REMOTE_SCENE_LIST);
484 case CREATE_REMOTE_SCENE_LIST:
485 createRemoteSceneList();
486 displayClear(runRemoteSceneCollection);
489 } catch (std::exception &e)
491 std::cout << e.what() << std::endl;
496 void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
498 std::lock_guard< std::mutex > lock(g_discoverymtx);
499 std::cout << "onResourceDiscovered callback" << std::endl;
501 std::string resourceURI = foundResource->getUri();
502 std::string hostAddress = foundResource->getAddress();
503 std::vector< std::string > vecRTs = foundResource->getTypes();
505 std::cout << "\t\tResource URI : " << resourceURI << std::endl;
506 std::cout << "\t\tResource Host : " << hostAddress << std::endl;
508 // if the found resource is a scene list resource
509 if (std::find(vecRTs.begin(), vecRTs.end(), "oic.wk.scenelist") != vecRTs.end())
510 g_foundListResource = foundResource;
512 // if the found resource is a light resource
513 else if (std::find(vecRTs.begin(), vecRTs.end(), "core.light") != vecRTs.end())
515 g_foundLightResource = foundResource;
518 // if the found resource is a fan resource
519 else if (std::find(vecRTs.begin(), vecRTs.end(), "core.fan") != vecRTs.end())
521 g_foundFanResource = foundResource;
524 if (g_foundListResource && g_foundLightResource && g_foundFanResource)
526 g_discoveryTask->cancel();
533 void discoverResource()
535 std::cout << "Wait 4 seconds until discovered." << std::endl;
540 = RCSDiscoveryManager::getInstance()->discoverResourceByTypes(RCSAddress::multicast(),
541 relativetUri, resourceTypes, &onResourceDiscovered);
543 catch (const RCSPlatformException &e)
545 std::cout << e.what() << std::endl;
547 std::unique_lock<std::mutex> lck(g_mtx);
549 g_cond.wait_for(lck, std::chrono::seconds(4));
561 g_currentRun = runCreateRemoteSceneList;
564 catch(std::exception &e)
566 std::cout << e.what() << std::endl;
572 std::cout << "Stopping the scene client" << std::endl;