mobility-toolbox-js 2.0.0-beta.33 → 2.0.0-beta.34

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.
Files changed (69) hide show
  1. package/api/RealtimeAPI.js +640 -0
  2. package/api/RoutingAPI.js +65 -0
  3. package/api/StopsAPI.js +70 -0
  4. package/api/index.js +10 -0
  5. package/api/typedefs.js +72 -0
  6. package/common/api/HttpAPI.d.ts +2 -2
  7. package/common/api/HttpAPI.d.ts.map +1 -1
  8. package/common/api/HttpAPI.js +84 -0
  9. package/common/api/WebSocketAPI.js +320 -0
  10. package/common/controls/Control.js +170 -0
  11. package/common/index.js +18 -0
  12. package/common/layers/Layer.js +257 -0
  13. package/common/mixins/CopyrightMixin.js +72 -0
  14. package/common/mixins/MapboxLayerMixin.js +240 -0
  15. package/common/mixins/RealtimeLayerMixin.js +705 -0
  16. package/common/mixins/StopFinderMixin.js +198 -0
  17. package/common/mixins/UserInteractionsLayerMixin.js +225 -0
  18. package/common/styles/index.js +24 -0
  19. package/common/styles/realtimeDefaultStyle.js +248 -0
  20. package/common/styles/realtimeDelayStyle.js +26 -0
  21. package/common/styles/realtimeSimpleStyle.js +24 -0
  22. package/common/typedefs.js +21 -0
  23. package/common/utils/cleanStopTime.js +30 -0
  24. package/common/utils/compareDepartures.js +37 -0
  25. package/common/utils/createCanvas.js +29 -0
  26. package/common/utils/createTrackerFilters.js +77 -0
  27. package/common/utils/getLayersAsFlatArray.js +16 -0
  28. package/common/utils/getMapboxMapCopyrights.js +26 -0
  29. package/common/utils/getMapboxRender.js +77 -0
  30. package/common/utils/getMaplibreRender.js +38 -0
  31. package/common/utils/getRealtimeModeSuffix.js +11 -0
  32. package/common/utils/getUrlWithParams.js +21 -0
  33. package/common/utils/getVehiclePosition.js +66 -0
  34. package/common/utils/index.js +37 -0
  35. package/common/utils/removeDuplicate.js +30 -0
  36. package/common/utils/renderTrajectories.js +119 -0
  37. package/common/utils/sortByDelay.js +22 -0
  38. package/common/utils/timeUtils.js +49 -0
  39. package/common/utils/trackerConfig.js +182 -0
  40. package/iife.js +7 -0
  41. package/index.js +11 -0
  42. package/mapbox/controls/CopyrightControl.js +73 -0
  43. package/mapbox/controls/index.js +6 -0
  44. package/mapbox/index.js +20 -0
  45. package/mapbox/layers/Layer.js +139 -0
  46. package/mapbox/layers/RealtimeLayer.js +312 -0
  47. package/mapbox/layers/index.js +7 -0
  48. package/mapbox/utils.js +57 -0
  49. package/mbt.js.map +2 -2
  50. package/mbt.min.js.map +2 -2
  51. package/ol/controls/CopyrightControl.js +90 -0
  52. package/ol/controls/RoutingControl.js +683 -0
  53. package/ol/controls/StopFinderControl.js +59 -0
  54. package/ol/controls/index.js +9 -0
  55. package/ol/index.js +21 -0
  56. package/ol/layers/Layer.js +180 -0
  57. package/ol/layers/MapboxLayer.js +137 -0
  58. package/ol/layers/MapboxStyleLayer.js +383 -0
  59. package/ol/layers/MaplibreLayer.js +69 -0
  60. package/ol/layers/RealtimeLayer.js +330 -0
  61. package/ol/layers/RoutingLayer.js +116 -0
  62. package/ol/layers/VectorLayer.js +72 -0
  63. package/ol/layers/WMSLayer.js +106 -0
  64. package/ol/layers/index.js +19 -0
  65. package/ol/styles/fullTrajectoryDelayStyle.js +35 -0
  66. package/ol/styles/fullTrajectoryStyle.js +46 -0
  67. package/ol/styles/index.js +7 -0
  68. package/package.json +1 -1
  69. package/setupTests.js +15 -0
