mobility-toolbox-js 2.0.0-beta.32 → 2.0.0-beta.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/RealtimeAPI.d.ts +268 -0
- package/api/RealtimeAPI.d.ts.map +1 -0
- package/api/RoutingAPI.d.ts +33 -0
- package/api/RoutingAPI.d.ts.map +1 -0
- package/api/StopsAPI.d.ts +36 -0
- package/api/StopsAPI.d.ts.map +1 -0
- package/api/index.d.ts +5 -0
- package/api/index.d.ts.map +1 -0
- package/api/typedefs.d.ts +105 -0
- package/api/typedefs.d.ts.map +1 -0
- package/common/api/HttpAPI.d.ts +31 -0
- package/common/api/HttpAPI.d.ts.map +1 -0
- package/common/api/WebSocketAPI.d.ts +95 -0
- package/common/api/WebSocketAPI.d.ts.map +1 -0
- package/common/controls/Control.d.ts +74 -0
- package/common/controls/Control.d.ts.map +1 -0
- package/common/{index.js → index.d.ts} +1 -0
- package/common/index.d.ts.map +1 -0
- package/common/layers/Layer.d.ts +80 -0
- package/common/layers/Layer.d.ts.map +1 -0
- package/common/mixins/CopyrightMixin.d.ts +22 -0
- package/common/mixins/CopyrightMixin.d.ts.map +1 -0
- package/common/mixins/MapboxLayerMixin.d.ts +27 -0
- package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
- package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
- package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
- package/common/mixins/StopFinderMixin.d.ts +40 -0
- package/common/mixins/StopFinderMixin.d.ts.map +1 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
- package/common/styles/{index.js → index.d.ts} +1 -0
- package/common/styles/index.d.ts.map +1 -0
- package/common/styles/realtimeDefaultStyle.d.ts +15 -0
- package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
- package/common/styles/realtimeDelayStyle.d.ts +11 -0
- package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
- package/common/styles/realtimeSimpleStyle.d.ts +3 -0
- package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
- package/common/typedefs.d.ts +50 -0
- package/common/typedefs.d.ts.map +1 -0
- package/common/utils/cleanStopTime.d.ts +7 -0
- package/common/utils/cleanStopTime.d.ts.map +1 -0
- package/common/utils/compareDepartures.d.ts +9 -0
- package/common/utils/compareDepartures.d.ts.map +1 -0
- package/common/utils/createCanvas.d.ts +9 -0
- package/common/utils/createCanvas.d.ts.map +1 -0
- package/common/utils/createTrackerFilters.d.ts +12 -0
- package/common/utils/createTrackerFilters.d.ts.map +1 -0
- package/common/utils/getLayersAsFlatArray.d.ts +3 -0
- package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
- package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
- package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
- package/common/utils/getMapboxRender.d.ts +5 -0
- package/common/utils/getMapboxRender.d.ts.map +1 -0
- package/common/utils/getMaplibreRender.d.ts +5 -0
- package/common/utils/getMaplibreRender.d.ts.map +1 -0
- package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
- package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
- package/common/utils/getUrlWithParams.d.ts +9 -0
- package/common/utils/getUrlWithParams.d.ts.map +1 -0
- package/common/utils/getVehiclePosition.d.ts +14 -0
- package/common/utils/getVehiclePosition.d.ts.map +1 -0
- package/common/utils/{index.js → index.d.ts} +3 -0
- package/common/utils/index.d.ts.map +1 -0
- package/common/utils/removeDuplicate.d.ts +10 -0
- package/common/utils/removeDuplicate.d.ts.map +1 -0
- package/common/utils/renderTrajectories.d.ts +20 -0
- package/common/utils/renderTrajectories.d.ts.map +1 -0
- package/common/utils/sortByDelay.d.ts +3 -0
- package/common/utils/sortByDelay.d.ts.map +1 -0
- package/common/utils/timeUtils.d.ts +5 -0
- package/common/utils/timeUtils.d.ts.map +1 -0
- package/common/utils/trackerConfig.d.ts +24 -0
- package/common/utils/trackerConfig.d.ts.map +1 -0
- package/iife.d.ts +3 -0
- package/iife.d.ts.map +1 -0
- package/index.d.ts +6 -0
- package/index.d.ts.map +1 -0
- package/mapbox/controls/CopyrightControl.d.ts +32 -0
- package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
- package/mapbox/controls/{index.js → index.d.ts} +1 -0
- package/mapbox/controls/index.d.ts.map +1 -0
- package/mapbox/{index.js → index.d.ts} +1 -0
- package/mapbox/index.d.ts.map +1 -0
- package/mapbox/layers/Layer.d.ts +47 -0
- package/mapbox/layers/Layer.d.ts.map +1 -0
- package/mapbox/layers/RealtimeLayer.d.ts +118 -0
- package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
- package/mapbox/layers/{index.js → index.d.ts} +1 -0
- package/mapbox/layers/index.d.ts.map +1 -0
- package/mapbox/utils.d.ts +8 -0
- package/mapbox/utils.d.ts.map +1 -0
- package/mbt.js +2049 -2106
- package/mbt.js.map +3 -3
- package/mbt.min.js +18 -18
- package/mbt.min.js.map +3 -3
- package/ol/controls/CopyrightControl.d.ts +31 -0
- package/ol/controls/CopyrightControl.d.ts.map +1 -0
- package/ol/controls/RoutingControl.d.ts +180 -0
- package/ol/controls/RoutingControl.d.ts.map +1 -0
- package/ol/controls/StopFinderControl.d.ts +32 -0
- package/ol/controls/StopFinderControl.d.ts.map +1 -0
- package/ol/controls/{index.js → index.d.ts} +1 -0
- package/ol/controls/index.d.ts.map +1 -0
- package/ol/{index.js → index.d.ts} +1 -0
- package/ol/index.d.ts.map +1 -0
- package/ol/layers/Layer.d.ts +49 -0
- package/ol/layers/Layer.d.ts.map +1 -0
- package/ol/layers/MapboxLayer.d.ts +42 -0
- package/ol/layers/MapboxLayer.d.ts.map +1 -0
- package/ol/layers/MapboxStyleLayer.d.ts +146 -0
- package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
- package/ol/layers/MaplibreLayer.d.ts +27 -0
- package/ol/layers/MaplibreLayer.d.ts.map +1 -0
- package/ol/layers/RealtimeLayer.d.ts +119 -0
- package/ol/layers/RealtimeLayer.d.ts.map +1 -0
- package/ol/layers/RoutingLayer.d.ts +24 -0
- package/ol/layers/RoutingLayer.d.ts.map +1 -0
- package/ol/layers/VectorLayer.d.ts +23 -0
- package/ol/layers/VectorLayer.d.ts.map +1 -0
- package/ol/layers/WMSLayer.d.ts +38 -0
- package/ol/layers/WMSLayer.d.ts.map +1 -0
- package/ol/layers/{index.js → index.d.ts} +1 -0
- package/ol/layers/index.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
- package/ol/styles/{index.js → index.d.ts} +1 -0
- package/ol/styles/index.d.ts.map +1 -0
- package/package.json +14 -36
- package/setupTests.d.ts +2 -0
- package/setupTests.d.ts.map +1 -0
- package/types/index.d.ts +7 -6
- package/types/routing.d.ts +25 -25
- package/types/stops.d.ts +19 -19
- package/api/RealtimeAPI.js +0 -357
- package/api/RealtimeAPI.test.js +0 -67
- package/api/RoutingAPI.js +0 -15
- package/api/RoutingAPI.test.js +0 -25
- package/api/StopsAPI.js +0 -12
- package/api/StopsAPI.test.js +0 -22
- package/api/index.js +0 -3
- package/api/typedefs.js +0 -0
- package/common/api/HttpAPI.js +0 -32
- package/common/api/HttpAPI.test.js +0 -50
- package/common/api/WebSocketAPI.js +0 -175
- package/common/api/WebSocketAPI.test.js +0 -311
- package/common/controls/Control.js +0 -81
- package/common/controls/Control.test.js +0 -87
- package/common/layers/Layer.js +0 -140
- package/common/layers/Layer.test.js +0 -134
- package/common/mixins/CopyrightMixin.js +0 -24
- package/common/mixins/RealtimeLayerMixin.js +0 -436
- package/common/mixins/StopFinderMixin.js +0 -110
- package/common/mixins/UserInteractionsLayerMixin.js +0 -124
- package/common/mixins/UserInteractionsLayerMixin.test.js +0 -199
- package/common/styles/realtimeDefaultStyle.js +0 -202
- package/common/styles/realtimeDelayStyle.js +0 -8
- package/common/styles/realtimeSimpleStyle.js +0 -18
- package/common/typedefs.js +0 -0
- package/common/utils/cleanStopTime.js +0 -19
- package/common/utils/compareDepartures.js +0 -25
- package/common/utils/createCanvas.js +0 -17
- package/common/utils/createTrackerFilters.js +0 -56
- package/common/utils/createTrackerFilters.test.js +0 -79
- package/common/utils/getLayersAsFlatArray.js +0 -14
- package/common/utils/getMapboxMapCopyrights.js +0 -19
- package/common/utils/getMapboxMapCopyrights.test.js +0 -40
- package/common/utils/getRealtimeModeSuffix.js +0 -2
- package/common/utils/getUrlWithParams.js +0 -11
- package/common/utils/getVehiclePosition.js +0 -42
- package/common/utils/removeDuplicate.js +0 -8
- package/common/utils/removeDuplicate.test.js +0 -19
- package/common/utils/renderTrajectories.js +0 -86
- package/common/utils/sortByDelay.js +0 -18
- package/common/utils/timeUtils.js +0 -20
- package/common/utils/timeUtils.test.js +0 -10
- package/common/utils/trackerConfig.js +0 -129
- package/common/utils/trackerConfig.test.js +0 -23
- package/index.js +0 -8
- package/mapbox/controls/CopyrightControl.js +0 -29
- package/mapbox/layers/Layer.js +0 -56
- package/mapbox/layers/Layer.test.js +0 -182
- package/mapbox/layers/RealtimeLayer.js +0 -185
- package/mapbox/layers/RealtimeLayer.test.js +0 -10
- package/mapbox/utils.js +0 -32
- package/ol/controls/CopyrightControl.js +0 -42
- package/ol/controls/CopyrightControl.test.js +0 -165
- package/ol/controls/RoutingControl.js +0 -388
- package/ol/controls/RoutingControl.test.js +0 -146
- package/ol/controls/StopFinderControl.js +0 -10
- package/ol/controls/StopFinderControl.test.js +0 -48
- package/ol/layers/Layer.js +0 -83
- package/ol/layers/Layer.test.js +0 -175
- package/ol/layers/MapboxLayer.js +0 -204
- package/ol/layers/MapboxLayer.test.js +0 -160
- package/ol/layers/MapboxStyleLayer.js +0 -187
- package/ol/layers/MapboxStyleLayer.test.js +0 -226
- package/ol/layers/MaplibreLayer.js +0 -140
- package/ol/layers/RealtimeLayer.js +0 -187
- package/ol/layers/RealtimeLayer.test.js +0 -67
- package/ol/layers/RoutingLayer.js +0 -61
- package/ol/layers/RoutingLayer.test.js +0 -39
- package/ol/layers/VectorLayer.js +0 -22
- package/ol/layers/VectorLayer.test.js +0 -76
- package/ol/layers/WMSLayer.js +0 -38
- package/ol/layers/WMSLayer.test.js +0 -57
- package/ol/styles/fullTrajectoryDelayStyle.js +0 -31
- package/ol/styles/fullTrajectoryStyle.js +0 -42
package/api/RealtimeAPI.js
DELETED
|
@@ -1,357 +0,0 @@
|
|
|
1
|
-
import WebSocketAPI from "../common/api/WebSocketAPI";
|
|
2
|
-
import cleanStopTime from "../common/utils/cleanStopTime";
|
|
3
|
-
import getModeSuffix from "../common/utils/getRealtimeModeSuffix";
|
|
4
|
-
import compareDepartures from "../common/utils/compareDepartures";
|
|
5
|
-
export const RealtimeModes = {
|
|
6
|
-
RAW: "raw",
|
|
7
|
-
TOPOGRAPHIC: "topographic",
|
|
8
|
-
SCHEMATIC: "schematic"
|
|
9
|
-
};
|
|
10
|
-
class RealtimeAPI {
|
|
11
|
-
constructor(options = {}) {
|
|
12
|
-
this.defineProperties(options);
|
|
13
|
-
this.subscribedStationUic = null;
|
|
14
|
-
this.departureUpdateTimeout = null;
|
|
15
|
-
this.maxDepartureAge = 30;
|
|
16
|
-
this.extraGeoms = {};
|
|
17
|
-
this.prefix = options.prefix || "";
|
|
18
|
-
this.onOpen = this.onOpen.bind(this);
|
|
19
|
-
}
|
|
20
|
-
defineProperties(options) {
|
|
21
|
-
let opt = options;
|
|
22
|
-
if (typeof options === "string") {
|
|
23
|
-
opt = { url: options };
|
|
24
|
-
}
|
|
25
|
-
const { apiKey } = opt;
|
|
26
|
-
let { url, projection, bbox, buffer = [100, 100] } = opt;
|
|
27
|
-
const wsApi = new WebSocketAPI();
|
|
28
|
-
if (apiKey) {
|
|
29
|
-
url = `${url || "wss://api.geops.io/tracker-ws/v1/"}?key=${apiKey}`;
|
|
30
|
-
}
|
|
31
|
-
Object.defineProperties(this, {
|
|
32
|
-
url: {
|
|
33
|
-
get: () => url,
|
|
34
|
-
set: (newUrl) => {
|
|
35
|
-
url = newUrl;
|
|
36
|
-
this.open();
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
projection: {
|
|
40
|
-
get: () => projection,
|
|
41
|
-
set: (newProjection) => {
|
|
42
|
-
if (newProjection !== projection) {
|
|
43
|
-
projection = newProjection;
|
|
44
|
-
if (this.wsApi) {
|
|
45
|
-
this.wsApi.send(`PROJECTION ${projection}`);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
bbox: {
|
|
51
|
-
get: () => bbox,
|
|
52
|
-
set: (newBbox) => {
|
|
53
|
-
if (JSON.stringify(newBbox) !== JSON.stringify(bbox)) {
|
|
54
|
-
bbox = newBbox;
|
|
55
|
-
if (this.wsApi) {
|
|
56
|
-
this.wsApi.send(`BBOX ${bbox.join(" ")}`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
buffer: {
|
|
62
|
-
get: () => buffer,
|
|
63
|
-
set: (newBuffer) => {
|
|
64
|
-
if (JSON.stringify(newBuffer) !== JSON.stringify(buffer)) {
|
|
65
|
-
buffer = newBuffer;
|
|
66
|
-
if (this.wsApi) {
|
|
67
|
-
this.wsApi.send(`BUFFER ${buffer.join(" ")}`);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
wsApi: {
|
|
73
|
-
value: wsApi,
|
|
74
|
-
writable: true
|
|
75
|
-
},
|
|
76
|
-
pingIntervalMs: {
|
|
77
|
-
value: options.pingIntervalMs || 1e4,
|
|
78
|
-
writable: true
|
|
79
|
-
},
|
|
80
|
-
reconnectTimeoutMs: {
|
|
81
|
-
value: options.pingIntervalMs || 100,
|
|
82
|
-
writable: true
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
open() {
|
|
87
|
-
this.close();
|
|
88
|
-
this.wsApi.connect(this.url, this.onOpen);
|
|
89
|
-
this.wsApi.websocket.onclose = () => {
|
|
90
|
-
this.onClose();
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
close() {
|
|
94
|
-
this.wsApi.close();
|
|
95
|
-
}
|
|
96
|
-
reset() {
|
|
97
|
-
this.wsApi.send("RESET");
|
|
98
|
-
}
|
|
99
|
-
onOpen() {
|
|
100
|
-
if (this.projection) {
|
|
101
|
-
this.wsApi.send(`PROJECTION ${this.projection}`);
|
|
102
|
-
}
|
|
103
|
-
if (this.bbox) {
|
|
104
|
-
this.wsApi.send(`BBOX ${this.bbox.join(" ")}`);
|
|
105
|
-
}
|
|
106
|
-
if (this.buffer) {
|
|
107
|
-
this.wsApi.send(`BUFFER ${this.buffer.join(" ")}`);
|
|
108
|
-
}
|
|
109
|
-
if (this.pingIntervalMs) {
|
|
110
|
-
window.clearInterval(this.pingInterval);
|
|
111
|
-
this.pingInterval = setInterval(() => {
|
|
112
|
-
this.wsApi.send("PING");
|
|
113
|
-
}, this.pingIntervalMs);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
onClose() {
|
|
117
|
-
window.clearTimeout(this.pingInterval);
|
|
118
|
-
window.clearTimeout(this.reconnectTimeout);
|
|
119
|
-
if (this.reconnectTimeoutMs) {
|
|
120
|
-
this.reconnectTimeout = window.setTimeout(() => this.open(), this.reconnectTimeoutMs);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
subscribe(channel, onSuccess, onError, quiet = false) {
|
|
124
|
-
this.wsApi.subscribe({ channel }, onSuccess, onError, quiet);
|
|
125
|
-
}
|
|
126
|
-
unsubscribe(channel, suffix, cb) {
|
|
127
|
-
this.wsApi.unsubscribe(`${channel}${getModeSuffix(RealtimeModes.SCHEMATIC, RealtimeModes)}${suffix}`, cb);
|
|
128
|
-
this.wsApi.unsubscribe(`${channel}${getModeSuffix(RealtimeModes.TOPOGRAPHIC, RealtimeModes)}${suffix || ""}`, cb);
|
|
129
|
-
}
|
|
130
|
-
filterDepartures(depObject, sortByMinArrivalTime = false) {
|
|
131
|
-
const departures = Object.keys(depObject).map((k) => depObject[k]);
|
|
132
|
-
departures.sort((a, b) => compareDepartures(a, b, sortByMinArrivalTime));
|
|
133
|
-
let future = new Date();
|
|
134
|
-
future.setMinutes(future.getMinutes() + this.maxDepartureAge);
|
|
135
|
-
future = future.getTime();
|
|
136
|
-
let past = new Date();
|
|
137
|
-
past.setMinutes(past.getMinutes() - this.maxDepartureAge);
|
|
138
|
-
past = past.getTime();
|
|
139
|
-
const departureArray = [];
|
|
140
|
-
const platformsBoarding = [];
|
|
141
|
-
let previousDeparture = null;
|
|
142
|
-
for (let i = departures.length - 1; i >= 0; i -= 1) {
|
|
143
|
-
const d = departures[i];
|
|
144
|
-
const t = new Date(d.time).getTime();
|
|
145
|
-
if (t > past && t < future) {
|
|
146
|
-
if (d.state === "BOARDING") {
|
|
147
|
-
if (platformsBoarding.indexOf(d.platform) === -1) {
|
|
148
|
-
platformsBoarding.push(d.platform);
|
|
149
|
-
} else {
|
|
150
|
-
d.state = "HIDDEN";
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (previousDeparture && d.to[0] === previousDeparture.to[0] && Math.abs(t - previousDeparture.time) < 1e3 && d.line.name === previousDeparture.line.name) {
|
|
154
|
-
d.state = "HIDDEN";
|
|
155
|
-
}
|
|
156
|
-
if (/(STOP_CANCELLED|JOURNEY_CANCELLED)/.test(d.state)) {
|
|
157
|
-
d.cancelled = true;
|
|
158
|
-
}
|
|
159
|
-
previousDeparture = d;
|
|
160
|
-
previousDeparture.time = t;
|
|
161
|
-
departureArray.unshift(d);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return departureArray;
|
|
165
|
-
}
|
|
166
|
-
subscribeDepartures(stationId, sortByMinArrivalTime, onMessage) {
|
|
167
|
-
window.clearTimeout(this.departureUpdateTimeout);
|
|
168
|
-
this.unsubscribeDepartures();
|
|
169
|
-
this.subscribedStationUic = stationId;
|
|
170
|
-
const channel = stationId ? `timetable_${stationId}` : null;
|
|
171
|
-
const departureObject = {};
|
|
172
|
-
this.subscribe(channel, (data) => {
|
|
173
|
-
if (data.source === channel) {
|
|
174
|
-
const content = data.content || {};
|
|
175
|
-
const tDiff = new Date(content.timestamp).getTime() - Date.now();
|
|
176
|
-
content.timediff = tDiff;
|
|
177
|
-
departureObject[content.call_id] = content;
|
|
178
|
-
window.clearTimeout(this.departureUpdateTimeout);
|
|
179
|
-
this.departureUpdateTimeout = window.setTimeout(() => {
|
|
180
|
-
const departures = this.filterDepartures(departureObject, sortByMinArrivalTime || false);
|
|
181
|
-
onMessage(departures);
|
|
182
|
-
}, 100);
|
|
183
|
-
}
|
|
184
|
-
}, () => {
|
|
185
|
-
onMessage([]);
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
unsubscribeDepartures(cb) {
|
|
189
|
-
if (this.subscribedStationUic) {
|
|
190
|
-
this.unsubscribe(`timetable_${this.subscribedStationUic}`, "", cb);
|
|
191
|
-
this.subscribedStationUic = null;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
subscribeDisruptions(onMessage) {
|
|
195
|
-
this.subscribe(`${this.prefix}newsticker`, (data) => {
|
|
196
|
-
onMessage(data.content);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
unsubscribeDisruptions(cb) {
|
|
200
|
-
this.unsubscribe(`${this.prefix}newsticker`, "", cb);
|
|
201
|
-
}
|
|
202
|
-
getStation(uic, mode) {
|
|
203
|
-
const params = {
|
|
204
|
-
channel: `station${getModeSuffix(mode, RealtimeModes)}`,
|
|
205
|
-
args: uic
|
|
206
|
-
};
|
|
207
|
-
return new Promise((resolve, reject) => {
|
|
208
|
-
this.wsApi.get(params, (data) => {
|
|
209
|
-
if (data.content) {
|
|
210
|
-
resolve(data.content);
|
|
211
|
-
} else {
|
|
212
|
-
reject();
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
getStations(mode) {
|
|
218
|
-
const stations = [];
|
|
219
|
-
const params = {
|
|
220
|
-
channel: `station${getModeSuffix(mode, RealtimeModes)}`
|
|
221
|
-
};
|
|
222
|
-
window.clearTimeout(this.stationUpdateTimeout);
|
|
223
|
-
return new Promise((resolve, reject) => {
|
|
224
|
-
this.wsApi.get(params, (data) => {
|
|
225
|
-
if (data.content) {
|
|
226
|
-
stations.push(data.content);
|
|
227
|
-
window.clearTimeout(this.stationUpdateTimeout);
|
|
228
|
-
this.stationUpdateTimeout = window.setTimeout(() => {
|
|
229
|
-
resolve(stations);
|
|
230
|
-
}, 50);
|
|
231
|
-
} else {
|
|
232
|
-
reject(data.content);
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
subscribeStations(mode, onMessage) {
|
|
238
|
-
this.unsubscribeStations();
|
|
239
|
-
this.subscribe(`station${getModeSuffix(mode, RealtimeModes)}`, (data) => {
|
|
240
|
-
if (data.content) {
|
|
241
|
-
onMessage(data.content);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
unsubscribeStations(cb) {
|
|
246
|
-
window.clearTimeout(this.stationUpdateTimeout);
|
|
247
|
-
this.unsubscribe("station", "", cb);
|
|
248
|
-
}
|
|
249
|
-
subscribeExtraGeoms(onMessage) {
|
|
250
|
-
this.subscribe("extra_geoms", (data) => {
|
|
251
|
-
const extraGeom = data.content;
|
|
252
|
-
if (extraGeom) {
|
|
253
|
-
const { ref } = extraGeom.properties;
|
|
254
|
-
if (extraGeom.type === "Feature") {
|
|
255
|
-
this.extraGeoms[ref] = extraGeom;
|
|
256
|
-
} else {
|
|
257
|
-
delete this.extraGeoms[ref];
|
|
258
|
-
}
|
|
259
|
-
onMessage(Object.keys(this.extraGeoms).map((key) => this.extraGeoms[key]));
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
unsubscribeExtraGeoms(cb) {
|
|
264
|
-
this.unsubscribe("extra_geoms", "", cb);
|
|
265
|
-
}
|
|
266
|
-
subscribeTrajectory(mode, onMessage, quiet = false) {
|
|
267
|
-
this.unsubscribeTrajectory(onMessage);
|
|
268
|
-
this.subscribe(`trajectory${getModeSuffix(mode, RealtimeModes)}`, onMessage, null, quiet);
|
|
269
|
-
}
|
|
270
|
-
unsubscribeTrajectory(cb) {
|
|
271
|
-
this.unsubscribe(`trajectory`, "", cb);
|
|
272
|
-
}
|
|
273
|
-
subscribeDeletedVehicles(mode, onMessage, quiet = false) {
|
|
274
|
-
this.unsubscribeDeletedVehicles(onMessage);
|
|
275
|
-
this.subscribe(`deleted_vehicles${getModeSuffix(mode, RealtimeModes)}`, onMessage, null, quiet);
|
|
276
|
-
}
|
|
277
|
-
unsubscribeDeletedVehicles(cb) {
|
|
278
|
-
this.unsubscribe("deleted_vehicles", "", cb);
|
|
279
|
-
}
|
|
280
|
-
getFullTrajectory(id, mode, generalizationLevel) {
|
|
281
|
-
const channel = [`full_trajectory${getModeSuffix(mode, RealtimeModes)}`];
|
|
282
|
-
if (id) {
|
|
283
|
-
channel.push(id);
|
|
284
|
-
}
|
|
285
|
-
if ((!mode || mode === RealtimeModes.TOPOGRAPHIC) && generalizationLevel) {
|
|
286
|
-
channel.push(`gen${generalizationLevel}`);
|
|
287
|
-
}
|
|
288
|
-
const params = {
|
|
289
|
-
channel: channel.join("_")
|
|
290
|
-
};
|
|
291
|
-
return new Promise((resolve) => {
|
|
292
|
-
this.wsApi.get(params, (data) => {
|
|
293
|
-
if (data.content) {
|
|
294
|
-
resolve(data.content);
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
getFullTrajectories(ids, mode, generalizationLevel) {
|
|
300
|
-
const promises = ids.map((id) => this.getFullTrajectory(id, mode, generalizationLevel));
|
|
301
|
-
return Promise.all(promises);
|
|
302
|
-
}
|
|
303
|
-
subscribeFullTrajectory(id, mode) {
|
|
304
|
-
this.unsubscribeFullTrajectory(id);
|
|
305
|
-
this.subscribe(`full_trajectory${getModeSuffix(mode, RealtimeModes)}_${id}`, (data) => {
|
|
306
|
-
console.log("subscribe full_trajectory", data);
|
|
307
|
-
}, (err) => {
|
|
308
|
-
console.log("subscribe full_trajectory error", err);
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
unsubscribeFullTrajectory(id, cb) {
|
|
312
|
-
this.unsubscribe("full_trajectory", `_${id}`, cb);
|
|
313
|
-
}
|
|
314
|
-
getStopSequence(id) {
|
|
315
|
-
const params = {
|
|
316
|
-
channel: `stopsequence_${id}`
|
|
317
|
-
};
|
|
318
|
-
return new Promise((resolve, reject) => {
|
|
319
|
-
this.wsApi.get(params, (data) => {
|
|
320
|
-
if (data.content && data.content.length) {
|
|
321
|
-
const content = data.content.map((stopSequence) => cleanStopTime(stopSequence));
|
|
322
|
-
resolve(content);
|
|
323
|
-
}
|
|
324
|
-
resolve([]);
|
|
325
|
-
}, (err) => {
|
|
326
|
-
reject(err);
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
getStopSequences(ids) {
|
|
331
|
-
const promises = ids.map((id) => this.getStopSequence(id));
|
|
332
|
-
return Promise.all(promises);
|
|
333
|
-
}
|
|
334
|
-
subscribeStopSequence(id, onMessage) {
|
|
335
|
-
window.clearTimeout(this.fullTrajectoryUpdateTimeout);
|
|
336
|
-
this.unsubscribeStopSequence(id);
|
|
337
|
-
this.subscribe(`stopsequence_${id}`, (data) => {
|
|
338
|
-
if (data.content && data.content.length) {
|
|
339
|
-
const content = data.content.map((stopSequence) => cleanStopTime(stopSequence));
|
|
340
|
-
onMessage(content);
|
|
341
|
-
}
|
|
342
|
-
}, (err) => {
|
|
343
|
-
console.log("subscribe stopsequence error", err);
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
unsubscribeStopSequence(id, cb) {
|
|
347
|
-
this.unsubscribe(`stopsequence`, `_${id}`, cb);
|
|
348
|
-
}
|
|
349
|
-
subscribeHealthCheck(onMessage) {
|
|
350
|
-
this.unsubscribeHealthCheck();
|
|
351
|
-
this.subscribe("healthcheck", onMessage);
|
|
352
|
-
}
|
|
353
|
-
unsubscribeHealthCheck() {
|
|
354
|
-
this.unsubscribe("healthcheck");
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
export default RealtimeAPI;
|
package/api/RealtimeAPI.test.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { RealtimeAPI, RealtimeModes } from ".";
|
|
2
|
-
describe("RealtimeAPI", () => {
|
|
3
|
-
let api;
|
|
4
|
-
let get;
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
get = jest.fn((params, cb) => {
|
|
7
|
-
cb({ content: "content" });
|
|
8
|
-
});
|
|
9
|
-
api = new RealtimeAPI();
|
|
10
|
-
api.wsApi = {
|
|
11
|
-
get
|
|
12
|
-
};
|
|
13
|
-
});
|
|
14
|
-
describe("#getFullTrajectory() calls fullTrajectory channel", () => {
|
|
15
|
-
test("without parameters", (done) => {
|
|
16
|
-
api.getFullTrajectory().then(() => {
|
|
17
|
-
expect(get.mock.calls.length).toBe(1);
|
|
18
|
-
expect(get.mock.calls[0][0]).toEqual({
|
|
19
|
-
channel: "full_trajectory"
|
|
20
|
-
});
|
|
21
|
-
done();
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
[null, RealtimeModes.TOPOGRAPHIC].forEach((mode) => {
|
|
25
|
-
describe(`using mode ${mode}`, () => {
|
|
26
|
-
test("using id", (done) => {
|
|
27
|
-
api.getFullTrajectory("foo", mode).then(() => {
|
|
28
|
-
expect(get.mock.calls.length).toBe(1);
|
|
29
|
-
expect(get.mock.calls[0][0]).toEqual({
|
|
30
|
-
channel: "full_trajectory_foo"
|
|
31
|
-
});
|
|
32
|
-
done();
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
test("using id and generalizationLevel param", (done) => {
|
|
36
|
-
api.getFullTrajectory("foo", mode, 5).then(() => {
|
|
37
|
-
expect(get.mock.calls.length).toBe(1);
|
|
38
|
-
expect(get.mock.calls[0][0]).toEqual({
|
|
39
|
-
channel: "full_trajectory_foo_gen5"
|
|
40
|
-
});
|
|
41
|
-
done();
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
describe("using schematic mode ", () => {
|
|
47
|
-
test("using id", (done) => {
|
|
48
|
-
api.getFullTrajectory("foo", RealtimeModes.SCHEMATIC).then(() => {
|
|
49
|
-
expect(get.mock.calls.length).toBe(1);
|
|
50
|
-
expect(get.mock.calls[0][0]).toEqual({
|
|
51
|
-
channel: "full_trajectory_schematic_foo"
|
|
52
|
-
});
|
|
53
|
-
done();
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
test("doesn't use generalizationLevel param", (done) => {
|
|
57
|
-
api.getFullTrajectory("foo", RealtimeModes.SCHEMATIC, 10).then(() => {
|
|
58
|
-
expect(get.mock.calls.length).toBe(1);
|
|
59
|
-
expect(get.mock.calls[0][0]).toEqual({
|
|
60
|
-
channel: "full_trajectory_schematic_foo"
|
|
61
|
-
});
|
|
62
|
-
done();
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
});
|
package/api/RoutingAPI.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import HttpAPI from "../common/api/HttpAPI";
|
|
2
|
-
class RoutingAPI extends HttpAPI {
|
|
3
|
-
constructor(options = {}) {
|
|
4
|
-
super({
|
|
5
|
-
url: "https://api.geops.io/routing/v1/",
|
|
6
|
-
...options
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
route(params, abortController = new AbortController()) {
|
|
10
|
-
return this.fetch("", params, {
|
|
11
|
-
signal: abortController.signal
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
export default RoutingAPI;
|
package/api/RoutingAPI.test.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import fetch from "jest-fetch-mock";
|
|
2
|
-
import RoutingAPI from "./RoutingAPI";
|
|
3
|
-
let api;
|
|
4
|
-
describe("RoutingAPI", () => {
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
global.fetch = fetch;
|
|
7
|
-
fetch.resetMocks();
|
|
8
|
-
api = new RoutingAPI({ apiKey: "apiKey" });
|
|
9
|
-
});
|
|
10
|
-
describe("#route", () => {
|
|
11
|
-
test("should success", (done) => {
|
|
12
|
-
fetch.mockResponseOnce(JSON.stringify(global.fetchRouteResponse));
|
|
13
|
-
return api.route({
|
|
14
|
-
mot: "bus",
|
|
15
|
-
via: "47.3739194713294,8.538274823394632|47.37595378493421,8.537490375951839"
|
|
16
|
-
}).then((featureCollection) => {
|
|
17
|
-
expect(fetch.mock.calls[0][0]).toEqual("https://api.geops.io/routing/v1/?key=apiKey&mot=bus&via=47.3739194713294%2C8.538274823394632%7C47.37595378493421%2C8.537490375951839");
|
|
18
|
-
expect(featureCollection.features[0].geometry.type).toEqual("LineString");
|
|
19
|
-
expect(featureCollection.features[0].properties.lines).toBeDefined();
|
|
20
|
-
expect(featureCollection.features[0].properties.station_to).toBeDefined();
|
|
21
|
-
done();
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
});
|
package/api/StopsAPI.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import HttpAPI from "../common/api/HttpAPI";
|
|
2
|
-
class StopsAPI extends HttpAPI {
|
|
3
|
-
constructor(options = {}) {
|
|
4
|
-
super({ url: "https://api.geops.io/stops/v1/", ...options });
|
|
5
|
-
}
|
|
6
|
-
search(params, abortController = {}) {
|
|
7
|
-
return this.fetch("", params, {
|
|
8
|
-
signal: abortController.signal
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export default StopsAPI;
|
package/api/StopsAPI.test.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import fetch from "jest-fetch-mock";
|
|
2
|
-
import StopsAPI from "./StopsAPI";
|
|
3
|
-
let api;
|
|
4
|
-
describe("StopsAPI", () => {
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
global.fetch = fetch;
|
|
7
|
-
fetch.resetMocks();
|
|
8
|
-
api = new StopsAPI({ apiKey: "apiKey" });
|
|
9
|
-
});
|
|
10
|
-
describe("#search", () => {
|
|
11
|
-
test("should success", (done) => {
|
|
12
|
-
fetch.mockResponseOnce(JSON.stringify(global.stopsSearchResponse));
|
|
13
|
-
return api.search({
|
|
14
|
-
q: "Bern"
|
|
15
|
-
}).then((featureCollection) => {
|
|
16
|
-
expect(fetch.mock.calls[0][0]).toEqual("https://api.geops.io/stops/v1/?key=apiKey&q=Bern");
|
|
17
|
-
expect(featureCollection.features[0].properties.name).toEqual("Bern");
|
|
18
|
-
done();
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
});
|
package/api/index.js
DELETED
package/api/typedefs.js
DELETED
|
File without changes
|
package/common/api/HttpAPI.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import BaseObject from "ol/Object";
|
|
2
|
-
import getUrlWithParams from "../utils/getUrlWithParams";
|
|
3
|
-
class HttpAPI extends BaseObject {
|
|
4
|
-
constructor(options = {}) {
|
|
5
|
-
super();
|
|
6
|
-
this.url = options.url;
|
|
7
|
-
this.apiKey = options.apiKey;
|
|
8
|
-
}
|
|
9
|
-
fetch(path, params, config) {
|
|
10
|
-
if (!this.apiKey && !/key=/.test(this.url)) {
|
|
11
|
-
return Promise.reject(new Error(`No apiKey defined for request to ${this.url}`));
|
|
12
|
-
}
|
|
13
|
-
const searchParams = params || {};
|
|
14
|
-
const url = getUrlWithParams(`${this.url}${path || ""}`, {
|
|
15
|
-
key: this.apiKey,
|
|
16
|
-
...searchParams
|
|
17
|
-
});
|
|
18
|
-
return fetch(url.toString(), config).then((response) => {
|
|
19
|
-
try {
|
|
20
|
-
return response.json().then((data) => {
|
|
21
|
-
if (data.error) {
|
|
22
|
-
throw new Error(data.error);
|
|
23
|
-
}
|
|
24
|
-
return data;
|
|
25
|
-
});
|
|
26
|
-
} catch (err) {
|
|
27
|
-
return Promise.reject(new Error(err));
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export default HttpAPI;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import fetch from "jest-fetch-mock";
|
|
2
|
-
import API from "./HttpAPI";
|
|
3
|
-
let api;
|
|
4
|
-
describe("HttpAPI", () => {
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
global.fetch = fetch;
|
|
7
|
-
fetch.resetMocks();
|
|
8
|
-
api = new API({ url: "https://foo.ch", apiKey: "apiKey" });
|
|
9
|
-
});
|
|
10
|
-
describe("#fetch", () => {
|
|
11
|
-
test("should success", () => {
|
|
12
|
-
fetch.mockResponseOnce(JSON.stringify({ foo: "bar" }));
|
|
13
|
-
return api.fetch("/path", {
|
|
14
|
-
q: "Bern",
|
|
15
|
-
fooUndefined: void 0,
|
|
16
|
-
fooNull: null,
|
|
17
|
-
fooEmpty: ""
|
|
18
|
-
}).then((response) => {
|
|
19
|
-
expect(fetch.mock.calls[0][0]).toEqual("https://foo.ch/path?key=apiKey&q=Bern&fooEmpty=");
|
|
20
|
-
expect(response).toEqual({ foo: "bar" });
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
describe("should display error message", () => {
|
|
24
|
-
test("reject error", (done) => {
|
|
25
|
-
fetch.mockRejectOnce(new Error("Fake error message"));
|
|
26
|
-
return api.fetch().catch((err) => {
|
|
27
|
-
expect(err.name).toEqual("Error");
|
|
28
|
-
expect(err.message).toEqual("Fake error message");
|
|
29
|
-
done();
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
test("if the response is invalid json", (done) => {
|
|
33
|
-
fetch.mockResponseOnce("invalid json");
|
|
34
|
-
api.fetch().catch((err) => {
|
|
35
|
-
expect(err.name).toEqual("FetchError");
|
|
36
|
-
expect(err.message).toEqual("invalid json response body at reason: Unexpected token i in JSON at position 0");
|
|
37
|
-
done();
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
test("if the response contains an error message", (done) => {
|
|
41
|
-
fetch.mockResponseOnce('{"error":"foo2"}');
|
|
42
|
-
api.fetch().catch((err) => {
|
|
43
|
-
expect(err.name).toEqual("Error");
|
|
44
|
-
expect(err.message).toEqual("foo2");
|
|
45
|
-
done();
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|