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 "PrimitiveResource.h"
22 #include "RCSResourceObject.h"
23 #include "OCPlatform.h"
27 using namespace OC::OCPlatform;
28 using namespace OIC::Service;
30 constexpr int DEFALUT_VALUE = 0;
32 constexpr int REQUEST_TEMP = 1;
33 constexpr int REQUEST_LIGHT = 2;
35 constexpr int PRESENCE_ON = 1;
36 constexpr int PRESENCE_OFF = 2;
38 constexpr int DEFALUT_SERVER = 1;
39 constexpr int CUSTOM_SERVER = 2;
40 constexpr int STOP = 3;
42 constexpr int INCREASE_TEMPERATURE = 1;
43 constexpr int DECREASE_TEMPERATURE = 2;
44 constexpr int STOP_TEMPERATURE_SENSOR = 3;
45 constexpr int STOP_LIGHT_SENSOR = 1;
47 constexpr int CORRECT_INPUT = 1;
48 constexpr int INCORRECT_INPUT = 2;
49 constexpr int QUIT = 3;
52 std::string resourceUri = "/a/TempSensor";
53 std::string resourceType = "core.TemperatureSensor";
54 std::string resourceInterface = "oic.if.";
55 std::string attributeKey = "Temperature";
56 int isPresenceOn = PRESENCE_ON;
58 RCSResourceObject::Ptr server;
60 int processUserInput();
69 std::cout << "====================================================================="
71 std::cout << " 1 - Creation of Resource [Auto control for requests]" << std::endl;
72 std::cout << " 2 - Creation of Resource [Developer control for Get and Set requests]"
74 std::cout << " 3 - Quit" << std::endl;
75 std::cout << "====================================================================="
79 void displayControlTemperatureMenu()
81 std::cout << "========================================================" << std::endl;
82 std::cout << "1. Increase Temperature by 10 degree" << std::endl;
83 std::cout << "2. Decrease Temperature by 10 degree" << std::endl;
84 std::cout << "3. Stop the Sensor" << std::endl;
85 std::cout << "========================================================" << std::endl;
88 void displayControlLightMenu()
90 std::cout << "========================================================" << std::endl;
91 std::cout << "1. Stop the Sensor" << std::endl;
92 std::cout << "========================================================" << std::endl;
95 void printAttribute(const RCSResourceAttributes& attrs)
97 for(const auto& attr : attrs)
99 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
100 << attr.value().toString() << std::endl;
104 //hander for get request (if developer choose second option for resource Creation)
105 RCSGetResponse requestHandlerForGet(const RCSRequest& request,
106 RCSResourceAttributes& attrs)
108 std::cout << "Recieved a Get request from Client" << std::endl;
109 RCSResourceObject::LockGuard lock(*server);
110 RCSResourceAttributes attributes = server->getAttributes();
112 std::cout << "\nSending response to Client : " << std::endl;
113 printAttribute(attributes);
115 return RCSGetResponse::defaultAction();
118 //hander for set request (if developer choose second option for resource Creation)
119 RCSSetResponse requestHandlerForSet(const RCSRequest& request,
120 RCSResourceAttributes& attrs)
122 std::cout << "Recieved a Set request from Client" << std::endl;
124 std::cout << "\n\nSending response to Client : " << std::endl;
125 RCSResourceObject::LockGuard lock(*server);
126 printAttribute(attrs);
127 return RCSSetResponse::defaultAction();
130 void createResource()
132 server = RCSResourceObject::Builder(resourceUri, resourceType,resourceInterface).
133 setDiscoverable(true).setObservable(true).build();
138 std::cout << "========================================================" << std::endl;
139 std::cout << "Select Resource Type" << std::endl;
140 std::cout << "1. Temperature" << std::endl;
141 std::cout << "2. Light" << std::endl;
142 std::cout << "========================================================" << std::endl;
144 switch(processUserInput())
147 resourceUri = "/a/TempSensor";
148 resourceType = "core.TemperatureSensor";
151 resourceUri = "/a/light";
152 resourceType = "core.light";
155 std::cout << "Invalid input, please try again" << std::endl;
163 catch (const PlatformException& e)
165 std::cout << "Exception in initServer : " << e.what() << std::endl;
168 server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
169 server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
170 server->setAttribute(attributeKey, DEFALUT_VALUE);
173 void changeTemperature(Control control)
175 RCSResourceObject::LockGuard lock(server);
176 if(Control::INCREASE == control)
178 server->getAttributes()[attributeKey] =
179 server->getAttribute<int>(attributeKey) + 10;
180 std::cout << "\nTemperature increased by 10 degree" << std::endl;
182 else if(Control::DECREASE == control)
184 server->getAttributes()[attributeKey] =
185 server->getAttribute<int>(attributeKey) - 10;
186 std::cout << "\nTemperature Decreased by 10 degree" << std::endl;
188 std::cout << "\nCurrent Temperature : "
189 << server->getAttributeValue(attributeKey).get<int>() << std::endl;
192 int processUserInput()
195 std::cin >> userInput;
199 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
205 int selectPresenceMenu()
207 std::cout << "========================================================" << std::endl;
208 std::cout << "1. Presence On" << std::endl;
209 std::cout << "2. Presence Off" << std::endl;
210 std::cout << "========================================================" << std::endl;
212 switch(processUserInput())
215 isPresenceOn = PRESENCE_ON;
217 return CORRECT_INPUT;
219 isPresenceOn = PRESENCE_OFF;
220 return CORRECT_INPUT;
222 std::cout << "Invalid input, please try again" << std::endl;
223 return INCORRECT_INPUT;
226 int selectServerMenu()
228 switch (processUserInput())
230 case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client.
232 return CORRECT_INPUT;
235 // Creation of Resource & setting get and set handler for handling get and
236 // set request from client in application.
239 server->setGetRequestHandler(requestHandlerForGet);
240 server->setSetRequestHandler(requestHandlerForSet);
241 return CORRECT_INPUT;
246 std::cout << "Invalid input, please try again" << std::endl;
247 return INCORRECT_INPUT;
251 int selectControlTemperatureMenu()
253 switch (processUserInput())
255 case INCREASE_TEMPERATURE:
256 changeTemperature(Control::INCREASE);
257 return CORRECT_INPUT;
259 case DECREASE_TEMPERATURE:
260 changeTemperature(Control::DECREASE);
261 return CORRECT_INPUT;
263 case STOP_TEMPERATURE_SENSOR:
267 std::cout << "Invalid input. Please try again." << std::endl;
268 return INCORRECT_INPUT;
272 int selectControlLightMenu()
274 switch (processUserInput())
276 case STOP_LIGHT_SENSOR:
280 std::cout << "Invalid input. Please try again." << std::endl;
281 return INCORRECT_INPUT;
289 int ret = selectPresenceMenu();
290 if(ret == CORRECT_INPUT) break;
296 int ret = selectServerMenu();
298 if(ret == QUIT) return;
299 if(ret == CORRECT_INPUT) break;
304 if(resourceType == "core.TemperatureSensor")
306 displayControlTemperatureMenu();
307 if (selectControlTemperatureMenu() == QUIT) return;
309 else if(resourceType == "core.light")
311 displayControlLightMenu();
312 if (selectControlLightMenu() == QUIT) return;
324 catch (const std::exception& e)
326 std::cout << "main exception : " << e.what() << std::endl;
329 if(isPresenceOn == PRESENCE_ON)
333 std::cout << "Stopping the Server" << std::endl;