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 ******************************************************************/
21 * @file ActionSet.java
23 * @brief This file contains class which provides functions to retrieve ActionSet from plain text.
26 package org.iotivity.service.tm;
28 import java.util.StringTokenizer;
29 import java.util.Vector;
31 import android.util.Log;
34 * This class provides functions to retrieve ActionSet from plain text.
35 * An ActionSet is a set of descriptions of actions needed by remote
36 * devices as members of a specific group. To create an ActionSet, one
37 * needs to know the Delimeter serialization.
40 public class ActionSet extends Time {
42 * String Tag for Logging
44 private static final String LOG_TAG = "ActionSet";
46 * A first segment before the first asterisk(*) in ActionSet string is an
49 public String actionsetName;
51 * Action instance stored in listOfAction vector variable.
53 public Vector<Action> listOfAction = new Vector<Action>();
56 * This function generates an ActionSet String which contains ActionSet
57 * name, delay in seconds, ActionSetType which can be 0(NONE), 1(SCHEDULED)
58 * or 2(RECURSIVE) and a list of actions. (Example: movieTime*10
59 * 1*uri=coap://10.251.44.228:49858/a/light|power=
60 * OFF*uri=coap://10.251.44.228:49858). The first segment before the first
61 * asterisk(*) is an ActionSet name. The second segment goes before the next
62 * asterisk. In the above example, 10 is the second segment which signifies
63 * time delay in seconds followed by a space and ActionSetType which
64 * signifies whether an ActionSet has to be triggered Recursively(2) or
65 * Scheduled manner(1) or immediately(0).
66 * "uri=coap://10.251.44.228:49858/a/light|power=10" is the third segment.
67 * This can be also divided into two sub segments by a vertical bar(|): URI
68 * and a pair of attribute key and value.
70 * @return String for a specific action.
74 public String toString() {
75 StringBuilder result = new StringBuilder();
78 result.append(actionsetName + "*");
80 // Append delay and type
81 result.append(getDelay() + " " + getType().ordinal() + "*");
83 // Append list of actions
84 for (int i = 0; i < listOfAction.size(); i++) {
87 result.append(listOfAction.elementAt(i).toString());
90 return result.toString();
94 * This function extracts a list of Actions from plain text and constructs
97 * @param actionsetString
98 * ActionSet in String format. (Example: movieTime*10
99 * 1*uri=coap://10.251.44.228:49858/a/light|power=
100 * OFF*uri=coap://10.251.44.228:49858). The first segment before
101 * the first asterisk(*) is an ActionSet name. The second segment
102 * goes before the next asterisk. In the above example, 10 is the
103 * second segment which signifies time delay in seconds followed
104 * by a space and ActionSetType which signifies whether an
105 * ActionSet has to be triggered Recursively(2) or Scheduled
106 * manner(1) or immediately(0).
107 * "uri=coap://10.251.44.228:49858/a/light|power=10" is the third
108 * segment. This can be also divided into two sub segments by a
109 * vertical bar(|): URI and a pair of attribute key and value).
111 * @return ActionSet which is a set of descriptions of actions needed by
112 * remote devices as members of a specific group.
115 public static ActionSet toActionSet(String actionsetString) {
116 if (0 == actionsetString.length()) {
120 ActionSet result = new ActionSet();
121 StringTokenizer tokenizer = new StringTokenizer(actionsetString, "*");
122 boolean actionNameFlag = false;
123 while (tokenizer.hasMoreTokens()) {
124 String segment = tokenizer.nextToken();
125 if (false == actionNameFlag) {
126 if (true == segment.contains("|")
127 || true == segment.contains("=")) {
128 Log.e(LOG_TAG, "Invalid actionset name string!");
132 result.actionsetName = segment;
133 actionNameFlag = true;
135 Action action = Action.toAction(segment);
136 if (null == action) {
137 Log.e(LOG_TAG, "Failed to convert string to Action class!");
141 result.listOfAction.add(action);