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 "RCSResourceObject.h"
22 #include "OCPlatform.h"
25 #define nestedAtrribute std::vector<std::vector<RCSResourceAttributes>>
28 using namespace OC::OCPlatform;
29 using namespace OIC::Service;
31 constexpr int DEFAULT_SPEED = 30;
32 constexpr int UP_SPEED = 50;
33 constexpr int DOWN_SPEED = 10;
35 constexpr int DEFALUT_SERVER = 1;
36 constexpr int CUSTOM_SERVER = 2;
37 constexpr int STOP = 3;
39 constexpr int PRINT_ATTRIBUTES = 1;
40 constexpr int INCREASE_SPEEDATTRIBUTE = 2;
41 constexpr int DECREASE_SPEEDATTRIBUTE = 3;
42 constexpr int STOP_SENSOR = 4;
44 constexpr int CORRECT_INPUT = 1;
45 constexpr int INCORRECT_INPUT = 2;
46 constexpr int QUIT = 3;
48 std::string resourceUri = "/a/airConditioner";
49 std::string resourceType = "core.ac";
50 std::string resourceInterface = "oic.if.";
51 std::string attributeKey = "deviceInfo";
53 RCSResourceAttributes model;
54 RCSResourceAttributes speed;
55 RCSResourceAttributes airCirculation;
56 RCSResourceAttributes temperature;
57 RCSResourceAttributes humidity;
58 RCSResourceAttributes power;
59 RCSResourceAttributes capacity;
60 RCSResourceAttributes weight;
61 RCSResourceAttributes dimensions;
62 RCSResourceAttributes red;
63 RCSResourceAttributes green;
65 std::vector<RCSResourceAttributes> generalInfo;
66 std::vector<RCSResourceAttributes> fan;
67 std::vector<RCSResourceAttributes> tempSensor;
68 std::vector<RCSResourceAttributes> efficiency;
69 std::vector<RCSResourceAttributes> light;
71 RCSResourceObject::Ptr server;
75 std::cout << "====================================================================="
77 std::cout << " 1 - Creation of Resource [Auto control for requests]" << std::endl;
78 std::cout << " 2 - Creation of Resource [Developer control for Get and Set requests]"
80 std::cout << " 3 - Quit" << std::endl;
81 std::cout << "====================================================================="
85 void displayControlMenu()
87 std::cout << "========================================================" << std::endl;
88 std::cout << "1. Print Nested attributes" << std::endl;
89 std::cout << "2. Increase Speed attributes" << std::endl;
90 std::cout << "3. Decrease Speed attributes" << std::endl;
91 std::cout << "4. Stop the Sensor" << std::endl;
92 std::cout << "========================================================" << std::endl;
95 nestedAtrribute createNestedAttribute(int speedValue)
97 nestedAtrribute *acServer = new nestedAtrribute();
99 model["model"] = "SamsungAC";
101 speed["speed"] = speedValue;
102 airCirculation["air"] = 425;
104 temperature["temp"] = 30;
105 humidity["humidity"] = 30;
107 power["power"] = 1600;
108 capacity["capacity"] = 1;
110 weight["weight"] = 3;
111 dimensions["dimensions"] = "10x25x35";
117 generalInfo.push_back(model);
118 generalInfo.push_back(weight);
119 generalInfo.push_back(dimensions);
122 fan.push_back(speed);
123 fan.push_back(airCirculation);
126 tempSensor.push_back(temperature);
127 tempSensor.push_back(humidity);
130 efficiency.push_back(power);
131 efficiency.push_back(capacity);
134 light.push_back(red);
135 light.push_back(green);
137 if (nullptr == acServer)
139 std::cout << "Null nestedAtrribute" << std::endl;
143 acServer->push_back(generalInfo);
144 acServer->push_back(fan);
145 acServer->push_back(tempSensor);
146 acServer->push_back(efficiency);
147 acServer->push_back(light);
153 void printAttribute(const RCSResourceAttributes &attrs)
155 for (const auto & attr : attrs)
157 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
158 << attr.value().toString() << std::endl;
159 std::cout << "=============================================\n" << std::endl;
161 OIC::Service::RCSResourceAttributes::Value attrValue = attr.value();
162 std::vector< std::vector<RCSResourceAttributes >> attrVector =
163 attrValue.get<std::vector< std::vector<RCSResourceAttributes >>>();
165 for (auto itr = attrVector.begin(); itr != attrVector.end(); ++itr)
167 std::vector<RCSResourceAttributes > attrKeyVector = *itr;
168 for (auto itrKey = attrKeyVector.begin(); itrKey != attrKeyVector.end(); ++itrKey)
170 for (const auto & attribute : *itrKey)
172 std::cout << "\t" << attribute.key() << " : " << attribute.value().toString() << std::endl;
175 std::cout << std::endl;
177 std::cout << "=============================================\n" << std::endl;
181 void printNestedAttribute()
183 RCSResourceObject::LockGuard lock(*server);
184 RCSResourceAttributes attributes = server->getAttributes();
186 std::cout << "\nPrinting nested attributes" << std::endl;
187 printAttribute(attributes);
191 void changeSpeedAttribute(int state)
193 nestedAtrribute attr;
195 if (INCREASE_SPEEDATTRIBUTE == state)
197 std::cout << "Increasing speed attribute to : " << UP_SPEED << std::endl;
198 attr = createNestedAttribute(UP_SPEED);
200 else if (DECREASE_SPEEDATTRIBUTE == state)
202 std::cout << "Decreasing speed attribute to : " << DOWN_SPEED << std::endl;
203 attr = createNestedAttribute(DOWN_SPEED);
206 RCSResourceObject::LockGuard lock(*server);
207 server->getAttributes()[attributeKey] = attr;
208 printNestedAttribute();
211 //hander for get request (if developer choose second option for resource Creation)
212 RCSGetResponse requestHandlerForGet(const RCSRequest& /*request*/,
213 RCSResourceAttributes& /*attrs*/)
215 std::cout << "Recieved a Get request from Client" << std::endl;
217 RCSResourceObject::LockGuard lock(*server);
218 RCSResourceAttributes attributes = server->getAttributes();
220 std::cout << "\nSending response to Client : " << std::endl;
221 printAttribute(attributes);
223 return RCSGetResponse::defaultAction();
226 //hander for set request (if developer choose second option for resource Creation)
227 RCSSetResponse requestHandlerForSet(const RCSRequest& /*request*/,
228 RCSResourceAttributes &attrs)
230 std::cout << "Recieved a Set request from Client" << std::endl;
232 std::cout << "\n\nSending response to Client : " << std::endl;
233 RCSResourceObject::LockGuard lock(*server);
234 printAttribute(attrs);
235 return RCSSetResponse::defaultAction();
238 void createResource()
240 server = RCSResourceObject::Builder(resourceUri, resourceType,
241 resourceInterface).setDiscoverable(true).setObservable(true).build();
250 catch (const RCSPlatformException &e)
252 std::cout << "Exception in initServer : " << e.what() << std::endl;
255 if (nullptr == server)
257 std::cout << "Null server resource" << std::endl;
261 server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
262 server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
264 nestedAtrribute attr = createNestedAttribute(DEFAULT_SPEED);
265 server->setAttribute(attributeKey, attr);
268 int processUserInput()
271 std::cin >> userInput;
275 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
281 int selectControlMenu()
283 switch (processUserInput())
285 case PRINT_ATTRIBUTES:
286 printNestedAttribute();
287 return CORRECT_INPUT;
289 case INCREASE_SPEEDATTRIBUTE:
290 changeSpeedAttribute(INCREASE_SPEEDATTRIBUTE);
291 return CORRECT_INPUT;
293 case DECREASE_SPEEDATTRIBUTE:
294 changeSpeedAttribute(DECREASE_SPEEDATTRIBUTE);
295 return CORRECT_INPUT;
301 std::cout << "Invalid input. Please try again." << std::endl;
302 return INCORRECT_INPUT;
306 int selectServerMenu()
308 switch (processUserInput())
310 case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client.
312 return CORRECT_INPUT;
315 // Creation of Resource & setting get and set handler for handling get and
316 // set request from client in application.
319 server->setGetRequestHandler(requestHandlerForGet);
320 server->setSetRequestHandler(requestHandlerForSet);
321 return CORRECT_INPUT;
326 std::cout << "Invalid input, please try again" << std::endl;
327 return INCORRECT_INPUT;
337 int ret = selectServerMenu();
339 if (ret == QUIT) return;
340 if (ret == CORRECT_INPUT) break;
345 displayControlMenu();
347 if (selectControlMenu() == QUIT) return;
360 catch (const std::exception &e)
362 std::cout << "main exception : " << e.what() << std::endl;
365 std::cout << "Stopping the Server" << std::endl;