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 ******************************************************************/
20 package com.tm.sample;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.Vector;
27 import org.iotivity.base.ObserveType;
28 import org.iotivity.base.OcException;
29 import org.iotivity.base.OcHeaderOption;
30 import org.iotivity.base.OcPlatform;
31 import org.iotivity.base.OcRepresentation;
32 import org.iotivity.base.OcResource;
33 import org.iotivity.base.OcResource.OnObserveListener;
34 import org.iotivity.base.OcResourceHandle;
35 import org.iotivity.service.tm.Action;
36 import org.iotivity.service.tm.ActionSet;
37 import org.iotivity.service.tm.Capability;
38 import org.iotivity.service.tm.GroupSynchronization;
39 import org.iotivity.service.tm.GroupSynchronization.*;
40 import org.iotivity.service.tm.OCStackResult;
41 import org.iotivity.service.tm.GroupManager;
42 import org.iotivity.service.tm.GroupManager.*;
43 import org.iotivity.service.tm.Time.ActionSetType;
45 import android.os.Message;
46 import android.util.Log;
49 * For calling the group APIs as per user selection on UI and for updating the
52 public class GroupClient {
54 private static final String LOG_TAG = "[TMSample] GroupClient";
55 private static Message msg;
56 public String logMessage;
58 private final String groupResourceType = "b.collection";
60 private final GroupManager groupManagerObj;
61 private final GroupSynchronization groupSynObj;
62 private final ActionListener actionListener;
63 private final ObserveListener observeListener;
64 private OcResource groupResource;
65 private OcResourceHandle groupResourceHandle;
66 private OcResourceHandle foundLightHandle;
67 private static GroupApiActivity groupApiActivityObj = null;
68 public static Vector<String> lights = new Vector<String>();
69 public static Vector<String> bookmarks = new Vector<String>();
72 * Listener for receiving observe notifications.
74 private class ObserveListener implements OnObserveListener {
76 public void onObserveCompleted(List<OcHeaderOption> headerOptionList,
77 OcRepresentation ocRepresentation, int sequenceNumber) {
78 Log.i(LOG_TAG, "onObserveCompleted invoked");
79 if (0 == ocRepresentation.getValueInt("level")) {
80 createActionSetBulbOn();
81 executeActionSetBulbOn(0);
82 } else if (5 == ocRepresentation.getValueInt("level")) {
83 createActionSetBulbOff();
84 executeActionSetBulbOff(0);
89 public void onObserveFailed(Throwable arg0) {
94 * Listener for receiving groups discovered in network.
96 private class FindGroupListener implements IFindGroupListener {
98 public void onGroupFindCallback(OcResource resource) {
99 Log.i(LOG_TAG, "onGroupFindCallback invoked");
100 if (resource != null) {
101 String uri = resource.getUri();
102 if (uri.equals("/b/collection") == true) {
103 String hostAddress = resource.getHost();
104 Log.d(LOG_TAG, "onGroupFindCallback URI : " + uri);
105 Log.d(LOG_TAG, "onGroupFindCallback HOST : " + hostAddress);
107 groupResource = resource;
108 Message msg = Message.obtain();
110 groupApiActivityObj.getHandler().sendMessage(msg);
112 logMessage = "onGroupFind" + "\n";
113 logMessage += "URI : " + uri + "\n";
114 logMessage += "Host :" + hostAddress;
115 GroupApiActivity.setMessageLog(logMessage);
116 msg = Message.obtain();
118 groupApiActivityObj.getHandler().sendMessage(msg);
120 Log.d(LOG_TAG, "onGroupFindCallback URI : " + uri);
123 Log.i(LOG_TAG, "Resource is NULL");
129 * Listener for receiving Light resource and Observe resources discovered in
132 private class FindCadidateResourceListener implements
133 IFindCandidateResourceListener {
136 public void onResourceFoundCallback(Vector<OcResource> resources) {
137 // TODO Auto-generated method stub
138 Log.i(LOG_TAG, "onResourceCallback invoked");
140 if (resources != null) {
141 for (int i = 0; i < resources.size(); i++) {
142 Log.d(LOG_TAG, "Resource information");
143 OcResource ocResource = resources.get(i);
144 String resourceURI = ocResource.getUri();
145 String hostAddress = ocResource.getHost();
147 logMessage = "API RESULT : " + "OC_STACK_OK" + "\n";
148 logMessage += "URI: " + resourceURI + "\n";
149 logMessage += "Host:" + hostAddress;
150 GroupApiActivity.setMessageLog(logMessage);
151 msg = Message.obtain();
153 groupApiActivityObj.getHandler().sendMessage(msg);
155 if (resourceURI.equals("/a/light") == true) {
156 if (lights.contains((hostAddress + resourceURI)) == false) {
157 lights.add((hostAddress + resourceURI));
158 if (groupApiActivityObj != null) {
160 logMessage = "API RESULT : " + "OC_STACK_OK"
162 logMessage += "URI: " + resourceURI + "\n";
163 logMessage += "Host:" + hostAddress;
164 GroupApiActivity.setMessageLog(logMessage);
165 msg = Message.obtain();
167 groupApiActivityObj.getHandler().sendMessage(
170 foundLightHandle = groupManagerObj
171 .bindResourceToGroup(ocResource,
172 groupResourceHandle);
174 } catch (OcException e) {
176 "bindResourceToGroup Exception!");
180 Log.i(LOG_TAG, "Resource is already registered!");
182 } else if (resourceURI.equalsIgnoreCase("/core/bookmark")) {
183 if (bookmarks.contains((hostAddress + resourceURI)) == false) {
184 bookmarks.add((hostAddress + resourceURI));
185 if (groupApiActivityObj != null) {
186 logMessage = "API RESULT : " + "OC_STACK_OK"
188 logMessage += "URI: " + resourceURI + "\n";
189 logMessage += "Host:" + hostAddress;
190 GroupApiActivity.setMessageLog(logMessage);
191 msg = Message.obtain();
193 groupApiActivityObj.getHandler().sendMessage(
206 private final FindCadidateResourceListener findCandidateResourceListener;
207 private final FindGroupListener findGroupListener;
209 public GroupClient() {
210 groupManagerObj = new GroupManager();
211 groupSynObj = GroupSynchronization.getInstance();
212 findGroupListener = new FindGroupListener();
213 actionListener = new ActionListener();
214 observeListener = new ObserveListener();
215 findCandidateResourceListener = new FindCadidateResourceListener();
217 groupSynObj.setGroupListener(findGroupListener);
219 .setFindCandidateResourceListener(findCandidateResourceListener);
220 groupManagerObj.setActionListener(actionListener);
222 groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
226 * This method creates group of the type "b.collection" handling light
229 public void createGroup() {
230 Map<String, OcResourceHandle> groupList = new HashMap<String, OcResourceHandle>();
232 // creating group of type b.collection
233 // OcPlatform.registerResource(ocResource);
234 OCStackResult result = groupSynObj.createGroup(groupResourceType);
235 if ((OCStackResult.OC_STACK_OK != result)) {
236 Log.e(LOG_TAG, "createGroup returned error: " + result.name());
239 Log.e(LOG_TAG, "createGroup success: " + result.name());
242 // getting the Created group Handle
243 groupList = groupSynObj.getGroupList();
244 if (groupList.containsKey(groupResourceType)) {
245 groupResourceHandle = groupList.get(groupResourceType);
247 Log.e(LOG_TAG, "group does not contain groupResourceType: "
253 * This method finds the group of type "b.collection".
255 public void findGroup() {
256 Log.d(LOG_TAG, "finding group");
258 Vector<String> resourceTypes = new Vector<String>();
259 resourceTypes.add(groupResourceType);
260 OCStackResult result = groupSynObj.findGroup(resourceTypes);
261 if (OCStackResult.OC_STACK_OK != result) {
262 Log.e(LOG_TAG, "findGroup returned error: " + result.name());
266 logMessage = "API RESULT : " + result.toString();
267 GroupApiActivity.setMessageLog(logMessage);
269 if (groupApiActivityObj != null) {
270 msg = Message.obtain();
272 groupApiActivityObj.getHandler().sendMessage(msg);
277 * This method finds the light resources of type "core.light".
279 public void findLightResources() {
280 Log.d(LOG_TAG, "finding light resources");
282 Vector<String> types = new Vector<String>();
283 types.add("core.light");
284 OCStackResult result = groupManagerObj.findCandidateResources(types, 5);
285 if (OCStackResult.OC_STACK_OK != result) {
287 "findCandidateResources returned error: " + result.name());
292 * This method finds the bookmark resources of type "core.bookmark".
294 public void findBookMarkResources() {
295 Log.d(LOG_TAG, "finding bookmark resources");
297 Vector<String> types = new Vector<String>();
298 types.add("core.bookmark");
299 OCStackResult result = groupManagerObj.findCandidateResources(types, 5);
300 if (OCStackResult.OC_STACK_OK != result) {
302 "findCandidateResources returned error: " + result.name());
307 * This method creates the action set for bulb on action.
309 public void createActionSetBulbOn() {
310 Log.i(LOG_TAG, "creating action set for bulb on action");
312 if (lights.size() == 0) {
314 .displayToastMessage("No Light server found in network!");
318 ActionSet actionSet = new ActionSet();
319 actionSet.actionsetName = "AllBulbOn";
321 // Create actions list
322 for (int i = 0; i < lights.size(); i++) {
323 Action action = new Action();
324 action.target = lights.get(i);
326 Capability capability = new Capability();
327 capability.capability = "power";
328 capability.status = "on";
330 action.listOfCapability.add(capability);
331 actionSet.listOfAction.add(action);
335 OCStackResult result = groupManagerObj.addActionSet(groupResource,
337 if (OCStackResult.OC_STACK_OK != result) {
338 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
341 } catch (OcException e) {
348 * This method creates the action set for bulb off action.
350 public void createActionSetBulbOff() {
351 Log.i(LOG_TAG, "creating action set for bulb off action");
353 if (lights.size() == 0) {
355 .displayToastMessage("No Light server found in network!");
359 ActionSet actionSet = new ActionSet();
360 actionSet.actionsetName = "AllBulbOff";
362 // Create actions list
363 for (int i = 0; i < lights.size(); i++) {
364 Action action = new Action();
365 action.target = lights.get(i);
367 Capability capability = new Capability();
368 capability.capability = "power";
369 capability.status = "off";
371 action.listOfCapability.add(capability);
372 actionSet.listOfAction.add(action);
376 OCStackResult result = groupManagerObj.addActionSet(groupResource,
378 if (OCStackResult.OC_STACK_OK != result) {
379 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
382 } catch (OcException e) {
389 * This method creates the recursive action set for bulb on action.
391 public void createRecursiveActionSetBulbOn() {
392 Log.i(LOG_TAG, "creating recursive action set for bulb on action");
394 if (lights.size() == 0) {
396 .displayToastMessage("No Light server found in network!");
400 ActionSet actionSet = new ActionSet();
401 actionSet.actionsetName = "AllBulbOnRecursive";
402 actionSet.setType(ActionSetType.RECURSIVE);
404 actionSet.mMonth = 0;
409 actionSet.setDelay(actionSet.getSecAbsTime());
411 // Create actions list
412 for (int i = 0; i < lights.size(); i++) {
413 Action action = new Action();
414 action.target = lights.get(i);
416 Capability capability = new Capability();
417 capability.capability = "power";
418 capability.status = "on";
420 action.listOfCapability.add(capability);
421 actionSet.listOfAction.add(action);
425 OCStackResult result = groupManagerObj.addActionSet(groupResource,
427 if (OCStackResult.OC_STACK_OK != result) {
428 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
431 } catch (OcException e) {
438 * This method creates the scheduled action set for bulb off action.
440 public void createScheduledActionSetBulbOff(long delay) {
441 Log.i(LOG_TAG, "creating scheduled action set for bulb off action");
443 if (lights.size() == 0) {
445 .displayToastMessage("No Light server found in network!");
449 ActionSet actionSet = new ActionSet();
450 actionSet.actionsetName = "AllBulbOffScheduled";
451 actionSet.setType(ActionSetType.SCHEDULED);
452 actionSet.setDelay(delay);
453 Log.i(LOG_TAG, "Set the delay of " + delay + " seconds");
455 // Create actions list
456 for (int i = 0; i < lights.size(); i++) {
457 Action action = new Action();
458 action.target = lights.get(i);
460 Capability capability = new Capability();
461 capability.capability = "power";
462 capability.status = "off";
464 action.listOfCapability.add(capability);
465 actionSet.listOfAction.add(action);
469 OCStackResult result = groupManagerObj.addActionSet(groupResource,
471 if (OCStackResult.OC_STACK_OK != result) {
472 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
475 } catch (OcException e) {
482 * This method is for executing the action Set "AllBulbOn".
484 public void executeActionSetBulbOn(long delay) {
485 Log.i(LOG_TAG, "executing the action set of bulb on action");
487 if (lights.size() == 0) {
489 .displayToastMessage("No Light server found in network!");
493 executeActionSet("AllBulbOn", delay);
497 * This method is for executing the action Set "AllBulbOff".
499 public void executeActionSetBulbOff(long delay) {
500 Log.i(LOG_TAG, "executing the action set of bulb off action");
502 if (lights.size() == 0) {
504 .displayToastMessage("No Light server found in network!");
508 executeActionSet("AllBulbOff", delay);
512 * This method is for executing the recursive action Set
513 * "AllBulbOnRecursive".
515 public void executeRecursiveActionSetBulbOn(long delay) {
516 Log.i(LOG_TAG, "executing the recursive action set of bulb on action");
518 if (lights.size() == 0) {
520 .displayToastMessage("No Light server found in network!");
524 executeActionSet("AllBulbOnRecursive", delay);
528 * This method is for executing the schedule action Set
529 * "AllBulbOffScheduled".
531 public void executeScheduledActionSetBulbOff(long delay) {
532 Log.i(LOG_TAG, "executing the schedule action set of bulb off action");
534 if (lights.size() == 0) {
536 .displayToastMessage("No Light server found in network!");
540 executeActionSet("AllBulbOffScheduled", delay);
544 * This method is for canceling the action Set "AllBulbOn".
546 public void cancelActionSetBulbOn() {
547 Log.i(LOG_TAG, "cancelling the action set of bulb on action");
549 if (lights.size() == 0) {
551 .displayToastMessage("No Light server found in network!");
555 cancelActionSet("AllBulbOn");
559 * This method is for canceling the action Set "AllBulbOff".
561 public void cancelActionSetBulbOff() {
562 Log.i(LOG_TAG, "cancelling the action set of bulb off action");
564 if (lights.size() == 0) {
566 .displayToastMessage("No Light server found in network!");
570 cancelActionSet("AllBulbOff");
574 * This method is for canceling the recursive action Set
575 * "AllBulbOnRecursive".
577 public void cancelRecursiveActionSetBulbOn() {
578 Log.i(LOG_TAG, "cancelling the recursive action set of bulb on action");
580 if (lights.size() == 0) {
582 .displayToastMessage("No Light server found in network!");
586 cancelActionSet("AllBulbOnRecursive");
590 * This method is for canceling the scheduled action Set
591 * "AllBulbOffScheduled".
593 public void cancelScheduledActionSetBulbOff() {
594 Log.i(LOG_TAG, "cancelling the scheduled action set of bulb off action");
596 if (lights.size() == 0) {
598 .displayToastMessage("No Light server found in network!");
602 cancelActionSet("AllBulbOffScheduled");
606 * This method is for getting the action Set "AllBulbOn".
608 public void getActionSetBulbOn() {
609 Log.i(LOG_TAG, "getting the action set of bulb on action");
612 OCStackResult result = groupManagerObj.getActionSet(groupResource,
614 if (OCStackResult.OC_STACK_OK != result) {
616 "getActionSetOn returned error: " + result.name());
619 } catch (OcException e) {
625 * This method is for getting the action Set "AllBulbOff".
627 public void getActionSetBulbOff() {
628 Log.i(LOG_TAG, "getting the action set of bulb off action");
631 OCStackResult result = groupManagerObj.getActionSet(groupResource,
633 if (OCStackResult.OC_STACK_OK != result) {
635 "getActionSetOn returned error: " + result.name());
638 } catch (OcException e) {
644 * This method is for deleting the action Set "AllBulbOn".
646 public void deleteActionSetBulbOn() {
647 Log.i(LOG_TAG, "deleting the action set of bulb on action");
650 OCStackResult result = groupManagerObj.deleteActionSet(
651 groupResource, "AllBulbOn");
652 if (OCStackResult.OC_STACK_OK != result) {
654 "deleteActionSet returned error : " + result.name());
657 } catch (OcException e) {
663 * This method is for deleting the action Set "AllBulbOff".
665 public void deleteActionSetBulbOff() {
666 Log.i(LOG_TAG, "deleting the action set of bulb off action");
669 OCStackResult result = groupManagerObj.deleteActionSet(
670 groupResource, "AllBulbOff");
671 if (OCStackResult.OC_STACK_OK != result) {
673 "deleteActionSet returned error : " + result.name());
676 } catch (OcException e) {
682 * This method is for observing the bulb on/off status.
684 public void observe(OcResource resource) {
685 Log.i(LOG_TAG, "Registering observer for bookmark resources status");
687 Map<String, String> queryMap = new HashMap<String, String>();
689 resource.observe(ObserveType.OBSERVE, queryMap, observeListener);
690 } catch (OcException e) {
696 * This method is for unbinding and unregister all the found resources and
699 public void leaveGroup() {
700 groupSynObj.setGroupListener(null);
701 groupManagerObj.setFindCandidateResourceListener(null);
702 groupManagerObj.setActionListener(null);
704 if (null != foundLightHandle) {
706 OcPlatform.unregisterResource(foundLightHandle);
707 } catch (OcException e) {
709 Log.i(LOG_TAG, "Resource Unregister Exception");
712 Log.i(LOG_TAG, "foundLightHandle is NULL");
714 if (null != groupResourceHandle) {
716 groupSynObj.deleteGroup(groupResourceType);
718 Log.i(LOG_TAG, "groupResourceHandle is NULL");
722 private void executeActionSet(String actonSetName, long delay) {
724 OCStackResult result = groupManagerObj.executeActionSet(
725 groupResource, actonSetName, delay);
726 if (OCStackResult.OC_STACK_OK != result) {
728 "executeActionSet retuned error : " + result.name());
731 } catch (OcException e) {
736 private void cancelActionSet(String actionSetName) {
738 OCStackResult result = groupManagerObj.cancelActionSet(
739 groupResource, actionSetName);
740 if (OCStackResult.OC_STACK_OK != result) {
742 "cancelActionSet returned error : " + result.name());
745 } catch (OcException e) {