7da312c267be5aca4fe370c58a9fb9739fbfedcf
[platform/upstream/iotivity.git] / service / resource-manipulation / examples / linux / SampleResourceServer.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 "PrimitiveResource.h"
22 #include "ResourceObject.h"
23 #include "OCPlatform.h"
24 #include "OCApi.h"
25
26 using namespace std;
27 using namespace OC;
28 using namespace OC::OCPlatform;
29 using namespace OIC::Service;
30
31 std::string resourceUri = "/a/TempHumSensor";
32 std::string resourceType = "core.TemperatureSensor";
33 std::string resourceInterface = "oic.if.";
34 std::string attributeKey = "Temperature";
35 ResourceObject::Ptr server;
36
37 //display the menu on the screen
38 void displayMenu()
39 {
40     std::cout << "=====================================================================\n\n";
41     std::cout << "   1 - Creation of Resource [Auto control for requests]" << std::endl;
42     std::cout << "   2 - Creation of Resource [Developer control for Get and Set requests]" <<std::endl;
43     std::cout << "   3 - Quit" << std::endl;
44     std::cout << "=====================================================================\n\n";
45 }
46
47 //hander for get request (if developer choose second option for resource Creation)
48 PrimitiveGetResponse RequestHandlerForGet(const PrimitiveRequest &request,
49         ResourceAttributes &attrs)
50 {
51     cout << "\nRecieved a Get request from Client" << std::endl;
52     ResourceObject::LockGuard lock(*server);
53     ResourceAttributes attr = server->getAttributes();
54     ResourceAttributes::const_iterator iter = attr.begin();
55     for (unsigned int i = 0; i < attr.size(); ++i)
56     {
57         std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
58         ++iter;
59     }
60     return PrimitiveGetResponse::create(attr);
61 }
62
63 //hander for set request (if developer choose second option for resource Creation)
64 PrimitiveSetResponse RequestHandlerForSet(const PrimitiveRequest &request,
65         ResourceAttributes &attrs)
66 {
67     cout << "\nRecieved a Set request from Client" << std::endl;
68     ResourceAttributes::const_iterator iter = attrs.begin();
69     for (unsigned int i = 0; i < attrs.size(); ++i)
70     {
71         std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
72         ++iter;
73     }
74     return PrimitiveSetResponse::create(attrs);
75 }
76
77 int main(void)
78 {
79
80     int userInput;
81     int initialTemperature = 0;
82     int temperatureInput;
83     bool flag = true;
84     startPresence(3);
85
86     displayMenu();
87
88     //userInput for creation of Resource
89     std::cin >> userInput;
90     std::cin.clear();
91     std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
92     try
93     {
94         while (flag)
95         {
96
97             switch (userInput)
98             {
99
100                 case 1:
101                //Creation of Resource & Auto control for all requests from Client
102                     {
103                         //creation of Resource
104                         server = ResourceObject::Builder(resourceUri, resourceType,
105                                                          resourceInterface).setDiscoverable(true).setObservable(false).build();
106                         std::cout << "Resource created successfully " << std::endl;
107
108                         //setting attribute for the Created Resource
109                         server->setAttribute(attributeKey, initialTemperature);
110                         flag = false;
111                         break;
112                     }
113
114                 case 2:
115             //Creation of Resource & setting get and set handler for handling get and set request from client in application
116                     {
117                         server = ResourceObject::Builder(resourceUri, resourceType,
118                                                          resourceInterface).setDiscoverable(true).setObservable(false).build();
119                         std::cout << "Resource created successfully " << std::endl;
120
121                         //setting attribute for the Created Resource
122                         server->setAttribute(attributeKey, initialTemperature);
123
124                         //setting handler for handling get request from the client
125                         server->setGetRequestHandler(RequestHandlerForGet);
126
127                         //  setting handler for handling set request from the client
128                         server->setSetRequestHandler(RequestHandlerForSet);
129                         flag = false;
130                         break;
131                     }
132                 case 3:
133                     return 0;
134                 default :
135                     std::cout << "Invalid Input" << std::endl;
136                     break;
137             }
138         }
139
140         while (true)
141         {
142             bool end = false;
143             cout << endl;
144             cout << "========================================================" << endl;
145             cout << "1. Increase Temperature by 10 degree" << endl;
146             cout << "2. Decrease Temperature by 10 degree" << endl;
147             cout << "3. Stop the Sensor" << endl;
148             cout << "========================================================" << endl;
149
150             //user Input for increasing/decreasing the temperature
151             cin >> temperatureInput;
152             if (std::cin.fail())
153             {
154                 std::cin.clear();
155                 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
156                 std::cout << "Invalid input type, please try again" << std::endl;
157                 continue;
158             }
159
160             switch (temperatureInput)
161             {
162                 case 1:
163                     {
164                         ResourceObject::LockGuard lock(*server);
165                         ResourceAttributes attrs = server->getAttributes();
166
167                         attrs[attributeKey] =  (server->getAttribute<int>(attributeKey)  + 10);
168                         server->setAttribute(attributeKey, attrs[attributeKey]);
169                         cout << "\nTemperature increased by 10 degree" << endl;
170
171                         //getting the current attribute and priniting it on the console
172                         attrs = server->getAttributes();
173                         cout << "\nCurrent Temperature : ";
174                         ResourceAttributes::const_iterator iter = attrs.begin();
175                         for (unsigned int i = 0; i < attrs.size(); ++i)
176                         {
177                             std::cout << iter->value().toString() << std::endl;
178                             ++iter;
179                         }
180                         break;
181                     }
182                 case 2:
183                     {
184                         ResourceObject::LockGuard lock(*server);
185                         ResourceAttributes attrs = server->getAttributes();
186                         attrs[attributeKey] =  (server->getAttribute<int>(attributeKey)  - 10);
187                         server->setAttribute(attributeKey, attrs[attributeKey]);
188                         cout << "\nTemperature decreased by 10 degree" << endl;
189
190                         //getting the current attribute and priniting it on the console
191                         attrs = server->getAttributes();
192                         cout << "\nCurrent Temperature : ";
193                         ResourceAttributes::const_iterator iter = attrs.begin();
194                         for (unsigned int i = 0; i < attrs.size(); ++i)
195                         {
196                             std::cout << iter->value().toString() << std::endl;
197                             ++iter;
198                         }
199                         break;
200                     }
201                 case 3:
202                     {
203                         cout << "Stopping the Resource" << endl;
204                         end = true;
205                         break;
206                     }
207                 default:
208                     {
209                         cout << "Invalid input. Please try again." << endl;
210                         break;
211                     }
212             }
213             if (end == true)
214             {
215                 break;
216             }
217         }
218     }
219     catch (exception &e)
220     {
221         cout << "main exception  : " << e.what() << endl;
222     }
223 }
224
225