[Resource-Manipulation] Renamed Resource-Manipulation to Resource-Encapsulation
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / linux / SampleResourceClient.cpp
1 #include<iostream>
2
3 #include "ResourceClient.h"
4 #include "ResourceAttributes.h"
5 #include "OCPlatform.h"
6
7 using namespace std;
8 using namespace OC;
9 using namespace OIC::Service;
10
11 std::shared_ptr<RemoteResourceObject>  resource;
12 ResourceAttributes resourceAttributes;
13 bool startCachingFlag;
14 bool isReady;
15
16 //callback function for discoverResource()
17 void OnResourceDiscovered(std::shared_ptr<RemoteResourceObject> foundResource)
18 {
19
20     cout << "\nOnResourceDiscovered callback" << std::endl;
21
22     std::string resourceURI = foundResource->getUri();
23     std::string hostAddress = foundResource->getAddress();
24
25     cout << "\tResource URI : " << resourceURI << std::endl;
26     cout << "\tResource Host : " << hostAddress << std::endl;
27
28     resource = foundResource;
29     isReady = true;
30 }
31
32 //callback for StartMonitoring()
33 void OnResourceStateChanged(ResourceState resourceState)
34 {
35
36     cout << "\nOnResourceStateChanged callback" << std::endl;
37
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;
48 }
49
50 //callback for startCaching() [uptodate]
51 void OnCacheUpdated(const ResourceAttributes atttribute )
52 {
53     cout << "\nOnCacheUpdated callback" << std::endl;
54     if (atttribute.empty())
55     {
56         std::cout << "Attribute is Empty" << std::endl;
57     }
58     else
59     {
60         ResourceAttributes::const_iterator iter = atttribute.begin();
61         for (unsigned int i = 0; i < atttribute.size(); ++i)
62         {
63             std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
64             ++iter;
65         }
66     }
67
68 }
69 //callback for getRemoteAttributes()
70 void OnRemoteAttributesReceivedCallback(const ResourceAttributes &atttribute)
71 {
72
73     std::cout << "\nOnRemoteAttributesReceivedCallback callback" << std::endl;
74     if (atttribute.empty())
75     {
76         std::cout << "Got empty attribute " << std::endl;
77     }
78     else
79     {
80         resourceAttributes = atttribute;
81         ResourceAttributes::const_iterator iter = atttribute.begin();
82         for (unsigned int i = 0; i < atttribute.size(); ++i)
83         {
84             std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
85             ++iter;
86         }
87     }
88 }
89
90 //callback for setRemoteAttributes()
91 void OnRemoteAttributesSetCallback(const ResourceAttributes &atttribute)
92 {
93
94     std::cout << "\nOnRemoteAttributesSetCallback callback" << std::endl;
95     if (atttribute.empty())
96     {
97         std::cout << "Got empty attribute " << std::endl;
98     }
99     else
100     {
101         resourceAttributes = atttribute;
102         ResourceAttributes::const_iterator iter = atttribute.begin();
103         for (unsigned int i = 0; i < atttribute.size(); ++i)
104         {
105             std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
106             ++iter;
107         }
108     }
109 }
110
111 int main()
112 {
113
114     DiscoveryManager *discoveryManagerInstance =  DiscoveryManager::getInstance();
115     bool cachingFlag = false;
116
117     //configuring the platform
118     PlatformConfig config
119     {
120         OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
121     };
122     OCPlatform::Configure(config);
123
124     std::cout << "\nPlatform configured successfully" << std::endl;
125     std::string uri = "";
126     std::string address = "";
127     std::string rt = "core.TemperatureSensor";
128
129     try
130     {
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);
134     }
135     catch (InvalidParameterException e)
136     {
137         cout << "Exeception in discoverResource" << e.what() << std::endl;
138     }
139
140     bool isRun = true;
141     int userInput;
142     while (isRun)
143     {
144         while (isReady)
145         {
146             cout << 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;
159
160             cin >> userInput;
161
162             if (userInput == 1)
163             {
164                 try
165                 {
166                     resource->startMonitoring(&OnResourceStateChanged);
167                     cout << "\n\n**********  Hosting Started ***********" << std::endl;
168                 }
169                 catch (InvalidParameterException e)
170                 {
171                     cout << "Exeception in startMonitoring :: " << e.what() << std::endl;
172                 }
173             }
174             else if (userInput == 2)
175             {
176                 resource->stopMonitoring();
177                 cout << "\n\n******  Hosting stopped******" << std::endl;
178             }
179             else if (userInput == 3)
180             {
181                 resource->getRemoteAttributes(&OnRemoteAttributesReceivedCallback);
182             }
183             else if (userInput == 4)
184             {
185                 int temperatureValue;
186                 if (0 == resourceAttributes.size())
187                 {
188                     cout << "\n***First Get the Attributes from Remote Device : press 3 to get attributes***" <<
189                          std::endl;
190                 }
191                 else
192                 {
193                     ResourceAttributes::const_iterator iter = resourceAttributes.begin();
194                     for (unsigned int i = 0; i < resourceAttributes.size(); ++i)
195                     {
196                         if ( iter->key() == "Temperature")
197                         {
198                             cout << "Enter the value you want to set :";
199                             cin >> temperatureValue;
200                             resourceAttributes["Temperature"]  = temperatureValue;
201                             resource->setRemoteAttributes(resourceAttributes, &OnRemoteAttributesSetCallback);
202                         }
203                         ++iter;
204                     }
205                 }
206             }
207             else if (userInput == 5)
208             {
209                 if (false == cachingFlag)
210                 {
211                     resource->startCaching();
212                     cout << "**********  caching Started ***********" << std::endl;
213                     cachingFlag = true;
214                 }
215                 else
216                 {
217                     cout << "***  Already Started... To start it again first stop it : press 10 ***" << std::endl;
218                 }
219             }
220             else if (userInput == 6)
221             {
222                 try
223                 {
224                     if (false == cachingFlag)
225                     {
226                         resource->startCaching(&OnCacheUpdated);
227                         cout << "**********  caching Started ***********" << std::endl;
228                     }
229                     else
230                     {
231                         cout << "***  Already Started... To start it again first stop it : press 10 ***" << std::endl;
232                     }
233                 }
234                 catch (InvalidParameterException e)
235                 {
236                     cout << "Exeception in startCaching :: " << e.what() << std::endl;
237                 }
238             }
239             else if (userInput == 7)
240             {
241
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;
255             }
256             else if (userInput == 8)
257             {
258                 try
259                 {
260                     ResourceAttributes atttribute = resource->getCachedAttributes();
261                     if (atttribute.empty())
262                     {
263                         cout << "Received cached attribute is empty" << std::endl;
264                     }
265                     else
266                     {
267                         ResourceAttributes::const_iterator iter = atttribute.begin();
268                         for (unsigned int i = 0; i < atttribute.size(); ++i)
269                         {
270                             std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
271                             ++iter;
272                         }
273                     }
274                 }
275                 catch (BadRequestException e)
276                 {
277                     cout << "getCachedAttributes exception : " << e.what() << std::endl;
278                 }
279             }
280             else if (userInput == 9)
281             {
282                 std::string key = "Temperature";
283                 try
284                 {
285                     ResourceAttributes::Value valueObj = resource->getCachedAttribute(key);
286                     int value = valueObj.get< int >();
287                     cout << "\nkey : " << key << "\nValue : " << value << std::endl;
288                 }
289                 catch (BadRequestException e)
290                 {
291                     cout << "getCachedAttribute exception : " << e.what() << std::endl;
292                 }
293                 catch (BadGetException e)
294                 {
295                     cout << "Exeception in getCachedAttribute  BadGetException:: " << e.what() << std::endl;
296                 }
297             }
298             else if (userInput == 10)
299             {
300                 resource->stopCaching();
301                 cachingFlag = false;
302                 cout << "****** Caching stopped ******" << std::endl;
303             }
304             else if (userInput == 11)
305             {
306                 isReady = false;
307                 isRun = false;
308             }
309             else
310             {
311                 cout << "***   Please enter the number between 1-11  ***" << std::endl;
312             }
313         }
314     }
315     return 0;
316 }
317