org.openuat.sensors
Class AsciiLineReaderBase

java.lang.Object
  extended by org.openuat.sensors.AsciiLineReaderBase
Direct Known Subclasses:
ParallelPortPWMReader, WiTiltRawReader

public abstract class AsciiLineReaderBase
extends java.lang.Object

This is a base class for reading from sensors that are represented by simple files and output ASCII lines, with one line for each sample. It handles registering of sinks and sending sample events to them as well as managing a background thread for sampling the values. The parseLine method must be implemented by derived classes, which is expected to call the emitSample method to send out the samples to all registered sinks.

Version:
1.0
Author:
Rene Mayrhofer

Field Summary
protected  int maxNumLines
          The maximum number of data lines to read from the device - depends on the sensor.
protected  java.io.InputStream port
          This represent the file to read from and is opened in the constructor.
 
Constructor Summary
protected AsciiLineReaderBase(java.io.InputStream stream, int maxNumLines)
          Initializes the reader base object.
protected AsciiLineReaderBase(int maxNumLines)
          Initializes the reader base object.
protected AsciiLineReaderBase(java.lang.String filename, int maxNumLines)
          Initializes the reader base object.
 
Method Summary
 void addSink(int[] lines, SamplesSink[] sink)
          Registers a sink, which will receive all new values as they are sampled.
 void dispose()
          This causes the reader to be shut down properly by calling stop() and making sure that all ressources are freed properly when this object is garbage collected.
protected  void emitSample(double[] sample)
          This method should be called by the parseLine method to send samples to all registered listeners.
 int getMaxNumLines()
          Returns the maximum number of lines that can be sampled.
protected abstract  void parseLine(java.lang.String line)
           
 boolean removeSink(int[] lines, SamplesSink[] sink)
          Removes a previously registered sink.
 void simulateSampling()
          Simulate sampling by reading all available lines from the spcified file.
 void start()
          Starts a new background thread to read from the file and create sample values as the lines are read.
 void stop()
          Stops the background thread, if started previously.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxNumLines

protected int maxNumLines
The maximum number of data lines to read from the device - depends on the sensor.


port

protected java.io.InputStream port
This represent the file to read from and is opened in the constructor.

See Also:
AsciiLineReaderBase(String, int)
Constructor Detail

AsciiLineReaderBase

protected AsciiLineReaderBase(java.lang.String filename,
                              int maxNumLines)
                       throws java.io.FileNotFoundException
Initializes the reader base object. It only saves the passed parameters and opens the InputStream to read from the specified file, and thus implicitly to check if the file exists and can be opened.

Parameters:
filename - The log to read from. This may either be a normal log file when simulation is intended or it can be a FIFO/pipe to read online data.
maxNumLines - The maximum number of data lines to read from the device - depends on the sensor.
Throws:
java.io.FileNotFoundException - When filename does not exist or can not be opened.

AsciiLineReaderBase

protected AsciiLineReaderBase(java.io.InputStream stream,
                              int maxNumLines)
Initializes the reader base object. It only saves the passed parameters. This is an alternative version to

Parameters:
stream - Specifies the InputStream to read from.
maxNumLines - The maximum number of data lines to read from the device - depends on the sensor.
See Also:
and should only be used in special cases.

AsciiLineReaderBase

protected AsciiLineReaderBase(int maxNumLines)
Initializes the reader base object. It only saves the passed parameter and does not initialze the port member variable. Any derived class using this constructor must initialize port before calling any other method.

Method Detail

addSink

public void addSink(int[] lines,
                    SamplesSink[] sink)
             throws java.lang.IllegalArgumentException
Registers a sink, which will receive all new values as they are sampled.

Parameters:
sink - The time series to fill. This array must have the same number of elements as the number of lines specified to the constructor.
lines - The set of lines on the device to read. Must be an integer array with a minimum length of 1 and a maximum length specified to the constructor, containing the indices of the lines to read. These indices are counted from 0 to maxNumLines-1. E.g. for a parallel port (see ParallelPortPWMReader), this corresponds to data lines DATA0 to DATA7. E.g. for a 3D accelerometer (see WiTiltRawReader), this corresponds to 0=X, 1=Y, 2=Z.
Throws:
java.lang.IllegalArgumentException

removeSink

public boolean removeSink(int[] lines,
                          SamplesSink[] sink)
Removes a previously registered sink.

Parameters:
sink - The time series to stop filling.
lines - The set of lines with which this sink has been registered.
Returns:
true if removed, false if not (i.e. if they have not been added previously).
See Also:
addSink(int[], SamplesSink[])

start

public void start()
Starts a new background thread to read from the file and create sample values as the lines are read.


stop

public void stop()
Stops the background thread, if started previously.


simulateSampling

public void simulateSampling()
                      throws java.io.IOException
Simulate sampling by reading all available lines from the spcified file.

Throws:
java.io.IOException

dispose

public void dispose()
This causes the reader to be shut down properly by calling stop() and making sure that all ressources are freed properly when this object is garbage collected. #see stop


getMaxNumLines

public int getMaxNumLines()
Returns the maximum number of lines that can be sampled. This depends on the specific sensor implementation.

Returns:
The value of @see maxNumLines.

emitSample

protected void emitSample(double[] sample)
This method should be called by the parseLine method to send samples to all registered listeners.

Parameters:
sample - The current sample.

parseLine

protected abstract void parseLine(java.lang.String line)


2005-2006, Rene Mayrhofer.