3 #include "ResourceClient.h"
4 #include "RCSResourceAttributes.h"
5 #include "OCPlatform.h"
6 #include "RCSAddress.h"
10 using namespace OIC::Service;
12 std::shared_ptr<RemoteResourceObject> resource;
13 RCSResourceAttributes resourceAttributes;
14 bool startCachingFlag;
17 //callback function for discoverResource()
18 void OnResourceDiscovered(std::shared_ptr<RemoteResourceObject> foundResource)
21 cout << "\nOnResourceDiscovered callback" << std::endl;
23 std::string resourceURI = foundResource->getUri();
24 std::string hostAddress = foundResource->getAddress();
26 cout << "\tResource URI : " << resourceURI << std::endl;
27 cout << "\tResource Host : " << hostAddress << std::endl;
29 resource = foundResource;
33 //callback for StartMonitoring()
34 void OnResourceStateChanged(ResourceState resourceState)
37 cout << "\nOnResourceStateChanged callback" << std::endl;
39 if (resourceState == ResourceState::NOT_MONITORING)
40 cout << "State changed to : NOT_MONITORING" << std::endl;
41 else if (resourceState == ResourceState::ALIVE)
42 cout << "State changed to : ALIVE" << std::endl;
43 else if (resourceState == ResourceState::REQUESTED)
44 cout << "State changed to : REQUESTED" << std::endl;
45 else if (resourceState == ResourceState::LOST_SIGNAL)
46 cout << "State changed to : LOST_SIGNAL" << std::endl;
47 else if (resourceState == ResourceState::DESTROYED)
48 cout << "State changed to : DESTROYED" << std::endl;
51 //callback for startCaching() [uptodate]
52 void OnCacheUpdated(const RCSResourceAttributes atttribute )
54 cout << "\nOnCacheUpdated callback" << std::endl;
55 if (atttribute.empty())
57 std::cout << "Attribute is Empty" << std::endl;
61 RCSResourceAttributes::const_iterator iter = atttribute.begin();
62 for (unsigned int i = 0; i < atttribute.size(); ++i)
64 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
70 //callback for getRemoteAttributes()
71 void OnRemoteAttributesReceivedCallback(const RCSResourceAttributes &atttribute)
74 std::cout << "\nOnRemoteAttributesReceivedCallback callback" << std::endl;
75 if (atttribute.empty())
77 std::cout << "Got empty attribute " << std::endl;
81 resourceAttributes = atttribute;
82 RCSResourceAttributes::const_iterator iter = atttribute.begin();
83 for (unsigned int i = 0; i < atttribute.size(); ++i)
85 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
91 //callback for setRemoteAttributes()
92 void OnRemoteAttributesSetCallback(const RCSResourceAttributes &atttribute)
95 std::cout << "\nOnRemoteAttributesSetCallback callback" << std::endl;
96 if (atttribute.empty())
98 std::cout << "Got empty attribute " << std::endl;
102 resourceAttributes = atttribute;
103 RCSResourceAttributes::const_iterator iter = atttribute.begin();
104 for (unsigned int i = 0; i < atttribute.size(); ++i)
106 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
115 DiscoveryManager *discoveryManagerInstance = DiscoveryManager::getInstance();
116 bool cachingFlag = false;
118 //configuring the platform
119 PlatformConfig config
121 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
123 OCPlatform::Configure(config);
125 std::cout << "\nPlatform configured successfully" << std::endl;
126 std::string uri = "";
127 std::string address = "";
128 std::string rt = "core.TemperatureSensor";
133 uri = OC_RSRVD_WELL_KNOWN_URI + uri + "?rt=" + rt;
135 //getting the object of RCSAddress for multicast discovery
136 RCSAddress rcsAddress = RCSAddress::multicast();
138 //discover the resource in the network
139 discoveryManagerInstance->discoverResource(rcsAddress, uri , &OnResourceDiscovered);
141 catch (InvalidParameterException e)
143 cout << "Exeception in discoverResource" << e.what() << std::endl;
153 cout << "1 :: Start Hosting" << endl;
154 cout << "2 :: Stop Hosting" << endl;
155 cout << "3 :: Get Attribute" << endl;
156 cout << "4 :: Set Attribute" << endl;
157 cout << "5 :: Start caching (No update to Application)" << endl;
158 cout << "6 :: Start caching (Update the application when data change)" <<
159 endl; //look for the datachange on server
160 cout << "7 :: Get Resource cache State" << endl;
161 cout << "8 :: Get Cached Attributes" << endl;
162 cout << "9 :: Get Cached Attribute" << endl;
163 cout << "10 :: Stop caching" << endl;
164 cout << "11 :: QUIT" << endl;
172 resource->startMonitoring(&OnResourceStateChanged);
173 cout << "\n\n********** Hosting Started ***********" << std::endl;
175 catch (InvalidParameterException e)
177 cout << "Exeception in startMonitoring :: " << e.what() << std::endl;
180 else if (userInput == 2)
182 resource->stopMonitoring();
183 cout << "\n\n****** Hosting stopped******" << std::endl;
185 else if (userInput == 3)
187 resource->getRemoteAttributes(&OnRemoteAttributesReceivedCallback);
189 else if (userInput == 4)
191 int temperatureValue;
192 if (0 == resourceAttributes.size())
194 cout << "\n***First Get the Attributes from Remote Device : press 3 to get attributes***" <<
199 RCSResourceAttributes::const_iterator iter = resourceAttributes.begin();
200 for (unsigned int i = 0; i < resourceAttributes.size(); ++i)
202 if ( iter->key() == "Temperature")
204 cout << "Enter the value you want to set :";
205 cin >> temperatureValue;
206 resourceAttributes["Temperature"] = temperatureValue;
207 resource->setRemoteAttributes(resourceAttributes, &OnRemoteAttributesSetCallback);
213 else if (userInput == 5)
215 if (false == cachingFlag)
217 resource->startCaching();
218 cout << "********** caching Started ***********" << std::endl;
223 cout << "*** Already Started... To start it again first stop it : press 10 ***" << std::endl;
226 else if (userInput == 6)
230 if (false == cachingFlag)
232 resource->startCaching(&OnCacheUpdated);
233 cout << "********** caching Started ***********" << std::endl;
237 cout << "*** Already Started... To start it again first stop it : press 10 ***" << std::endl;
240 catch (InvalidParameterException e)
242 cout << "Exeception in startCaching :: " << e.what() << std::endl;
245 else if (userInput == 7)
248 CacheState state = resource->getResourceCacheState();
249 if (state == CacheState ::READY)
250 cout << "Current Cache State : " << "CACHE_STATE ::READY" << std::endl;
251 else if (state == CacheState ::READY_YET)
252 cout << "Current Cache State : " << "CACHE_STATE ::READY_YET" << std::endl;
253 else if (state == CacheState ::LOST_SIGNAL)
254 cout << "Current Cache State : " << "CACHE_STATE ::LOST_SIGNAL" << std::endl;
255 else if (state == CacheState ::DESTROYED)
256 cout << "Current Cache State : " << "CACHE_STATE ::DESTROYED" << std::endl;
257 else if (state == CacheState ::UPDATING)
258 cout << "Current Cache State : " << "CACHE_STATE ::UPDATING" << std::endl;
259 else if (state == CacheState ::NONE)
260 cout << "Current Cache State : " << "CACHE_STATE ::NONE" << std::endl;
262 else if (userInput == 8)
266 RCSResourceAttributes atttribute = resource->getCachedAttributes();
267 if (atttribute.empty())
269 cout << "Received cached attribute is empty" << std::endl;
273 RCSResourceAttributes::const_iterator iter = atttribute.begin();
274 for (unsigned int i = 0; i < atttribute.size(); ++i)
276 std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
281 catch (BadRequestException e)
283 cout << "getCachedAttributes exception : " << e.what() << std::endl;
286 else if (userInput == 9)
288 std::string key = "Temperature";
291 RCSResourceAttributes::Value valueObj = resource->getCachedAttribute(key);
292 int value = valueObj.get< int >();
293 cout << "\nkey : " << key << "\nValue : " << value << std::endl;
295 catch (BadRequestException e)
297 cout << "getCachedAttribute exception : " << e.what() << std::endl;
299 catch (BadGetException e)
301 cout << "Exeception in getCachedAttribute BadGetException:: " << e.what() << std::endl;
304 else if (userInput == 10)
306 resource->stopCaching();
308 cout << "****** Caching stopped ******" << std::endl;
310 else if (userInput == 11)
317 cout << "*** Please enter the number between 1-11 ***" << std::endl;