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