Fix for observe request handling issue.
[platform/upstream/iotivity.git] / service / simulator / examples / server / service_provider.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "simulator_manager.h"
22
23 class SimLightResource
24 {
25     public:
26         void startTest()
27         {
28             printMenu();
29             bool cont = true;
30             while (cont)
31             {
32                 int choice = -1;
33                 std::cout << "Enter your choice: ";
34                 std::cin >> choice;
35                 if (choice < 0 || choice > 9)
36                 {
37                     std::cout << "Invaild choice !" << std::endl; continue;
38                 }
39
40                 switch (choice)
41                 {
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;
51                     case 0: cont = false;
52                 }
53             }
54         }
55
56     private:
57         void printMenu()
58         {
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;
71         }
72
73         int selectResource()
74         {
75             int index = 1;
76             for (auto & resource : m_resources)
77             {
78                 std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
79             }
80
81             int choice = -1;
82             std::cout << "Choose the resource: ";
83             std::cin >> choice;
84
85             if (choice < 1 || choice > index - 1)
86             {
87                 std::cout << "Invalid choice !" << std::endl;
88                 choice = -1;
89             }
90
91             return choice;
92         }
93
94         void onResourceModelChanged(const std::string &uri,
95                                     const SimulatorResourceModel &resModel)
96         {
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())
101             {
102                 std::cout << attribute.second.getName() << " :  " << attribute.second.valueToString().c_str() << std::endl;
103             }
104             std::cout << "########################" << std::endl;
105         }
106
107         void simulateResource()
108         {
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;
114
115             m_resources.push_back(resource);
116             std::cout << "Resource created successfully! URI= " << resource->getURI().c_str() << std::endl;
117         }
118
119         void deleteResource()
120         {
121             int index = selectResource();
122             if (-1 == index)
123                 return;
124
125             SimulatorManager::getInstance()->deleteResource(m_resources[index - 1]);
126             std::cout << "Resource deleted successfully! " << std::endl;
127         }
128
129         void updateAttributePower()
130         {
131             int index = selectResource();
132             if (-1 == index)
133                 return;
134
135             SimulatorResourcePtr resource = m_resources[index - 1];
136             SimulatorResourceModel resModel = resource->getModel();
137             SimulatorResourceModel::Attribute powerAttribute;
138             resModel.getAttribute("power", powerAttribute);
139
140             int allowedValuesSize = powerAttribute.getAllowedValuesSize();
141             if (0 == allowedValuesSize)
142             {
143                 std::cout << "This attribute does not have allowed values!" << std::endl;
144                 return;
145             }
146
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++)
150             {
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;
154
155                 // Display the resource to user to verify the changed attribute value
156                 displayResource(resource);
157                 std::cout << std::endl << std::endl;
158
159                 // Get user input for continuing this operation
160                 if ((index + 1) < allowedValuesSize)
161                 {
162                     int choice;
163                     std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
164                     std::cin >> choice;
165                     if (0 == choice)
166                         break;
167                 }
168             }
169
170             std::cout << "All the allowed values are tried!" << std::endl;
171         }
172
173         void updateAttributeIntensity()
174         {
175             int index = selectResource();
176             if (-1 == index)
177                 return;
178
179             SimulatorResourcePtr resource = m_resources[index - 1];
180             SimulatorResourceModel resModel = resource->getModel();
181             SimulatorResourceModel::Attribute intensityAttribute;
182             resModel.getAttribute("intensity", intensityAttribute);
183
184             int allowedValuesSize = intensityAttribute.getAllowedValuesSize();
185             if (0 == allowedValuesSize)
186             {
187                 std::cout << "This attribute does not have allowed values!" << std::endl;
188                 return;
189             }
190
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++)
194             {
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;
198
199                 // Display the resource to user to verify the changed attribute value
200                 displayResource(resource);
201                 std::cout << std::endl << std::endl;
202
203                 // Get user input for continuing this operation
204                 if ((index + 1) < allowedValuesSize)
205                 {
206                     int choice;
207                     std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
208                     std::cin >> choice;
209                     if (0 == choice)
210                         break;
211                 }
212             }
213
214             std::cout << "All the allowed values are tried!" << std::endl;
215         }
216
217         void displayResource()
218         {
219             int index = selectResource();
220             if (-1 == index)
221                 return;
222
223             SimulatorResourcePtr resource = m_resources[index - 1];
224             displayResource(resource);
225         }
226
227         void displayResource(SimulatorResourcePtr resource)
228         {
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;
234
235             // Attributes
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)
240             {
241                 std::cout << (attribute.second).getName() << " :  {" << std::endl;
242                 std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
243                 int min, max;
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;
249             }
250             std::cout << "#############################" << std::endl;
251         }
252
253         void automateResourceUpdate()
254         {
255             int index = selectResource();
256             if (-1 == index)
257                 return;
258
259             int id;
260             if (SIMULATOR_SUCCESS != m_resources[index - 1]->startUpdateAutomation(AutomationType::NORMAL, id))
261                 std::cout << "startUpdateAutomation() returned error!" << std::endl;
262             else
263                 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
264         }
265
266         void automateAttributeUpdate()
267         {
268             int index = selectResource();
269             if (-1 == index)
270                 return;
271
272             SimulatorResourcePtr resource = m_resources[index - 1];
273             SimulatorResourceModel resModel = resource->getModel();
274             std::map<std::string, SimulatorResourceModel::Attribute> attributes = resModel.getAttributes();
275             int size = 0;
276             for (auto & attribute : attributes)
277             {
278                 std::cout << ++size << ": " << attribute.first.c_str() << std::endl;
279             }
280
281             if (0 == size)
282             {
283                 std::cout << "This resource doest not contain any attributes!" << std::endl;
284                 return;
285             }
286
287             int choice = -1;
288             std::cout << "Select the attribute which you want to automate for updation: " << std::endl;
289             std::cin >> choice;
290             if (choice == -1 || choice > size)
291             {
292                 std::cout << "Invalid selection!" << std::endl;
293                 return;
294             }
295
296             int count = 0;
297             std::string attributeName;
298             for (auto & attribute : attributes)
299             {
300                 if (count == choice - 1)
301                 {
302                     attributeName = attribute.first;
303                     break;
304                 }
305
306                 count++;
307             }
308
309             std::cout << "Requesting attribute automation for " << attributeName.c_str() << std::endl;
310             int id;
311             if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(attributeName, AutomationType::NORMAL, id))
312                 std::cout << "startUpdateAutomation() returned error!" << std::endl;
313             else
314                 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
315         }
316
317         void stopAutomation()
318         {
319             int index = selectResource();
320             if (-1 == index)
321                 return;
322
323             SimulatorResourcePtr resource = m_resources[index - 1];
324             int automationid;
325             std::cout << "Enter automation id: " << std::endl;
326             std::cin >> automationid;
327             resource->stopUpdateAutomation(automationid);
328         }
329
330     private:
331         std::vector<SimulatorResourcePtr> m_resources;
332 };
333
334 void printMainMenu()
335 {
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;
341 }
342
343 int main(void)
344 {
345     SimLightResource lightResource;
346
347     printMainMenu();
348     bool cont = true;
349     while (cont == true)
350     {
351         int choice = -1;
352         std::cout << "Enter your choice: ";
353         std::cin >> choice;
354         if (choice < 0 || choice > 2)
355         {
356             std::cout << "Invaild choice !" << std::endl; continue;
357         }
358
359         switch (choice)
360         {
361             case 1: lightResource.startTest();
362                 std::cout << "Welcome back to main menu !" << std::endl;
363                 break;
364             case 2: printMainMenu(); break;
365             case 0: cont = false;
366         }
367     }
368
369     std::cout << "Terminating test !!!" << std::endl;
370 }