Imported Upstream version 1.0.1
[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 "timer.h"
29
30 #include <GroupManager.h>
31
32 using namespace std;
33 using namespace OC;
34 using namespace OIC;
35 namespace PH = std::placeholders;
36
37 bool isReady = false;
38
39 OCResourceHandle resourceHandle;
40 std::vector<OCResourceHandle> resourceHandleVector;
41
42 shared_ptr<OCResource> g_resource;
43 vector<string> lights;
44
45 GroupManager *groupMgr = new GroupManager();
46
47 void onGet(const HeaderOptions& opt, const OCRepresentation &rep,
48         const int eCode);
49
50 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep,
51         const int eCode);
52
53 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep,
54         const int eCode);
55
56 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
57         const int& eCode, const int& sequenceNumber);
58
59 void allBulbOn();
60 void allBulbOff();
61
62 shared_ptr<OCResource> g_light;
63
64 void foundResources(
65         std::vector<std::shared_ptr<OC::OCResource> > listOfResource)
66 {
67
68     for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end();
69             ++rsrc)
70     {
71         std::string resourceURI = (*rsrc)->uri();
72         std::string hostAddress = (*rsrc)->host();
73
74         if (resourceURI == "/a/light")
75         {
76             cout << "\tResource URI : " << resourceURI << endl;
77             cout << "\tResource Host : " << hostAddress << endl;
78
79             OCResourceHandle foundResourceHandle;
80             OCStackResult result = OCPlatform::registerResource(
81                     foundResourceHandle, (*rsrc));
82             cout << "\tresource registed!" << endl;
83             if (result == OC_STACK_OK)
84             {
85                 OCPlatform::bindResource(resourceHandle, foundResourceHandle);
86                 resourceHandleVector.push_back(foundResourceHandle);
87             }
88             else
89             {
90                 cout << "\tresource Error!" << endl;
91             }
92
93             lights.push_back((hostAddress + resourceURI));
94
95             g_light = (*rsrc);
96         }
97     }
98
99     isReady = true;
100 }
101
102 void foundResource(std::shared_ptr<OCResource> resource)
103 {
104     std::string resourceURI;
105     std::string hostAddress;
106
107     cout << "FOUND RESOURCE" << endl;
108
109     try
110     {
111         if (resource)
112         {
113             resourceURI = resource->uri();
114             hostAddress = resource->host();
115             if (resourceURI == "/core/a/collection")
116             {
117                 g_resource = resource;
118
119                 // g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
120
121                 cout << "FOUND " << resourceURI << endl;
122                 // printf("\tHOST :: %s\n", resource->host().c_str());
123             }
124             else if (resourceURI == "/core/bookmark")
125             {
126                 resource->observe(ObserveType::Observe, QueryParamsMap(),
127                         &onObserve);
128             }
129         }
130     }
131     catch (std::exception& e)
132     {
133         std::cout << "Exception: " << e.what() << std::endl;
134     }
135 }
136
137 void onGet(const HeaderOptions& /*opt*/, const OCRepresentation &/*rep*/,
138         const int eCode)
139 {
140     cout << "\nonGet" << endl;
141     if (eCode == OC_STACK_OK)
142         cout << "\tResult is OK." << endl;
143     else
144         cout << "\tInvalid parameter." << endl;
145 }
146
147 void onPut(const HeaderOptions& /*opt*/, const OCRepresentation &/*rep*/,
148         const int eCode)
149 {
150     cout << "\nonPut" << endl;
151     if (eCode == OC_STACK_OK)
152         cout << "\tResult is OK." << endl;
153     else
154         cout << "\tInvalid parameter." << endl;
155 }
156
157 void onPost(const HeaderOptions& /*opt*/, const OCRepresentation &rep,
158         const int /*eCode*/)
159 {
160     printf("\nonPost\n");
161
162     if (rep.hasAttribute("ActionSet"))
163     {
164         std::string plainText;
165
166         if (rep.getValue("ActionSet", plainText))
167         {
168             ActionSet *actionset = groupMgr->getActionSetfromString(plainText);
169             if (actionset != NULL)
170             {
171                 cout << endl << "\tACTIONSET NAME :: "
172                         << actionset->actionsetName << endl;
173                 for (auto actIter = actionset->listOfAction.begin();
174                         actIter != actionset->listOfAction.end(); ++actIter)
175                 {
176                     cout << "\t\tTARGET :: " << (*actIter)->target << endl;
177
178                     for (auto capaIter = (*actIter)->listOfCapability.begin();
179                             capaIter != (*actIter)->listOfCapability.end();
180                             ++capaIter)
181                     {
182                         cout << "\t\t\t" << (*capaIter)->capability << " :: "
183                                 << (*capaIter)->status << endl;
184                     }
185                 }
186             }
187             delete actionset;
188         }
189
190         // printf( "\tPlain Text :: %s\n", plainText.c_str() );
191     }
192     else if (rep.hasAttribute("DoAction"))
193     {
194         std::string plainText;
195         if (rep.getValue("DoAction", plainText))
196         {
197             cout << "\t" << plainText << endl;
198         }
199     }
200     else
201     {
202
203     }
204 }
205
206 void allBulbOff()
207 {
208     OCRepresentation rep;
209
210     rep.setValue("DoAction", std::string("AllBulbOff"));
211
212     if (g_resource)
213     {
214         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
215                 &onPost);
216     }
217 }
218
219 void allBulbOn()
220 {
221     OCRepresentation rep;
222
223     rep.setValue("DoAction", std::string("AllBulbOn"));
224
225     if (g_resource)
226     {
227         OCStackResult res = g_resource->post("a.collection", GROUP_INTERFACE,
228             rep, QueryParamsMap(), &onPost);
229
230         if( res != OC_STACK_OK )
231             cout << "failed" << endl;
232     }
233 }
234
235 void Scheduled_AllbulbOff()
236 {
237     groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
238 }
239 void Scheduled_AllbulbOffEx()
240 {
241     groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", 10, &onPost);
242 }
243 void CancelScheduled_AllBulbOff()
244 {
245     groupMgr->cancelActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
246 }
247 void Recursive_allBulbOn()
248 {
249     groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
250 }
251 void Recursive_allBulbOnEx()
252 {
253     groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", 10, &onPost);
254 }
255
256 void CancelRecursive_allBulbOn()
257 {
258
259     groupMgr->cancelActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
260 }
261
262 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
263         const int& eCode, const int& sequenceNumber)
264 {
265     if (eCode == OC_STACK_OK)
266     {
267         int level;
268
269         std::cout << "OBSERVE RESULT:" << std::endl;
270         std::cout << "\tSequenceNumber: " << sequenceNumber << endl;
271
272         if (rep.getValue("level", level))
273         {
274             if (level == 0)
275             {
276                 allBulbOn();
277             }
278             else
279             {
280                 allBulbOff();
281             }
282         }
283         std::cout << "\tlevel: " << level << std::endl;
284     }
285     else
286     {
287         std::cout << "onObserve Response error: " << eCode << std::endl;
288         std::exit(-1);
289     }
290 }
291
292 void createActionSet_AllBulbOff()
293 {
294     string actionsetDesc;
295     ActionSet *allBulbOff = new ActionSet();
296     allBulbOff->actionsetName = "AllBulbOff";
297
298     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
299     {
300         Action *action = new Action();
301         action->target = (*iter);
302
303         Capability *capa = new Capability();
304         capa->capability = "power";
305         capa->status = "off";
306
307         action->listOfCapability.push_back(capa);
308         allBulbOff->listOfAction.push_back(action);
309     }
310     if (g_resource)
311     {
312         groupMgr->addActionSet(g_resource, allBulbOff, onPut);
313     }
314
315     delete allBulbOff;
316 }
317
318 void createActionSet_AllBulbOn()
319 {
320     string actionsetDesc;
321     ActionSet *allBulbOff = new ActionSet();
322     allBulbOff->actionsetName = "AllBulbOn";
323
324     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
325     {
326         Action *action = new Action();
327         action->target = (*iter);
328
329         Capability *capa = new Capability();
330         capa->capability = "power";
331         capa->status = "on";
332
333         action->listOfCapability.push_back(capa);
334         allBulbOff->listOfAction.push_back(action);
335     }
336     if (g_resource)
337     {
338         groupMgr->addActionSet(g_resource, allBulbOff, onPut);
339     }
340
341     delete allBulbOff;
342 }
343
344 void createScheduledActionSet_AllBulbOff()
345 {
346     string actionsetDesc;
347     ActionSet *allBulbOff = new ActionSet();
348     allBulbOff->type = OIC::ACTIONSET_TYPE::SCHEDULED;
349     allBulbOff->actionsetName = "AllBulbOffScheduledCall";
350
351     printf("ENTER(YYYY-MM-DD hh:mm:ss) :: ");
352     int res = scanf("%d-%d-%d %d:%d:%d", &allBulbOff->mTime.tm_year,
353             &allBulbOff->mTime.tm_mon, &allBulbOff->mTime.tm_mday,
354             &allBulbOff->mTime.tm_hour, &allBulbOff->mTime.tm_min,
355             &allBulbOff->mTime.tm_sec);
356     if( res < 0 )
357     {
358         printf("Invalid Input. try again.");
359         delete allBulbOff;
360         return;
361     }
362
363     allBulbOff->setDelay(allBulbOff->getSecondsFromAbsoluteTime());
364     printf("DELAY :: %ld\n", allBulbOff->getSecondsFromAbsoluteTime());
365
366     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
367     {
368         Action *action = new Action();
369         action->target = (*iter);
370
371         Capability *capa = new Capability();
372         capa->capability = "power";
373         capa->status = "off";
374
375         action->listOfCapability.push_back(capa);
376         allBulbOff->listOfAction.push_back(action);
377     }
378     if (g_resource)
379     {
380         groupMgr->addActionSet(g_resource, allBulbOff, onPut);
381     }
382
383     delete allBulbOff;
384 }
385
386 void createRecursiveActionSet_AllBulbOn()
387 {
388     string actionsetDesc;
389     ActionSet *allBulbOn = new ActionSet();
390     allBulbOn->type = OIC::ACTIONSET_TYPE::RECURSIVE;
391
392     allBulbOn->actionsetName = "AllBulbOnRecursiveCall";
393     allBulbOn->mTime.tm_year = 0;
394     allBulbOn->mTime.tm_mon = 0;
395     allBulbOn->mTime.tm_mday = 0;
396     allBulbOn->mTime.tm_hour = 0;
397     allBulbOn->mTime.tm_min = 0;
398     allBulbOn->mTime.tm_sec = 5;
399
400     allBulbOn->setDelay(allBulbOn->getSecAbsTime());
401
402     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
403     {
404         Action *action = new Action();
405         action->target = (*iter);
406
407         Capability *capa = new Capability();
408         capa->capability = "power";
409         capa->status = "on";
410
411         action->listOfCapability.push_back(capa);
412         allBulbOn->listOfAction.push_back(action);
413     }
414     if (g_resource)
415     {
416         groupMgr->addActionSet(g_resource, allBulbOn, onPut);
417     }
418
419     delete allBulbOn;
420 }
421
422 int main()
423 {
424     PlatformConfig config
425     { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0,
426             OC::QualityOfService::LowQos };
427
428     try
429     {
430         string resourceURI = "/core/a/collection";
431         string resourceTypeName = "a.collection";
432         string resourceInterface = BATCH_INTERFACE;
433         OCPlatform::Configure(config);
434
435         // Find lights for group creation.
436         vector<string> types;
437         types.push_back("core.light");
438         groupMgr->findCandidateResources(types, &foundResources, 5);
439
440         OCStackResult res = OCPlatform::registerResource(resourceHandle, resourceURI,
441                 resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE);
442
443         if( res != OC_STACK_OK )
444         {
445             cout << "Resource registeration failed." << endl;
446             return 0;
447         }
448
449         cout << "registerResource is called." << endl;
450
451         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
452         OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
453
454         bool isRun = true;
455
456         while (isRun)
457         {
458             while (isReady)
459             {
460                 int n;
461
462                 cout << endl;
463                 cout << "1 :: CREATE ACTIONSET" << endl;
464                 cout << "2 :: EXECUTE ACTIONSET(ALLBULBON)" << endl;
465                 cout << "3 :: EXECUTE ACTIONSET(ALLBULBOFF)" << endl;
466                 cout << "4 :: CREATE ACTIONSET(R_ALLBULBON)" << endl;
467                 cout << "\t41 :: EXECUTE ACTIONSET 42 :: CANCEL ACTIONSET" << endl;
468                 cout << "5 :: CREATE ACTIONSET(S_ALLBULBON)" << endl;
469                 cout << "\t51 :: EXECUTE ACTIONSET 52 :: CANCEL ACTIONSET" << endl;
470                 cout << "6 :: GET ACTIONSET" << endl;
471                 cout << "7 :: DELETE ACTIONSET" << endl;
472                 cout << "8 :: QUIT" << endl;
473                 cout << "9 :: FIND GROUP" << endl;
474                 cout << "0 :: FIND BOOKMARK TO OBSERVE"
475                         << endl;
476
477                 //fflush(stdin);
478                 cin >> n;
479
480                 if (n == 9)
481                 {
482                     std::string query = OC_RSRVD_WELL_KNOWN_URI;
483                     query.append("?rt=");
484                     query.append(resourceTypeName);
485
486                     OCPlatform::findResource("",
487                             query,
488                             CT_DEFAULT,
489                             &foundResource);
490
491                     // OCPlatform::findResource("",
492                     //         query,
493                     //         OC_WIFI,
494                     //         &foundResource);
495                 }
496                 else if (n == 0)
497                 {
498                     std::string query = OC_RSRVD_WELL_KNOWN_URI;
499                     query.append("?rt=");
500                     query.append("core.bookmark");
501
502                     OCPlatform::findResource("",
503                             query,
504                             CT_DEFAULT,
505                             &foundResource);
506                     // OCPlatform::findResource("",
507                     //         query,
508                     //         OC_WIFI,
509                     //         &foundResource);
510                 }
511                 else if (n == 1)
512                 {
513                     createActionSet_AllBulbOff();
514                     createActionSet_AllBulbOn();
515                 }
516                 else if (n == 2)
517                 {
518                     allBulbOn();
519                 }
520                 else if (n == 3)
521                 {
522                     allBulbOff();
523                 }
524                 else if (n == 4)
525                 {
526                     createRecursiveActionSet_AllBulbOn();
527                 }
528                 else if (n == 41)
529                 {
530                     Recursive_allBulbOn();
531                 }
532                 else if (n == 42)
533                 {
534                     CancelRecursive_allBulbOn();
535                 }
536                 // Exampel of
537                 else if (n == 43)
538                 {
539                     Recursive_allBulbOnEx();
540                 }
541                 else if (n == 5)
542                 {
543                     createScheduledActionSet_AllBulbOff();
544                 }
545                 else if (n == 51)
546                 {
547                     Scheduled_AllbulbOff();
548                 }
549                 else if (n == 52)
550                 {
551                     CancelScheduled_AllBulbOff();
552                 }
553                 else if (n == 53)
554                 {
555                     Scheduled_AllbulbOffEx();
556                 }
557                 else if (n == 6)
558                 {
559                     groupMgr->getActionSet(g_resource, "AllBulbOff", onPost);
560                 }
561                 else if (n == 7)
562                 {
563                     groupMgr->deleteActionSet(g_resource, "AllBulbOff", onPut);
564                 }
565                 else if (n == 8)
566                 {
567                     isRun = false;
568                     break;
569                 }
570                 else if(n == 100)
571                 {
572
573                     OCRepresentation rep;
574
575                     rep.setValue("power", std::string("on"));
576
577                     g_light->put(rep, QueryParamsMap(), &onPut);
578
579                 }
580             }
581         }
582         usleep(500*1000);
583     }
584     catch (OCException& e)
585     {
586         cout << "ERROR :: " << e.reason() << endl;
587     }
588
589     return 0;
590 }