mobility-toolbox-js 3.0.0-beta.2 → 3.0.0-beta.21

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 (125) hide show
  1. package/api/HttpAPI.d.ts +20 -0
  2. package/api/HttpAPI.js +0 -11
  3. package/api/RealtimeAPI.d.ts +404 -0
  4. package/api/RealtimeAPI.js +342 -276
  5. package/api/RoutingAPI.d.ts +47 -0
  6. package/api/RoutingAPI.js +17 -7
  7. package/api/StopsAPI.d.ts +44 -0
  8. package/api/StopsAPI.js +16 -10
  9. package/api/WebSocketAPI.d.ts +147 -0
  10. package/api/WebSocketAPI.js +164 -164
  11. package/api/index.d.ts +3 -0
  12. package/api/index.js +1 -1
  13. package/api/typedefs.d.ts +76 -0
  14. package/api/typedefs.js +27 -42
  15. package/common/controls/StopFinderControlCommon.d.ts +53 -0
  16. package/common/controls/StopFinderControlCommon.js +31 -31
  17. package/common/index.d.ts +2 -0
  18. package/common/index.js +1 -1
  19. package/common/mixins/RealtimeLayerMixin.d.ts +267 -0
  20. package/common/mixins/RealtimeLayerMixin.js +401 -393
  21. package/common/styles/index.d.ts +4 -0
  22. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  23. package/common/styles/realtimeDefaultStyle.js +6 -6
  24. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  25. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  26. package/common/styles/realtimeHeadingStyle.js +5 -5
  27. package/common/styles/realtimeSimpleStyle.d.ts +4 -0
  28. package/common/typedefs.d.ts +219 -0
  29. package/common/typedefs.js +7 -1
  30. package/common/utils/compareDepartures.d.ts +10 -0
  31. package/common/utils/compareDepartures.js +2 -2
  32. package/common/utils/constants.d.ts +5 -0
  33. package/common/utils/createCanvas.d.ts +10 -0
  34. package/common/utils/createDefaultCopyrightElt.d.ts +5 -0
  35. package/common/utils/createDefaultStopFinderElt.d.ts +5 -0
  36. package/common/utils/createRealtimeFilters.d.ts +12 -0
  37. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  38. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  39. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  40. package/common/utils/getLayersAsFlatArray.js +5 -1
  41. package/common/utils/getMapGlCopyrights.d.ts +17 -0
  42. package/common/utils/getMapGlCopyrights.js +3 -3
  43. package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
  44. package/common/utils/getRealtimeModeSuffix.js +1 -0
  45. package/common/utils/getUrlWithParams.d.ts +8 -0
  46. package/common/utils/getVehiclePosition.d.ts +17 -0
  47. package/common/utils/getVehiclePosition.js +9 -3
  48. package/common/utils/index.d.ts +16 -0
  49. package/common/utils/realtimeConfig.d.ts +64 -0
  50. package/common/utils/removeDuplicate.d.ts +9 -0
  51. package/common/utils/renderTrajectories.d.ts +16 -0
  52. package/common/utils/renderTrajectories.js +6 -6
  53. package/common/utils/sortAndFilterDepartures.d.ts +15 -0
  54. package/common/utils/sortAndFilterDepartures.js +1 -1
  55. package/common/utils/sortByDelay.d.ts +3 -0
  56. package/common/utils/sortByDelay.js +5 -1
  57. package/common/utils/timeUtils.d.ts +23 -0
  58. package/common/utils/toMercatorExtent.d.ts +5 -0
  59. package/iife.d.ts +2 -0
  60. package/index.d.ts +9 -0
  61. package/maplibre/controls/CopyrightControl.d.ts +35 -0
  62. package/maplibre/controls/index.d.ts +1 -0
  63. package/maplibre/index.d.ts +5 -0
  64. package/maplibre/layers/Layer.d.ts +28 -0
  65. package/maplibre/layers/Layer.js +1 -1
  66. package/maplibre/layers/RealtimeLayer.d.ts +181 -0
  67. package/maplibre/layers/RealtimeLayer.js +29 -5
  68. package/maplibre/layers/index.d.ts +2 -0
  69. package/maplibre/utils/getMercatorResolution.d.ts +7 -0
  70. package/maplibre/utils/getSourceCoordinates.d.ts +7 -0
  71. package/maplibre/utils/getSourceCoordinates.js +5 -5
  72. package/maplibre/utils/index.d.ts +2 -0
  73. package/mbt.js +22103 -14430
  74. package/mbt.js.map +4 -4
  75. package/mbt.min.js +61 -58
  76. package/mbt.min.js.map +4 -4
  77. package/ol/controls/CopyrightControl.d.ts +31 -0
  78. package/ol/controls/CopyrightControl.js +18 -8
  79. package/ol/controls/RoutingControl.d.ts +202 -0
  80. package/ol/controls/RoutingControl.js +220 -219
  81. package/ol/controls/StopFinderControl.d.ts +37 -0
  82. package/ol/controls/StopFinderControl.js +4 -1
  83. package/ol/controls/index.d.ts +3 -0
  84. package/ol/index.d.ts +7 -0
  85. package/ol/index.js +1 -0
  86. package/ol/layers/Layer.d.ts +101 -0
  87. package/ol/layers/Layer.js +25 -0
  88. package/ol/layers/MaplibreLayer.d.ts +160 -0
  89. package/ol/layers/MaplibreLayer.js +97 -27
  90. package/ol/layers/MaplibreStyleLayer.d.ts +237 -0
  91. package/ol/layers/MaplibreStyleLayer.js +291 -267
  92. package/ol/layers/RealtimeLayer.d.ts +283 -0
  93. package/ol/layers/RealtimeLayer.js +143 -128
  94. package/ol/layers/VectorLayer.d.ts +18 -0
  95. package/ol/layers/VectorLayer.js +31 -0
  96. package/ol/layers/index.d.ts +5 -0
  97. package/ol/layers/index.js +3 -0
  98. package/ol/mixins/MobilityLayerMixin.d.ts +96 -0
  99. package/ol/mixins/MobilityLayerMixin.js +1 -4
  100. package/ol/mixins/PropertiesLayerMixin.d.ts +135 -0
  101. package/ol/mixins/PropertiesLayerMixin.js +112 -140
  102. package/ol/mixins/index.d.ts +1 -0
  103. package/ol/mixins/index.js +2 -0
  104. package/ol/renderers/MaplibreLayerRenderer.d.ts +0 -0
  105. package/ol/renderers/MaplibreLayerRenderer.js +142 -114
  106. package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +20 -0
  107. package/ol/renderers/MaplibreStyleLayerRenderer.js +20 -23
  108. package/ol/renderers/RealtimeLayerRenderer.d.ts +22 -0
  109. package/ol/renderers/RealtimeLayerRenderer.js +58 -53
  110. package/ol/styles/fullTrajectoryDelayStyle.d.ts +6 -0
  111. package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
  112. package/ol/styles/index.d.ts +3 -0
  113. package/ol/styles/routingStyle.d.ts +4 -0
  114. package/ol/utils/getFeatureInfoAtCoordinate.d.ts +8 -0
  115. package/ol/utils/getFeatureInfoAtCoordinate.js +12 -18
  116. package/ol/utils/index.d.ts +1 -0
  117. package/package.json +31 -31
  118. package/setupTests.d.ts +1 -0
  119. package/setupTests.js +3 -4
  120. package/types/common.d.ts +55 -48
  121. package/types/index.d.ts +1 -1
  122. package/types/realtime.d.ts +91 -93
  123. package/types/routing.d.ts +60 -60
  124. package/types/stops.d.ts +62 -62
  125. package/ol/layers/MapGlLayer.js +0 -142
