-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim-events.js
137 lines (118 loc) · 3.04 KB
/
sim-events.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const util = require('./util.js')
const open = () => {
// type: 'door_open'
return {
'type': 'door_open'
}
}
const close = () => {
// type: 'door_close'
return {
'type': 'door_close'
}
}
const routeInfo = (route, stops) => {
// type: 'route_info'
// coordinates: [<coordinates>],
// stops: [<stops>],
// distance: <number>
let distance = 0
for (let i = 0; i < route.length - 1; i++) {
distance += util.computeDistanceKm(route[i], route[i + 1])
}
return {
'type': 'route_info',
'coordinates': route,
'stops': stops,
'distance': distance
}
}
const start = (route, stops) => {
// type: 'trip_start'
// from_coordinates: [<lng>, <lat>],
// to_coordinates: [<lng>, <lat>],
// from_stop: <stop>,
// to_stop: <stop>,
// distance: <number>
let distance = 0
for (let i = 0; i < route.length - 1; i++) {
distance += util.computeDistanceKm(route[i], route[i + 1])
}
const fromCoord = (route[0].coordinates || route[0])
const toCoord = (route[route.length - 1].coordinates || route[route.length - 1])
let event = {
'type': 'trip_start',
'from_coordinates': fromCoord,
'to_coordinates': toCoord,
'distance': distance
}
if (stops) {
const fromStop = stops.filter(stop => {
return (stop.coordinates || stop)[0] === fromCoord[0] &&
(stop.coordinates || stop)[1] === fromCoord[1]
})
const toStop = stops.filter(stop => {
return (stop.coordinates || stop)[0] === toCoord[0] &&
(stop.coordinates || stop)[1] === toCoord[1]
})
event['from_stop'] = fromStop[0]
event['to_stop'] = toStop[0]
}
return event
}
const end = (route, stops) => {
// type: 'trip_end'
// from_coordinates: [<lng>, <lat>],
// to_coordinates: [<lng>, <lat>],
// from_stop: <stop>,
// to_stop: <stop>,
// distance: <number>
let event = start(route, stops)
event.type = 'trip_end'
return event
}
const position = (currentposition, route) => {
// type: 'geo_position'
// coordinates: [<lng>, <lat>],
// distance_travelled: <number>,
// distance_remaining: <number>
let current = (currentposition.coordinates || currentposition)
let travelled = -1
let remaining = -1
const lng = current[0]
const lat = current[1]
if (route && route.length) {
travelled = 0
remaining = 0
let dest = false
for (let i = 0; i < route.length - 1; i++) {
let r = (route[i].coordinates || route[i])
if (r[0] === lng && r[1] === lat) {
dest = true
}
if (dest) {
remaining += util.computeDistanceKm(route[i], route[i + 1])
} else {
travelled += util.computeDistanceKm(route[i], route[i + 1])
}
}
}
let pos = {
'type': 'geo_position',
'coordinates': current,
'distance_travelled': travelled,
'distance_remaining': remaining
}
if (currentposition.properties) {
pos.properties = currentposition.properties
}
return pos
}
module.exports = {
tripStart: start,
tripEnd: end,
doorOpen: open,
doorClose: close,
geoPosition: position,
routeInfo: routeInfo
}