This commit is contained in:
Arndt 2016-08-06 13:46:30 +02:00
parent 19fd3112bf
commit 42e9ddbdd1
21 changed files with 327 additions and 135 deletions

View file

@ -2,9 +2,14 @@ package btools.routingapp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import android.app.Service;
@ -24,85 +29,201 @@ public class BRouterService extends Service
return myBRouterServiceStub;
}
private IBRouterService.Stub myBRouterServiceStub = new IBRouterService.Stub()
private IBRouterService.Stub myBRouterServiceStub = new IBRouterService.Stub()
{
@Override
public String getTrackFromParams(Bundle params) throws RemoteException
{
BRouterWorker worker = new BRouterWorker();
@Override
public String getTrackFromParams( Bundle params ) throws RemoteException
{
BRouterWorker worker = new BRouterWorker();
// get base dir from private file
String baseDir = null;
InputStream configInput = null;
try
{
configInput = openFileInput( "config.dat" );
BufferedReader br = new BufferedReader( new InputStreamReader (configInput ) );
baseDir = br.readLine();
}
catch( Exception e ) {}
finally
{
if ( configInput != null ) try { configInput.close(); } catch( Exception ee ) {}
}
String fast = params.getString( "fast" );
boolean isFast = "1".equals( fast ) || "true".equals( fast ) || "yes".equals( fast );
String mode_key = params.getString( "v" ) + "_" + (isFast ? "fast" : "short");
// get base dir from private file
String baseDir = null;
InputStream configInput = null;
try
{
configInput = openFileInput( "config.dat" );
BufferedReader br = new BufferedReader( new InputStreamReader( configInput ) );
baseDir = br.readLine();
}
catch (Exception e)
{
}
finally
{
if ( configInput != null ) try { configInput.close(); } catch (Exception ee) {}
}
worker.segmentDir = baseDir + "/brouter/segments4";
boolean configFound = false;
BufferedReader br = null;
try
{
String modesFile = baseDir + "/brouter/modes/serviceconfig.dat";
br = new BufferedReader( new FileReader (modesFile ) );
worker.segmentDir = baseDir + "/brouter/segments4";
for(;;)
{
String line = br.readLine();
if ( line == null ) break;
ServiceModeConfig smc = new ServiceModeConfig( line );
if ( !smc.mode.equals( mode_key ) ) continue;
worker.profilePath = baseDir + "/brouter/profiles2/" + smc.profile + ".brf";
worker.rawTrackPath = baseDir + "/brouter/modes/" + mode_key + "_rawtrack.dat";
CoordinateReader cor = CoordinateReader.obtainValidReader( baseDir, worker.segmentDir );
worker.nogoList = new ArrayList<OsmNodeNamed>();
// veto nogos by profiles veto list
for(OsmNodeNamed nogo : cor.nogopoints )
{
if ( !smc.nogoVetos.contains( nogo.ilon + "," + nogo.ilat ) )
{
worker.nogoList.add( nogo );
}
}
configFound = true;
}
}
catch( Exception e )
{
return "no brouter service config found, mode " + mode_key;
}
finally
{
if ( br != null ) try { br.close(); } catch( Exception ee ) {}
}
if ( !configFound )
{
return "no brouter service config found for mode " + mode_key;
}
String remoteProfile = params.getString( "remoteProfile" );
if ( remoteProfile == null )
{
remoteProfile = checkForTestDummy( baseDir );
}
String errMsg = remoteProfile == null
? getConfigFromMode( worker, baseDir, params.getString( "v" ), params.getString( "fast" ) )
: getConfigForRemoteProfile( worker, baseDir, remoteProfile );
if ( errMsg != null )
{
return errMsg;
}
try
{
return worker.getTrackFromParams( params );
}
catch (IllegalArgumentException iae)
{
return iae.getMessage();
}
}
private String getConfigFromMode( BRouterWorker worker, String baseDir, String mode, String fast )
{
boolean isFast = "1".equals( fast ) || "true".equals( fast ) || "yes".equals( fast );
String mode_key = mode + "_" + ( isFast ? "fast" : "short" );
BufferedReader br = null;
try
{
String modesFile = baseDir + "/brouter/modes/serviceconfig.dat";
br = new BufferedReader( new FileReader( modesFile ) );
for ( ;; )
{
String line = br.readLine();
if ( line == null )
break;
ServiceModeConfig smc = new ServiceModeConfig( line );
if ( !smc.mode.equals( mode_key ) )
continue;
worker.profilePath = baseDir + "/brouter/profiles2/" + smc.profile + ".brf";
worker.rawTrackPath = baseDir + "/brouter/modes/" + mode_key + "_rawtrack.dat";
CoordinateReader cor = CoordinateReader.obtainValidReader( baseDir, worker.segmentDir );
worker.nogoList = new ArrayList<OsmNodeNamed>();
// veto nogos by profiles veto list
for ( OsmNodeNamed nogo : cor.nogopoints )
{
if ( !smc.nogoVetos.contains( nogo.ilon + "," + nogo.ilat ) )
{
worker.nogoList.add( nogo );
}
}
return null;
}
}
catch (Exception e)
{
return "no brouter service config found, mode " + mode_key;
}
finally
{
if ( br != null ) try { br.close(); } catch( Exception ee ) {}
}
return "no brouter service config found for mode " + mode_key;
}
private String getConfigForRemoteProfile( BRouterWorker worker, String baseDir, String remoteProfile )
{
worker.profilePath = baseDir + "/brouter/profiles2/remote.brf";
worker.rawTrackPath = baseDir + "/brouter/modes/remote_rawtrack.dat";
// store profile only if not identical (to preserve timestamp)
byte[] profileBytes = remoteProfile.getBytes();
File profileFile = new File( worker.profilePath );
try
{
// add nogos from waypoint database
CoordinateReader cor = CoordinateReader.obtainValidReader( baseDir, worker.segmentDir );
worker.nogoList = new ArrayList<OsmNodeNamed>( cor.nogopoints );
if ( !fileEqual( profileBytes, profileFile ) )
{
OutputStream os = null;
try
{
os = new FileOutputStream( profileFile );
os.write( profileBytes );
}
finally
{
if ( os != null ) try { os.close(); } catch( IOException io ) {}
}
}
}
catch( Exception e )
{
return "error caching remote profile: " + e;
}
return null;
}
private boolean fileEqual( byte[] fileBytes, File file ) throws Exception
{
if ( !file.exists() )
{
return false;
}
int nbytes = fileBytes.length;
int pos = 0;
int blen = 8192;
byte[] buf = new byte[blen];
InputStream is = null;
try
{
is = new FileInputStream( file );
while( pos < nbytes )
{
int len = is.read( buf, 0, blen );
if ( len <= 0 ) return false;
if ( pos + len > nbytes ) return false;
for( int j=0; j<len; j++ )
{
if ( fileBytes[pos++] != buf[j] )
{
return false;
}
}
}
return true;
}
finally
{
if ( is != null ) try { is.close(); } catch( IOException io ) {}
}
}
private String checkForTestDummy( String baseDir )
{
File testdummy = new File( baseDir + "/brouter/profiles2/remotetestdummy.brf" );
if ( !testdummy.exists() ) return null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try
{
br = new BufferedReader( new FileReader( testdummy ) );
for ( ;; )
{
String line = br.readLine();
if ( line == null )
break;
sb.append( line ).append( '\n' );
}
return sb.toString();
}
catch (Exception e)
{
throw new RuntimeException( "error reading " + testdummy );
}
finally
{
if ( br != null ) try { br.close(); } catch( Exception ee ) {}
}
}
try
{
return worker.getTrackFromParams(params);
}
catch( IllegalArgumentException iae )
{
return iae.getMessage();
}
}
};
@Override

