1 package org.wcb.plugins.beans;
2
3 import org.wcb.autohome.AutoHomeDaemonSession;
4 import org.wcb.autohome.exceptions.HomeException;
5 import org.wcb.autohome.interfaces.IX10Module;
6 import org.wcb.autohome.interfaces.X10DeviceConstants;
7
8 import java.util.Vector;
9
10 import org.wcb.autohome.implementations.X10Module;
11 import org.wcb.autohome.implementations.Macro;
12 import javax.servlet.http.HttpServletRequest;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public class X10WebComponent {
89
90 public static String X10_HOUSE_CODE = "house";
91 public static String X10_MODULE_ID = "id";
92 public static String ON_BUTTON = "on";
93 public static String OFF_BUTTON = "off";
94 public static String EXECUTE_BUTTON = "execute";
95 public static String COMMAND = "command";
96 public static String DESCRIPTION = "description";
97
98 public X10WebComponent(){
99 }
100
101 /***
102 * Gets a list of all the saved X10 modules.
103 * @return Vector of IX10Module
104 */
105 public Vector getAllX10Modules(){
106 Vector _returnVal = AutoHomeDaemonSession.getInstance().loadAllX10Devices();
107 if(_returnVal!=null){
108 return _returnVal;
109 }
110 _returnVal=new Vector();
111 _returnVal.addElement(new X10Module());
112 return _returnVal;
113 }
114
115 /***
116 * Returns the string name of the saved serial port
117 * @return String of saved serial port name.
118 */
119 public String getSavedSerialPort(){
120 return AutoHomeDaemonSession.getInstance().getSerialPort().getPort();
121 }
122
123 /***
124 * If the serial port is active then return a printable string
125 * indicating connected. Otherwise, return a printable string disconnected.
126 *
127 * @return String of "Connected" or "Disconnected"
128 */
129 public String getIsPortConnected(){
130 if(AutoHomeDaemonSession.getInstance().isX10GatwayConnected())
131 return "Connected";
132 else
133 return "Disconnected";
134 }
135
136 /***
137 * This will return a vector of X10Event these
138 * can be cast to the IX10Event interface
139 * @return a vector of IMacro objects
140 */
141 public Vector getMacros(){
142 Vector _returnVal = AutoHomeDaemonSession.getInstance().loadAllCM11AMacros();
143 if(_returnVal!=null){
144 return _returnVal;
145 }
146 _returnVal=new Vector();
147 _returnVal.addElement(new Macro());
148 return _returnVal;
149 }
150
151 /***
152 * Process request information when users do form submits
153 * based on this we will save the keys, save the file, read
154 * in a new directory's build.properties file or run the ANT
155 * build script.
156 * @param request servlet form post request.
157 */
158 public void processRequest(HttpServletRequest request) {
159 if(request.getMethod().equalsIgnoreCase("post"))
160 {
161 String action = request.getParameter("action");
162 if(action!=null)
163 {
164 char cHouse = this.getHouseCode(request.getParameter(X10_HOUSE_CODE));
165 int iId = this.getDeviceCode(request.getParameter(X10_MODULE_ID));
166 String sDescription = request.getParameter(DESCRIPTION);
167 if(AutoHomeDaemonSession.getInstance().isX10GatwayConnected())
168 {
169 if(action.equalsIgnoreCase(ON_BUTTON))
170 {
171 IX10Module module = new X10Module(cHouse,iId,"Name",sDescription,X10DeviceConstants.APPLIANCE_MODULE_ON);
172 this.sendCommandToX10Module(module,X10DeviceConstants.ON_ACTION);
173 }
174 if(action.equalsIgnoreCase(OFF_BUTTON))
175 {
176 IX10Module module = new X10Module(cHouse,iId,"Name",sDescription,X10DeviceConstants.APPLIANCE_MODULE_ON);
177 this.sendCommandToX10Module(module,X10DeviceConstants.OFF_ACTION);
178 }
179 if(action.equalsIgnoreCase(EXECUTE_BUTTON))
180 {
181 int iCommand = Integer.parseInt(request.getParameter(COMMAND));
182 this.sendCommandToSection(cHouse,iCommand);
183 }
184 }
185 }
186 }
187 }
188
189 /***
190 * Sends the module interface and the command to that module off
191 * through the x10 gateway.
192 * @param module
193 * @param iCmd
194 * @return
195 */
196 private boolean sendCommandToX10Module(IX10Module module, int iCmd)
197 {
198 try
199 {
200 switch(iCmd)
201 {
202 case X10DeviceConstants.OFF_ACTION:
203 AutoHomeDaemonSession.getInstance().printMessage("Turning off "+module.getDescription());
204 break;
205 case X10DeviceConstants.ON_ACTION:
206 AutoHomeDaemonSession.getInstance().printMessage("Turning on "+module.getDescription());
207 break;
208 }
209 AutoHomeDaemonSession.getInstance().sendCommandToX10Module(module,iCmd);
210 }
211 catch(HomeException he)
212 {
213 return false;
214 }
215 return true;
216 }
217
218 /***
219 * Sends an all command to a section or house code. The
220 * house code can be a character between A - P.
221 * The commands are 0 - all lights off; 1 - all lights on;
222 * 2 - all modules off;
223 * @param cHouse Valid A - P
224 * @param iCmd Valid 0 - 2
225 * @return Success or fail
226 */
227 private boolean sendCommandToSection(char cHouse, int iCmd)
228 {
229 try
230 {
231 AutoHomeDaemonSession.getInstance().sendCommandToSection(cHouse,iCmd);
232 } catch(HomeException err) {
233 return false;
234 }
235 return true;
236 }
237
238 /***
239 * Gets the first position in the DeviceID text field
240 * and returns a Upper Case Character value.
241 */
242 private char getHouseCode(String sDeviceID)
243 {
244 char cReturnValue='A';
245 try
246 {
247 cReturnValue = sDeviceID.toUpperCase().charAt(0);
248 }
249 catch(Exception err)
250 {
251 cReturnValue = 'A';
252 }
253 return cReturnValue;
254 }
255
256 /***
257 * Gets the last positions in the DeviceID text field
258 * and returns an integer value. If the entered integers
259 * are greater than 16 then 16 is returned;
260 */
261 private int getDeviceCode(String sDeviceID)
262 {
263 int returnValue = 0;
264 try
265 {
266 returnValue = Integer.parseInt(sDeviceID);
267 }
268 catch(Exception err)
269 {
270 returnValue = 1;
271 }
272 if (returnValue >16)
273 {
274
275 returnValue = 16;
276 }
277 return returnValue;
278 }
279 }
280