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 "RCSRequest.h"
23 #include "OCPlatform.h"
25 using namespace OC::OCPlatform;
26 using namespace OIC::Service;
30 constexpr int RESOURCE_TEMP = 1;
31 constexpr int RESOURCE_LIGHT = 2;
33 constexpr int DEFAULT_SERVER = 1;
34 constexpr int CUSTOM_SERVER = 2;
36 constexpr int INCREASE = 1;
37 constexpr int DECREASE = 2;
39 const std::string BASELINE_INTERFACE = "oic.if.baseline";
40 const std::string ACTUATOR_INTERFACE = "oic.if.a";
41 const std::string SENSOR_INTERFACE = "oic.if.s";
42 const std::string CUSTOM_INTERFACE = "test.custom";
44 typedef void (*DisplayControlMenuFunc)();
45 typedef std::function<void()> Run;
49 bool g_isPresenceStarted = false;
51 RCSResourceObject::Ptr g_resource;
53 int processUserInput(int min, int max)
67 if (min <= input && input <= max)
74 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
76 throw std::runtime_error("Invalid Input, please try again");
79 void displayControlTemperatureMenu()
81 std::cout << "========================================================\n";
82 std::cout << INCREASE << ". Increase Temperature by 1 degree \n";
83 std::cout << DECREASE << ". Decrease Temperature by 1 degree \n";
84 std::cout << DECREASE + 1 << ". Quit \n";
85 std::cout << "========================================================\n";
88 void displayControlLightMenu()
90 std::cout << "========================================================\n";
91 std::cout << INCREASE << ". Increase Brightness by 1 stage \n";
92 std::cout << DECREASE << ". Decrease Brightness by 1 stage \n";
93 std::cout << DECREASE + 1 << ". Quit \n";
94 std::cout << "========================================================\n";
97 void printAttributes(const RCSResourceAttributes& attrs)
99 for (const auto& attr : attrs)
101 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
102 << attr.value().toString() << std::endl;
106 RCSGetResponse requestHandlerForGet(const RCSRequest & req, RCSResourceAttributes& attrs)
108 std::cout << "Received a Get request from Client" << std::endl;
109 printAttributes(attrs);
112 RCSResourceObject::LockGuard lock(g_resource);
113 std::cout << "\nSending response to Client : " << std::endl;
114 if (req.getInterface() == CUSTOM_INTERFACE)
116 auto attr = g_resource->getAttributes();
117 static RCSByteString::DataType binval {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
118 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
119 attr["blob"] = RCSByteString {binval};
120 printAttributes(attr);
121 return RCSGetResponse::create(attr);
125 printAttributes(g_resource->getAttributes());
126 return RCSGetResponse::defaultAction();
131 RCSSetResponse requestHandlerForSet(const RCSRequest&, RCSResourceAttributes& attrs)
133 std::cout << "Received a Set request from Client" << std::endl;
134 printAttributes(attrs);
136 return RCSSetResponse::defaultAction();
139 void initServer(const std::string& resourceUri, const std::string& resourceType,
140 const std::string& attrKey)
142 g_resource = RCSResourceObject::Builder(resourceUri, resourceType, ACTUATOR_INTERFACE)
143 .addInterface(CUSTOM_INTERFACE)
144 .addInterface(SENSOR_INTERFACE)
145 .setDefaultInterface(BASELINE_INTERFACE)
146 .setDiscoverable(true)
150 g_resource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
151 g_resource->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
152 g_resource->setAttribute(attrKey, 0);
155 void updateAttribute(const std::string& attrKey, int control)
157 const int diff = control == INCREASE ? 1 : - 1;
160 RCSResourceObject::LockGuard lock(g_resource);
161 auto& attrs = g_resource->getAttributes();
162 attrs[attrKey] = attrs[attrKey].get<int>() + diff;
165 if (control == INCREASE)
167 std::cout << attrKey << " increased." << std::endl;
171 std::cout << attrKey << " decreased." << std::endl;
173 std::cout << "\nCurrent " << attrKey << ": "
174 << g_resource->getAttributeValue(attrKey).get<int>() << std::endl;
177 void runResourceControl(DisplayControlMenuFunc displayMenuFunc, const std::string& attrKey)
180 updateAttribute(attrKey, processUserInput(INCREASE, DECREASE));
183 void runResourceTypeSelection(int resourceMode)
185 std::cout << "========================================================\n";
186 std::cout << "Select Resource Type \n";
187 std::cout << RESOURCE_TEMP << ". Temperature \n";
188 std::cout << RESOURCE_LIGHT << ". Light \n";
189 std::cout << RESOURCE_LIGHT + 1 << ". Quit \n";
190 std::cout << "========================================================\n";
192 int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT);
193 DisplayControlMenuFunc displayMenuFunc = nullptr;
196 switch (resourceType)
199 attrKey = "Temperature";
200 initServer("/a/TempSensor", "oic.r.temperaturesensor", attrKey);
202 displayMenuFunc = displayControlTemperatureMenu;
206 attrKey = "Brightness";
207 initServer("/a/light", "oic.r.light", attrKey);
209 displayMenuFunc = displayControlLightMenu;
213 if (resourceMode == CUSTOM_SERVER)
215 g_resource->setGetRequestHandler(requestHandlerForGet);
216 g_resource->setSetRequestHandler(requestHandlerForSet);
219 g_currentRun = std::bind(runResourceControl, displayMenuFunc, std::move(attrKey));
222 void runResourceModeSelection()
224 std::cout << "======================================================== \n";
225 std::cout << DEFAULT_SERVER << ". Creation of Simple Resource Without Handlers \n";
226 std::cout << CUSTOM_SERVER << ". Creation of Resource With Set and Get Handlers \n";
227 std::cout << CUSTOM_SERVER + 1 << ". Quit \n";
228 std::cout << "======================================================== \n";
230 g_currentRun = std::bind(runResourceTypeSelection,
231 processUserInput(DEFAULT_SERVER, CUSTOM_SERVER));
234 void runPresenceSelection()
236 constexpr int PRESENCE_ON = 1;
237 constexpr int PRESENCE_OFF = 2;
239 std::cout << "========================================================\n";
240 std::cout << PRESENCE_ON << ". Presence On \n";
241 std::cout << PRESENCE_OFF << ". Presence Off \n";
242 std::cout << PRESENCE_OFF + 1 << ". Quit \n";
243 std::cout << "========================================================\n";
245 if (processUserInput(PRESENCE_ON, PRESENCE_OFF) == PRESENCE_ON)
247 g_isPresenceStarted = true;
251 g_currentRun = runResourceModeSelection;
256 g_currentRun = runPresenceSelection;
264 catch (const std::exception& e)
266 std::cout << e.what() << std::endl;
268 catch (const CloseApp&)
273 std::cout << "Stopping the server" << std::endl;
277 if (g_isPresenceStarted)
285 std::cout << "presence stop fail" << std::endl;