View Javadoc

1   package org.wcb.autohome.factories;
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   */
20  import java.util.*;
21  import java.io.*;
22  
23  import org.wcb.autohome.interfaces.IDeviceRemote;
24  import org.wcb.autohome.interfaces.IMacro;
25  import org.wcb.autohome.interfaces.X10DeviceConstants;
26  import org.wcb.autohome.implementations.X10Events;
27  import org.wcb.autohome.implementations.X10Module;
28  import org.wcb.autohome.implementations.X10MonitorEvent;
29  
30  /***
31   *  Project:   Alice X10 Home Automation
32   *  Filename:  $Id: DeviceFactory.java,v 1.17 2004/02/25 21:38:36 wbogaardt Exp $
33   *  Abstract:  Centralized backend for various components to send events to
34   *             Serial port output. This mostly handles reading from a file and writting
35   *             to a file the various user entries such as devices and macros for latter
36   *             retrieval.
37   *
38   * $Log: DeviceFactory.java,v $
39   * Revision 1.17  2004/02/25 21:38:36  wbogaardt
40   * added javadocs and fixed formating for checkstyle report
41   *
42   * Revision 1.16  2004/02/03 21:02:27  wbogaardt
43   * moved DeviceFactory away from rmi creation and simplified interface between gateway
44   *
45   * Revision 1.15  2004/01/17 08:15:46  wbogaardt
46   * Have an initial working monitoring frame that actually shows the date and time an event takes place
47   *
48   * Revision 1.14  2004/01/17 07:21:17  wbogaardt
49   * added serialization to run events and allow monitoring of these events to the file system to reload later
50   *
51   * Revision 1.13  2004/01/16 00:53:43  wbogaardt
52   * Fixed a very obscure bug with the Macro Panel that it didn't added new
53   * x10 devices to the drop down of available x10 device for the macro. Modified Macro triggers to change the events
54   * to integer verses strings cleaner this way.
55   *
56   * Revision 1.12  2003/12/24 20:24:20  wbogaardt
57   * Fixed display of console message on start up
58   *
59   * Revision 1.11  2003/12/20 21:32:39  wbogaardt
60   * enabled new file menu option and added functionality
61   *
62   * Revision 1.10  2003/12/16 22:08:37  wbogaardt
63   * refactored events daemon handeling
64   *
65   * Revision 1.9  2003/12/12 23:17:36  wbogaardt
66   * javadoc comments refactored methods so they are more descriptive
67   *
68   * Revision 1.8  2003/12/09 23:11:42  wbogaardt
69   *
70   * C: ----------------------------------------------------------------------
71   *
72   * Revision 1.7  2003/12/08 21:57:24  wbogaardt
73   * refactored out properties loading for old x10 format
74   *
75   * Revision 1.6  2003/12/08 21:35:18  wbogaardt
76   * refactored out old methods of saving macros and modules
77   *
78   * Revision 1.5  2003/12/08 21:09:04  wbogaardt
79   * refactored old method signatures improved error handeling on windows
80   *
81   * Revision 1.4  2003/12/06 00:41:53  wbogaardt
82   * deprecated some method calls
83   *
84   * Revision 1.3  2003/10/10 21:39:07  wbogaardt
85   * modified macro triggers to use calendar in stead of strings
86   *
87   * Revision 1.2  2003/10/10 18:39:12  wbogaardt
88   * changed date time information from a string to a calendar object
89   *
90   *
91   * @author wbogaardt
92   * @version 1.0
93   */
94  public class DeviceFactory implements IDeviceRemote, X10DeviceConstants {
95  
96      private static Vector[] FILE_SAVED_HASH = new Vector[5];
97  
98      /***
99       * Default Consturctor
100      */
101     public DeviceFactory() {
102         super();
103     }
104 
105     /***
106      * This new method takes in an Array of Vectors and assigns them
107      * to the appropriate Vector Array so that other objects can access these
108      * Vectors.
109      * @param sfilename an array of Vectors which have serilized objects.
110      */
111     public void loadFile(String sfilename)
112     {
113         /*Serialize in the properties file of the saved device file */
114         FILE_SAVED_HASH = new Vector[5];
115         try
116         {
117             FileInputStream fin = new FileInputStream(sfilename);
118             ObjectInputStream inStream = new ObjectInputStream(fin);
119             System.out.println("Reading in: " + (String) inStream.readObject());
120             FILE_SAVED_HASH[0] = (Vector) inStream.readObject(); //X10MODULES
121             FILE_SAVED_HASH[1] = (Vector) inStream.readObject(); //EVENTS
122             FILE_SAVED_HASH[2] = (Vector) inStream.readObject(); //TRIGGERS
123             FILE_SAVED_HASH[3] = (Vector) inStream.readObject(); //MACROS
124             FILE_SAVED_HASH[4] = (Vector) inStream.readObject(); //MONITORS
125         }
126         catch (IOException ioe)
127         {
128             Vector vX10modules = new Vector();
129             vX10modules.add(new X10Module());
130             FILE_SAVED_HASH[0] = vX10modules; //X10MODULES
131             Vector vEvents = new Vector();
132             vEvents.add(new X10Events());
133             FILE_SAVED_HASH[1] = vEvents; //EVENTS
134             FILE_SAVED_HASH[2] = new Vector(); //TRIGGERS
135             FILE_SAVED_HASH[3] = new Vector(); //MACROS
136             Vector vMonitors = new Vector();
137             vMonitors.add(new X10MonitorEvent());
138             FILE_SAVED_HASH[4] = vMonitors;  //Monitors
139         }
140         catch (ClassNotFoundException cnfe)
141         {
142             Vector vX10modules = new Vector();
143             vX10modules.add(new X10Module());
144             FILE_SAVED_HASH[0] = vX10modules; //X10MODULES
145             Vector vEvents = new Vector();
146             vEvents.add(new X10Events());
147             FILE_SAVED_HASH[1] = vEvents; //EVENTS
148             FILE_SAVED_HASH[2] = new Vector(); //TRIGGERS
149             FILE_SAVED_HASH[3] = new Vector(); //MACROS
150             Vector vMonitors = new Vector();
151             vMonitors.add(new X10MonitorEvent());
152             FILE_SAVED_HASH[4] = vMonitors; //Monitors
153         }
154     }
155 
156 
157     /***
158      * Saves all of the module information to the user selected
159      * save to file this method is usually called from a
160      * save or save as command. This also writes the information
161      * to the diskdrive.
162      * @param sfileName Full path and file name
163      * @return true if file saved succesfully
164      */
165     public boolean saveFile(String sfileName) {
166         try
167         {
168             FileOutputStream output = new FileOutputStream(sfileName);
169             ObjectOutputStream outStream = new ObjectOutputStream(output);
170             outStream.writeObject("FILENAME: " + sfileName);
171             outStream.writeObject(this.readInX10Devices()); //write the X10 modules vector
172             outStream.writeObject(this.readInX10Events()); //write the Events vector
173             outStream.writeObject(this.loadMacroTriggers()); //write the Triggers vector
174             outStream.writeObject(this.readInX10Macros()); //write the Macros vector
175             outStream.writeObject(this.readInAliceMonitors()); //write the Alice monitors
176             outStream.flush();
177             outStream.close();
178         }
179         catch (IOException err)
180         {
181             System.err.println("Unable to write file :" + err);
182             return false;
183         }
184         return true;
185     }
186 
187     /***
188      * Create a new file such as when the user select
189      * File->new
190      */
191     public void createNewFile() {
192         Vector vModules = new Vector();
193         vModules.add(new X10Module());
194         FILE_SAVED_HASH[0] = vModules; //X10MODULES
195         Vector vEvents = new Vector();
196         vEvents.add(new X10Events());
197         FILE_SAVED_HASH[1] = vEvents; //EVENTS
198         FILE_SAVED_HASH[2] = new Vector(); //TRIGGERS
199         FILE_SAVED_HASH[3] = new Vector(); //MACROS
200         Vector vMonitors = new Vector();
201         vMonitors.add(new X10MonitorEvent());
202         FILE_SAVED_HASH[4] = vMonitors; //Monitors
203 
204     }
205 
206     /***
207      * Returns a list of X10Devices saved in the file.
208      * @return Vector of IX10Module
209      */
210     public Vector readInX10Devices()
211     {
212         return FILE_SAVED_HASH[0];
213     }
214 
215     /***
216      * Save the vector of IX10Module into a properties
217      * Hash table key for quick access and storage;
218      * @param items Vector of IX10Module which are serialized.
219       */
220     public void writeX10Devices(Vector items)
221     {
222         FILE_SAVED_HASH[0] = items;
223     }
224 
225     /***
226      * Returns a vector of IX10Events which are specific to the
227      * Alice environment.
228      * @return Vector of IX10Events
229      */
230     public Vector readInX10Events()
231     {
232         if (FILE_SAVED_HASH[1] != null)
233         {
234             return FILE_SAVED_HASH[1];
235         }
236         Vector vEvents = new Vector();
237         vEvents.add(new X10Events());
238         return vEvents;
239     }
240 
241     /***
242      * This is used to store the alice monitoring events.
243      * @param vList list of events
244      */
245     public void writeAliceMonitors(Vector vList)
246     {
247         FILE_SAVED_HASH[4] = vList;
248     }
249 
250     /***
251      * Save the vector list of IX10Events into a
252      * Hash table key for quick access and storage.
253      * @param items Vector of IX10Events, which are serialized.
254      */
255     public void writeX10Events(Vector items)
256     {
257         FILE_SAVED_HASH[1] = items;
258     }
259 
260     /***
261      * Reads from the hash the serialized objects as
262      * a Vector of IX10Macro.
263      * @return  Vector of IX10Macr serialized objects default value is an empyt Vector.
264      */
265     public Vector readInX10Macros()
266     {
267         if (FILE_SAVED_HASH[3] != null)
268         {
269             return FILE_SAVED_HASH[3];
270         }
271         return new Vector();
272     }
273 
274     /***
275      * Loads the alice monitors and returns them as a vector to
276      * the application
277      * @return  Vector of IX10MonitorEvents, which also have IRunEvents arrays
278      */
279     public Vector readInAliceMonitors()
280     {
281         if (FILE_SAVED_HASH[4] != null)
282         {
283             return FILE_SAVED_HASH[4];
284         }
285         Vector vMonitors = new Vector();
286         vMonitors.add(new X10MonitorEvent());
287         return vMonitors;
288     }
289 
290     /***
291      * Writes the IMacro vector to the array for serialization storage.
292      * @param vec Vector of IMacros to be serialized
293      */
294     public void writeMacros(Vector vec)
295     {
296         FILE_SAVED_HASH[3] = vec;
297     }
298 
299     /***
300      * Load the Macro item triggers.  Thes are events that
301      * are handled by the CM11A X10 interface. These triggers
302      * are light A1 being turned on or appliance B2 turned off.
303      * Trigger are stored as
304      * Trigger.1=
305      * @return vector of IMacroTrigger objects.
306      */
307     public Vector loadMacroTriggers()
308     {
309         return FILE_SAVED_HASH[2];
310     }
311 
312     /***
313      * Save the Macro item triggers.  Thes are events that
314      * are handled by the CM11A X10 interface. These triggers
315      * are light A1 being turned on or appliance B2 turned off.
316      * @param items vector of IMacroTrigger objects to be serialized.
317      */
318     public void saveMacroTriggers(Vector items)
319     {
320         FILE_SAVED_HASH[2] = items;
321     }
322 
323     /***
324      *Moved saving macro information in the hash table
325      *@param macro saves a serialized object into the hash.
326      */
327     public void saveMacro(IMacro macro)
328     {
329         int count = FILE_SAVED_HASH[3].size();
330         boolean saved = false;
331         IMacro currMacro;
332         for (int i = 0; i < count; i++)
333         {
334             currMacro = (IMacro) FILE_SAVED_HASH[3].elementAt(i);
335             if (macro.getMacroName().equalsIgnoreCase(currMacro.getMacroName()))
336             {
337                 FILE_SAVED_HASH[3].setElementAt(macro, i);
338                 saved = true;
339             }
340         }
341         if (!saved)
342         {
343             FILE_SAVED_HASH[3].addElement(macro);
344         }
345     }
346 
347     /***
348      *Moved saving macro information in the hash table
349      *@param macro allows removal of IMacro from hash.
350      */
351     public void deleteMacro(IMacro macro)
352     {
353         int count = FILE_SAVED_HASH[3].size();
354         IMacro currMacro;
355         for (int i = 0; i < count; i++)
356         {
357             currMacro = (IMacro) FILE_SAVED_HASH[3].elementAt(i);
358             if (macro.getMacroName().equalsIgnoreCase(currMacro.getMacroName()))
359             {
360                 FILE_SAVED_HASH[3].removeElementAt(i);
361             }
362         }
363     }
364 }