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 std::string getPropertyTypeString(SimulatorResourceModel::AttributeProperty::Type type)
41 case SimulatorResourceModel::AttributeProperty::Type::RANGE:
43 case SimulatorResourceModel::AttributeProperty::Type::VALUE_SET:
52 class AppLogger : public ILogger
55 void write(std::string time, ILogger::Level level, std::string message)
57 std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
60 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
62 class ClientController
72 std::cout << "Enter your choice: ";
74 if (choice < 0 || choice > 14)
76 std::cout << "Invaild choice !" << std::endl; continue;
81 case 1: findResource(); break;
82 case 2: displayResource(); break;
83 case 3: observeResource(); break;
84 case 4: cancelObserving(); break;
85 case 5: sendGet(); break;
86 case 6: sendPut(); break;
87 case 7: sendPost(); break;
88 case 8: sendAllGETRequests(); break;
89 case 9: sendAllPUTRequests(); break;
90 case 10: sendAllPOSTRequests(); break;
91 case 11: configure(); break;
92 case 12: getDeviceInfo(); break;
93 case 13: getPlatformInfo(); break;
94 case 14: printMenu(); break;
103 std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
104 std::cout << "1. Find resource" << std::endl;
105 std::cout << "2. Display resource information" << std::endl;
106 std::cout << "3. Observe for resource change" << std::endl;
107 std::cout << "4. Cancel observation" << std::endl;
108 std::cout << "5. Send GET message" << std::endl;
109 std::cout << "6. Send PUT message" << std::endl;
110 std::cout << "7. Send POST message" << std::endl;
111 std::cout << "8. Send All GET requests" << std::endl;
112 std::cout << "9. Send All PUT requests" << std::endl;
113 std::cout << "10. Send All POST requests" << std::endl;
114 std::cout << "11. Configure (using RAML file)" << std::endl;
115 std::cout << "12. Get Device Information" << std::endl;
116 std::cout << "13. Get Platform Information" << std::endl;
117 std::cout << "14: Help" << std::endl;
118 std::cout << "0. Exit" << std::endl;
119 std::cout << "###################################################" << std::endl;
122 SimulatorRemoteResourceSP selectResource()
124 std::lock_guard<std::recursive_mutex> lock(m_mutex);
125 if (0 == m_resList.size())
127 std::cout << "No resources!" << std::endl;
132 std::vector<std::string> ids;
133 for (auto & resourceEntry : m_resList)
135 std::cout << index++ << ": " << (resourceEntry.second)->getURI() << "[" <<
136 (resourceEntry.second)->getHost() << "]" << std::endl;
137 ids.push_back((resourceEntry.second)->getID());
141 std::cout << "Choose the resource: ";
144 if (choice < 1 || choice > index - 1)
146 std::cout << "Invalid choice !" << std::endl;
150 return m_resList[ids[choice - 1]];
155 std::string resourceType;
156 std::cout << "Enter resource type : ";
157 std::cin >> resourceType;
159 ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
161 std::cout << "Resource found ######" << std::endl;
162 displayResource(resource);
165 std::lock_guard<std::recursive_mutex> lock(m_mutex);
166 if (m_resList.end() == m_resList.find(resource->getID()))
167 m_resList[resource->getID()] = resource;
169 std::cout << "Resource with UID: " << resource->getID() << "already exist in the list!" <<
175 SimulatorManager::getInstance()->findResource(resourceType, callback);
176 std::cout << "SimulatorManager::findResource is successful" << std::endl;
179 catch (InvalidArgsException &e)
181 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
184 catch (SimulatorException &e)
186 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
191 void displayResource()
193 displayResource(selectResource());
196 void displayResource(SimulatorRemoteResourceSP resource)
198 if (!resource) return;
200 std::cout << "#############################" << std::endl;
201 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
202 std::cout << "Host: " << resource->getHost().c_str() << std::endl;
203 std::cout << "ID: " << resource->getID().c_str() << std::endl;
204 std::cout << "Resource Types: ";
205 for (auto & type : resource->getResourceTypes())
206 std::cout << type << " ";
207 std::cout << "\nInterface Types: ";
208 for (auto & type : resource->getResourceInterfaces())
209 std::cout << type << " ";
210 std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable()
211 << std::noboolalpha << std::endl;
212 std::cout << "#############################" << std::endl;
215 void observeResource()
217 SimulatorRemoteResourceSP resource = selectResource();
218 if (!resource) return;
220 // callback implementaion
221 SimulatorRemoteResource::ObserveNotificationCallback callback =
222 [](std::string uid, SimulatorResult errorCode, SimulatorResourceModelSP rep, int seq)
224 std::cout << "\nObserve notification received ###[errorcode: " << errorCode <<
225 " seq: " << seq << "UID: " << uid << "]" << std::endl;
227 std::cout << "Representation is: " << std::endl;
228 std::cout << rep->toString() << std::endl;
233 resource->observe(ObserveType::OBSERVE, callback);
234 std::cout << "Observe is successful!" << std::endl;
236 catch (InvalidArgsException &e)
238 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
239 << e.what() << "]" << std::endl;
241 catch (SimulatorException &e)
243 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
244 e.what() << "]" << std::endl;
248 void cancelObserving()
250 SimulatorRemoteResourceSP resource = selectResource();
251 if (!resource) return;
255 resource->cancelObserve();
256 std::cout << "Cancelling observe is successful!" << std::endl;
258 catch (SimulatorException &e)
260 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
261 e.what() << "]" << std::endl;
267 SimulatorRemoteResourceSP resource = selectResource();
268 if (!resource) return;
270 // callback implementaion
271 SimulatorRemoteResource::ResponseCallback callback =
272 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
274 std::cout << "\nGET Response received ### [errorcode: " << errorCode << "]"
276 std::cout << "UID is: " << uId << std::endl;
277 std::cout << "Representation is: " << std::endl;
278 std::cout << rep->toString() << std::endl;
283 resource->get(std::map <std::string, std::string>(), callback);
284 std::cout << "GET is successful!" << std::endl;
286 catch (InvalidArgsException &e)
288 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
289 << e.what() << "]" << std::endl;
291 catch (NoSupportException &e)
293 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
294 e.what() << "]" << std::endl;
296 catch (SimulatorException &e)
298 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
299 e.what() << "]" << std::endl;
305 SimulatorRemoteResourceSP resource = selectResource();
306 if (!resource) return;
308 // callback implementaion
309 SimulatorRemoteResource::ResponseCallback callback =
310 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
312 std::cout << "\nPUT Response received ![errorcode: " << errorCode << "]"
314 std::cout << "UID is: " << uId << std::endl;
315 std::cout << "Representation is: " << std::endl;
316 std::cout << rep->toString() << std::endl;
321 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
323 rep->add("power", value);
324 rep->add("intensity", 15);
326 resource->put(std::map <std::string, std::string>(), rep, callback);
327 std::cout << "PUT is successful!" << std::endl;
329 catch (InvalidArgsException &e)
331 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
332 << e.what() << "]" << std::endl;
334 catch (NoSupportException &e)
336 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
337 e.what() << "]" << std::endl;
339 catch (SimulatorException &e)
341 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
342 e.what() << "]" << std::endl;
348 SimulatorRemoteResourceSP resource = selectResource();
349 if (!resource) return;
351 // callback implementaion
352 SimulatorRemoteResource::ResponseCallback callback =
353 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
355 std::cout << "\nPOST Response received ![errorcode: " << errorCode << "]"
357 std::cout << "UID is: " << uId << std::endl;
358 std::cout << "Representation is: " << std::endl;
359 std::cout << rep->toString() << std::endl;
364 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
366 rep->add("power", value);
367 rep->add("intensity", 17);
369 resource->post(std::map <std::string, std::string>(), rep, callback);
370 std::cout << "POST is successful!" << std::endl;
372 catch (InvalidArgsException &e)
374 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
375 << e.what() << "]" << std::endl;
377 catch (NoSupportException &e)
379 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
380 e.what() << "]" << std::endl;
382 catch (SimulatorException &e)
384 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
385 e.what() << "]" << std::endl;
389 void sendAllGETRequests()
391 SimulatorRemoteResourceSP resource = selectResource();
392 if (!resource) return;
394 SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
395 OperationState state)
397 std::cout << "\nResource verification status received ![id: " << sessionId <<
398 " State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
404 int id = resource->startVerification(RequestType::RQ_TYPE_GET, callback);
405 std::cout << "startVerification for GET is successful!id: " << id << std::endl;
407 catch (InvalidArgsException &e)
409 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
410 << e.what() << "]" << std::endl;
412 catch (NoSupportException &e)
414 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
415 e.what() << "]" << std::endl;
417 catch (SimulatorException &e)
419 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
420 e.what() << "]" << std::endl;
424 void sendAllPUTRequests()
426 SimulatorRemoteResourceSP resource = selectResource();
427 if (!resource) return;
429 SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
430 OperationState state)
432 std::cout << "\nResource verification status received ![id: " << sessionId <<
433 " State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
439 int id = resource->startVerification(RequestType::RQ_TYPE_PUT, callback);
440 std::cout << "startVerification for PUT is successful!id: " << id << std::endl;
442 catch (InvalidArgsException &e)
444 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
445 << e.what() << "]" << std::endl;
447 catch (NoSupportException &e)
449 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
450 e.what() << "]" << std::endl;
452 catch (SimulatorException &e)
454 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
455 e.what() << "]" << std::endl;
459 void sendAllPOSTRequests()
461 SimulatorRemoteResourceSP resource = selectResource();
462 if (!resource) return;
464 SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
465 OperationState state)
467 std::cout << "\nResource verification status received ![id: " << sessionId <<
468 " State: " << getOperationStateString(state) << " UID: " << uid << "]"
474 int id = resource->startVerification(RequestType::RQ_TYPE_POST, callback);
475 std::cout << "startVerification for POST is successful!id: " << id << std::endl;
477 catch (InvalidArgsException &e)
479 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
480 << e.what() << "]" << std::endl;
482 catch (NoSupportException &e)
484 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
485 e.what() << "]" << std::endl;
487 catch (SimulatorException &e)
489 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
490 e.what() << "]" << std::endl;
496 SimulatorRemoteResourceSP resource = selectResource();
502 std::string configPath;
503 std::cout << "Enter the config path: ";
504 std::cin >> configPath;
506 SimulatorResourceModelSP representation = resource->configure(configPath);
509 std::cout << "configuration is successful!" << std::endl;
510 std::map<std::string, SimulatorResourceModel::Attribute> attributes =
511 representation->getAttributes();
512 std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
513 for (auto & attribute : attributes)
515 std::cout << (attribute.second).getName() << " : {" << std::endl;
516 std::cout << "value: " << (attribute.second).toString() << std::endl;
517 SimulatorResourceModel::AttributeProperty prop = (attribute.second).getProperty();
518 std::cout << "Supported values given by : " << getPropertyTypeString(prop.type()) << std::endl;
519 if (SimulatorResourceModel::AttributeProperty::Type::RANGE == prop.type())
521 std::cout << "Min: " << prop.min() << std::endl;
522 std::cout << "Max: " << prop.max() << std::endl;
524 else if (SimulatorResourceModel::AttributeProperty::Type::VALUE_SET == prop.type())
526 std::cout << "Value set: " << prop.valueSetToString() << std::endl;
529 std::cout << "}" << std::endl << std::endl;
531 std::cout << "#############################" << std::endl;
534 catch (InvalidArgsException &e)
536 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
537 << e.what() << "]" << std::endl;
539 catch (SimulatorException &e)
541 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
542 e.what() << "]" << std::endl;
548 SimulatorRemoteResourceSP resource = selectResource();
554 SimulatorManager::getInstance()->getDeviceInfo(resource->getHost(), std::bind([](DeviceInfo & deviceInfo)
556 std::cout << "###Device Information received...." << std::endl;
557 std::ostringstream out;
558 out << "Device name: " << deviceInfo.getName() << std::endl;
559 out << "Device ID: " << deviceInfo.getID() << std::endl;
560 out << "Device Spec version: " << deviceInfo.getSpecVersion() << std::endl;
561 out << "Device dat model version: " << deviceInfo.getDataModelVersion() << std::endl;
563 std::cout << out.str() << std::endl;
564 }, std::placeholders::_1));
566 catch (InvalidArgsException &e)
568 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
571 catch (SimulatorException &e)
573 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
578 void getPlatformInfo()
580 SimulatorRemoteResourceSP resource = selectResource();
586 SimulatorManager::getInstance()->getPlatformInfo(resource->getHost(), std::bind([](PlatformInfo & platformInfo)
588 std::cout << "###Platform Information received...." << std::endl;
589 std::ostringstream out;
590 out << "Platform ID: " << platformInfo.getPlatformID() << std::endl;
591 out << "Platform version: " << platformInfo.getPlatformVersion() << std::endl;
592 out << "Manufacturer name: " << platformInfo.getManufacturerName() << std::endl;
593 out << "Manufacturer url: " << platformInfo.getManufacturerUrl() << std::endl;
594 out << "Modle number: " << platformInfo.getModelNumber() << std::endl;
595 out << "Date of manufacture: " << platformInfo.getDateOfManfacture() << std::endl;
596 out << "Operatio system version: " << platformInfo.getOSVersion() << std::endl;
597 out << "Hardware version: " << platformInfo.getHardwareVersion() << std::endl;
598 out << "Firmware version: " << platformInfo.getFirmwareVersion() << std::endl;
599 out << "Support url: " << platformInfo.getSupportUrl() << std::endl;
600 out << "System time: " << platformInfo.getSystemTime() << std::endl;
602 std::cout << out.str() << std::endl;
603 }, std::placeholders::_1));
605 catch (InvalidArgsException &e)
607 std::cout << "InvalidArgsException occured [code : " << e.code()
608 << " Detail: " << e.what() << "]" << std::endl;
610 catch (SimulatorException &e)
612 std::cout << "SimulatorException occured [code : " << e.code()
613 << " Detail: " << e.what() << "]" << std::endl;
618 std::recursive_mutex m_mutex;
619 std::map<std::string, SimulatorRemoteResourceSP> m_resList;
624 std::cout << "############### MAIN MENU###############" << std::endl;
625 std::cout << "1. Client Controller Test" << std::endl;
626 std::cout << "2. Set Logger" << std::endl;
627 std::cout << "3. Help" << std::endl;
628 std::cout << "0. Exit" << std::endl;
629 std::cout << "######################################" << std::endl;
634 std::cout << "1. Default console logger" << std::endl;
635 std::cout << "2. Default file logger" << std::endl;
636 std::cout << "3. custom logger" << std::endl;
640 if (choice <= 0 || choice > 3)
642 std::cout << "Invalid selection !" << std::endl;
650 if (false == SimulatorManager::getInstance()->setConsoleLogger())
651 std::cout << "Failed to set the default console logger" << std::endl;
657 std::string filePath;
658 std::cout << "Enter the file path (without file name) : ";
659 std::cin >> filePath;
660 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
661 std::cout << "Failed to set default file logger" << std::endl;
666 SimulatorManager::getInstance()->setLogger(gAppLogger);
672 ClientController clientController;
678 std::cout << "Enter your choice: ";
680 if (choice < 0 || choice > 5)
682 std::cout << "Invaild choice !" << std::endl; continue;
687 case 1: clientController.startTest();
688 std::cout << "Welcome back to main menu !" << std::endl;
691 case 2: setLogger(); break;
693 case 3: printMainMenu(); break;
695 case 0: cont = false;
699 std::cout << "Terminating test !!!" << std::endl;