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.EnumSet;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Vector;
28 import org.iotivity.base.EntityHandlerResult;
29 import org.iotivity.base.ObserveType;
30 import org.iotivity.base.OcException;
31 import org.iotivity.base.OcHeaderOption;
32 import org.iotivity.base.OcPlatform;
33 import org.iotivity.base.OcRepresentation;
34 import org.iotivity.base.OcResource;
35 import org.iotivity.base.OcResourceRequest;
36 import org.iotivity.base.OcStackConfig;
37 import org.iotivity.base.ResourceProperty;
38 import org.iotivity.base.OcResource.OnObserveListener;
39 import org.iotivity.base.OcResourceHandle;
40 import org.iotivity.service.tm.Action;
41 import org.iotivity.service.tm.ActionSet;
42 import org.iotivity.service.tm.Capability;
43 import org.iotivity.service.tm.OCStackResult;
44 import org.iotivity.service.tm.GroupManager;
45 import org.iotivity.service.tm.GroupManager.*;
46 import org.iotivity.service.tm.Time.ActionSetType;
48 import android.os.Message;
49 import android.util.Log;
52 * For calling the group APIs as per user selection on UI and for updating the
55 public class GroupClient {
57 private static final String LOG_TAG = "[TMSample] GroupClient";
58 private static Message msg;
59 public String logMessage;
61 private final String groupResourceType = "b.collection";
62 private final String groupResourceURI = "/b/collection";
64 private final GroupManager groupManagerObj;
65 private final ActionListener actionListener;
66 private final ObserveListener observeListener;
67 private OcResource groupResource;
68 private OcResourceHandle groupResourceHandle;
69 private OcResourceHandle foundLightHandle;
70 private static GroupApiActivity groupApiActivityObj = null;
71 public static Vector<String> lights = new Vector<String>();
72 public static Vector<String> bookmarks = new Vector<String>();
73 public static boolean groupFound = false;
76 * Listener for receiving observe notifications.
78 private class ObserveListener implements OnObserveListener {
80 public void onObserveCompleted(List<OcHeaderOption> headerOptionList,
81 OcRepresentation ocRepresentation, int sequenceNumber) {
82 Log.i(LOG_TAG, "onObserveCompleted invoked");
83 if (0 == ocRepresentation.getValueInt("level")) {
84 createActionSetBulbOn();
85 executeActionSetBulbOn(0);
86 } else if (5 == ocRepresentation.getValueInt("level")) {
87 createActionSetBulbOff();
88 executeActionSetBulbOff(0);
93 public void onObserveFailed(Throwable arg0) {
98 * Listener for receiving group resource , Light resource and Observe resources discovered in
101 private class FindCadidateResourceListener implements
102 IFindCandidateResourceListener {
105 public void onResourceFoundCallback(Vector<OcResource> resources) {
106 // TODO Auto-generated method stub
107 Log.i(LOG_TAG, "onResourceCallback invoked");
109 if (resources != null) {
110 for (int i = 0; i < resources.size(); i++) {
111 Log.d(LOG_TAG, "Resource information");
112 OcResource ocResource = resources.get(i);
113 String resourceURI = ocResource.getUri();
114 String hostAddress = ocResource.getHost();
116 logMessage = "API RESULT : " + "OC_STACK_OK" + "\n";
117 logMessage += "URI: " + resourceURI + "\n";
118 logMessage += "Host:" + hostAddress;
119 GroupApiActivity.setMessageLog(logMessage);
120 msg = Message.obtain();
122 groupApiActivityObj.getHandler().sendMessage(msg);
123 if (resourceURI.equals("/b/collection") == true) {
127 Log.d(LOG_TAG, "Group Found URI : " + resourceURI);
128 Log.d(LOG_TAG, "Group Foundk HOST : " + hostAddress);
130 groupResource = ocResource;
132 Message msg = Message.obtain();
134 groupApiActivityObj.getHandler().sendMessage(msg);
136 logMessage = "onGroupFind" + "\n";
137 logMessage += "URI : " + resourceURI + "\n";
138 logMessage += "Host :" + hostAddress;
139 GroupApiActivity.setMessageLog(logMessage);
140 msg = Message.obtain();
142 groupApiActivityObj.getHandler().sendMessage(msg);
144 Log.d(LOG_TAG, "Group Already found ");
147 }else if (resourceURI.equals("/a/light") == true) {
148 if (lights.contains((hostAddress + resourceURI)) == false) {
149 lights.add((hostAddress + resourceURI));
150 if (groupApiActivityObj != null) {
152 logMessage = "API RESULT : " + "OC_STACK_OK"
154 logMessage += "URI: " + resourceURI + "\n";
155 logMessage += "Host:" + hostAddress;
156 GroupApiActivity.setMessageLog(logMessage);
157 msg = Message.obtain();
159 groupApiActivityObj.getHandler().sendMessage(
162 foundLightHandle = OcPlatform.registerResource(ocResource);
163 Log.d(LOG_TAG, "Platform registeration done");
164 } catch (OcException e) {
165 // TODO Auto-generated catch block
170 OcPlatform.bindResource(groupResourceHandle, foundLightHandle);
171 Log.d(LOG_TAG, "Bind resource done");
172 } catch (OcException e) {
173 // TODO Auto-generated catch block
178 Log.i(LOG_TAG, "Resource is already registered!");
180 } else if (resourceURI.equalsIgnoreCase("/core/bookmark")) {
181 if (bookmarks.contains((hostAddress + resourceURI)) == false) {
182 bookmarks.add((hostAddress + resourceURI));
183 if (groupApiActivityObj != null) {
184 logMessage = "API RESULT : " + "OC_STACK_OK"
186 logMessage += "URI: " + resourceURI + "\n";
187 logMessage += "Host:" + hostAddress;
188 GroupApiActivity.setMessageLog(logMessage);
189 msg = Message.obtain();
191 groupApiActivityObj.getHandler().sendMessage(
204 private final FindCadidateResourceListener findCandidateResourceListener;
206 public GroupClient() {
207 groupManagerObj = new GroupManager();
208 actionListener = new ActionListener();
209 observeListener = new ObserveListener();
210 findCandidateResourceListener = new FindCadidateResourceListener();
213 .setFindCandidateResourceListener(findCandidateResourceListener);
214 groupManagerObj.setActionListener(actionListener);
216 groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
220 * This method creates group of the type "b.collection" handling light
223 public void createGroup() {
226 groupResourceHandle = OcPlatform.registerResource(
229 OcPlatform.BATCH_INTERFACE, null, EnumSet.of(
230 ResourceProperty.DISCOVERABLE));
231 } catch (OcException e) {
232 Log.e(LOG_TAG, "go exception");
233 Log.e(LOG_TAG, "RegisterResource error. " + e.getMessage());
237 OcPlatform.bindInterfaceToResource(groupResourceHandle, OcPlatform.GROUP_INTERFACE);
238 } catch (OcException e) {
239 // TODO Auto-generated catch block
244 OcPlatform.bindInterfaceToResource(groupResourceHandle, OcPlatform.DEFAULT_INTERFACE);
245 } catch (OcException e) {
246 // TODO Auto-generated catch block
252 * This method finds the group of type "b.collection".
254 public void findGroup() {
255 Log.d(LOG_TAG, "finding group");
257 Vector<String> types = new Vector<String>();
258 types.add(groupResourceType);
259 OCStackResult result = groupManagerObj.findCandidateResources(types, 5);
260 if (OCStackResult.OC_STACK_OK != result) {
262 "findCandidateResources returned error: " + result.name());
265 logMessage = "API RESULT : " + result.toString();
266 GroupApiActivity.setMessageLog(logMessage);
268 if (groupApiActivityObj != null) {
269 msg = Message.obtain();
271 groupApiActivityObj.getHandler().sendMessage(msg);
276 * This method finds the light resources of type "core.light".
278 public void findLightResources() {
279 Log.d(LOG_TAG, "finding light resources");
281 Vector<String> types = new Vector<String>();
282 types.add("core.light");
283 OCStackResult result = groupManagerObj.findCandidateResources(types, 5);
284 if (OCStackResult.OC_STACK_OK != result) {
286 "findCandidateResources returned error: " + result.name());
291 * This method finds the bookmark resources of type "core.bookmark".
293 public void findBookMarkResources() {
294 Log.d(LOG_TAG, "finding bookmark resources");
296 Vector<String> types = new Vector<String>();
297 types.add("core.bookmark");
298 OCStackResult result = groupManagerObj.findCandidateResources(types, 5);
299 if (OCStackResult.OC_STACK_OK != result) {
301 "findCandidateResources returned error: " + result.name());
306 * This method creates the action set for bulb on action.
308 public void createActionSetBulbOn() {
309 Log.i(LOG_TAG, "creating action set for bulb on action");
311 if (lights.size() == 0) {
313 .displayToastMessage("No Light server found in network!");
317 ActionSet actionSet = new ActionSet();
318 actionSet.actionsetName = "AllBulbOn";
320 // Create actions list
321 for (int i = 0; i < lights.size(); i++) {
322 Action action = new Action();
323 action.target = lights.get(i);
325 Capability capability = new Capability();
326 capability.capability = "power";
327 capability.status = "on";
329 action.listOfCapability.add(capability);
330 actionSet.listOfAction.add(action);
334 OCStackResult result = groupManagerObj.addActionSet(groupResource,
336 if (OCStackResult.OC_STACK_OK != result) {
337 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
340 } catch (OcException e) {
347 * This method creates the action set for bulb off action.
349 public void createActionSetBulbOff() {
350 Log.i(LOG_TAG, "creating action set for bulb off action");
352 if (lights.size() == 0) {
354 .displayToastMessage("No Light server found in network!");
358 ActionSet actionSet = new ActionSet();
359 actionSet.actionsetName = "AllBulbOff";
361 // Create actions list
362 for (int i = 0; i < lights.size(); i++) {
363 Action action = new Action();
364 action.target = lights.get(i);
366 Capability capability = new Capability();
367 capability.capability = "power";
368 capability.status = "off";
370 action.listOfCapability.add(capability);
371 actionSet.listOfAction.add(action);
375 OCStackResult result = groupManagerObj.addActionSet(groupResource,
377 if (OCStackResult.OC_STACK_OK != result) {
378 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
381 } catch (OcException e) {
388 * This method creates the recursive action set for bulb on action.
390 public void createRecursiveActionSetBulbOn() {
391 Log.i(LOG_TAG, "creating recursive action set for bulb on action");
393 if (lights.size() == 0) {
395 .displayToastMessage("No Light server found in network!");
399 ActionSet actionSet = new ActionSet();
400 actionSet.actionsetName = "AllBulbOnRecursive";
401 actionSet.setType(ActionSetType.RECURSIVE);
403 actionSet.mMonth = 0;
408 actionSet.setDelay(actionSet.getSecAbsTime());
410 // Create actions list
411 for (int i = 0; i < lights.size(); i++) {
412 Action action = new Action();
413 action.target = lights.get(i);
415 Capability capability = new Capability();
416 capability.capability = "power";
417 capability.status = "on";
419 action.listOfCapability.add(capability);
420 actionSet.listOfAction.add(action);
424 OCStackResult result = groupManagerObj.addActionSet(groupResource,
426 if (OCStackResult.OC_STACK_OK != result) {
427 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
430 } catch (OcException e) {
437 * This method creates the scheduled action set for bulb off action.
439 public void createScheduledActionSetBulbOff(long delay) {
440 Log.i(LOG_TAG, "creating scheduled action set for bulb off action");
442 if (lights.size() == 0) {
444 .displayToastMessage("No Light server found in network!");
448 ActionSet actionSet = new ActionSet();
449 actionSet.actionsetName = "AllBulbOffScheduled";
450 actionSet.setType(ActionSetType.SCHEDULED);
451 actionSet.setDelay(delay);
452 Log.i(LOG_TAG, "Set the delay of " + delay + " seconds");
454 // Create actions list
455 for (int i = 0; i < lights.size(); i++) {
456 Action action = new Action();
457 action.target = lights.get(i);
459 Capability capability = new Capability();
460 capability.capability = "power";
461 capability.status = "off";
463 action.listOfCapability.add(capability);
464 actionSet.listOfAction.add(action);
468 OCStackResult result = groupManagerObj.addActionSet(groupResource,
470 if (OCStackResult.OC_STACK_OK != result) {
471 Log.e(LOG_TAG, "addActionSet returned error: " + result.name());
474 } catch (OcException e) {
481 * This method is for executing the action Set "AllBulbOn".
483 public void executeActionSetBulbOn(long delay) {
484 Log.i(LOG_TAG, "executing the action set of bulb on action");
486 if (lights.size() == 0) {
488 .displayToastMessage("No Light server found in network!");
492 executeActionSet("AllBulbOn", delay);
496 * This method is for executing the action Set "AllBulbOff".
498 public void executeActionSetBulbOff(long delay) {
499 Log.i(LOG_TAG, "executing the action set of bulb off action");
501 if (lights.size() == 0) {
503 .displayToastMessage("No Light server found in network!");
507 executeActionSet("AllBulbOff", delay);
511 * This method is for executing the recursive action Set
512 * "AllBulbOnRecursive".
514 public void executeRecursiveActionSetBulbOn(long delay) {
515 Log.i(LOG_TAG, "executing the recursive action set of bulb on action");
517 if (lights.size() == 0) {
519 .displayToastMessage("No Light server found in network!");
523 executeActionSet("AllBulbOnRecursive", delay);
527 * This method is for executing the schedule action Set
528 * "AllBulbOffScheduled".
530 public void executeScheduledActionSetBulbOff(long delay) {
531 Log.i(LOG_TAG, "executing the schedule action set of bulb off action");
533 if (lights.size() == 0) {
535 .displayToastMessage("No Light server found in network!");
539 executeActionSet("AllBulbOffScheduled", delay);
543 * This method is for canceling the action Set "AllBulbOn".
545 public void cancelActionSetBulbOn() {
546 Log.i(LOG_TAG, "cancelling the action set of bulb on action");
548 if (lights.size() == 0) {
550 .displayToastMessage("No Light server found in network!");
554 cancelActionSet("AllBulbOn");
558 * This method is for canceling the action Set "AllBulbOff".
560 public void cancelActionSetBulbOff() {
561 Log.i(LOG_TAG, "cancelling the action set of bulb off action");
563 if (lights.size() == 0) {
565 .displayToastMessage("No Light server found in network!");
569 cancelActionSet("AllBulbOff");
573 * This method is for canceling the recursive action Set
574 * "AllBulbOnRecursive".
576 public void cancelRecursiveActionSetBulbOn() {
577 Log.i(LOG_TAG, "cancelling the recursive action set of bulb on action");
579 if (lights.size() == 0) {
581 .displayToastMessage("No Light server found in network!");
585 cancelActionSet("AllBulbOnRecursive");
589 * This method is for canceling the scheduled action Set
590 * "AllBulbOffScheduled".
592 public void cancelScheduledActionSetBulbOff() {
593 Log.i(LOG_TAG, "cancelling the scheduled action set of bulb off action");
595 if (lights.size() == 0) {
597 .displayToastMessage("No Light server found in network!");
601 cancelActionSet("AllBulbOffScheduled");
605 * This method is for getting the action Set "AllBulbOn".
607 public void getActionSetBulbOn() {
608 Log.i(LOG_TAG, "getting the action set of bulb on action");
611 OCStackResult result = groupManagerObj.getActionSet(groupResource,
613 if (OCStackResult.OC_STACK_OK != result) {
615 "getActionSetOn returned error: " + result.name());
618 } catch (OcException e) {
624 * This method is for getting the action Set "AllBulbOff".
626 public void getActionSetBulbOff() {
627 Log.i(LOG_TAG, "getting the action set of bulb off action");
630 OCStackResult result = groupManagerObj.getActionSet(groupResource,
632 if (OCStackResult.OC_STACK_OK != result) {
634 "getActionSetOn returned error: " + result.name());
637 } catch (OcException e) {
643 * This method is for deleting the action Set "AllBulbOn".
645 public void deleteActionSetBulbOn() {
646 Log.i(LOG_TAG, "deleting the action set of bulb on action");
649 OCStackResult result = groupManagerObj.deleteActionSet(
650 groupResource, "AllBulbOn");
651 if (OCStackResult.OC_STACK_OK != result) {
653 "deleteActionSet returned error : " + result.name());
656 } catch (OcException e) {
662 * This method is for deleting the action Set "AllBulbOff".
664 public void deleteActionSetBulbOff() {
665 Log.i(LOG_TAG, "deleting the action set of bulb off action");
668 OCStackResult result = groupManagerObj.deleteActionSet(
669 groupResource, "AllBulbOff");
670 if (OCStackResult.OC_STACK_OK != result) {
672 "deleteActionSet returned error : " + result.name());
675 } catch (OcException e) {
681 * This method is for observing the bulb on/off status.
683 public void observe(OcResource resource) {
684 Log.i(LOG_TAG, "Registering observer for bookmark resources status");
686 Map<String, String> queryMap = new HashMap<String, String>();
688 resource.observe(ObserveType.OBSERVE, queryMap, observeListener);
689 } catch (OcException e) {
694 private void executeActionSet(String actonSetName, long delay) {
696 OCStackResult result = groupManagerObj.executeActionSet(
697 groupResource, actonSetName, delay);
698 if (OCStackResult.OC_STACK_OK != result) {
700 "executeActionSet retuned error : " + result.name());
703 } catch (OcException e) {
708 private void cancelActionSet(String actionSetName) {
710 OCStackResult result = groupManagerObj.cancelActionSet(
711 groupResource, actionSetName);
712 if (OCStackResult.OC_STACK_OK != result) {
714 "cancelActionSet returned error : " + result.name());
717 } catch (OcException e) {