1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 "simulator_manager.h"
23 std::vector<SimulatorSingleResourceSP> g_singleResources;
24 std::vector<SimulatorCollectionResourceSP> g_collectionResources;
26 class AppLogger : public ILogger
29 void write(std::string time, ILogger::Level level, std::string message)
31 std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " "
35 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
39 if (0 == g_singleResources.size())
41 std::cout << "No resouces!" << std::endl;
46 for (auto &resource : g_singleResources)
48 std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
52 std::cout << "Choose the resource: ";
55 if (choice < 1 || choice > index - 1)
57 std::cout << "Invalid choice !" << std::endl;
64 void simulateResource()
68 // Resource model change callback
69 SimulatorResource::ResourceModelUpdateCallback modelChangeCB =
70 [](const std::string & uri, const SimulatorResourceModel & resModel)
72 std::cout << "[callback] Resource model is changed URI: " << uri.c_str() << std::endl;
73 std::cout << "#### Modified attributes are ####" << std::endl;
74 std::cout << "#### Updated resource model ####" << std::endl;
75 std::cout << resModel.asString() << std::endl;
76 std::cout << "########################" << std::endl;
79 // Observer added/removed callback
80 SimulatorResource::ObserverCallback observerCB =
81 [] (const std::string & uri, ObservationStatus state, const ObserverInfo & observerInfo)
83 std::cout << "[callback] Observer notification received..." << uri << std::endl;
85 std::ostringstream out;
86 out << "ID: " << (int) observerInfo.id << std::endl;
87 out << " [address: " << observerInfo.address << " port: " << observerInfo.port
89 out << "State: " << ((state == ObservationStatus::REGISTER) ? "REGISTER" : "UNREGISTER") <<
91 std::cout << out.str();
94 // Get the RAML file path from user
95 std::string configPath;
96 std::cout << "Enter RAML path: ";
97 std::cin >> configPath;
99 SimulatorResourceSP resource =
100 SimulatorManager::getInstance()->createResource(configPath);
102 // Add resource to appropriate list
103 if (SimulatorResource::Type::SINGLE_RESOURCE == resource->getType())
105 std::cout << "Single type resource created [URI: " << resource->getURI() << " ]" << std::endl;
106 SimulatorSingleResourceSP singleRes =
107 std::dynamic_pointer_cast<SimulatorSingleResource>(resource);
110 std::cout << "Error occured while converting SimulatorResource to SimulatorSingleResource!" << std::endl;
114 singleRes->setModelChangeCallback(modelChangeCB);
115 singleRes->setObserverCallback(observerCB);
116 g_singleResources.push_back(singleRes);
120 std::cout << "Collection type resource created [URI: " << resource->getURI() << " ]" << std::endl;
121 SimulatorCollectionResourceSP collectionRes =
122 std::dynamic_pointer_cast<SimulatorCollectionResource>(resource);
125 std::cout << "Error occured while converting SimulatorResource to SimulatorCollectionResource!" << std::endl;
129 collectionRes->setObserverCallback(observerCB);
130 g_collectionResources.push_back(collectionRes);
133 catch (InvalidArgsException &e)
135 std::cout << "InvalidArgsException occured [code : " << e.code() << " Details: "
136 << e.what() << "]" << std::endl;
138 catch (SimulatorException &e)
140 std::cout << "SimulatorException occured [code : " << e.code() << " Details: "
141 << e.what() << "]" << std::endl;
145 void displayResource()
147 int index = selectResource();
151 SimulatorSingleResourceSP resource = g_singleResources[index - 1];
153 std::cout << "#############################" << std::endl;
154 std::cout << "Name: " << resource->getName() << std::endl;
155 std::cout << "URI: " << resource->getURI() << std::endl;
156 std::cout << "Resource type: " << resource->getResourceType() << std::endl;
157 std::cout << "Interface type:";
158 for (auto &interfaceType : resource->getInterface())
159 std::cout << " " << interfaceType << std::endl;
162 std::cout << "##### Representation #####" << std::endl;
163 std::cout << resource->getResourceModel().asString() << std::endl;
164 std::cout << "#############################" << std::endl;
169 int index = selectResource();
173 SimulatorSingleResourceSP resource = g_singleResources[index - 1];
175 std::cout << "Resource started!" << std::endl;
180 int index = selectResource();
184 SimulatorSingleResourceSP resource = g_singleResources[index - 1];
186 std::cout << "Resource stopped!" << std::endl;
189 void automateResourceUpdate()
191 SimulatorSingleResource::AutoUpdateCompleteCallback callback =
192 [](const std::string & uri, const int id)
194 std::cout << "Update automation is completed [URI: " << uri
195 << " AutomationID: " << id << "] ###" << std::endl;
198 int index = selectResource();
202 AutoUpdateType type = AutoUpdateType::ONE_TIME;
204 std::cout << "Press 1 if you want recurrent automation: ";
207 type = AutoUpdateType::REPEAT;
211 int id = g_singleResources[index - 1]->startResourceUpdation(type, -1, callback);
213 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
215 catch (SimulatorException &e)
217 std::cout << "SimulatorException occured [code : " << e.code() << " Details: " <<
218 e.what() << "]" << std::endl;
222 void automateAttributeUpdate()
224 SimulatorSingleResource::AutoUpdateCompleteCallback callback =
225 [](const std::string & uri, const int id)
227 std::cout << "Update automation is completed [URI: " << uri
228 << " AutomationID: " << id << "] ###" << std::endl;
231 int index = selectResource();
235 SimulatorSingleResourceSP resource = g_singleResources[index - 1];
236 std::map<std::string, SimulatorResourceAttribute> attributes =
237 resource->getAttributes();
239 for (auto &attributeEntry : attributes)
241 std::cout << ++size << ": " << attributeEntry.first << std::endl;
246 std::cout << "This resource doest not contain any attributes!" << std::endl;
251 std::cout << "Select the attribute which you want to automate for updation: " <<
254 if (choice < 0 || choice > size)
256 std::cout << "Invalid selection!" << std::endl;
261 std::string attributeName;
262 for (auto &attributeEntry : attributes)
264 if (count == choice - 1)
266 attributeName = attributeEntry.first;
273 AutoUpdateType type = AutoUpdateType::ONE_TIME;
274 std::cout << "Press 1 if you want recurrent automation: ";
277 type = AutoUpdateType::REPEAT;
279 std::cout << "Requesting attribute automation for " << attributeName <<
284 int id = resource->startAttributeUpdation(attributeName, type, -1, callback);
285 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
287 catch (SimulatorException &e)
289 std::cout << "SimulatorException occured [Error: " << e.code() << " Details: " <<
290 e.what() << "]" << std::endl;
294 void stopAutomation()
296 int index = selectResource();
300 SimulatorSingleResourceSP resource = g_singleResources[index - 1];
302 // Select the automation to stop
303 std::vector<int> ids;
305 std::vector<int> rids = resource->getResourceUpdations();
306 std::vector<int> aids = resource->getAttributeUpdations();
307 ids.insert(ids.end(), rids.begin(), rids.end());
308 ids.insert(ids.end(), aids.begin(), aids.end());
313 std::cout << "No automation operation is going on this resource right now!" <<
320 std::cout << id << " ";
321 resource->stopUpdation(id);
327 int index = selectResource();
331 SimulatorSingleResourceSP resource = g_singleResources[index - 1];
333 std::vector<ObserverInfo> observersList = resource->getObservers();
335 std::cout << "##### Number of Observers [" << observersList.size() << "]" << std::endl;
336 for (auto &observerInfo : observersList)
338 std::cout << " ID : " << (int) observerInfo.id << " [address: " <<
339 observerInfo.address << " port: " << observerInfo.port << "]" << std::endl;
341 std::cout << "########################" << std::endl;
346 std::cout << "############### MAIN MENU###############" << std::endl;
347 std::cout << "1. Simulate resource" << std::endl;
348 std::cout << "2. Display resource information" << std::endl;
349 std::cout << "3. Start resource" << std::endl;
350 std::cout << "4. Stop resource" << std::endl;
351 std::cout << "5. Automate resource update" << std::endl;
352 std::cout << "6. Automate attributes update" << std::endl;
353 std::cout << "7. Stop Automation" << std::endl;
354 std::cout << "8. Get Observers of a resource" << std::endl;
355 std::cout << "9. Set Logger" << std::endl;
356 std::cout << "10. Set Device Info" << std::endl;
357 std::cout << "11. Set Platform Info" << std::endl;
358 std::cout << "12. Add Interface" << std::endl;
359 std::cout << "13. Help" << std::endl;
360 std::cout << "0. Exit" << std::endl;
361 std::cout << "######################################" << std::endl;
366 std::cout << "1. Default console logger" << std::endl;
367 std::cout << "2. Default file logger" << std::endl;
368 std::cout << "3. custom logger" << std::endl;
372 if (choice <= 0 || choice > 3)
374 std::cout << "Invalid selection !" << std::endl;
382 if (false == SimulatorManager::getInstance()->setConsoleLogger())
383 std::cout << "Failed to set the default console logger" << std::endl;
387 std::string filePath;
388 std::cout << "Enter the file path (without file name) : ";
389 std::cin >> filePath;
390 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
391 std::cout << "Failed to set default file logger" << std::endl;
393 case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
401 SimulatorManager::getInstance()->setDeviceInfo("IoTivity Simulator Linux Sample");
402 std::cout << "Setting Device Info is successful" << std::endl;
404 catch (InvalidArgsException &e)
406 std::cout << "InvalidArgsException occured [code : " << e.code() << " Details: "
407 << e.what() << "]" << std::endl;
409 catch (SimulatorException &e)
411 std::cout << "SimulatorException occured [code : " << e.code() << " Details: "
412 << e.what() << "]" << std::endl;
416 void setPlatformInfo()
419 pInfo.setPlatformID("Samsung Platform Identifier");
420 pInfo.setFirmwareVersion("FirwareVersion01");
421 pInfo.setHardwareVersion("HardwareVersion01");
422 pInfo.setManufacturerName("Samsung");
423 pInfo.setManufacturerUrl("www.samsung.com");
424 pInfo.setModelNumber("Samsung Model Num01");
425 pInfo.setOSVersion("OSVersion01");
426 pInfo.setPlatformVersion("PlatformVersion01");
427 pInfo.setSupportUrl("http://www.samsung.com/support");
428 pInfo.setSystemTime("2015-09-10T11:10:30Z");
429 pInfo.setDateOfManfacture("2015-09-10T11:10:30Z");
433 SimulatorManager::getInstance()->setPlatformInfo(pInfo);
434 std::cout << "Setting Platform Info is successful" << std::endl;
436 catch (SimulatorException &e)
438 std::cout << "SimulatorException occured [code : " << e.code() << " Details: "
439 << e.what() << "]" << std::endl;
446 int index = selectResource();
450 SimulatorSingleResourceSP resource = g_singleResources[index - 1];
451 resource->addInterface("oic.if.s");
452 resource->addInterface("oic.if.a");
455 int main(int argc, char *argv[])
462 std::cout << "Enter your choice: ";
464 if (choice < 0 || choice > 14)
466 std::cout << "Invaild choice !" << std::endl; continue;
471 case 1 : simulateResource(); break;
472 case 2 : displayResource(); break;
473 case 3 : startResource(); break;
474 case 4 : stopResource(); break;
475 case 5 : automateResourceUpdate(); break;
476 case 6 : automateAttributeUpdate(); break;
477 case 7 : stopAutomation(); break;
478 case 8 : getObservers(); break;
479 case 9 : setLogger(); break;
480 case 10: setDeviceInfo(); break;
481 case 11: setPlatformInfo(); break;
482 case 12: addInterface(); break;
483 case 13: printMainMenu(); break;
484 case 0: cont = false;
488 std::cout << "Terminating test !!!" << std::endl;