1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include <condition_variable>
26 #include "RCSDiscoveryManager.h"
27 #include "RCSRemoteResourceObject.h"
28 #include "RCSResourceAttributes.h"
29 #include "RCSAddress.h"
31 #include "OCPlatform.h"
34 using namespace OIC::Service;
36 constexpr int CORRECT_INPUT = 1;
37 constexpr int INCORRECT_INPUT = 2;
38 constexpr int QUIT_INPUT = 3;
48 typedef void(*ClientMenuHandler)();
49 typedef int ReturnValue;
54 ClientMenuHandler m_handler;
58 void getAttributeFromRemoteServer();
59 void setAttributeToRemoteServer();
61 ClientMenu clientMenu[] =
63 {Menu::GET_ATTRIBUTE, getAttributeFromRemoteServer, CORRECT_INPUT},
64 {Menu::SET_ATTRIBUTE, setAttributeToRemoteServer, CORRECT_INPUT},
65 {Menu::QUIT, [](){}, QUIT_INPUT},
66 {Menu::END_OF_MENU, nullptr, INCORRECT_INPUT}
69 const std::string defaultKey = "deviceInfo";
70 const std::string resourceType = "core.ac";
73 std::condition_variable cond;
75 RCSRemoteResourceObject::Ptr g_resource;
79 std::cout << std::endl;
80 std::cout << "1 :: Get Attribute" << std::endl;
81 std::cout << "2 :: Set Attribute" << std::endl;
84 void onResourceDiscovered(RCSRemoteResourceObject::Ptr foundResource)
86 std::cout << "onResourceDiscovered callback" << std::endl;
88 std::cout << "\t\tResource URI : " << foundResource->getUri() << std::endl;
89 std::cout << "\t\tResource Host : " << foundResource->getAddress() << std::endl;
91 g_resource = foundResource;
96 void onRemoteAttributesReceivedCallback(const RCSResourceAttributes &attributes, int /*eCode*/)
98 std::cout << "onRemoteAttributesReceivedCallback callback\n" << std::endl;
100 if (attributes.empty())
102 std::cout << "\tAttribute is Empty" << std::endl;
106 for (const auto & attr : attributes)
108 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
109 << attr.value().toString() << std::endl;
110 std::cout << "=============================================\n" << std::endl;
112 const auto& doubleVector = attr.value().
113 get< std::vector< std::vector< RCSResourceAttributes > > >();
115 for (const auto& vector : doubleVector)
117 for (const auto& attrs : vector)
119 for (const auto & kvPair : attrs)
121 std::cout << "\t" << kvPair.key() << " : " <<
122 kvPair.value().toString() << std::endl;
125 std::cout << std::endl;
127 std::cout << "=============================================\n" << std::endl;
132 std::vector< std::vector< RCSResourceAttributes > > createNestedAttribute(int speedValue,
135 RCSResourceAttributes model;
136 RCSResourceAttributes weight;
137 RCSResourceAttributes dimensions;
139 model["model"] = "SamsungAC";
140 weight["weight"] = 3;
141 dimensions["dimensions"] = "10x25x35";
143 RCSResourceAttributes speed;
144 RCSResourceAttributes airCirculation;
146 speed["speed"] = speedValue;
147 airCirculation["air"] = aircValue;
149 RCSResourceAttributes temperature;
150 RCSResourceAttributes humidity;
152 temperature["temp"] = 30;
153 humidity["humidity"] = 30;
155 RCSResourceAttributes power;
156 RCSResourceAttributes capacity;
158 power["power"] = 1600;
159 capacity["capacity"] = 1;
161 RCSResourceAttributes red;
162 RCSResourceAttributes green;
167 std::vector< RCSResourceAttributes > generalInfo{ model, weight, dimensions };
168 std::vector< RCSResourceAttributes > fan{ speed, airCirculation } ;
169 std::vector< RCSResourceAttributes > tempSensor{ temperature, humidity } ;
170 std::vector< RCSResourceAttributes > efficiency{ power, capacity };
171 std::vector< RCSResourceAttributes > light{ red, green };
173 std::vector< std::vector< RCSResourceAttributes > > acServer;
175 acServer.push_back(generalInfo);
176 acServer.push_back(fan);
177 acServer.push_back(tempSensor);
178 acServer.push_back(efficiency);
179 acServer.push_back(light);
184 int processUserInput()
187 std::cin >> userInput;
191 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
197 void getAttributeFromRemoteServer()
199 g_resource->getRemoteAttributes(&onRemoteAttributesReceivedCallback);
202 void setAttributeToRemoteServer()
207 std::cout << "\tEnter the Fan Speed you want to set : ";
209 std::cout << "\tEnter the Air circulation value you want to set :";
212 RCSResourceAttributes setAttribute;
213 setAttribute[defaultKey] = createNestedAttribute(speed, airc);
215 g_resource->setRemoteAttributes(setAttribute,
216 &onRemoteAttributesReceivedCallback);
219 int selectClientMenu(int selectedMenu)
221 for (int i = 0; clientMenu[i].m_menu != Menu::END_OF_MENU; i++)
223 if (clientMenu[i].m_menu == selectedMenu)
225 clientMenu[i].m_handler();
226 return clientMenu[i].m_result;
230 std::cout << "Invalid input, please try again" << std::endl;
232 return INCORRECT_INPUT;
241 if (selectClientMenu(processUserInput()) == QUIT_INPUT)
248 void platFormConfigure()
250 PlatformConfig config
252 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
254 OCPlatform::Configure(config);
257 bool discoverResource()
259 std::cout << "Wait 2 seconds until discovered." << std::endl;
263 RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(),
264 resourceType, &onResourceDiscovered);
266 catch (const RCSPlatformException& e)
268 std::cout << e.what() << std::endl;
272 std::cout << "unknown exception" << std::endl;
274 std::unique_lock<std::mutex> lck(mtx);
275 cond.wait_for(lck, std::chrono::seconds(2));
277 return g_resource != nullptr;
284 if (!discoverResource())
286 std::cout << "Can't discovered Server... Exiting the Client." << std::endl;
294 catch (const std::exception &e)
296 std::cout << "main exception : " << e.what() << std::endl;
299 std::cout << "Stopping the client" << std::endl;