📄 gpsdriver.java
字号:
}
if ( replayMode )
{
disableReplay();
}
replayMode = true;
replayThread = new Thread( this, "replaythread" );
replayThread.start();
}
/**
* Disables replay mode.
*/
public void disableReplay()
{
if ( replayThread != null )
{
replayMode = false;
while ( replayThread != null )
{
delay();
}
}
}
/**
* Enables GPS hardware reporting.
*/
public void enableHardware()
{
if ( gpsMode == false )
{
gpsMode = true;
gpsThread = new Thread( this, "gpsthread" );
gpsThread.start();
}
}
/**
* Disables GPS hardware reporting.
*/
public void disableHardware()
{
if ( gpsMode )
{
gpsMode = false;
while ( gpsThread != null )
{
delay();
}
}
}
/**
* Start logging GPS information into the given output stream (usually a
* FileOutputStream will be used).
*
*@param s Output stream to use for logging.
*/
public void enableRawLogging( OutputStream s )
{
disableRawLogging();
logRawStream = s;
logRawInput = true;
}
/**
* Stops logging process. Does nothing if logging was not enabled.
*/
public void disableRawLogging()
{
if ( logRawStream != null )
{
try
{
logRawStream.close();
}
catch ( IOException ioe )
{
}
}
logRawInput = false;
logRawStream = null;
}
/**
* Enables logging current GPS data into a stream.
*
*@param s Stream to use for logging.
*/
public void enableLogging( OutputStream s )
{
gpsLog = new GPSLog( new Date() );
disableLogging();
logStream = s;
logInput = true;
logThread = new Thread( this, "logthread" );
logThread.start();
}
/**
* Disable logging.
*/
public void disableLogging()
{
if ( logInput == false )
{
return;
}
logInput = false;
while ( logThread != null )
{
delay();
}
try
{
gpsLog.save( logStream );
logStream.flush();
}
catch ( IOException ioe )
{
}
logInput = false;
logStream = null;
}
/**
* Close the GPS device.
*
*@throws IOException
*/
public void close() throws IOException
{
if ( gpsThread != null )
{
gpsThread.stop();
gpsThread = null;
}
if ( inputStream != null )
{
inputStream.close();
}
if ( outputStream != null )
{
outputStream.close();
}
if ( serialPort != null )
{
serialPort.close();
}
inputStream = null;
outputStream = null;
serialPort = null;
}
/**
* Method
*/
public void replayThread()
{
int t = 0;
long t0 = System.currentTimeMillis();
try
{
while ( t < gpsReplayLog.getDuration() && replayMode == true )
{
info("replay at t="+t+" / "+gpsReplayLog.getDuration());
gpsInfo = gpsReplayLog.getInfoAt( t );
t++;
processListeners();
long t1 = System.currentTimeMillis();
try
{
Thread.currentThread().sleep( t0 + 1000 * t - t1 );
}
catch ( Exception ie )
{
}
}
}
catch ( Exception e )
{
e.printStackTrace();
}
replayThread = null;
}
/**
* Method
*/
public void logThread()
{
long t0 = System.currentTimeMillis();
int t = 0;
while ( logInput == true )
{
GPSInfo gc = null;
try
{
gc = ( GPSInfo ) gpsInfo.clone();
}
catch ( CloneNotSupportedException cnse )
{
}
//normGPSInfo( gc );
gpsLog.setInfoAt( t, gc );
t++;
long t1 = System.currentTimeMillis();
try
{
long sl = 1000 * t + t0 - t1;
info( "logging at t="+gpsLog.getDuration() );
if ( sl > 0 )
{
Thread.currentThread().sleep( sl );
}
}
catch ( InterruptedException ie )
{
}
}
logThread = null;
}
/**
* Method
*/
public void gpsThread()
{
if ( inputStream == null )
{
gpsThread = null;
return;
}
BufferedReader in = new BufferedReader( new InputStreamReader(
inputStream ) );
tmpInfo = new GPSInfo();
gpsInfo = new GPSInfo();
NMEA.infiniteAge( tmpInfo );
long t0;
long t1;
long t2;
String msg = "";
boolean replayLog = false;
long replayBase = -1;
try
{
msg = in.readLine();
}
catch ( Exception e )
{
}
//System.out.println( msg );
// check if we are reading previously logged messages
if ( simulate == true && msg.charAt( 0 ) != '$' && msg.indexOf( ":" ) > 0
&& Character.isDigit( msg.charAt( 0 ) ) )
{
replayLog = true;
replayBase = Long.parseLong( msg.substring( 0, msg.indexOf( ':' ) ) );
}
t0 = t1 = System.currentTimeMillis();
while ( readMode == true )
{
try
{
msg = in.readLine();
info( "msg from GPS device: "+msg );
if ( msg == null )
{
delay();
}
// check if we are reading previously logged messages
if ( msg != null && replayLog )
{
// read the time when the msg was logged, and
// skip time in msg
long twait = Long.parseLong( msg.substring( 0, msg.indexOf( ':' ) ) ) - replayBase;
msg = msg.substring( msg.indexOf( ':' ) + 1 );
//System.out.println("Replay "+msg);
long ct = System.currentTimeMillis();
// wait to replay correct timely behaviour
if ( ct - t0 < twait )
{
//System.out.println(" sleeping "+ (twait - (ct-t0)) );
Thread.currentThread().sleep( twait - ( ct - t0 ) );
//Thread.currentThread().sleep( 10 );
}
}
/* if (simulate && msg!=null)
{
if (replayBase<0)
{
if (gpsInfo.utc!=null)
{
replayBase = utcToSeconds( gpsInfo.utc );
}
}
else
{
if (gpsInfo.utc!=null)
{
long ti = utcToSeconds( gpsInfo.utc );
Thread.currentThread().sleep( 1000*(ti-replayBase) );
replayBase = ti;
}
}
}
*/
if ( logInput )
{
try
{
logStream.write( new String( "" + System.currentTimeMillis() + ":" ).getBytes() );
logStream.write( ( msg + "\n" ).getBytes() );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
if ( msg != null && NMEA.check( msg ) )
{
//System.out.println("DEBUG "+msg);
// parse known messages
synchronized ( gpsInfo )
{
if ( NMEA.parse( msg, tmpInfo ) )
{
gpsParsedMessages++;
}
gpsInfoUpdated = true;
gpsInfo = ( GPSInfo ) tmpInfo.clone();
normGPSInfo( gpsInfo );
processListeners();
}
}
t2 = System.currentTimeMillis();
// check if GPSInfo has to be aged...
while ( t2 - t1 > 1000 )
{
NMEA.increaseAge( tmpInfo );
t1 += 1000;
//System.out.println("\n\n*** GPSInfo ***");
//NMEA.printAll( gpsInfo );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
gpsThread = null;
}
/**
* Main thread.
*/
public void run()
{
String name = Thread.currentThread().getName();
if ( name.equals( "logthread" ) )
{
logThread();
}
else if ( name.equals( "gpsthread" ) )
{
gpsThread();
}
else if ( name.equals( "replaythread" ) )
{
replayThread();
}
}
/**
* Add new GPS listener.
*
*@param gl GPSListener to add.
*/
public void addGPSListener( GPSListener gl )
{
synchronized ( listeners )
{
// insert listeners once only!
if ( !listeners.contains( gl ) )
{
listeners.add( gl );
}
}
}
/**
* Remove GPS listener.
*
*@param gl GPSListener to remove.
*/
public void removeGPSListener( GPSListener gl )
{
synchronized ( listeners )
{
listeners.remove( gl );
}
}
/**
* Send updated GPSInfo to all registered listeners.
*/
void processListeners()
{
for ( int i = 0; i < listeners.size(); i++ )
{
GPSListener vl = ( GPSListener ) listeners.get( i );
vl.gpsEvent( getGPSInfo() );
}
}
/**
* Delay for a couple of milliseconds.
*/
protected void delay()
{
try
{
Thread.sleep( 50 );
}
catch (InterruptedException ignored)
{}
}
/**
* Print info message if verbose is true
*/
protected void info(String msg)
{
if (verbose)
System.out.println( "GPSDriver: "+msg );
}
/**
* Set verbose mode.
*/
public void setVerbose( boolean mode )
{
verbose = mode;
}
/**
* Get current verbose mode.
*/
public boolean getVerbose()
{
return verbose;
}
/**
* Description of the Class
*
*@author walther
*/
static class Configuration {
public String port;
public int baudRate;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -