View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  
19  package org.indi.objects;
20  
21  import java.util.Date;
22  
23  /**
24   * A indivector hosting indiswitches
25   * 
26   * @author Dirk Hünniger
27   * 
28   */
29  public class SwitchVector extends Vector<Switch> {
30      /**
31       * 
32       */
33      private static final long serialVersionUID = 1L;
34      /**
35       * a switch rule descibing how many switches in this vector ca be set to On
36       * at the same time.
37       */
38      private SwitchRule switchRule;
39  
40      /**
41       * class constructor
42       * 
43       * @param device
44       *                the name of the device this SwitchVector is associated
45       *                with
46       * @param name
47       *                the name of this SwitchVector
48       * @param timestamp
49       *                the timestamp to be sent along with this vector
50       */
51      public SwitchVector(String device, String name, String timestamp) {
52  	super(device, name, timestamp);
53      }
54  
55      /**
56       * class constructor
57       * 
58       * @param device
59       *                the name of the device this SwitchVector is associated
60       *                with
61       * @param name
62       *                the name of this SwitchVector
63       * @param label
64       *                the label to be used for this SwitchVector by a GUI
65       * @param group
66       *                the group this vector belongs to
67       * @param state
68       *                the State this vector is in
69       * @param permission
70       *                the permissions the user has to change the properties of
71       *                this vector
72       * @param timeout
73       *                the worst case time it takes for a property of this vector
74       *                to change
75       * @param timestamp
76       *                the timestamp to be sent along with this vector
77       * @param switchRule
78       *                a rule defining how switches can be On at the same time
79       * @param message
80       *                a message to be sent along with this vector
81       */
82      public SwitchVector(String device, String name, String label, String group,
83  	    State state, Permission permission, double timeout,
84  	    String timestamp, SwitchRule switchRule, String message) {
85  	super(device, name, label, group, state, permission, timeout,
86  		timestamp, message);
87  	this.switchRule = switchRule;
88      }
89  
90      /**
91       * class constructor
92       * 
93       * @param device
94       *                the name of the device this SwitchVector is associated
95       *                with
96       * @param name
97       *                the name of this SwitchVector
98       * @param state
99       *                the State this vector is in
100      * @param timeout
101      *                the worst case time it takes for a property of this vector
102      *                to change
103      * @param timestamp
104      *                the timestamp to be sent along with this vector
105      * @param message
106      *                a message to be sent along with this vector
107      */
108     public SwitchVector(String device, String name, State state,
109 	    double timeout, String timestamp, String message) {
110 	super(device, name, state, timeout, timestamp, message);
111     }
112 
113     /**
114      * class constructor
115      * 
116      * @param device
117      *                the name of the device this SwitchVector is associated
118      *                with
119      * @param name
120      *                the name of this SwitchVector
121      * @param label
122      *                the label to be used for this SwitchVector by a GUI
123      * @param group
124      *                the group this vector belongs to
125      * @param state
126      *                the State this vector is in
127      * @param permission
128      *                the permissions the user has to change the properties of
129      *                this vector
130      * @param timeout
131      *                the worst case time it takes for a property of this vector
132      *                to change
133      * @param switchRule
134      *                a rule defining how switches can be On at the same time
135      */
136     public SwitchVector(String device, String name, String label, String group,
137 	    State state, Permission permission, SwitchRule switchRule,
138 	    double timeout) {
139 	super(device, name, label, group, state, permission, timeout, null,
140 		null);
141 	this.switchRule = switchRule;
142     }
143 
144     /**
145      * 
146      * @return the switchrule of this vector
147      */
148     public SwitchRule getSwitchRule() {
149 	return this.switchRule;
150     }
151 
152     /**
153      * convert a switch rule to a string as used by the indi protocol
154      * 
155      * @param rule
156      *                the switchrule to be converted
157      * @return the string representation of the switchrule
158      */
159     protected static String switchRuleToString(SwitchRule rule) {
160 	switch (rule) {
161 	case OneOfMany:
162 	    return "OneOfMany";
163 	case AtMostOne:
164 	    return "AtMostOne";
165 	case AnyOfMany:
166 	    return "AnyOfMany";
167 	default:
168 	    throw new RuntimeException();
169 	}
170     }
171 
172     @Override
173     public String getStartTag(TransferType type, String message) {
174 	switch (type) {
175 	case Def:
176 	    return "<def" + getType() + " device=\"" + this.device
177 		    + "\" name=\"" + this.name + "\" label=\"" + this.label
178 		    + "\" group=\"" + this.group + "\" state=\""
179 		    + Light.stateToString(this.state) + "\" perm=\""
180 		    + permissionToString(this.permission) + "\" rule=\""
181 		    + switchRuleToString(this.switchRule) + "\" timeout=\""
182 		    + Double.toString(this.timeout) + "\" timestamp=\""
183 		    + getDateFormat().format(new Date())
184 		    + ((message == null) ? "" : "\" message=\"" + message)
185 		    + "\">";
186 	default:
187 	    return super.getStartTag(type, message);
188 	}
189     }
190 
191     @Override
192     protected String getType() {
193 	return "SwitchVector";
194     }
195 }