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 SimLightResource
33 std::cout << "Enter your choice: ";
35 if (choice < 0 || choice > 9)
37 std::cout << "Invaild choice !" << std::endl; continue;
42 case 1: simulateResource(); break;
43 case 2: displayResource(); break;
44 case 3: deleteResource(); break;
45 case 4: updateAttributePower(); break;
46 case 5: updateAttributeIntensity(); break;
47 case 6: automateResourceUpdate(); break;
48 case 7: automateAttributeUpdate(); break;
49 case 8: stopAutomation(); break;
50 case 9: printMenu(); break;
59 std::cout << "########### LIGHT RESOURCE TESTING ###########" << std::endl;
60 std::cout << "1. Simulate resource" << std::endl;
61 std::cout << "2. Display resource information" << std::endl;
62 std::cout << "3. Delete resource" << std::endl;
63 std::cout << "4. Update attribute \"power\"" << std::endl;
64 std::cout << "5. Update attribute \"intensity\"" << std::endl;
65 std::cout << "6. Automate resource update" << std::endl;
66 std::cout << "7. Automate attributes update" << std::endl;
67 std::cout << "8. Stop Automation" << std::endl;
68 std::cout << "9: Help" << std::endl;
69 std::cout << "0. Exit" << std::endl;
70 std::cout << "#######################################" << std::endl;
76 for (auto & resource : m_resources)
78 std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
82 std::cout << "Choose the resource: ";
85 if (choice < 1 || choice > index - 1)
87 std::cout << "Invalid choice !" << std::endl;
94 void onResourceModelChanged(const std::string &uri,
95 const SimulatorResourceModel &resModel)
97 std::cout << "[callback] Resource model is changed URI: " << uri.c_str() << " Count : " <<
98 resModel.size() << std::endl;
99 std::cout << "#### Modified attributes are ####" << std::endl;
100 for (auto & attribute : resModel.getAttributes())
102 std::cout << attribute.second.getName() << " : " << attribute.second.valueToString().c_str() << std::endl;
104 std::cout << "########################" << std::endl;
107 void simulateResource()
109 SimulatorResource::ResourceModelChangedCB callback = std::bind(
110 &SimLightResource::onResourceModelChanged, this, std::placeholders::_1, std::placeholders::_2);
111 SimulatorResourcePtr resource = SimulatorManager::getInstance()->createResource("", callback);
112 if (NULL == resource.get())
113 std::cout << "Failed to create resource" << std::endl;
115 m_resources.push_back(resource);
116 std::cout << "Resource created successfully! URI= " << resource->getURI().c_str() << std::endl;
119 void deleteResource()
121 int index = selectResource();
125 SimulatorManager::getInstance()->deleteResource(m_resources[index - 1]);
126 std::cout << "Resource deleted successfully! " << std::endl;
129 void updateAttributePower()
131 int index = selectResource();
135 SimulatorResourcePtr resource = m_resources[index - 1];
136 SimulatorResourceModel resModel = resource->getModel();
137 SimulatorResourceModel::Attribute powerAttribute;
138 resModel.getAttribute("power", powerAttribute);
140 int allowedValuesSize = powerAttribute.getAllowedValuesSize();
141 if (0 == allowedValuesSize)
143 std::cout << "This attribute does not have allowed values!" << std::endl;
147 std::cout << "Setting the new values from allowed values list to power attribute" << std::endl;
148 // Update all possible values from allowed values
149 for (int index = 0; index < allowedValuesSize; index++)
151 // Update the new value and display the resource model after modifying
152 resource->updateAttributeFromAllowedValues("power", index);
153 std::cout << "Attribute value is modified ####" << std::endl;
155 // Display the resource to user to verify the changed attribute value
156 displayResource(resource);
157 std::cout << std::endl << std::endl;
159 // Get user input for continuing this operation
160 if ((index + 1) < allowedValuesSize)
163 std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
170 std::cout << "All the allowed values are tried!" << std::endl;
173 void updateAttributeIntensity()
175 int index = selectResource();
179 SimulatorResourcePtr resource = m_resources[index - 1];
180 SimulatorResourceModel resModel = resource->getModel();
181 SimulatorResourceModel::Attribute intensityAttribute;
182 resModel.getAttribute("intensity", intensityAttribute);
184 int allowedValuesSize = intensityAttribute.getAllowedValuesSize();
185 if (0 == allowedValuesSize)
187 std::cout << "This attribute does not have allowed values!" << std::endl;
191 std::cout << "Setting the new values from allowed values list to intensity attribute" << std::endl;
192 // Update all possible values from allowed values
193 for (int index = 0; index < allowedValuesSize; index++)
195 // Update the new value and display the resource model after modifying
196 resource->updateAttributeFromAllowedValues("intensity", index);
197 std::cout << "Attribute value is modified ####" << std::endl;
199 // Display the resource to user to verify the changed attribute value
200 displayResource(resource);
201 std::cout << std::endl << std::endl;
203 // Get user input for continuing this operation
204 if ((index + 1) < allowedValuesSize)
207 std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
214 std::cout << "All the allowed values are tried!" << std::endl;
217 void displayResource()
219 int index = selectResource();
223 SimulatorResourcePtr resource = m_resources[index - 1];
224 displayResource(resource);
227 void displayResource(SimulatorResourcePtr resource)
229 std::cout << "#############################" << std::endl;
230 std::cout << "Name: " << resource->getName().c_str() << std::endl;
231 std::cout << "URI: " << resource->getURI().c_str() << std::endl;
232 std::cout << "R. Type: " << resource->getResourceType().c_str() << std::endl;
233 std::cout << "I. Type: " << resource->getInterfaceType().c_str() << std::endl;
236 SimulatorResourceModel resModel = resource->getModel();
237 std::map<std::string, SimulatorResourceModel::Attribute> attributes = resModel.getAttributes();
238 std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
239 for (auto & attribute : attributes)
241 std::cout << (attribute.second).getName() << " : {" << std::endl;
242 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
244 (attribute.second).getRange(min, max);
245 std::cout << "min: " << min << std::endl;
246 std::cout << "max: " << max << std::endl;
247 std::cout << "allowed values : " << (attribute.second).allowedValuesToString() << std::endl;
248 std::cout << "}" << std::endl << std::endl;
250 std::cout << "#############################" << std::endl;
253 void automateResourceUpdate()
255 int index = selectResource();
260 if (SIMULATOR_SUCCESS != m_resources[index - 1]->startUpdateAutomation(AutomationType::NORMAL, id))
261 std::cout << "startUpdateAutomation() returned error!" << std::endl;
263 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
266 void automateAttributeUpdate()
268 int index = selectResource();
272 SimulatorResourcePtr resource = m_resources[index - 1];
273 SimulatorResourceModel resModel = resource->getModel();
274 std::map<std::string, SimulatorResourceModel::Attribute> attributes = resModel.getAttributes();
276 for (auto & attribute : attributes)
278 std::cout << ++size << ": " << attribute.first.c_str() << std::endl;
283 std::cout << "This resource doest not contain any attributes!" << std::endl;
288 std::cout << "Select the attribute which you want to automate for updation: " << std::endl;
290 if (choice == -1 || choice > size)
292 std::cout << "Invalid selection!" << std::endl;
297 std::string attributeName;
298 for (auto & attribute : attributes)
300 if (count == choice - 1)
302 attributeName = attribute.first;
309 std::cout << "Requesting attribute automation for " << attributeName.c_str() << std::endl;
311 if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(attributeName, AutomationType::NORMAL, id))
312 std::cout << "startUpdateAutomation() returned error!" << std::endl;
314 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
317 void stopAutomation()
319 int index = selectResource();
323 SimulatorResourcePtr resource = m_resources[index - 1];
325 std::cout << "Enter automation id: " << std::endl;
326 std::cin >> automationid;
327 resource->stopUpdateAutomation(automationid);
331 std::vector<SimulatorResourcePtr> m_resources;
336 std::cout << "############### MAIN MENU###############" << std::endl;
337 std::cout << "1. Test simulation of light resource" << std::endl;
338 std::cout << "2. Help" << std::endl;
339 std::cout << "0. Exit" << std::endl;
340 std::cout << "######################################" << std::endl;
345 SimLightResource lightResource;
352 std::cout << "Enter your choice: ";
354 if (choice < 0 || choice > 2)
356 std::cout << "Invaild choice !" << std::endl; continue;
361 case 1: lightResource.startTest();
362 std::cout << "Welcome back to main menu !" << std::endl;
364 case 2: printMainMenu(); break;
365 case 0: cont = false;
369 std::cout << "Terminating test !!!" << std::endl;