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 DEFALUT_SERVER = 1;
33 constexpr int CUSTOM_SERVER = 2;
34 constexpr int STOP = 3;
36 constexpr int INCREASE_TEMPERATURE = 1;
37 constexpr int DECREASE_TEMPERATURE = 2;
38 constexpr int STOP_SENSOR = 3;
40 constexpr int CORRECT_INPUT = 1;
41 constexpr int INCORRECT_INPUT = 2;
42 constexpr int QUIT = 3;
44 std::string resourceUri = "/a/TempSensor";
45 std::string resourceType = "core.TemperatureSensor";
46 std::string resourceInterface = "oic.if.";
47 std::string attributeKey = "Temperature";
49 RCSResourceObject::Ptr server;
58 std::cout << "====================================================================="
60 std::cout << " 1 - Creation of Resource [Auto control for requests]" << std::endl;
61 std::cout << " 2 - Creation of Resource [Developer control for Get and Set requests]"
63 std::cout << " 3 - Quit" << std::endl;
64 std::cout << "====================================================================="
68 void displayControlTemperatureMenu()
70 std::cout << "========================================================" << std::endl;
71 std::cout << "1. Increase Temperature by 10 degree" << std::endl;
72 std::cout << "2. Decrease Temperature by 10 degree" << std::endl;
73 std::cout << "3. Stop the Sensor" << std::endl;
74 std::cout << "========================================================" << std::endl;
77 void printAttribute(const RCSResourceAttributes& attrs)
79 for(const auto& attr : attrs)
81 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
82 << attr.value().toString() << std::endl;
86 //hander for get request (if developer choose second option for resource Creation)
87 RCSGetResponse requestHandlerForGet(const RCSRequest& request,
88 RCSResourceAttributes& attrs)
90 std::cout << "Recieved a Get request from Client" << std::endl;
92 RCSResourceObject::LockGuard lock(*server);
93 RCSResourceAttributes attributes = server->getAttributes();
95 std::cout << "\nSending response to Client : " << std::endl;
96 printAttribute(attributes);
98 return RCSGetResponse::defaultAction();
101 //hander for set request (if developer choose second option for resource Creation)
102 RCSSetResponse requestHandlerForSet(const RCSRequest& request,
103 RCSResourceAttributes& attrs)
105 std::cout << "Recieved a Set request from Client" << std::endl;
107 std::cout << "\n\nSending response to Client : " << std::endl;
108 RCSResourceObject::LockGuard lock(*server);
109 printAttribute(attrs);
110 return RCSSetResponse::defaultAction();
113 void createResource()
115 server = RCSResourceObject::Builder(resourceUri, resourceType,
116 resourceInterface).setDiscoverable(true).setObservable(true).build();
125 catch (const PlatformException& e)
127 std::cout << "Exception in initServer : " << e.what() << std::endl;
130 server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
131 server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
132 server->setAttribute(attributeKey, DEFALUT_VALUE);
135 void changeTemperature(Control control)
137 RCSResourceObject::LockGuard lock(server);
138 if(Control::INCREASE == control)
140 server->getAttributes()[attributeKey] =
141 server->getAttribute<int>(attributeKey) + 10;
142 std::cout << "\nTemperature increased by 10 degree" << std::endl;
144 else if(Control::DECREASE == control)
146 server->getAttributes()[attributeKey] =
147 server->getAttribute<int>(attributeKey) - 10;
148 std::cout << "\nTemperature Decreased by 10 degree" << std::endl;
150 std::cout << "\nCurrent Temperature : "
151 << server->getAttributeValue(attributeKey).get<int>() << std::endl;
154 int processUserInput()
157 std::cin >> userInput;
161 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
167 int selectServerMenu()
169 switch (processUserInput())
171 case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client.
173 return CORRECT_INPUT;
176 // Creation of Resource & setting get and set handler for handling get and
177 // set request from client in application.
180 server->setGetRequestHandler(requestHandlerForGet);
181 server->setSetRequestHandler(requestHandlerForSet);
182 return CORRECT_INPUT;
187 std::cout << "Invalid input, please try again" << std::endl;
188 return INCORRECT_INPUT;
192 int selectControlTemperatureMenu()
194 switch (processUserInput())
196 case INCREASE_TEMPERATURE:
197 changeTemperature(Control::INCREASE);
198 return CORRECT_INPUT;
200 case DECREASE_TEMPERATURE:
201 changeTemperature(Control::DECREASE);
202 return CORRECT_INPUT;
208 std::cout << "Invalid input. Please try again." << std::endl;
209 return INCORRECT_INPUT;
219 int ret = selectServerMenu();
221 if(ret == QUIT) return;
222 if(ret == CORRECT_INPUT) break;
227 displayControlTemperatureMenu();
229 if (selectControlTemperatureMenu() == QUIT) return;
242 catch (const std::exception& e)
244 std::cout << "main exception : " << e.what() << std::endl;
247 std::cout << "Stopping the Server" << std::endl;