3 #include "ResourceClient.h"
4 #include "ResourceAttributes.h"
5 #include "OCPlatform.h"
9 using namespace OIC::Service;
11 std::shared_ptr<RemoteResourceObject> resource;
12 ResourceAttributes resourceAttributes;
13 bool startCachingFlag;
16 //callback function for discoverResource()
17 void OnResourceDiscovered(std::shared_ptr<RemoteResourceObject> foundResource)
20 cout << "\nOnResourceDiscovered callback" << std::endl;
22 std::string resourceURI = foundResource->getUri();
23 std::string hostAddress = foundResource->getAddress();
25 cout << "\tResource URI : " << resourceURI << std::endl;
26 cout << "\tResource Host : " << hostAddress << std::endl;
28 resource = foundResource;
32 //callback for StartMonitoring()
33 void OnResourceStateChanged(ResourceState resourceState)
36 cout << "\nOnResourceStateChanged callback" << std::endl;
38 if (resourceState == ResourceState::NOT_MONITORING)
39 cout << "State changed to : NOT_MONITORING" << std::endl;
40 else if (resourceState == ResourceState::ALIVE)
41 cout << "State changed to : ALIVE" << std::endl;
42 else if (resourceState == ResourceState::REQUESTED)
43 cout << "State changed to : REQUESTED" << std::endl;
44 else if (resourceState == ResourceState::LOST_SIGNAL)
45 cout << "State changed to : LOST_SIGNAL" << std::endl;
46 else if (resourceState == ResourceState::DESTROYED)
47 cout << "State changed to : DESTROYED" << std::endl;
50 //callback for startCaching() [uptodate]
51 void OnCacheUpdated(const ResourceAttributes atttribute )
53 cout << "\nOnCacheUpdated callback" << std::endl;
54 if (atttribute.empty())
56 std::cout << "Attribute is Empty" << std::endl;
60 ResourceAttributes::const_iterator iter = atttribute.begin();
61 for (unsigned int i = 0; i < atttribute.size(); ++i)
63 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
69 //callback for getRemoteAttributes()
70 void OnRemoteAttributesReceivedCallback(const ResourceAttributes &atttribute)
73 std::cout << "\nOnRemoteAttributesReceivedCallback callback" << std::endl;
74 if (atttribute.empty())
76 std::cout << "Got empty attribute " << std::endl;
80 resourceAttributes = atttribute;
81 ResourceAttributes::const_iterator iter = atttribute.begin();
82 for (unsigned int i = 0; i < atttribute.size(); ++i)
84 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
90 //callback for setRemoteAttributes()
91 void OnRemoteAttributesSetCallback(const ResourceAttributes &atttribute)
94 std::cout << "\nOnRemoteAttributesSetCallback callback" << std::endl;
95 if (atttribute.empty())
97 std::cout << "Got empty attribute " << std::endl;
101 resourceAttributes = atttribute;
102 ResourceAttributes::const_iterator iter = atttribute.begin();
103 for (unsigned int i = 0; i < atttribute.size(); ++i)
105 std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
114 DiscoveryManager *discoveryManagerInstance = DiscoveryManager::getInstance();
115 bool cachingFlag = false;
117 //configuring the platform
118 PlatformConfig config
120 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
122 OCPlatform::Configure(config);
124 std::cout << "\nPlatform configured successfully" << std::endl;
125 std::string uri = "";
126 std::string address = "";
127 std::string rt = "core.TemperatureSensor";
131 uri = OC_RSRVD_WELL_KNOWN_URI + uri + "?rt=" + rt;
132 //discover the resource in the network
133 discoveryManagerInstance->discoverResource(address, uri , CT_DEFAULT, &OnResourceDiscovered);
135 catch (InvalidParameterException e)
137 cout << "Exeception in discoverResource" << e.what() << std::endl;
147 cout << "1 :: Start Hosting" << endl;
148 cout << "2 :: Stop Hosting" << endl;
149 cout << "3 :: Get Attribute" << endl;
150 cout << "4 :: Set Attribute" << endl;
151 cout << "5 :: Start caching (No update to Application)" << endl;
152 cout << "6 :: Start caching (Update the application when data change)" <<
153 endl; //look for the datachange on server
154 cout << "7 :: Get Resource cache State" << endl;
155 cout << "8 :: Get Cached Attributes" << endl;
156 cout << "9 :: Get Cached Attribute" << endl;
157 cout << "10 :: Stop caching" << endl;
158 cout << "11 :: QUIT" << endl;
166 resource->startMonitoring(&OnResourceStateChanged);
167 cout << "\n\n********** Hosting Started ***********" << std::endl;
169 catch (InvalidParameterException e)
171 cout << "Exeception in startMonitoring :: " << e.what() << std::endl;
174 else if (userInput == 2)
176 resource->stopMonitoring();
177 cout << "\n\n****** Hosting stopped******" << std::endl;
179 else if (userInput == 3)
181 resource->getRemoteAttributes(&OnRemoteAttributesReceivedCallback);
183 else if (userInput == 4)
185 int temperatureValue;
186 if (0 == resourceAttributes.size())
188 cout << "\n***First Get the Attributes from Remote Device : press 3 to get attributes***" <<
193 ResourceAttributes::const_iterator iter = resourceAttributes.begin();
194 for (unsigned int i = 0; i < resourceAttributes.size(); ++i)
196 if ( iter->key() == "Temperature")
198 cout << "Enter the value you want to set :";
199 cin >> temperatureValue;
200 resourceAttributes["Temperature"] = temperatureValue;
201 resource->setRemoteAttributes(resourceAttributes, &OnRemoteAttributesSetCallback);
207 else if (userInput == 5)
209 if (false == cachingFlag)
211 resource->startCaching();
212 cout << "********** caching Started ***********" << std::endl;
217 cout << "*** Already Started... To start it again first stop it : press 10 ***" << std::endl;
220 else if (userInput == 6)
224 if (false == cachingFlag)
226 resource->startCaching(&OnCacheUpdated);
227 cout << "********** caching Started ***********" << std::endl;
231 cout << "*** Already Started... To start it again first stop it : press 10 ***" << std::endl;
234 catch (InvalidParameterException e)
236 cout << "Exeception in startCaching :: " << e.what() << std::endl;
239 else if (userInput == 7)
242 CacheState state = resource->getResourceCacheState();
243 if (state == CacheState ::READY)
244 cout << "Current Cache State : " << "CACHE_STATE ::READY" << std::endl;
245 else if (state == CacheState ::READY_YET)
246 cout << "Current Cache State : " << "CACHE_STATE ::READY_YET" << std::endl;
247 else if (state == CacheState ::LOST_SIGNAL)
248 cout << "Current Cache State : " << "CACHE_STATE ::LOST_SIGNAL" << std::endl;
249 else if (state == CacheState ::DESTROYED)
250 cout << "Current Cache State : " << "CACHE_STATE ::DESTROYED" << std::endl;
251 else if (state == CacheState ::UPDATING)
252 cout << "Current Cache State : " << "CACHE_STATE ::UPDATING" << std::endl;
253 else if (state == CacheState ::NONE)
254 cout << "Current Cache State : " << "CACHE_STATE ::NONE" << std::endl;
256 else if (userInput == 8)
260 ResourceAttributes atttribute = resource->getCachedAttributes();
261 if (atttribute.empty())
263 cout << "Received cached attribute is empty" << std::endl;
267 ResourceAttributes::const_iterator iter = atttribute.begin();
268 for (unsigned int i = 0; i < atttribute.size(); ++i)
270 std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
275 catch (BadRequestException e)
277 cout << "getCachedAttributes exception : " << e.what() << std::endl;
280 else if (userInput == 9)
282 std::string key = "Temperature";
285 ResourceAttributes::Value valueObj = resource->getCachedAttribute(key);
286 int value = valueObj.get< int >();
287 cout << "\nkey : " << key << "\nValue : " << value << std::endl;
289 catch (BadRequestException e)
291 cout << "getCachedAttribute exception : " << e.what() << std::endl;
293 catch (BadGetException e)
295 cout << "Exeception in getCachedAttribute BadGetException:: " << e.what() << std::endl;
298 else if (userInput == 10)
300 resource->stopCaching();
302 cout << "****** Caching stopped ******" << std::endl;
304 else if (userInput == 11)
311 cout << "*** Please enter the number between 1-11 ***" << std::endl;