mobility-toolbox-js 2.0.0-beta.8 → 2.0.1-beta.13

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 (304) hide show
  1. package/api/RoutingAPI.js +15 -0
  2. package/api/RoutingAPI.test.js +25 -0
  3. package/api/StopsAPI.js +12 -0
  4. package/api/StopsAPI.test.js +22 -0
  5. package/api/TralisAPI.js +359 -0
  6. package/api/TralisAPI.test.js +67 -0
  7. package/{src/api/tralis → api}/TralisAPIUtils.js +2 -32
  8. package/api/index.js +3 -0
  9. package/{index.html → api/typedefs.js} +0 -0
  10. package/common/Tracker.js +93 -0
  11. package/common/api/HttpAPI.js +30 -0
  12. package/common/api/HttpAPI.test.js +50 -0
  13. package/common/api/WebSocketAPI.js +175 -0
  14. package/{src/api/tralis/WebSocketConnector.test.js → common/api/WebSocketAPI.test.js} +100 -145
  15. package/common/controls/Control.js +81 -0
  16. package/{src/common → common}/controls/Control.test.js +32 -43
  17. package/common/index.js +4 -0
  18. package/common/layers/Layer.js +213 -0
  19. package/{src/common → common}/layers/Layer.test.js +185 -244
  20. package/common/mixins/CopyrightMixin.js +24 -0
  21. package/common/mixins/SearchMixin.js +110 -0
  22. package/common/mixins/TralisLayerMixin.js +479 -0
  23. package/common/styles/index.js +4 -0
  24. package/{src/common → common}/styles/trackerDefaultStyle.js +39 -175
  25. package/common/styles/trackerDelayStyle.js +8 -0
  26. package/{src/common → common}/styles/trackerSimpleStyle.js +4 -8
  27. package/{src/common → common}/trackerConfig.js +61 -99
  28. package/common/trackerConfig.test.js +23 -0
  29. package/{src/doc/examples/ol-mapbox-layer.md → common/typedefs.js} +0 -0
  30. package/common/utils/createTrackerFilters.js +56 -0
  31. package/common/utils/createTrackerFilters.test.js +79 -0
  32. package/{src/common → common}/utils/getMapboxMapCopyrights.js +3 -16
  33. package/common/utils/getMapboxMapCopyrights.test.js +40 -0
  34. package/{src/common → common}/utils/getMapboxStyleUrl.js +3 -13
  35. package/{src/common → common}/utils/getVehiclePosition.js +3 -33
  36. package/common/utils/index.js +5 -0
  37. package/common/utils/removeDuplicate.js +8 -0
  38. package/common/utils/removeDuplicate.test.js +19 -0
  39. package/{src/common → common}/utils/sortByDelay.js +2 -7
  40. package/common/utils/timeUtils.js +20 -0
  41. package/common/utils/timeUtils.test.js +10 -0
  42. package/index.js +8 -0
  43. package/mapbox/controls/CopyrightControl.js +29 -0
  44. package/mapbox/controls/index.js +1 -0
  45. package/mapbox/index.js +4 -0
  46. package/mapbox/layers/Layer.js +57 -0
  47. package/mapbox/layers/Layer.test.js +182 -0
  48. package/mapbox/layers/TralisLayer.js +182 -0
  49. package/{src/mapbox → mapbox}/layers/TralisLayer.test.js +12 -14
  50. package/mapbox/layers/index.js +2 -0
  51. package/{src/mapbox → mapbox}/utils.js +7 -21
  52. package/mbt.js +50444 -0
  53. package/mbt.js.map +7 -0
  54. package/mbt.min.js +1005 -0
  55. package/mbt.min.js.map +7 -0
  56. package/ol/controls/CopyrightControl.js +42 -0
  57. package/ol/controls/CopyrightControl.test.js +165 -0
  58. package/ol/controls/RoutingControl.js +387 -0
  59. package/ol/controls/RoutingControl.test.js +151 -0
  60. package/ol/controls/StopFinderControl.js +10 -0
  61. package/ol/controls/StopFinderControl.test.js +48 -0
  62. package/ol/controls/index.js +3 -0
  63. package/ol/index.js +5 -0
  64. package/ol/layers/Layer.js +88 -0
  65. package/ol/layers/Layer.test.js +174 -0
  66. package/ol/layers/MapboxLayer.js +203 -0
  67. package/{src/ol → ol}/layers/MapboxLayer.test.js +58 -84
  68. package/ol/layers/MapboxStyleLayer.js +187 -0
  69. package/{src/ol → ol}/layers/MapboxStyleLayer.test.js +97 -128
  70. package/ol/layers/MaplibreLayer.js +139 -0
  71. package/ol/layers/RoutingLayer.js +61 -0
  72. package/{src/ol → ol}/layers/RoutingLayer.test.js +15 -24
  73. package/ol/layers/TralisLayer.js +185 -0
  74. package/ol/layers/TralisLayer.test.js +79 -0
  75. package/ol/layers/VectorLayer.js +22 -0
  76. package/{src/ol → ol}/layers/VectorLayer.test.js +34 -45
  77. package/ol/layers/WMSLayer.js +38 -0
  78. package/ol/layers/WMSLayer.test.js +76 -0
  79. package/ol/layers/index.js +8 -0
  80. package/{src/ol → ol}/styles/fullTrajectoryDelayStyle.js +11 -15
  81. package/ol/styles/fullTrajectoryStyle.js +43 -0
  82. package/ol/styles/index.js +2 -0
  83. package/package.json +34 -74
  84. package/.babelrc +0 -6
  85. package/.esdoc.json +0 -17
  86. package/.eslintignore +0 -1
  87. package/.github/workflows/conventional-pr-title.yml +0 -21
  88. package/.github/workflows/cypress.yml +0 -29
  89. package/.github/workflows/nodejs.yml +0 -28
  90. package/.husky/commit-msg +0 -4
  91. package/.husky/post-checkout +0 -4
  92. package/.husky/post-merge +0 -4
  93. package/.husky/post-rebase +0 -4
  94. package/.husky/pre-commit +0 -4
  95. package/.neutrinorc.js +0 -176
  96. package/.nvmrc +0 -1
  97. package/CHANGELOG.md +0 -10
  98. package/LICENSE +0 -21
  99. package/__mocks__/mapbox-gl.js +0 -81
  100. package/__mocks__/maplibre-gl.js +0 -81
  101. package/commitlint.config.js +0 -1
  102. package/cypress/fixtures/example.json +0 -5
  103. package/cypress/integration/examples/api.spec.js +0 -7
  104. package/cypress/integration/examples/examples.spec.js +0 -7
  105. package/cypress/integration/examples/navigation.spec.js +0 -29
  106. package/cypress/plugins/index.js +0 -21
  107. package/cypress/support/commands.js +0 -25
  108. package/cypress/support/index.js +0 -20
  109. package/cypress.json +0 -4
  110. package/data/fetchRoute.json +0 -292
  111. package/data/fetchTrajectories.json +0 -18
  112. package/data/fetchTrajectoryById.json +0 -3
  113. package/data/fetchTrajectoryStations.json +0 -18
  114. package/data/stopsSearch.json +0 -15
  115. package/documentation.yml +0 -4
  116. package/esdoc/README.md +0 -27
  117. package/esdoc/plugins/MyPlugin.js +0 -69
  118. package/esdoc/plugins/dynamic-property-plugin/Plugin.js +0 -50
  119. package/esdoc/plugins/externals-plugin/Plugin.js +0 -45
  120. package/esdoc/plugins/externals-plugin/externals.js +0 -96
  121. package/global-setup.js +0 -3
  122. package/indexweb.html +0 -49
  123. package/jest.config.js +0 -5
  124. package/pull_request_template.md +0 -17
  125. package/renovate.json +0 -4
  126. package/scripts/read-pkg-json.js +0 -22
  127. package/src/api/index.js +0 -3
  128. package/src/api/routing/RoutingAPI.js +0 -44
  129. package/src/api/routing/RoutingAPI.test.js +0 -41
  130. package/src/api/stops/StopsAPI.js +0 -41
  131. package/src/api/stops/StopsAPI.test.js +0 -34
  132. package/src/api/tralis/TralisAPI.js +0 -731
  133. package/src/api/tralis/TralisAPI.test.js +0 -75
  134. package/src/api/tralis/WebSocketConnector.js +0 -338
  135. package/src/api/tralis/typedefs.js +0 -81
  136. package/src/assets/Lato-Black.ttf +0 -0
  137. package/src/assets/Lato-BlackItalic.ttf +0 -0
  138. package/src/assets/Lato-Bold.ttf +0 -0
  139. package/src/assets/Lato-BoldItalic.ttf +0 -0
  140. package/src/assets/Lato-Italic.ttf +0 -0
  141. package/src/assets/Lato-Light.ttf +0 -0
  142. package/src/assets/Lato-LightItalic.ttf +0 -0
  143. package/src/assets/Lato-Regular.ttf +0 -0
  144. package/src/assets/Lato-Thin.ttf +0 -0
  145. package/src/assets/Lato-ThinItalic.ttf +0 -0
  146. package/src/assets/OFL.txt +0 -93
  147. package/src/common/Tracker.js +0 -197
  148. package/src/common/api/api.js +0 -64
  149. package/src/common/api/api.test.js +0 -68
  150. package/src/common/controls/Control.js +0 -146
  151. package/src/common/index.js +0 -5
  152. package/src/common/layers/Layer.js +0 -404
  153. package/src/common/mixins/CopyrightMixin.js +0 -48
  154. package/src/common/mixins/SearchMixin.js +0 -176
  155. package/src/common/mixins/TralisLayerMixin.js +0 -930
  156. package/src/common/styles/index.js +0 -4
  157. package/src/common/styles/trackerDelayStyle.js +0 -17
  158. package/src/common/trackerConfig.test.js +0 -25
  159. package/src/common/typedefs.js +0 -23
  160. package/src/common/utils/createTrackerFilters.js +0 -87
  161. package/src/common/utils/createTrackerFilters.test.js +0 -95
  162. package/src/common/utils/getMapboxMapCopyrights.test.js +0 -47
  163. package/src/common/utils/index.js +0 -5
  164. package/src/common/utils/removeDuplicate.js +0 -22
  165. package/src/common/utils/removeDuplicate.test.js +0 -22
  166. package/src/common/utils/timeUtils.js +0 -44
  167. package/src/common/utils/timeUtils.test.js +0 -16
  168. package/src/doc/App.js +0 -116
  169. package/src/doc/App.scss +0 -51
  170. package/src/doc/_redirects +0 -2
  171. package/src/doc/components/CodeSandboxButton.js +0 -103
  172. package/src/doc/components/Documentation.js +0 -40
  173. package/src/doc/components/Esdoc/Anchor.js +0 -57
  174. package/src/doc/components/Esdoc/ClassDoc.js +0 -272
  175. package/src/doc/components/Esdoc/DeprecatedHTML.js +0 -16
  176. package/src/doc/components/Esdoc/DetailDocs.js +0 -281
  177. package/src/doc/components/Esdoc/DetailHTML.js +0 -33
  178. package/src/doc/components/Esdoc/DirectSubclassHTML.js +0 -30
  179. package/src/doc/components/Esdoc/DocBuilderUtils.js +0 -694
  180. package/src/doc/components/Esdoc/DocLinkHTML.js +0 -62
  181. package/src/doc/components/Esdoc/DocsLinkHTML.js +0 -38
  182. package/src/doc/components/Esdoc/Esdoc.js +0 -63
  183. package/src/doc/components/Esdoc/EsdocContent.js +0 -51
  184. package/src/doc/components/Esdoc/EsdocNavigation.js +0 -13
  185. package/src/doc/components/Esdoc/EsdocSearch.js +0 -81
  186. package/src/doc/components/Esdoc/ExperimentalHTML.js +0 -17
  187. package/src/doc/components/Esdoc/ExtendsChainHTML.js +0 -32
  188. package/src/doc/components/Esdoc/FileDocLinkHTML.js +0 -60
  189. package/src/doc/components/Esdoc/IdentifiersDoc.js +0 -113
  190. package/src/doc/components/Esdoc/IndirectSubclassHTML.js +0 -30
  191. package/src/doc/components/Esdoc/InheritedSummaryDoc.js +0 -70
  192. package/src/doc/components/Esdoc/InheritedSummaryHTML.js +0 -38
  193. package/src/doc/components/Esdoc/MixinClassesHTML.js +0 -29
  194. package/src/doc/components/Esdoc/NavDoc.js +0 -112
  195. package/src/doc/components/Esdoc/OverrideMethod.js +0 -44
  196. package/src/doc/components/Esdoc/OverrideMethodDescription.js +0 -35
  197. package/src/doc/components/Esdoc/Properties.js +0 -89
  198. package/src/doc/components/Esdoc/README.md +0 -45
  199. package/src/doc/components/Esdoc/SignatureHTML.js +0 -123
  200. package/src/doc/components/Esdoc/SingleDoc.js +0 -31
  201. package/src/doc/components/Esdoc/SummaryDoc.js +0 -160
  202. package/src/doc/components/Esdoc/SummaryHTML.js +0 -96
  203. package/src/doc/components/Esdoc/TypeDocLinkHTML.js +0 -249
  204. package/src/doc/components/Esdoc/css/identifiers.css +0 -38
  205. package/src/doc/components/Esdoc/css/search.css +0 -76
  206. package/src/doc/components/Esdoc/css/style.css +0 -603
  207. package/src/doc/components/Esdoc/index.js +0 -7
  208. package/src/doc/components/Example.js +0 -153
  209. package/src/doc/components/Examples.js +0 -183
  210. package/src/doc/components/Home.js +0 -106
  211. package/src/doc/components/TrackerExample.js +0 -38
  212. package/src/doc/examples/assets/tralis-live-map/index.js +0 -11
  213. package/src/doc/examples/assets/tralis-live-map/s1kreis.svg +0 -105
  214. package/src/doc/examples/assets/tralis-live-map/s20kreis.svg +0 -101
  215. package/src/doc/examples/assets/tralis-live-map/s2kreis.svg +0 -95
  216. package/src/doc/examples/assets/tralis-live-map/s3kreis.svg +0 -95
  217. package/src/doc/examples/assets/tralis-live-map/s4kreis.svg +0 -95
  218. package/src/doc/examples/assets/tralis-live-map/s6kreis.svg +0 -95
  219. package/src/doc/examples/assets/tralis-live-map/s7kreis.svg +0 -95
  220. package/src/doc/examples/assets/tralis-live-map/s8kreis.svg +0 -93
  221. package/src/doc/examples/assets/tralis-live-map/unknown.svg +0 -107
  222. package/src/doc/examples/mb-copyright.html +0 -26
  223. package/src/doc/examples/mb-copyright.js +0 -37
  224. package/src/doc/examples/mb-tracker.html +0 -1
  225. package/src/doc/examples/mb-tracker.js +0 -39
  226. package/src/doc/examples/mb-tracker.md +0 -1
  227. package/src/doc/examples/mb-tralis.html +0 -1
  228. package/src/doc/examples/mb-tralis.js +0 -34
  229. package/src/doc/examples/ol-copyright.html +0 -26
  230. package/src/doc/examples/ol-copyright.js +0 -43
  231. package/src/doc/examples/ol-mapbox-layer.html +0 -1
  232. package/src/doc/examples/ol-mapbox-layer.js +0 -28
  233. package/src/doc/examples/ol-mapbox-style-layer.html +0 -12
  234. package/src/doc/examples/ol-mapbox-style-layer.js +0 -44
  235. package/src/doc/examples/ol-query.html +0 -32
  236. package/src/doc/examples/ol-query.js +0 -83
  237. package/src/doc/examples/ol-routing.html +0 -26
  238. package/src/doc/examples/ol-routing.js +0 -59
  239. package/src/doc/examples/ol-routing.md +0 -1
  240. package/src/doc/examples/ol-stop-finder.html +0 -15
  241. package/src/doc/examples/ol-stop-finder.js +0 -31
  242. package/src/doc/examples/ol-stop-finder.md +0 -1
  243. package/src/doc/examples/ol-tracker.html +0 -1
  244. package/src/doc/examples/ol-tracker.js +0 -44
  245. package/src/doc/examples/ol-tracker.md +0 -1
  246. package/src/doc/examples/ol-tralis.html +0 -5
  247. package/src/doc/examples/ol-tralis.js +0 -57
  248. package/src/doc/examples/tralis-live-map.html +0 -1
  249. package/src/doc/examples/tralis-live-map.js +0 -51
  250. package/src/doc/examples/tralis-live-map.md +0 -3
  251. package/src/doc/examples.js +0 -107
  252. package/src/doc/img/examples/live_tracker_mb.jpg +0 -0
  253. package/src/doc/img/examples/live_tracker_munich.jpg +0 -0
  254. package/src/doc/img/examples/live_tracker_ol.jpg +0 -0
  255. package/src/doc/img/examples/mapbox.jpg +0 -0
  256. package/src/doc/img/examples/mapbox_style.jpg +0 -0
  257. package/src/doc/img/examples/ol-copyright.png +0 -0
  258. package/src/doc/img/examples/query_objects.jpg +0 -0
  259. package/src/doc/img/examples/routing.jpg +0 -0
  260. package/src/doc/img/examples/simple_map.jpg +0 -0
  261. package/src/doc/img/examples/stops.jpg +0 -0
  262. package/src/doc/img/favico.ico +0 -0
  263. package/src/doc/index.js +0 -21
  264. package/src/iife.js +0 -7
  265. package/src/index.js +0 -10
  266. package/src/mapbox/controls/CopyrightControl.js +0 -58
  267. package/src/mapbox/controls/index.js +0 -2
  268. package/src/mapbox/index.js +0 -4
  269. package/src/mapbox/layers/Layer.js +0 -118
  270. package/src/mapbox/layers/Layer.test.js +0 -202
  271. package/src/mapbox/layers/TralisLayer.js +0 -329
  272. package/src/mapbox/layers/index.js +0 -2
  273. package/src/ol/README.md +0 -0
  274. package/src/ol/controls/CopyrightControl.js +0 -80
  275. package/src/ol/controls/CopyrightControl.test.js +0 -211
  276. package/src/ol/controls/RoutingControl.js +0 -752
  277. package/src/ol/controls/RoutingControl.test.js +0 -216
  278. package/src/ol/controls/StopFinderControl.js +0 -38
  279. package/src/ol/controls/StopFinderControl.test.js +0 -59
  280. package/src/ol/controls/index.js +0 -3
  281. package/src/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  282. package/src/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  283. package/src/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  284. package/src/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  285. package/src/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  286. package/src/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  287. package/src/ol/controls/snapshots/RoutingControlStation2.json +0 -49
  288. package/src/ol/index.js +0 -5
  289. package/src/ol/layers/Layer.js +0 -193
  290. package/src/ol/layers/Layer.test.js +0 -197
  291. package/src/ol/layers/MapboxLayer.js +0 -378
  292. package/src/ol/layers/MapboxStyleLayer.js +0 -417
  293. package/src/ol/layers/MaplibreLayer.js +0 -280
  294. package/src/ol/layers/RoutingLayer.js +0 -91
  295. package/src/ol/layers/TralisLayer.js +0 -359
  296. package/src/ol/layers/TralisLayer.test.js +0 -97
  297. package/src/ol/layers/VectorLayer.js +0 -43
  298. package/src/ol/layers/WMSLayer.js +0 -80
  299. package/src/ol/layers/WMSLayer.test.js +0 -84
  300. package/src/ol/layers/index.js +0 -8
  301. package/src/ol/styles/fullTrajectoryStyle.js +0 -51
  302. package/src/ol/styles/index.js +0 -2
  303. package/src/setupTests.js +0 -15
  304. package/webpack.config.js +0 -6
