1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 #include "tmsampleapp.h"
30 namespace PH = std::placeholders;
32 static Evas_Object *log_entry = NULL;
33 static Evas_Object *list = NULL;
34 static Evas_Object *naviframe = NULL;
36 string BULBOFF = "AllBulbOff";
37 string BULBON = "AllBulbOn";
38 string resourceURI = "/core/b/collection";
39 string resourceTypeName = "b.collection";
41 std::vector< OCResourceHandle > groupResourceHandleVector;
42 OCResourceHandle resourceHandle = NULL;
43 OCResourceHandle foundResourceHandle = NULL;
44 shared_ptr< OCResource > g_resource ;
45 std::vector< string > lights;
47 GroupManager *groupMgr = NULL;
49 typedef struct datetime_popup
53 } datetime_popup_fields;
55 // Function to update the log in UI
56 void *updateGroupLog(void *data)
58 string *log = (string *)data;
60 elm_entry_entry_append(log_entry, (*log).c_str());
61 elm_entry_cursor_end_set(log_entry);
65 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
68 dlog_print(DLOG_INFO, LOG_TAG, "#### onPut Callback Received!!!!");
69 logMessage += "API Result: Success<br>onPut Callback Received<br>";
70 if (OC_STACK_OK == eCode)
72 dlog_print(DLOG_INFO, LOG_TAG, "#### Result is OK");
76 dlog_print(DLOG_INFO, LOG_TAG, "#### Invalid Parameter");
78 logMessage += "----------------------<br>";
79 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
80 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
84 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
86 dlog_print(DLOG_INFO, LOG_TAG, "#### onPost callback received ENTRY!!!!");
88 logMessage += "API Result: Success<br>onPost Callback Received<br>";
90 if (rep.hasAttribute("ActionSet"))
93 if (rep.getValue("ActionSet", plainText))
95 ActionSet *actionset = groupMgr->getActionSetfromString(plainText);
96 if (NULL != actionset)
98 dlog_print(DLOG_INFO, LOG_TAG, "#### ACTIONSET NAME :: (%s)",
99 actionset->actionsetName.c_str());
100 logMessage += "ACTIONSET NAME :: " + actionset->actionsetName + "<br>";
101 for (auto actIter = actionset->listOfAction.begin();
102 actIter != actionset->listOfAction.end(); ++actIter)
104 dlog_print(DLOG_INFO, LOG_TAG, "#### TARGET :: (%s)",
105 (*actIter)->target.c_str());
106 logMessage += logMessage + "TARGET :: " + (*actIter)->target + "<br>";
107 for (auto capaIter = (*actIter)->listOfCapability.begin();
108 capaIter != (*actIter)->listOfCapability.end(); ++capaIter)
110 dlog_print(DLOG_INFO, LOG_TAG, "#### POWER :: (%s)",
111 (*capaIter)->status.c_str());
112 logMessage += logMessage + "CAPABILITY :: " +
113 (*capaIter)->status + "<br>";
120 else if (rep.hasAttribute("DoAction"))
123 if (rep.getValue("DoAction", plainText))
125 logMessage += plainText + "<br>";
126 dlog_print(DLOG_INFO, LOG_TAG, "#### DO ACTION :: (%s)", plainText.c_str());
130 logMessage += "----------------------<br>";
131 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
132 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
134 dlog_print(DLOG_INFO, LOG_TAG, "#### onPost callback received EXIT!!!!");
137 // Method for Creating the action Set AllBulbOff
138 static void createActionSet_AllBulbOff()
140 dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOff ENTRY");
142 OIC::ActionSet *actionSet = new OIC::ActionSet();
143 actionSet->actionsetName = BULBOFF;
144 int size = lights.size();
148 string logMessage = "NO LIGHTSERVER FOUND <br>";
149 logMessage += "----------------------<br>";
150 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
151 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
152 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
158 for (int i = 0; i < size; i++)
160 OIC::Action *action = new OIC::Action();
161 action->target = lights.at(i);
163 OIC::Capability *capability = new OIC::Capability();
164 capability->capability = "power";
165 capability->status = "off";
167 action->listOfCapability.push_back(capability);
168 actionSet->listOfAction.push_back(action);
170 dlog_print(DLOG_INFO, LOG_TAG, "#### G_URI: %s", g_resource->uri().c_str());
171 dlog_print(DLOG_INFO, LOG_TAG, "#### G_HOST: %S", g_resource->host().c_str());
177 groupMgr->addActionSet(g_resource, actionSet, onPut);
180 catch (std::exception &e)
182 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
186 string logMessage = "Create actionset AllBulbOFF success <br>";
187 logMessage += "----------------------<br>";
188 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
189 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
190 dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOff EXIT");
193 /* Method for Creating the action Set AllBulbOn
194 once we create the ActionSet we can execute Action Set using executeActionSetOn()
195 or delete using deleteActionSetOn() */
196 static void createActionSet_AllBulbOn()
198 dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOn ENTRY");
200 OIC::ActionSet *actionSet = new OIC::ActionSet();
201 actionSet->actionsetName = BULBON;
202 int size = lights.size();
206 string logMessage = "NO LIGHTSERVER FOUND <br>";
207 logMessage += "----------------------<br>";
208 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
209 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
214 for (int i = 0; i < size; i++)
216 OIC::Action *action = new OIC::Action();
217 action->target = lights.at(i);
219 OIC::Capability *capability = new OIC::Capability();
220 capability->capability = "power";
221 capability->status = "on";
223 action->listOfCapability.push_back(capability);
224 actionSet->listOfAction.push_back(action);
226 string URI = g_resource->uri();
227 string host = g_resource->host();
228 dlog_print(DLOG_INFO, LOG_TAG, "#### G_URI: %s", g_resource->uri().c_str());
229 dlog_print(DLOG_INFO, LOG_TAG, "#### G_HOST: %S", g_resource->host().c_str());
235 groupMgr->addActionSet(g_resource, actionSet, onPut);
238 catch (std::exception &e)
240 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
244 string logMessage = "Create actionset AllBulbON success <br>";
245 logMessage += "----------------------<br>";
246 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
247 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
248 dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOff OFF EXIT");
251 static void createRecursiveActionSet_AllBulbOn(void *data, Evas_Object *obj, void *event_info)
253 dlog_print(DLOG_INFO, LOG_TAG, "#### createRecursiveActionSet_AllBulbOn ENTRY");
254 string actionsetDesc;
255 ActionSet *allBulbOn = new ActionSet();
256 allBulbOn->type = OIC::ACTIONSET_TYPE::RECURSIVE;
258 allBulbOn->actionsetName = "AllBulbOnRecursiveCall";
259 allBulbOn->mTime.tm_year = 0;
260 allBulbOn->mTime.tm_mon = 0;
261 allBulbOn->mTime.tm_mday = 0;
262 allBulbOn->mTime.tm_hour = 0;
263 allBulbOn->mTime.tm_min = 0;
264 allBulbOn->mTime.tm_sec = 5;
266 allBulbOn->setDelay(allBulbOn->getSecAbsTime());
268 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
270 Action *action = new Action();
271 action->target = (*iter);
273 Capability *capa = new Capability();
274 capa->capability = "power";
277 action->listOfCapability.push_back(capa);
278 allBulbOn->listOfAction.push_back(action);
282 groupMgr->addActionSet(g_resource, allBulbOn, onPut);
286 dlog_print(DLOG_INFO, LOG_TAG, "#### createRecursiveActionSet_AllBulbOn EXIT");
289 static void createScheduledActionSet_AllBulbOff(int date, int month, int year,
290 int hour, int minute, int second)
292 dlog_print(DLOG_INFO, LOG_TAG, "#### createScheduledActionSet_AllBulbOff ENTRY");
293 string actionsetDesc;
294 ActionSet *allBulbOff = new ActionSet();
295 allBulbOff->type = OIC::ACTIONSET_TYPE::SCHEDULED;
296 allBulbOff->actionsetName = "AllBulbOffScheduledCall";
297 allBulbOff->mTime.tm_year = year;
298 allBulbOff->mTime.tm_mon = month;
299 allBulbOff->mTime.tm_mday = date;
300 allBulbOff->mTime.tm_hour = hour;
301 allBulbOff->mTime.tm_min = minute;
302 allBulbOff->mTime.tm_sec = second;
303 dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_year :: %ld",
304 allBulbOff->mTime.tm_year);
305 dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_mon :: %ld",
306 allBulbOff->mTime.tm_mon);
307 dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_mday :: %ld",
308 allBulbOff->mTime.tm_mday);
309 dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_hour :: %ld",
310 allBulbOff->mTime.tm_hour);
311 dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_min :: %ld",
312 allBulbOff->mTime.tm_min);
313 dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_sec :: %ld",
314 allBulbOff->mTime.tm_sec);
316 allBulbOff->setDelay(allBulbOff->getSecondsFromAbsoluteTime());
318 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
320 Action *action = new Action();
321 action->target = (*iter);
323 Capability *capa = new Capability();
324 capa->capability = "power";
325 capa->status = "off";
327 action->listOfCapability.push_back(capa);
328 allBulbOff->listOfAction.push_back(action);
332 groupMgr->addActionSet(g_resource, allBulbOff, onPut);
336 dlog_print(DLOG_INFO, LOG_TAG, "#### createScheduledActionSet_AllBulbOff EXIT");
339 static void scheduled_AllbulbOff(void *data, Evas_Object *obj, void *event_info)
341 groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
344 static void scheduled_AllbulbOffEx(void *data, Evas_Object *obj, void *event_info)
346 groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", 10, &onPost);
349 static void cancelScheduled_AllBulbOff(void *data, Evas_Object *obj, void *event_info)
351 groupMgr->cancelActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
354 static void recursive_allBulbOn(void *data, Evas_Object *obj, void *event_info)
356 groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
359 static void recursive_allBulbOnEx(void *data, Evas_Object *obj, void *event_info)
361 groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", 10, &onPost);
364 static void cancelRecursive_allBulbOn(void *data, Evas_Object *obj, void *event_info)
367 groupMgr->cancelActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
370 /* Method for executing the action Set AllBulbOff that we have created using
371 createActionSet_AllBulbOff() */
372 static void executeActionSetOff(void *data, Evas_Object *obj, void *event_info)
374 dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOff ENTRY");
375 int size = lights.size();
379 string logMessage = "NO LIGHTSERVER FOUND <br>";
380 logMessage += "----------------------<br>";
381 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
382 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
390 groupMgr->executeActionSet(g_resource, BULBOFF, &onPost);
393 catch (std::exception &e)
395 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
398 string logMessage = "Actionset OFF called successfully <br>";
399 logMessage += "----------------------<br>";
400 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
401 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
402 dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOff EXIT");
405 /* Method for executing the action Set AllBulbOn that we have created using
406 createActionSet_AllBulbOn() */
407 static void executeActionSetOn(void *data, Evas_Object *obj, void *event_info)
409 dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOn ENTRY");
410 int size = lights.size();
414 string logMessage = "NO LIGHTSERVER FOUND <br>";
415 logMessage += "----------------------<br>";
416 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
417 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
425 groupMgr->executeActionSet(g_resource, BULBON, &onPost);
428 catch (std::exception &e)
430 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
433 string logMessage = "Actionset ON called successfully <br>";
434 logMessage += "----------------------<br>";
435 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
436 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
437 dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOn EXIT");
440 /* Method for getting the action Set AllBulbOff that we have created using
441 createActionSet_AllBulbOff() */
442 static void getActionSetOff(void *data, Evas_Object *obj, void *event_info)
444 dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOff ENTRY");
445 int size = lights.size();
449 string logMessage = "NO LIGHTSERVER FOUND <br>";
450 logMessage += "----------------------<br>";
451 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
452 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
460 groupMgr->getActionSet(g_resource, BULBOFF, &onPost);
463 catch (std::exception &e)
465 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
468 dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOff EXIT");
471 /* Method for getting the action Set AllBulbOn that we have created using
472 createActionSet_AllBulbOn() */
473 static void getActionSetOn(void *data, Evas_Object *obj, void *event_info)
475 dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOn ENTRY");
476 int size = lights.size();
480 string logMessage = "NO LIGHTSERVER FOUND <br>";
481 logMessage += "----------------------<br>";
482 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
483 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
491 groupMgr->getActionSet(g_resource, BULBON, &onPost);
494 catch (std::exception &e)
496 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
499 dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOn EXIT");
502 /* Method for deleting the action Set AllBulbOff that we have created using
503 createActionSet_AllBulbOff() */
504 static void deleteActionSetOff(void *data, Evas_Object *obj, void *event_info)
506 dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOff ENTRY");
507 int size = lights.size();
511 string logMessage = "NO LIGHTSERVER FOUND <br>";
512 logMessage += "----------------------<br>";
513 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
514 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
522 groupMgr->deleteActionSet(g_resource, BULBOFF, &onPost);
525 catch (std::exception &e)
527 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
530 string logMessage = "Actionset OFF DELETED <br>";
531 logMessage += "----------------------<br>";
532 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
533 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
534 dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOff EXIT");
537 /* Method for deleting the action Set AllBulbOn that we have created using
538 createActionSet_AllBulbOn() */
539 static void deleteActionSetOn(void *data, Evas_Object *obj, void *event_info)
541 dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOn ENTRY");
542 int size = lights.size();
546 string logMessage = "NO LIGHTSERVER FOUND <br>";
547 logMessage += "----------------------<br>";
548 dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
549 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
557 groupMgr->deleteActionSet(g_resource, BULBON, &onPost);
560 catch (std::exception &e)
562 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
565 string logMessage = "Actionset ON DELETED <br>";
566 logMessage += "----------------------<br>";
567 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
568 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
569 dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOn EXIT");
572 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep, const int &eCode,
573 const int &sequenceNumber)
577 if (OC_STACK_OK == eCode)
580 buf = (char *)malloc(4 * sizeof(char));
583 dlog_print(DLOG_INFO, LOG_TAG, " buf malloc failed");
586 sprintf(buf, "%d", sequenceNumber);
587 logMessage = "OBSERVE RESULT <br>";
588 logMessage += "Sequencenumber:" + string(buf) + "<br>";
590 if (rep.getValue("level", level))
594 createActionSet_AllBulbOn();
595 executeActionSetOn(NULL, NULL, NULL);
599 createActionSet_AllBulbOff();
600 executeActionSetOff(NULL, NULL, NULL);
603 sprintf(buf, "%d", level);
604 logMessage += "level:" + string(buf) + "<br>";
609 logMessage = "onObserve error!!!";
611 logMessage += "----------------------<br>";
612 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
613 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
617 // Callback to be called when resources are found in the network
618 void foundResources(std::vector< std::shared_ptr< OC::OCResource > > listOfResource)
622 dlog_print(DLOG_INFO, LOG_TAG, "#### foundResources entry!!!!");
624 for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end(); ++rsrc)
626 string resourceURI = (*rsrc)->uri();
627 string hostAddress = (*rsrc)->host();
629 dlog_print(DLOG_INFO, LOG_TAG, "#### found uri: %s", resourceURI.c_str());
630 dlog_print(DLOG_INFO, LOG_TAG, "#### found host address: %s", hostAddress.c_str());
631 string logMessage = "URI: " + resourceURI + "<br>";
632 logMessage = logMessage + "Host:" + hostAddress + "<br>";
633 logMessage += "----------------------<br>";
635 if (resourceURI == "/a/light")
638 found = std::find(lights.begin(), lights.end(),
639 hostAddress + resourceURI) != lights.end();
642 lights.push_back((hostAddress + resourceURI));
646 dlog_print(DLOG_INFO, LOG_TAG, "#### Registering Resource");
647 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
649 dlog_print(DLOG_INFO, LOG_TAG, "#### %s REGISTERED", resourceURI.c_str());
650 if (result == OC_STACK_OK)
652 OCPlatform::bindResource(resourceHandle, foundResourceHandle);
653 dlog_print(DLOG_INFO, LOG_TAG, "#### Bind Resource Done");
654 groupResourceHandleVector.push_back(foundResourceHandle);
658 dlog_print(DLOG_ERROR, LOG_TAG, "#### Register Resource ERROR");
661 catch (std::exception &e)
663 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
667 else if (resourceURI == "/core/bookmark")
669 logMessage += "OBSERVING : " + resourceURI + "<br>";
670 logMessage += "----------------------<br>";
671 (*rsrc)->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
672 dlog_print(DLOG_INFO, LOG_TAG, "#### after calling observe() for bookmark!!!!");
674 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
675 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
679 dlog_print(DLOG_INFO, LOG_TAG, "#### foundResources exit!!!!");
683 dlog_print(DLOG_INFO, LOG_TAG, "#### Exception caught in foundResources");
687 static void create_group()
690 groupMgr = new GroupManager();
692 if (NULL != groupMgr)
694 dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources from "
696 vector< string > types;
697 types.push_back("core.light");
698 groupMgr->findCandidateResources(types, &foundResources, FINDRESOURCE_TIMEOUT);
703 dlog_print(DLOG_INFO, LOG_TAG, "#### calling registerResource from create_group!!!!");
704 OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
705 BATCH_INTERFACE, NULL,
706 OC_DISCOVERABLE | OC_OBSERVABLE);
708 if (NULL != resourceHandle)
709 dlog_print(DLOG_INFO, LOG_TAG, "#### Obtained resourceHandle from "
710 "registerResource!!!!");
712 dlog_print(DLOG_INFO, LOG_TAG, "#### calling bindInterfaceToResource from "
714 OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
715 OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
717 catch (std::exception &e)
719 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
723 // Method for Finding the Light Resource
724 void findLightResource(void *data, Evas_Object *obj, void *event_info)
726 dlog_print(DLOG_INFO, LOG_TAG, "#### findLightResource ENTRY");
727 if (NULL != groupMgr)
729 vector< string > types;
730 types.push_back("core.light");
731 OCStackResult result = groupMgr->findCandidateResources(types, &foundResources,
732 FINDRESOURCE_TIMEOUT);
733 if (result == OC_STACK_OK)
735 dlog_print(DLOG_INFO, LOG_TAG, "#### create_group -- findCandidateResources :: "
740 dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources failed!!");
743 dlog_print(DLOG_INFO, LOG_TAG, "#### findLightResource EXIT");
746 // Method for observing the Bookmark Resource
747 void observeBookMark(void *data, Evas_Object *obj, void *event_info)
749 dlog_print(DLOG_INFO, LOG_TAG, "#### observeBookMark ENTRY");
750 if (NULL != groupMgr)
752 vector< string > types;
753 types.push_back("core.bookmark");
754 OCStackResult result = groupMgr->findCandidateResources(types, &foundResources,
755 FINDRESOURCE_TIMEOUT);
756 if (OC_STACK_OK == result)
758 dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources :: "
763 dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources failed!!");
766 dlog_print(DLOG_INFO, LOG_TAG, "#### observeBookMark EXIT");
769 static void onDestroy()
771 dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
775 if (NULL != foundResourceHandle)
777 OCPlatform::unregisterResource(foundResourceHandle);
778 dlog_print(DLOG_INFO, LOG_TAG, "#### Light Resource unregistered");
783 dlog_print(DLOG_INFO, LOG_TAG, "#### No resouceHandle found to unregister");
786 catch (std::exception &e)
788 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
793 if (NULL != resourceHandle)
795 // Unbind Light resource
796 if (NULL != foundResourceHandle)
798 OCPlatform::unbindResource(resourceHandle, foundResourceHandle);
799 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource Unbind Done");
801 OCPlatform::unregisterResource(resourceHandle);
802 dlog_print(DLOG_INFO, LOG_TAG, "#### Group Unregistered");
806 dlog_print(DLOG_INFO, LOG_TAG, "#### foundResourceHandle is NULL");
809 catch (std::exception &e)
811 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
817 static void createActionSet(void *data, Evas_Object *obj, void *event_info)
819 createActionSet_AllBulbOff();
820 createActionSet_AllBulbOn();
824 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
826 datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
827 evas_object_del(popup_fields->popup);
831 static int parseString(const char *str, int slen, int *beg, int what)
833 int i, val = 0, ch = '/';
849 // Remove whitespaces(if any) at the beginning
850 while (str[*beg] == ' ')
854 for (i = *beg; i < slen; i++)
858 val = (val * 10) + (str[i] - 48);
867 static bool validate(int date, int month, int year, int hour, int minute, int second)
869 if (date <= 0 || month <= 0 || year <= 0 || hour < 0 || minute < 0 || second < 0
870 || month >= 13 || hour >= 24 || minute >= 60 || second >= 60)
878 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
880 datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
881 Evas_Object *entry = popup_fields->entry;
882 const char *dateTimeValue = elm_entry_entry_get(entry);
884 len = strlen(dateTimeValue);
885 if (NULL == dateTimeValue || 1 > len)
887 dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL DateTime Value");
888 string logMessage = "DateTime should not be NULL<br>";
889 logMessage += "----------------------<br>";
890 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
891 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
895 int date, month, year, hour, minute, second;
897 date = parseString(dateTimeValue, len, &beg, 0);
898 month = parseString(dateTimeValue, len, &beg, 1);
899 year = parseString(dateTimeValue, len, &beg, 2);
900 hour = parseString(dateTimeValue, len, &beg, 3);
901 minute = parseString(dateTimeValue, len, &beg, 4);
902 second = parseString(dateTimeValue, len, &beg, 5);
904 dlog_print(DLOG_INFO, LOG_TAG, "#### %d", date);
905 dlog_print(DLOG_INFO, LOG_TAG, "#### %d", month);
906 dlog_print(DLOG_INFO, LOG_TAG, "#### %d", year);
907 dlog_print(DLOG_INFO, LOG_TAG, "#### %d", hour);
908 dlog_print(DLOG_INFO, LOG_TAG, "#### %d", minute);
909 dlog_print(DLOG_INFO, LOG_TAG, "#### %d", second);
910 bool valid = validate(date, month, year, hour, minute, second);
913 createScheduledActionSet_AllBulbOff(date, month, year, hour, minute, second);
917 dlog_print(DLOG_INFO, LOG_TAG, "#### Incorrect date/time values");
918 string logMessage = "Incorrect date/time value<br>";
919 logMessage += "----------------------<br>";
920 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
921 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
925 evas_object_del(popup_fields->popup);
930 list_scheduled_actionset_cb(void *data, Evas_Object *obj, void *event_info)
932 Evas_Object *popup, *btn;
933 Evas_Object *nf = naviframe;
938 popup = elm_popup_add(nf);
939 elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
940 eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
941 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
942 elm_object_part_text_set(popup, "title,text", "Enter the date and time");
944 layout = elm_layout_add(popup);
945 elm_layout_file_set(layout, ELM_DEMO_EDJ, "popup_datetime_text");
946 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
947 elm_object_content_set(popup, layout);
949 entry = elm_entry_add(layout);
950 elm_entry_single_line_set(entry, EINA_TRUE);
951 elm_entry_scrollable_set(entry, EINA_TRUE);
952 evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
953 evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
954 eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
955 elm_object_part_text_set(entry, "elm.guide", "dd/mm/yyyy hh:mm:ss");
956 elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
957 elm_object_part_content_set(layout, "elm.swallow.content", entry);
959 datetime_popup_fields *popup_fields;
960 popup_fields = (datetime_popup_fields *)malloc(sizeof(datetime_popup_fields));
961 if (NULL == popup_fields)
963 dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
967 popup_fields->popup = popup;
968 popup_fields->entry = entry;
972 btn = elm_button_add(popup);
973 elm_object_style_set(btn, "popup");
974 elm_object_text_set(btn, "Cancel");
975 elm_object_part_content_set(popup, "button1", btn);
976 evas_object_smart_callback_add(btn, "clicked", popup_cancel_clicked_cb, popup_fields);
979 btn = elm_button_add(popup);
980 elm_object_style_set(btn, "popup");
981 elm_object_text_set(btn, "Set");
982 elm_object_part_content_set(popup, "button2", btn);
983 evas_object_smart_callback_add(btn, "clicked", popup_set_clicked_cb, popup_fields);
985 evas_object_show(popup);
988 void *showGroupAPIs(void *data)
990 // Add items to the list only if the list is empty
991 const Eina_List *eina_list = elm_list_items_get(list);
992 int count = eina_list_count(eina_list);
995 elm_list_item_append(list, "1. Create ActionSet<br>(ALLBULBON and ALLBULBOFF)", NULL, NULL,
996 createActionSet, NULL);
998 elm_list_item_append(list, "2. Execute ActionSet (ALLBULBON)", NULL, NULL,
999 executeActionSetOn, NULL);
1001 elm_list_item_append(list, "3. Execute ActionSet (ALLBULBOFF)", NULL, NULL,
1002 executeActionSetOff, NULL);
1004 elm_list_item_append(list, "4. Create ActionSet<br>(Recursive_ALLBULBON)", NULL, NULL,
1005 createRecursiveActionSet_AllBulbOn, NULL);
1007 elm_list_item_append(list, " 4.1 Execute ActionSet", NULL, NULL,
1008 recursive_allBulbOn, NULL);
1010 elm_list_item_append(list, " 4.2 Cancel ActionSet", NULL, NULL,
1011 cancelRecursive_allBulbOn, NULL);
1013 elm_list_item_append(list, "5. Create ActionSet<br>(Scheduled_ALLBULBOFF)", NULL, NULL,
1014 list_scheduled_actionset_cb, NULL);
1016 elm_list_item_append(list, " 5.1 Execute ActionSet", NULL, NULL,
1017 scheduled_AllbulbOff, NULL);
1019 elm_list_item_append(list, " 5.2 Cancel ActionSet", NULL, NULL,
1020 cancelScheduled_AllBulbOff, NULL);
1022 elm_list_item_append(list, "6. Get ActionSet (All BULBOFF)", NULL, NULL,
1023 getActionSetOff, NULL);
1025 elm_list_item_append(list, "7. Delete ActionSet (All BULBOFF)", NULL, NULL,
1026 deleteActionSetOff, NULL);
1028 elm_list_item_append(list, "8. Find BookMark to Observe", NULL, NULL,
1029 observeBookMark, NULL);
1036 // Callback to be called when a resource is found in the network
1037 void foundResource(shared_ptr< OCResource > resource)
1039 dlog_print(DLOG_INFO, LOG_TAG, "#### foundResource entry!!!!");
1043 string resourceURI = resource->uri();
1044 string hostAddress = resource->host();
1046 if (resourceURI == "/core/b/collection")
1048 g_resource = resource;
1049 dlog_print(DLOG_INFO, LOG_TAG, "#### FOUND URI: %s", resourceURI.c_str());
1050 dlog_print(DLOG_INFO, LOG_TAG, "#### FOUND HOST: %s", hostAddress.c_str());
1051 logMessage = "FOUND RESOURCE URI <br>" + resourceURI + "<br>";
1052 logMessage += "FOUND RESOURCE HOST <br>" + hostAddress + "<br>";
1053 logMessage += "----------------------<br>";
1054 // Show the UI list of group APIs
1055 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showGroupAPIs, NULL);
1058 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
1059 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
1063 dlog_print(DLOG_INFO, LOG_TAG, "#### foundResource exit!!!!");
1066 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
1068 Elm_Object_Item *it = (Elm_Object_Item *)event_info;
1069 elm_list_item_selected_set(it, EINA_FALSE);
1072 // Method for Finding the Group
1073 static void find_group()
1075 dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup ENTRY");
1076 std::string query = OC_RSRVD_WELL_KNOWN_URI;
1077 query.append("?rt=");
1078 query.append(resourceTypeName);
1080 OCPlatform::findResource("", query, CT_DEFAULT, &foundResource);
1082 dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup EXIT");
1085 // Method to be called when the Find Group UI Button is selected
1087 find_group_cb(void *data, Evas_Object *obj, void *event_info)
1095 dlog_print(DLOG_ERROR, "find_group_cb", "list is NULL - So unable to add items!!!");
1100 naviframe_pop_cb(void *data, Elm_Object_Item *it)
1104 if (NULL != log_entry)
1106 evas_object_del(log_entry);
1111 evas_object_del(list);
1117 // Method to be called when the Group APIs UI Button is selected
1118 void group_cb(void *data, Evas_Object *obj, void *event_info)
1120 Evas_Object *layout;
1121 Evas_Object *scroller;
1122 Evas_Object *nf = (Evas_Object *)data;
1123 Evas_Object *find_button;
1124 Elm_Object_Item *nf_it;
1129 scroller = elm_scroller_add(nf);
1130 elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
1131 elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
1134 layout = elm_layout_add(nf);
1135 elm_layout_file_set(layout, ELM_DEMO_EDJ, "group_layout");
1136 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1138 elm_object_content_set(scroller, layout);
1141 find_button = elm_button_add(layout);
1142 elm_object_part_content_set(layout, "find_button", find_button);
1143 elm_object_text_set(find_button, "Find Group");
1144 evas_object_smart_callback_add(find_button, "clicked", find_group_cb, NULL);
1147 list = elm_list_add(layout);
1148 elm_list_mode_set(list, ELM_LIST_COMPRESS);
1149 evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
1150 elm_object_part_content_set(layout, "list", list);
1153 // log_entry - textarea for log
1154 log_entry = elm_entry_add(layout);
1155 elm_entry_scrollable_set(log_entry, EINA_TRUE);
1156 elm_entry_editable_set(log_entry, EINA_FALSE);
1157 elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
1158 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1159 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
1160 elm_object_part_content_set(layout, "log", log_entry);
1162 nf_it = elm_naviframe_item_push(nf, "Group APIs", NULL, NULL, scroller, NULL);
1163 elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);