1 package org.wcb.plugins.beans;
2
3 import org.wcb.autohome.AutoHomeDaemonSession;
4 import org.wcb.autohome.implementations.SerialPortBean;
5 import org.wcb.autohome.interfaces.X10DeviceConstants;
6 import org.wcb.autohome.exceptions.HomeException;
7
8 import javax.servlet.http.HttpServletRequest;
9 import java.util.Vector;
10 import java.util.Enumeration;
11
12
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 public class SerialPortController {
54
55 private String sStatus;
56
57 public String getStatusMessage(){
58 return sStatus;
59 }
60
61 /***
62 * This will return a vector of available serial ports.
63 * @return Vector string list of avaliable serial ports
64 */
65 public Vector getSerialPorts(){
66 Vector _returnVal = AutoHomeDaemonSession.getInstance().getAvailablePorts();
67 if(_returnVal!=null){
68 return _returnVal;
69 }
70 return _returnVal;
71 }
72
73 /***
74 * Returns the string name of the saved serial port
75 * @return String name of saved serial port.
76 */
77 public String getSavedSerialPort(){
78 return AutoHomeDaemonSession.getInstance().getSerialPort().getPort();
79 }
80
81 /***
82 * If the serial port is active then return a printable string
83 * indicating connected. Otherwise, return a printable string disconnected.
84 *
85 * @return String from boolean indicating "Connected" or "Disconnected";
86 */
87 public String getIsPortConnected(){
88 if(AutoHomeDaemonSession.getInstance().isX10GatwayConnected())
89 return "Connected";
90 else
91 return "Disconnected";
92 }
93
94 /***
95 * Gets the current saved serial port baud rate
96 * @return Saved baud rate or defaults to 9600
97 */
98 public int getBaudRate(){
99 return AutoHomeDaemonSession.getInstance().getSerialPort().getBaud();
100 }
101
102 /***
103 * Gets the current saved data bit rate of the serial port.
104 * @return Saved data bit rate for serial port or default of 8
105 */
106 public int getDataBitRate(){
107 return AutoHomeDaemonSession.getInstance().getSerialPort().getDataBit();
108 }
109
110 /***
111 * Gets the current saved stop bit rate of the serial port.
112 * @return Saved stop bit rate of serial port or default of 1
113 */
114 public int getStopBitRate(){
115 return AutoHomeDaemonSession.getInstance().getSerialPort().getStopBit();
116 }
117
118 /***
119 * Gets the current saved parity of the serial port. The string
120 * return can be Odd, Even, or None.
121 * @return Saved partiy for serial port of defaults to "None"
122 */
123 public String getParity(){
124 String _returnString = AutoHomeDaemonSession.getInstance().getSerialPort().getParity();
125 if(_returnString==null)
126 return "None";
127 return _returnString;
128 }
129
130 /***
131 * This takes returns an html representation of a combo
132 * drop down box with the selected serial port
133 *
134 * @return a string of serial ports with html tags in place for select drop down
135 */
136 public String getSelectedSerialPortHTML()
137 {
138 Enumeration eSerial = this.getSerialPorts().elements();
139 String sSelected = this.getSavedSerialPort();
140 String sIsSelected = "";
141 StringBuffer returnBuffer = new StringBuffer();
142 while(eSerial.hasMoreElements()){
143 String value = (String)eSerial.nextElement();
144 if(value.equalsIgnoreCase(sSelected))
145 sIsSelected="selected";
146 else
147 sIsSelected="";
148 returnBuffer.append("<option value="+value+" "+sIsSelected+">"+value);
149 }
150 return returnBuffer.toString();
151 }
152
153 /***
154 * This returns an html dropdown of options of diffent x10 devices.
155 * @return String with select drop down of X10 gateway devices
156 */
157 public String getHTMLX10Device()
158 {
159 StringBuffer returnBuffer = new StringBuffer();
160 if(AutoHomeDaemonSession.getInstance().getX10GatewayType() == X10DeviceConstants.CM11A_TRANSMITTER)
161 {
162 returnBuffer.append("<option value="+X10DeviceConstants.CM11A_TRANSMITTER+" selected>CM11A");
163 returnBuffer.append("<option value="+X10DeviceConstants.CM17A_TRANSMITTER+">CM17A \"Firecracker\"");
164 }
165 else
166 {
167 returnBuffer.append("<option value="+X10DeviceConstants.CM11A_TRANSMITTER+">CM11A");
168 returnBuffer.append("<option value="+X10DeviceConstants.CM17A_TRANSMITTER+" selected>CM17A");
169 }
170 return returnBuffer.toString();
171 }
172
173 /***
174 * Process request information when users do form submits
175 * based on this we will save the keys, save the file, read
176 * in a new directory's build.properties file or run the ANT
177 * build script.
178 * @param request Http servlet request from form post call.
179 */
180 public void processRequest(HttpServletRequest request) {
181 sStatus = "";
182 if(request.getParameter("x10File")!=null)
183 loadNewFile(request.getParameter("x10File"));
184 if(request.getMethod().equalsIgnoreCase("post")){
185 String action = request.getParameter("action");
186 String sSerialName = request.getParameter("serial");
187 int iBaud = Integer.parseInt(request.getParameter("baud_rate"));
188 int iStop = Integer.parseInt(request.getParameter("stop_bit"));
189 int iDataBit= Integer.parseInt(request.getParameter("data_bit"));
190 int iInterfaceType = Integer.parseInt(request.getParameter("x10device"));
191 String sParity = request.getParameter("parity");
192 if(action!=null){
193 if(action.equalsIgnoreCase("connect"))
194 {
195 this.saveParameters(iInterfaceType,sSerialName,iBaud,iStop,iDataBit,sParity);
196 try
197 {
198 AutoHomeDaemonSession.getInstance().connectPortToDevice(iInterfaceType);
199 }
200 catch(HomeException he)
201 {
202 sStatus = "Problem - "+he;
203 }
204 }
205 if(action.equalsIgnoreCase("disconnect"))
206 {
207 try{
208 AutoHomeDaemonSession.getInstance().closeSerialPort();
209 }
210 catch(HomeException he)
211 {
212 sStatus = "Problem - "+he;
213 }
214 }
215 }
216 }
217 }
218
219 /***
220 * This saves the settings that were submitted to the servlet through the user reqest.
221 * @param iX10Device
222 * @param sSerialName
223 * @param iBaud
224 * @param iStop
225 * @param iData
226 * @param sParity
227 */
228 private void saveParameters(int iX10Device, String sSerialName, int iBaud, int iStop, int iData, String sParity)
229 {
230 AutoHomeDaemonSession.getInstance().setInterfaceType(iX10Device);
231 SerialPortBean serialBean = new SerialPortBean(sSerialName);
232 serialBean.setBaud(iBaud);
233 serialBean.setStopBit(iStop);
234 serialBean.setDataBit(iData);
235 serialBean.setParity(sParity);
236 AutoHomeDaemonSession.getInstance().setSerialPort(serialBean);
237 AutoHomeDaemonSession.getInstance().saveServerFile();
238 }
239
240 private void loadNewFile(String filename){
241 AutoHomeDaemonSession.getInstance().loadX10File(filename);
242 }
243 }