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 > 12)
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: printMenu(); break;
101 std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
102 std::cout << "1. Find resource" << std::endl;
103 std::cout << "2. Display resource information" << std::endl;
104 std::cout << "3. Observe for resource change" << std::endl;
105 std::cout << "4. Cancel observation" << std::endl;
106 std::cout << "5. Send GET message" << std::endl;
107 std::cout << "6. Send PUT message" << std::endl;
108 std::cout << "7. Send POST message" << std::endl;
109 std::cout << "8. Send All GET requests" << std::endl;
110 std::cout << "9. Send All PUT requests" << std::endl;
111 std::cout << "10. Send All POST requests" << std::endl;
112 std::cout << "11. Configure (using RAML file)" << std::endl;
113 std::cout << "12: Help" << std::endl;
114 std::cout << "0. Exit" << std::endl;
115 std::cout << "###################################################" << std::endl;
118 SimulatorRemoteResourceSP selectResource()
120 std::lock_guard<std::recursive_mutex> lock(m_mutex);
121 if (0 == m_resList.size())
123 std::cout << "No resources!" << std::endl;
128 std::vector<std::string> ids;
129 for (auto & resourceEntry : m_resList)
131 std::cout << index++ << ": " << (resourceEntry.second)->getURI() << "[" <<
132 (resourceEntry.second)->getHost() << "]" << std::endl;
133 ids.push_back((resourceEntry.second)->getID());
137 std::cout << "Choose the resource: ";
140 if (choice < 1 || choice > index - 1)
142 std::cout << "Invalid choice !" << std::endl;
146 return m_resList[ids[choice - 1]];
151 std::string resourceType;
152 std::cout << "Enter resource type : ";
153 std::cin >> resourceType;
155 ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
157 std::cout << "Resource found ######" << std::endl;
158 displayResource(resource);
161 std::lock_guard<std::recursive_mutex> lock(m_mutex);
162 if (m_resList.end() == m_resList.find(resource->getID()))
163 m_resList[resource->getID()] = resource;
165 std::cout << "Resource with UID: " << resource->getID() << "already exist in the list!" <<
171 SimulatorManager::getInstance()->findResource(resourceType, callback);
172 std::cout << "SimulatorManager::findResource is successful" << std::endl;
175 catch (InvalidArgsException &e)
177 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
180 catch (SimulatorException &e)
182 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
187 void displayResource()
189 displayResource(selectResource());
192 void displayResource(SimulatorRemoteResourceSP resource)
194 if (!resource) return;
196 std::cout << "#############################" << std::endl;
197 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
198 std::cout << "Host: " << resource->getHost().c_str() << std::endl;
199 std::cout << "ID: " << resource->getID().c_str() << std::endl;
200 std::cout << "Resource Types: ";
201 for (auto & type : resource->getResourceTypes())
202 std::cout << type << " ";
203 std::cout << "\nInterface Types: ";
204 for (auto & type : resource->getResourceInterfaces())
205 std::cout << type << " ";
206 std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable()
207 << std::noboolalpha << std::endl;
208 std::cout << "#############################" << std::endl;
211 void observeResource()
213 SimulatorRemoteResourceSP resource = selectResource();
214 if (!resource) return;
216 // callback implementaion
217 SimulatorRemoteResource::ObserveNotificationCallback callback =
218 [](std::string uid, SimulatorResult errorCode, SimulatorResourceModelSP rep, int seq)
220 std::cout << "\nObserve notification received ###[errorcode: " << errorCode <<
221 " seq: " << seq << "UID: " << uid << "]" << std::endl;
223 std::cout << "Representation is: " << std::endl;
224 std::cout << rep->toString() << std::endl;
229 resource->observe(ObserveType::OBSERVE, callback);
230 std::cout << "Observe is successful!" << std::endl;
232 catch (InvalidArgsException &e)
234 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
235 << e.what() << "]" << std::endl;
237 catch (SimulatorException &e)
239 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
240 e.what() << "]" << std::endl;
244 void cancelObserving()
246 SimulatorRemoteResourceSP resource = selectResource();
247 if (!resource) return;
251 resource->cancelObserve();
252 std::cout << "Cancelling observe is successful!" << std::endl;
254 catch (SimulatorException &e)
256 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
257 e.what() << "]" << std::endl;
263 SimulatorRemoteResourceSP resource = selectResource();
264 if (!resource) return;
266 // callback implementaion
267 SimulatorRemoteResource::ResponseCallback callback =
268 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
270 std::cout << "\nGET Response received ### [errorcode: " << errorCode << "]"
272 std::cout << "UID is: " << uId << std::endl;
273 std::cout << "Representation is: " << std::endl;
274 std::cout << rep->toString() << std::endl;
279 resource->get(std::map <std::string, std::string>(), callback);
280 std::cout << "GET is successful!" << std::endl;
282 catch (InvalidArgsException &e)
284 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
285 << e.what() << "]" << std::endl;
287 catch (NoSupportException &e)
289 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
290 e.what() << "]" << std::endl;
292 catch (SimulatorException &e)
294 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
295 e.what() << "]" << std::endl;
301 SimulatorRemoteResourceSP resource = selectResource();
302 if (!resource) return;
304 // callback implementaion
305 SimulatorRemoteResource::ResponseCallback callback =
306 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
308 std::cout << "\nPUT Response received ![errorcode: " << errorCode << "]"
310 std::cout << "UID is: " << uId << std::endl;
311 std::cout << "Representation is: " << std::endl;
312 std::cout << rep->toString() << std::endl;
317 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
319 rep->add("power", value);
320 rep->add("intensity", 15);
322 resource->put(std::map <std::string, std::string>(), rep, callback);
323 std::cout << "PUT is successful!" << std::endl;
325 catch (InvalidArgsException &e)
327 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
328 << e.what() << "]" << std::endl;
330 catch (NoSupportException &e)
332 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
333 e.what() << "]" << std::endl;
335 catch (SimulatorException &e)
337 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
338 e.what() << "]" << std::endl;
344 SimulatorRemoteResourceSP resource = selectResource();
345 if (!resource) return;
347 // callback implementaion
348 SimulatorRemoteResource::ResponseCallback callback =
349 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
351 std::cout << "\nPOST Response received ![errorcode: " << errorCode << "]"
353 std::cout << "UID is: " << uId << std::endl;
354 std::cout << "Representation is: " << std::endl;
355 std::cout << rep->toString() << std::endl;
360 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
362 rep->add("power", value);
363 rep->add("intensity", 17);
365 resource->post(std::map <std::string, std::string>(), rep, callback);
366 std::cout << "POST is successful!" << std::endl;
368 catch (InvalidArgsException &e)
370 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
371 << e.what() << "]" << std::endl;
373 catch (NoSupportException &e)
375 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
376 e.what() << "]" << std::endl;
378 catch (SimulatorException &e)
380 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
381 e.what() << "]" << std::endl;
385 void sendAllGETRequests()
387 SimulatorRemoteResourceSP resource = selectResource();
388 if (!resource) return;
390 SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
391 OperationState state)
393 std::cout << "\nResource verification status received ![id: " << sessionId <<
394 " State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
400 int id = resource->startVerification(RequestType::RQ_TYPE_GET, callback);
401 std::cout << "startVerification for GET is successful!id: " << id << std::endl;
403 catch (InvalidArgsException &e)
405 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
406 << e.what() << "]" << std::endl;
408 catch (NoSupportException &e)
410 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
411 e.what() << "]" << std::endl;
413 catch (SimulatorException &e)
415 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
416 e.what() << "]" << std::endl;
420 void sendAllPUTRequests()
422 SimulatorRemoteResourceSP resource = selectResource();
423 if (!resource) return;
425 SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
426 OperationState state)
428 std::cout << "\nResource verification status received ![id: " << sessionId <<
429 " State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
435 int id = resource->startVerification(RequestType::RQ_TYPE_PUT, callback);
436 std::cout << "startVerification for PUT is successful!id: " << id << std::endl;
438 catch (InvalidArgsException &e)
440 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
441 << e.what() << "]" << std::endl;
443 catch (NoSupportException &e)
445 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
446 e.what() << "]" << std::endl;
448 catch (SimulatorException &e)
450 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
451 e.what() << "]" << std::endl;
455 void sendAllPOSTRequests()
457 SimulatorRemoteResourceSP resource = selectResource();
458 if (!resource) return;
460 SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
461 OperationState state)
463 std::cout << "\nResource verification status received ![id: " << sessionId <<
464 " State: " << getOperationStateString(state) << " UID: " << uid << "]"
470 int id = resource->startVerification(RequestType::RQ_TYPE_POST, callback);
471 std::cout << "startVerification for POST is successful!id: " << id << std::endl;
473 catch (InvalidArgsException &e)
475 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
476 << e.what() << "]" << std::endl;
478 catch (NoSupportException &e)
480 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
481 e.what() << "]" << std::endl;
483 catch (SimulatorException &e)
485 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
486 e.what() << "]" << std::endl;
492 SimulatorRemoteResourceSP resource = selectResource();
498 std::string configPath;
499 std::cout << "Enter the config path: ";
500 std::cin >> configPath;
502 SimulatorResourceModelSP representation = resource->configure(configPath);
505 std::cout << "configuration is successful!" << std::endl;
506 std::map<std::string, SimulatorResourceModel::Attribute> attributes =
507 representation->getAttributes();
508 std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
509 for (auto & attribute : attributes)
511 std::cout << (attribute.second).getName() << " : {" << std::endl;
512 std::cout << "value: " << (attribute.second).toString() << std::endl;
513 SimulatorResourceModel::AttributeProperty prop = (attribute.second).getProperty();
514 std::cout << "Supported values given by : " << getPropertyTypeString(prop.type()) << std::endl;
515 if (SimulatorResourceModel::AttributeProperty::Type::RANGE == prop.type())
517 std::cout << "Min: " << prop.min() << std::endl;
518 std::cout << "Max: " << prop.max() << std::endl;
520 else if (SimulatorResourceModel::AttributeProperty::Type::VALUE_SET == prop.type())
522 std::cout << "Value set: " << prop.valueSetToString() << std::endl;
525 std::cout << "}" << std::endl << std::endl;
527 std::cout << "#############################" << std::endl;
530 catch (InvalidArgsException &e)
532 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
533 << e.what() << "]" << std::endl;
535 catch (SimulatorException &e)
537 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
538 e.what() << "]" << std::endl;
543 std::recursive_mutex m_mutex;
544 std::map<std::string, SimulatorRemoteResourceSP> m_resList;
549 std::cout << "############### MAIN MENU###############" << std::endl;
550 std::cout << "1. Client Controller Test" << std::endl;
551 std::cout << "2. Get device information" << std::endl;
552 std::cout << "3. Get platform information" << std::endl;
553 std::cout << "4. Set Logger" << std::endl;
554 std::cout << "5. Help" << std::endl;
555 std::cout << "0. Exit" << std::endl;
556 std::cout << "######################################" << std::endl;
561 std::cout << "1. Default console logger" << std::endl;
562 std::cout << "2. Default file logger" << std::endl;
563 std::cout << "3. custom logger" << std::endl;
567 if (choice <= 0 || choice > 3)
569 std::cout << "Invalid selection !" << std::endl;
577 if (false == SimulatorManager::getInstance()->setConsoleLogger())
578 std::cout << "Failed to set the default console logger" << std::endl;
584 std::string filePath;
585 std::cout << "Enter the file path (without file name) : ";
586 std::cin >> filePath;
587 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
588 std::cout << "Failed to set default file logger" << std::endl;
593 SimulatorManager::getInstance()->setLogger(gAppLogger);
599 ClientController clientController;
605 std::cout << "Enter your choice: ";
607 if (choice < 0 || choice > 5)
609 std::cout << "Invaild choice !" << std::endl; continue;
614 case 1: clientController.startTest();
615 std::cout << "Welcome back to main menu !" << std::endl;
622 SimulatorManager::getInstance()->getDeviceInfo(std::bind([](DeviceInfo & deviceInfo)
624 std::cout << "###Device Information received...." << std::endl;
625 std::ostringstream out;
626 out << "Device name: " << deviceInfo.getName() << std::endl;
627 out << "Device ID: " << deviceInfo.getID() << std::endl;
628 out << "Device Spec version: " << deviceInfo.getSpecVersion() << std::endl;
629 out << "Device dat model version: " << deviceInfo.getDataModelVersion() << std::endl;
631 std::cout << out.str() << std::endl;
632 }, std::placeholders::_1));
634 catch (InvalidArgsException &e)
636 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
639 catch (SimulatorException &e)
641 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
651 SimulatorManager::getInstance()->getPlatformInfo(std::bind([](PlatformInfo & platformInfo)
653 std::cout << "###Platform Information received...." << std::endl;
654 std::ostringstream out;
655 out << "Platform ID: " << platformInfo.getPlatformID() << std::endl;
656 out << "Platform version: " << platformInfo.getPlatformVersion() << std::endl;
657 out << "Manufacturer name: " << platformInfo.getManufacturerName() << std::endl;
658 out << "Manufacturer url: " << platformInfo.getManufacturerUrl() << std::endl;
659 out << "Modle number: " << platformInfo.getModelNumber() << std::endl;
660 out << "Date of manufacture: " << platformInfo.getDateOfManfacture() << std::endl;
661 out << "Operatio system version: " << platformInfo.getOSVersion() << std::endl;
662 out << "Hardware version: " << platformInfo.getHardwareVersion() << std::endl;
663 out << "Firmware version: " << platformInfo.getFirmwareVersion() << std::endl;
664 out << "Support url: " << platformInfo.getSupportUrl() << std::endl;
665 out << "System time: " << platformInfo.getSystemTime() << std::endl;
667 std::cout << out.str() << std::endl;
668 }, std::placeholders::_1));
670 catch (InvalidArgsException &e)
672 std::cout << "InvalidArgsException occured [code : " << e.code()
673 << " Detail: " << e.what() << "]" << std::endl;
675 catch (SimulatorException &e)
677 std::cout << "SimulatorException occured [code : " << e.code()
678 << " Detail: " << e.what() << "]" << std::endl;
683 case 4: setLogger(); break;
685 case 5: printMainMenu(); break;
687 case 0: cont = false;
691 std::cout << "Terminating test !!!" << std::endl;