3 #include "RCSDiscoveryManager.h"
4 #include "RCSRemoteResourceObject.h"
5 #include "RCSResourceAttributes.h"
6 #include "OCPlatform.h"
7 #include "RCSAddress.h"
11 using namespace OIC::Service;
13 std::shared_ptr<RCSRemoteResourceObject> resource;
14 RCSResourceAttributes resourceAttributes;
15 bool startCachingFlag;
18 //callback function for discoverResource()
19 void OnResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
22 cout << "\nOnResourceDiscovered callback" << std::endl;
24 std::string resourceURI = foundResource->getUri();
25 std::string hostAddress = foundResource->getAddress();
27 cout << "\tResource URI : " << resourceURI << std::endl;
28 cout << "\tResource Host : " << hostAddress << std::endl;
30 resource = foundResource;
34 //callback for StartMonitoring()
35 void OnResourceStateChanged(ResourceState resourceState)
38 cout << "\nOnResourceStateChanged callback" << std::endl;
40 if (resourceState == ResourceState::NONE)
41 cout << "State changed to : NONE" << std::endl;
42 else if (resourceState == ResourceState::ALIVE)
43 cout << "State changed to : ALIVE" << std::endl;
44 else if (resourceState == ResourceState::REQUESTED)
45 cout << "State changed to : REQUESTED" << std::endl;
46 else if (resourceState == ResourceState::LOST_SIGNAL)
48 cout << "State changed to : LOST_SIGNAL" << std::endl;
51 else if (resourceState == ResourceState::DESTROYED)
52 cout << "State changed to : DESTROYED" << std::endl;
55 //callback for startCaching() [uptodate]
56 void OnCacheUpdated(const RCSResourceAttributes atttribute )
58 cout << "\nOnCacheUpdated callback" << std::endl;
59 if (atttribute.empty())
61 std::cout << "Attribute is Empty" << std::endl;
65 RCSResourceAttributes::const_iterator iter = atttribute.begin();
66 for (unsigned int i = 0; i < atttribute.size(); ++i)
68 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
74 //callback for getRemoteAttributes()
75 void OnRemoteAttributesReceivedCallback(const RCSResourceAttributes &atttribute)
78 std::cout << "\nOnRemoteAttributesReceivedCallback callback" << std::endl;
79 if (atttribute.empty())
81 std::cout << "Got empty attribute " << std::endl;
85 resourceAttributes = atttribute;
86 RCSResourceAttributes::const_iterator iter = atttribute.begin();
87 for (unsigned int i = 0; i < atttribute.size(); ++i)
89 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
95 //callback for setRemoteAttributes()
96 void OnRemoteAttributesSetCallback(const RCSResourceAttributes &atttribute)
99 std::cout << "\nOnRemoteAttributesSetCallback callback" << std::endl;
100 if (atttribute.empty())
102 std::cout << "Got empty attribute " << std::endl;
106 resourceAttributes = atttribute;
107 RCSResourceAttributes::const_iterator iter = atttribute.begin();
108 for (unsigned int i = 0; i < atttribute.size(); ++i)
110 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
119 RCSDiscoveryManager *discoveryManagerInstance = RCSDiscoveryManager::getInstance();
120 bool cachingFlag = false;
121 bool startMonitoringFlag= false;
123 //configuring the platform
124 PlatformConfig config
126 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
128 OCPlatform::Configure(config);
130 std::cout << "\nPlatform configured successfully" << std::endl;
131 std::string uri = "";
132 std::string address = "";
133 std::string rt = "core.TemperatureSensor";
138 uri = OC_RSRVD_WELL_KNOWN_URI + uri + "?rt=" + rt;
140 //getting the object of RCSAddress for multicast discovery
141 RCSAddress rcsAddress = RCSAddress::multicast();
143 //discover the resource in the network
144 discoveryManagerInstance->discoverResource(rcsAddress, uri , &OnResourceDiscovered);
146 catch (InvalidParameterException e)
148 cout << "Exeception in discoverResource" << e.what() << std::endl;
158 cout << "1 :: Start Hosting" << endl;
159 cout << "2 :: Stop Hosting" << endl;
160 cout << "3 :: Get Attribute" << endl;
161 cout << "4 :: Set Attribute" << endl;
162 cout << "5 :: Start caching (No update to Application)" << endl;
163 cout << "6 :: Start caching (Update the application when data change)" <<
164 endl; //look for the datachange on server
165 cout << "7 :: Get Resource cache State" << endl;
166 cout << "8 :: Get Cached Attributes" << endl;
167 cout << "9 :: Get Cached Attribute" << endl;
168 cout << "10 :: Stop caching" << endl;
169 cout << "11 :: QUIT" << endl;
177 cout << "\n\n******No Resource found :-> Run the Server and Restart this App******" << std::endl;
183 if(false == startMonitoringFlag)
185 resource->startMonitoring(&OnResourceStateChanged);
186 startMonitoringFlag = true;
187 cout << "\n\n********** Hosting Started ***********" << std::endl;
191 cout << "\n\n********** Already Started ***********" << std::endl;
195 catch (InvalidParameterException e)
197 cout << "Exeception in startMonitoring :: " << e.what() << std::endl;
201 else if (userInput == 2)
205 cout << "\n\n******No Resource found :-> Run the Server and Restart this App******" << std::endl;
209 if(true == startMonitoringFlag)
211 resource->stopMonitoring();
212 cout << "\n\n****** Hosting stopped******" << std::endl;
213 startMonitoringFlag = false;
217 cout << "\n\n******Hosting not started : press 1 to start Hosting******" << std::endl;
221 else if (userInput == 3)
225 cout << "\n\n******No Resource found :-> Run the Server and Restart this App******" << std::endl;
229 resource->getRemoteAttributes(&OnRemoteAttributesReceivedCallback);
232 else if (userInput == 4)
234 int temperatureValue;
237 cout << "\n\n******No Resource found :-> Run the Server and Restart this App******" << std::endl;
241 if (0 == resourceAttributes.size())
243 cout << "\n***First Get the Attributes from Remote Device : press 3 to get attributes***" <<
248 RCSResourceAttributes::const_iterator iter = resourceAttributes.begin();
249 for (unsigned int i = 0; i < resourceAttributes.size(); ++i)
251 if ( iter->key() == "Temperature")
253 cout << "Enter the value you want to set :";
254 cin >> temperatureValue;
255 resourceAttributes["Temperature"] = temperatureValue;
256 resource->setRemoteAttributes(resourceAttributes, &OnRemoteAttributesSetCallback);
263 else if (userInput == 5)
267 cout << "\n\n******No Resource found :-> Run the Server and Restart this app******" << std::endl;
271 if (false == cachingFlag)
273 resource->startCaching();
274 cout << "********** caching Started ***********" << std::endl;
279 cout << "*** Already Started... To start it again first stop it : press 10 ***" << std::endl;
283 else if (userInput == 6)
288 cout << "\n\n******No Resource found :-> Run the Server and Restart this app******" << std::endl;
294 if (false == cachingFlag)
296 resource->startCaching(&OnCacheUpdated);
297 cout << "********** caching Started ***********" << std::endl;
302 cout << "*** Already Started... To start it again first stop it : press 10 ***" << std::endl;
305 catch (InvalidParameterException e)
307 cout << "Exeception in startCaching :: " << e.what() << std::endl;
311 else if (userInput == 7)
315 cout << "\n\n******No Resource found :-> Run the Server and Restart this app******" << std::endl;
319 CacheState state = resource->getCacheState();
320 if (state == CacheState ::READY)
321 cout << "Current Cache State : " << "CACHE_STATE ::READY" << std::endl;
322 else if (state == CacheState ::UNREADY)
323 cout << "Current Cache State : " << "CACHE_STATE ::UNREADY" << std::endl;
324 else if (state == CacheState ::LOST_SIGNAL)
325 cout << "Current Cache State : " << "CACHE_STATE ::LOST_SIGNAL" << std::endl;
326 else if (state == CacheState ::NONE)
327 cout << "Current Cache State : " << "CACHE_STATE ::NONE" << std::endl;
330 else if (userInput == 8)
334 cout << "\n\n******No Resource found :-> Run the Server and Restart this app******" << std::endl;
340 RCSResourceAttributes atttribute = resource->getCachedAttributes();
341 if (atttribute.empty())
343 cout << "Received cached attribute is empty" << std::endl;
347 RCSResourceAttributes::const_iterator iter = atttribute.begin();
348 for (unsigned int i = 0; i < atttribute.size(); ++i)
350 std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
355 catch (BadRequestException e)
357 cout << "getCachedAttributes exception : " << e.what() << std::endl;
361 else if (userInput == 9)
363 std::string key = "Temperature";
366 cout << "\n\n******No Resource found :-> Run the Server and Restart this app******" << std::endl;
372 RCSResourceAttributes::Value valueObj = resource->getCachedAttribute(key);
373 int value = valueObj.get< int >();
374 cout << "\nkey : " << key << "\nValue : " << value << std::endl;
376 catch (BadRequestException e)
378 cout << "getCachedAttribute exception : " << e.what() << std::endl;
380 catch (BadGetException e)
382 cout << "Exeception in getCachedAttribute BadGetException:: " << e.what() << std::endl;
386 else if (userInput == 10)
390 cout << "\n\n******No Resource found :-> Run the Server and Restart this app******" << std::endl;
394 if(true == cachingFlag)
396 resource->stopCaching();
398 cout << "****** Caching stopped ******" << std::endl;
402 cout << "****** Caching not started : press 5 or 6 to start Caching ******" << std::endl;
406 else if (userInput == 11)
413 cout << "*** Please enter the number between 1-11 ***" << std::endl;