View file

@ -167,20 +167,22 @@ public class BRouterView extends View
String basedir = fbd.getAbsolutePath();
AppLogger.log( "using basedir: " + basedir );
String version = "v1.4.3";
// create missing directories
assertDirectoryExists( "project directory", basedir + "/brouter", null );
assertDirectoryExists( "project directory", basedir + "/brouter", null, null );
segmentDir = basedir + "/brouter/segments4";
if ( assertDirectoryExists( "data directory", segmentDir, "segments4.zip" ) )
if ( assertDirectoryExists( "data directory", segmentDir, "segments4.zip", null ) )
{
ConfigMigration.tryMigrateStorageConfig(
new File( basedir + "/brouter/segments3/storageconfig.txt" ),
new File( basedir + "/brouter/segments4/storageconfig.txt" ) );
}
profileDir = basedir + "/brouter/profiles2";
assertDirectoryExists( "profile directory", profileDir, "profiles2.zip" );
assertDirectoryExists( "profile directory", profileDir, "profiles2.zip", version );
modesDir = basedir + "/brouter/modes";
assertDirectoryExists( "modes directory", modesDir, "modes.zip" );
assertDirectoryExists( "readmes directory", basedir + "/brouter/readmes", "readmes.zip" );
assertDirectoryExists( "modes directory", modesDir, "modes.zip", null );
assertDirectoryExists( "readmes directory", basedir + "/brouter/readmes", "readmes.zip", version );
cor = CoordinateReader.obtainValidReader( basedir, segmentDir );
wpList = cor.waypoints;
@ -196,7 +198,7 @@ public class BRouterView extends View
if ( cor.tracksdir != null )
{
tracksDir = cor.basedir + cor.tracksdir;
assertDirectoryExists( "track directory", tracksDir, null );
assertDirectoryExists( "track directory", tracksDir, null, null );
// output redirect: look for a pointerfile in tracksdir
File tracksDirPointer = new File( tracksDir + "/brouter.redirect" );
@ -465,6 +467,12 @@ public class BRouterView extends View
rc.prepareNogoPoints( nogoList );
rc.nogopoints = nogoList;
// for profile remote, use ref-track logic same as service interface
if ( "remote".equals( profileName ) )
{
rc.rawTrackPath = modesDir + "/remote_rawtrack.dat";
}
cr = new RoutingEngine( tracksDir + "/brouter", null, segmentDir, wpList, rc );
cr.start();
invalidate();
@ -477,12 +485,27 @@ public class BRouterView extends View
}
}
private boolean assertDirectoryExists( String message, String path, String assetZip )
private boolean assertDirectoryExists( String message, String path, String assetZip, String versionTag )
{
File f = new File( path );
if ( !f.exists() )
boolean exists = f.exists();
if ( !exists )
{
f.mkdirs();
}
if ( versionTag != null )
{
File vtag = new File( f, versionTag );
try
{
exists = !vtag.createNewFile();
}
catch( IOException io ) { throw new RuntimeException( "error checking version tag " + vtag ); }
}
if ( !exists )
{
// default contents from assets archive
if ( assetZip != null )
{
@ -680,11 +703,20 @@ public class BRouterView extends View
}
else
{
String result = "version = BRouter-1.4.2\n" + "distance = " + cr.getDistance() / 1000. + " km\n" + "filtered ascend = " + cr.getAscend()
String result = "version = BRouter-1.4.3\n" + "distance = " + cr.getDistance() / 1000. + " km\n" + "filtered ascend = " + cr.getAscend()
+ " m\n" + "plain ascend = " + cr.getPlainAscend();
rawTrack = cr.getFoundRawTrack();
// for profile "remote", always persist referencetrack
if ( cr.getAlternativeIndex() == 0 )
{
if ( "remote".equals( profileName ) )
{
writeRawTrackToMode( "remote" );
}
}
String title = "Success";
if ( cr.getAlternativeIndex() > 0 )
title += " / " + cr.getAlternativeIndex() + ". Alternative";

View file

@ -67,7 +67,7 @@ public class BRouterWorker
cr.doRun( maxRunningTime );
// store new reference track if any
// (can exist fot timeed-out search)
// (can exist for timed-out search)
if ( cr.getFoundRawTrack() != null )
{
try

View file

@ -17,6 +17,7 @@ interface IBRouterService {
// "nogoRadi"-->double[] array of nogo radius in meters; may be null.
// "fast"-->[0|1]
// "v"-->[motorcar|bicycle|foot]
// "remoteProfile"--> (String), net-content of a profile. If remoteProfile != null, v+fast are ignored
//return null if all ok and no path given, the track if ok and path given, an error message if it was wrong
//call in a background thread, heavy task!