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 ******************************************************************/
23 #include "RCSResourceObject.h"
25 using namespace OIC::Service;
27 constexpr int DEFAULT_SPEED = 30;
28 constexpr int UP_SPEED = 50;
29 constexpr int DOWN_SPEED = 10;
31 constexpr int DEFALUT_SERVER = 1;
32 constexpr int CUSTOM_SERVER = 2;
33 constexpr int STOP = 3;
35 constexpr int PRINT_ATTRIBUTES = 1;
36 constexpr int INCREASE_SPEED_ATTRIBUTE = 2;
37 constexpr int DECREASE_SPEED_ATTRIBUTE = 3;
38 constexpr int STOP_SENSOR = 4;
40 constexpr int CORRECT_INPUT = 1;
41 constexpr int INCORRECT_INPUT = 2;
42 constexpr int QUIT = 3;
44 const std::string resourceUri = "/a/airConditioner";
45 const std::string resourceType = "core.ac";
46 const std::string resourceInterface = "oic.if.baseline";
47 const std::string attributeKey = "deviceInfo";
49 RCSResourceObject::Ptr g_server;
53 std::cout << "====================================================================="
55 std::cout << " 1 - Creation of Resource [Auto control for requests]" << std::endl;
56 std::cout << " 2 - Creation of Resource [Developer control for Get and Set requests]"
58 std::cout << " 3 - Quit" << std::endl;
59 std::cout << "====================================================================="
63 void displayControlMenu()
65 std::cout << "========================================================" << std::endl;
66 std::cout << "1. Print Nested attributes" << std::endl;
67 std::cout << "2. Increase Speed attributes" << std::endl;
68 std::cout << "3. Decrease Speed attributes" << std::endl;
69 std::cout << "4. Stop the Sensor" << std::endl;
70 std::cout << "========================================================" << std::endl;
73 std::vector< std::vector< RCSResourceAttributes > > createNestedAttribute(int speedValue)
75 RCSResourceAttributes model;
76 RCSResourceAttributes weight;
77 RCSResourceAttributes dimensions;
79 model["model"] = "SamsungAC";
81 dimensions["dimensions"] = "10x25x35";
83 RCSResourceAttributes speed;
84 RCSResourceAttributes airCirculation;
86 speed["speed"] = speedValue;
87 airCirculation["air"] = 425;
89 RCSResourceAttributes temperature;
90 RCSResourceAttributes humidity;
92 temperature["temp"] = 30;
93 humidity["humidity"] = 30;
95 RCSResourceAttributes power;
96 RCSResourceAttributes capacity;
98 power["power"] = 1600;
99 capacity["capacity"] = 1;
101 RCSResourceAttributes red;
102 RCSResourceAttributes green;
107 std::vector< RCSResourceAttributes > generalInfo{ model, weight, dimensions };
108 std::vector< RCSResourceAttributes > fan{ speed, airCirculation };
109 std::vector< RCSResourceAttributes > tempSensor{ temperature, humidity };
110 std::vector< RCSResourceAttributes > efficiency{ power, capacity };
111 std::vector< RCSResourceAttributes > light{ red, green };
113 std::vector< std::vector< RCSResourceAttributes > > acServer;
115 acServer.push_back(generalInfo);
116 acServer.push_back(fan);
117 acServer.push_back(tempSensor);
118 acServer.push_back(efficiency);
119 acServer.push_back(light);
124 void printAttribute(const RCSResourceAttributes &attrs)
126 for (const auto & attr : attrs)
128 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
129 << attr.value().toString() << std::endl;
130 std::cout << "=============================================\n" << std::endl;
132 const auto& doubleVector = attr.value().
133 get< std::vector< std::vector< RCSResourceAttributes > > >();
135 for (const auto& vector : doubleVector)
137 for (const auto& attrs : vector)
139 for (const auto & kvPair : attrs)
141 std::cout << "\t" << kvPair.key() << " : " <<
142 kvPair.value().toString() << std::endl;
145 std::cout << std::endl;
147 std::cout << "=============================================\n" << std::endl;
151 void printNestedAttribute()
153 RCSResourceObject::LockGuard lock(*g_server);
154 const auto& attributes = g_server->getAttributes();
156 std::cout << "\nPrinting nested attributes" << std::endl;
157 printAttribute(attributes);
161 void changeSpeedAttribute(int state)
163 std::vector< std::vector< RCSResourceAttributes > > attr;
165 if (INCREASE_SPEED_ATTRIBUTE == state)
167 std::cout << "Increasing speed attribute to : " << UP_SPEED << std::endl;
168 attr = createNestedAttribute(UP_SPEED);
170 else if (DECREASE_SPEED_ATTRIBUTE == state)
172 std::cout << "Decreasing speed attribute to : " << DOWN_SPEED << std::endl;
173 attr = createNestedAttribute(DOWN_SPEED);
176 RCSResourceObject::LockGuard lock(*g_server);
177 g_server->getAttributes()[attributeKey] = attr;
178 printNestedAttribute();
181 //hander for get request (if developer choose second option for resource Creation)
182 RCSGetResponse requestHandlerForGet(const RCSRequest& /*request*/,
183 RCSResourceAttributes& /*attrs*/)
185 std::cout << "Recieved a Get request from Client" << std::endl;
187 RCSResourceObject::LockGuard lock(*g_server);
188 std::cout << "Sending response to Client : " << std::endl;
189 printAttribute(g_server->getAttributes());
191 return RCSGetResponse::defaultAction();
194 //hander for set request (if developer choose second option for resource Creation)
195 RCSSetResponse requestHandlerForSet(const RCSRequest& /*request*/,
196 RCSResourceAttributes &attrs)
198 std::cout << "Recieved a Set request from Client" << std::endl;
200 std::cout << "Requested attributes : " << std::endl;
201 printAttribute(attrs);
202 return RCSSetResponse::defaultAction();
209 g_server = RCSResourceObject::Builder(resourceUri, resourceType,
210 resourceInterface).build();
212 catch (const RCSPlatformException &e)
214 std::cout << "Exception in initServer : " << e.what() << std::endl;
218 g_server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
219 g_server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
221 g_server->setAttribute(attributeKey, createNestedAttribute(DEFAULT_SPEED));
224 int processUserInput()
227 std::cin >> userInput;
231 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
237 int selectControlMenu()
239 switch (processUserInput())
241 case PRINT_ATTRIBUTES:
242 printNestedAttribute();
243 return CORRECT_INPUT;
245 case INCREASE_SPEED_ATTRIBUTE:
246 changeSpeedAttribute(INCREASE_SPEED_ATTRIBUTE);
247 return CORRECT_INPUT;
249 case DECREASE_SPEED_ATTRIBUTE:
250 changeSpeedAttribute(DECREASE_SPEED_ATTRIBUTE);
251 return CORRECT_INPUT;
257 std::cout << "Invalid input. Please try again." << std::endl;
258 return INCORRECT_INPUT;
262 int selectServerMenu()
264 switch (processUserInput())
267 // Creation of Resource & Auto control for all requests from Client.
269 return CORRECT_INPUT;
272 // Creation of Resource & setting get and set handler for handling get and
273 // set request from client in application.
276 g_server->setGetRequestHandler(requestHandlerForGet);
277 g_server->setSetRequestHandler(requestHandlerForSet);
278 return CORRECT_INPUT;
283 std::cout << "Invalid input, please try again" << std::endl;
284 return INCORRECT_INPUT;
294 int ret = selectServerMenu();
296 if (ret == QUIT) return;
297 if (ret == CORRECT_INPUT) break;
302 displayControlMenu();
304 if (selectControlMenu() == QUIT) return;
314 catch (const std::exception &e)
316 std::cout << "main exception : " << e.what() << std::endl;
319 std::cout << "Stopping the server" << std::endl;