-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreittiopas-hsl.js
88 lines (77 loc) · 2.26 KB
/
reittiopas-hsl.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
.pragma library
var _types = [];
_types[1] = busData;
_types[2] = tramData;
_types[3] = busData;
_types[4] = busData;
_types[5] = busData;
_types[6] = metroData;
_types[7] = ferryData;
_types[8] = busData;
_types[12] = trainData;
_types[21] = busData;
_types[22] = busData;
_types[23] = busData;
_types[24] = busData;
_types[25] = busData;
// takes in a single leg and extaxts start/end times of that + type
function decode( legData ) {
var leg = {};
var firstLeg = legData.locs[0];
var lastLeg = legData.locs[ legData.locs.length -1 ];
leg.first = {};
leg.first.location = firstLeg.coord;
leg.first.name = firstLeg.name ? firstLeg.name : '';
leg.first.time = _parseTime( firstLeg.arrTime );
leg.last = {};
leg.last.location = lastLeg.coord;
leg.last.name = lastLeg.name ? lastLeg.name : '';
leg.last.time = _parseTime( lastLeg.depTime );
leg.type = decode_code( legData.type, legData.code ).type
leg.code = decode_code( legData.type, legData.code ).code;
return leg;
}
function decode_code (type, code) {
if( type == 'walk' ) return { code: '', type : 'walk' };
return _types[type](code);
}
function busData (code) {
var line = code.slice(1,5);
line = _removeZeros(line);
return { type: 'bus', code: line };
}
function trainData(code) {
var code = code[4];
return { type: 'train', code: code}
}
function tramData(code) {
var code = code.slice(3,5).trim();
return { type: 'tram', code: code}
}
function metroData(code){
if( code[6] == 2 ) {
code = 'Ruoholahti';
} else {
code = code[4] == 'M' ? 'Mellunmäki': 'Vuosaari';
}
return { type: 'metro', code: code}
}
function ferryData(code) {
return { type: 'ferry', code: ''}
}
function _removeZeros( line ) {
while( line[0] == '0' ) {
line = line.slice(1);
}
return line;
}
function _parseTime(timeString){
// HSL returns dates as yyyymmddhhmm -string
var t = timeString;
return new Date( t.slice(0,4) , // yyyy
parseInt( t.slice(4,6) ) - 1 , // mm, starst from 0
t.slice(6,8) , // dd
t.slice(8,10) , // hh
t.slice(10,12) , // mm
00, 00 ); // secs, ms:
}