Modifying version number for building on tizen 3.0
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / groupaction / groupserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <OCPlatform.h>
22 #include <OCApi.h>
23
24 #include <functional>
25 #include <pthread.h>
26 #include <iostream>
27
28 #include <ThingsManager.h>
29
30 using namespace std;
31 using namespace OC;
32 using namespace OIC;
33 namespace PH = std::placeholders;
34
35 bool isReady = false;
36
37 OCResourceHandle resourceHandle;
38 std::vector< OCResourceHandle > resourceHandleVector;
39
40 shared_ptr< OCResource > g_resource;
41 vector< string > lights;
42
43 ThingsManager *thingsMgr = new ThingsManager();
44
45 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
46
47 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
48
49 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
50
51 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, const int& eCode,
52         const int& sequenceNumber);
53
54 void allBulbOn();
55 void allBulbOff();
56
57 void foundResources(std::vector< std::shared_ptr< OC::OCResource > > listOfResource)
58 {
59
60     for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end(); ++rsrc)
61     {
62         std::string resourceURI = (*rsrc)->uri();
63         std::string hostAddress = (*rsrc)->host();
64
65         if (resourceURI == "/a/light")
66         {
67
68             cout << "\tResource URI : " << resourceURI << endl;
69             cout << "\tResource Host : " << hostAddress << endl;
70
71             OCResourceHandle foundResourceHandle;
72             OCStackResult result = OCPlatform::registerResource(foundResourceHandle, (*rsrc));
73             cout << "\tresource registed!" << endl;
74             if (result == OC_STACK_OK)
75             {
76                 OCPlatform::bindResource(resourceHandle, foundResourceHandle);
77                 resourceHandleVector.push_back(foundResourceHandle);
78             }
79             else
80             {
81                 cout << "\tresource Error!" << endl;
82             }
83
84             lights.push_back((hostAddress + resourceURI));
85         }
86     }
87
88     isReady = true;
89 }
90
91 void foundResource(std::shared_ptr< OCResource > resource)
92 {
93     std::string resourceURI;
94     std::string hostAddress;
95
96     try
97     {
98         cout << "FOUND RESOURCE" << endl;
99
100         if (resource)
101         {
102             resourceURI = resource->uri();
103             hostAddress = resource->host();
104             if (resourceURI == "/core/a/collection")
105             {
106                 g_resource = resource;
107
108                 // g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
109
110                 printf("\tHOST :: %s\n", resource->host().c_str());
111             }
112             else if (resourceURI == "/core/bookmark")
113             {
114                 resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
115             }
116
117             // p_platform.bindResource(resourceHandle, foundResourceHandle);
118
119         }
120     }
121     catch (std::exception& e)
122     {
123         std::cout << "" << std::endl;
124     }
125 }
126
127 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
128 {
129     // std::vector<OCRepresentation> children = rep.getChildren();
130
131     // cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
132     // for( auto iter = children.begin(); iter != children.end();  ++iter )
133     // {
134     //     lights.push_back((*iter).getUri());
135     //     cout << "\tURI :: " << (*iter).getUri() << endl;
136     // }
137 }
138
139 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
140 {
141     printf("\nonPut\n");
142 }
143
144 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
145 {
146     printf("\nonPost\n");
147
148     if (rep.hasAttribute("ActionSet"))
149     {
150         std::string plainText;
151
152         if (rep.getValue("ActionSet", plainText))
153         {
154             ActionSet *actionset = thingsMgr->getActionSetfromString(plainText);
155             if (actionset != NULL)
156             {
157                 cout << endl << "\tACTIONSET NAME :: " << actionset->actionsetName << endl;
158                 for (auto actIter = actionset->listOfAction.begin();
159                         actIter != actionset->listOfAction.end(); ++actIter)
160                 {
161                     cout << "\t\tTARGET :: " << (*actIter)->target << endl;
162
163                     for (auto capaIter = (*actIter)->listOfCapability.begin();
164                             capaIter != (*actIter)->listOfCapability.end(); ++capaIter)
165                     {
166                         cout << "\t\t\t" << (*capaIter)->capability << " :: " << (*capaIter)->status
167                                 << endl;
168                     }
169                 }
170             }
171             delete actionset;
172         }
173
174         // printf( "\tPlain Text :: %s\n", plainText.c_str() );
175     }
176     else if (rep.hasAttribute("DoAction"))
177     {
178         std::string plainText;
179         if (rep.getValue("DoAction", plainText))
180         {
181             cout << "\t" << plainText << endl;
182         }
183     }
184     else
185     {
186
187     }
188 }
189
190 void allBulbOff()
191 {
192     OCRepresentation rep;
193
194     rep.setValue("DoAction", std::string("AllBulbOff"));
195
196     if (g_resource)
197     {
198         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), &onPost);
199     }
200 }
201
202 void allBulbOn()
203 {
204     OCRepresentation rep;
205
206     rep.setValue("DoAction", std::string("AllBulbOn"));
207
208     if (g_resource)
209     {
210         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), &onPost);
211     }
212 }
213
214 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, const int& eCode,
215         const int& sequenceNumber)
216 {
217     if (eCode == OC_STACK_OK)
218     {
219         int level;
220
221         std::cout << "OBSERVE RESULT:" << std::endl;
222         std::cout << "\tSequenceNumber: " << sequenceNumber << endl;
223
224         if (rep.getValue("level", level))
225         {
226             if (level == 0)
227             {
228                 allBulbOn();
229             }
230             else
231             {
232                 allBulbOff();
233             }
234         }
235         std::cout << "\tlevel: " << level << std::endl;
236     }
237     else
238     {
239         std::cout << "onObserve Response error: " << eCode << std::endl;
240         std::exit(-1);
241     }
242 }
243
244 void createActionSet_AllBulbOff()
245 {
246     string actionsetDesc;
247     ActionSet *allBulbOff = new ActionSet();
248     allBulbOff->actionsetName = "AllBulbOff";
249
250     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
251     {
252         Action *action = new Action();
253         action->target = (*iter);
254
255         Capability *capa = new Capability();
256         capa->capability = "power";
257         capa->status = "off";
258
259         action->listOfCapability.push_back(capa);
260         allBulbOff->listOfAction.push_back(action);
261     }
262     // actionsetDesc = thingsMgr->getStringFromActionSet(allBulbOff);
263
264     // cout << "ActionSet :: " << actionsetDesc << endl;
265
266     // OCRepresentation rep;
267     // rep.setValue("ActionSet", actionsetDesc);
268
269     if (g_resource)
270     {
271         thingsMgr->addActionSet(g_resource, allBulbOff, onPut);
272         // g_resource->put("a.collection", GROUP_INTERFACE, rep,
273         //     QueryParamsMap(), &onPut);
274     }
275
276     delete allBulbOff;
277 }
278
279 void createActionSet_AllBulbOn()
280 {
281     string actionsetDesc;
282     ActionSet *allBulbOff = new ActionSet();
283     allBulbOff->actionsetName = "AllBulbOn";
284
285     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
286     {
287         Action *action = new Action();
288         action->target = (*iter);
289
290         Capability *capa = new Capability();
291         capa->capability = "power";
292         capa->status = "on";
293
294         action->listOfCapability.push_back(capa);
295         allBulbOff->listOfAction.push_back(action);
296     }
297     // actionsetDesc = thingsMgr->getStringFromActionSet(allBulbOff);
298
299     // cout << "ActionSet :: " << actionsetDesc << endl;
300
301     // OCRepresentation rep;
302     // rep.setValue("ActionSet", actionsetDesc);
303
304     if (g_resource)
305     {
306         thingsMgr->addActionSet(g_resource, allBulbOff, onPut);
307         // g_resource->put("a.collection", GROUP_INTERFACE, rep,
308         //     QueryParamsMap(), &onPut);
309     }
310
311     delete allBulbOff;
312 }
313
314 int main()
315 {
316     PlatformConfig config
317     { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
318
319     try
320     {
321         string resourceURI = "/core/a/collection";
322         string resourceTypeName = "a.collection";
323         string resourceInterface = BATCH_INTERFACE;
324         OCPlatform::Configure(config);
325
326         // Find lights for group creation.
327         vector< string > types;
328         types.push_back("core.light");
329         thingsMgr->findCandidateResources(types, &foundResources, 5);
330
331         OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
332                 resourceInterface, NULL,
333                 //&entityHandler, // entityHandler
334                 OC_DISCOVERABLE);
335
336         cout << "registerResource is called." << endl;
337
338         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
339         OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
340
341         bool isRun = true;
342
343         while (isRun)
344         {
345             while (isReady)
346             {
347                 int n;
348
349                 cout << endl;
350                 cout << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTIONSET(ALLBULBON)"
351                         << "3 :: EXECUTE ACTIONSET(ALLBULBOFF)" << endl;
352                 cout << "4 :: GET ACTIONSET 5 :: DELETE ACTIONSET 6 :: QUIT" << endl;
353                 cout << "9 :: FIND GROUP 0 :: FIND BOOKMARK TO OBSERVE" << endl;
354
355                 fflush(stdin);
356                 cin >> n;
357
358                 if (n == 9)
359                 {
360                     OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection",
361                             &foundResource);
362                 }
363                 else if (n == 0)
364                 {
365                     OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.bookmark",
366                             &foundResource);
367                 }
368                 else if (n == 1)
369                 {
370
371                     // Craete Action Set
372                     // "AllBulbOff"
373                     //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10";
374                     createActionSet_AllBulbOff();
375                     createActionSet_AllBulbOn();
376
377                 }
378                 else if (n == 2)
379                 {
380
381                     allBulbOn();
382                     // thingsMgr->executeActionSet(g_resource, "AllBulbOn", onPost);
383
384                 }
385                 else if (n == 3)
386                 {
387
388                     allBulbOff();
389                     // thingsMgr->executeActionSet(g_resource, "AllBulbOff", onPost);
390
391                 }
392                 else if (n == 4)
393                 {
394                     // OCRepresentation rep;
395
396                     // rep.setValue("GetActionSet", std::string("AllBulbOff"));
397
398                     // if(g_resource)
399                     // {
400                     //     g_resource->post("a.collection", GROUP_INTERFACE, rep,
401                     //         QueryParamsMap(), &onPost);
402                     // }
403
404                     thingsMgr->getActionSet(g_resource, "AllBulbOff", onPost);
405                 }
406                 else if (n == 5)
407                 {
408                     // OCRepresentation rep;
409
410                     // rep.setValue("DelActionSet", std::string("AllBulbOff"));
411
412                     // if(g_resource)
413                     // {
414                     //     g_resource->put("a.collection", GROUP_INTERFACE, rep,
415                     //         QueryParamsMap(), &onPut);
416                     // }
417                     thingsMgr->deleteActionSet(g_resource, "AllBulbOff", onPut);
418                 }
419                 else if (n == 6)
420                 {
421                     isRun = false;
422                     break;
423                 }
424             }
425         }
426     }
427     catch (OCException& e)
428     {
429
430     }
431
432     return 0;
433 }