View Javadoc
1   package fr.ifremer.tutti.caliper.feed;
2   
3   /*
4    * #%L
5    * Tutti :: Ichtyometer API
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.caliper.feed.event.CaliperFeedReaderEvent;
26  import fr.ifremer.tutti.caliper.feed.event.CaliperFeedReaderListener;
27  import fr.ifremer.tutti.caliper.feed.record.CaliperFeedReaderMeasureRecord;
28  import fr.ifremer.tutti.caliper.feed.record.CaliperFeedReaderRecordFactory;
29  import fr.ifremer.tutti.caliper.feed.record.CaliperFeedReaderRecordSupport;
30  import gnu.io.CommPortIdentifier;
31  import gnu.io.NoSuchPortException;
32  import gnu.io.PortInUseException;
33  import gnu.io.SerialPort;
34  import gnu.io.SerialPortEvent;
35  import gnu.io.SerialPortEventListener;
36  import gnu.io.UnsupportedCommOperationException;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  import javax.swing.event.EventListenerList;
41  import java.io.Closeable;
42  import java.io.IOException;
43  import java.io.InputStream;
44  import java.util.TooManyListenersException;
45  
46  /**
47   * To read some records from a caliper in feed mode.
48   *
49   * @author Kevin Morin (Code Lutin)
50   * @since 4.5
51   */
52  public class CaliperFeedReader implements Closeable {
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(CaliperFeedReader.class);
56  
57      private static final String SERIAL_PORT_PREFIX = "COM";
58  
59      /**
60       * Name of the serial port
61       */
62      protected String serialPortName;
63  
64      /**
65       * Serial port open in the {@code start} method.
66       */
67      protected SerialPort serialPort;
68  
69      /**
70       * To keep list of {@link CaliperFeedReaderListener} listeners.
71       */
72      protected final EventListenerList listenerList;
73  
74      /**
75       * To generate some feed record from the rax record send by the board.
76       *
77       * @since 3.10
78       */
79      protected final CaliperFeedReaderRecordFactory recordFactory;
80  
81      public CaliperFeedReader() {
82          listenerList = new EventListenerList();
83          recordFactory = new CaliperFeedReaderRecordFactory();
84      }
85  
86      public void start(int portNumber) throws CaliperConnectionException {
87  
88          this.serialPortName = SERIAL_PORT_PREFIX + portNumber;
89  
90          try {
91              CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
92  
93              serialPort = portIdentifier.open(this.getClass().getName(), 2000);
94              serialPort.setSerialPortParams(4800, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
95  
96              InputStream inputStream = serialPort.getInputStream();
97  
98              serialPort.addEventListener(new SerialReader(inputStream));
99              serialPort.notifyOnDataAvailable(true);
100 
101             if (log.isDebugEnabled()) {
102                 log.debug("Ready to read remote device...");
103             }
104 
105         } catch (IOException | TooManyListenersException | PortInUseException | NoSuchPortException | UnsupportedCommOperationException e) {
106             if (log.isErrorEnabled()) {
107                 log.error("Error while connecting to the serial port " + serialPortName, e);
108             }
109             throw new CaliperConnectionException(e);
110         }
111     }
112 
113     @Override
114     public void close() throws IOException {
115 
116         try {
117             serialPort.close();
118 
119         } finally {
120             // do nothing if the close fails
121         }
122     }
123 
124     public void addFeedModeReaderListener(CaliperFeedReaderListener listener) {
125         listenerList.add(CaliperFeedReaderListener.class, listener);
126     }
127 
128     public void removeFeedModeReaderListener(CaliperFeedReaderListener listener) {
129         listenerList.remove(CaliperFeedReaderListener.class, listener);
130     }
131 
132     public void removeAllFeedModeReaderListeners() {
133         for (CaliperFeedReaderListener listener : listenerList.getListeners(CaliperFeedReaderListener.class)) {
134             listenerList.remove(CaliperFeedReaderListener.class, listener);
135         }
136     }
137 
138     public String getSerialPortName() {
139         return serialPortName;
140     }
141 
142     /**
143      * Handles the input coming from the serial port. A new line character
144      * is treated as the end of a block in this example.
145      */
146     public class SerialReader implements SerialPortEventListener {
147 
148         private InputStream in;
149         private byte[] buffer = new byte[1024];
150 
151         public SerialReader(InputStream in)
152         {
153             this.in = in;
154         }
155 
156         public void serialEvent(SerialPortEvent arg0) {
157 
158             try {
159                 CaliperFeedReaderRecordSupport readerRecord = readRecord();
160 
161                 if (readerRecord != null && readerRecord instanceof CaliperFeedReaderMeasureRecord) {
162 
163                     // send new record to listeners
164                     CaliperFeedReaderEvent e = new CaliperFeedReaderEvent(CaliperFeedReader.this, (CaliperFeedReaderMeasureRecord) readerRecord);
165                     for (CaliperFeedReaderListener listener : listenerList.getListeners(CaliperFeedReaderListener.class)) {
166                         listener.recordRead(e);
167                     }
168 
169                 }
170 
171             } catch (IOException e) {
172                 if (log.isErrorEnabled()) {
173                     log.error("Could not read record", e);
174                 }
175             }
176         }
177 
178         private CaliperFeedReaderRecordSupport readRecord() throws IOException {
179 
180             int len = 0;
181             int data;
182 
183             while (( data = in.read()) > -1) {
184                 if (data == '\n') {
185                     break;
186                 }
187                 buffer[len++] = (byte) data;
188             }
189 
190             String result = new String(buffer,0,len);
191 
192             if (log.isInfoEnabled()) {
193                 log.info("New raw record: " + result);
194             }
195             CaliperFeedReaderRecordSupport readerRecord = recordFactory.newRecord(result);
196 
197             if (log.isDebugEnabled()) {
198                 log.debug("New feed record: " + readerRecord);
199             }
200 
201             return readerRecord;
202         }
203 
204     }
205 
206 }