001/*
002 *  $Rev: 419 $:     Revision of last commit
003 *  $Author: tgutwin $:  Author of last commit
004 *  $Date: 2014-06-03 12:13:29 -0700 (Tue, 03 Jun 2014) $:    Date of last commit
005 *  $URL: $
006 *
007 *  Written by Tom Gutwin
008 *  Copyright (C) 2014 Tom B. Gutwin, North Vancouver BC Canada
009 *
010 *  This program is free software; you can redistribute it and/or modify
011 *  it under the terms of the GNU General Public License as published by
012 *  the Free Software Foundation; either version 2 of the License, or
013 *  (at your option) any later version.
014 *
015 *  This program is distributed in the hope that it will be useful,
016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018 *  GNU General Public License for more details.
019 *
020 *  You should have received a copy of the GNU General Public License
021 *  along with this program; if not, write to the Free Software
022 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
023 */
024
025package ca.bc.webarts.tools.rainforest;
026
027
028import java.util.Arrays;
029import java.util.Vector;
030import java.util.Calendar;
031
032import ca.bc.webarts.tools.Log;
033import ca.bc.webarts.widgets.Util;
034
035import java.net.InetAddress;
036import java.net.MalformedURLException;
037import java.net.UnknownHostException;
038import java.net.URL;
039import java.net.URLConnection;
040import java.io.FileWriter;
041import java.io.IOException;
042
043import javax.servlet.http.HttpServletRequest;
044import javax.servlet.http.HttpServletResponse;
045
046import nu.xom.Attribute;
047import nu.xom.Builder;
048import nu.xom.Document;
049import nu.xom.Element;
050import nu.xom.Elements;
051import nu.xom.Node;
052import nu.xom.ParsingException;
053import nu.xom.ValidityException;
054import nu.xom.Serializer;
055
056
057/**
058  * An impl of the Rainforest Automation Eagle Device Uploader Messaging interface.
059  *
060  * This class implements a message handler to parse the XML Eagle UploaderAPI messages and expose the contained data values.
061  * It does not listen for and/or recieve the messages. You must call this class from a servlet or other class
062  * that will be receiving the http post request.
063  * <br />
064  * This class is written and copyright by Tom B. Gutwin, on his own personal time. <br />
065 *  This program is free software; you can redistribute it and/or modify
066 *  it under the terms of the GNU General Public License as published by
067 *  the Free Software Foundation; either version 2 of the License, or
068 *  (at your option) any later version.<br />
069 *
070 *  This program is distributed in the hope that it will be useful,
071 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
072 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
073 *  <a href="http://www.gnu.org/licenses/gpl.html">GNU General Public License</a> for more details.<br />
074 *
075 *  You should have received a copy of the GNU General Public License
076 *  along with this program; if not, write to the Free Software
077 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.<br />
078  * <br />
079  * The Eagle Device is a product of <a href="http://www.rainforestautomation.com">Rainforest Automation</a>.
080  **/
081public class EagleUploader
082{
083  protected static final String className_ = "EagleUploader";
084  protected static final String version_ = Util.spacesToCapsInString("0.0.1_[$Rev: $]");
085  protected static final String uploaderVersion_ = "v6-July2014";
086
087  /**  A holder for this clients System File Separator.  */
088  public final static String SYSTEM_FILE_SEPERATOR = java.io.File.separator;
089
090  /**  A holder for this clients System line termination separator.  */
091  public final static String SYSTEM_LINE_SEPERATOR =System.getProperty("line.separator");
092
093  protected static Log log_= Log.getInstance();
094  protected static boolean debugOut_ = true;
095
096  protected boolean persistingToFile_ = true;
097  protected boolean persistingToDB_ = false;
098  FileWriter f_ = null;
099  String fname_ = "/tmp/eagleMessages.txt";
100
101  Builder xmlBuilder = new Builder();
102  Document msgDoc_ = null;
103
104  /**  Constructor for the EagleUploader object */
105  public EagleUploader()
106  {
107    /*
108    System.out.println("[ca.bc.webarts.tools.EagleUploader > EagleUploader()]" +
109      (new java.util.Date()).toString());
110    */
111
112    initPersister();
113  }// -- Constructor
114
115
116  public void initPersister()
117  {
118    if (persistingToFile_) initFilePersister();
119  }
120
121
122  public void initFilePersister()
123  {
124    if (f_==null)
125      try
126      {
127        f_ = new FileWriter(fname_);
128      }
129      catch (IOException ioEx)
130      {
131      }
132  }
133
134
135  public void closePersister()
136  {
137    closeFilePersister();
138  }
139
140
141  public void closeFilePersister()
142  {
143    if (f_!=null)
144    {
145      try
146      {
147        f_.flush();
148        f_.close();
149      }
150      catch (IOException ioEx)
151      {
152        System.out.println("\nERROR Writing file: "+fname_);
153      }
154      f_ = null;
155    }
156  }
157
158
159  public String newMessage(String msgBody)
160  {
161    String retVal = "";
162
163    msgDoc_ = xmlBuilder.build(msgBody,null);
164
165    if (msgDoc_.getRootElement().getLocalName().equals("InstantaneousDemand"))
166
167
168    if (persistingToFile_)
169    try
170    {
171      if (f_!=null)
172      {
173        f_.write(msgBody);
174        f_.write("\n--------------------\n");
175        /*
176        FileOutputStream fos = new FileOutputStream(fileName);
177        byte[] strBytes = s.getBytes();
178        fos.write(strBytes);
179        fos.flush();
180        fos.close();
181        fos = null;
182        System.gc(); // this is required because a bug in Java won't realease
183        */
184      }
185    }
186    catch (IOException ioEx)
187    {
188      System.out.println("\nERROR Writing file: "+fname_);
189      retVal = null;
190    }
191
192    return retVal;
193  }
194}
195
196
197class MessageBody
198{
199}
200
201
202abstract class MessageFragment
203{
204  protected static String rootName_ = "";
205}
206
207
208abstract class MessageCommand
209{
210  protected static String rootName_ = "LocalCommand";
211}
212
213
214class InstantaneousDemand extends MessageFragment
215{
216  protected static String rootName_ = "InstantaneousDemand";
217}
218
219
220class SetFastPoll extends MessageCommand
221{
222
223  public SetFastPoll()
224  {
225    rootName_ = "RavenCommand";
226  }
227}
228