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"
24 using namespace OC::OCPlatform;
25 using namespace OIC::Service;
29 constexpr int RESOURCE_TEMP = 1;
30 constexpr int RESOURCE_LIGHT = 2;
32 constexpr int DEFAULT_SERVER = 1;
33 constexpr int CUSTOM_SERVER = 2;
35 constexpr int INCREASE = 1;
36 constexpr int DECREASE = 2;
38 const std::string BASELINE_INTERFACE = "oic.if.baseline";
39 const std::string ACTUATOR_INTERFACE = "oic.if.a";
40 const std::string SENSOR_INTERFACE = "oic.if.s";
41 const std::string CUSTOM_INTERFACE = "test.custom";
43 typedef void (*DisplayControlMenuFunc)();
44 typedef std::function<void()> Run;
48 bool g_isPresenceStarted = false;
50 RCSResourceObject::Ptr g_resource;
52 int processUserInput(int min, int max)
66 if (min <= input && input <= max)
73 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
75 throw std::runtime_error("Invalid Input, please try again");
78 void displayControlTemperatureMenu()
80 std::cout << "========================================================\n";
81 std::cout << INCREASE << ". Increase Temperature by 1 degree \n";
82 std::cout << DECREASE << ". Decrease Temperature by 1 degree \n";
83 std::cout << DECREASE + 1 << ". Quit \n";
84 std::cout << "========================================================\n";
87 void displayControlLightMenu()
89 std::cout << "========================================================\n";
90 std::cout << INCREASE << ". Increase Brightness by 1 stage \n";
91 std::cout << DECREASE << ". Decrease Brightness by 1 stage \n";
92 std::cout << DECREASE + 1 << ". Quit \n";
93 std::cout << "========================================================\n";
96 void printAttributes(const RCSResourceAttributes& attrs)
98 for (const auto& attr : attrs)
100 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
101 << attr.value().toString() << std::endl;
105 RCSGetResponse requestHandlerForGet(const RCSRequest&, RCSResourceAttributes& attrs)
107 std::cout << "Received a Get request from Client" << std::endl;
108 printAttributes(attrs);
111 RCSResourceObject::LockGuard lock(g_resource);
112 std::cout << "\nSending response to Client : " << std::endl;
113 printAttributes(g_resource->getAttributes());
116 return RCSGetResponse::defaultAction();
119 RCSSetResponse requestHandlerForSet(const RCSRequest&, RCSResourceAttributes& attrs)
121 std::cout << "Received a Set request from Client" << std::endl;
122 printAttributes(attrs);
124 return RCSSetResponse::defaultAction();
127 void initServer(const std::string& resourceUri, const std::string& resourceType,
128 const std::string& attrKey)
130 g_resource = RCSResourceObject::Builder(resourceUri, resourceType, ACTUATOR_INTERFACE)
131 .addInterface(CUSTOM_INTERFACE)
132 .addInterface(SENSOR_INTERFACE)
133 .setDefaultInterface(BASELINE_INTERFACE)
134 .setDiscoverable(true)
138 g_resource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
139 g_resource->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
140 g_resource->setAttribute(attrKey, 0);
143 void updateAttribute(const std::string& attrKey, int control)
145 const int diff = control == INCREASE ? 1 : - 1;
148 RCSResourceObject::LockGuard lock(g_resource);
149 auto& attrs = g_resource->getAttributes();
150 attrs[attrKey] = attrs[attrKey].get<int>() + diff;
153 if (control == INCREASE)
155 std::cout << attrKey << " increased." << std::endl;
159 std::cout << attrKey << " decreased." << std::endl;
161 std::cout << "\nCurrent " << attrKey << ": "
162 << g_resource->getAttributeValue(attrKey).get<int>() << std::endl;
165 void runResourceControl(DisplayControlMenuFunc displayMenuFunc, const std::string& attrKey)
168 updateAttribute(attrKey, processUserInput(INCREASE, DECREASE));
171 void runResourceTypeSelection(int resourceMode)
173 std::cout << "========================================================\n";
174 std::cout << "Select Resource Type \n";
175 std::cout << RESOURCE_TEMP << ". Temperature \n";
176 std::cout << RESOURCE_LIGHT << ". Light \n";
177 std::cout << RESOURCE_LIGHT + 1 << ". Quit \n";
178 std::cout << "========================================================\n";
180 int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT);
181 DisplayControlMenuFunc displayMenuFunc;
184 switch (resourceType)
187 attrKey = "Temperature";
188 initServer("/a/TempSensor", "oic.r.temperaturesensor", attrKey);
190 displayMenuFunc = displayControlTemperatureMenu;
194 attrKey = "Brightness";
195 initServer("/a/light", "oic.r.light", attrKey);
197 displayMenuFunc = displayControlLightMenu;
201 if (resourceMode == CUSTOM_SERVER)
203 g_resource->setGetRequestHandler(requestHandlerForGet);
204 g_resource->setSetRequestHandler(requestHandlerForSet);
207 g_currentRun = std::bind(runResourceControl, displayMenuFunc, std::move(attrKey));
210 void runResourceModeSelection()
212 std::cout << "======================================================== \n";
213 std::cout << DEFAULT_SERVER << ". Creation of Simple Resource Without Handlers \n";
214 std::cout << CUSTOM_SERVER << ". Creation of Resource With Set and Get Handlers \n";
215 std::cout << CUSTOM_SERVER + 1 << ". Quit \n";
216 std::cout << "======================================================== \n";
218 g_currentRun = std::bind(runResourceTypeSelection,
219 processUserInput(DEFAULT_SERVER, CUSTOM_SERVER));
222 void runPresenceSelection()
224 constexpr int PRESENCE_ON = 1;
225 constexpr int PRESENCE_OFF = 2;
227 std::cout << "========================================================\n";
228 std::cout << PRESENCE_ON << ". Presence On \n";
229 std::cout << PRESENCE_OFF << ". Presence Off \n";
230 std::cout << PRESENCE_OFF + 1 << ". Quit \n";
231 std::cout << "========================================================\n";
233 if (processUserInput(PRESENCE_ON, PRESENCE_OFF) == PRESENCE_ON)
235 g_isPresenceStarted = true;
239 g_currentRun = runResourceModeSelection;
244 g_currentRun = runPresenceSelection;
252 catch (const std::exception& e)
254 std::cout << e.what() << std::endl;
256 catch (const CloseApp&)
261 std::cout << "Stopping the server" << std::endl;
265 if (g_isPresenceStarted)