View Javadoc

1   package org.wcb.autohome.util;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   *  Project:   Home Automation Interface
20   *  Filename:  $Id: AliceX10AddressListener.java,v 1.9 2004/07/22 03:17:55 wbogaardt Exp $
21   *  Abstract:  Centralized backend for various components to send events to
22   *             Serial port output.
23   */
24  
25  import com.jpeterson.x10.event.AddressListener;
26  import com.jpeterson.x10.event.AddressEvent;
27  import com.jpeterson.x10.event.FunctionListener;
28  import com.jpeterson.x10.event.FunctionEvent;
29  
30  import org.wcb.autohome.AutoHomeAdminSession;
31  import org.wcb.autohome.exceptions.HomeException;
32  import org.wcb.autohome.implementations.RunEvent;
33  import org.wcb.autohome.interfaces.IX10MonitorEvent;
34  import org.wcb.autohome.interfaces.IRunEvent;
35  import org.wcb.autohome.model.MonitorTableModel;
36  
37  import java.io.IOException;
38  
39  /***
40   * AliceX10AddressListener.java
41   *
42   * Created on December 12, 2003, 7:59 PM
43   *
44   * @author  wbogaardt
45   *
46   *  Project:   Home Automation Interface
47   *  Filename:  $Id: AliceX10AddressListener.java,v 1.9 2004/07/22 03:17:55 wbogaardt Exp $
48   *  Abstract:  Listens and monitors CM11A device for events occuring over power line and
49   *             logs the results.
50   *
51   *  $Log: AliceX10AddressListener.java,v $
52   *  Revision 1.9  2004/07/22 03:17:55  wbogaardt
53   *  removed deprecated methods
54   *
55   *  Revision 1.8  2004/02/02 23:41:31  wbogaardt
56   *  refactored email so that properties are maintained in bean object and added allowing username and password authentication for email servers that require it.
57   *
58   *  Revision 1.7  2004/01/21 05:29:43  wbogaardt
59   *  fixed bug saving file format and added disable monitoring
60   *
61   *  Revision 1.6  2004/01/20 19:36:00  wbogaardt
62   *  fixed adding new objects to check for redundancy
63   *
64   *  Revision 1.5  2004/01/20 05:31:05  wbogaardt
65   *  added speech event for runing
66   *
67   *  Revision 1.4  2004/01/20 00:20:58  wbogaardt
68   *  allow email service to send status code back if email succeds or fails
69   *
70   *  Revision 1.3  2004/01/19 22:35:38  wbogaardt
71   *  added fixes to runing events and emails so they work and added a testing of a monitored event through the table popup on a right mouse click.
72   *
73   *  Revision 1.2  2004/01/17 08:15:46  wbogaardt
74   *  Have an initial working monitoring frame that actually shows the date and time an event takes place
75   *
76   *  Revision 1.1  2003/12/13 04:30:59  wbogaardt
77   *  new CM11A monitoring listeners.
78   *
79   */
80  public class AliceX10AddressListener implements AddressListener, FunctionListener {
81  
82      private MonitorTableModel mModel;
83      private EmailService eMailService;
84      private IX10MonitorEvent iMainEvent;
85      private static int deviceID = 0;
86  
87      /*** Creates a new instance of AliceX10AddressListener */
88      public AliceX10AddressListener(MonitorTableModel mModel) {
89          this.mModel = mModel;
90          eMailService = new EmailService(AutoHomeAdminSession.getInstance().getEmailInformation());
91      }
92  
93      public void address(AddressEvent addressEvent) {
94          deviceID = addressEvent.getDeviceCode();
95        //iMainEvent = this.mModel.getModuleOfEventDetected(addressEvent.getHouseCode(), this.deviceID, addressEvent.toString());
96        //if(iMainEvent != null)
97       //{
98         //     this.runAction(iMainEvent);
99         //}
100     }
101 
102     /***
103      * Run the event from the detected item.
104      * @param event The main event that the action was detected.
105      */
106     public void runAction(IX10MonitorEvent event){
107         IRunEvent[] runEvents = event.getRunEvent();
108         switch(runEvents[0].getRunType())
109         {
110             case RunEvent.RUN_APPLICATION:
111                 this.runApplication(event);
112                 break;
113             case RunEvent.RUN_MODULE:
114                 this.runModuleAction(event);
115                 break;
116             case RunEvent.SEND_EMAIL:
117                 this.sendEmailNotification(event);
118                 break;
119             case RunEvent.SPEAK_COMMAND:
120                 this.speakSentence(event);
121                 break;
122         }
123     }
124 
125     /***
126      * This runs the user specified application if the module detects some event.
127      *
128      * @param iMonitor The monitoring module
129      */
130     private void runApplication(IX10MonitorEvent iMonitor){
131         IRunEvent[] runEvents = iMonitor.getRunEvent();
132         String[] cmd = new String[1];
133         //there were arguments so add them to the array
134         if(!isAllArgumentsNull(runEvents[0].getArguments())){
135             cmd = new String[1 + runEvents[0].getArguments().length];
136             for(int iCount = 0; iCount < runEvents[0].getArguments().length; iCount++)
137             {
138                 if(runEvents[0].getArguments()[iCount] != null){
139                     cmd[1 + iCount] = runEvents[0].getArguments()[iCount];
140                 }
141                 else
142                 {
143                     cmd[1 + iCount] = ""; //hava a blank argument at least to stop break of parameters
144                 }
145             }
146         }
147         cmd[0] = runEvents[0].getCommand();
148 
149         /* run the user defined command and parameters */
150         if (cmd[0] != null)
151         {
152             try
153             {
154                 Runtime r = Runtime.getRuntime();
155                 r.exec(cmd);
156             }
157             catch (IOException ioe)
158             {
159                 System.err.println("Unable to run command or application " + ioe);
160             }
161         }
162     }
163 
164     /***
165      * Tests to see if all the arguments are null if they are then
166      * there were no arguments to pass to the runtime.
167      * @param args the argumentts array to check
168      * @return true has no arguments false has arguments
169      */
170     private boolean isAllArgumentsNull(String[] args){
171         for(int iCount = 0; iCount < args.length; iCount++)
172         {
173             if(args[iCount] != null){
174                 return false;
175             }
176         }
177         return true;
178     }
179 
180     /***
181      * This sends of an email message to the user of the monitor that noticed an action.
182      * @param iMonitor The monitoring X10 device.
183      */
184     private void sendEmailNotification(IX10MonitorEvent iMonitor){
185         this.eMailService.sendEventMessage(iMonitor);
186     }
187 
188     /***
189      * This Runs the user specified action on an x10 device, which the monitoring x10 device has
190      * been assigned to.
191      * @param iMonitor The monitoring X10 device.
192      */
193     private void runModuleAction(IX10MonitorEvent iMonitor){
194         IRunEvent[] runEvents = iMonitor.getRunEvent();
195         try{
196             System.out.println(runEvents[0].getX10Module().getFullDeviceCode() + " running " + runEvents[0].getModuleCommand());
197             AutoHomeAdminSession.getInstance().sendCommandToX10Module(runEvents[0].getX10Module(), runEvents[0].getModuleCommand());
198         }catch(HomeException he)
199         {
200             System.err.println("Home exception " + he);
201         }
202     }
203 
204     private void speakSentence(IX10MonitorEvent iMonitor){
205         IRunEvent[] runEvents = iMonitor.getRunEvent();
206         AutoHomeAdminSession.getInstance().printMessage(runEvents[0].getSentence());
207     }
208 
209     /***
210      * This function detects all lights off function on the House.  So this listener must first get the
211      * device code information from the AddressListener, which should fire first. Then this should fire
212      * as the next event to get the status type. The address listener gives the house and device id.
213      * @param functionEvent The function or action that was fired on a house code.
214      */
215     public void functionAllLightsOff(FunctionEvent functionEvent) {
216          iMainEvent = this.mModel.getModuleOfEventDetected(functionEvent.getHouseCode(), deviceID, "All lights off");
217         if(iMainEvent != null)
218         {
219             this.runAction(iMainEvent);
220         }
221     }
222 
223     /***
224      * This function detects all lights on function on the House.  So this listener must first get the
225      * device code information from the AddressListener, which should fire first. Then this should fire
226      * as the next event to get the status type. The address listener gives the house and device id.
227      * @param functionEvent The function or action that was fired on a house code.
228      */
229     public void functionAllLightsOn(FunctionEvent functionEvent) {
230          iMainEvent = this.mModel.getModuleOfEventDetected(functionEvent.getHouseCode(), deviceID, "All lights on");
231         if(iMainEvent != null)
232         {
233             this.runAction(iMainEvent);
234         }
235     }
236 
237     /***
238      * This function detects all units off function on the House.  So this listener must first get the
239      * device code information from the AddressListener, which should fire first. Then this should fire
240      * as the next event to get the status type. The address listener gives the house and device id.
241      * @param functionEvent The function or action that was fired on a house code.
242      */
243     public void functionAllUnitsOff(FunctionEvent functionEvent) {
244          iMainEvent = this.mModel.getModuleOfEventDetected(functionEvent.getHouseCode(), deviceID, "All Units off");
245         if(iMainEvent != null)
246         {
247             this.runAction(iMainEvent);
248         }
249     }
250     /***
251      * This function detects a Brighten function on the House.  So this listener must first get the
252      * device code information from the AddressListener, which should fire first. Then this should fire
253      * as the next event to get the status type. The address listener gives the house and device id.
254      * @param functionEvent The function or action that was fired on a house code.
255      */
256     public void functionBright(FunctionEvent functionEvent) {
257          iMainEvent = this.mModel.getModuleOfEventDetected(functionEvent.getHouseCode(), deviceID, "Brighten");
258         if(iMainEvent != null)
259         {
260             this.runAction(iMainEvent);
261         }
262     }
263     /***
264      * This function detects a dim function on the House.  So this listener must first get the
265      * device code information from the AddressListener, which should fire first. Then this should fire
266      * as the next event to get the status type. The address listener gives the house and device id.
267      * @param functionEvent The function or action that was fired on a house code.
268      */
269     public void functionDim(FunctionEvent functionEvent) {
270          iMainEvent = this.mModel.getModuleOfEventDetected(functionEvent.getHouseCode(), deviceID, "Dim light");
271         if(iMainEvent != null)
272         {
273             this.runAction(iMainEvent);
274         }
275     }
276 
277     public void functionHailAcknowledge(FunctionEvent functionEvent) {
278     }
279 
280     public void functionHailRequest(FunctionEvent functionEvent) {
281     }
282 
283     /***
284      * This function detects an off function on the House.  So this listener must first get the
285      * device code information from the AddressListener, which should fire first. Then this should fire
286      * as the next event to get the status type. The address listener gives the house and device id.
287      * @param functionEvent The function or action that was fired on a house code.
288      */
289     public void functionOff(FunctionEvent functionEvent) {
290         iMainEvent = this.mModel.getModuleOfEventDetected(functionEvent.getHouseCode(), deviceID, "Off");
291         if(iMainEvent != null)
292         {
293             this.runAction(iMainEvent);
294         }
295     }
296 
297     /***
298      * This function detects an on function on the House.  So this listener must first get the
299      * device code information from the AddressListener, which should fire first. Then this should fire
300      * as the next event to get the status type. The address listener gives the house and device id.
301      * @param functionEvent The function or action that was fired on a house code.
302      */
303     public void functionOn(FunctionEvent functionEvent) {
304        iMainEvent = this.mModel.getModuleOfEventDetected(functionEvent.getHouseCode(), deviceID, "On");
305         if(iMainEvent != null)
306         {
307             this.runAction(iMainEvent);
308         }
309     }
310 
311     public void functionPresetDim1(FunctionEvent functionEvent) {
312     }
313 
314     public void functionPresetDim2(FunctionEvent functionEvent) {
315     }
316 
317     public void functionStatusOff(FunctionEvent functionEvent) {
318         AutoHomeAdminSession.getInstance().printMessage("Detected status off module at " + functionEvent.getHouseCode());
319     }
320 
321     public void functionStatusOn(FunctionEvent functionEvent) {
322         AutoHomeAdminSession.getInstance().printMessage("Detected status on module at " + functionEvent.getHouseCode());
323     }
324 
325     public void functionStatusRequest(FunctionEvent functionEvent) {
326     }
327 
328 }