Displaying and editing the complex value types for attributes.
[platform/upstream/iotivity.git] / service / simulator / examples / client / simulator_client.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "simulator_manager.h"
22 #include <map>
23 #include <mutex>
24
25 std::string getOperationStateString(OperationState state)
26 {
27     switch (state)
28     {
29         case OP_START: return "OP_START";
30         case OP_COMPLETE: return "OP_COMPLETE";
31         case OP_ABORT: return "OP_ABORT";
32     }
33
34     return "OP_UNKNOWN";
35 }
36
37 std::string getPropertyTypeString(SimulatorResourceModel::AttributeProperty::Type type)
38 {
39     switch(type)
40     {
41         case SimulatorResourceModel::AttributeProperty::Type::RANGE:
42             return "RANGE";
43         case SimulatorResourceModel::AttributeProperty::Type::VALUE_SET:
44             return "VALUE_SET";
45         default:
46             break;
47     }
48
49     return "UNKNOWN";
50 }
51
52 class AppLogger : public ILogger
53 {
54     public:
55         void write(std::string time, ILogger::Level level, std::string message)
56         {
57             std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
58         }
59 };
60 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
61
62 class ClientController
63 {
64     public:
65         void startTest()
66         {
67             printMenu();
68             bool cont = true;
69             while (cont)
70             {
71                 int choice = -1;
72                 std::cout << "Enter your choice: ";
73                 std::cin >> choice;
74                 if (choice < 0 || choice > 14)
75                 {
76                     std::cout << "Invaild choice !" << std::endl; continue;
77                 }
78
79                 switch (choice)
80                 {
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;
95                     case 0: cont = false;
96                 }
97             }
98         }
99
100     private:
101         void printMenu()
102         {
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;
120         }
121
122         SimulatorRemoteResourceSP selectResource()
123         {
124             std::lock_guard<std::recursive_mutex> lock(m_mutex);
125             if (0 == m_resList.size())
126             {
127                 std::cout << "No resources!" << std::endl;
128                 return nullptr;
129             }
130
131             int index = 1;
132             std::vector<std::string> ids;
133             for (auto & resourceEntry : m_resList)
134             {
135                 std::cout << index++ << ": " << (resourceEntry.second)->getURI() << "[" <<
136                           (resourceEntry.second)->getHost()  << "]" << std::endl;
137                 ids.push_back((resourceEntry.second)->getID());
138             }
139
140             int choice = -1;
141             std::cout << "Choose the resource: ";
142             std::cin >> choice;
143
144             if (choice < 1 || choice > index - 1)
145             {
146                 std::cout << "Invalid choice !" << std::endl;
147                 return nullptr;
148             }
149
150             return m_resList[ids[choice - 1]];
151         }
152
153         void findResource()
154         {
155             std::string resourceType;
156             std::cout << "Enter resource type : ";
157             std::cin >> resourceType;
158
159             ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
160             {
161                 std::cout << "Resource found ######" << std::endl;
162                 displayResource(resource);
163
164                 // Add to local list
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;
168                 else
169                     std::cout << "Resource with UID: " << resource->getID() << "already exist in the list!" <<
170                               std::endl;
171             };
172
173             try
174             {
175                 SimulatorManager::getInstance()->findResource(resourceType, callback);
176                 std::cout << "SimulatorManager::findResource is successful" << std::endl;
177                 m_resList.clear();
178             }
179             catch (InvalidArgsException &e)
180             {
181                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
182                           << std::endl;
183             }
184             catch (SimulatorException &e)
185             {
186                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
187                           std::endl;
188             }
189         }
190
191         void displayResource()
192         {
193             displayResource(selectResource());
194         }
195
196         void displayResource(SimulatorRemoteResourceSP resource)
197         {
198             if (!resource) return;
199
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;
213         }
214
215         void observeResource()
216         {
217             SimulatorRemoteResourceSP resource = selectResource();
218             if (!resource) return;
219
220             // callback implementaion
221             SimulatorRemoteResource::ObserveNotificationCallback callback =
222                 [](std::string uid, SimulatorResult errorCode, SimulatorResourceModelSP rep, int seq)
223             {
224                 std::cout << "\nObserve notification received ###[errorcode:  " << errorCode <<
225                     " seq:  " << seq << "UID: " << uid << "]" << std::endl;
226
227                 std::cout << "Representation is: " << std::endl;
228                 std::cout << rep->toString() << std::endl;
229             };
230
231             try
232             {
233                 resource->observe(ObserveType::OBSERVE, callback);
234                 std::cout << "Observe is successful!" << std::endl;
235             }
236             catch (InvalidArgsException &e)
237             {
238                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
239                         << e.what() << "]" << std::endl;
240             }
241             catch (SimulatorException &e)
242             {
243                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
244                         e.what() << "]" << std::endl;
245             }
246         }
247
248         void cancelObserving()
249         {
250             SimulatorRemoteResourceSP resource = selectResource();
251             if (!resource) return;
252
253             try
254             {
255                 resource->cancelObserve();
256                 std::cout << "Cancelling observe is successful!" << std::endl;
257             }
258             catch (SimulatorException &e)
259             {
260                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
261                         e.what() << "]" << std::endl;
262             }
263         }
264
265         void sendGet()
266         {
267             SimulatorRemoteResourceSP resource = selectResource();
268             if (!resource) return;
269
270             // callback implementaion
271             SimulatorRemoteResource::ResponseCallback callback =
272                 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
273             {
274                 std::cout << "\nGET Response received ### [errorcode:  " << errorCode << "]"
275                         << std::endl;
276                 std::cout << "UID is: " << uId << std::endl;
277                 std::cout << "Representation is: " << std::endl;
278                 std::cout << rep->toString() << std::endl;
279             };
280
281             try
282             {
283                 resource->get(std::map <std::string, std::string>(), callback);
284                 std::cout << "GET is successful!" << std::endl;
285             }
286             catch (InvalidArgsException &e)
287             {
288                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
289                         << e.what() << "]" << std::endl;
290             }
291             catch (NoSupportException &e)
292             {
293                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
294                         e.what() << "]" << std::endl;
295             }
296             catch (SimulatorException &e)
297             {
298                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
299                         e.what() << "]" << std::endl;
300             }
301         }
302
303         void sendPut()
304         {
305             SimulatorRemoteResourceSP resource = selectResource();
306             if (!resource) return;
307
308             // callback implementaion
309             SimulatorRemoteResource::ResponseCallback callback =
310                 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
311             {
312                 std::cout << "\nPUT Response received ![errorcode:  " << errorCode << "]"
313                         << std::endl;
314                 std::cout << "UID is: " << uId << std::endl;
315                 std::cout << "Representation is: " << std::endl;
316                 std::cout << rep->toString() << std::endl;
317             };
318
319             try
320             {
321                 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
322                 bool value = false;
323                 rep->add("power", value);
324                 rep->add("intensity", 15);
325
326                 resource->put(std::map <std::string, std::string>(), rep, callback);
327                 std::cout << "PUT is successful!" << std::endl;
328             }
329             catch (InvalidArgsException &e)
330             {
331                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
332                         << e.what() << "]" << std::endl;
333             }
334             catch (NoSupportException &e)
335             {
336                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
337                         e.what() << "]" << std::endl;
338             }
339             catch (SimulatorException &e)
340             {
341                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
342                         e.what() << "]" << std::endl;
343             }
344         }
345
346         void sendPost()
347         {
348             SimulatorRemoteResourceSP resource = selectResource();
349             if (!resource) return;
350
351             // callback implementaion
352             SimulatorRemoteResource::ResponseCallback callback =
353                 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
354             {
355                 std::cout << "\nPOST Response received ![errorcode:  " << errorCode << "]"
356                         << std::endl;
357                 std::cout << "UID is: " << uId << std::endl;
358                 std::cout << "Representation is: " << std::endl;
359                 std::cout << rep->toString() << std::endl;
360             };
361
362             try
363             {
364                 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
365                 bool value = true;
366                 rep->add("power", value);
367                 rep->add("intensity", 17);
368
369                 resource->post(std::map <std::string, std::string>(), rep, callback);
370                 std::cout << "POST is successful!" << std::endl;
371             }
372             catch (InvalidArgsException &e)
373             {
374                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
375                         << e.what() << "]" << std::endl;
376             }
377             catch (NoSupportException &e)
378             {
379                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
380                         e.what() << "]" << std::endl;
381             }
382             catch (SimulatorException &e)
383             {
384                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
385                         e.what() << "]" << std::endl;
386             }
387         }
388
389         void sendAllGETRequests()
390         {
391             SimulatorRemoteResourceSP resource = selectResource();
392             if (!resource) return;
393
394             SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
395                     OperationState state)
396             {
397                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
398                         "  State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
399                         std::endl;
400             };
401
402             try
403             {
404                 int id = resource->startVerification(RequestType::RQ_TYPE_GET, callback);
405                 std::cout << "startVerification for GET is successful!id: " << id << std::endl;
406             }
407             catch (InvalidArgsException &e)
408             {
409                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
410                         << e.what() << "]" << std::endl;
411             }
412             catch (NoSupportException &e)
413             {
414                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
415                         e.what() << "]" << std::endl;
416             }
417             catch (SimulatorException &e)
418             {
419                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
420                         e.what() << "]" << std::endl;
421             }
422         }
423
424         void sendAllPUTRequests()
425         {
426             SimulatorRemoteResourceSP resource = selectResource();
427             if (!resource) return;
428
429             SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
430                     OperationState state)
431             {
432                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
433                         "  State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
434                         std::endl;
435             };
436
437             try
438             {
439                 int id = resource->startVerification(RequestType::RQ_TYPE_PUT, callback);
440                 std::cout << "startVerification for PUT is successful!id: " << id << std::endl;
441             }
442             catch (InvalidArgsException &e)
443             {
444                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
445                         << e.what() << "]" << std::endl;
446             }
447             catch (NoSupportException &e)
448             {
449                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
450                         e.what() << "]" << std::endl;
451             }
452             catch (SimulatorException &e)
453             {
454                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
455                         e.what() << "]" << std::endl;
456             }
457         }
458
459         void sendAllPOSTRequests()
460         {
461             SimulatorRemoteResourceSP resource = selectResource();
462             if (!resource) return;
463
464             SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
465                     OperationState state)
466             {
467                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
468                         "  State: " << getOperationStateString(state) << " UID: " << uid << "]"
469                         << std::endl;
470             };
471
472             try
473             {
474                 int id = resource->startVerification(RequestType::RQ_TYPE_POST, callback);
475                 std::cout << "startVerification for POST is successful!id: " << id << std::endl;
476             }
477             catch (InvalidArgsException &e)
478             {
479                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
480                         << e.what() << "]" << std::endl;
481             }
482             catch (NoSupportException &e)
483             {
484                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
485                         e.what() << "]" << std::endl;
486             }
487             catch (SimulatorException &e)
488             {
489                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
490                         e.what() << "]" << std::endl;
491             }
492         }
493
494         void configure()
495         {
496             SimulatorRemoteResourceSP resource = selectResource();
497             if (!resource)
498                 return;
499
500             try
501             {
502                 std::string configPath;
503                 std::cout << "Enter the config path: ";
504                 std::cin >> configPath;
505
506                 SimulatorResourceModelSP representation = resource->configure(configPath);
507                 if (representation)
508                 {
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)
514                     {
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())
520                         {
521                             std::cout << "Min: " << prop.min() << std::endl;
522                             std::cout << "Max: " << prop.max() << std::endl;
523                         }
524                         else if (SimulatorResourceModel::AttributeProperty::Type::VALUE_SET == prop.type())
525                         {
526                             std::cout << "Value set: " << prop.valueSetToString() << std::endl;
527                         }
528
529                         std::cout << "}" << std::endl << std::endl;
530                     }
531                     std::cout << "#############################" << std::endl;
532                 }
533             }
534             catch (InvalidArgsException &e)
535             {
536                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
537                         << e.what() << "]" << std::endl;
538             }
539             catch (SimulatorException &e)
540             {
541                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
542                         e.what() << "]" << std::endl;
543             }
544         }
545
546         void getDeviceInfo()
547         {
548             SimulatorRemoteResourceSP resource = selectResource();
549             if (!resource)
550                 return;
551
552             try
553             {
554                 SimulatorManager::getInstance()->getDeviceInfo(resource->getHost(), std::bind([](DeviceInfo & deviceInfo)
555                 {
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;
562
563                     std::cout << out.str() << std::endl;
564                 }, std::placeholders::_1));
565             }
566             catch (InvalidArgsException &e)
567             {
568                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
569                           << std::endl;
570             }
571             catch (SimulatorException &e)
572             {
573                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
574                           std::endl;
575             }
576         }
577
578         void getPlatformInfo()
579         {
580             SimulatorRemoteResourceSP resource = selectResource();
581             if (!resource)
582                 return;
583
584             try
585             {
586                 SimulatorManager::getInstance()->getPlatformInfo(resource->getHost(), std::bind([](PlatformInfo & platformInfo)
587                 {
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;
601
602                     std::cout << out.str() << std::endl;
603                 }, std::placeholders::_1));
604             }
605             catch (InvalidArgsException &e)
606             {
607                 std::cout << "InvalidArgsException occured [code : " << e.code()
608                         << " Detail: " << e.what() << "]" << std::endl;
609             }
610             catch (SimulatorException &e)
611             {
612                 std::cout << "SimulatorException occured [code : " << e.code()
613                         << " Detail: " << e.what() << "]" << std::endl;
614             }
615         }
616
617     private:
618         std::recursive_mutex m_mutex;
619         std::map<std::string, SimulatorRemoteResourceSP> m_resList;
620 };
621
622 void printMainMenu()
623 {
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;
630 }
631
632 void setLogger()
633 {
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;
637
638     int choice = -1;
639     std::cin >> choice;
640     if (choice <= 0 || choice > 3)
641     {
642         std::cout << "Invalid selection !" << std::endl;
643         return;
644     }
645
646     switch (choice)
647     {
648         case 1:
649             {
650                 if (false == SimulatorManager::getInstance()->setConsoleLogger())
651                     std::cout << "Failed to set the default console logger" << std::endl;
652             }
653             break;
654
655         case 2:
656             {
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;
662             }
663             break;
664
665         case 3:
666             SimulatorManager::getInstance()->setLogger(gAppLogger);
667     }
668 }
669
670 int main(void)
671 {
672     ClientController clientController;
673     printMainMenu();
674     bool cont = true;
675     while (cont == true)
676     {
677         int choice = -1;
678         std::cout << "Enter your choice: ";
679         std::cin >> choice;
680         if (choice < 0 || choice > 5)
681         {
682             std::cout << "Invaild choice !" << std::endl; continue;
683         }
684
685         switch (choice)
686         {
687             case 1: clientController.startTest();
688                 std::cout << "Welcome back to main menu !" << std::endl;
689                 break;
690
691             case 2: setLogger(); break;
692
693             case 3: printMainMenu(); break;
694
695             case 0: cont = false;
696         }
697     }
698
699     std::cout << "Terminating test !!!" << std::endl;
700 }