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 SimLightResource
43 std::cout << "Enter your choice: ";
45 if (choice < 0 || choice > 9)
47 std::cout << "Invaild choice !" << std::endl; continue;
52 case 1: simulateResource(); break;
53 case 2: displayResource(); break;
54 case 3: deleteResource(); break;
55 case 4: updateAttributePower(); break;
56 case 5: updateAttributeIntensity(); break;
57 case 6: automateResourceUpdate(); break;
58 case 7: automateAttributeUpdate(); break;
59 case 8: stopAutomation(); break;
60 case 9: printMenu(); break;
69 std::cout << "########### LIGHT RESOURCE TESTING ###########" << std::endl;
70 std::cout << "1. Simulate resource" << std::endl;
71 std::cout << "2. Display resource information" << std::endl;
72 std::cout << "3. Delete resource" << std::endl;
73 std::cout << "4. Update attribute \"power\"" << std::endl;
74 std::cout << "5. Update attribute \"intensity\"" << std::endl;
75 std::cout << "6. Automate resource update" << std::endl;
76 std::cout << "7. Automate attributes update" << std::endl;
77 std::cout << "8. Stop Automation" << std::endl;
78 std::cout << "9: Help" << std::endl;
79 std::cout << "0. Exit" << std::endl;
80 std::cout << "#######################################" << std::endl;
85 if (0 == m_resources.size())
87 std::cout << "No resouces!" << std::endl;
92 for (auto & resource : m_resources)
94 std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
98 std::cout << "Choose the resource: ";
101 if (choice < 1 || choice > index - 1)
103 std::cout << "Invalid choice !" << std::endl;
110 void onResourceModelChanged(const std::string &uri,
111 const SimulatorResourceModel &resModel)
113 std::cout << "[callback] Resource model is changed URI: " << uri.c_str() << " Count : " <<
114 resModel.size() << std::endl;
115 std::cout << "#### Modified attributes are ####" << std::endl;
116 for (auto & attribute : resModel.getAttributes())
118 std::cout << attribute.second.getName() << " : " << attribute.second.valueToString().c_str() <<
121 std::cout << "########################" << std::endl;
124 void simulateResource()
126 SimulatorResourceServer::ResourceModelChangedCB callback = std::bind(
127 &SimLightResource::onResourceModelChanged, this, std::placeholders::_1, std::placeholders::_2);
128 SimulatorResourceServerPtr resource = SimulatorManager::getInstance()->createResource("", callback);
129 if (NULL == resource.get())
130 std::cout << "Failed to create resource" << std::endl;
132 m_resources.push_back(resource);
133 std::cout << "Resource created successfully! URI= " << resource->getURI().c_str() << std::endl;
136 void deleteResource()
139 std::cout << "1. Delete single resource" << std::endl;
140 std::cout << "2. Delete resources on resource types" << std::endl;
141 std::cout << "3. Delete all resources" << std::endl;
143 std::cout << "Enter your choice: ";
145 if (choice < 1 || choice > 3)
147 std::cout << "Invalid choice !" << std::endl;
155 int index = selectResource();
159 if (SIMULATOR_SUCCESS == SimulatorManager::getInstance()->deleteResource(m_resources[index - 1]))
161 std::cout << "Resource deleted successfully! " << std::endl;
162 m_resources.erase(m_resources.begin() + (index - 1));
166 std::cout << "Failed to delete resource!" << std::endl;
171 std::string resourceType;
172 std::cout << "Enter resource type: ";
173 std::cin >> resourceType;
174 if (resourceType.empty())
176 std::cout << "Invalid resource type!" << std::endl;
180 if (SIMULATOR_SUCCESS == SimulatorManager::getInstance()->deleteResources(resourceType))
182 std::cout << "Resources of type \"" << resourceType << "\"" << " deleted successfully! " <<
184 std::vector<SimulatorResourceServerPtr>::iterator ite = m_resources.begin();
185 while (ite != m_resources.end())
187 if (!resourceType.compare((*ite)->getResourceType()))
189 ite = m_resources.erase(ite);
197 std::cout << "Failed to delete resources of type \"" << resourceType << "\"!" << std::endl;
202 if (SIMULATOR_SUCCESS == SimulatorManager::getInstance()->deleteResources())
204 std::cout << "All resources deleted successfully! " << std::endl;
209 std::cout << "Failed to delete all resources!" << std::endl;
216 void updateAttributePower()
218 int index = selectResource();
222 SimulatorResourceServerPtr resource = m_resources[index - 1];
223 SimulatorResourceModel resModel = resource->getModel();
224 SimulatorResourceModel::Attribute powerAttribute;
225 resModel.getAttribute("power", powerAttribute);
227 int allowedValuesSize = powerAttribute.getAllowedValuesSize();
228 if (0 == allowedValuesSize)
230 std::cout << "This attribute does not have allowed values!" << std::endl;
234 std::cout << "Setting the new values from allowed values list to power attribute" << std::endl;
235 // Update all possible values from allowed values
236 for (int index = 0; index < allowedValuesSize; index++)
238 // Update the new value and display the resource model after modifying
239 resource->updateAttributeFromAllowedValues("power", index);
240 std::cout << "Attribute value is modified ####" << std::endl;
242 // Display the resource to user to verify the changed attribute value
243 displayResource(resource);
244 std::cout << std::endl << std::endl;
246 // Get user input for continuing this operation
247 if ((index + 1) < allowedValuesSize)
250 std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
257 std::cout << "All the allowed values are tried!" << std::endl;
260 void updateAttributeIntensity()
262 int index = selectResource();
266 SimulatorResourceServerPtr resource = m_resources[index - 1];
267 SimulatorResourceModel resModel = resource->getModel();
268 SimulatorResourceModel::Attribute intensityAttribute;
269 resModel.getAttribute("intensity", intensityAttribute);
271 int allowedValuesSize = intensityAttribute.getAllowedValuesSize();
272 if (0 == allowedValuesSize)
274 std::cout << "This attribute does not have allowed values!" << std::endl;
278 std::cout << "Setting the new values from allowed values list to intensity attribute" << std::endl;
279 // Update all possible values from allowed values
280 for (int index = 0; index < allowedValuesSize; index++)
282 // Update the new value and display the resource model after modifying
283 resource->updateAttributeFromAllowedValues("intensity", index);
284 std::cout << "Attribute value is modified ####" << std::endl;
286 // Display the resource to user to verify the changed attribute value
287 displayResource(resource);
288 std::cout << std::endl << std::endl;
290 // Get user input for continuing this operation
291 if ((index + 1) < allowedValuesSize)
294 std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
301 std::cout << "All the allowed values are tried!" << std::endl;
304 void displayResource()
306 int index = selectResource();
310 SimulatorResourceServerPtr resource = m_resources[index - 1];
311 displayResource(resource);
314 void displayResource(SimulatorResourceServerPtr resource)
316 std::cout << "#############################" << std::endl;
317 std::cout << "Name: " << resource->getName().c_str() << std::endl;
318 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
319 std::cout << "R. Type: " << resource->getResourceType().c_str() << std::endl;
320 std::cout << "I. Type: " << resource->getInterfaceType().c_str() << std::endl;
323 SimulatorResourceModel resModel = resource->getModel();
324 std::map<std::string, SimulatorResourceModel::Attribute> attributes = resModel.getAttributes();
325 std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
326 for (auto & attribute : attributes)
328 std::cout << (attribute.second).getName() << " : {" << std::endl;
329 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
331 (attribute.second).getRange(min, max);
332 std::cout << "min: " << min << std::endl;
333 std::cout << "max: " << max << std::endl;
334 std::cout << "allowed values : " << (attribute.second).allowedValuesToString() << std::endl;
335 std::cout << "}" << std::endl << std::endl;
337 std::cout << "#############################" << std::endl;
340 void onUpdateAutomationCompleted(const std::string &uri,
343 std::cout << "Update automation is completed [URI: " << uri.c_str() << " AutomationID: " <<
344 id << "] ###" << std::endl;
347 void automateResourceUpdate()
349 int index = selectResource();
353 AutomationType type = AutomationType::NORMAL;
355 std::cout << "Press 1 if you want recurrent automation: ";
358 type = AutomationType::RECURRENT;
361 if (SIMULATOR_SUCCESS != m_resources[index - 1]->startUpdateAutomation(type,
362 std::bind(&SimLightResource::onUpdateAutomationCompleted, this, std::placeholders::_1,
363 std::placeholders::_2),
365 std::cout << "startUpdateAutomation() returned error!" << std::endl;
367 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
370 void automateAttributeUpdate()
372 int index = selectResource();
376 SimulatorResourceServerPtr resource = m_resources[index - 1];
377 SimulatorResourceModel resModel = resource->getModel();
378 std::map<std::string, SimulatorResourceModel::Attribute> attributes = resModel.getAttributes();
380 for (auto & attribute : attributes)
382 std::cout << ++size << ": " << attribute.first.c_str() << std::endl;
387 std::cout << "This resource doest not contain any attributes!" << std::endl;
392 std::cout << "Select the attribute which you want to automate for updation: " << std::endl;
394 if (choice < 0 || choice > size)
396 std::cout << "Invalid selection!" << std::endl;
401 std::string attributeName;
402 for (auto & attribute : attributes)
404 if (count == choice - 1)
406 attributeName = attribute.first;
413 AutomationType type = AutomationType::NORMAL;
414 std::cout << "Press 1 if you want recurrent automation: ";
417 type = AutomationType::RECURRENT;
419 std::cout << "Requesting attribute automation for " << attributeName.c_str() << std::endl;
421 if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(attributeName, type,
422 std::bind(&SimLightResource::onUpdateAutomationCompleted, this, std::placeholders::_1,
423 std::placeholders::_2),
425 std::cout << "startUpdateAutomation() returned error!" << std::endl;
427 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
430 void stopAutomation()
432 int index = selectResource();
436 SimulatorResourceServerPtr resource = m_resources[index - 1];
438 // Select the automation to stop
439 std::vector<int> ids;
441 std::vector<int> rids = resource->getResourceAutomationIds();
442 std::vector<int> aids = resource->getAttributeAutomationIds();
443 ids.insert(ids.end(), rids.begin(), rids.end());
444 ids.insert(ids.end(), aids.begin(), aids.end());
449 std::cout << "No automation operation is going on this resource right now!" << std::endl;
453 for (auto & id : ids)
454 std::cout << id << " ";
457 std::cout << "\nEnter automation id: " << std::endl;
458 std::cin >> automationid;
459 resource->stopUpdateAutomation(automationid);
463 std::vector<SimulatorResourceServerPtr> m_resources;
468 std::cout << "############### MAIN MENU###############" << std::endl;
469 std::cout << "1. Test simulation of light resource" << std::endl;
470 std::cout << "2. Set Logger" << std::endl;
471 std::cout << "3. Help" << std::endl;
472 std::cout << "0. Exit" << std::endl;
473 std::cout << "######################################" << std::endl;
478 std::cout << "1. Default console logger" << std::endl;
479 std::cout << "2. Default file logger" << std::endl;
480 std::cout << "3. custom logger" << std::endl;
484 if (choice <= 0 || choice > 3)
486 std::cout << "Invalid selection !" << std::endl;
494 if (false == SimulatorManager::getInstance()->setDefaultConsoleLogger())
495 std::cout << "Failed to set the default console logger" << std::endl;
499 std::string filePath;
500 std::cout << "Enter the file path (without file name) : ";
501 std::cin >> filePath;
502 if (false == SimulatorManager::getInstance()->setDefaultFileLogger(filePath))
503 std::cout << "Failed to set default file logger" << std::endl;
505 case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
511 SimLightResource lightResource;
518 std::cout << "Enter your choice: ";
520 if (choice < 0 || choice > 3)
522 std::cout << "Invaild choice !" << std::endl; continue;
527 case 1: lightResource.startTest();
528 std::cout << "Welcome back to main menu !" << std::endl;
530 case 2: setLogger(); break;
531 case 3: printMainMenu(); break;
532 case 0: cont = false;
536 std::cout << "Terminating test !!!" << std::endl;