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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "condition_variable"
25 #include "RCSDiscoveryManager.h"
26 #include "RCSRemoteResourceObject.h"
27 #include "RCSResourceAttributes.h"
28 #include "RCSAddress.h"
30 #include "OCPlatform.h"
32 #define nestedAtrribute std::vector<std::vector<RCSResourceAttributes>>
35 using namespace OIC::Service;
37 constexpr int CORRECT_INPUT = 1;
38 constexpr int INCORRECT_INPUT = 2;
39 constexpr int QUIT_INPUT = 3;
41 std::shared_ptr<RCSRemoteResourceObject> resource;
43 const std::string defaultKey = "deviceInfo";
44 const std::string resourceType = "core.ac";
45 const std::string relativetUri = OC_RSRVD_WELL_KNOWN_URI;
47 RCSResourceAttributes model;
48 RCSResourceAttributes speed;
49 RCSResourceAttributes airCirculation;
50 RCSResourceAttributes temperature;
51 RCSResourceAttributes humidity;
52 RCSResourceAttributes power;
53 RCSResourceAttributes capacity;
54 RCSResourceAttributes weight;
55 RCSResourceAttributes dimensions;
56 RCSResourceAttributes red;
57 RCSResourceAttributes green;
59 std::vector<RCSResourceAttributes> generalInfo;
60 std::vector<RCSResourceAttributes> fan;
61 std::vector<RCSResourceAttributes> tempSensor;
62 std::vector<RCSResourceAttributes> efficiency;
63 std::vector<RCSResourceAttributes> light;
67 std::condition_variable cond;
69 void getAttributeFromRemoteServer();
70 void setAttributeToRemoteServer();
80 typedef void(*ClientMenuHandler)();
81 typedef int ReturnValue;
86 ClientMenuHandler m_handler;
90 ClientMenu clientMenu[] =
92 {Menu::GET_ATTRIBUTE, getAttributeFromRemoteServer, CORRECT_INPUT},
93 {Menu::SET_ATTRIBUTE, setAttributeToRemoteServer, CORRECT_INPUT},
94 {Menu::QUIT, [](){}, QUIT_INPUT},
95 {Menu::END_OF_MENU, nullptr, INCORRECT_INPUT}
100 std::cout << std::endl;
101 std::cout << "1 :: Get Attribute" << std::endl;
102 std::cout << "2 :: Set Attribute" << std::endl;
105 void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
107 std::cout << "onResourceDiscovered callback" << std::endl;
109 std::string resourceURI = foundResource->getUri();
110 std::string hostAddress = foundResource->getAddress();
112 std::cout << "\t\tResource URI : " << resourceURI << std::endl;
113 std::cout << "\t\tResource Host : " << hostAddress << std::endl;
115 resource = foundResource;
120 void onRemoteAttributesReceivedCallback(const RCSResourceAttributes &attributes, int /*eCode*/)
122 std::cout << "onRemoteAttributesReceivedCallback callback\n" << std::endl;
124 if (attributes.empty())
126 std::cout << "\tAttribute is Empty" << std::endl;
130 for (const auto & attr : attributes)
132 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
133 << attr.value().toString() << std::endl;
134 std::cout << "=============================================\n" << std::endl;
136 OIC::Service::RCSResourceAttributes::Value attrValue = attr.value();
137 std::vector< std::vector<RCSResourceAttributes >> attrVector =
138 attrValue.get<std::vector< std::vector<RCSResourceAttributes >>>();
140 for (auto itr = attrVector.begin(); itr != attrVector.end(); ++itr)
142 std::vector<RCSResourceAttributes > attrKeyVector = *itr;
143 for (auto itrKey = attrKeyVector.begin(); itrKey != attrKeyVector.end(); ++itrKey)
145 for (const auto & attribute : *itrKey)
147 std::cout << "\t" << attribute.key() << " : " << attribute.value().toString() << std::endl;
150 std::cout << std::endl;
153 std::cout << "=============================================\n" << std::endl;
157 nestedAtrribute createNestedAttribute(int speedValue, int aircValue)
159 nestedAtrribute *acServer = new nestedAtrribute();
161 model["model"] = "SamsungAC";
163 speed["speed"] = speedValue;
164 airCirculation["air"] = aircValue;
166 temperature["temp"] = 30;
167 humidity["humidity"] = 30;
169 power["power"] = 1600;
170 capacity["capacity"] = 1;
172 weight["weight"] = 3;
173 dimensions["dimensions"] = "10x25x35";
179 generalInfo.push_back(model);
180 generalInfo.push_back(weight);
181 generalInfo.push_back(dimensions);
184 fan.push_back(speed);
185 fan.push_back(airCirculation);
188 tempSensor.push_back(temperature);
189 tempSensor.push_back(humidity);
192 efficiency.push_back(power);
193 efficiency.push_back(capacity);
196 light.push_back(red);
197 light.push_back(green);
199 if (nullptr == acServer)
201 std::cout << "Null nestedAtrribute" << std::endl;
205 acServer->push_back(generalInfo);
206 acServer->push_back(fan);
207 acServer->push_back(tempSensor);
208 acServer->push_back(efficiency);
209 acServer->push_back(light);
215 int processUserInput()
218 std::cin >> userInput;
222 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
228 void getAttributeFromRemoteServer()
230 if(nullptr == resource)
233 resource->getRemoteAttributes(&onRemoteAttributesReceivedCallback);
236 void setAttributeToRemoteServer()
238 if(nullptr == resource)
243 std::cout << "\tEnter the Fan Speed you want to set : ";
245 std::cout << "\tEnter the Air circulation value you want to set :";
248 nestedAtrribute nestedAttr = createNestedAttribute(speed, airc);
250 RCSResourceAttributes setAttribute;
251 setAttribute[defaultKey] = nestedAttr;
253 resource->setRemoteAttributes(setAttribute,
254 &onRemoteAttributesReceivedCallback);
257 int selectClientMenu(int selectedMenu)
259 for (int i = 0; clientMenu[i].m_menu != Menu::END_OF_MENU; i++)
261 if (clientMenu[i].m_menu == selectedMenu)
263 clientMenu[i].m_handler();
264 return clientMenu[i].m_result;
268 std::cout << "Invalid input, please try again" << std::endl;
270 return INCORRECT_INPUT;
279 if (selectClientMenu(processUserInput()) == QUIT_INPUT)
284 void platFormConfigure()
286 PlatformConfig config
288 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
290 OCPlatform::Configure(config);
293 bool discoverResource()
295 std::cout << "Wait 2 seconds until discovered." << std::endl;
299 RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(),
300 relativetUri, resourceType, &onResourceDiscovered);
302 catch(PlatformException e)
304 std::cout << "Platform Exception while calling discoverResourceByType" << std::endl;
306 std::unique_lock<std::mutex> lck(mtx);
307 cond.wait_for(lck, std::chrono::seconds(2));
309 return resource != nullptr;
316 if (!discoverResource())
318 std::cout << "Can't discovered Server... Exiting the Client." << std::endl;
326 catch (const std::exception &e)
328 std::cout << "main exception : " << e.what() << std::endl;
331 std::cout << "Stopping the Client" << std::endl;