@@ -1,731 +0,0 @@
1
- import WebSocketConnector from './WebSocketConnector';
2
- import {
3
- getModeSuffix,
4
- cleanStopTime,
5
- compareDepartures,
6
- } from './TralisAPIUtils';
7
-
8
- /**
9
- * Enum for Tralis modes.
10
- * @readonly
11
- * @typedef {string} TralisMode
12
- * @property {string} RAW "raw"
13
- * @property {string} SCHEMATIC "schematic"
14
- * @property {string} TOPOGRAPHIC "topographic"
15
- * @enum {TralisMode}
16
- */
17
- export const TralisModes = {
18
- RAW: 'raw',
19
- TOPOGRAPHIC: 'topographic',
20
- SCHEMATIC: 'schematic',
21
- };
22
-
23
- /**
24
- * This class provides convenience methods to access to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
25
- *
26
- * @example
27
- * import { TralisAPI } from 'mobility-toolbox-js/api';
28
- *
29
- * const api = new TralisAPI({
30
- * url: "yourUrl",
31
- * apiKey: "yourApiKey"
32
- * });
33
- *
34
- * @example
35
- * import { TralisAPI } from 'mobility-toolbox-js/api';
36
- *
37
- * const api = new TralisAPI("yourUrl");
38
- */
39
- class TralisAPI {
40
- /**
41
- * Constructor
42
- *
43
- * @param {Object|string} options A string representing the url of the service or an object containing the url and the apiKey.
44
- * @param {string} options.url Url to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
45
- * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
46
- * @param {string} [options.prefix=''] Service prefix to specify tenant.
47
- * @param {string} [options.projection] The epsg code of the projection for features. Default to EPSG:3857.
48
- * @param {number[4]} [options.bbox=[minX, minY, maxX, maxY, zoom, tenant] The bounding box to receive data from.
49
- */
50
- constructor(options = {}) {
51
- this.defineProperties(options);
52
-
53
- /** @ignore */
54
- this.subscribedStationUic = null;
55
-
56
- /** @ignore */
57
- this.departureUpdateTimeout = null;
58
-
59
- /** @ignore */
60
- this.maxDepartureAge = 30;
61
-
62
- /** @ignore */
63
- this.extraGeoms = {};
64
-
65
- /** @ignore */
66
- this.prefix = options.prefix || '';
67
-
68
- /** @ignore */
69
- this.onOpen = this.onOpen.bind(this);
70
- }
71
-
72
- defineProperties(options) {
73
- let opt = options;
74
-
75
- if (typeof options === 'string') {
76
- opt = { url: options };
77
- }
78
-
79
- const { apiKey } = opt;
80
- let { url, projection, bbox, buffer = [100, 100] } = opt;
81
- const conn = new WebSocketConnector();
82
-
83
- if (apiKey) {
84
- url = `${url || 'wss://api.geops.io/tracker-ws/v1/'}?key=${apiKey}`;
85
- }
86
-
87
- Object.defineProperties(this, {
88
- url: {
89
- get: () => url,
90
- set: (newUrl) => {
91
- url = newUrl;
92
- this.open();
93
- },
94
- },
95
- projection: {
96
- get: () => projection,
97
- set: (newProjection) => {
98
- if (newProjection !== projection) {
99
- projection = newProjection;
100
- if (this.conn) {
101
- this.conn.send(`PROJECTION ${projection}`);
102
- }
103
- }
104
- },
105
- },
106
- bbox: {
107
- get: () => bbox,
108
- set: (newBbox) => {
109
- if (JSON.stringify(newBbox) !== JSON.stringify(bbox)) {
110
- bbox = newBbox;
111
- if (this.conn) {
112
- this.conn.send(`BBOX ${bbox.join(' ')}`);
113
- }
114
- }
115
- },
116
- },
117
- buffer: {
118
- get: () => buffer,
119
- set: (newBuffer) => {
120
- if (JSON.stringify(newBuffer) !== JSON.stringify(buffer)) {
121
- buffer = newBuffer;
122
- if (this.conn) {
123
- this.conn.send(`BUFFER ${buffer.join(' ')}`);
124
- }
125
- }
126
- },
127
- },
128
- /**
129
- * The websocket helper class to connect the websocket.
130
- *
131
- * @private
132
- */
133
- conn: {
134
- value: conn,
135
- writable: true,
136
- },
137
- /**
138
- * Interval between PING request in ms.
139
- * If equal to 0, no PING request are sent.
140
- * @type {number}
141
- * @private
142
- */
143
- pingIntervalMs: {
144
- value: options.pingIntervalMs || 10000,
145
- writable: true,
146
- },
147
- /**
148
- * Timeout in ms after an automatic reconnection when the websoscket has been closed by the server.
149
- * @type {number}
150
- */
151
- reconnectTimeoutMs: {
152
- value: options.pingIntervalMs || 100,
153
- writable: true,
154
- },
155
- });
156
- }
157
-
158
- open() {
159
- this.close();
160
- // Register BBOX and PROJECTION messages must be send before previous subscriptions.
161
- this.conn.connect(this.url, this.onOpen);
162
-
163
- // Register reconnection on close.
164
- this.conn.websocket.onclose = () => {
165
- this.onClose();
166
- };
167
- }
168
-
169
- /**
170
- * Close the websocket connection without reconnection.
171
- */
172
- close() {
173
- this.conn.close();
174
- }
175
-
176
- /**
177
- * Unsubscribe trajectory and deleted_vehicles channels. To resubscribe you have to set a new BBOX.
178
- */
179
- // eslint-disable-next-line class-methods-use-this
180
- reset() {
181
- this.conn.send('RESET');
182
- }
183
-
184
- /**
185
- * Callback when the websocket is opened and ready.
186
- * It applies the bbox and the projection.
187
- */
188
- onOpen() {
189
- if (this.projection) {
190
- this.conn.send(`PROJECTION ${this.projection}`);
191
- }
192
-
193
- if (this.bbox) {
194
- this.conn.send(`BBOX ${this.bbox.join(' ')}`);
195
- }
196
-
197
- if (this.buffer) {
198
- this.conn.send(`BUFFER ${this.buffer.join(' ')}`);
199
- }
200
-
201
- /**
202
- * Keep websocket alive
203
- */
204
- if (this.pingIntervalMs) {
205
- window.clearInterval(this.pingInterval);
206
- /** @ignore */
207
- this.pingInterval = setInterval(() => {
208
- this.conn.send('PING');
209
- }, this.pingIntervalMs);
210
- }
211
- }
212
-
213
- /**
214
- * Callback when the websocket is closed by the server.
215
- * It auto reconnects after a timeout.
216
- */
217
- onClose() {
218
- window.clearTimeout(this.pingInterval);
219
- window.clearTimeout(this.reconnectTimeout);
220
-
221
- if (this.reconnectTimeoutMs) {
222
- /** @ignore */
223
- this.reconnectTimeout = window.setTimeout(
224
- () => this.open(),
225
- this.reconnectTimeoutMs,
226
- );
227
- }
228
- }
229
-
230
- /**
231
- * Subscribe to a channel.
232
- *
233
- * @param {string} channel Name of the websocket channel to subscribe.
234
- * @param {function} onSuccess Callback when the subscription succeeds.
235
- * @param {function} onError Callback when the subscription fails.
236
- * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
237
- * @private
238
- */
239
- subscribe(channel, onSuccess, onError, quiet = false) {
240
- this.conn.subscribe({ channel }, onSuccess, onError, quiet);
241
- }
242
-
243
- /**
244
- * Unsubscribe both modes of a channel.
245
- *
246
- * @param {string} channel Name of the websocket channel to unsubscribe.
247
- * @param {string} suffix Suffix to add to the channel name.
248
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
249
- * @private
250
- */
251
- unsubscribe(channel, suffix, cb) {
252
- this.conn.unsubscribe(
253
- `${channel}${getModeSuffix(TralisModes.SCHEMATIC, TralisModes)}${suffix}`,
254
- cb,
255
- );
256
- this.conn.unsubscribe(
257
- `${channel}${getModeSuffix(TralisModes.TOPOGRAPHIC, TralisModes)}${
258
- suffix || ''
259
- }`,
260
- cb,
261
- );
262
- }
263
-
264
- /**
265
- * Filter departures and return an array.
266
- *
267
- * @param {Object} depObject The object containing departures by id.
268
- * @param {boolean} [sortByMinArrivalTime=false] If true sort departures by arrival time.
269
- * @return {Array<departure>} Return departures array.
270
- * @private
271
- */
272
- filterDepartures(depObject, sortByMinArrivalTime = false) {
273
- const departures = Object.keys(depObject).map((k) => depObject[k]);
274
- departures.sort((a, b) => compareDepartures(a, b, sortByMinArrivalTime));
275
-
276
- let future = new Date();
277
- future.setMinutes(future.getMinutes() + this.maxDepartureAge);
278
- future = future.getTime();
279
-
280
- let past = new Date();
281
- past.setMinutes(past.getMinutes() - this.maxDepartureAge);
282
- past = past.getTime();
283
-
284
- const departureArray = [];
285
- const platformsBoarding = [];
286
- let previousDeparture = null;
287
-
288
- for (let i = departures.length - 1; i >= 0; i -= 1) {
289
- const d = departures[i];
290
- const t = new Date(d.time).getTime();
291
-
292
- // Only show departures within the next 30 minutes
293
- if (t > past && t < future) {
294
- // If 2 trains are boarding at the same platform,
295
- // remove the older one.
296
- if (d.state === 'BOARDING') {
297
- if (platformsBoarding.indexOf(d.platform) === -1) {
298
- platformsBoarding.push(d.platform);
299
- } else {
300
- d.state = 'HIDDEN';
301
- }
302
- }
303
-
304
- // If two trains with the same line number and destinatin
305
- // and a departure difference < 1 minute, hide the second one.
306
- if (
307
- previousDeparture &&
308
- d.to[0] === previousDeparture.to[0] &&
309
- Math.abs(t - previousDeparture.time) < 1000 &&
310
- d.line.name === previousDeparture.line.name
311
- ) {
312
- d.state = 'HIDDEN';
313
- }
314
-
315
- if (/(STOP_CANCELLED|JOURNEY_CANCELLED)/.test(d.state)) {
316
- d.cancelled = true;
317
- }
318
-
319
- previousDeparture = d;
320
- previousDeparture.time = t;
321
- departureArray.unshift(d);
322
- }
323
- }
324
-
325
- return departureArray;
326
- }
327
-
328
- /**
329
- * Subscribe to departures channel of a given station.
330
- *
331
- * @param {number} stationId UIC of the station.
332
- * @param {Boolean} sortByMinArrivalTime Sort by minimum arrival time
333
- * @param {function(departures:Departure[])} onMessage Function called on each message of the channel.
334
- */
335
- subscribeDepartures(stationId, sortByMinArrivalTime, onMessage) {
336
- window.clearTimeout(this.departureUpdateTimeout);
337
- this.unsubscribeDepartures();
338
- this.subscribedStationUic = stationId;
339
- const channel = stationId ? `timetable_${stationId}` : null;
340
- const departureObject = {};
341
- this.subscribe(
342
- channel,
343
- (data) => {
344
- if (data.source === channel) {
345
- const content = data.content || {};
346
- const tDiff = new Date(content.timestamp).getTime() - Date.now();
347
- content.timediff = tDiff;
348
- departureObject[content.call_id] = content;
349
-
350
- window.clearTimeout(this.departureUpdateTimeout);
351
- this.departureUpdateTimeout = window.setTimeout(() => {
352
- const departures = this.filterDepartures(
353
- departureObject,
354
- sortByMinArrivalTime || false,
355
- );
356
- onMessage(departures);
357
- }, 100);
358
- }
359
- },
360
- () => {
361
- onMessage([]);
362
- },
363
- );
364
- }
365
-
366
- /**
367
- * Unsubscribe from current departures channel.
368
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
369
- */
370
- unsubscribeDepartures(cb) {
371
- if (this.subscribedStationUic) {
372
- this.unsubscribe(`timetable_${this.subscribedStationUic}`, '', cb);
373
- this.subscribedStationUic = null;
374
- }
375
- }
376
-
377
- /**
378
- * Subscribe to the disruptions channel for tenant.
379
- *
380
- * @param {function} onMessage Function called on each message of the channel.
381
- */
382
- subscribeDisruptions(onMessage) {
383
- this.subscribe(`${this.prefix}newsticker`, (data) => {
384
- onMessage(data.content);
385
- });
386
- }
387
-
388
- /**
389
- * Unsubscribe disruptions.
390
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
391
- */
392
- unsubscribeDisruptions(cb) {
393
- this.unsubscribe(`${this.prefix}newsticker`, '', cb);
394
- }
395
-
396
- /**
397
- * Return a station with a given uic number and a mode.
398
- *
399
- * @param {number} uic UIC of the station.
400
- * @param {TralisMode} mode Tralis mode.
401
- * @return {Promise<Station>} A station.
402
- */
403
- getStation(uic, mode) {
404
- const params = {
405
- channel: `station${getModeSuffix(mode, TralisModes)}`,
406
- args: uic,
407
- };
408
-
409
- return new Promise((resolve, reject) => {
410
- this.conn.get(params, (data) => {
411
- if (data.content) {
412
- resolve(data.content);
413
- } else {
414
- reject();
415
- }
416
- });
417
- });
418
- }
419
-
420
- /**
421
- * Update the model's station list for a given mode and a bbox.
422
- *
423
- * @param {TralisMode} mode Tralis mode.
424
- * @return {Promise<Array<Station>>} An array of stations.
425
- */
426
- getStations(mode) {
427
- const stations = [];
428
- const params = {
429
- channel: `station${getModeSuffix(mode, TralisModes)}`,
430
- };
431
- window.clearTimeout(this.stationUpdateTimeout);
432
- return new Promise((resolve, reject) => {
433
- this.conn.get(params, (data) => {
434
- if (data.content) {
435
- stations.push(data.content);
436
- window.clearTimeout(this.stationUpdateTimeout);
437
- /** @ignore */
438
- this.stationUpdateTimeout = window.setTimeout(() => {
439
- resolve(stations);
440
- }, 50);
441
- } else {
442
- reject(data.content);
443
- }
444
- });
445
- });
446
- }
447
-
448
- /**
449
- * Subscribe to stations channel.
450
- * One message pro station.
451
- *
452
- * @param {TralisMode} mode Tralis mode.
453
- * @param {function(station: Station)} onMessage Function called on each message of the channel.
454
- */
455
- subscribeStations(mode, onMessage) {
456
- this.unsubscribeStations();
457
- this.subscribe(`station${getModeSuffix(mode, TralisModes)}`, (data) => {
458
- if (data.content) {
459
- onMessage(data.content);
460
- }
461
- });
462
- }
463
-
464
- /**
465
- * Unsubscribe to stations channel.
466
- * @param {function} cb The listener callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribe.
467
- */
468
- unsubscribeStations(cb) {
469
- window.clearTimeout(this.stationUpdateTimeout);
470
- this.unsubscribe('station', '', cb);
471
- }
472
-
473
- /**
474
- * Subscribe to extra_geoms channel.
475
- *
476
- * @param {function(extraGeoms: GeosJSONFeature[])} onMessage Function called on each message of the channel.
477
- */
478
- subscribeExtraGeoms(onMessage) {
479
- this.subscribe('extra_geoms', (data) => {
480
- const extraGeom = data.content;
481
-
482
- if (extraGeom) {
483
- const { ref } = extraGeom.properties;
484
-
485
- if (extraGeom.type === 'Feature') {
486
- this.extraGeoms[ref] = extraGeom;
487
- } else {
488
- delete this.extraGeoms[ref];
489
- }
490
-
491
- onMessage(
492
- Object.keys(this.extraGeoms).map((key) => this.extraGeoms[key]),
493
- );
494
- }
495
- });
496
- }
497
-
498
- /**
499
- * Unsubscribe to extra_geoms channel.
500
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
501
- */
502
- unsubscribeExtraGeoms(cb) {
503
- this.unsubscribe('extra_geoms', '', cb);
504
- }
505
-
506
- /**
507
- * Subscribe to trajectory channel.
508
- *
509
- * @param {TralisMode} mode Tralis mode.
510
- * @param {function(trajectory: TralisTrajectory)} onMessage Function called on each message of the channel.
511
- * @param {boolean} quiet If true, the subscription will not send GET and SUB requests to the websocket.
512
- */
513
- subscribeTrajectory(mode, onMessage, quiet = false) {
514
- this.unsubscribeTrajectory(onMessage);
515
- this.subscribe(
516
- `trajectory${getModeSuffix(mode, TralisModes)}`,
517
- onMessage,
518
- null,
519
- quiet,
520
- );
521
- }
522
-
523
- /**
524
- * Unsubscribe to trajectory channels.
525
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
526
- */
527
- unsubscribeTrajectory(cb) {
528
- this.unsubscribe(`trajectory`, '', cb);
529
- }
530
-
531
- /**
532
- * Subscribe to deleted_vhicles channel.
533
- *
534
- * @param {TralisMode} mode Tralis mode.
535
- * @param {function(response: { content: Vehicle })} onMessage Function called on each message of the channel.
536
- * @param {boolean} quiet If true, the subscription will not send GET and SUB requests to the websocket.
537
- */
538
- subscribeDeletedVehicles(mode, onMessage, quiet = false) {
539
- this.unsubscribeDeletedVehicles(onMessage);
540
- this.subscribe(
541
- `deleted_vehicles${getModeSuffix(mode, TralisModes)}`,
542
- onMessage,
543
- null,
544
- quiet,
545
- );
546
- }
547
-
548
- /**
549
- * Unsubscribe to deleted_vhicles channels.
550
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
551
- */
552
- unsubscribeDeletedVehicles(cb) {
553
- this.unsubscribe('deleted_vehicles', '', cb);
554
- }
555
-
556
- /**
557
- * Get a full trajectory of a vehicule .
558
- *
559
- * @param {string} id A vehicle id.
560
- * @param {TralisMode} mode Tralis mode.
561
- * @param {string} generalizationLevel The generalization level to request. Can be one of 5 (more generalized), 10, 30, 100, undefined (less generalized).
562
- * @return {Promise<FullTrajectory>} Return a full trajectory.
563
- */
564
- getFullTrajectory(id, mode, generalizationLevel) {
565
- const channel = [`full_trajectory${getModeSuffix(mode, TralisModes)}`];
566
- if (id) {
567
- channel.push(id);
568
- }
569
-
570
- if ((!mode || mode === TralisModes.TOPOGRAPHIC) && generalizationLevel) {
571
- channel.push(`gen${generalizationLevel}`);
572
- }
573
-
574
- const params = {
575
- channel: channel.join('_'),
576
- };
577
-
578
- return new Promise((resolve) => {
579
- this.conn.get(params, (data) => {
580
- if (data.content) {
581
- resolve(data.content);
582
- }
583
- });
584
- });
585
- }
586
-
587
- /**
588
- * Get full trajectories of a vehicules .
589
- *
590
- * @param {string[]} ids List of vehicles ids.
591
- * @param {TralisMode} mode Tralis mode.
592
- * @param {string} generalizationLevel The generalization level to request. Can be one of '', 'gen5', 'gen10', 'gen30', 'gen100'.
593
- * @return {Promise<Array<FullTrajectory>>} Return an array of full trajectories.
594
- */
595
- getFullTrajectories(ids, mode, generalizationLevel) {
596
- const promises = ids.map((id) =>
597
- this.getFullTrajectory(id, mode, generalizationLevel),
598
- );
599
- return Promise.all(promises);
600
- }
601
-
602
- /**
603
- * Subscribe to full_trajectory channel of a given vehicle.
604
- *
605
- * @param {string} id A vehicle id.
606
- * @param {TralisMode} mode Tralis mode.
607
- */
608
- subscribeFullTrajectory(id, mode) {
609
- // window.clearTimeout(this.fullTrajectoryUpdateTimeout);
610
- this.unsubscribeFullTrajectory(id);
611
- this.subscribe(
612
- `full_trajectory${getModeSuffix(mode, TralisModes)}_${id}`,
613
- (data) => {
614
- // eslint-disable-next-line no-console
615
- console.log('subscribe full_trajectory', data);
616
- },
617
- (err) => {
618
- // eslint-disable-next-line no-console
619
- console.log('subscribe full_trajectory error', err);
620
- },
621
- );
622
- }
623
-
624
- /**
625
- * Unsubscribe from full_trajectory channel
626
- *
627
- * @param {string} id A vehicle id.
628
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
629
- */
630
- unsubscribeFullTrajectory(id, cb) {
631
- this.unsubscribe('full_trajectory', `_${id}`, cb);
632
- }
633
-
634
- /**
635
- * Get the list of stops for this vehicle.
636
- *
637
- * @param {string} id A vehicle id.
638
- * @return {Promise<StopSequence>} Returns a stop sequence object.
639
- */
640
- getStopSequence(id) {
641
- const params = {
642
- channel: `stopsequence_${id}`,
643
- };
644
- return new Promise((resolve, reject) => {
645
- this.conn.get(
646
- params,
647
- (data) => {
648
- if (data.content && data.content.length) {
649
- const content = data.content.map((stopSequence) =>
650
- cleanStopTime(stopSequence),
651
- );
652
-
653
- // Remove the delay from arrivalTime and departureTime
654
- resolve(content);
655
- }
656
- resolve([]);
657
- },
658
- (err) => {
659
- reject(err);
660
- },
661
- );
662
- });
663
- }
664
-
665
- /**
666
- * Get a list of stops for a list of vehicles.
667
- *
668
- * @param {string[]} ids List of vehicles ids.
669
- * @return {Promise<Array<StopSequence>>} Return an array of stop sequences.
670
- */
671
- getStopSequences(ids) {
672
- const promises = ids.map((id) => this.getStopSequence(id));
673
- return Promise.all(promises);
674
- }
675
-
676
- /**
677
- * Subscribe to stopsequence channel of a given vehicle.
678
- *
679
- * @param {string} id A vehicle id.
680
- * @param {function(stopSequence: StopSequence)} onMessage Function called on each message of the channel.
681
- */
682
- subscribeStopSequence(id, onMessage) {
683
- window.clearTimeout(this.fullTrajectoryUpdateTimeout);
684
- this.unsubscribeStopSequence(id);
685
-
686
- this.subscribe(
687
- `stopsequence_${id}`,
688
- (data) => {
689
- if (data.content && data.content.length) {
690
- const content = data.content.map((stopSequence) =>
691
- cleanStopTime(stopSequence),
692
- );
693
-
694
- // Remove the delay from arrivalTime and departureTime
695
- onMessage(content);
696
- }
697
- },
698
- (err) => {
699
- // eslint-disable-next-line no-console
700
- console.log('subscribe stopsequence error', err);
701
- },
702
- );
703
- }
704
-
705
- /**
706
- * Unsubscribe from stopsequence channel
707
- *
708
- * @param {string} id A vehicle id.
709
- * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
710
- */
711
- unsubscribeStopSequence(id, cb) {
712
- this.unsubscribe(`stopsequence`, `_${id}`, cb);
713
- }
714
-
715
- /**
716
- * Subscribe to healthcheck channel.
717
- * @param {function} onMessage Callback when the subscribe to healthcheck channel succeeds.
718
- */
719
- subscribeHealthCheck(onMessage) {
720
- this.unsubscribeHealthCheck();
721
- this.subscribe('healthcheck', onMessage);
722
- }
723
-
724
- /**
725
- * Unsubscribe to healthcheck channel.
726
- */
727
- unsubscribeHealthCheck() {
728
- this.unsubscribe('healthcheck');
729
- }
730
- }
731
- export default TralisAPI;