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 class AppLogger : public ILogger
26 void write(std::string time, ILogger::Level level, std::string message)
28 std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
31 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
33 class ClientController
43 std::cout << "Enter your choice: ";
45 if (choice < 0 || choice > 8)
47 std::cout << "Invaild choice !" << std::endl; continue;
52 case 1: findResource(); break;
53 case 2: displayResource(); break;
54 case 3: observeResource(); break;
55 case 4: cancelObserving(); break;
56 case 5: sendGet(); break;
57 case 6: sendPut(); break;
58 case 7: sendPost(); break;
59 case 8: printMenu(); break;
68 std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
69 std::cout << "1. Find resource" << std::endl;
70 std::cout << "2. Display resource information" << std::endl;
71 std::cout << "3. Observe for resource change" << std::endl;
72 std::cout << "4. Cancel observation" << std::endl;
73 std::cout << "5. Send GET message" << std::endl;
74 std::cout << "6. Send PUT message" << std::endl;
75 std::cout << "7. Send POST message" << std::endl;
76 std::cout << "8: Help" << std::endl;
77 std::cout << "0. Exit" << std::endl;
78 std::cout << "###################################################" << std::endl;
81 int selectResource(std::vector<SimulatorRemoteResourcePtr> resourceList)
83 if (0 == resourceList.size())
85 std::cout << "No resouces!" << std::endl;
90 for (auto & resource : resourceList)
92 std::cout << index++ << ": " << resource->getURI() << "[" << resource->getHost() << "]" << std::endl;
96 std::cout << "Choose the resource: ";
99 if (choice < 1 || choice > index - 1)
101 std::cout << "Invalid choice !" << std::endl;
110 std::string resourceType;
111 std::cout << "Enter resource type : ";
112 std::cin >> resourceType;
114 ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
116 std::cout << "Resource found ######" << std::endl;
117 displayResource(resource);
120 SimulatorResult result = SimulatorManager::getInstance()->findResource
121 (resourceType, callback);
122 if (SIMULATOR_SUCCESS != result)
123 std::cout << "SimulatorManager::findResource returned error : " << result << std::endl;
126 void displayResource()
128 std::vector<SimulatorRemoteResourcePtr> resourceList =
129 SimulatorManager::getInstance()->getFoundResources();
131 int index = selectResource(resourceList);
135 displayResource(resourceList[index - 1]);
138 void displayResource(SimulatorRemoteResourcePtr resource)
140 std::cout << "#############################" << std::endl;
141 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
142 std::cout << "Host: " << resource->getHost().c_str() << std::endl;
143 std::cout << "Resource Types: ";
144 for (auto &type : resource->getResourceTypes())
145 std::cout << type << " ";
146 std::cout << "\nInterface Types: ";
147 for (auto &type : resource->getResourceInterfaces())
148 std::cout << type << " ";
149 std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable() << std::endl;
150 std::cout << "#############################" << std::endl;
153 void observeResource()
155 std::vector<SimulatorRemoteResourcePtr> resourceList =
156 SimulatorManager::getInstance()->getFoundResources();
158 int index = selectResource(resourceList);
162 SimulatorRemoteResourcePtr resource = resourceList[index - 1];
164 // callback implementaion
165 SimulatorRemoteResource::RepresentationChangeCallback callback =
166 [](int errorCode, const SimulatorResourceModel &rep, int seq)
168 std::cout << "\nObserve notificatoin received ###[errorcode: " << errorCode << " seq: " << seq << "]" << std::endl;
169 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
170 for (auto & attribute : attributes)
172 std::cout << (attribute.second).getName() << " : {" << std::endl;
173 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
174 std::cout << "}" << std::endl;
176 std::cout << std::endl;
179 std::map <std::string, std::string> queryParams;
180 SimulatorResult result = resource->observe(SimulatorRemoteResource::OBSERVE, queryParams, callback);
181 if ( SIMULATOR_SUCCESS == result)
182 std::cout << "Observe is successfull!" << std::endl;
184 std::cout << "Observe is failed!error: " << result << std::endl;
187 void cancelObserving()
189 std::vector<SimulatorRemoteResourcePtr> resourceList =
190 SimulatorManager::getInstance()->getFoundResources();
192 int index = selectResource(resourceList);
196 SimulatorRemoteResourcePtr resource = resourceList[index - 1];
197 SimulatorResult result = resource->cancelObserve();
198 if ( SIMULATOR_SUCCESS == result)
199 std::cout << "Cancelling observe is successfull!" << std::endl;
201 std::cout << "Cancelling observe is failed!error: " << result << std::endl;
206 std::vector<SimulatorRemoteResourcePtr> resourceList =
207 SimulatorManager::getInstance()->getFoundResources();
209 int index = selectResource(resourceList);
213 SimulatorRemoteResourcePtr resource = resourceList[index - 1];
215 // callback implementaion
216 SimulatorRemoteResource::ResponseCallback callback =
217 [](int errorCode, const SimulatorResourceModel & rep)
219 std::cout << "\nGET Response received ### [errorcode: " << errorCode << "]" << std::endl;
220 std::cout << "Representation is: " << std::endl;
221 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
222 for (auto & attribute : attributes)
224 std::cout << (attribute.second).getName() << " : {" << std::endl;
225 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
226 std::cout << "}" << std::endl;
228 std::cout << std::endl;
231 std::map <std::string, std::string> queryParams;
232 SimulatorResult result = resource->get(queryParams, callback);
233 if ( SIMULATOR_SUCCESS == result)
234 std::cout << "GET is successfull!" << std::endl;
236 std::cout << "GET is failed!error: " << result << std::endl;
241 std::vector<SimulatorRemoteResourcePtr> resourceList =
242 SimulatorManager::getInstance()->getFoundResources();
244 int index = selectResource(resourceList);
248 SimulatorRemoteResourcePtr resource = resourceList[index - 1];
250 // callback implementaion
251 SimulatorRemoteResource::ResponseCallback callback =
252 [](int errorCode, const SimulatorResourceModel & rep)
254 std::cout << "\nPUT Response received ![errorcode: " << errorCode << "]" << std::endl;
255 std::cout << "Representation is: " << std::endl;
256 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
257 for (auto & attribute : attributes)
259 std::cout << (attribute.second).getName() << " : {" << std::endl;
260 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
261 std::cout << "}" << std::endl;
263 std::cout << std::endl;
266 std::map <std::string, std::string> queryParams;
267 SimulatorResourceModel rep;
268 rep.addAttribute("power", "off");
269 rep.addAttribute("intensity", 5);
271 SimulatorResult result = resource->put(rep, queryParams, callback);
272 if ( SIMULATOR_SUCCESS == result)
273 std::cout << "PUT is successfull!" << std::endl;
275 std::cout << "PUT is failed!error: " << result << std::endl;
280 std::vector<SimulatorRemoteResourcePtr> resourceList =
281 SimulatorManager::getInstance()->getFoundResources();
283 int index = selectResource(resourceList);
287 SimulatorRemoteResourcePtr resource = resourceList[index - 1];
289 // callback implementaion
290 SimulatorRemoteResource::ResponseCallback callback =
291 [](int errorCode, const SimulatorResourceModel & rep)
293 std::cout << "\nPOST Response received ![errorcode: " << errorCode << "]" << std::endl;
294 std::cout << "Representation is: " << std::endl;
295 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
296 for (auto & attribute : attributes)
298 std::cout << (attribute.second).getName() << " : {" << std::endl;
299 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
300 std::cout << "}" << std::endl;
302 std::cout << std::endl;
305 std::map <std::string, std::string> queryParams;
306 SimulatorResourceModel rep;
307 rep.addAttribute("power", "on");
308 rep.addAttribute("intensity", 7);
310 SimulatorResult result = resource->post(rep, queryParams, callback);
311 if ( SIMULATOR_SUCCESS == result)
312 std::cout << "POST is successfull!" << std::endl;
314 std::cout << "POST is failed!error: " << result << std::endl;
320 std::cout << "############### MAIN MENU###############" << std::endl;
321 std::cout << "1. Client Controller Test" << std::endl;
322 std::cout << "2. Set Logger" << std::endl;
323 std::cout << "3. Help" << std::endl;
324 std::cout << "0. Exit" << std::endl;
325 std::cout << "######################################" << std::endl;
330 std::cout << "1. Default console logger" << std::endl;
331 std::cout << "2. Default file logger" << std::endl;
332 std::cout << "3. custom logger" << std::endl;
336 if (choice <= 0 || choice > 3)
338 std::cout << "Invalid selection !" << std::endl;
346 if (false == SimulatorManager::getInstance()->setDefaultConsoleLogger())
347 std::cout << "Failed to set the default console logger" << std::endl;
351 std::string filePath;
352 std::cout << "Enter the file path (without file name) : ";
353 std::cin >> filePath;
354 if (false == SimulatorManager::getInstance()->setDefaultFileLogger(filePath))
355 std::cout << "Failed to set default file logger" << std::endl;
357 case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
363 ClientController clientController;
369 std::cout << "Enter your choice: ";
371 if (choice < 0 || choice > 3)
373 std::cout << "Invaild choice !" << std::endl; continue;
378 case 1: clientController.startTest();
379 std::cout << "Welcome back to main menu !" << std::endl;
381 case 2: setLogger(); break;
382 case 3: printMainMenu(); break;
383 case 0: cont = false;
387 std::cout << "Terminating test !!!" << std::endl;