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.implementations.X10Events;
6 import org.wcb.autohome.implementations.X10Module;
7 import org.wcb.autohome.interfaces.X10DeviceConstants;
8 import org.wcb.autohome.interfaces.IX10Events;
9 import org.wcb.autohome.interfaces.IX10Module;
10
11 import javax.servlet.http.HttpServletRequest;
12 import java.util.Vector;
13 import java.util.Calendar;
14 import java.util.GregorianCalendar;
15 import java.text.SimpleDateFormat;
16 import java.text.ParseException;
17
18 /***
19 * Copyright (C) 1999 Walter Bogaardt
20 *
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2 of the License, or (at your option) any later version.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 *
35 * Project: Home Automation Interface
36 * Filename: $Id: EventsController.java,v 1.8 2004/01/16 00:53:43 wbogaardt Exp $
37 * Abstract: Web bean for x10 jsp events and allow activating and deactivation of alice events..
38 *
39 * $Log: EventsController.java,v $
40 * Revision 1.8 2004/01/16 00:53:43 wbogaardt
41 * Fixed a very obscure bug with the Macro Panel that it didn't added new
42 * 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.
43 *
44 * Revision 1.7 2004/01/15 21:05:20 wbogaardt
45 * major revamp of Modules and interfaces changes overall structure of how information is stored
46 *
47 * Revision 1.6 2003/12/18 18:05:05 wbogaardt
48 * fixed events controller bug. When session timed out it deleted all of the events. Fixed bug with refresh button kept adding new items rather than refreshing the line item.
49 *
50 * Revision 1.5 2003/12/17 23:34:57 wbogaardt
51 * fixed capturing of house code to pass it to upper case.
52 *
53 * Revision 1.4 2003/12/17 18:14:06 wbogaardt
54 * moved strings to static variables for both jsp and servlet
55 *
56 * Revision 1.3 2003/12/17 02:13:21 wbogaardt
57 * Initial phase of events controller works
58 *
59 * Revision 1.2 2003/12/16 23:03:09 wbogaardt
60 * added update and delete function to events controller and removed dead imports in module controller
61 *
62 * Revision 1.1 2003/12/16 22:09:23 wbogaardt
63 * new events controller for website
64 *
65 */
66 public class EventsController {
67
68 private Vector vEvents;
69 private SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
70 public static String X10_MODULE = "x10Module";
71 public static String DESCRIPTION = "description";
72 public static String EVENT_ACTION = "eventAction";
73 public static String HH_MM = "time";
74 public static String AM_PM = "time_pm";
75 public static String MONDAY = "monday";
76 public static String TUESDAY = "tuesday";
77 public static String WEDNESDAY = "wednesday";
78 public static String THURSDAY = "thursday";
79 public static String FRIDAY = "friday";
80 public static String SATURDAY = "saturday";
81 public static String SUNDAY = "sunday";
82 public static String ACTION = "action";
83 public static String ADD_BUTTON = "add";
84 public static String REFRESH_BUTTON = "refresh";
85 public static String DELETE_BUTTON = "delete";
86 public static String RUN_BUTTON = "run";
87 public static String STOP_BUTTON = "stop";
88 public static String EVENT_ROW = "row";
89
90
91 public EventsController(){
92 }
93
94 /***
95 * Process request information when users do form submits
96 * based on this we will save the keys, save the file, read
97 * in a new directory's build.properties file or run the ANT
98 * build script.
99 * @param request servlet form post request.
100 */
101 public void processRequest(HttpServletRequest request) {
102 if(request.getMethod().equalsIgnoreCase("post"))
103 {
104 String action = request.getParameter(ACTION);
105 if(action!=null)
106 {
107 String sX10Module = request.getParameter(X10_MODULE);
108 String sDescription = request.getParameter(DESCRIPTION);
109 String sEventType = request.getParameter(EVENT_ACTION);
110 String sTime = request.getParameter(HH_MM);
111 String sAMPM = request.getParameter(AM_PM);
112 boolean bMonday = this.getBooleanFromCheckBox(request.getParameter(MONDAY));
113 boolean bTuesday = this.getBooleanFromCheckBox(request.getParameter(TUESDAY));
114 boolean bWednesday = this.getBooleanFromCheckBox(request.getParameter(WEDNESDAY));
115 boolean bThursday = this.getBooleanFromCheckBox(request.getParameter(THURSDAY));
116 boolean bFriday = this.getBooleanFromCheckBox(request.getParameter(FRIDAY));
117 boolean bSaturday = this.getBooleanFromCheckBox(request.getParameter(SATURDAY));
118 boolean bSunday = this.getBooleanFromCheckBox(request.getParameter(SUNDAY));
119 if(action.equalsIgnoreCase(ADD_BUTTON))
120 {
121 X10Events returnEvents = new X10Events();
122 returnEvents.setDescription(sDescription);
123 returnEvents.setMonday(bMonday);
124 returnEvents.setTuesday(bTuesday);
125 returnEvents.setWednesday(bWednesday);
126 returnEvents.setThursday(bThursday);
127 returnEvents.setFriday(bFriday);
128 returnEvents.setSaturday(bSaturday);
129 returnEvents.setSunday(bSunday);
130 returnEvents.setDeviceModule(new X10Module(this.getHouseCode(sX10Module),this.getDeviceCode(sX10Module),"","",X10DeviceConstants.LAMP_MODULE_ON));
131
132
133 returnEvents.setTime(this.getTime(sTime,sAMPM));
134 returnEvents.setAction(sEventType);
135 this.addEvent(returnEvents);
136 }
137 if(action.equalsIgnoreCase(REFRESH_BUTTON)){
138 int iRow = Integer.parseInt(request.getParameter(EVENT_ROW));
139 X10Events returnEvents = new X10Events();
140 returnEvents.setDescription(sDescription);
141 returnEvents.setMonday(bMonday);
142 returnEvents.setTuesday(bTuesday);
143 returnEvents.setWednesday(bWednesday);
144 returnEvents.setThursday(bThursday);
145 returnEvents.setFriday(bFriday);
146 returnEvents.setSaturday(bSaturday);
147 returnEvents.setSunday(bSunday);
148 returnEvents.setDeviceModule(new X10Module(this.getHouseCode(sX10Module),this.getDeviceCode(sX10Module),"","",X10DeviceConstants.LAMP_MODULE_ON));
149
150
151 returnEvents.setTime(this.getTime(sTime,sAMPM));
152 returnEvents.setAction(sEventType);
153 this.refreshEvent(returnEvents, iRow);
154 }
155 if(action.equalsIgnoreCase(DELETE_BUTTON)){
156 int iRow = Integer.parseInt(request.getParameter(EVENT_ROW));
157 this.deleteRow(iRow);
158 }
159 if(AutoHomeDaemonSession.getInstance().isX10GatwayConnected())
160 {
161 if(action.equalsIgnoreCase(RUN_BUTTON))
162 {
163 AutoHomeDaemonSession.getInstance().runEventsDaemon();
164 } else if(action.equalsIgnoreCase(STOP_BUTTON))
165 {
166 AutoHomeDaemonSession.getInstance().stopEventsDaemon();
167 }
168 }
169 }
170 }
171 }
172
173 /***
174 * Allows adding of new events to the list.
175 *
176 * @param xEvent X10Events object to be added to the vector
177 * @return success on saved file
178 */
179 private boolean addEvent(X10Events xEvent)
180 {
181 vEvents = AutoHomeDaemonSession.getInstance().loadAllAliceEvents();
182 if(vEvents != null)
183 {
184 vEvents.add(xEvent);
185 AutoHomeDaemonSession.getInstance().saveAllAliceEvents(vEvents);
186 return this.saveAllData();
187 }
188 else if (vEvents == null) {
189 vEvents = new Vector();
190 vEvents.addElement(xEvent);
191 AutoHomeDaemonSession.getInstance().saveAllAliceEvents(vEvents);
192 return this.saveAllData();
193 }
194 return false;
195 }
196
197 /***
198 * This saves the alice events in a vector then persists this information
199 * to the servlet file. If it is a new event then it is simply appended.
200 * @param event Event to save in the vector.
201 * @param iRow row number in the vector to replace.
202 * @return true indicates success in saving file.
203 */
204 private boolean refreshEvent(X10Events event, int iRow){
205 if(vEvents != null)
206 {
207 vEvents.setElementAt(event,iRow);
208 AutoHomeDaemonSession.getInstance().saveAllAliceEvents(vEvents);
209 return this.saveAllData();
210 }
211 else if (vEvents == null) {
212 vEvents = new Vector();
213 vEvents.addElement(event);
214 AutoHomeDaemonSession.getInstance().saveAllAliceEvents(vEvents);
215 return this.saveAllData();
216 }
217 return false;
218 }
219
220 /***
221 * Saves the data to the x10 file in the web application.
222 * @return true indicates successful saving file.
223 */
224 private boolean saveAllData()
225 {
226 try
227 {
228 AutoHomeDaemonSession.getInstance().saveFile(AutoHomeDaemonSession.getInstance().getServletPath()+System.getProperty("file.separator")+"x10default.x10");
229 }
230 catch(HomeException he)
231 {
232 System.err.println("Home exception "+he);
233 return false;
234 }
235 return true;
236 }
237
238 /***
239 * Takes the time in hh:mm format and the string of AM or PM
240 * and returns a calendar object.
241 * @param sTime string as in hh:mm
242 * @param sPM string either AM or PM
243 * @return calendar object of the parameter.
244 */
245 private Calendar getTime(String sTime, String sPM)
246 {
247 try {
248 Calendar timestamp = new GregorianCalendar();
249 String ampm = "AM";
250 if(sPM.equalsIgnoreCase("AM"))
251 ampm=" AM";
252 else {
253 ampm=" PM";
254 }
255 timestamp.setTime(sdf.parse(sTime+ampm));
256 return timestamp;
257 } catch(ParseException pe){
258 return new GregorianCalendar();
259 }
260 }
261 /***
262 * This deletes the table row from the table model
263 * and then updates the Hash Table to set the
264 * Key value(ModuleID) to null;
265 * @param iRow row item in the vector to be deleted.
266 */
267 private void deleteRow(int iRow) {
268 vEvents.remove(iRow);
269 AutoHomeDaemonSession.getInstance().saveAllAliceEvents(vEvents);
270 }
271
272 /***
273 * Returns a boolean value based on passed in servlet form request
274 * parameter.
275 * @param sValue Request parameter string
276 * @return true if html check box item was checked.
277 */
278 private boolean getBooleanFromCheckBox(String sValue)
279 {
280 if(sValue!=null && sValue.equalsIgnoreCase("true"))
281 return true;
282 return false;
283 }
284
285 /***
286 * This message lets the jsp know if the events
287 * daemon is running in the background. The appropriate
288 * message will be displayed if it is or is not running.
289 * @return Printable message
290 */
291 public String isDaemonRunningMessage(){
292 if(AutoHomeDaemonSession.getInstance().isEventDaemonRunning())
293 {
294 return "Alice events are running!";
295 }
296 return "Alice events are not running";
297 }
298 /***
299 * This will return a vector of X10Event these
300 * can be cast to the IX10Event interface
301 * @return Vector of IX10Event objects
302 */
303 public Vector getAllEvents(){
304 vEvents = AutoHomeDaemonSession.getInstance().loadAllAliceEvents();
305 if(vEvents!=null){
306 return vEvents;
307 }
308 vEvents=new Vector();
309 X10Events x10Event = new X10Events();
310 x10Event.setMonday(true);
311 x10Event.setDescription("Empty Event");
312 x10Event.setTime(new GregorianCalendar());
313 x10Event.setDeviceModule(new X10Module());
314 x10Event.setAction("on");
315 vEvents.addElement(x10Event);
316 return vEvents;
317 }
318
319 /***
320 * Gets a list of all the saved X10 modules.
321 * @return Vector of IX10Module
322 */
323 public Vector getAllX10Modules(){
324 Vector _returnVal = AutoHomeDaemonSession.getInstance().loadAllX10Devices();
325 if(_returnVal!=null){
326 return _returnVal;
327 }
328 _returnVal=new Vector();
329 _returnVal.addElement(new X10Module());
330 return _returnVal;
331 }
332
333 /***
334 * Gets the Event action and returns it as a selected
335 * drop down. ONly dealing with on and off actions.
336 * @param iEvent
337 * @return HTML drop down of action
338 */
339 public String getEventAction(IX10Events iEvent)
340 {
341 StringBuffer sbReturn = new StringBuffer();
342 if(iEvent.getAction().equalsIgnoreCase("on"))
343 {
344 sbReturn.append("<option value=\"on\" selected>ON");
345 sbReturn.append("<option value=\"off\">OFF");
346 }
347 else
348 {
349 sbReturn.append("<option value=\"on\">ON");
350 sbReturn.append("<option value=\"off\" selected>OFF");
351 }
352 return sbReturn.toString();
353 }
354
355 /***
356 * Returns an HTML dropdown selection of the selected module for this event.
357 * @param iEvent
358 * @return HTML selection of the x10 module
359 */
360 public String getX10ModuleDropDown(IX10Events iEvent)
361 {
362 StringBuffer sbReturnValue = new StringBuffer();
363 Vector x10modules = this.getAllX10Modules();
364 int count = x10modules.size();
365 IX10Module x10;
366 String selected = "selected";
367 IX10Module iEventModule = iEvent.getModule();
368 for(int i=0;i<count;i++){
369 x10 = (IX10Module)x10modules.elementAt(i);
370 if((iEventModule.getFullDeviceCode()).equalsIgnoreCase(x10.getFullDeviceCode()))
371 {
372 selected = "selected";
373 }
374 else
375 {
376 selected = "";
377 }
378 sbReturnValue.append("<option value="+x10.getFullDeviceCode()+" ");
379 sbReturnValue.append(selected+">"+x10.getFullDeviceCode());
380 }
381 return sbReturnValue.toString();
382 }
383
384 /***
385 * Allows display of time in html page. The time is
386 * a 12 hour format so it should return as HH:mm
387 * @param iEvent
388 * @return HH:mm time format
389 */
390 public String getX10EventTimeHTML(IX10Events iEvent)
391 {
392 StringBuffer sbReturn = new StringBuffer();
393 sbReturn.append(iEvent.getTime().get(Calendar.HOUR) + ":");
394 sbReturn.append(iEvent.getTime().get(Calendar.MINUTE));
395 return sbReturn.toString();
396 }
397
398 /***
399 * Gets the time and returns a drop down box
400 * with am or pm selected. This is for the modify
401 * existing event on a page.
402 * @param iEvent
403 * @return html selection options of AM and PM
404 */
405 public String getX10EventTimeAMPMhtml(IX10Events iEvent)
406 {
407 StringBuffer sbReturn = new StringBuffer();
408 if(iEvent.getTime().get(Calendar.AM_PM) == 0)
409 {
410 sbReturn.append("<option value=am selected>AM");
411 sbReturn.append("<option value=pm>PM");
412 }
413 else
414 {
415 sbReturn.append("<option value=am>AM");
416 sbReturn.append("<option value=pm selected>PM");
417 }
418 return sbReturn.toString();
419 }
420
421 /***
422 * Gets the first position in the DeviceID text field
423 * and returns a Upper Case Character value.
424 */
425 private char getHouseCode(String sDeviceID)
426 {
427 char cReturnValue='A';
428 try
429 {
430 cReturnValue = sDeviceID.toUpperCase().charAt(0);
431 }
432 catch(Exception err)
433 {
434 cReturnValue = 'A';
435 }
436 return cReturnValue;
437 }
438
439 /***
440 * Gets the last positions in the DeviceID text field
441 * and returns an integer value. If the entered integers
442 * are greater than 16 then 16 is returned;
443 */
444 private int getDeviceCode(String sDeviceID)
445 {
446 int returnValue = 0;
447 try
448 {
449 returnValue = Integer.parseInt(sDeviceID.substring(1));
450 }
451 catch(Exception err)
452 {
453 returnValue = 1;
454 }
455 if (returnValue >16)
456 {
457
458 returnValue = 16;
459 }
460 return returnValue;
461 }
462 }