@@ -0,0 +1,640 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RealtimeModes = void 0;
4
+ var WebSocketAPI_1 = require("../common/api/WebSocketAPI");
5
+ var cleanStopTime_1 = require("../common/utils/cleanStopTime");
6
+ var getRealtimeModeSuffix_1 = require("../common/utils/getRealtimeModeSuffix");
7
+ var compareDepartures_1 = require("../common/utils/compareDepartures");
8
+ /**
9
+ * Enum for Realtime modes.
10
+ * @readonly
11
+ * @typedef {string} RealtimeMode
12
+ * @property {string} RAW "raw"
13
+ * @property {string} SCHEMATIC "schematic"
14
+ * @property {string} TOPOGRAPHIC "topographic"
15
+ * @enum {RealtimeMode}
16
+ */
17
+ exports.RealtimeModes = {
18
+ RAW: 'raw',
19
+ TOPOGRAPHIC: 'topographic',
20
+ SCHEMATIC: 'schematic',
21
+ };
22
+ /**
23
+ * This class provides convenience methods to access to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
24
+ *
25
+ * @example
26
+ * import { RealtimeAPI } from 'mobility-toolbox-js/api';
27
+ *
28
+ * const api = new RealtimeAPI({
29
+ * url: "yourUrl",
30
+ * apiKey: "yourApiKey"
31
+ * });
32
+ *
33
+ * @example
34
+ * import { RealtimeAPI } from 'mobility-toolbox-js/api';
35
+ *
36
+ * const api = new RealtimeAPI("yourUrl");
37
+ */
38
+ var RealtimeAPI = /** @class */ (function () {
39
+ /**
40
+ * Constructor
41
+ *
42
+ * @param {Object|string} options A string representing the url of the service or an object containing the url and the apiKey.
43
+ * @param {string} options.url Url to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
44
+ * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
45
+ * @param {string} [options.prefix=''] Service prefix to specify tenant.
46
+ * @param {string} [options.projection] The epsg code of the projection for features. Default to EPSG:3857.
47
+ * @param {number[4]} [options.bbox=[minX, minY, maxX, maxY, zoom, tenant] The bounding box to receive data from.
48
+ */
49
+ function RealtimeAPI(options) {
50
+ if (options === void 0) { options = {}; }
51
+ this.defineProperties(options);
52
+ /** @ignore */
53
+ this.subscribedStationUic = null;
54
+ /** @ignore */
55
+ this.departureUpdateTimeout = null;
56
+ /** @ignore */
57
+ this.maxDepartureAge = 30;
58
+ /** @ignore */
59
+ this.extraGeoms = {};
60
+ /** @ignore */
61
+ this.prefix = options.prefix || '';
62
+ /** @ignore */
63
+ this.onOpen = this.onOpen.bind(this);
64
+ }
65
+ RealtimeAPI.prototype.defineProperties = function (options) {
66
+ var _this = this;
67
+ var opt = options;
68
+ if (typeof options === 'string') {
69
+ opt = { url: options };
70
+ }
71
+ var apiKey = opt.apiKey;
72
+ var url = opt.url, projection = opt.projection, bbox = opt.bbox, _a = opt.buffer, buffer = _a === void 0 ? [100, 100] : _a;
73
+ var wsApi = new WebSocketAPI_1.default();
74
+ if (apiKey) {
75
+ url = "".concat(url || 'wss://api.geops.io/tracker-ws/v1/', "?key=").concat(apiKey);
76
+ }
77
+ Object.defineProperties(this, {
78
+ url: {
79
+ get: function () { return url; },
80
+ set: function (newUrl) {
81
+ url = newUrl;
82
+ _this.open();
83
+ },
84
+ },
85
+ projection: {
86
+ get: function () { return projection; },
87
+ set: function (newProjection) {
88
+ if (newProjection !== projection) {
89
+ projection = newProjection;
90
+ if (_this.wsApi) {
91
+ _this.wsApi.send("PROJECTION ".concat(projection));
92
+ }
93
+ }
94
+ },
95
+ },
96
+ bbox: {
97
+ get: function () { return bbox; },
98
+ set: function (newBbox) {
99
+ if (JSON.stringify(newBbox) !== JSON.stringify(bbox)) {
100
+ bbox = newBbox;
101
+ if (_this.wsApi) {
102
+ _this.wsApi.send("BBOX ".concat(bbox.join(' ')));
103
+ }
104
+ }
105
+ },
106
+ },
107
+ buffer: {
108
+ get: function () { return buffer; },
109
+ set: function (newBuffer) {
110
+ if (JSON.stringify(newBuffer) !== JSON.stringify(buffer)) {
111
+ buffer = newBuffer;
112
+ if (_this.wsApi) {
113
+ _this.wsApi.send("BUFFER ".concat(buffer.join(' ')));
114
+ }
115
+ }
116
+ },
117
+ },
118
+ /**
119
+ * The websocket helper class to connect the websocket.
120
+ *
121
+ * @private
122
+ */
123
+ wsApi: {
124
+ value: wsApi,
125
+ writable: true,
126
+ },
127
+ /**
128
+ * Interval between PING request in ms.
129
+ * If equal to 0, no PING request are sent.
130
+ * @type {number}
131
+ * @private
132
+ */
133
+ pingIntervalMs: {
134
+ value: options.pingIntervalMs || 10000,
135
+ writable: true,
136
+ },
137
+ /**
138
+ * Timeout in ms after an automatic reconnection when the websoscket has been closed by the server.
139
+ * @type {number}
140
+ */
141
+ reconnectTimeoutMs: {
142
+ value: options.pingIntervalMs || 100,
143
+ writable: true,
144
+ },
145
+ });
146
+ };
147
+ RealtimeAPI.prototype.open = function () {
148
+ var _this = this;
149
+ this.close();
150
+ // Register BBOX and PROJECTION messages must be send before previous subscriptions.
151
+ this.wsApi.connect(this.url, this.onOpen);
152
+ // Register reconnection on close.
153
+ this.wsApi.websocket.onclose = function () {
154
+ _this.onClose();
155
+ };
156
+ };
157
+ /**
158
+ * Close the websocket connection without reconnection.
159
+ */
160
+ RealtimeAPI.prototype.close = function () {
161
+ this.wsApi.close();
162
+ };
163
+ /**
164
+ * Unsubscribe trajectory and deleted_vehicles channels. To resubscribe you have to set a new BBOX.
165
+ */
166
+ // eslint-disable-next-line class-methods-use-this
167
+ RealtimeAPI.prototype.reset = function () {
168
+ this.wsApi.send('RESET');
169
+ };
170
+ /**
171
+ * Callback when the websocket is opened and ready.
172
+ * It applies the bbox and the projection.
173
+ */
174
+ RealtimeAPI.prototype.onOpen = function () {
175
+ var _this = this;
176
+ if (this.projection) {
177
+ this.wsApi.send("PROJECTION ".concat(this.projection));
178
+ }
179
+ if (this.bbox) {
180
+ this.wsApi.send("BBOX ".concat(this.bbox.join(' ')));
181
+ }
182
+ if (this.buffer) {
183
+ this.wsApi.send("BUFFER ".concat(this.buffer.join(' ')));
184
+ }
185
+ /**
186
+ * Keep websocket alive
187
+ */
188
+ if (this.pingIntervalMs) {
189
+ window.clearInterval(this.pingInterval);
190
+ /** @ignore */
191
+ this.pingInterval = setInterval(function () {
192
+ _this.wsApi.send('PING');
193
+ }, this.pingIntervalMs);
194
+ }
195
+ };
196
+ /**
197
+ * Callback when the websocket is closed by the server.
198
+ * It auto reconnects after a timeout.
199
+ */
200
+ RealtimeAPI.prototype.onClose = function () {
201
+ var _this = this;
202
+ window.clearTimeout(this.pingInterval);
203
+ window.clearTimeout(this.reconnectTimeout);
204
+ if (this.reconnectTimeoutMs) {
205
+ /** @ignore */
206
+ this.reconnectTimeout = window.setTimeout(function () { return _this.open(); }, this.reconnectTimeoutMs);
207
+ }
208
+ };
209
+ /**
210
+ * Subscribe to a channel.
211
+ *
212
+ * @param {string} channel Name of the websocket channel to subscribe.
213
+ * @param {function} onSuccess Callback when the subscription succeeds.
214
+ * @param {function} onError Callback when the subscription fails.
215
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
216
+ * @private
217
+ */
218
+ RealtimeAPI.prototype.subscribe = function (channel, onSuccess, onError, quiet) {
219
+ if (quiet === void 0) { quiet = false; }
220
+ this.wsApi.subscribe({ channel: channel }, onSuccess, onError, quiet);
221
+ };
222
+ /**
223
+ * Unsubscribe both modes of a channel.
224
+ *
225
+ * @param {string} channel Name of the websocket channel to unsubscribe.
226
+ * @param {string} suffix Suffix to add to the channel name.
227
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
228
+ * @private
229
+ */
230
+ RealtimeAPI.prototype.unsubscribe = function (channel, suffix, cb) {
231
+ this.wsApi.unsubscribe("".concat(channel).concat((0, getRealtimeModeSuffix_1.default)(exports.RealtimeModes.SCHEMATIC, exports.RealtimeModes)).concat(suffix), cb);
232
+ this.wsApi.unsubscribe("".concat(channel).concat((0, getRealtimeModeSuffix_1.default)(exports.RealtimeModes.TOPOGRAPHIC, exports.RealtimeModes)).concat(suffix || ''), cb);
233
+ };
234
+ /**
235
+ * Filter departures and return an array.
236
+ *
237
+ * @param {Object} depObject The object containing departures by id.
238
+ * @param {boolean} [sortByMinArrivalTime=false] If true sort departures by arrival time.
239
+ * @return {Array<departure>} Return departures array.
240
+ * @private
241
+ */
242
+ RealtimeAPI.prototype.filterDepartures = function (depObject, sortByMinArrivalTime) {
243
+ if (sortByMinArrivalTime === void 0) { sortByMinArrivalTime = false; }
244
+ var departures = Object.keys(depObject).map(function (k) { return depObject[k]; });
245
+ departures.sort(function (a, b) { return (0, compareDepartures_1.default)(a, b, sortByMinArrivalTime); });
246
+ var future = new Date();
247
+ future.setMinutes(future.getMinutes() + this.maxDepartureAge);
248
+ future = future.getTime();
249
+ var past = new Date();
250
+ past.setMinutes(past.getMinutes() - this.maxDepartureAge);
251
+ past = past.getTime();
252
+ var departureArray = [];
253
+ var platformsBoarding = [];
254
+ var previousDeparture = null;
255
+ for (var i = departures.length - 1; i >= 0; i -= 1) {
256
+ var d = departures[i];
257
+ var t = new Date(d.time).getTime();
258
+ // Only show departures within the next 30 minutes
259
+ if (t > past && t < future) {
260
+ // If 2 trains are boarding at the same platform,
261
+ // remove the older one.
262
+ if (d.state === 'BOARDING') {
263
+ if (platformsBoarding.indexOf(d.platform) === -1) {
264
+ platformsBoarding.push(d.platform);
265
+ }
266
+ else {
267
+ d.state = 'HIDDEN';
268
+ }
269
+ }
270
+ // If two trains with the same line number and destinatin
271
+ // and a departure difference < 1 minute, hide the second one.
272
+ if (previousDeparture &&
273
+ d.to[0] === previousDeparture.to[0] &&
274
+ Math.abs(t - previousDeparture.time) < 1000 &&
275
+ d.line.name === previousDeparture.line.name) {
276
+ d.state = 'HIDDEN';
277
+ }
278
+ if (/(STOP_CANCELLED|JOURNEY_CANCELLED)/.test(d.state)) {
279
+ d.cancelled = true;
280
+ }
281
+ previousDeparture = d;
282
+ previousDeparture.time = t;
283
+ departureArray.unshift(d);
284
+ }
285
+ }
286
+ return departureArray;
287
+ };
288
+ /**
289
+ * Subscribe to departures channel of a given station.
290
+ *
291
+ * @param {number} stationId UIC of the station.
292
+ * @param {Boolean} sortByMinArrivalTime Sort by minimum arrival time
293
+ * @param {function(departures:Departure[])} onMessage Function called on each message of the channel.
294
+ */
295
+ RealtimeAPI.prototype.subscribeDepartures = function (stationId, sortByMinArrivalTime, onMessage) {
296
+ var _this = this;
297
+ window.clearTimeout(this.departureUpdateTimeout);
298
+ this.unsubscribeDepartures();
299
+ this.subscribedStationUic = stationId;
300
+ var channel = stationId ? "timetable_".concat(stationId) : null;
301
+ var departureObject = {};
302
+ this.subscribe(channel, function (data) {
303
+ if (data.source === channel) {
304
+ var content = data.content || {};
305
+ var tDiff = new Date(content.timestamp).getTime() - Date.now();
306
+ content.timediff = tDiff;
307
+ departureObject[content.call_id] = content;
308
+ window.clearTimeout(_this.departureUpdateTimeout);
309
+ _this.departureUpdateTimeout = window.setTimeout(function () {
310
+ var departures = _this.filterDepartures(departureObject, sortByMinArrivalTime || false);
311
+ onMessage(departures);
312
+ }, 100);
313
+ }
314
+ }, function () {
315
+ onMessage([]);
316
+ });
317
+ };
318
+ /**
319
+ * Unsubscribe from current departures channel.
320
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
321
+ */
322
+ RealtimeAPI.prototype.unsubscribeDepartures = function (cb) {
323
+ if (this.subscribedStationUic) {
324
+ this.unsubscribe("timetable_".concat(this.subscribedStationUic), '', cb);
325
+ this.subscribedStationUic = null;
326
+ }
327
+ };
328
+ /**
329
+ * Subscribe to the disruptions channel for tenant.
330
+ *
331
+ * @param {function} onMessage Function called on each message of the channel.
332
+ */
333
+ RealtimeAPI.prototype.subscribeDisruptions = function (onMessage) {
334
+ this.subscribe("".concat(this.prefix, "newsticker"), function (data) {
335
+ onMessage(data.content);
336
+ });
337
+ };
338
+ /**
339
+ * Unsubscribe disruptions.
340
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
341
+ */
342
+ RealtimeAPI.prototype.unsubscribeDisruptions = function (cb) {
343
+ this.unsubscribe("".concat(this.prefix, "newsticker"), '', cb);
344
+ };
345
+ /**
346
+ * Return a station with a given uic number and a mode.
347
+ *
348
+ * @param {number} uic UIC of the station.
349
+ * @param {RealtimeMode} mode Realtime mode.
350
+ * @return {Promise<Station>} A station.
351
+ */
352
+ RealtimeAPI.prototype.getStation = function (uic, mode) {
353
+ var _this = this;
354
+ var params = {
355
+ channel: "station".concat((0, getRealtimeModeSuffix_1.default)(mode, exports.RealtimeModes)),
356
+ args: uic,
357
+ };
358
+ return new Promise(function (resolve, reject) {
359
+ _this.wsApi.get(params, function (data) {
360
+ if (data.content) {
361
+ resolve(data.content);
362
+ }
363
+ else {
364
+ reject();
365
+ }
366
+ });
367
+ });
368
+ };
369
+ /**
370
+ * Update the model's station list for a given mode and a bbox.
371
+ *
372
+ * @param {RealtimeMode} mode Realtime mode.
373
+ * @return {Promise<Array<Station>>} An array of stations.
374
+ */
375
+ RealtimeAPI.prototype.getStations = function (mode) {
376
+ var _this = this;
377
+ var stations = [];
378
+ var params = {
379
+ channel: "station".concat((0, getRealtimeModeSuffix_1.default)(mode, exports.RealtimeModes)),
380
+ };
381
+ window.clearTimeout(this.stationUpdateTimeout);
382
+ return new Promise(function (resolve, reject) {
383
+ _this.wsApi.get(params, function (data) {
384
+ if (data.content) {
385
+ stations.push(data.content);
386
+ window.clearTimeout(_this.stationUpdateTimeout);
387
+ /** @ignore */
388
+ _this.stationUpdateTimeout = window.setTimeout(function () {
389
+ resolve(stations);
390
+ }, 50);
391
+ }
392
+ else {
393
+ reject(data.content);
394
+ }
395
+ });
396
+ });
397
+ };
398
+ /**
399
+ * Subscribe to stations channel.
400
+ * One message pro station.
401
+ *
402
+ * @param {RealtimeMode} mode Realtime mode.
403
+ * @param {function(station: Station)} onMessage Function called on each message of the channel.
404
+ */
405
+ RealtimeAPI.prototype.subscribeStations = function (mode, onMessage) {
406
+ this.unsubscribeStations();
407
+ this.subscribe("station".concat((0, getRealtimeModeSuffix_1.default)(mode, exports.RealtimeModes)), function (data) {
408
+ if (data.content) {
409
+ onMessage(data.content);
410
+ }
411
+ });
412
+ };
413
+ /**
414
+ * Unsubscribe to stations channel.
415
+ * @param {function} cb The listener callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribe.
416
+ */
417
+ RealtimeAPI.prototype.unsubscribeStations = function (cb) {
418
+ window.clearTimeout(this.stationUpdateTimeout);
419
+ this.unsubscribe('station', '', cb);
420
+ };
421
+ /**
422
+ * Subscribe to extra_geoms channel.
423
+ *
424
+ * @param {function(extraGeoms: GeosJSONFeature[])} onMessage Function called on each message of the channel.
425
+ */
426
+ RealtimeAPI.prototype.subscribeExtraGeoms = function (onMessage) {
427
+ var _this = this;
428
+ this.subscribe('extra_geoms', function (data) {
429
+ var extraGeom = data.content;
430
+ if (extraGeom) {
431
+ var ref = extraGeom.properties.ref;
432
+ if (extraGeom.type === 'Feature') {
433
+ _this.extraGeoms[ref] = extraGeom;
434
+ }
435
+ else {
436
+ delete _this.extraGeoms[ref];
437
+ }
438
+ onMessage(Object.keys(_this.extraGeoms).map(function (key) { return _this.extraGeoms[key]; }));
439
+ }
440
+ });
441
+ };
442
+ /**
443
+ * Unsubscribe to extra_geoms channel.
444
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
445
+ */
446
+ RealtimeAPI.prototype.unsubscribeExtraGeoms = function (cb) {
447
+ this.unsubscribe('extra_geoms', '', cb);
448
+ };
449
+ /**
450
+ * Subscribe to trajectory channel.
451
+ *
452
+ * @param {RealtimeMode} mode Realtime mode.
453
+ * @param {function(trajectory: RealtimeTrajectory)} onMessage Function called on each message of the channel.
454
+ * @param {boolean} quiet If true, the subscription will not send GET and SUB requests to the websocket.
455
+ */
456
+ RealtimeAPI.prototype.subscribeTrajectory = function (mode, onMessage, quiet) {
457
+ if (quiet === void 0) { quiet = false; }
458
+ this.unsubscribeTrajectory(onMessage);
459
+ this.subscribe("trajectory".concat((0, getRealtimeModeSuffix_1.default)(mode, exports.RealtimeModes)), onMessage, null, quiet);
460
+ };
461
+ /**
462
+ * Unsubscribe to trajectory channels.
463
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
464
+ */
465
+ RealtimeAPI.prototype.unsubscribeTrajectory = function (cb) {
466
+ this.unsubscribe("trajectory", '', cb);
467
+ };
468
+ /**
469
+ * Subscribe to deleted_vhicles channel.
470
+ *
471
+ * @param {RealtimeMode} mode Realtime mode.
472
+ * @param {function(response: { content: Vehicle })} onMessage Function called on each message of the channel.
473
+ * @param {boolean} quiet If true, the subscription will not send GET and SUB requests to the websocket.
474
+ */
475
+ RealtimeAPI.prototype.subscribeDeletedVehicles = function (mode, onMessage, quiet) {
476
+ if (quiet === void 0) { quiet = false; }
477
+ this.unsubscribeDeletedVehicles(onMessage);
478
+ this.subscribe("deleted_vehicles".concat((0, getRealtimeModeSuffix_1.default)(mode, exports.RealtimeModes)), onMessage, null, quiet);
479
+ };
480
+ /**
481
+ * Unsubscribe to deleted_vhicles channels.
482
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
483
+ */
484
+ RealtimeAPI.prototype.unsubscribeDeletedVehicles = function (cb) {
485
+ this.unsubscribe('deleted_vehicles', '', cb);
486
+ };
487
+ /**
488
+ * Get a full trajectory of a vehicule .
489
+ *
490
+ * @param {string} id A vehicle id.
491
+ * @param {RealtimeMode} mode Realtime mode.
492
+ * @param {string} generalizationLevel The generalization level to request. Can be one of 5 (more generalized), 10, 30, 100, undefined (less generalized).
493
+ * @return {Promise<FullTrajectory>} Return a full trajectory.
494
+ */
495
+ RealtimeAPI.prototype.getFullTrajectory = function (id, mode, generalizationLevel) {
496
+ var _this = this;
497
+ var channel = ["full_trajectory".concat((0, getRealtimeModeSuffix_1.default)(mode, exports.RealtimeModes))];
498
+ if (id) {
499
+ channel.push(id);
500
+ }
501
+ if ((!mode || mode === exports.RealtimeModes.TOPOGRAPHIC) && generalizationLevel) {
502
+ channel.push("gen".concat(generalizationLevel));
503
+ }
504
+ var params = {
505
+ channel: channel.join('_'),
506
+ };
507
+ return new Promise(function (resolve) {
508
+ _this.wsApi.get(params, function (data) {
509
+ if (data.content) {
510
+ resolve(data.content);
511
+ }
512
+ });
513
+ });
514
+ };
515
+ /**
516
+ * Get full trajectories of a vehicules .
517
+ *
518
+ * @param {string[]} ids List of vehicles ids.
519
+ * @param {RealtimeMode} mode Realtime mode.
520
+ * @param {string} generalizationLevel The generalization level to request. Can be one of '', 'gen5', 'gen10', 'gen30', 'gen100'.
521
+ * @return {Promise<Array<FullTrajectory>>} Return an array of full trajectories.
522
+ */
523
+ RealtimeAPI.prototype.getFullTrajectories = function (ids, mode, generalizationLevel) {
524
+ var _this = this;
525
+ var promises = ids.map(function (id) {
526
+ return _this.getFullTrajectory(id, mode, generalizationLevel);
527
+ });
528
+ return Promise.all(promises);
529
+ };
530
+ /**
531
+ * Subscribe to full_trajectory channel of a given vehicle.
532
+ *
533
+ * @param {string} id A vehicle id.
534
+ * @param {RealtimeMode} mode Realtime mode.
535
+ */
536
+ RealtimeAPI.prototype.subscribeFullTrajectory = function (id, mode) {
537
+ // window.clearTimeout(this.fullTrajectoryUpdateTimeout);
538
+ this.unsubscribeFullTrajectory(id);
539
+ this.subscribe("full_trajectory".concat((0, getRealtimeModeSuffix_1.default)(mode, exports.RealtimeModes), "_").concat(id), function (data) {
540
+ // eslint-disable-next-line no-console
541
+ console.log('subscribe full_trajectory', data);
542
+ }, function (err) {
543
+ // eslint-disable-next-line no-console
544
+ console.log('subscribe full_trajectory error', err);
545
+ });
546
+ };
547
+ /**
548
+ * Unsubscribe from full_trajectory channel
549
+ *
550
+ * @param {string} id A vehicle id.
551
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
552
+ */
553
+ RealtimeAPI.prototype.unsubscribeFullTrajectory = function (id, cb) {
554
+ this.unsubscribe('full_trajectory', "_".concat(id), cb);
555
+ };
556
+ /**
557
+ * Get the list of stops for this vehicle.
558
+ *
559
+ * @param {string} id A vehicle id.
560
+ * @return {Promise<StopSequence>} Returns a stop sequence object.
561
+ */
562
+ RealtimeAPI.prototype.getStopSequence = function (id) {
563
+ var _this = this;
564
+ var params = {
565
+ channel: "stopsequence_".concat(id),
566
+ };
567
+ return new Promise(function (resolve, reject) {
568
+ _this.wsApi.get(params, function (data) {
569
+ if (data.content && data.content.length) {
570
+ var content = data.content.map(function (stopSequence) {
571
+ return (0, cleanStopTime_1.default)(stopSequence);
572
+ });
573
+ // Remove the delay from arrivalTime and departureTime
574
+ resolve(content);
575
+ }
576
+ resolve([]);
577
+ }, function (err) {
578
+ reject(err);
579
+ });
580
+ });
581
+ };
582
+ /**
583
+ * Get a list of stops for a list of vehicles.
584
+ *
585
+ * @param {string[]} ids List of vehicles ids.
586
+ * @return {Promise<Array<StopSequence>>} Return an array of stop sequences.
587
+ */
588
+ RealtimeAPI.prototype.getStopSequences = function (ids) {
589
+ var _this = this;
590
+ var promises = ids.map(function (id) { return _this.getStopSequence(id); });
591
+ return Promise.all(promises);
592
+ };
593
+ /**
594
+ * Subscribe to stopsequence channel of a given vehicle.
595
+ *
596
+ * @param {string} id A vehicle id.
597
+ * @param {function(stopSequence: StopSequence)} onMessage Function called on each message of the channel.
598
+ */
599
+ RealtimeAPI.prototype.subscribeStopSequence = function (id, onMessage) {
600
+ window.clearTimeout(this.fullTrajectoryUpdateTimeout);
601
+ this.unsubscribeStopSequence(id);
602
+ this.subscribe("stopsequence_".concat(id), function (data) {
603
+ if (data.content && data.content.length) {
604
+ var content = data.content.map(function (stopSequence) {
605
+ return (0, cleanStopTime_1.default)(stopSequence);
606
+ });
607
+ // Remove the delay from arrivalTime and departureTime
608
+ onMessage(content);
609
+ }
610
+ }, function (err) {
611
+ // eslint-disable-next-line no-console
612
+ console.log('subscribe stopsequence error', err);
613
+ });
614
+ };
615
+ /**
616
+ * Unsubscribe from stopsequence channel
617
+ *
618
+ * @param {string} id A vehicle id.
619
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
620
+ */
621
+ RealtimeAPI.prototype.unsubscribeStopSequence = function (id, cb) {
622
+ this.unsubscribe("stopsequence", "_".concat(id), cb);
623
+ };
624
+ /**
625
+ * Subscribe to healthcheck channel.
626
+ * @param {function} onMessage Callback when the subscribe to healthcheck channel succeeds.
627
+ */
628
+ RealtimeAPI.prototype.subscribeHealthCheck = function (onMessage) {
629
+ this.unsubscribeHealthCheck();
630
+ this.subscribe('healthcheck', onMessage);
631
+ };
632
+ /**
633
+ * Unsubscribe to healthcheck channel.
634
+ */
635
+ RealtimeAPI.prototype.unsubscribeHealthCheck = function () {
636
+ this.unsubscribe('healthcheck');
637
+ };
638
+ return RealtimeAPI;
639
+ }());
640
+ exports.default = RealtimeAPI;