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
37 void startTest(std::string &configPath)
44 std::cout << "Enter your choice: ";
46 if (choice < 0 || choice > 10)
48 std::cout << "Invaild choice !" << std::endl; continue;
53 case 1 : simulateResource(configPath); break;
54 case 2 : displayResource(); break;
60 catch (InvalidArgsException &e)
62 std::cout << "InvalidArgsException occured [code : " << e.code() <<
63 " Detail: " << e.what() << "]" << std::endl;
66 case 4 : updateAttributePower(); break;
67 case 5 : updateAttributeIntensity(); break;
68 case 6 : automateResourceUpdate(); break;
69 case 7 : automateAttributeUpdate(); break;
70 case 8 : stopAutomation(); break;
71 case 9 : getObservers(); break;
72 case 10: printMenu(); break;
81 std::cout << "########### LIGHT RESOURCE TESTING ###########" << std::endl;
82 std::cout << "1. Simulate resource" << std::endl;
83 std::cout << "2. Display resource information" << std::endl;
84 std::cout << "3. Delete resource" << std::endl;
85 std::cout << "4. Update attribute \"power\"" << std::endl;
86 std::cout << "5. Update attribute \"intensity\"" << std::endl;
87 std::cout << "6. Automate resource update" << std::endl;
88 std::cout << "7. Automate attributes update" << std::endl;
89 std::cout << "8. Stop Automation" << std::endl;
90 std::cout << "9. Get Observers of a resource" << std::endl;
91 std::cout << "10: Help" << std::endl;
92 std::cout << "0. Exit" << std::endl;
93 std::cout << "#######################################" << std::endl;
98 if (0 == m_resources.size())
100 std::cout << "No resouces!" << std::endl;
105 for (auto & resource : m_resources)
107 std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
111 std::cout << "Choose the resource: ";
114 if (choice < 1 || choice > index - 1)
116 std::cout << "Invalid choice !" << std::endl;
123 void onResourceModelChanged(const std::string &uri,
124 const SimulatorResourceModel &resModel)
126 std::cout << "[callback] Resource model is changed URI: " << uri.c_str()
127 << " Count : " << resModel.size() << std::endl;
128 std::cout << "#### Modified attributes are ####" << std::endl;
129 for (auto & attribute : resModel.getAttributes())
131 std::cout << attribute.second.getName() << " : "
132 << attribute.second.valueToString().c_str() << std::endl;
134 std::cout << "########################" << std::endl;
137 void simulateResource(std::string &configPath)
139 SimulatorResourceServer::ResourceModelChangedCB callback = std::bind(
140 &SimLightResource::onResourceModelChanged, this, std::placeholders::_1,
141 std::placeholders::_2);
145 SimulatorResourceServerSP resource =
146 SimulatorManager::getInstance()->createResource(configPath, callback);
147 m_resources.push_back(resource);
148 std::cout << "Resource created successfully! URI= " << resource->getURI().c_str()
151 catch (InvalidArgsException &e)
153 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
154 << e.what() << "]" << std::endl;
156 catch (SimulatorException &e)
158 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: "
159 << e.what() << "]" << std::endl;
163 void deleteResource()
166 std::cout << "1. Delete single resource" << std::endl;
167 std::cout << "2. Delete resources on resource types" << std::endl;
168 std::cout << "3. Delete all resources" << std::endl;
170 std::cout << "Enter your choice: ";
172 if (choice < 1 || choice > 3)
174 std::cout << "Invalid choice !" << std::endl;
182 int index = selectResource();
186 SimulatorManager::getInstance()->deleteResource(m_resources[index - 1]);
187 std::cout << "Resource deleted successfully! " << std::endl;
188 m_resources.erase(m_resources.begin() + (index - 1));
194 std::string resourceType;
195 std::cout << "Enter resource type: ";
196 std::cin >> resourceType;
197 if (resourceType.empty())
199 std::cout << "Invalid resource type!" << std::endl;
205 SimulatorManager::getInstance()->deleteResource(resourceType);
206 std::cout << "Resources of type \"" << resourceType << "\"" <<
207 " deleted successfully! " << std::endl;
208 std::vector<SimulatorResourceServerSP>::iterator ite = m_resources.begin();
209 while (ite != m_resources.end())
211 if (!resourceType.compare((*ite)->getResourceType()))
213 ite = m_resources.erase(ite);
219 catch (InvalidArgsException &e)
221 std::cout << "InvalidArgsException occured [code : " << e.code()
222 << " Detail: " << e.what() << "]" << std::endl;
224 catch (SimulatorException &e)
226 std::cout << "SimulatorException occured [code : " << e.code()
227 << " Detail: " << e.what() << "]" << std::endl;
233 SimulatorManager::getInstance()->deleteResource();
234 std::cout << "All resources deleted successfully! " << std::endl;
241 void updateAttributePower()
243 int index = selectResource();
247 SimulatorResourceServerSP resource = m_resources[index - 1];
248 SimulatorResourceModel resModel = resource->getModel();
249 SimulatorResourceModel::Attribute powerAttribute;
250 resModel.getAttribute("power", powerAttribute);
252 int allowedValuesSize = powerAttribute.getAllowedValuesSize();
253 if (0 == allowedValuesSize)
255 std::cout << "This attribute does not have allowed values!" << std::endl;
259 std::cout << "Setting the new values from allowed values list to power attribute" <<
261 // Update all possible values from allowed values
262 for (int index = 0; index < allowedValuesSize; index++)
264 // Update the new value and display the resource model after modifying
265 resource->updateFromAllowedValues("power", index);
266 std::cout << "Attribute value is modified ####" << std::endl;
268 // Display the resource to user to verify the changed attribute value
269 displayResource(resource);
270 std::cout << std::endl << std::endl;
272 // Get user input for continuing this operation
273 if ((index + 1) < allowedValuesSize)
276 std::cout << "Would you like to change attribute value again ? (1/0): ";
283 std::cout << "All the allowed values are tried!" << std::endl;
286 void updateAttributeIntensity()
288 int index = selectResource();
292 SimulatorResourceServerSP resource = m_resources[index - 1];
293 SimulatorResourceModel resModel = resource->getModel();
294 SimulatorResourceModel::Attribute intensityAttribute;
295 resModel.getAttribute("intensity", intensityAttribute);
298 intensityAttribute.getRange(min, max);
301 std::cout << "This attribute does not have range!" << std::endl;
305 std::cout << "Setting the new values from allowed values list to intensity attribute"
307 // Update all possible values from allowed values
308 for (int index = min; index <= max; index++)
310 // Update the new value and display the resource model after modifying
311 resource->updateAttributeValue("intensity", index);
312 std::cout << "Attribute value is modified ####" << std::endl;
314 // Display the resource to user to verify the changed attribute value
315 displayResource(resource);
316 std::cout << std::endl << std::endl;
318 // Get user input for continuing this operation
319 if ((index + 1) <= max)
322 std::cout << "Would you like to change attribute value again ? (1/0): ";
329 std::cout << "All the allowed values are tried!" << std::endl;
332 void displayResource()
334 int index = selectResource();
338 SimulatorResourceServerSP resource = m_resources[index - 1];
339 displayResource(resource);
342 void displayResource(SimulatorResourceServerSP resource)
344 std::cout << "#############################" << std::endl;
345 std::cout << "Name: " << resource->getName().c_str() << std::endl;
346 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
347 std::cout << "R. Type: " << resource->getResourceType().c_str() << std::endl;
348 std::cout << "I. Type: " << resource->getInterfaceType().c_str() << std::endl;
351 SimulatorResourceModel resModel = resource->getModel();
352 std::map<std::string, SimulatorResourceModel::Attribute> attributes =
353 resModel.getAttributes();
354 std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
355 for (auto & attribute : attributes)
357 std::cout << (attribute.second).getName() << " : {" << std::endl;
358 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
360 (attribute.second).getRange(min, max);
361 std::cout << "min: " << min << std::endl;
362 std::cout << "max: " << max << std::endl;
363 std::cout << "allowed values : ";
365 for (auto & value : (attribute.second).allowedValuesToString())
366 std::cout << value << " ";
367 std::cout << "]" << std::endl;
368 std::cout << "}" << std::endl << std::endl;
370 std::cout << "#############################" << std::endl;
373 void onUpdateAutomationCompleted(const std::string &uri,
376 std::cout << "Update automation is completed [URI: " << uri.c_str()
377 << " AutomationID: " << id << "] ###" << std::endl;
380 void automateResourceUpdate()
382 int index = selectResource();
386 AutomationType type = AutomationType::NORMAL;
388 std::cout << "Press 1 if you want recurrent automation: ";
391 type = AutomationType::RECURRENT;
395 int id = m_resources[index - 1]->startUpdateAutomation(type,
396 std::bind(&SimLightResource::onUpdateAutomationCompleted, this,
397 std::placeholders::_1, std::placeholders::_2));
399 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
401 catch (SimulatorException &e)
403 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
404 e.what() << "]" << std::endl;
408 void automateAttributeUpdate()
410 int index = selectResource();
414 SimulatorResourceServerSP resource = m_resources[index - 1];
415 SimulatorResourceModel resModel = resource->getModel();
416 std::map<std::string, SimulatorResourceModel::Attribute> attributes =
417 resModel.getAttributes();
419 for (auto & attribute : attributes)
421 std::cout << ++size << ": " << attribute.first.c_str() << std::endl;
426 std::cout << "This resource doest not contain any attributes!" << std::endl;
431 std::cout << "Select the attribute which you want to automate for updation: " <<
434 if (choice < 0 || choice > size)
436 std::cout << "Invalid selection!" << std::endl;
441 std::string attributeName;
442 for (auto & attribute : attributes)
444 if (count == choice - 1)
446 attributeName = attribute.first;
453 AutomationType type = AutomationType::NORMAL;
454 std::cout << "Press 1 if you want recurrent automation: ";
457 type = AutomationType::RECURRENT;
459 std::cout << "Requesting attribute automation for " << attributeName.c_str() <<
465 int id = resource->startUpdateAutomation(attributeName, type,
466 std::bind(&SimLightResource::onUpdateAutomationCompleted, this,
467 std::placeholders::_1, std::placeholders::_2));
468 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
470 catch (SimulatorException &e)
472 std::cout << "SimulatorException occured [Error: " << e.code() << " Details: " <<
473 e.what() << "]" << std::endl;
477 void stopAutomation()
479 int index = selectResource();
483 SimulatorResourceServerSP resource = m_resources[index - 1];
485 // Select the automation to stop
486 std::vector<int> ids;
488 std::vector<int> rids = resource->getResourceAutomationIds();
489 std::vector<int> aids = resource->getAttributeAutomationIds();
490 ids.insert(ids.end(), rids.begin(), rids.end());
491 ids.insert(ids.end(), aids.begin(), aids.end());
496 std::cout << "No automation operation is going on this resource right now!" <<
501 for (auto & id : ids)
502 std::cout << id << " ";
505 std::cout << "\nEnter automation id: " << std::endl;
506 std::cin >> automationid;
507 resource->stopUpdateAutomation(automationid);
510 void onObserverChanged(const std::string &uri, ObservationStatus state,
511 const ObserverInfo &observerInfo)
513 std::cout << "[callback] Observer notification received..." << uri.c_str() << std::endl;
514 std::ostringstream out;
515 out << "ID: " << (int) observerInfo.id << std::endl;
516 out << " [address: " << observerInfo.address << " port: " << observerInfo.port
518 std::cout << out.str();
523 int index = selectResource();
527 SimulatorResourceServerSP resource = m_resources[index - 1];
529 SimulatorResourceServer::ObserverCB callback = std::bind(
530 &SimLightResource::onObserverChanged, this, std::placeholders::_1,
531 std::placeholders::_2, std::placeholders::_3);
532 resource->setObserverCallback(callback);
534 std::vector<ObserverInfo> observersList = resource->getObserversList();
536 std::cout << "##### Number of Observers [" << observersList.size() << "]" << std::endl;
537 for (auto & observerInfo : observersList)
539 std::cout << " ID : " << (int) observerInfo.id << " [address: " <<
540 observerInfo.address << " port: " << observerInfo.port << "]" << std::endl;
542 std::cout << "########################" << std::endl;
546 std::vector<SimulatorResourceServerSP> m_resources;
551 std::cout << "############### MAIN MENU###############" << std::endl;
552 std::cout << "1. Test simulation of resource" << std::endl;
553 std::cout << "2. Set Logger" << std::endl;
554 std::cout << "3. Help" << std::endl;
555 std::cout << "0. Exit" << std::endl;
557 "To set the Resource from RAML file, run the service provider with argument of Path of Raml File."
560 "Example: ./simulator-server PATH-TO-RAML-FILE"
562 std::cout << "######################################" << std::endl;
567 std::cout << "1. Default console logger" << std::endl;
568 std::cout << "2. Default file logger" << std::endl;
569 std::cout << "3. custom logger" << std::endl;
573 if (choice <= 0 || choice > 3)
575 std::cout << "Invalid selection !" << std::endl;
583 if (false == SimulatorManager::getInstance()->setConsoleLogger())
584 std::cout << "Failed to set the default console logger" << std::endl;
588 std::string filePath;
589 std::cout << "Enter the file path (without file name) : ";
590 std::cin >> filePath;
591 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
592 std::cout << "Failed to set default file logger" << std::endl;
594 case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
598 int main(int argc, char *argv[])
600 std::string configPath = "";
604 char *value = argv[1];
605 configPath.append(value);
611 SimLightResource lightResource;
618 std::cout << "Enter your choice: ";
620 if (choice < 0 || choice > 3)
622 std::cout << "Invaild choice !" << std::endl; continue;
627 case 1: lightResource.startTest(configPath);
628 std::cout << "Welcome back to main menu !" << std::endl;
630 case 2: setLogger(); break;
631 case 3: printMainMenu(); break;
632 case 0: cont = false;
636 std::cout << "Terminating test !!!" << std::endl;