View Javadoc
1   package net.sf.mbus4j;
2   
3   /*
4    * #%L
5    * mbus4j-core
6    * %%
7    * Copyright (C) 2009 - 2014 MBus4J
8    * %%
9    * mbus4j - Drivers for the M-Bus protocol - http://mbus4j.sourceforge.net/
10   * Copyright (C) 2009-2014, mbus4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
29   */
30  import java.io.Closeable;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.ObjectInputStream;
34  import java.io.ObjectOutputStream;
35  import java.io.OutputStream;
36  import java.io.Serializable;
37  import net.sf.json.JSONObject;
38  import net.sf.mbus4j.json.JSONSerializable;
39  import net.sf.mbus4j.json.JsonSerializeType;
40  
41  /**
42   *
43   * @author aploese
44   */
45  public abstract class Connection implements JSONSerializable, Serializable, Closeable {
46  
47      public static final int DEFAULT_BAUDRATE = 2400;
48  
49      /**
50       * @return the connState
51       */
52      public State getConnState() {
53          return connState;
54      }
55  
56      /**
57       * @param connState the connState to set
58       */
59      protected void setConnState(State connState) {
60          this.connState = connState;
61      }
62  
63      /**
64       * @return the loggingStream
65       */
66      public OutputStream getLoggingStream() {
67          return loggingStream;
68      }
69  
70      /**
71       * @param loggingStream the loggingStream to set
72       */
73      public void setLoggingStream(OutputStream loggingStream) {
74          this.loggingStream = loggingStream;
75      }
76  
77      public enum State {
78  
79          OPENING, OPEN, CLOSING, CLOSED
80      };
81  
82      private int bitPerSecond;
83  
84      private int responseTimeOutOffset;
85      protected transient InputStream is;
86      protected transient OutputStream os;
87      private transient State connState = State.CLOSED;
88      private transient OutputStream loggingStream; 
89  
90      public Connection() {
91  
92      }
93  
94      public Connection(int bitPerSecond, int responseTimeOutOffset) {
95          this.bitPerSecond = bitPerSecond;
96          this.responseTimeOutOffset = responseTimeOutOffset;
97      }
98  
99      public InputStream getInputStream() {
100         return is;
101     }
102 
103     public OutputStream getOutputStrteam() {
104         return os;
105     }
106 
107     /**
108      * @return the bitPerSecond
109      */
110     public int getBitPerSecond() {
111         return bitPerSecond;
112     }
113 
114     public abstract String getName();
115     
116     
117     /**
118      * @param bitPerSecond the bitPerSecond to set
119      */
120     public void setBitPerSecond(int bitPerSecond) {
121         this.bitPerSecond = bitPerSecond;
122     }
123 
124     /**
125      * @return the responseTimeOutOffset
126      */
127     public int getResponseTimeOutOffset() {
128         return responseTimeOutOffset;
129     }
130 
131     /**
132      * @param responseTimeOutOffset the responseTimeOutOffset to set
133      */
134     public void setResponseTimeOutOffset(int responseTimeOutOffset) {
135         this.responseTimeOutOffset = responseTimeOutOffset;
136     }
137 
138     public abstract void open() throws IOException;
139 
140     @Override
141     public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
142         JSONObject result = new JSONObject();
143 
144         result.accumulate("bitPerSecond", bitPerSecond);
145         result.accumulate("responseTimeOutOffset", responseTimeOutOffset);
146 
147         return result;
148     }
149 
150     @Override
151     public void fromJSON(JSONObject json) {
152         bitPerSecond = json.getInt("bitPerSecond");
153         responseTimeOutOffset = json.getInt("responseTimeOutOffset");
154     }
155 
156     private static final long serialVersionUID = -1;
157     private static final int version = 1;
158 
159     // Serialization for saveDataSource
160     private void writeObject(ObjectOutputStream out) throws IOException {
161         out.writeInt(version);
162         out.writeInt(bitPerSecond);
163         out.writeInt(responseTimeOutOffset);
164     }
165 
166     private void readObject(ObjectInputStream in) throws IOException {
167         int ver = in.readInt();
168         switch (ver) {
169             case 1:
170                 readObjectVer1(in);
171                 break;
172         }
173     }
174 
175     private void readObjectVer1(ObjectInputStream in) throws IOException {
176         bitPerSecond = in.readInt();
177         responseTimeOutOffset = in.readInt();
178     }
179 
180     /**
181      * find fields in json and create approbirate connection instance
182      * @param json
183      * @return 
184      */
185     public static Connection createFromJSON(JSONObject json) {
186         Connection result = null;
187         if (json.containsKey(SerialPortConnection.SERIAL_CONNECTION)) {
188             result = new SerialPortConnection();
189             result.fromJSON(json.getJSONObject(SerialPortConnection.SERIAL_CONNECTION));
190         } else if (json.containsKey(TcpIpConnection.TCP_IP_CONNECTION)) {
191             result = new TcpIpConnection();
192             result.fromJSON(json.getJSONObject(TcpIpConnection.TCP_IP_CONNECTION));
193         }
194         return result;
195     }
196 
197     public abstract String getJsonFieldName();
198 
199 }