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) << " "
32 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
34 class SimLightResource
44 std::cout << "Enter your choice: ";
46 if (choice < 0 || choice > 10)
48 std::cout << "Invaild choice !" << std::endl; continue;
53 case 1 : simulateResource(); break;
54 case 2 : displayResource(); break;
55 case 3 : deleteResource(); break;
56 case 4 : updateAttributePower(); break;
57 case 5 : updateAttributeIntensity(); break;
58 case 6 : automateResourceUpdate(); break;
59 case 7 : automateAttributeUpdate(); break;
60 case 8 : stopAutomation(); break;
61 case 9 : getObservers(); break;
62 case 10: printMenu(); break;
71 std::cout << "########### LIGHT RESOURCE TESTING ###########" << std::endl;
72 std::cout << "1. Simulate resource" << std::endl;
73 std::cout << "2. Display resource information" << std::endl;
74 std::cout << "3. Delete resource" << std::endl;
75 std::cout << "4. Update attribute \"power\"" << std::endl;
76 std::cout << "5. Update attribute \"intensity\"" << std::endl;
77 std::cout << "6. Automate resource update" << std::endl;
78 std::cout << "7. Automate attributes update" << std::endl;
79 std::cout << "8. Stop Automation" << std::endl;
80 std::cout << "9. Get Observers of a resource" << std::endl;
81 std::cout << "10: Help" << std::endl;
82 std::cout << "0. Exit" << std::endl;
83 std::cout << "#######################################" << std::endl;
88 if (0 == m_resources.size())
90 std::cout << "No resouces!" << std::endl;
95 for (auto & resource : m_resources)
97 std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
101 std::cout << "Choose the resource: ";
104 if (choice < 1 || choice > index - 1)
106 std::cout << "Invalid choice !" << std::endl;
113 void onResourceModelChanged(const std::string &uri,
114 const SimulatorResourceModel &resModel)
116 std::cout << "[callback] Resource model is changed URI: " << uri.c_str()
117 << " Count : " << resModel.size() << std::endl;
118 std::cout << "#### Modified attributes are ####" << std::endl;
119 for (auto & attribute : resModel.getAttributes())
121 std::cout << attribute.second.getName() << " : "
122 << attribute.second.valueToString().c_str() << std::endl;
124 std::cout << "########################" << std::endl;
127 void simulateResource()
129 SimulatorResourceServer::ResourceModelChangedCB callback = std::bind(
130 &SimLightResource::onResourceModelChanged, this, std::placeholders::_1,
131 std::placeholders::_2);
135 std::string configPath;
136 std::cout << "Enter RAML path: ";
137 std::cin>>configPath;
138 SimulatorResourceServerSP resource =
139 SimulatorManager::getInstance()->createResource(configPath, callback);
140 m_resources.push_back(resource);
141 std::cout << "Resource created successfully! URI= " << resource->getURI().c_str()
144 catch (InvalidArgsException &e)
146 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
147 << e.what() << "]" << std::endl;
149 catch (SimulatorException &e)
151 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: "
152 << e.what() << "]" << std::endl;
156 void deleteResource()
159 std::cout << "1. Delete single resource" << std::endl;
160 std::cout << "2. Delete resources on resource types" << std::endl;
161 std::cout << "3. Delete all resources" << std::endl;
163 std::cout << "Enter your choice: ";
165 if (choice < 1 || choice > 3)
167 std::cout << "Invalid choice !" << std::endl;
175 int index = selectResource();
179 SimulatorManager::getInstance()->deleteResource(m_resources[index - 1]);
180 std::cout << "Resource deleted successfully! " << std::endl;
181 m_resources.erase(m_resources.begin() + (index - 1));
187 std::string resourceType;
188 std::cout << "Enter resource type: ";
189 std::cin >> resourceType;
190 if (resourceType.empty())
192 std::cout << "Invalid resource type!" << std::endl;
198 SimulatorManager::getInstance()->deleteResource(resourceType);
199 std::cout << "Resources of type \"" << resourceType << "\"" <<
200 " deleted successfully! " << std::endl;
201 std::vector<SimulatorResourceServerSP>::iterator ite = m_resources.begin();
202 while (ite != m_resources.end())
204 if (!resourceType.compare((*ite)->getResourceType()))
206 ite = m_resources.erase(ite);
212 catch (InvalidArgsException &e)
214 std::cout << "InvalidArgsException occured [code : " << e.code()
215 << " Detail: " << e.what() << "]" << std::endl;
217 catch (SimulatorException &e)
219 std::cout << "SimulatorException occured [code : " << e.code()
220 << " Detail: " << e.what() << "]" << std::endl;
226 SimulatorManager::getInstance()->deleteResource();
227 std::cout << "All resources deleted successfully! " << std::endl;
234 void updateAttributePower()
236 int index = selectResource();
240 SimulatorResourceServerSP resource = m_resources[index - 1];
241 SimulatorResourceModel resModel = resource->getModel();
242 SimulatorResourceModel::Attribute powerAttribute;
243 resModel.getAttribute("power", powerAttribute);
245 int allowedValuesSize = powerAttribute.getAllowedValuesSize();
246 if (0 == allowedValuesSize)
248 std::cout << "This attribute does not have allowed values!" << std::endl;
252 std::cout << "Setting the new values from allowed values list to power attribute" <<
254 // Update all possible values from allowed values
255 for (int index = 0; index < allowedValuesSize; index++)
257 // Update the new value and display the resource model after modifying
258 resource->updateFromAllowedValues("power", index);
259 std::cout << "Attribute value is modified ####" << std::endl;
261 // Display the resource to user to verify the changed attribute value
262 displayResource(resource);
263 std::cout << std::endl << std::endl;
265 // Get user input for continuing this operation
266 if ((index + 1) < allowedValuesSize)
269 std::cout << "Would you like to change attribute value again ? (1/0): ";
276 std::cout << "All the allowed values are tried!" << std::endl;
279 void updateAttributeIntensity()
281 int index = selectResource();
285 SimulatorResourceServerSP resource = m_resources[index - 1];
286 SimulatorResourceModel resModel = resource->getModel();
287 SimulatorResourceModel::Attribute intensityAttribute;
288 resModel.getAttribute("intensity", intensityAttribute);
291 intensityAttribute.getRange(min, max);
294 std::cout << "This attribute does not have range!" << std::endl;
298 std::cout << "Setting the new values from allowed values list to intensity attribute"
300 // Update all possible values from allowed values
301 for (int index = min; index <= max; index++)
303 // Update the new value and display the resource model after modifying
304 resource->updateAttributeValue("intensity", index);
305 std::cout << "Attribute value is modified ####" << std::endl;
307 // Display the resource to user to verify the changed attribute value
308 displayResource(resource);
309 std::cout << std::endl << std::endl;
311 // Get user input for continuing this operation
312 if ((index + 1) <= max)
315 std::cout << "Would you like to change attribute value again ? (1/0): ";
322 std::cout << "All the allowed values are tried!" << std::endl;
325 void displayResource()
327 int index = selectResource();
331 SimulatorResourceServerSP resource = m_resources[index - 1];
332 displayResource(resource);
335 void displayResource(SimulatorResourceServerSP resource)
337 std::cout << "#############################" << std::endl;
338 std::cout << "Name: " << resource->getName().c_str() << std::endl;
339 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
340 std::cout << "R. Type: " << resource->getResourceType().c_str() << std::endl;
341 std::cout << "I. Type: " << resource->getInterfaceType().c_str() << std::endl;
344 SimulatorResourceModel resModel = resource->getModel();
345 std::map<std::string, SimulatorResourceModel::Attribute> attributes =
346 resModel.getAttributes();
347 std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
348 for (auto & attribute : attributes)
350 std::cout << (attribute.second).getName() << " : {" << std::endl;
351 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
353 (attribute.second).getRange(min, max);
354 std::cout << "min: " << min << std::endl;
355 std::cout << "max: " << max << std::endl;
356 std::cout << "allowed values : ";
358 for (auto & value : (attribute.second).allowedValuesToString())
359 std::cout << value << " ";
360 std::cout << "]" << std::endl;
361 std::cout << "}" << std::endl << std::endl;
363 std::cout << "#############################" << std::endl;
366 void onUpdateAutomationCompleted(const std::string &uri,
369 std::cout << "Update automation is completed [URI: " << uri.c_str()
370 << " AutomationID: " << id << "] ###" << std::endl;
373 void automateResourceUpdate()
375 int index = selectResource();
379 AutomationType type = AutomationType::NORMAL;
381 std::cout << "Press 1 if you want recurrent automation: ";
384 type = AutomationType::RECURRENT;
388 int id = m_resources[index - 1]->startUpdateAutomation(type, 500,
389 std::bind(&SimLightResource::onUpdateAutomationCompleted, this,
390 std::placeholders::_1, std::placeholders::_2));
392 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
394 catch (SimulatorException &e)
396 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
397 e.what() << "]" << std::endl;
401 void automateAttributeUpdate()
403 int index = selectResource();
407 SimulatorResourceServerSP resource = m_resources[index - 1];
408 SimulatorResourceModel resModel = resource->getModel();
409 std::map<std::string, SimulatorResourceModel::Attribute> attributes =
410 resModel.getAttributes();
412 for (auto & attribute : attributes)
414 std::cout << ++size << ": " << attribute.first.c_str() << std::endl;
419 std::cout << "This resource doest not contain any attributes!" << std::endl;
424 std::cout << "Select the attribute which you want to automate for updation: " <<
427 if (choice < 0 || choice > size)
429 std::cout << "Invalid selection!" << std::endl;
434 std::string attributeName;
435 for (auto & attribute : attributes)
437 if (count == choice - 1)
439 attributeName = attribute.first;
446 AutomationType type = AutomationType::NORMAL;
447 std::cout << "Press 1 if you want recurrent automation: ";
450 type = AutomationType::RECURRENT;
452 std::cout << "Requesting attribute automation for " << attributeName.c_str() <<
458 int id = resource->startUpdateAutomation(attributeName, type, 500,
459 std::bind(&SimLightResource::onUpdateAutomationCompleted, this,
460 std::placeholders::_1, std::placeholders::_2));
461 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
463 catch (SimulatorException &e)
465 std::cout << "SimulatorException occured [Error: " << e.code() << " Details: " <<
466 e.what() << "]" << std::endl;
470 void stopAutomation()
472 int index = selectResource();
476 SimulatorResourceServerSP resource = m_resources[index - 1];
478 // Select the automation to stop
479 std::vector<int> ids;
481 std::vector<int> rids = resource->getResourceAutomationIds();
482 std::vector<int> aids = resource->getAttributeAutomationIds();
483 ids.insert(ids.end(), rids.begin(), rids.end());
484 ids.insert(ids.end(), aids.begin(), aids.end());
489 std::cout << "No automation operation is going on this resource right now!" <<
494 for (auto & id : ids)
495 std::cout << id << " ";
498 std::cout << "\nEnter automation id: " << std::endl;
499 std::cin >> automationid;
500 resource->stopUpdateAutomation(automationid);
503 void onObserverChanged(const std::string &uri, ObservationStatus state,
504 const ObserverInfo &observerInfo)
506 std::cout << "[callback] Observer notification received..." << uri.c_str() << std::endl;
507 std::ostringstream out;
508 out << "ID: " << (int) observerInfo.id << std::endl;
509 out << " [address: " << observerInfo.address << " port: " << observerInfo.port
511 std::cout << out.str();
516 int index = selectResource();
520 SimulatorResourceServerSP resource = m_resources[index - 1];
522 SimulatorResourceServer::ObserverCB callback = std::bind(
523 &SimLightResource::onObserverChanged, this, std::placeholders::_1,
524 std::placeholders::_2, std::placeholders::_3);
525 resource->setObserverCallback(callback);
527 std::vector<ObserverInfo> observersList = resource->getObserversList();
529 std::cout << "##### Number of Observers [" << observersList.size() << "]" << std::endl;
530 for (auto & observerInfo : observersList)
532 std::cout << " ID : " << (int) observerInfo.id << " [address: " <<
533 observerInfo.address << " port: " << observerInfo.port << "]" << std::endl;
535 std::cout << "########################" << std::endl;
539 std::vector<SimulatorResourceServerSP> m_resources;
544 std::cout << "############### MAIN MENU###############" << std::endl;
545 std::cout << "1. Test simulation of resource" << std::endl;
546 std::cout << "2. Set Logger" << std::endl;
547 std::cout << "3. Help" << std::endl;
548 std::cout << "0. Exit" << std::endl;
549 std::cout << "######################################" << std::endl;
554 std::cout << "1. Default console logger" << std::endl;
555 std::cout << "2. Default file logger" << std::endl;
556 std::cout << "3. custom logger" << std::endl;
560 if (choice <= 0 || choice > 3)
562 std::cout << "Invalid selection !" << std::endl;
570 if (false == SimulatorManager::getInstance()->setConsoleLogger())
571 std::cout << "Failed to set the default console logger" << std::endl;
575 std::string filePath;
576 std::cout << "Enter the file path (without file name) : ";
577 std::cin >> filePath;
578 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
579 std::cout << "Failed to set default file logger" << std::endl;
581 case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
585 int main(int argc, char *argv[])
587 SimLightResource lightResource;
594 std::cout << "Enter your choice: ";
596 if (choice < 0 || choice > 3)
598 std::cout << "Invaild choice !" << std::endl; continue;
603 case 1: lightResource.startTest();
604 std::cout << "Welcome back to main menu !" << std::endl;
606 case 2: setLogger(); break;
607 case 3: printMainMenu(); break;
608 case 0: cont = false;
612 std::cout << "Terminating test !!!" << std::endl;