@@ -1,7 +1,8 @@
1
- /* eslint-disable class-methods-use-this */
2
- import WebSocketAPI from './WebSocketAPI';
1
+ /* eslint-disable no-underscore-dangle */
3
2
  import debounceWebsocketMessages from '../common/utils/debounceWebsocketMessages';
4
3
  import getModeSuffix from '../common/utils/getRealtimeModeSuffix';
4
+ /* eslint-disable class-methods-use-this */
5
+ import WebSocketAPI from './WebSocketAPI';
5
6
  /**
6
7
  * Enum for Realtime modes.
7
8
  * @readonly
@@ -10,11 +11,12 @@ import getModeSuffix from '../common/utils/getRealtimeModeSuffix';
10
11
  * @property {string} SCHEMATIC "schematic"
11
12
  * @property {string} TOPOGRAPHIC "topographic"
12
13
  * @enum {RealtimeMode}
14
+ * @public
13
15
  */
14
16
  export const RealtimeModes = {
15
17
  RAW: 'raw',
16
- TOPOGRAPHIC: 'topographic',
17
18
  SCHEMATIC: 'schematic',
19
+ TOPOGRAPHIC: 'topographic',
18
20
  };
19
21
  /**
20
22
  * This class provides convenience methods to use to the [geOps Realtime API](https://developer.geops.io/apis/realtime/).
@@ -24,145 +26,172 @@ export const RealtimeModes = {
24
26
  *
25
27
  * const api = new RealtimeAPI({
26
28
  * apiKey: "yourApiKey",
27
- * bbox: [782001, 5888803, 923410, 5923660, 11, "mots=rail],
29
+ * bbox: [782001, 5888803, 923410, 5923660, 11, "mots=rail"],
28
30
  * // url: "wss://api.geops.io/tracker-ws/v1/",
29
31
  * });
30
32
  *
33
+ * // Open the websocket connection
31
34
  * api.open();
32
35
  *
36
+ * // Subscribe to channel
33
37
  * api.subscribeTrajectory('topographic', (data) => {
34
38
  * console.log('Log trajectories:', JSON.stringify(data.content));
35
39
  * });
36
40
  *
37
- * @classproperty {string} apiKey - Access key for [geOps APIs](https://developer.geops.io/)
38
- * @classproperty {RealtimeBbox} bbox - The bounding box to receive data from. \ Example: \ [ minX, minY, maxX, maxY, zoom, "tenant=tenant1", "gen_level=5", "mots=mot1,mot2" ]. \ tenant, gen_level and mots are optional.
39
- * @classproperty {string} url - The [geOps Realtime API](https://developer.geops.io/apis/realtime/) url.
41
+ * // Close the websocket connection
42
+ * // api.close();
43
+ *
40
44
  * @public
41
45
  */
