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"
25 std::string getOperationStateString(OperationState state)
29 case OP_START: return "OP_START";
30 case OP_COMPLETE: return "OP_COMPLETE";
31 case OP_ABORT: return "OP_ABORT";
37 class AppLogger : public ILogger
40 void write(std::string time, ILogger::Level level, std::string message)
42 std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
45 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
47 class ClientController
57 std::cout << "Enter your choice: ";
59 if (choice < 0 || choice > 14)
61 std::cout << "Invaild choice !" << std::endl; continue;
66 case 1: findResource(); break;
67 case 2: displayResource(); break;
68 case 3: observeResource(); break;
69 case 4: cancelObserving(); break;
70 case 5: sendGet(); break;
71 case 6: sendPut(); break;
72 case 7: sendPost(); break;
73 case 8: sendAllGETRequests(); break;
74 case 9: sendAllPUTRequests(); break;
75 case 10: sendAllPOSTRequests(); break;
76 case 11: configure(); break;
77 case 12: getDeviceInfo(); break;
78 case 13: getPlatformInfo(); break;
79 case 14: printMenu(); break;
88 std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
89 std::cout << "1. Find resource" << std::endl;
90 std::cout << "2. Display resource information" << std::endl;
91 std::cout << "3. Observe for resource change" << std::endl;
92 std::cout << "4. Cancel observation" << std::endl;
93 std::cout << "5. Send GET message" << std::endl;
94 std::cout << "6. Send PUT message" << std::endl;
95 std::cout << "7. Send POST message" << std::endl;
96 std::cout << "8. Send All GET requests" << std::endl;
97 std::cout << "9. Send All PUT requests" << std::endl;
98 std::cout << "10. Send All POST requests" << std::endl;
99 std::cout << "11. Configure (using RAML file)" << std::endl;
100 std::cout << "12. Get Device Information" << std::endl;
101 std::cout << "13. Get Platform Information" << std::endl;
102 std::cout << "14: Help" << std::endl;
103 std::cout << "0. Exit" << std::endl;
104 std::cout << "###################################################" << std::endl;
107 SimulatorRemoteResourceSP selectResource()
109 std::lock_guard<std::recursive_mutex> lock(m_mutex);
110 if (0 == m_resList.size())
112 std::cout << "No resources!" << std::endl;
117 std::vector<std::string> ids;
118 for (auto &resourceEntry : m_resList)
120 std::cout << index++ << ": " << (resourceEntry.second)->getURI() << "[" <<
121 (resourceEntry.second)->getHost() << "]" << std::endl;
122 ids.push_back((resourceEntry.second)->getID());
126 std::cout << "Choose the resource: ";
129 if (choice < 1 || choice > index - 1)
131 std::cout << "Invalid choice !" << std::endl;
135 return m_resList[ids[choice - 1]];
140 std::string resourceType;
141 std::cout << "Enter resource type : ";
142 std::cin >> resourceType;
144 ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
146 std::cout << "Resource found ######" << std::endl;
147 displayResource(resource);
150 std::lock_guard<std::recursive_mutex> lock(m_mutex);
151 if (m_resList.end() == m_resList.find(resource->getID()))
152 m_resList[resource->getID()] = resource;
154 std::cout << "Resource with UID: " << resource->getID() << "already exist in the list!" <<
160 SimulatorManager::getInstance()->findResource(resourceType, callback);
161 std::cout << "SimulatorManager::findResource is successful" << std::endl;
164 catch (InvalidArgsException &e)
166 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
169 catch (SimulatorException &e)
171 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
176 void displayResource()
178 displayResource(selectResource());
181 void displayResource(SimulatorRemoteResourceSP resource)
183 if (!resource) return;
185 std::cout << "#############################" << std::endl;
186 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
187 std::cout << "Host: " << resource->getHost().c_str() << std::endl;
188 std::cout << "ID: " << resource->getID().c_str() << std::endl;
189 std::cout << "Resource Types: ";
190 for (auto &type : resource->getResourceTypes())
191 std::cout << type << " ";
192 std::cout << "\nInterface Types: ";
193 for (auto &type : resource->getInterface())
194 std::cout << type << " ";
195 std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable()
196 << std::noboolalpha << std::endl;
197 std::cout << "#############################" << std::endl;
200 void observeResource()
202 SimulatorRemoteResourceSP resource = selectResource();
203 if (!resource) return;
205 // callback implementaion
206 SimulatorRemoteResource::ObserveNotificationCallback callback =
207 [](const std::string & uid, SimulatorResult result,
208 const SimulatorResourceModel & rep, int seq)
210 std::cout << "\nObserve notification received ###[errorcode: " << result <<
211 " seq: " << seq << "UID: " << uid << "]" << std::endl;
213 std::cout << "Representation is: " << std::endl;
214 std::cout << rep.asString() << std::endl;
219 resource->observe(ObserveType::OBSERVE, callback);
220 std::cout << "Observe is successful!" << std::endl;
222 catch (InvalidArgsException &e)
224 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
225 << e.what() << "]" << std::endl;
227 catch (SimulatorException &e)
229 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
230 e.what() << "]" << std::endl;
234 void cancelObserving()
236 SimulatorRemoteResourceSP resource = selectResource();
237 if (!resource) return;
241 resource->cancelObserve();
242 std::cout << "Cancelling observe is successful!" << std::endl;
244 catch (SimulatorException &e)
246 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
247 e.what() << "]" << std::endl;
253 SimulatorRemoteResourceSP resource = selectResource();
254 if (!resource) return;
256 // callback implementaion
257 SimulatorRemoteResource::ResponseCallback callback =
258 [](const std::string & uid, SimulatorResult result, const SimulatorResourceModel & rep)
260 std::cout << "\nGET Response received ### [errorcode: " << result << "]"
262 std::cout << "UID is: " << uid << std::endl;
263 std::cout << "Representation is: " << std::endl;
264 std::cout << rep.asString() << std::endl;
269 resource->get(std::map <std::string, std::string>(), callback);
270 std::cout << "GET is successful!" << std::endl;
272 catch (InvalidArgsException &e)
274 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
275 << e.what() << "]" << std::endl;
277 catch (NoSupportException &e)
279 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
280 e.what() << "]" << std::endl;
282 catch (SimulatorException &e)
284 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
285 e.what() << "]" << std::endl;
291 SimulatorRemoteResourceSP resource = selectResource();
292 if (!resource) return;
294 // callback implementaion
295 SimulatorRemoteResource::ResponseCallback callback =
296 [](const std::string & uid, SimulatorResult result, const SimulatorResourceModel & rep)
298 std::cout << "\nPUT Response received ![errorcode: " << result << "]"
300 std::cout << "UID is: " << uid << std::endl;
301 std::cout << "Representation is: " << std::endl;
302 std::cout << rep.asString() << std::endl;
307 SimulatorResourceModel rep;
308 rep.add("power", false);
309 rep.add("intensity", 15);
311 resource->put(rep, callback);
312 std::cout << "PUT is successful!" << std::endl;
314 catch (InvalidArgsException &e)
316 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
317 << e.what() << "]" << std::endl;
319 catch (NoSupportException &e)
321 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
322 e.what() << "]" << std::endl;
324 catch (SimulatorException &e)
326 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
327 e.what() << "]" << std::endl;
333 SimulatorRemoteResourceSP resource = selectResource();
334 if (!resource) return;
336 // callback implementaion
337 SimulatorRemoteResource::ResponseCallback callback =
338 [](const std::string & uid, SimulatorResult result, const SimulatorResourceModel & rep)
340 std::cout << "\nPOST Response received ![errorcode: " << result << "]"
342 std::cout << "UID is: " << uid << std::endl;
343 std::cout << "Representation is: " << std::endl;
344 std::cout << rep.asString() << std::endl;
349 SimulatorResourceModel rep;
350 rep.add("power", true);
351 rep.add("intensity", 17);
353 resource->post(rep, callback);
354 std::cout << "POST is successful!" << std::endl;
356 catch (InvalidArgsException &e)
358 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
359 << e.what() << "]" << std::endl;
361 catch (NoSupportException &e)
363 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
364 e.what() << "]" << std::endl;
366 catch (SimulatorException &e)
368 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
369 e.what() << "]" << std::endl;
373 void sendAllGETRequests()
375 SimulatorRemoteResourceSP resource = selectResource();
376 if (!resource) return;
378 SimulatorRemoteResource::AutoRequestGenerationCallback callback =
379 [] (const std::string & uid, int sessionId, OperationState state)
381 std::cout << "\nResource verification status received ![id: " << sessionId <<
382 " State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
388 int id = resource->startAutoRequesting(RequestType::RQ_TYPE_GET, callback);
389 std::cout << "startVerification for GET is successful!id: " << id << std::endl;
391 catch (InvalidArgsException &e)
393 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
394 << e.what() << "]" << std::endl;
396 catch (NoSupportException &e)
398 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
399 e.what() << "]" << std::endl;
401 catch (SimulatorException &e)
403 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
404 e.what() << "]" << std::endl;
408 void sendAllPUTRequests()
410 SimulatorRemoteResourceSP resource = selectResource();
411 if (!resource) return;
413 SimulatorRemoteResource::AutoRequestGenerationCallback callback =
414 [] (const std::string & uid, int sessionId, OperationState state)
416 std::cout << "\nResource verification status received ![id: " << sessionId <<
417 " State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
423 int id = resource->startAutoRequesting(RequestType::RQ_TYPE_PUT, callback);
424 std::cout << "startVerification for PUT is successful!id: " << id << std::endl;
426 catch (InvalidArgsException &e)
428 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
429 << e.what() << "]" << std::endl;
431 catch (NoSupportException &e)
433 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
434 e.what() << "]" << std::endl;
436 catch (SimulatorException &e)
438 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
439 e.what() << "]" << std::endl;
443 void sendAllPOSTRequests()
445 SimulatorRemoteResourceSP resource = selectResource();
446 if (!resource) return;
448 SimulatorRemoteResource::AutoRequestGenerationCallback callback =
449 [] (const std::string & uid, int sessionId, OperationState state)
451 std::cout << "\nResource verification status received ![id: " << sessionId <<
452 " State: " << getOperationStateString(state) << " UID: " << uid << "]"
458 int id = resource->startAutoRequesting(RequestType::RQ_TYPE_POST, callback);
459 std::cout << "startVerification for POST is successful!id: " << id << std::endl;
461 catch (InvalidArgsException &e)
463 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
464 << e.what() << "]" << std::endl;
466 catch (NoSupportException &e)
468 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
469 e.what() << "]" << std::endl;
471 catch (SimulatorException &e)
473 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
474 e.what() << "]" << std::endl;
480 SimulatorRemoteResourceSP resource = selectResource();
486 std::string configPath;
487 std::cout << "Enter the config path: ";
488 std::cin >> configPath;
490 resource->configure(configPath);
492 catch (InvalidArgsException &e)
494 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
495 << e.what() << "]" << std::endl;
497 catch (SimulatorException &e)
499 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
500 e.what() << "]" << std::endl;
506 SimulatorRemoteResourceSP resource = selectResource();
512 SimulatorManager::getInstance()->getDeviceInfo(resource->getHost(),
513 std::bind([](const std::string & host, DeviceInfo & deviceInfo)
515 std::cout << "###Device Information received...." << std::endl;
516 std::ostringstream out;
517 out << "Host URI: " << host << std::endl;
518 out << "Device name: " << deviceInfo.getName() << std::endl;
519 out << "Device ID: " << deviceInfo.getID() << std::endl;
520 out << "Device Spec version: " << deviceInfo.getSpecVersion() << std::endl;
521 out << "Device dat model version: " << deviceInfo.getDataModelVersion() << std::endl;
523 std::cout << out.str() << std::endl;
524 }, std::placeholders::_1, std::placeholders::_2));
526 catch (InvalidArgsException &e)
528 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
531 catch (SimulatorException &e)
533 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
538 void getPlatformInfo()
540 SimulatorRemoteResourceSP resource = selectResource();
546 SimulatorManager::getInstance()->getPlatformInfo(resource->getHost(),
547 std::bind([](const std::string & host, PlatformInfo & platformInfo)
549 std::cout << "###Platform Information received...." << std::endl;
550 std::ostringstream out;
551 out << "Host URI: " << host << std::endl;
552 out << "Platform ID: " << platformInfo.getPlatformID() << std::endl;
553 out << "Platform version: " << platformInfo.getPlatformVersion() << std::endl;
554 out << "Manufacturer name: " << platformInfo.getManufacturerName() << std::endl;
555 out << "Manufacturer url: " << platformInfo.getManufacturerUrl() << std::endl;
556 out << "Model number: " << platformInfo.getModelNumber() << std::endl;
557 out << "Date of manufacture: " << platformInfo.getDateOfManfacture() << std::endl;
558 out << "Operatio system version: " << platformInfo.getOSVersion() << std::endl;
559 out << "Hardware version: " << platformInfo.getHardwareVersion() << std::endl;
560 out << "Firmware version: " << platformInfo.getFirmwareVersion() << std::endl;
561 out << "Support url: " << platformInfo.getSupportUrl() << std::endl;
562 out << "System time: " << platformInfo.getSystemTime() << std::endl;
564 std::cout << out.str() << std::endl;
565 }, std::placeholders::_1, std::placeholders::_2));
567 catch (InvalidArgsException &e)
569 std::cout << "InvalidArgsException occured [code : " << e.code()
570 << " Detail: " << e.what() << "]" << std::endl;
572 catch (SimulatorException &e)
574 std::cout << "SimulatorException occured [code : " << e.code()
575 << " Detail: " << e.what() << "]" << std::endl;
580 std::recursive_mutex m_mutex;
581 std::map<std::string, SimulatorRemoteResourceSP> m_resList;
586 std::cout << "############### MAIN MENU###############" << std::endl;
587 std::cout << "1. Client Controller Test" << std::endl;
588 std::cout << "2. Set Logger" << std::endl;
589 std::cout << "3. Help" << std::endl;
590 std::cout << "0. Exit" << std::endl;
591 std::cout << "######################################" << std::endl;
596 std::cout << "1. Default console logger" << std::endl;
597 std::cout << "2. Default file logger" << std::endl;
598 std::cout << "3. custom logger" << std::endl;
602 if (choice <= 0 || choice > 3)
604 std::cout << "Invalid selection !" << std::endl;
612 if (false == SimulatorManager::getInstance()->setConsoleLogger())
613 std::cout << "Failed to set the default console logger" << std::endl;
619 std::string filePath;
620 std::cout << "Enter the file path (without file name) : ";
621 std::cin >> filePath;
622 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
623 std::cout << "Failed to set default file logger" << std::endl;
628 SimulatorManager::getInstance()->setLogger(gAppLogger);
634 ClientController clientController;
640 std::cout << "Enter your choice: ";
642 if (choice < 0 || choice > 5)
644 std::cout << "Invaild choice !" << std::endl; continue;
649 case 1: clientController.startTest();
650 std::cout << "Welcome back to main menu !" << std::endl;
653 case 2: setLogger(); break;
655 case 3: printMainMenu(); break;
657 case 0: cont = false;
661 std::cout << "Terminating test !!!" << std::endl;