View Javadoc

1   package org.wcb.autohome.implementations;
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:  Alice X10 Home Automation
20   */
21  import java.io.Serializable;
22  import java.util.Calendar;
23  
24  import org.wcb.autohome.interfaces.IMacroTrigger;
25  import org.wcb.autohome.interfaces.X10DeviceConstants;
26  import org.wcb.autohome.interfaces.IMacro;
27  import org.wcb.autohome.interfaces.IX10Module;
28  
29  /***
30   *Filename:    $Id: MacroTrigger.java,v 1.4 2004/02/28 00:21:49 wbogaardt Exp $
31   * 
32   *Abstract: This is the triggering event that will cause a macro and its associated 
33   *          events to execut.  A trigger can be thought of as an action sent or started
34   *          by an X10Module or a time period being met.  Macro triggers are capable of running
35   *          only on CM11A enabled interfaces. CM17A devices do not have any capability
36   *          to store information or macros. If you want to run timed events with the CM17A then
37   *          you are required to have a program running constantly such as a daemon. This
38   *          also means that your computer is always on.  
39   *
40   * $Log: MacroTrigger.java,v $
41   * Revision 1.4  2004/02/28 00:21:49  wbogaardt
42   * fixed formating to be compliant with sun coding convention
43   *
44   * Revision 1.3  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.2  2003/10/10 21:39:07  wbogaardt
48   * modified macro triggers to use calendar in stead of strings
49   *
50   *
51   *@author wbogaardt
52   *@version 1.1
53   *
54   */
55  public class MacroTrigger implements IMacroTrigger, Serializable, X10DeviceConstants {
56  
57      // public data
58      private int triggerType;   // Trigger type of TM or EV
59      private char   houseCode;     // unique id of the house code A-P
60      private int    deviceCode;    // status of the module within the house 1-16
61      private String description;   // brief description
62      private String fullDeviceCode;
63      private IMacro[] macros = new IMacro[2];
64      private String actionEvent;  //  the action to take place
65      private String initiator;   // the initiator whether a Module or a time
66      private Calendar cStartTime;   // the start time for timer triggers
67      private Calendar cStopTime;    // the stop time for timer triggers
68      private boolean sunday = false;
69      private boolean monday = false;
70      private boolean tuesday = false;
71      private boolean wednesday = false;
72      private boolean thursday = false;
73      private boolean friday = false;
74      private boolean saturday = false;
75      private IX10Module iX10Module;
76  
77      /***
78       *  Full constructor to build a macro trigger
79       * @param t Trigger type either event detected type or time based
80       * @param iModule X10 module to execute the trigger event on
81       * @param desc Description of the trigger
82       * @param act Type of action that the X10 module should take place
83       * @param start The start time of the trigger
84       * @param end  The end time of the trigger
85       * @param m List of macros associated with this trigger
86       */
87      public MacroTrigger(int t, IX10Module iModule, String desc, String act, Calendar start, Calendar end, IMacro[] m)
88      {
89          // initialize from args
90          triggerType   = t;
91          this.iX10Module = iModule;
92          description   = new String(desc);
93          actionEvent   = new String(act);
94          cStartTime = start;
95          cStopTime = end;
96          if (m != null)
97          {
98              for (int cnt = 0; cnt < m.length; cnt++)
99              {
100                 macros[cnt] = m[cnt];
101             }
102         }
103         else
104         {
105             macros[0] = new Macro();
106         }
107         /* Event type triggers require additional stuff */
108         if (triggerType == TRIGGER_EVENT)
109         {
110         }
111     }
112 
113     /***
114      * Default Constructor for macro triggers
115       */
116     public MacroTrigger()
117     {
118         this(TRIGGER_EVENT, new X10Module(), "NO DESCRIPTION", "On", null, null, null);
119     }
120 
121     /* clone constructor */
122     /***
123      * This will clone and create an object from the macro trigger interface
124      * @param newO The interface to clone as an object.
125      */
126     public MacroTrigger(IMacroTrigger newO)
127     {
128         this(newO.getTriggerType(), newO.getX10Module(), newO.getDescription(), newO.getAction(),
129                 newO.getStartTime(), newO.getStopTime(),
130                 newO.getMacros());
131     }
132 
133     /***
134      * Used to do debug printing
135      */
136     public void print()
137     {
138         System.out.println(initiator + "\t" + macros[0].getMacroName() + "\t" + triggerType);
139     }
140 
141     /* modifiers for the interface */
142     /***
143      *Trigger type is either a time event or X10 module 
144      *excuting some action.
145      *
146      *@param n - See X10DeviceConstants for TRIGGER_EVENT or TIMER_EVENT
147      */
148     public void setTriggerType(int n)
149     {
150         triggerType = n;
151     }
152 
153     /***
154      *House code for action or trigger based
155      *event X10 module.
156      *@param a - House code of X10 device.
157      */
158     public void setHouseCode(char a)
159     {
160         houseCode = a;
161     }
162 
163     /***
164      *Device or zone code of x10 device
165      *being executed.
166      *@param i - Device/zone code.
167      */
168     public void setDeviceCode(int i)
169     {
170         deviceCode = i;
171     }
172 
173     /***
174      * Sets the X10 module that will execute based on the trigger.
175      * @param imod X10Module to run when trigger requirements are met.
176      */
177     public void setX10Module(IX10Module imod) {
178         this.iX10Module = imod;
179     }
180     /***
181      *Based on trigger type this is the initiators
182      *information. The initiator is another x10 module that
183      * may have fired an action.
184      *
185      *@param initiat - Initiator information this is a an X10 module code such as A1
186      */
187     public void setInitiator(String initiat)
188     {
189             Character hs = new Character(initiat.charAt(0));
190             int dc = 0;
191             try
192             {
193                 dc = Integer.parseInt(initiat.substring(1));
194             }
195             catch (NumberFormatException err)
196             {
197                 dc = 1;
198             }
199             fullDeviceCode = new String(hs.toString() + Integer.toString(dc));
200             setHouseCode(hs.charValue());
201             setDeviceCode(dc);
202             initiator = fullDeviceCode;
203     }
204 
205     /***
206      * Set the action of the trigger.
207      * @param act Action type for the trigger ON_ACTION OR OFF_ACTION
208      */
209     public void setAction(String act)
210     {
211         actionEvent = new String(act);
212     }
213 
214     /***
215      *User defined description of the Trigger macro
216      *
217      *@param desc - description for user
218      */
219     public void setDescription(String desc)
220     {
221         description = new String(desc);
222     }
223 
224     /***
225      *Allows seting of the Macro by
226      *name and not by type which should be IMacro
227      * @param mac An array of IMacro objects
228      */
229     public void setMacros(IMacro[] mac)
230     {
231         macros = mac;
232     }
233 
234     /***
235      *This is the the time in 24 hour format
236      *this is in hh:mm format.
237      * @param startTime For time based triggers the calendar time of starting
238      */
239     public void setStartTime(Calendar startTime)
240     {
241         this.cStartTime = startTime;
242     }
243 
244     /***
245      *This is the the time in 24 hour format
246      *this is in hh:mm format.
247      * @param stp for time based triggers the calendar time of stoping the trigger
248      */
249     public void setStopTime(Calendar stp)
250     {
251         this.cStopTime = stp;
252     }
253 
254     /* accessors for the interface */
255     /***
256      *The types of triggers that can
257      *fire off a macro.  There are two types of triggers
258      *Trigger/event based and timmer based. Event based triggers are if
259      *an X10 module is activated,either turned on or off, and its
260      *detected by the CM11A interface then the macro is executed.
261      *Timer based triggers wait for a particular time or day of week
262      *to pass which will be the excecutor.
263      *
264      *@return int - 10 = Timer event, 11 = Trigger event
265      */
266     public int getTriggerType()
267     {
268         return triggerType;
269     }
270 
271     /***
272      *the house code of is used in instances for module activated events.  
273      *
274      *@return char - House code ranges from A - P
275      */ 
276     public char getHouseCode()
277     {
278         return houseCode;
279     }
280 
281     /***
282      *the zone code of is used in instances for module activated events.  
283      *
284      *@return int - zone code ranges 1-16;
285      */ 
286     public int  getDeviceCode()
287     {
288         return deviceCode;
289     }
290 
291     /***
292      * The X10 Module associated witht he macro trigger.
293      * @return The interface to an X10Module object
294      */
295     public IX10Module getX10Module() {
296         if (iX10Module == null)
297         {
298             iX10Module = new X10Module();
299         }
300         return iX10Module;
301     }
302 
303     /***
304      * Get the initator type for the trigger this can be either a detected event
305      * as identified by TRIGGER_EVENT constants or a time of day to execute the
306      * trigger which is startTime/stopTime
307      * @return The type of initiator to run
308      */
309     public String getInitiator()
310     {
311         if (triggerType == TRIGGER_EVENT)
312         {
313             return initiator;
314         }
315         else
316         {
317             return getStartTime() + "/" + getStopTime();
318         }
319     }
320 
321     /***
322      * The action that this trigger will execute on this is either
323      * the ON_ACTION or OFF_ACTION commands
324      * @return ON_ACTION or OFF_ACTION string
325      */
326     public String getAction()
327     {
328         return actionEvent;
329     }
330 
331     /***
332      * For time based triggers enable execution on Sunday
333      * @param s True enables execute trigger on specified weekday.
334      */
335     public void setSunday(boolean s)
336     {
337         sunday = s;
338     }
339 
340      /***
341      * For time based triggers enable execution on Monday
342      * @param m True enables execute trigger on specified weekday.
343      */
344     public void setMonday(boolean m)
345     {
346         monday = m;
347     }
348 
349      /***
350      * For time based triggers enable execution on Tuesday
351      * @param t True enables execute trigger on specified weekday.
352      */
353     public void setTuesday(boolean t)
354     {
355         tuesday = t;
356     }
357 
358      /***
359      * For time based triggers enable execution on Wednesday
360      * @param w True enables execute trigger on specified weekday.
361      */
362     public void setWednesday(boolean w)
363     {
364         wednesday = w;
365     }
366 
367      /***
368      * For time based triggers enable execution on Thursday
369      * @param t True enables execute trigger on specified weekday.
370      */
371     public void setThursday(boolean t)
372     {
373         thursday = t;
374     }
375 
376      /***
377      * For time based triggers enable execution on Friday
378      * @param f True enables execute trigger on specified weekday.
379      */
380     public void setFriday(boolean f)
381     {
382         friday = f;
383     }
384 
385      /***
386      * For time based triggers enable execution on Saturday
387      * @param s True enables execute trigger on specified weekday.
388      */
389     public void setSaturday(boolean s)
390     {
391         saturday = s;
392     }
393 
394     /***
395      * Execute trigger on a Sunday
396      * @return true indicates execute event for the day of week
397      */
398     public boolean getSunday()
399     {
400         return sunday;
401     }
402 
403     /***
404      * Execute trigger on a monday
405      * @return true indicates execute event for the day of week
406      */
407     public boolean getMonday()
408     {
409         return monday;
410     }
411 
412     /***
413      * Execute trigger on a Tuesday
414      * @return true indicates execute event for the day of week
415      */
416     public boolean getTuesday()
417     {
418         return tuesday;
419     }
420 
421     /***
422      * Execute trigger on a Wednesday
423      * @return true indicates execute event for the day of week
424      */
425     public boolean getWednesday()
426     {
427         return wednesday;
428     }
429 
430     /***
431      * Execute trigger on a Thursday
432      * @return true indicates execute event for the day of week
433      */
434     public boolean getThursday()
435     {
436         return thursday;
437     }
438 
439     /***
440      * Execute trigger on a Friday
441      * @return true indicates execute event for the day of week
442      */
443     public boolean getFriday()
444     {
445         return friday;
446     }
447 
448     /***
449      * Execute trigger on a Saturday
450      * @return true indicates execute event for the day of week
451      */
452     public boolean getSaturday()
453     {
454         return saturday;
455     }
456 
457     /***
458      * Get the description of the macro trigger
459      * @return Defaults to NO DESCRIPTION if it has not been set.
460      */
461     public String getDescription()
462     {
463         if (description.startsWith("") && description.length() < 1)
464         {
465             return "NO DESCRIPTION";
466         }
467         return description;
468     }
469 
470     /***
471      *This method returns an array of IMacros
472      *which are stored as the first element in 
473      *the array as the start macro and the 
474      *second element is the stop macro 
475      *Start and stop macros are also associated
476      *mainly with timer based triggers and not
477      *trigger or action related.
478      *
479      *@return IMacro[] - array with start and stop macro elements
480      */
481     public IMacro[] getMacros()
482     {
483         return macros;
484     }
485 
486     /***
487      *Returns simple string with 24 hour
488      *formated start time of this trigger;
489      *
490      *@return String - Simple time in hh:mm format
491      */
492     public Calendar getStartTime()
493     {
494         return cStartTime;
495     }
496 
497     /***
498      *Returns simple string with 24 hour
499      *formated start time of this trigger;
500      *
501      *@return String - Simple time in hh:mm format
502      */
503     public Calendar getStopTime()
504     {
505         return cStopTime;
506     }
507 
508     /***
509      *Returns the full X10 module identifier which is in
510      *housecode followed by zone code.
511      *
512      *@return String - X10 module identifier in A1 format
513      */
514     public String getFullDeviceCode()
515     {
516         return fullDeviceCode;
517     }
518 
519 }
520 
521