42
46
  class RealtimeAPI {
43
47
  /**
44
48
  * Constructor
45
49
  *
46
- * @param {RealtimeAPIOptions} options A string representing the url of the service or an object containing the url and the apiKey.
47
- * @param {string} options.url Url to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
50
+ * @param {Object} options Options.
48
51
  * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
49
- * @param {number[5]} [options.bbox=[minX, minY, maxX, maxY, zoom, tenant]] The bounding box to receive data from.
52
+ * @param {string[]} options.bbox The bounding box to receive data from.
53
+ * @param {string} [options.url='wss://api.geops.io/tracker-ws/v1/'] Url of the [geOps Realtime API](https://developer.geops.io/apis/realtime/).
54
+ * @public
50
55
  */
51
56
  constructor(options = {}) {
52
57
  this.version = '2';
53
- this.defineProperties(options);
54
- this.onOpen = this.onOpen.bind(this);
55
- }
56
- defineProperties(options = {}) {
57
- let opt = options || {};
58
+ let opt = options;
58
59
  if (typeof options === 'string') {
59
60
  opt = { url: options };
60
61
  }
61
- const { apiKey, version } = opt;
62
- let { url, bbox, buffer = [100, 100] } = opt;
62
+ const { apiKey } = opt;
63
+ const { url } = opt;
63
64
  const wsApi = new WebSocketAPI();
64
- if (!url) {
65
- url = 'wss://api.geops.io/tracker-ws/v1/';
65
+ let suffix = '';
66
+ if (apiKey && !(url === null || url === void 0 ? void 0 : url.includes('key='))) {
67
+ suffix = `?key=${apiKey}`;
66
68
  }
67
- if (apiKey) {
68
- url = `${url}?key=${apiKey}`;
69
+ this._url = (url || 'wss://api.geops.io/tracker-ws/v1/') + suffix;
70
+ this._buffer = opt.buffer || [100, 100];
71
+ this._bbox = opt.bbox;
72
+ this.version = opt.version || '2';
73
+ /**
74
+ * Interval between PING request in ms.
75
+ * If equal to 0, no PING request are sent.
76
+ * @type {number}
77
+ */
78
+ this.pingIntervalMs = opt.pingIntervalMs || 10000;
79
+ /**
80
+ * Timeout in ms after an automatic reconnection when the websoscket has been closed by the server.
81
+ * @type {number}
82
+ */
83
+ this.reconnectTimeoutMs = opt.reconnectTimeoutMs || 100;
84
+ /**
85
+ * The websocket helper class to connect the websocket.
86
+ */
87
+ this.wsApi = wsApi;
88
+ }
89
+ /**
90
+ * Close the websocket connection without reconnection.
91
+ *
92
+ * @public
93
+ */
94
+ close() {
95
+ this.wsApi.close();
96
+ }
97
+ /**
98
+ * Send GET to a channel.
99
+ *
100
+ * @param {string | WebSocketAPIParameters} channelOrParams Name of the websocket channel to send GET or an object representing parameters to send
101
+ * @return {Promise<WebSocketAPIMessageEventData<?>>} A websocket response.
102
+ * @public
103
+ */
104
+ get(channelOrParams) {
105
+ let params = channelOrParams;
106
+ if (typeof channelOrParams === 'string') {
107
+ params = { channel: channelOrParams };
69
108
  }
70
- Object.defineProperties(this, {
71
- url: {
72
- get: () => url,
73
- set: (newUrl) => {
74
- if (url !== newUrl) {
75
- url = newUrl;
76
- // Update the websocket only if the url has changed and the websocket is already open or is opening.
77
- if (this.wsApi.open || this.wsApi.connecting) {
78
- this.open();
79
- }
80
- }
81
- },
82
- },
83
- bbox: {
84
- get: () => bbox,
85
- set: (newBbox) => {
86
- if (JSON.stringify(newBbox) !== JSON.stringify(bbox)) {
87
- bbox = newBbox;
88
- if (this.wsApi && bbox) {
89
- this.wsApi.send(`BBOX ${bbox.join(' ')}`);
90
- }
91
- }
92
- },
93
- },
94
- buffer: {
95
- get: () => buffer,
96
- set: (newBuffer) => {
97
- if (JSON.stringify(newBuffer) !== JSON.stringify(buffer)) {
98
- buffer = newBuffer;
99
- if (this.wsApi) {
100
- this.wsApi.send(`BUFFER ${buffer.join(' ')}`);
101
- }
102
- }
103
- },
104
- },
105
- version: {
106
- value: version,
107
- writable: true,
108
- },
109
- /**
110
- * The websocket helper class to connect the websocket.
111
- *
112
- * @private
113
- */
114
- wsApi: {
115
- value: wsApi,
116
- writable: true,
117
- },
118
- /**
119
- * Interval between PING request in ms.
120
- * If equal to 0, no PING request are sent.
121
- * @type {number}
122
- * @private
123
- */
124
- pingIntervalMs: {
125
- value: options.pingIntervalMs || 10000,
126
- writable: true,
127
- },
128
- /**
129
- * Timeout in ms after an automatic reconnection when the websoscket has been closed by the server.
130
- * @type {number}
131
- */
132
- reconnectTimeoutMs: {
133
- value: options.pingIntervalMs || 100,
134
- writable: true,
135
- },
109
+ return new Promise((resolve, reject) => {
110
+ this.wsApi.get(params, resolve, reject);
136
111
  });
137
112
  }
138
113
  /**
139
- * Open the websocket connection.
114
+ * Get a full trajectory of a vehicule .
140
115
  *
116
+ * @param {string} id A vehicle id.
117
+ * @param {RealtimeMode} mode Realtime mode.
118
+ * @param {string} generalizationLevel The generalization level to request. Can be one of 5 (more generalized), 10, 30, 100, undefined (less generalized).
119
+ * @return {Promise<{data: { content: RealtimeFullTrajectory }}>} Return a full trajectory.
141
120
  * @public
142
121
  */
143
- open() {
144
- this.wsApi.connect(this.url, this.onOpen);
145
- // Register reconnection on close.
146
- if (this.wsApi.websocket) {
147
- this.wsApi.websocket.onclose = () => {
148
- this.onClose();
149
- };
122
+ getFullTrajectory(id, mode, generalizationLevel) {
123
+ let suffix = '';
124
+ if (this.version === '1') {
125
+ suffix = getModeSuffix(mode, RealtimeModes);
126
+ }
127
+ const channel = [`full_trajectory${suffix}`];
128
+ if (id) {
129
+ channel.push(id);
150
130
  }
131
+ if ((!mode || mode === RealtimeModes.TOPOGRAPHIC) && generalizationLevel) {
132
+ channel.push(`gen${generalizationLevel}`);
133
+ }
134
+ return this.get(channel.join('_'));
151
135
  }
152
136
  /**
153
- * Close the websocket connection without reconnection.
137
+ * Return a station with a given uic number and a mode.
154
138
  *
139
+ * @param {number} uic UIC of the station.
140
+ * @param {RealtimeMode} mode Realtime mode.
141
+ * @return {Promise<{data: { content: RealtimeStation }}>} A station.
155
142
  * @public
156
143
  */
157
- close() {
158
- this.wsApi.close();
144
+ getStation(uic, mode) {
145
+ const params = {
146
+ args: uic,
147
+ channel: `station${getModeSuffix(mode, RealtimeModes)}`,
148
+ };
149
+ return this.get(params);
159
150
  }
160
151
  /**
161
- * Unsubscribe trajectory and deleted_vehicles channels. To resubscribe you have to set a new BBOX.
152
+ * Get the list of ststions available for a specifc mode. The promise is resolved every 100ms
153
+ * @param {RealtimeMode} mode Realtime mode.
154
+ * @param {number} timeout = 100 Duration in ms between each promise resolve calls.
155
+ * @return {Promise<RealtimeStation[]>} An array of stations.
156
+ * @public
162
157
  */
163
- // eslint-disable-next-line class-methods-use-this
164
- reset() {
165
- this.wsApi.send('RESET');
158
+ getStations(mode, timeout = 100) {
159
+ return new Promise((resolve) => {
160
+ this.get(`station${getModeSuffix(mode, RealtimeModes)}`).then(debounceWebsocketMessages(resolve, undefined, timeout));
161
+ });
162
+ }
163
+ /**
164
+ * Get the list of stops for this vehicle.
165
+ *
166
+ * @param {string} id A vehicle id.
167
+ * @return {Promise<{data: { content: RealtimeStopSequence[] }}>} Returns a stop sequence object.
168
+ * @public
169
+ */
170
+ getStopSequence(id) {
171
+ return this.get(`stopsequence_${id}`);
172
+ }
173
+ /**
174
+ * Return a partial trajectory with a given id and a mode.
175
+ *
176
+ * @param {number} id The identifier of a trajectory.
177
+ * @param {RealtimeMode} mode Realtime mode.
178
+ * @return {Promise<{data: { content: RealtimeTrajectory }}>} A trajectory.
179
+ * @public
180
+ */
181
+ getTrajectory(id, mode) {
182
+ return this.get(`partial_trajectory${getModeSuffix(mode, RealtimeModes)}_${id}`);
183
+ }
184
+ /**
185
+ * Callback when the websocket is closed by the server.
186
+ * It auto reconnects after a timeout.
187
+ * @private
188
+ */
189
+ onClose() {
190
+ window.clearTimeout(this.pingInterval);
191
+ window.clearTimeout(this.reconnectTimeout);
192
+ if (this.reconnectTimeoutMs) {
193
+ this.reconnectTimeout = window.setTimeout(() => this.open(), this.reconnectTimeoutMs);
194
+ }
166
195
  }
167
196
  /**
168
197
  * Callback when the websocket is opened and ready.
@@ -187,31 +216,24 @@ class RealtimeAPI {
187
216
  }
188
217
  }
189
218
  /**
190
- * Callback when the websocket is closed by the server.
191
- * It auto reconnects after a timeout.
192
- * @private
219
+ * Open the websocket connection.
220
+ *
221
+ * @public
193
222
  */
194
- onClose() {
195
- window.clearTimeout(this.pingInterval);
196
- window.clearTimeout(this.reconnectTimeout);
197
- if (this.reconnectTimeoutMs) {
198
- this.reconnectTimeout = window.setTimeout(() => this.open(), this.reconnectTimeoutMs);
223
+ open() {
224
+ this.wsApi.connect(this.url, this.onOpen.bind(this));
225
+ // Register reconnection on close.
226
+ if (this.wsApi.websocket) {
227
+ this.wsApi.websocket.onclose = () => {
228
+ this.onClose();
229
+ };
199
230
  }
200
231
  }
201
232
  /**
202
- * Send GET to a channel.
203
- *
204
- * @param {string | WebSocketAPIParameters} channelOrParams Name of the websocket channel to send GET or an object representing parameters to send
205
- * @return {Promise<WebSocketAPIMessageEventData<?>>} A websocket response.
233
+ * Unsubscribe trajectory and deleted_vehicles channels. To resubscribe you have to set a new BBOX.
206
234
  */
207
- get(channelOrParams) {
208
- let params = channelOrParams;
209
- if (typeof channelOrParams === 'string') {
210
- params = { channel: channelOrParams };
211
- }
212
- return new Promise((resolve, reject) => {
213
- this.wsApi.get(params, resolve, reject);
214
- });
235
+ reset() {
236
+ this.wsApi.send('RESET');
215
237
  }
216
238
  /**
217
239
  * Subscribe to a channel.
@@ -220,7 +242,7 @@ class RealtimeAPI {
220
242
  * @param {function} onSuccess Callback when the subscription succeeds.
221
243
  * @param {function} onError Callback when the subscription fails.
222
244
  * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
223
- * @private
245
+ * @public
224
246
  */
225
247
  subscribe(channel, onSuccess, onError = () => { }, quiet = false) {
226
248
  if (!channel || !onSuccess) {
@@ -229,87 +251,93 @@ class RealtimeAPI {
229
251
  this.wsApi.subscribe({ channel }, onSuccess, onError, quiet);
230
252
  }
231
253
  /**
232
- * Unsubscribe both modes of a channel.
254
+ * Subscribe to deleted_vhicles channel.
233
255
  *
234
- * @param {string} channel Name of the websocket channel to unsubscribe.
235
- * @param {string} suffix Suffix to add to the channel name.
236
- * @param {function} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
237
- * @private
256
+ * @param {RealtimeMode} mode Realtime mode.
257
+ * @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
258
+ * @param {function} onError Callback when the subscription fails.
259
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
260
+ * @public
238
261
  */
239
- unsubscribe(channel, suffix = '', onMessage) {
240
- const suffixSchenatic = getModeSuffix(RealtimeModes.SCHEMATIC, RealtimeModes);
241
- const suffixTopographic = getModeSuffix(RealtimeModes.TOPOGRAPHIC, RealtimeModes);
242
- this.wsApi.unsubscribe(`${channel}${suffixSchenatic}${suffix || ''}`, onMessage);
243
- this.wsApi.unsubscribe(`${channel}${suffixTopographic}${suffix || ''}`, onMessage);
262
+ subscribeDeletedVehicles(mode, onMessage, onError = () => { }, quiet = false) {
263
+ this.unsubscribeDeletedVehicles(onMessage);
264
+ let suffix = '';
265
+ if (this.version === '1') {
266
+ suffix = getModeSuffix(mode, RealtimeModes);
267
+ }
268
+ this.subscribe(`deleted_vehicles${suffix}`, onMessage, onError, quiet);
244
269
  }
245
270
  /**
246
271
  * Subscribe to departures channel of a given station.
247
272
  *
248
273
  * @param {number} stationId UIC of the station.
249
- * @param {Boolean} sortByMinArrivalTime Sort by minimum arrival time
250
- * @param {function(departures:Departure[])} onMessage Function called on each message of the channel.
274
+ * @param {function(departures: RealtimeDeparture[])} onMessage Function called on each message of the channel.
251
275
  * @param {function} onError Callback when the subscription fails.
252
276
  * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
253
- * @public
277
+ * @deprecated Use subscribeTimetable instead.
254
278
  */
255
279
  subscribeDepartures(stationId, onMessage, onError = () => { }, quiet = false) {
256
- this.subscribe(`timetable_${stationId}`, onMessage, onError, quiet);
257
- }
258
- /**
259
- * Unsubscribe from current departures channel.
260
- * @param {RealtimeStationId} id Station's id
261
- * @param {function(data: { content: RealtimeDeparture[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
262
- * @public
263
- */
264
- unsubscribeDepartures(id, onMessage) {
265
- this.unsubscribe(`timetable_${id}`, '', onMessage);
280
+ this.subscribeTimetable(stationId, onMessage, onError, quiet);
266
281
  }
267
282
  /**
268
283
  * Subscribe to the disruptions channel for tenant.
269
284
  *
285
+ * @param {RealtimeTenant} tenant Tenant's id
270
286
  * @param {function(data: { content: RealtimeNews[] })} onMessage Function called on each message of the channel.
271
287
  * @param {function} onError Callback when the subscription fails.
272
288
  * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
273
- * @public
289
+ * @deprecated Use subscribeNewsticker instead.
274
290
  */
275
291
  subscribeDisruptions(tenant, onMessage, onError = () => { }, quiet = false) {
276
- this.subscribe(`${tenant}_newsticker`, onMessage, onError, quiet);
292
+ this.subscribeNewsticker(tenant, onMessage, onError, quiet);
277
293
  }
278
294
  /**
279
- * Unsubscribe disruptions.
295
+ * Subscribe to extra_geoms channel.
280
296
  *
281
- * @param {function(data: { content: RealtimeNews[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
282
- * @public
297
+ * @param {function(data: { content: RealtimeExtraGeom })} onMessage Function called on each message of the channel.
298
+ * @param {function} onError Callback when the subscription fails.
299
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
283
300
  */
284
- unsubscribeDisruptions(tenant, onMessage) {
285
- this.unsubscribe(`${tenant}_newsticker`, '', onMessage);
301
+ subscribeExtraGeoms(onMessage, onError = () => { }, quiet = false) {
302
+ this.subscribe('extra_geoms', onMessage, onError, quiet);
286
303
  }
287
304
  /**
288
- * Return a station with a given uic number and a mode.
305
+ * Subscribe to full_trajectory channel of a given vehicle.
289
306
  *
290
- * @param {number} uic UIC of the station.
307
+ * @param {string} id A vehicle id.
291
308
  * @param {RealtimeMode} mode Realtime mode.
292
- * @return {Promise<{data: { content: RealtimeStation }}>} A station.
309
+ * @param {function(data:{content: RealtimeFullTrajectory})} onMessage Function called on each message of the channel.
310
+ * @param {function} onError Callback when the subscription fails.
311
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
293
312
  * @public
294
313
  */
295
- getStation(uic, mode) {
296
- const params = {
297
- channel: `station${getModeSuffix(mode, RealtimeModes)}`,
298
- args: uic,
299
- };
300
- return this.get(params);
314
+ subscribeFullTrajectory(id, mode, onMessage, onError = () => { }, quiet = false) {
315
+ let suffix = '';
316
+ if (this.version === '1') {
317
+ suffix = getModeSuffix(mode, RealtimeModes);
318
+ }
319
+ this.subscribe(`full_trajectory${suffix}_${id}`, onMessage, onError, quiet);
301
320
  }
302
321
  /**
303
- * Get the list of ststions available for a specifc mode. The promise is resolved every 100ms
304
- * @param {RealtimeMode} mode Realtime mode.
305
- * @param {number} timeout = 100 Duration in ms between each promise resolve calls.
306
- * @return {Promise<RealtimeStation[]>} An array of stations.
322
+ * Subscribe to healthcheck channel.
323
+ * @param {function(data: { content: string })} onMessage Callback when the subscribe to healthcheck channel succeeds.
324
+ * @param {function} onError Callback when the subscription fails.
325
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
326
+ */
327
+ subscribeHealthCheck(onMessage, onError = () => { }, quiet = false) {
328
+ this.subscribe('healthcheck', onMessage, onError, quiet);
329
+ }
330
+ /**
331
+ * Subscribe to the newsticker channel for tenant.
332
+ *
333
+ * @param {RealtimeTenant} tenant Tenant's id
334
+ * @param {function(data: { content: RealtimeNews[] })} onMessage Function called on each message of the channel.
335
+ * @param {function} onError Callback when the subscription fails.
336
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
307
337
  * @public
308
338
  */
309
- getStations(mode, timeout = 100) {
310
- return new Promise((resolve) => {
311
- this.get(`station${getModeSuffix(mode, RealtimeModes)}`).then(debounceWebsocketMessages(resolve, undefined, timeout));
312
- });
339
+ subscribeNewsticker(tenant, onMessage, onError = () => { }, quiet = false) {
340
+ this.subscribe(`${tenant}_newsticker`, onMessage, onError, quiet);
313
341
  }
314
342
  /**
315
343
  * Subscribe to stations channel.
@@ -325,46 +353,34 @@ class RealtimeAPI {
325
353
  this.subscribe(`station${getModeSuffix(mode, RealtimeModes)}`, onMessage, onError, quiet);
326
354
  }
327
355
  /**
328
- * Unsubscribe to stations channel.
329
- * @param {function(data: { content: RealtimeStation })} onMessage The listener callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribe.
330
- * @public
331
- */
332
- unsubscribeStations(onMessage) {
333
- this.unsubscribe('station', '', onMessage);
334
- }
335
- /**
336
- * Subscribe to extra_geoms channel.
356
+ * Subscribe to stopsequence channel of a given vehicle.
337
357
  *
338
- * @param {function(data: { content: RealtimeExtraGeom })} onMessage Function called on each message of the channel.
358
+ * @param {string} id A vehicle id.
359
+ * @param {function(data: { content: RealtimeStopSequence[] })} onMessage Function called on each message of the channel.
339
360
  * @param {function} onError Callback when the subscription fails.
340
361
  * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
362
+ * @public
341
363
  */
342
- subscribeExtraGeoms(onMessage, onError = () => { }, quiet = false) {
343
- this.subscribe('extra_geoms', onMessage, onError, quiet);
344
- }
345
- /**
346
- * Unsubscribe to extra_geoms channel.
347
- * @param {function(data: { content: RealtimeExtraGeom })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
348
- */
349
- unsubscribeExtraGeoms(onMessage) {
350
- this.unsubscribe('extra_geoms', '', onMessage);
364
+ subscribeStopSequence(id, onMessage, onError = () => { }, quiet = false) {
365
+ this.subscribe(`stopsequence_${id}`, onMessage, onError, quiet);
351
366
  }
352
367
  /**
353
- * Return a partial trajectory with a given id and a mode.
368
+ * Subscribe to timetable channel of a given station.
354
369
  *
355
- * @param {number} trainId The identifier of a trajectory.
356
- * @param {RealtimeMode} mode Realtime mode.
357
- * @return {Promise<{data: { content: RealtimeTrajectory }}>} A trajectory.
370
+ * @param {number} stationId UIC of the station.
371
+ * @param {function(departures: RealtimeDeparture[])} onMessage Function called on each message of the channel.
372
+ * @param {function} onError Callback when the subscription fails.
373
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
358
374
  * @public
359
375
  */
360
- getTrajectory(id, mode) {
361
- return this.get(`partial_trajectory${getModeSuffix(mode, RealtimeModes)}_${id}`);
376
+ subscribeTimetable(stationId, onMessage, onError = () => { }, quiet = false) {
377
+ this.subscribe(`timetable_${stationId}`, onMessage, onError, quiet);
362
378
  }
363
379
  /**
364
380
  * Subscribe to trajectory channel.
365
381
  *
366
382
  * @param {RealtimeMode} mode Realtime mode.
367
- * @param {function(data: { content: RealtimeTrajectoryResponse[] })} onMessage Function called on each message of the channel.
383
+ * @param {function(data: { content: RealtimeTrajectory })} onMessage Function called on each message of the channel.
368
384
  * @param {function} onError Callback when the subscription fails.
369
385
  * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
370
386
  * @public
@@ -378,133 +394,183 @@ class RealtimeAPI {
378
394
  this.subscribe(`trajectory${suffix}`, onMessage, onError, quiet);
379
395
  }
380
396
  /**
381
- * Unsubscribe to trajectory channels.
382
- * @param {function(data: { content: RealtimeTrajectoryResponse[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
383
- * @public
384
- */
385
- unsubscribeTrajectory(onMessage) {
386
- this.unsubscribe(`trajectory`, '', onMessage);
387
- }
388
- /**
389
- * Subscribe to deleted_vhicles channel.
397
+ * Unsubscribe both modes of a channel.
390
398
  *
391
- * @param {RealtimeMode} mode Realtime mode.
392
- * @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
393
- * @param {function} onError Callback when the subscription fails.
394
- * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
399
+ * @param {string} channel Name of the websocket channel to unsubscribe.
400
+ * @param {string} suffix Suffix to add to the channel name.
401
+ * @param {function} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
402
+ * @public
395
403
  */
396
- subscribeDeletedVehicles(mode, onMessage, onError = () => { }, quiet = false) {
397
- this.unsubscribeDeletedVehicles(onMessage);
398
- let suffix = '';
399
- if (this.version === '1') {
400
- suffix = getModeSuffix(mode, RealtimeModes);
401
- }
402
- this.subscribe(`deleted_vehicles${suffix}`, onMessage, onError, quiet);
404
+ unsubscribe(channel, suffix = '', onMessage) {
405
+ const suffixSchenatic = getModeSuffix(RealtimeModes.SCHEMATIC, RealtimeModes);
406
+ const suffixTopographic = getModeSuffix(RealtimeModes.TOPOGRAPHIC, RealtimeModes);
407
+ this.wsApi.unsubscribe(`${channel}${suffixSchenatic}${suffix || ''}`, onMessage);
408
+ this.wsApi.unsubscribe(`${channel}${suffixTopographic}${suffix || ''}`, onMessage);
403
409
  }
404
410
  /**
405
411
  * Unsubscribe to deleted_vhicles channels.
406
412
  * @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
413
+ * @public
407
414
  */
408
415
  unsubscribeDeletedVehicles(onMessage) {
409
416
  this.unsubscribe('deleted_vehicles', '', onMessage);
410
417
  }
411
418
  /**
412
- * Get a full trajectory of a vehicule .
413
- *
414
- * @param {string} id A vehicle id.
415
- * @param {RealtimeMode} mode Realtime mode.
416
- * @param {string} generalizationLevel The generalization level to request. Can be one of 5 (more generalized), 10, 30, 100, undefined (less generalized).
417
- * @return {Promise<{ data: { content: RealtimeFullTrajectory } }>} Return a full trajectory.
418
- * @public
419
+ * Unsubscribe from current departures channel.
420
+ * @param {number} stationId UIC of the station.
421
+ * @param {function(data: { content: RealtimeDeparture[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
422
+ * @deprecated Use RealtimeAPI.unsubscribeTimetabe instead.
419
423
  */
420
- getFullTrajectory(id, mode, generalizationLevel) {
421
- let suffix = '';
422
- if (this.version === '1') {
423
- suffix = getModeSuffix(mode, RealtimeModes);
424
- }
425
- const channel = [`full_trajectory${suffix}`];
426
- if (id) {
427
- channel.push(id);
428
- }
429
- if ((!mode || mode === RealtimeModes.TOPOGRAPHIC) && generalizationLevel) {
430
- channel.push(`gen${generalizationLevel}`);
431
- }
432
- return this.get(channel.join('_'));
424
+ unsubscribeDepartures(stationId, onMessage) {
425
+ this.unsubscribeTimetabe(stationId, onMessage);
433
426
  }
434
427
  /**
435
- * Subscribe to full_trajectory channel of a given vehicle.
436
- *
437
- * @param {string} id A vehicle id.
438
- * @param {RealtimeMode} mode Realtime mode.
439
- * @param {function(data: { content: RealtimeFullTrajectory })} onMessage Function called on each message of the channel.
440
- * @param {function} onError Callback when the subscription fails.
441
- * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
442
- * @public
428
+ * Unsubscribe disruptions.
429
+ * @param {RealtimeTenant} tenant Tenant's id
430
+ * @param {Function(data: { content: RealtimeNews[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
431
+ * @deprecated Use unsubscribeNewsticker instead.
443
432
  */
444
- subscribeFullTrajectory(id, mode, onMessage, onError = () => { }, quiet = false) {
445
- let suffix = '';
446
- if (this.version === '1') {
447
- suffix = getModeSuffix(mode, RealtimeModes);
448
- }
449
- this.subscribe(`full_trajectory${suffix}_${id}`, onMessage, onError, quiet);
433
+ unsubscribeDisruptions(tenant, onMessage) {
434
+ this.unsubscribeNewsticker(tenant, onMessage);
435
+ }
436
+ /**
437
+ * Unsubscribe to extra_geoms channel.
438
+ * @param {function(data: { content: RealtimeExtraGeom })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
439
+ */
440
+ unsubscribeExtraGeoms(onMessage) {
441
+ this.unsubscribe('extra_geoms', '', onMessage);
450
442
  }
451
443
  /**
452
444
  * Unsubscribe from full_trajectory channel
453
445
  *
454
446
  * @param {string} id A vehicle id.
455
- * @param {function(data: { content: RealtimeFullTrajectory })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
447
+ * @param {onFullTrajectoryMessageCallback} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
456
448
  * @public
457
449
  */
458
450
  unsubscribeFullTrajectory(id, onMessage) {
459
451
  this.unsubscribe('full_trajectory', `_${id}`, onMessage);
460
452
  }
461
453
  /**
462
- * Get the list of stops for this vehicle.
463
- *
464
- * @param {string} id A vehicle id.
465
- * @return {Promise<{ data: { content: StopSequence[] } }>} Returns a stop sequence object.
454
+ * Unsubscribe to healthcheck channel.
455
+ * @param {function(data: { content: string })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
456
+ */
457
+ unsubscribeHealthCheck(onMessage) {
458
+ this.unsubscribe('healthcheck', '', onMessage);
459
+ }
460
+ /**
461
+ * Unsubscribe disruptions.
462
+ * @param {RealtimeTenant} tenant Tenant's id
463
+ * @param {Function(data: { content: RealtimeNews[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
466
464
  * @public
467
465
  */
468
- getStopSequence(id) {
469
- return this.get(`stopsequence_${id}`);
466
+ unsubscribeNewsticker(tenant, onMessage) {
467
+ this.unsubscribe(`${tenant}_newsticker`, '', onMessage);
470
468
  }
471
469
  /**
472
- * Subscribe to stopsequence channel of a given vehicle.
473
- *
474
- * @param {string} id A vehicle id.
475
- * @param {function(data: { content: StopSequence[] })} onMessage Function called on each message of the channel.
476
- * @param {function} onError Callback when the subscription fails.
477
- * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
470
+ * Unsubscribe to stations channel.
471
+ * @param {function(data: { content: RealtimeStation })} onMessage The listener callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribe.
478
472
  * @public
479
473
  */
480
- subscribeStopSequence(id, onMessage, onError = () => { }, quiet = false) {
481
- this.subscribe(`stopsequence_${id}`, onMessage, onError, quiet);
474
+ unsubscribeStations(onMessage) {
475
+ this.unsubscribe('station', '', onMessage);
482
476
  }
483
477
  /**
484
478
  * Unsubscribe from stopsequence channel
485
479
  *
486
480
  * @param {string} id A vehicle id.
487
- * @param {function(data: { content: StopSequence[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
481
+ * @param {function(data: { content: RealtimeStopSequence[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
488
482
  * @public
489
483
  */
490
484
  unsubscribeStopSequence(id, onMessage) {
491
485
  this.unsubscribe(`stopsequence`, `_${id}`, onMessage);
492
486
  }
493
487
  /**
494
- * Subscribe to healthcheck channel.
495
- * @param {function(data: { content: string })} onMessage Callback when the subscribe to healthcheck channel succeeds.
496
- * @param {function} onError Callback when the subscription fails.
497
- * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
488
+ * Unsubscribe from current departures channel.
489
+ * @param {number} stationId UIC of the station.
490
+ * @param {function(data: { content: RealtimeDeparture[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
491
+ * @public
498
492
  */
499
- subscribeHealthCheck(onMessage, onError = () => { }, quiet = false) {
500
- this.subscribe('healthcheck', onMessage, onError, quiet);
493
+ unsubscribeTimetabe(stationId, onMessage) {
494
+ this.unsubscribe(`timetable_${stationId}`, '', onMessage);
501
495
  }
502
496
  /**
503
- * Unsubscribe to healthcheck channel.
504
- * @param {function(data: { content: string })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
497
+ * Unsubscribe to trajectory channels.
498
+ * @param {function(data: { content: RealtimeTrajectory })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
499
+ * @public
505
500
  */
506
- unsubscribeHealthCheck(onMessage) {
507
- this.unsubscribe('healthcheck', '', onMessage);
501
+ unsubscribeTrajectory(onMessage) {
502
+ this.unsubscribe(`trajectory`, '', onMessage);
503
+ }
504
+ /**
505
+ * This callback type is called `requestCallback` and is displayed as a global symbol.
506
+ *
507
+ * @callback onFullTrajectoryMessageCallback
508
+ * @param {number} responseCode
509
+ * @param {string} responseMessage
510
+ */
511
+ /**
512
+ * The bounding box to receive data from.\
513
+ * Example: [minX, minY, maxX, maxY, zoom, mots , gen_level, tenant, ...]\
514
+ * &nbsp;\
515
+ * Where:
516
+ * - **minX**: a string representation of an integer (not a float) representing the minimal X coordinate (in EPSG:3857) of a bounding box\
517
+ * &nbsp;
518
+ * - **minY**: a string representation of an integer (not a float) representing the minimal Y coordinate (in EPSG:3857) of a bounding box\
519
+ * &nbsp;
520
+ * - **maxX**: a string representation of an integer (not a float) representing the maximal X coordinate (in EPSG:3857) of a bounding box\
521
+ * &nbsp;
522
+ * - **maxY**: a string representation of an integer (not a float) representing the maximal Y coordinate (in EPSG:3857) of a bounding box\
523
+ * &nbsp;
524
+ * - **zoom**: a string representation of an integer representing the zoom level (from 4 to 22). When zoom < 8 only the trains are displayed for performance reasons.\
525
+ * &nbsp;
526
+ * - **mots**: A comma separated list of modes of transport. **Optional**.\
527
+ * Example: "mots=rail,subway".\
528
+ * &nbsp;
529
+ * - **gen_level**: An integer representing the generalization level. **Optional**.\
530
+ * Example: "gen_level=5"\
531
+ * &nbsp;
532
+ * - **tenant**: A string representing the tenant. **Optional**.\
533
+ * Example: "tenant=sbb"\
534
+ * &nbsp;
535
+ * - ...: Any other values added to the bbox will be send to the server
536
+ *
537
+ * @type {string[]}
538
+ *
539
+ * @public
540
+ */
541
+ get bbox() {
542
+ return this._bbox;
543
+ }
544
+ set bbox(newBbox) {
545
+ if (JSON.stringify(newBbox) !== JSON.stringify(this._bbox)) {
546
+ this._bbox = newBbox;
547
+ if (this.wsApi && this._bbox) {
548
+ this.wsApi.send(`BBOX ${this._bbox.join(' ')}`);
549
+ }
550
+ }
551
+ }
552
+ get buffer() {
553
+ return this._buffer;
554
+ }
555
+ set buffer(newBuffer) {
556
+ if (JSON.stringify(newBuffer) !== JSON.stringify(this._buffer)) {
557
+ this._buffer = newBuffer;
558
+ if (this.wsApi && this._buffer) {
559
+ this.wsApi.send(`BUFFER ${this._buffer.join(' ')}`);
560
+ }
561
+ }
562
+ }
563
+ get url() {
564
+ return this._url;
565
+ }
566
+ set url(newUrl) {
567
+ if (this._url !== newUrl) {
568
+ this._url = newUrl;
569
+ // Update the websocket only if the url has changed and the websocket is already open or is opening.
570
+ if (this.wsApi.open || this.wsApi.connecting) {
571
+ this.open();
572
+ }
573
+ }
508
574
  }
509
575
  }
510
576
  export default RealtimeAPI;