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   import org.wcb.autohome.implementations.X10Module;
8   
9   import javax.servlet.http.HttpServletRequest;
10  import java.util.Vector;
11  
12  /***
13  * Copyright (C) 1999  Walter Bogaardt
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
28  *
29  *   Project:   Home Automation Interface
30  *  Filename:  $Id: WapController.java,v 1.4 2004/02/03 21:02:27 wbogaardt Exp $
31  *  Abstract:  Web bean for x10 Wap browser support.
32  *
33  * $Log: WapController.java,v $
34  * Revision 1.4  2004/02/03 21:02:27  wbogaardt
35  * moved DeviceFactory away from rmi creation and simplified interface between gateway
36  *
37  * Revision 1.3  2004/01/16 00:53:43  wbogaardt
38  * Fixed a very obscure bug with the Macro Panel that it didn't added new
39  * 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.
40  *
41  * Revision 1.2  2004/01/15 21:05:20  wbogaardt
42  * major revamp of Modules and interfaces changes overall structure of how information is stored
43  *
44  * Revision 1.1  2004/01/12 22:12:34  wbogaardt
45  * Bean controller for WAP jsp pages
46  *
47   */
48  public class WapController {
49  
50      public static String X10_HOUSE_CODE = "house";
51      public static String X10_MODULE_ID = "id";
52      public static String ON_BUTTON = "on";
53      public static String OFF_BUTTON = "off";
54      public static String EXECUTE_BUTTON = "execute";
55      public static String COMMAND = "command";
56      public static String DESCRIPTION = "description";
57  
58      public WapController(){
59      }
60  
61      /***
62       * Gets a list of all the saved X10 modules.
63       * @return Vector of IX10Module
64       */
65      public Vector getAllX10Modules(){
66          Vector _returnVal = AutoHomeDaemonSession.getInstance().loadAllX10Devices();
67          if(_returnVal!=null){
68              return _returnVal;
69          }
70          _returnVal=new Vector();
71          _returnVal.addElement(new X10Module());
72          return _returnVal;
73      }
74  
75      /***
76       * Returns the string name of the saved serial port
77       * @return String of saved serial port name.
78       */
79      public String getSavedSerialPort(){
80          return  AutoHomeDaemonSession.getInstance().getSerialPort().getPort();
81      }
82  
83      /***
84       * If the serial port is active then return a printable string
85       * indicating connected. Otherwise, return a printable string disconnected.
86       *
87       * @return String of "Connected" or "Disconnected"
88       */
89      public String getIsPortConnected(){
90          if(AutoHomeDaemonSession.getInstance().isX10GatwayConnected())
91              return "Connected";
92          else
93              return "Disconnected";
94      }
95  
96      /***
97       * Process request information when users do form submits
98       * based on this we will save the keys, save the file, read
99       * in a new directory's build.properties file or run the ANT
100      * build script.
101      * @param request servlet form post request.
102      */
103     public void processRequest(HttpServletRequest request) {
104             String action = request.getParameter("action");
105             if(action!=null)
106             {
107                 char cHouse = this.getHouseCode(request.getParameter(X10_MODULE_ID));
108                 int iId = this.getDeviceCode(request.getParameter(X10_MODULE_ID).substring(1));
109                 if(AutoHomeDaemonSession.getInstance().isX10GatwayConnected())
110                 {
111                     if(action.equalsIgnoreCase(ON_BUTTON))
112                     {
113                         IX10Module module = new X10Module(cHouse,iId,"",request.getParameter(X10_MODULE_ID),X10DeviceConstants.APPLIANCE_MODULE_ON);
114                         this.sendCommandToX10Module(module,X10DeviceConstants.ON_ACTION);
115                     }
116                     if(action.equalsIgnoreCase(OFF_BUTTON))
117                     {
118                         IX10Module module = new X10Module(cHouse,iId,"",request.getParameter(X10_MODULE_ID),X10DeviceConstants.APPLIANCE_MODULE_ON);
119                         this.sendCommandToX10Module(module,X10DeviceConstants.OFF_ACTION);
120                     }
121                 }
122             }
123     }
124 
125     /***
126      * Sends the module interface and the command to that module off
127      * through the x10 gateway.
128      * @param module
129      * @param iCmd
130      * @return
131      */
132     private boolean sendCommandToX10Module(IX10Module module, int iCmd)
133     {
134         try
135         {
136 	    switch(iCmd)
137 		{
138 		   case X10DeviceConstants.OFF_ACTION:
139 	    		AutoHomeDaemonSession.getInstance().printMessage("Turning off "+module.getDescription());
140 		        break;
141 		   case X10DeviceConstants.ON_ACTION:
142 			    AutoHomeDaemonSession.getInstance().printMessage("Turning on "+module.getDescription());
143 		        break;
144 		 }
145             AutoHomeDaemonSession.getInstance().sendCommandToX10Module(module,iCmd);
146         }
147         catch(HomeException he)
148         {
149             return false;
150         }
151         return true;
152     }
153 
154     /***
155      * Sends an all command to a section or house code. The
156      * house code can be a character between A - P.
157      * The commands are 0 - all lights off; 1 - all lights on;
158      * 2 - all modules off;
159      * @param cHouse Valid A - P
160      * @param iCmd Valid 0 - 2
161      * @return Success or fail
162      */
163     private boolean sendCommandToSection(char cHouse, int iCmd)
164     {
165         try
166         {
167             AutoHomeDaemonSession.getInstance().sendCommandToSection(cHouse,iCmd);
168         } catch(HomeException err) {
169             return false;
170         }
171         return true;
172     }
173 
174     /***
175      * Gets the first position in the DeviceID text field
176      * and returns a Upper Case Character value.
177      */
178     private char getHouseCode(String sDeviceID)
179     {
180         char cReturnValue='A';
181         try
182         {
183             cReturnValue = sDeviceID.toUpperCase().charAt(0);
184         }
185         catch(Exception err)
186         {
187             cReturnValue = 'A';
188         }
189         return cReturnValue;
190     }
191 
192     /***
193      * Gets the last positions in the DeviceID text field
194      * and returns an integer value.  If the entered integers
195      * are greater than 16 then 16 is returned;
196      */
197     private int getDeviceCode(String sDeviceID)
198     {
199         int returnValue = 0;
200         try
201         {
202             returnValue = Integer.parseInt(sDeviceID);
203         }
204         catch(Exception err)
205         {
206                   returnValue = 1;
207         }
208         if (returnValue >16)
209         {
210             //invalude digit
211             returnValue = 16;
212         }
213         return returnValue;
214     }
215 }