katja's git: oeffisearch

fast and simple tripplanner

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
import { createClient as createVendoClient } from 'db-vendo-client';
import { createClient as createHafasClient } from 'hafas-client';

import { profile as dbNavProfile       } from 'db-vendo-client/p/dbnav/index.js';
import { profile as bvgProfile         } from 'hafas-client/p/bvg/index.js';
import { profile as nahshProfile       } from 'hafas-client/p/nahsh/index.js';
import { profile as rmvProfile         } from 'hafas-client/p/rmv/index.js';
import { profile as oebbProfile        } from 'hafas-client/p/oebb/index.js';
import { profile as rejseplanenProfile } from 'hafas-client/p/rejseplanen/index.js'

const clients  = {};

export let   client;
export const profiles = {
	db:          { name: "DB",          backend: 'vendo', profile: dbNavProfile },
	bvg:         { name: "BVG",         backend: 'hafas', profile: bvgProfile },
	nahsh:       { name: "NAH.SH",      backend: 'hafas', profile: nahshProfile },
	rmv:         { name: "RMV",         backend: 'hafas', profile: rmvProfile },
	oebb:        { name: "ÖBB",         backend: 'hafas', profile: oebbProfile },
	rejseplanen: { name: "Rejseplanen", backend: 'hafas', profile: rejseplanenProfile },
}

export const getDefaultProfile = () => 'db';
export const getHafasClient = async profileName => {
	if (!profiles[profileName]) throw "Unknown profile name: " + profileName;

	if (!clients[profileName]) {
		if (profiles[profileName].backend === 'vendo') {
			clients[profileName] = createVendoClient(profiles[profileName].profile, APP_NAME, {enrichStations: false});
			if (isDevServer) console.info('initialized vendo client with profile ' + profileName);
		}

		if (profiles[profileName].backend === 'hafas') {
			clients[profileName] = createHafasClient(profiles[profileName].profile, APP_NAME);
			if (isDevServer) console.info('initialized hafas client with profile ' + profileName);
		}
	}

	return clients[profileName];
}

export const initHafasClient = async profileName => {
	client = await getHafasClient(profileName);
};