diff --git a/examples/config.js b/examples/config.js index 21cc54b3..48d56f59 100644 --- a/examples/config.js +++ b/examples/config.js @@ -38,7 +38,79 @@ module.exports = { // } // } // } - // } + // }, + // // Configuration for simple routing modes, using OSRM-like engines + // driving: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { + // port: 7000, // Port used to access OSRM, either locally or remotely + // host: null, // If set to null, localhost will be used. Ignored if autoStart set to true + // autoStart: true, // If true, a local instance of OSRM will be started + // enabled: true // If true, this mode will be configured, otherwise will be left out + // } + // } + // }, + // cycling: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 8000, host: null, autoStart: true, enabled: true } + // } + // }, + // walking: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 5001, host: null, autoStart: true, enabled: true } + // } + // }, + // bus_suburb: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 7110, host: null, autoStart: true, enabled: true } + // } + // }, + // bus_urban: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 7120, host: null, autoStart: true, enabled: true } + // } + // }, + // rail: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 9000, host: null, autoStart: false, enabled: false } + // } + // }, + // tram: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 9100, host: null, autoStart: false, enabled: false } + // } + // }, + // tram_train: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 9200, host: null, autoStart: false, enabled: false } + // } + // }, + // metro: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 9300, host: null, autoStart: false, enabled: false } + // } + // }, + // monorail: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 9400, host: null, autoStart: false, enabled: false } + // } + // }, + // cable_car: { + // defaultEngine: 'osrmRouting', + // engines: { + // osrmRouting: { port: 9500, host: null, autoStart: false, enabled: false } + // } + // }, // }, mapDefaultCenter: { @@ -78,77 +150,6 @@ module.exports = { }, defaultPreferences: { - osrmRouting: { - modes: { - driving: { - // !!! Be careful: use your own server, since you may be blocked on the osrm demo server after too many queries - port : 7000, // Port used to access OSRM, either locally or remotely - host : null, // If set to null, localhost will be used. Ignored if autoStart set to true - autoStart: true, // If true, a local instance of OSRM will be started - enabled : true // If true, this mode will be configured, otherwise will be left out - }, - cycling: { - port : 8000, - host : null, - autoStart: true, - enabled : true - }, - walking: { - port : 5001, - host : null, - autoStart: true, - enabled : true - }, - bus_suburb: { - port : 7110, - host : null, - autoStart: true, - enabled : true - }, - bus_urban: { - port : 7120, - host : null, - autoStart: true, - enabled : true - }, - rail: { - port : 9000, - host : null, - autoStart: false, - enabled : false - }, - tram: { - port : 9100, - host : null, - autoStart: false, - enabled : false - }, - tram_train: { - port : 9200, - host : null, - autoStart: false, - enabled : false - }, - metro: { - port : 9300, - host : null, - autoStart: false, - enabled : false - }, - monorail: { - port : 9400, - host : null, - autoStart: false, - enabled : false - }, - cable_car: { - port : 9500, - host : null, - autoStart: false, - enabled : false - } - } - }, transit: { routing: { batch: { diff --git a/packages/chaire-lib-backend/src/config/ServerConfig.ts b/packages/chaire-lib-backend/src/config/ServerConfig.ts index 6cf20250..96774a93 100644 --- a/packages/chaire-lib-backend/src/config/ServerConfig.ts +++ b/packages/chaire-lib-backend/src/config/ServerConfig.ts @@ -58,6 +58,17 @@ class ServerConfig { } return engineConfiguration[instance] || { port: 4000, cacheAllScenarios: false }; }; + + getAllModesForEngine = (engine: string): string[] => { + const modesForEngine: string[] = []; + for (const modeName in config.routing) { + if (config.routing[modeName].engines[engine] !== undefined) { + modesForEngine.push(modeName); + } + } + + return modesForEngine; + }; } const instance = new ServerConfig(); diff --git a/packages/chaire-lib-backend/src/config/__tests__/ServerConfig.test.ts b/packages/chaire-lib-backend/src/config/__tests__/ServerConfig.test.ts index 8e28c3b4..34fd01c3 100644 --- a/packages/chaire-lib-backend/src/config/__tests__/ServerConfig.test.ts +++ b/packages/chaire-lib-backend/src/config/__tests__/ServerConfig.test.ts @@ -37,7 +37,7 @@ describe('get routing mode and engine configs', () => { }); test('getRoutingModeConfig, mode does not exist', () => { - expect(ServerConfig.getRoutingModeConfig('walking')).toBeUndefined(); + expect(ServerConfig.getRoutingModeConfig('walking_way_data_as_name')).toBeUndefined(); }); test('getRoutingEngineConfigForMode, mode and engine exist', () => { @@ -52,7 +52,29 @@ describe('get routing mode and engine configs', () => { }); test('getRoutingEngineConfigForMode, mode does not exist', () => { - expect(ServerConfig.getRoutingEngineConfigForMode('walking', 'osrm')).toBeUndefined(); + expect(ServerConfig.getRoutingEngineConfigForMode('walking_way_data_as_name', 'osrmRouting')).toBeUndefined(); }); + test('getAllModesForEngine, at least one mode uses engine', () => { + expect(ServerConfig.getAllModesForEngine('trRouting')).toEqual(['transit']); + expect(ServerConfig.getAllModesForEngine('osrmRouting')).toEqual([ + 'driving', + 'driving_congestion', + 'cycling', + 'walking', + 'bus_suburb', + 'bus_urban', + 'bus_congestion', + 'rail', + 'tram', + 'tram_train', + 'metro', + 'monorail', + 'cable_car' + ]); + }); + + test('getAllModesForEngine, no mode uses engine', () => { + expect(ServerConfig.getAllModesForEngine('valhalla')).toEqual([]); + }); }); diff --git a/packages/chaire-lib-backend/src/config/__tests__/server.config.test.ts b/packages/chaire-lib-backend/src/config/__tests__/server.config.test.ts index 684e6e9d..e441c2dc 100644 --- a/packages/chaire-lib-backend/src/config/__tests__/server.config.test.ts +++ b/packages/chaire-lib-backend/src/config/__tests__/server.config.test.ts @@ -16,6 +16,7 @@ test('Expected default with env', () => { expect(config.projectDirectory).toEqual(path.normalize(`${__dirname}/../../../../../tests/dir`)); expect(config.routing.transit.engines.trRouting!.single).toEqual({ port: 4000, cacheAllScenarios: false, debug: false, logs: { nbFiles: 3, maxFileSizeKB: 5120 } }); expect(config.routing.transit.engines.trRouting!.batch).toEqual({ port: 14000, cacheAllScenarios: false, debug: false, logs: { nbFiles: 3, maxFileSizeKB: 5120 }}); + expect(config.routing.driving!.engines.osrmRouting).toEqual({ port: 7000, host: null, autoStart: true, enabled: true }); }); test('setProjectConfiguration', () => { @@ -28,6 +29,12 @@ test('setProjectConfiguration', () => { engines: { trRouting: { single: { port: 5000 }, batch: { logs: { maxFileSizeKB: 10000 } } } as any } + }, + driving: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { port: 1234, enabled: false } as any + } } } }); @@ -37,4 +44,5 @@ test('setProjectConfiguration', () => { // Make sure the deep merge works for object configs expect(config.routing.transit.engines.trRouting!.single).toEqual({ port: 5000, cacheAllScenarios: false, debug: false, logs: { nbFiles: 3, maxFileSizeKB: 5120 } }); expect(config.routing.transit.engines.trRouting!.batch).toEqual({ port: 14000, cacheAllScenarios: false, debug: false, logs: { nbFiles: 3, maxFileSizeKB: 10000 } }); + expect(config.routing.driving!.engines.osrmRouting).toEqual({ port: 1234, host: null, autoStart: true, enabled: false }); }); diff --git a/packages/chaire-lib-backend/src/config/server.config.ts b/packages/chaire-lib-backend/src/config/server.config.ts index 20bdbc1e..404beb92 100644 --- a/packages/chaire-lib-backend/src/config/server.config.ts +++ b/packages/chaire-lib-backend/src/config/server.config.ts @@ -50,6 +50,26 @@ export type TrRoutingConfig = { // support it in both batch and single calculation }; +export type OsrmRoutingConfig = { + /** + * Port used to access OSRM, either locally or remotely. + */ + port: number | null; + /** + * If set to null, localhost will be used. Ignored if autoStart set to true. + * Do not prefix with the protocol (ex: 'some.osrm.site.com') + */ + host: string | null; + /** + * If true, a local instance of OSRM will be started when launching transition (client). + */ + autoStart: boolean; + /** + * If true, this mode will be configured, otherwise it will be left out. Usable when routing paths. + */ + enabled: boolean; +}; + // TODO Some project config option depend on the application, so should not be // typed in chaire-lib. Each app (transition , evolution, etc) should add types // to the config and should have a project.config file which imports this one @@ -87,8 +107,7 @@ export type ServerSideProjectConfiguration = { [key in RoutingMode]?: { defaultEngine: string; engines: { - // TODO Type and intialize these configuration for routing modes - [key: string]: any; + osrmRouting?: OsrmRoutingConfig; }; }; }; @@ -165,6 +184,149 @@ setProjectConfigurationCommon({ } } } + }, + driving: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 7000, + host: null, + autoStart: true, + enabled: true + } + } + }, + driving_congestion: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 7500, + host: null, + autoStart: false, + enabled: false + } + } + }, + cycling: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 8000, + host: null, + autoStart: true, + enabled: true + } + } + }, + walking: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 5000, + host: null, + autoStart: true, + enabled: true + } + } + }, + bus_suburb: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 7200, + host: null, + autoStart: true, + enabled: true + } + } + }, + bus_urban: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 7300, + host: null, + autoStart: true, + enabled: true + } + } + }, + bus_congestion: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 7400, + host: null, + autoStart: false, + enabled: false + } + } + }, + rail: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 9000, + host: null, + autoStart: false, + enabled: false + } + } + }, + tram: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 9100, + host: null, + autoStart: false, + enabled: false + } + } + }, + tram_train: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 9200, + host: null, + autoStart: false, + enabled: false + } + } + }, + metro: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 9300, + host: null, + autoStart: false, + enabled: false + } + } + }, + monorail: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 9400, + host: null, + autoStart: false, + enabled: false + } + } + }, + cable_car: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { + port: 9500, + host: null, + autoStart: false, + enabled: false + } + } } } }); @@ -191,6 +353,35 @@ export const setProjectConfiguration = (newConfig: Partial { - const osrmServerPrefs = Preferences.get('osrmRouting', {}); - if ( - osrmServerPrefs.modes[routingMode] && - osrmServerPrefs.modes[routingMode].enabled === true && - osrmServerPrefs.modes[routingMode].autoStart !== true - ) { + const osrmModeConfig = ServerConfig.getRoutingEngineConfigForMode(routingMode, 'osrmRouting'); + if (osrmModeConfig && osrmModeConfig.enabled === true && osrmModeConfig.autoStart !== true) { // Assume that an external OSRM is always available // TODO Do a "ping" check on the service to be sure return true; } - const port = osrmServerPrefs.modes[routingMode].port; + const port = osrmModeConfig.port; const serviceName = getServiceName(routingMode, port); @@ -43,8 +41,8 @@ const routingModeIsAvailable = async function (routingMode: RoutingMode): Promis const availableRoutingModes = async function (): Promise { const availableRoutingModes: RoutingMode[] = []; - const osrmServerPrefs = Preferences.get('osrmRouting', {}); - for (const routingMode in osrmServerPrefs.modes) { + const osrmServerModes = ServerConfig.getAllModesForEngine('osrmRouting'); + for (const routingMode of osrmServerModes) { if (!routingModes.includes(routingMode as RoutingMode)) { console.log(`Mode ${routingMode} in osrmRouting preferences is not a routing mode, ignoring`); continue; @@ -61,10 +59,16 @@ const availableRoutingModes = async function (): Promise { function errorConfiguringMode(mode: RoutingMode, message: string) { console.log('Error configuring OSRM mode ' + mode + ' ' + message); - const updatedProperties = {}; - updatedProperties['modes'] = {}; - updatedProperties['modes'][mode] = { enabled: false }; - Preferences.updateServerPrefs({ osrmRouting: updatedProperties }); + setProjectConfiguration({ + routing: { + [mode]: { + defaultEngine: 'osrmRouting', + engines: { + osrmRouting: { enabled: false } + } + } + } as any + }); } // FIXME This function should only have to be called by the main server thread, but it is also used by threads, without starting the servers @@ -72,17 +76,17 @@ function errorConfiguringMode(mode: RoutingMode, message: string) { all the OSRM mode described. Will either start the necessary process or attempt to connect to external ones */ const configureAllOsrmServers = async function (startServers = true): Promise { - const osrmServerPrefs = Preferences.get('osrmRouting', {}); - for (const routingModeStr in osrmServerPrefs.modes) { + const osrmModes = ServerConfig.getAllModesForEngine('osrmRouting'); + for (const routingModeStr of osrmModes) { const routingMode = routingModeStr as RoutingMode; - const modeConfig = osrmServerPrefs.modes[routingMode]; + const modeConfig = ServerConfig.getRoutingEngineConfigForMode(routingMode, 'osrmRouting'); // TODO Do we want to accept camelCase equivalent of each routing modes? if (routingModes.includes(routingMode)) { // Only configure mode that are enabled if (modeConfig.enabled === true) { const port = modeConfig.port; // Use the host in the config even for local server starts as it allows to fine-tune the url to contact (for example ipv4 vs ipv6 names) - const host = modeConfig.host !== null ? modeConfig.host : ''; + const host = !_isBlank(modeConfig.host) ? modeConfig.host : ''; if (port === null || port === undefined || port <= 0) { errorConfiguringMode(routingMode, 'Invalid port number'); @@ -128,8 +132,9 @@ function getOsrmRoutedStartArgs(osrmDirectory: string, mode: string, port: strin //TODO set type for parameters instead of any //TODO set type for Promise return (in all the file) const start = function (parameters = {} as any): Promise { + // TODO: Make the mode and port params mandatory const mode = parameters.mode || 'walking'; - const port = parameters.port || Preferences.get(`osrmRouting.${mode}.port`); + const port = parameters.port || ServerConfig.getRoutingEngineConfigForMode(mode, 'osrmRouting').port; const serviceName = getServiceName(mode, port); const tagName = 'osrm'; const osrmDirectoryPath = getOsrmDirectoryPathForMode(mode, parameters.directoryPrefix); @@ -146,8 +151,9 @@ const start = function (parameters = {} as any): Promise { }; const stop = function (parameters): Promise { + // TODO: Make the mode and port params mandatory const mode = parameters.mode || 'walking'; - const port = parameters.port || Preferences.get(`osrmRouting.${mode}.port`); + const port = parameters.port || ServerConfig.getRoutingEngineConfigForMode(mode, 'osrmRouting').port; const serviceName = getServiceName(mode, port); const tagName = 'osrm'; @@ -157,8 +163,9 @@ const stop = function (parameters): Promise { }; const restart = function (parameters): Promise { + // TODO: Make the mode and port params mandatory const mode = parameters.mode || 'walking'; - const port = parameters.port || Preferences.get(`osrmRouting.${mode}.port`); + const port = parameters.port || ServerConfig.getRoutingEngineConfigForMode(mode, 'osrmRouting').port; const serviceName = getServiceName(mode, port); const tagName = 'osrm'; const osrmDirectoryPath = getOsrmDirectoryPathForMode(mode, parameters.directoryPrefix); diff --git a/packages/chaire-lib-backend/src/utils/processManagers/OSRMServicePath.ts b/packages/chaire-lib-backend/src/utils/processManagers/OSRMServicePath.ts index 65e8abbd..126ba056 100644 --- a/packages/chaire-lib-backend/src/utils/processManagers/OSRMServicePath.ts +++ b/packages/chaire-lib-backend/src/utils/processManagers/OSRMServicePath.ts @@ -11,7 +11,8 @@ import { RoutingMode } from 'chaire-lib-common/lib/config/routingModes'; const defaultDirectoryPrefix = process.env.OSRM_DIRECTORY_PREFIX ? process.env.OSRM_DIRECTORY_PREFIX - : Preferences.get('osrmRouting.directoryPrefix', ''); + : // TODO: Migrate this value from preferences to config like the osrm modes. See issue #1140 + Preferences.get('osrmRouting.directoryPrefix', ''); const getDirectoryPrefix = function (directoryPrefix = defaultDirectoryPrefix) { return !_isBlank(directoryPrefix) ? directoryPrefix + '_' : ''; diff --git a/packages/chaire-lib-backend/src/utils/processManagers/__tests__/OSRMProcessManager.test.ts b/packages/chaire-lib-backend/src/utils/processManagers/__tests__/OSRMProcessManager.test.ts index 130a96f9..1de0d37a 100644 --- a/packages/chaire-lib-backend/src/utils/processManagers/__tests__/OSRMProcessManager.test.ts +++ b/packages/chaire-lib-backend/src/utils/processManagers/__tests__/OSRMProcessManager.test.ts @@ -12,11 +12,13 @@ require('child_process').spawn = osrmSpawn; import { directoryManager } from '../../filesystem/directoryManager'; import { fileManager } from '../../filesystem/fileManager'; - +import _cloneDeep from 'lodash/cloneDeep'; import OSRMProcessManager from '../OSRMProcessManager'; import OSRMServicePreparation from '../OSRMServicePreparation'; -import Preferences from 'chaire-lib-common/lib/config/Preferences'; import OSRMService from '../../osrm/OSRMService'; +import config, { setProjectConfiguration } from '../../../config/server.config'; +import { RoutingMode } from 'chaire-lib-common/lib/config/routingModes'; +import ServerConfig from '../../../config/ServerConfig'; // Mock process.kill to make sure we don't kill random processes //const mockKill = jest.fn(); @@ -27,6 +29,9 @@ jest.setTimeout(30000); const existingOsmFilePath = path.normalize(`${__dirname}/../../../../../../tests/files/osm_network_data.osm`); +// Clone the original config to reset it after each test +let originalTestConfig = _cloneDeep(config); + beforeAll(function(done) { directoryManager.emptyDirectory('osrm'); done(); @@ -50,6 +55,7 @@ describe('OSRM Service Manager', function() { osrmSpawn = mockSpawn(verbose); osrmSpawn.setStrategy(null); require('child_process').spawn = osrmSpawn; + setProjectConfiguration(originalTestConfig); }); test('should extract, contract and/or prepare osm file for single mode walking with default config', async function() { @@ -176,9 +182,17 @@ describe('OSRM Service Manager', function() { // TODO is this test title the right one? test('should start only walking and update preferences', async () => { - // Clean up pids file, so that configureAllOsrmServers to not attempt a restart and try to kill - // a random process - fileManager.deleteFile("pids/osrmMode__driving__port7999.pid"); + setProjectConfiguration({ + routing: { + driving: { engines: { osrmRouting: { port: 7999 } } }, + bus_suburb: { engines: { osrmRouting: { port: 7889 } } }, + bus_urban: { engines: { osrmRouting: { port: 7879 } } } + } as any + }); + + // Clean up pids file, so that configureAllOsrmServers to not attempt a restart and try to kill + // a random process + fileManager.deleteFile("pids/osrmMode__driving__port7999.pid"); osrmSpawn.sequence.add(osrmSpawn.simple(0 /* exit code */, '[info] To prepare the data for routing, run:' /* stdout */)); osrmSpawn.sequence.add(osrmSpawn.simple(0 /* exit code */, '[info] finished preprocessing' /* stdout */)); osrmSpawn.sequence.add(osrmSpawn.simple(0 /* exit code */, 'running and waiting for requests' /* stdout */)); @@ -188,13 +202,16 @@ describe('OSRM Service Manager', function() { const prepareResult = await OSRMServicePreparation.prepare(existingOsmFilePath, ['walking']); expect(prepareResult.status).toBe('prepared'); + const osrmModes = ServerConfig.getAllModesForEngine("osrmRouting"); + // Make sure the list of osrms to auto-start is longer than 1 const autoStartOsrm : string[] = []; - const osrmServerPrefsBefore = Preferences.current.osrmRouting; - for (const routingMode in osrmServerPrefsBefore.modes) { - if (osrmServerPrefsBefore.modes[routingMode].autoStart === true) { - autoStartOsrm.push(routingMode); - } + for (const routingModeStr of osrmModes) { + const routingMode = routingModeStr as RoutingMode; + const osrmConfig = ServerConfig.getRoutingEngineConfigForMode(routingMode, 'osrmRouting'); + if (osrmConfig.autoStart === true) { + autoStartOsrm.push(routingMode); + } } await OSRMProcessManager.configureAllOsrmServers(); @@ -204,11 +221,12 @@ describe('OSRM Service Manager', function() { expect(osrmSpawn.calls[4].command).toBe('osrm-routed'); const availableOsrms : string[] = []; - const osrmServerPrefsAfter = Preferences.current.osrmRouting; - for (const routingMode in osrmServerPrefsAfter.modes) { - if (osrmServerPrefsAfter.modes[routingMode].enabled === true) { - availableOsrms.push(routingMode); - } + for (const routingModeStr of osrmModes) { + const routingMode = routingModeStr as RoutingMode; + const osrmConfig = ServerConfig.getRoutingEngineConfigForMode(routingMode, 'osrmRouting'); + if (osrmConfig.enabled === true) { + availableOsrms.push(routingMode); + } } let containsAll = (baseArray, contained) => contained.every(v => baseArray.includes(v)); @@ -219,15 +237,25 @@ describe('OSRM Service Manager', function() { test('should not start servers if start is not request, even if autostart is true', async () => { + setProjectConfiguration({ + routing: { + bus_suburb: { engines: { osrmRouting: { enabled: false } } }, + bus_urban: { engines: { osrmRouting: { enabled: false } } } + } as any + }); + // Clean up pids file, so that configureAllOsrmServers to not attempt a restart and try to kill // a random process - fileManager.deleteFile("pids/osrmMode__driving__port7999.pid"); + fileManager.deleteFile("pids/osrmMode__driving__port7000.pid"); + + const osrmModes = ServerConfig.getAllModesForEngine("osrmRouting"); // Make sure the list of osrms to auto-start is longer than 1 const autoStartOsrm : string[] = []; - const osrmServerPrefsBefore = Preferences.current.osrmRouting; - for (const routingMode in osrmServerPrefsBefore.modes) { - if (osrmServerPrefsBefore.modes[routingMode].autoStart === true) { + for (const routingModeStr of osrmModes) { + const routingMode = routingModeStr as RoutingMode; + const osrmConfig = ServerConfig.getRoutingEngineConfigForMode(routingMode, 'osrmRouting'); + if (osrmConfig.autoStart === true) { autoStartOsrm.push(routingMode); } } @@ -237,9 +265,10 @@ describe('OSRM Service Manager', function() { expect(osrmSpawn.calls.length).toBe(0); const availableOsrms : string[] = []; - const osrmServerPrefsAfter = Preferences.current.osrmRouting; - for (const routingMode in osrmServerPrefsAfter.modes) { - if (osrmServerPrefsAfter.modes[routingMode].enabled === true) { + for (const routingModeStr of osrmModes) { + const routingMode = routingModeStr as RoutingMode; + const osrmConfig = ServerConfig.getRoutingEngineConfigForMode(routingMode, 'osrmRouting'); + if (osrmConfig.enabled === true) { availableOsrms.push(routingMode); } } diff --git a/packages/chaire-lib-common/src/config/__tests__/Preferences.test.ts b/packages/chaire-lib-common/src/config/__tests__/Preferences.test.ts index 049146f1..5abc6f92 100644 --- a/packages/chaire-lib-common/src/config/__tests__/Preferences.test.ts +++ b/packages/chaire-lib-common/src/config/__tests__/Preferences.test.ts @@ -44,11 +44,13 @@ test('Test current match attributes', () => { test('Test initialize PreferencesClass with custom attributes', () => { const preferences = new PreferencesClass({ data: { foo: 'bar' }, osrmRouting: { directoryPrefix: 'newPrefix' } }); + // TODO: Migrate this value from preferences to config like the osrm modes. See issue #1140 expect(preferences.get('osrmRouting.directoryPrefix')).toBe('newPrefix'); expect(preferences.getData('foo')).toBe('bar'); }); test('Test get preferences', () => { + // TODO: Migrate this value from preferences to config like the osrm modes. See issue #1140 expect(Preferences.get('osrmRouting.directoryPrefix')).toBe('test'); }); diff --git a/packages/chaire-lib-common/src/config/defaultPreferences.config.ts b/packages/chaire-lib-common/src/config/defaultPreferences.config.ts index 6336693a..b201e3d1 100644 --- a/packages/chaire-lib-common/src/config/defaultPreferences.config.ts +++ b/packages/chaire-lib-common/src/config/defaultPreferences.config.ts @@ -200,93 +200,10 @@ const defaultPreferences: PreferencesModel = { // Keep empty for none (will only use the mode name): // If empty, directories will be projects/{PROJECT_SHORTNAME}/osrm/{MODE} // If not empty, directories will be projects/{PROJECT_SHORTNAME}/osrm/directoryPrefix_{MODE} + // TODO: Move these params to chaire-lib-backend with server.config. See issue #1140 directoryPrefix: '', maxDistanceFromNearestNetworkNodeMeters: 300, - useContinueStraightForMapMatching: false, // use only if using the forked version of osrm-backend with support for continue-straight in match query - modes: { - driving: { - // set host to null when you want node to start osrm locally with the provided osrmPath and port - // !!! Be careful: use your own server, since you may be blocked on the osrm demo server after too many queries - port: 7000, // set to null when using remote osrm server - osrmPath: null, // set to null when using a remote osrm server - autoStart: true, // autostart osrm server for this mode when lauching transition (client) - enabled: true // usable when routing paths - }, - driving_congestion: { - // set host to null when you want node to start osrm locally with the provided osrmPath and port - // !!! Be careful: use your own server, since you may be blocked on the osrm demo server after too many queries - port: 7500, // set to null when using remote osrm server - osrmPath: null, // set to null when using a remote osrm server - autoStart: false, // autostart osrm server for this mode when lauching transition (client) - enabled: false // usable when routing paths - }, - cycling: { - port: 8000, - osrmPath: null, - autoStart: true, - enabled: true - }, - walking: { - port: 5000, - osrmPath: null, - autoStart: true, - enabled: true - }, - bus_suburb: { - port: 7200, - osrmPath: null, - autoStart: true, - enabled: true - }, - bus_urban: { - port: 7300, - osrmPath: null, - autoStart: true, - enabled: true - }, - bus_congestion: { - port: 7400, - osrmPath: null, - autoStart: false, - enabled: false - }, - rail: { - port: 9000, - osrmPath: null, - autoStart: false, - enabled: false - }, - tram: { - port: 9100, - osrmPath: null, - autoStart: false, - enabled: false - }, - tram_train: { - port: 9200, - osrmPath: null, - autoStart: false, - enabled: false - }, - metro: { - port: 9300, - osrmPath: null, - autoStart: false, - enabled: false - }, - monorail: { - port: 9400, - osrmPath: null, - autoStart: false, - enabled: false - }, - cable_car: { - port: 9500, - osrmPath: null, - autoStart: false, - enabled: false - } - } + useContinueStraightForMapMatching: false // use only if using the forked version of osrm-backend with support for continue-straight in match query }, colorPicker: { colors: [ diff --git a/packages/chaire-lib-common/src/services/routing/OSRMRoutingService.ts b/packages/chaire-lib-common/src/services/routing/OSRMRoutingService.ts index 84fd879c..9c61b067 100644 --- a/packages/chaire-lib-common/src/services/routing/OSRMRoutingService.ts +++ b/packages/chaire-lib-common/src/services/routing/OSRMRoutingService.ts @@ -88,6 +88,7 @@ export default class OSRMRoutingService extends RoutingService.default { const origin = waypoints[0]; const destination = waypoints[waypoints.length - 1]; + // TODO: Migrate this value from preferences to config like the osrm modes. See issue #1140 const maxDistance = Preferences.get('osrmRouting.maxDistanceFromNearestNetworkNodeMeters'); if (origin.distance > maxDistance || destination.distance > maxDistance) { @@ -190,6 +191,7 @@ export default class OSRMRoutingService extends RoutingService.default { annotations: _isBlank(params.annotations) ? true : params.annotations, // gaps : 'ignore', continue_straight: + // TODO: Migrate this value from preferences to config like the osrm modes. See issue #1140 Preferences.current.osrmRouting.useContinueStraightForMapMatching === true ? true : undefined // see comment in chaire-lib-common/lib/config/defaultPreferences.config.js }); return this.processRoutingResult(routingResult); diff --git a/tests/config_test.js b/tests/config_test.js index 21f882e3..bffb4631 100644 --- a/tests/config_test.js +++ b/tests/config_test.js @@ -44,86 +44,87 @@ module.exports = { defaultPreferences: { defaultSection: "test", + // Change to the new osrm config. See issue #1137 osrmRouting: { directoryPrefix: 'test', modes: { driving: { - // set host to null when you want node to start osrm locally with the provided osrmPath and port + // set host to null when you want node to start osrm locally with the provided port // !!! Be careful: use your own server, since you may be blocked on the osrm demo server after too many queries port : 7999, // set to null when using remote osrm server - osrmPath : null, // set to null when using a remote osrm server + host : null, // set to null when using a remote osrm server autoStart: true, enabled : true }, cycling: { port : 8999, - osrmPath : null, + host : null, autoStart: true, enabled : false }, walking: { port : 5999, - osrmPath : null, + host : null, autoStart: true, enabled : false }, driving_congestion: { port : 8899, - osrmPath : null, + host : null, autoStart: true, enabled : false }, bus_suburb: { port : 7889, - osrmPath : null, + host : null, autoStart: true, enabled : true }, bus_urban: { port : 7879, - osrmPath : null, + host : null, autoStart: true, enabled : true }, bus_congestion: { port : 7869, - osrmPath : null, + host : null, autoStart: true, enabled : false }, rail: { port : 9999, - osrmPath : null, + host : null, autoStart: false, enabled : false }, tram: { port : 9199, - osrmPath : null, + host : null, autoStart: false, enabled : false }, tram_train: { port : 9299, - osrmPath : null, + host : null, autoStart: false, enabled : false }, metro: { port : 9399, - osrmPath : null, + host : null, autoStart: false, enabled : false }, monorail: { port : 9499, - osrmPath : null, + host : null, autoStart: false, enabled : false }, cable_car: { port : 9599, - osrmPath : null, + host : null, autoStart: false, enabled : false }