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.IFindCandidateResourceListener;
39 import org.iotivity.service.tm.IFindGroupListener;
40 import org.iotivity.service.tm.OCStackResult;
41 import org.iotivity.service.tm.ThingsManager;
42 import org.iotivity.service.tm.Time.ActionSetType;
44 import android.os.Message;
45 import android.util.Log;
48 * For calling the group APIs as per user selection on UI and for updating the
51 public class GroupClient {
53 private static final String LOG_TAG = "[TMSample] GroupClient";
54 private static Message msg;
55 public String logMessage;
57 private final String groupResourceType = "b.collection";
59 private final ThingsManager thingsManagerObj;
60 private final ActionListener actionListener;
61 private final ObserveListener observeListener;
62 private OcResource groupResource;
63 private OcResourceHandle groupResourceHandle;
64 private OcResourceHandle foundLightHandle;
65 private static GroupApiActivity groupApiActivityObj = null;
66 public static Vector<String> lights = new Vector<String>();
67 public static Vector<String> bookmarks = new Vector<String>();
70 * Listener for receiving observe notifications.
72 private class ObserveListener implements OnObserveListener {
74 public void onObserveCompleted(List<OcHeaderOption> headerOptionList,
75 OcRepresentation ocRepresentation, int sequenceNumber) {
76 Log.i(LOG_TAG, "onObserveCompleted invoked");
77 if (0 == ocRepresentation.getValueInt("level")) {
78 createActionSetBulbOn();
79 executeActionSetBulbOn(0);
80 } else if (5 == ocRepresentation.getValueInt("level")) {
81 createActionSetBulbOff();
82 executeActionSetBulbOff(0);
87 public void onObserveFailed(Throwable arg0) {
92 * Listener for receiving groups discovered in network.
94 private class FindGroupListener implements IFindGroupListener {
96 public void onGroupFindCallback(OcResource resource) {
97 Log.i(LOG_TAG, "onGroupFindCallback invoked");
98 if (resource != null) {
99 String uri = resource.getUri();
100 if (uri.equals("/b/collection") == true) {
101 String hostAddress = resource.getHost();
102 Log.d(LOG_TAG, "onGroupFindCallback URI : " + uri);
103 Log.d(LOG_TAG, "onGroupFindCallback HOST : " + hostAddress);
105 groupResource = resource;
106 Message msg = Message.obtain();
108 groupApiActivityObj.getHandler().sendMessage(msg);
110 logMessage = "onGroupFind" + "\n";
111 logMessage = logMessage + "URI : " + uri + "\n";
112 logMessage = logMessage + "Host :" + hostAddress;
113 GroupApiActivity.setMessageLog(logMessage);
114 msg = Message.obtain();
116 groupApiActivityObj.getHandler().sendMessage(msg);
118 Log.d(LOG_TAG, "onGroupFindCallback URI : " + uri);
121 Log.i(LOG_TAG, "Resource is NULL");
127 * Listener for receiving Light resource and Observe resources discovered in
130 private class FindCadidateResourceListener implements
131 IFindCandidateResourceListener {
133 public void onResourceCallback(Vector<OcResource> resources) {
134 Log.i(LOG_TAG, "onResourceCallback invoked");
136 if (resources != null) {
137 for (int i = 0; i < resources.size(); i++) {
138 Log.d(LOG_TAG, "Resource information");
139 OcResource ocResource = resources.get(i);
140 String resourceURI = ocResource.getUri();
141 String hostAddress = ocResource.getHost();
143 logMessage = "API RESULT : " + "OC_STACK_OK" + "\n";
144 logMessage = logMessage + "URI: " + resourceURI + "\n";
145 logMessage = logMessage + "Host:" + hostAddress;
146 GroupApiActivity.setMessageLog(logMessage);
147 msg = Message.obtain();
149 groupApiActivityObj.getHandler().sendMessage(msg);
151 if (resourceURI.equals("/a/light") == true) {
152 if (lights.contains((hostAddress + resourceURI)) == false) {
153 lights.add((hostAddress + resourceURI));
154 if (groupApiActivityObj != null) {
156 logMessage = "API RESULT : " + "OC_STACK_OK"
158 logMessage = logMessage + "URI: " + resourceURI
160 logMessage = logMessage + "Host:" + hostAddress;
161 GroupApiActivity.setMessageLog(logMessage);
162 msg = Message.obtain();
164 groupApiActivityObj.getHandler().sendMessage(
167 foundLightHandle = thingsManagerObj
168 .bindResourceToGroup(ocResource,
169 groupResourceHandle);
171 } catch (OcException e) {
173 "bindResourceToGroup Exception!");
177 Log.i(LOG_TAG, "Resource is already registered!");
179 } else if (resourceURI.equalsIgnoreCase("/core/bookmark")) {
180 if (bookmarks.contains((hostAddress + resourceURI)) == false) {
181 bookmarks.add((hostAddress + resourceURI));
182 if (groupApiActivityObj != null) {
183 logMessage = "API RESULT : " + "OC_STACK_OK"
185 logMessage = logMessage + "URI: " + resourceURI
187 logMessage = logMessage + "Host:" + hostAddress;
188 GroupApiActivity.setMessageLog(logMessage);
189 msg = Message.obtain();
191 groupApiActivityObj.getHandler().sendMessage(
203 private final FindCadidateResourceListener findCandidateResourceListener;
204 private final FindGroupListener findGroupListener;
206 public GroupClient() {
207 thingsManagerObj = new ThingsManager();
208 findGroupListener = new FindGroupListener();
209 actionListener = new ActionListener();
210 observeListener = new ObserveListener();
211 findCandidateResourceListener = new FindCadidateResourceListener();
213 thingsManagerObj.setGroupListener(findGroupListener);
215 .setFindCandidateResourceListener(findCandidateResourceListener);
216 thingsManagerObj.setActionListener(actionListener);
218 groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
222 * This method creates group of the type "b.collection" handling light
225 public void createGroup() {
226 Map<String, OcResourceHandle> groupList = new HashMap<String, OcResourceHandle>();
228 // creating group of type b.collection
229 OCStackResult result = thingsManagerObj.createGroup(groupResourceType);
230 if ((OCStackResult.OC_STACK_OK != result)) {
231 Log.e(LOG_TAG, "createGroup returned error: " + result.name());
234 Log.e(LOG_TAG, "createGroup success: " + result.name());
237 // getting the Created group Handle
238 groupList = thingsManagerObj.getGroupList();
239 if (groupList.containsKey(groupResourceType)) {
240 groupResourceHandle = groupList.get(groupResourceType);
242 Log.e(LOG_TAG, "group does not contain groupResourceType: "
248 * This method finds the group of type "b.collection".
250 public void findGroup() {
251 Log.d(LOG_TAG, "finding group");
253 Vector<String> resourceTypes = new Vector<String>();
254 resourceTypes.add(groupResourceType);
255 OCStackResult result = thingsManagerObj.findGroup(resourceTypes);
256 if (OCStackResult.OC_STACK_OK != result) {
257 Log.e(LOG_TAG, "findGroup returned error: " + result.name());
261 logMessage = "API RESULT : " + result.toString();
262 GroupApiActivity.setMessageLog(logMessage);
264 if (groupApiActivityObj != null) {
265 msg = Message.obtain();
267 groupApiActivityObj.getHandler().sendMessage(msg);
272 * This method finds the light resources of type "core.light".
274 public void findLightResources() {
275 Log.d(LOG_TAG, "finding light resources");
277 Vector<String> types = new Vector<String>();
278 types.add("core.light");
279 OCStackResult result = thingsManagerObj
280 .findCandidateResources(types, 5);
281 if (OCStackResult.OC_STACK_OK != result) {
283 "findCandidateResources returned error: " + result.name());
288 * This method finds the bookmark resources of type "core.bookmark".
290 public void findBookMarkResources() {
291 Log.d(LOG_TAG, "finding bookmark resources");
293 Vector<String> types = new Vector<String>();
294 types.add("core.bookmark");
295 OCStackResult result = thingsManagerObj
296 .findCandidateResources(types, 5);
297 if (OCStackResult.OC_STACK_OK != result) {
299 "findCandidateResources returned error: " + result.name());
304 * This method creates the action set for bulb on action.
306 public void createActionSetBulbOn() {
307 Log.i(LOG_TAG, "creating action set for bulb on action");
309 if (lights.size() == 0) {
311 .displayToastMessage("No Light server found in network!");
315 ActionSet actionSet = new ActionSet();
316 actionSet.actionsetName = "AllBulbOn";
318 // Create actions list
319 for (int i = 0; i < lights.size(); i++) {
320 Action action = new Action();
321 action.target = lights.get(i);
323 Capability capability = new Capability();
324 capability.capability = "power";
325 capability.status = "on";
327 action.listOfCapability.add(capability);
328 actionSet.listOfAction.add(action);
332 OCStackResult result = thingsManagerObj.addActionSet(groupResource,
334 if (OCStackResult.OC_STACK_OK != result) {
335 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
338 } catch (OcException e) {
345 * This method creates the action set for bulb off action.
347 public void createActionSetBulbOff() {
348 Log.i(LOG_TAG, "creating action set for bulb off action");
350 if (lights.size() == 0) {
352 .displayToastMessage("No Light server found in network!");
356 ActionSet actionSet = new ActionSet();
357 actionSet.actionsetName = "AllBulbOff";
359 // Create actions list
360 for (int i = 0; i < lights.size(); i++) {
361 Action action = new Action();
362 action.target = lights.get(i);
364 Capability capability = new Capability();
365 capability.capability = "power";
366 capability.status = "off";
368 action.listOfCapability.add(capability);
369 actionSet.listOfAction.add(action);
373 OCStackResult result = thingsManagerObj.addActionSet(groupResource,
375 if (OCStackResult.OC_STACK_OK != result) {
376 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
379 } catch (OcException e) {
386 * This method creates the recursive action set for bulb on action.
388 public void createRecursiveActionSetBulbOn() {
389 Log.i(LOG_TAG, "creating recursive action set for bulb on action");
391 if (lights.size() == 0) {
393 .displayToastMessage("No Light server found in network!");
397 ActionSet actionSet = new ActionSet();
398 actionSet.actionsetName = "AllBulbOnRecursive";
399 actionSet.setType(ActionSetType.RECURSIVE);
401 actionSet.mMonth = 0;
406 actionSet.setDelay(actionSet.getSecAbsTime());
408 // Create actions list
409 for (int i = 0; i < lights.size(); i++) {
410 Action action = new Action();
411 action.target = lights.get(i);
413 Capability capability = new Capability();
414 capability.capability = "power";
415 capability.status = "on";
417 action.listOfCapability.add(capability);
418 actionSet.listOfAction.add(action);
422 OCStackResult result = thingsManagerObj.addActionSet(groupResource,
424 if (OCStackResult.OC_STACK_OK != result) {
425 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
428 } catch (OcException e) {
435 * This method creates the scheduled action set for bulb off action.
437 public void createScheduledActionSetBulbOff(long delay) {
438 Log.i(LOG_TAG, "creating scheduled action set for bulb off action");
440 if (lights.size() == 0) {
442 .displayToastMessage("No Light server found in network!");
446 ActionSet actionSet = new ActionSet();
447 actionSet.actionsetName = "AllBulbOffScheduled";
448 actionSet.setType(ActionSetType.SCHEDULED);
449 actionSet.setDelay(delay);
450 Log.i(LOG_TAG, "Set the delay of " + delay + " seconds");
452 // Create actions list
453 for (int i = 0; i < lights.size(); i++) {
454 Action action = new Action();
455 action.target = lights.get(i);
457 Capability capability = new Capability();
458 capability.capability = "power";
459 capability.status = "off";
461 action.listOfCapability.add(capability);
462 actionSet.listOfAction.add(action);
466 OCStackResult result = thingsManagerObj.addActionSet(groupResource,
468 if (OCStackResult.OC_STACK_OK != result) {
469 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
472 } catch (OcException e) {
479 * This method is for executing the action Set "AllBulbOn".
481 public void executeActionSetBulbOn(long delay) {
482 Log.i(LOG_TAG, "executing the action set of bulb on action");
484 if (lights.size() == 0) {
486 .displayToastMessage("No Light server found in network!");
490 executeActionSet("AllBulbOn", delay);
494 * This method is for executing the action Set "AllBulbOff".
496 public void executeActionSetBulbOff(long delay) {
497 Log.i(LOG_TAG, "executing the action set of bulb off action");
499 if (lights.size() == 0) {
501 .displayToastMessage("No Light server found in network!");
505 executeActionSet("AllBulbOff", delay);
509 * This method is for executing the recursive action Set
510 * "AllBulbOnRecursive".
512 public void executeRecursiveActionSetBulbOn(long delay) {
513 Log.i(LOG_TAG, "executing the recursive action set of bulb on action");
515 if (lights.size() == 0) {
517 .displayToastMessage("No Light server found in network!");
521 executeActionSet("AllBulbOnRecursive", delay);
525 * This method is for executing the schedule action Set
526 * "AllBulbOffScheduled".
528 public void executeScheduledActionSetBulbOff(long delay) {
529 Log.i(LOG_TAG, "executing the schedule action set of bulb off action");
531 if (lights.size() == 0) {
533 .displayToastMessage("No Light server found in network!");
537 executeActionSet("AllBulbOffScheduled", delay);
541 * This method is for canceling the action Set "AllBulbOn".
543 public void cancelActionSetBulbOn() {
544 Log.i(LOG_TAG, "cancelling the action set of bulb on action");
546 if (lights.size() == 0) {
548 .displayToastMessage("No Light server found in network!");
552 cancelActionSet("AllBulbOn");
556 * This method is for canceling the action Set "AllBulbOff".
558 public void cancelActionSetBulbOff() {
559 Log.i(LOG_TAG, "cancelling the action set of bulb off action");
561 if (lights.size() == 0) {
563 .displayToastMessage("No Light server found in network!");
567 cancelActionSet("AllBulbOff");
571 * This method is for canceling the recursive action Set
572 * "AllBulbOnRecursive".
574 public void cancelRecursiveActionSetBulbOn() {
575 Log.i(LOG_TAG, "cancelling the recursive action set of bulb on action");
577 if (lights.size() == 0) {
579 .displayToastMessage("No Light server found in network!");
583 cancelActionSet("AllBulbOnRecursive");
587 * This method is for canceling the scheduled action Set
588 * "AllBulbOffScheduled".
590 public void cancelScheduledActionSetBulbOff() {
591 Log.i(LOG_TAG, "cancelling the scheduled action set of bulb off action");
593 if (lights.size() == 0) {
595 .displayToastMessage("No Light server found in network!");
599 cancelActionSet("AllBulbOffScheduled");
603 * This method is for getting the action Set "AllBulbOn".
605 public void getActionSetBulbOn() {
606 Log.i(LOG_TAG, "getting the action set of bulb on action");
609 OCStackResult result = thingsManagerObj.getActionSet(groupResource,
611 if (OCStackResult.OC_STACK_OK != result) {
613 "getActionSetOn returned error: " + result.name());
616 } catch (OcException e) {
622 * This method is for getting the action Set "AllBulbOff".
624 public void getActionSetBulbOff() {
625 Log.i(LOG_TAG, "getting the action set of bulb off action");
628 OCStackResult result = thingsManagerObj.getActionSet(groupResource,
630 if (OCStackResult.OC_STACK_OK != result) {
632 "getActionSetOn returned error: " + result.name());
635 } catch (OcException e) {
641 * This method is for deleting the action Set "AllBulbOn".
643 public void deleteActionSetBulbOn() {
644 Log.i(LOG_TAG, "deleting the action set of bulb on action");
647 OCStackResult result = thingsManagerObj.deleteActionSet(
648 groupResource, "AllBulbOn");
649 if (OCStackResult.OC_STACK_OK != result) {
651 "deleteActionSet returned error : " + result.name());
654 } catch (OcException e) {
660 * This method is for deleting the action Set "AllBulbOff".
662 public void deleteActionSetBulbOff() {
663 Log.i(LOG_TAG, "deleting the action set of bulb off action");
666 OCStackResult result = thingsManagerObj.deleteActionSet(
667 groupResource, "AllBulbOff");
668 if (OCStackResult.OC_STACK_OK != result) {
670 "deleteActionSet returned error : " + result.name());
673 } catch (OcException e) {
679 * This method is for observing the bulb on/off status.
681 public void observe(OcResource resource) {
682 Log.i(LOG_TAG, "Registering observer for bookmark resources status");
684 Map<String, String> queryMap = new HashMap<String, String>();
686 resource.observe(ObserveType.OBSERVE, queryMap, observeListener);
687 } catch (OcException e) {
693 * This method is for unbinding and unregister all the found resources and
696 public void leaveGroup() {
697 thingsManagerObj.setGroupListener(null);
698 thingsManagerObj.setFindCandidateResourceListener(null);
699 thingsManagerObj.setActionListener(null);
701 if (null != foundLightHandle) {
703 OcPlatform.unregisterResource(foundLightHandle);
704 } catch (OcException e) {
706 Log.i(LOG_TAG, "Resource Unregister Exception");
709 Log.i(LOG_TAG, "foundLightHandle is NULL");
711 if (null != groupResourceHandle) {
713 thingsManagerObj.deleteGroup(groupResourceType);
715 Log.i(LOG_TAG, "groupResourceHandle is NULL");
719 private void executeActionSet(String actonSetName, long delay) {
721 OCStackResult result = thingsManagerObj.executeActionSet(
722 groupResource, actonSetName, delay);
723 if (OCStackResult.OC_STACK_OK != result) {
725 "executeActionSet retuned error : " + result.name());
728 } catch (OcException e) {
733 private void cancelActionSet(String actionSetName) {
735 OCStackResult result = thingsManagerObj.cancelActionSet(
736 groupResource, actionSetName);
737 if (OCStackResult.OC_STACK_OK != result) {
739 "cancelActionSet returned error : " + result.name());
742 } catch (OcException e) {