View Javadoc

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  * Copyright (C) 1999  Walter Bogaardt
15  *
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2 of the License, or (at your option) any later version.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
29  *
30  *   Project:   Home Automation Interface
31  *  Filename:  $Id: X10WebComponent.java,v 1.20 2004/02/03 21:02:27 wbogaardt Exp $
32  *  Abstract:  Web bean for x10 jsp.
33  *
34  * $Log: X10WebComponent.java,v $
35  * Revision 1.20  2004/02/03 21:02:27  wbogaardt
36  * moved DeviceFactory away from rmi creation and simplified interface between gateway
37  *
38  * Revision 1.19  2004/01/16 00:53:43  wbogaardt
39  * Fixed a very obscure bug with the Macro Panel that it didn't added new
40  * x10 devices to the drop down of available x10 device for the macro. Modified Macro triggers to change the events to integer verses strings cleaner this way.
41  *
42  * Revision 1.18  2004/01/15 21:05:20  wbogaardt
43  * major revamp of Modules and interfaces changes overall structure of how information is stored
44  *
45  * Revision 1.17  2003/12/18 06:10:15  wbogaardt
46  * fixed problem with componets sending command
47  *
48  * Revision 1.16  2003/12/17 23:34:57  wbogaardt
49  * fixed capturing of house code to pass it to upper case.
50  *
51  * Revision 1.15  2003/12/17 21:27:06  wbogaardt
52  * Improved error traping of CM11A send command to all house
53  *
54  * Revision 1.14  2003/12/16 22:08:37  wbogaardt
55  * refactored events daemon handeling
56  *
57  * Revision 1.13  2003/12/13 00:29:09  wbogaardt
58  * added cm11a gateway montitor listener option.
59  *
60  * Revision 1.12  2003/12/12 23:17:36  wbogaardt
61  * javadoc comments refactored methods so they are more descriptive
62  *
63  * Revision 1.11  2003/12/12 05:13:30  wbogaardt
64  * fixed web component to say the module description
65  *
66  * Revision 1.10  2003/12/12 04:27:47  wbogaardt
67  * added some speech ability to web interface
68  *
69  * Revision 1.9  2003/12/11 22:01:41  wbogaardt
70  * removed deprecated method of loading speach engine also removed reference to autohome adminsession from X10WebComponent
71  *
72  * Revision 1.8  2003/12/10 22:48:15  wbogaardt
73  * trap error and do not send x10 command through interface if it determined that serial port is not connected
74  *
75  * Revision 1.7  2003/12/10 19:24:42  wbogaardt
76  * Added new serial port controller bean and modified X10Webcomponent
77  *
78  * Revision 1.6  2003/12/09 21:36:53  wbogaardt
79  * modified Session objects so that works with gui app as well as web app. Extended functionality of serial page in web app, and improved error traping of exceptions
80  *
81  * Revision 1.5  2003/12/09 00:37:06  wbogaardt
82  * moved methods from authomAdminSession to parent class
83  *
84  * Revision 1.4  2003/12/08 23:46:04  wbogaardt
85  * added javadoc comments and cleaned up Main class
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             //invalude digit
275             returnValue = 16;
276         }
277         return returnValue;
278     }
279 }
280