inhumate-rti 0.0.26

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 (103) hide show
  1. package/lib/constants.d.ts +44 -0
  2. package/lib/constants.js +45 -0
  3. package/lib/constants.js.map +1 -0
  4. package/lib/generated/Channels.d.ts +68 -0
  5. package/lib/generated/Channels.js +369 -0
  6. package/lib/generated/Channels.js.map +1 -0
  7. package/lib/generated/Clients.d.ts +124 -0
  8. package/lib/generated/Clients.js +871 -0
  9. package/lib/generated/Clients.js.map +1 -0
  10. package/lib/generated/Color.d.ts +26 -0
  11. package/lib/generated/Color.js +87 -0
  12. package/lib/generated/Color.js.map +1 -0
  13. package/lib/generated/Commands.d.ts +97 -0
  14. package/lib/generated/Commands.js +545 -0
  15. package/lib/generated/Commands.js.map +1 -0
  16. package/lib/generated/Entity.d.ts +54 -0
  17. package/lib/generated/Entity.js +332 -0
  18. package/lib/generated/Entity.js.map +1 -0
  19. package/lib/generated/EntityCategory.d.ts +11 -0
  20. package/lib/generated/EntityCategory.js +51 -0
  21. package/lib/generated/EntityCategory.js.map +1 -0
  22. package/lib/generated/EntityDomain.d.ts +12 -0
  23. package/lib/generated/EntityDomain.js +57 -0
  24. package/lib/generated/EntityDomain.js.map +1 -0
  25. package/lib/generated/EntityOperation.d.ts +54 -0
  26. package/lib/generated/EntityOperation.js +294 -0
  27. package/lib/generated/EntityOperation.js.map +1 -0
  28. package/lib/generated/EntityPosition.d.ts +97 -0
  29. package/lib/generated/EntityPosition.js +607 -0
  30. package/lib/generated/EntityPosition.js.map +1 -0
  31. package/lib/generated/Geometry.d.ts +221 -0
  32. package/lib/generated/Geometry.js +1427 -0
  33. package/lib/generated/Geometry.js.map +1 -0
  34. package/lib/generated/GeometryOperation.d.ts +39 -0
  35. package/lib/generated/GeometryOperation.js +166 -0
  36. package/lib/generated/GeometryOperation.js.map +1 -0
  37. package/lib/generated/IdMessage.d.ts +24 -0
  38. package/lib/generated/IdMessage.js +57 -0
  39. package/lib/generated/IdMessage.js.map +1 -0
  40. package/lib/generated/Injectable.d.ts +39 -0
  41. package/lib/generated/Injectable.js +176 -0
  42. package/lib/generated/Injectable.js.map +1 -0
  43. package/lib/generated/InjectableOperation.d.ts +25 -0
  44. package/lib/generated/InjectableOperation.js +59 -0
  45. package/lib/generated/InjectableOperation.js.map +1 -0
  46. package/lib/generated/Injection.d.ts +57 -0
  47. package/lib/generated/Injection.js +307 -0
  48. package/lib/generated/Injection.js.map +1 -0
  49. package/lib/generated/InjectionOperation.d.ts +87 -0
  50. package/lib/generated/InjectionOperation.js +539 -0
  51. package/lib/generated/InjectionOperation.js.map +1 -0
  52. package/lib/generated/LVCCategory.d.ts +10 -0
  53. package/lib/generated/LVCCategory.js +45 -0
  54. package/lib/generated/LVCCategory.js.map +1 -0
  55. package/lib/generated/LaunchConfigurations.d.ts +60 -0
  56. package/lib/generated/LaunchConfigurations.js +362 -0
  57. package/lib/generated/LaunchConfigurations.js.map +1 -0
  58. package/lib/generated/LaunchEvent.d.ts +55 -0
  59. package/lib/generated/LaunchEvent.js +283 -0
  60. package/lib/generated/LaunchEvent.js.map +1 -0
  61. package/lib/generated/Logs.d.ts +152 -0
  62. package/lib/generated/Logs.js +1088 -0
  63. package/lib/generated/Logs.js.map +1 -0
  64. package/lib/generated/Measurement.d.ts +42 -0
  65. package/lib/generated/Measurement.js +212 -0
  66. package/lib/generated/Measurement.js.map +1 -0
  67. package/lib/generated/MeasurementBundle.d.ts +86 -0
  68. package/lib/generated/MeasurementBundle.js +539 -0
  69. package/lib/generated/MeasurementBundle.js.map +1 -0
  70. package/lib/generated/Measures.d.ts +43 -0
  71. package/lib/generated/Measures.js +216 -0
  72. package/lib/generated/Measures.js.map +1 -0
  73. package/lib/generated/MessageBundle.d.ts +81 -0
  74. package/lib/generated/MessageBundle.js +488 -0
  75. package/lib/generated/MessageBundle.js.map +1 -0
  76. package/lib/generated/Parameter.d.ts +29 -0
  77. package/lib/generated/Parameter.js +129 -0
  78. package/lib/generated/Parameter.js.map +1 -0
  79. package/lib/generated/RuntimeControl.d.ts +150 -0
  80. package/lib/generated/RuntimeControl.js +1014 -0
  81. package/lib/generated/RuntimeControl.js.map +1 -0
  82. package/lib/generated/RuntimeState.d.ts +24 -0
  83. package/lib/generated/RuntimeState.js +129 -0
  84. package/lib/generated/RuntimeState.js.map +1 -0
  85. package/lib/generated/Scenarios.d.ts +40 -0
  86. package/lib/generated/Scenarios.js +162 -0
  87. package/lib/generated/Scenarios.js.map +1 -0
  88. package/lib/generated/google/protobuf/empty.d.ts +23 -0
  89. package/lib/generated/google/protobuf/empty.js +40 -0
  90. package/lib/generated/google/protobuf/empty.js.map +1 -0
  91. package/lib/generated/google/protobuf/timestamp.d.ts +25 -0
  92. package/lib/generated/google/protobuf/timestamp.js +87 -0
  93. package/lib/generated/google/protobuf/timestamp.js.map +1 -0
  94. package/lib/index.d.ts +8 -0
  95. package/lib/index.js +8 -0
  96. package/lib/index.js.map +1 -0
  97. package/lib/proto.d.ts +26 -0
  98. package/lib/proto.js +27 -0
  99. package/lib/proto.js.map +1 -0
  100. package/lib/rticlient.d.ts +164 -0
  101. package/lib/rticlient.js +775 -0
  102. package/lib/rticlient.js.map +1 -0
  103. package/package.json +48 -0
@@ -0,0 +1,775 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
11
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
12
+ var m = o[Symbol.asyncIterator], i;
13
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
14
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
15
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
16
+ };
17
+ import { EventEmitter } from "events";
18
+ import { AGClientSocket } from "socketcluster-client";
19
+ import { v4 as uuidv4 } from "uuid";
20
+ import * as base64 from "base64-js";
21
+ import constants, { channel as RTIchannel } from "./constants.js";
22
+ import { Clients, Client, ParticipantRegistration } from "./generated/Clients.js";
23
+ import { RuntimeState } from "./generated/RuntimeState.js";
24
+ import { Channels, Channel, ChannelUsage, ChannelUse } from "./generated/Channels.js";
25
+ import { RuntimeControl } from "./generated/RuntimeControl.js";
26
+ import { Measures, Measure } from "./generated/Measures.js";
27
+ import { Measurement, Measurement_Window } from "./generated/Measurement.js";
28
+ export class RTIClient extends EventEmitter {
29
+ get clientId() {
30
+ return this._clientId;
31
+ }
32
+ get user() {
33
+ return this._user;
34
+ }
35
+ get participant() {
36
+ return this._participant;
37
+ }
38
+ get role() {
39
+ return this._role;
40
+ }
41
+ get fullName() {
42
+ return this._fullName;
43
+ }
44
+ get capabilities() {
45
+ return this._capabilities;
46
+ }
47
+ get isConnected() {
48
+ return this.connected;
49
+ }
50
+ get authToken() {
51
+ return this.socket ? this.socket.authToken : undefined;
52
+ }
53
+ get url() {
54
+ return this._url;
55
+ }
56
+ get brokerVersion() {
57
+ return this._brokerVersion;
58
+ }
59
+ get state() {
60
+ return this._state;
61
+ }
62
+ set state(value) {
63
+ if (value != this._state) {
64
+ this._state = value;
65
+ this.publishClient();
66
+ this.emit("client", this.myClient);
67
+ }
68
+ }
69
+ get usedChannels() {
70
+ return Object.values(this._usedChannels);
71
+ }
72
+ get knownChannels() {
73
+ return Object.values(this._knownChannels);
74
+ }
75
+ get knownClients() {
76
+ return Object.values(this._knownClients);
77
+ }
78
+ get usedMeasures() {
79
+ return Object.values(this._usedMeasures);
80
+ }
81
+ get knownMeasures() {
82
+ return Object.values(this._knownMeasures);
83
+ }
84
+ get compatibilityMode() {
85
+ return this._compatibilityMode;
86
+ }
87
+ get ownChannelPrefix() {
88
+ return `@${this.clientId}:`;
89
+ }
90
+ constructor(options) {
91
+ super();
92
+ this.application = "typescript";
93
+ this.applicationVersion = "";
94
+ this.integrationVersion = "";
95
+ this.engineVersion = "";
96
+ this._user = undefined;
97
+ this._secret = undefined;
98
+ this._password = undefined;
99
+ this._participant = undefined;
100
+ this._role = undefined;
101
+ this._fullName = undefined;
102
+ this._capabilities = [];
103
+ this.incognito = false;
104
+ this.incognitoChannels = false;
105
+ this.connected = false;
106
+ this._brokerVersion = undefined;
107
+ this._state = RuntimeState.UNKNOWN;
108
+ this._usedChannels = {};
109
+ this._knownChannels = {};
110
+ this._knownClients = {};
111
+ this._usedMeasures = {};
112
+ this._knownMeasures = {};
113
+ this._compatibilityMode = false;
114
+ this.measurementIntervalTimeScale = 1;
115
+ this.collectMeasurementsInterval = undefined;
116
+ this.collectQueue = {};
117
+ this.lastCollect = {};
118
+ this.die = false;
119
+ this._handlers = {};
120
+ const env = typeof process === "undefined" ? {} : process.env || {};
121
+ var url = options && options.url;
122
+ if (!url)
123
+ url = env["RTI_URL"];
124
+ if (!url && typeof location == "object" && location.host) {
125
+ if (location.protocol == "https:") {
126
+ url = `wss://${location.host}`;
127
+ }
128
+ else if (location.protocol == "app:" && location.hostname == ".") {
129
+ url = `ws://localhost:${constants.defaultPort}`;
130
+ }
131
+ else if (location.hostname && location.hostname.startsWith("127.")) {
132
+ url = `ws://${location.hostname}:${constants.defaultPort}`;
133
+ }
134
+ else if (location.host && location.host != "localhost" && !location.host.startsWith("localhost:")) {
135
+ url = `ws://${location.host}`;
136
+ }
137
+ }
138
+ if (!url)
139
+ url = `ws://${constants.defaultHost}:${constants.defaultPort}`;
140
+ if (!url.startsWith("ws://") && !url.startsWith("wss://"))
141
+ url = `ws://${url}`;
142
+ this._url = url;
143
+ if (options && options.application)
144
+ this.application = options.application;
145
+ if (options && options.applicationVersion)
146
+ this.applicationVersion = options.applicationVersion;
147
+ if (options && options.integrationVersion)
148
+ this.integrationVersion = options.integrationVersion;
149
+ if (options && options.engineVersion)
150
+ this.engineVersion = options.engineVersion;
151
+ if (options && options.incognito)
152
+ this.incognito = options.incognito;
153
+ if (options && options.incognitoChannels)
154
+ this.incognitoChannels = options.incognitoChannels;
155
+ this._clientId = "";
156
+ if (!this._clientId && options && options.clientId)
157
+ this._clientId = options.clientId;
158
+ this.federation = "";
159
+ if (!this.federation && options && options.federation)
160
+ this.federation = options.federation;
161
+ if (!this.federation)
162
+ this.federation = env["RTI_FEDERATION"] || "";
163
+ if (this.federation)
164
+ this.federation = this.federation.replace("/", "_");
165
+ this.host = "";
166
+ if (!this.host && options && options.host)
167
+ this.host = options.host;
168
+ if (!this.host)
169
+ this.host = env["RTI_HOST"] || "";
170
+ if (this.host && this.host.indexOf(".") >= 0)
171
+ this.host = this.host.split(".")[0];
172
+ this.station = "";
173
+ if (!this.station && options && options.station)
174
+ this.station = options.station;
175
+ if (!this.station)
176
+ this.station = env["RTI_STATION"] || "";
177
+ if (!this._user && options && options.user)
178
+ this._user = options.user;
179
+ if (!this._participant && options && options.participant)
180
+ this._participant = options.participant;
181
+ if (!this._role && options && options.role)
182
+ this._role = options.role;
183
+ if (!this._fullName && options && options.fullName)
184
+ this._fullName = options.fullName;
185
+ if (options && options.capabilities)
186
+ this._capabilities = options.capabilities;
187
+ let secret = env["RTI_SECRET"];
188
+ if (!secret && options && options.secret)
189
+ secret = options.secret;
190
+ this._secret = secret;
191
+ const socketOptions = {};
192
+ var u = new URL(this._url);
193
+ socketOptions.port = parseInt(u.port);
194
+ socketOptions.hostname = u.hostname;
195
+ socketOptions.secure = u.protocol == "wss:" || u.protocol == "https:";
196
+ socketOptions.path = u.pathname;
197
+ if (options && typeof options.connect !== "undefined") {
198
+ socketOptions.autoConnect = options.connect;
199
+ }
200
+ this.socket = new AGClientSocket(socketOptions);
201
+ this.setupSocket();
202
+ }
203
+ setupSocket() {
204
+ var _a, _b;
205
+ const tokenData = () => {
206
+ if (!this._clientId)
207
+ this._clientId = uuidv4();
208
+ const authToken = {};
209
+ if (this._secret)
210
+ authToken.secret = this._secret;
211
+ if (this._user)
212
+ authToken.user = this._user;
213
+ if (this._password)
214
+ authToken.password = this._password;
215
+ if (this._participant)
216
+ authToken.participant = this._participant;
217
+ if (this._fullName)
218
+ authToken.fullName = this._fullName;
219
+ if (this._role)
220
+ authToken.role = this._role;
221
+ authToken.application = this.application;
222
+ authToken.clientId = this.clientId;
223
+ authToken.clientLibraryVersion = constants.version;
224
+ if (this.federation)
225
+ authToken.federation = this.federation;
226
+ return authToken;
227
+ };
228
+ this.forAwait(this.socket.listener("error"), (error) => {
229
+ this.emit("error", typeof error == "object" && "error" in error ? error.error : error);
230
+ });
231
+ this.forAwait(this.socket.listener("connect"), (event) => {
232
+ const token = this.socket.authToken;
233
+ if ((!token && !this.socket.signedAuthToken) || (this._clientId && token && this._clientId != token.clientId) || (this._user && token && this._user != token.user)) {
234
+ this.socket.transmit("auth", tokenData());
235
+ }
236
+ });
237
+ this.forAwait(this.socket.listener("close"), (event) => {
238
+ this.connected = false;
239
+ this.emit("disconnect");
240
+ });
241
+ this.forAwait(this.socket.listener("authenticate"), (event) => {
242
+ const token = this.socket.authToken;
243
+ if (token.user && this._user && this._user != token.user) {
244
+ this.socket.deauthenticate();
245
+ }
246
+ else {
247
+ if (token.clientId && this._clientId != token.clientId) {
248
+ if (this._clientId)
249
+ console.warn("RTI authenticated with unexpected client id", token.clientId);
250
+ this._clientId = token.clientId;
251
+ }
252
+ if (token.user)
253
+ this._user = token.user;
254
+ if ("secret" in token)
255
+ this._secret = undefined;
256
+ if ("password" in token)
257
+ this._password = undefined;
258
+ this.emit("authenticate");
259
+ if (!this.connected) {
260
+ this.connected = true;
261
+ if (!this.incognito && this.clientId) {
262
+ this.publishClient();
263
+ this.publishMeasures();
264
+ }
265
+ if (this.federation)
266
+ this.publishText(RTIchannel.federations, this.federation, false);
267
+ this.emit("connect");
268
+ }
269
+ }
270
+ });
271
+ this.forAwait(this.socket.receiver("fail"), (error) => {
272
+ this.emit("error", typeof error == "object" && "error" in error ? error.error : error);
273
+ this.disconnect();
274
+ });
275
+ this.forAwait(this.socket.receiver("ping"), (content) => {
276
+ this.socket.transmit("pong", content);
277
+ });
278
+ this.forAwait(this.socket.receiver("broker-version"), (content) => {
279
+ this._brokerVersion = content;
280
+ this.emit("broker-version", content);
281
+ });
282
+ if ((_a = this.socket.transport) === null || _a === void 0 ? void 0 : _a.socket) {
283
+ const sockSocket = (_b = this.socket.transport) === null || _b === void 0 ? void 0 : _b.socket;
284
+ sockSocket.addEventListener("message", (message) => {
285
+ if (message.data === "#1") {
286
+ this.socket.send("#2");
287
+ if (!this._compatibilityMode) {
288
+ this._compatibilityMode = true;
289
+ this.emit("compatibility-mode", this._compatibilityMode);
290
+ this.socket.transport.pingTimeoutDisabled = true;
291
+ clearInterval(this.socket.transport._pingTimeoutTicker);
292
+ }
293
+ }
294
+ });
295
+ }
296
+ this.subscribe(RTIchannel.clients, Clients, (m) => this.onClients(m), false);
297
+ this.subscribe(RTIchannel.channels, Channels, (m) => this.onChannels(m), false);
298
+ this.subscribe(RTIchannel.measures, Measures, (m) => this.onMeasures(m), false);
299
+ this.subscribeText(RTIchannel.federations, (message) => {
300
+ if (message == "?")
301
+ this.publishText(RTIchannel.federations, this.federation, false);
302
+ }, false);
303
+ }
304
+ forAwait(iterable, handler) {
305
+ return __awaiter(this, void 0, void 0, function* () {
306
+ var _a, iterable_1, iterable_1_1;
307
+ var _b, e_1, _c, _d;
308
+ try {
309
+ for (_a = true, iterable_1 = __asyncValues(iterable); iterable_1_1 = yield iterable_1.next(), _b = iterable_1_1.done, !_b; _a = true) {
310
+ _d = iterable_1_1.value;
311
+ _a = false;
312
+ let item = _d;
313
+ if (this.die || handler(item) === false)
314
+ break;
315
+ }
316
+ }
317
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
318
+ finally {
319
+ try {
320
+ if (!_a && !_b && (_c = iterable_1.return)) yield _c.call(iterable_1);
321
+ }
322
+ finally { if (e_1) throw e_1.error; }
323
+ }
324
+ });
325
+ }
326
+ kill() {
327
+ if (this.socket.state == "open")
328
+ this.socket.disconnect();
329
+ this.socket.killAllChannels();
330
+ this.socket.killAllListeners();
331
+ this.socket.killAllReceivers();
332
+ this.socket.killAllProcedures();
333
+ this.die = true;
334
+ this.connected = false;
335
+ }
336
+ invoke(event, data, options) {
337
+ return this.socket.invoke(event, data, options);
338
+ }
339
+ getToken(data) {
340
+ return __awaiter(this, void 0, void 0, function* () {
341
+ const result = yield this.socket.invoke("gettoken", data);
342
+ return result;
343
+ });
344
+ }
345
+ verifyToken(token) {
346
+ return __awaiter(this, void 0, void 0, function* () {
347
+ const result = yield this.socket.invoke("verifytoken", token);
348
+ return result;
349
+ });
350
+ }
351
+ onClients(message) {
352
+ if (message.requestClients) {
353
+ if (!this.incognito && this.clientId)
354
+ this.publishClient();
355
+ }
356
+ else if (message.client) {
357
+ this._knownClients[message.client.id] = message.client;
358
+ this.emit("client", message.client);
359
+ }
360
+ else if (message.logClient) {
361
+ if (!(message.logClient.id in this._knownClients)) {
362
+ this._knownClients[message.logClient.id] = message.logClient;
363
+ this.emit("logclient", message.logClient);
364
+ }
365
+ }
366
+ else if (message.registerParticipant) {
367
+ const reg = message.registerParticipant;
368
+ if ((!reg.clientId || reg.clientId == this.clientId) &&
369
+ (!reg.host || reg.host == this.host) &&
370
+ (!reg.station || reg.station == this.station) &&
371
+ (reg.participant != this.participant || reg.role != this.role || reg.fullName != this.fullName)) {
372
+ this._participant = reg.participant;
373
+ this._role = reg.role;
374
+ this._fullName = reg.fullName;
375
+ this.publishClient();
376
+ this.emit("client", this.myClient);
377
+ }
378
+ }
379
+ }
380
+ onChannels(message) {
381
+ if (message.requestChannelUsage && !this.incognito && !this.incognitoChannels) {
382
+ const usage = ChannelUsage.create({ clientId: this.clientId });
383
+ for (const use of Object.values(this._usedChannels)) {
384
+ usage.usage.push(use);
385
+ }
386
+ this.publish(RTIchannel.channels, Channels, { channelUsage: usage }, false);
387
+ }
388
+ else if (message.channelUsage) {
389
+ for (const usage of message.channelUsage.usage) {
390
+ this.discoverChannel(usage.channel);
391
+ this.emit("channel", usage.channel);
392
+ }
393
+ }
394
+ else if (message.channel) {
395
+ this.discoverChannel(message.channel);
396
+ this.emit("channel", message.channel);
397
+ }
398
+ else if (message.logChannel) {
399
+ if (!(message.logChannel.name in this._knownChannels)) {
400
+ this.discoverChannel(message.logChannel);
401
+ this.emit("logchannel", message.logChannel);
402
+ }
403
+ }
404
+ }
405
+ onMeasures(message) {
406
+ if (message.requestMeasures) {
407
+ if (!this.incognito) {
408
+ this.publishMeasures();
409
+ }
410
+ else if (message.measure) {
411
+ this._knownMeasures[message.measure.id] = message.measure;
412
+ this.emit("measure", message.measure);
413
+ }
414
+ else if (message.logMeasure) {
415
+ if (!(message.logMeasure.id in this._knownMeasures)) {
416
+ this._knownMeasures[message.logMeasure.id] = message.logMeasure;
417
+ this.emit("logmeasure", message.logMeasure);
418
+ }
419
+ }
420
+ }
421
+ }
422
+ publishClient() {
423
+ this.publish(RTIchannel.clients, Clients, { client: this.myClient }, false);
424
+ }
425
+ publishMeasures() {
426
+ for (const measure of Object.values(this._knownMeasures)) {
427
+ this.publish(RTIchannel.measures, Measures, { measure }, false);
428
+ }
429
+ }
430
+ get myClient() {
431
+ return Client.create({
432
+ id: this.clientId,
433
+ state: this._state,
434
+ host: this.host,
435
+ station: this.station,
436
+ user: this.user,
437
+ participant: this.participant,
438
+ role: this.role,
439
+ fullName: this.fullName,
440
+ application: this.application,
441
+ applicationVersion: this.applicationVersion,
442
+ clientLibraryVersion: constants.version,
443
+ integrationVersion: this.integrationVersion,
444
+ engineVersion: !this.engineVersion && typeof process !== "undefined" && process.version ? `Node ${process.version}` : this.engineVersion,
445
+ userAgent: typeof navigator !== "undefined" && navigator.userAgent ? navigator.userAgent : "",
446
+ capabilities: this.capabilities,
447
+ });
448
+ }
449
+ publishError(errorMessage, errorState) {
450
+ this.publish(RTIchannel.control, RuntimeControl, {
451
+ error: {
452
+ clientId: this.clientId,
453
+ message: errorMessage,
454
+ errorState,
455
+ },
456
+ }, false);
457
+ }
458
+ publishHeartbeat() {
459
+ this.publish(RTIchannel.clients, Clients, { clientHeartbeat: { clientId: this.clientId } }, false);
460
+ }
461
+ publishProgress(progress) {
462
+ this.publish(RTIchannel.clients, Clients, { progress: { clientId: this.clientId, progress } }, false);
463
+ }
464
+ publishValue(value, highlight = false, error = false) {
465
+ this.publish(RTIchannel.clients, Clients, { value: { clientId: this.clientId, value, highlight, error } }, false);
466
+ }
467
+ changePassword(oldPassword, newPassword) {
468
+ return __awaiter(this, void 0, void 0, function* () {
469
+ const result = yield this.socket.invoke("passwd", { oldPassword, newPassword });
470
+ return result;
471
+ });
472
+ }
473
+ subscribe(channelName, type, handler, register = true) {
474
+ if (!type)
475
+ throw new Error("cannot subscribe with undefined type");
476
+ if (register)
477
+ this.registerChannelUsage(channelName, false, type.name);
478
+ return this.doSubscribe(channelName, (message) => {
479
+ const data = RTIClient.parse(type, message);
480
+ if (handler.length == 2) {
481
+ handler(channelName, data);
482
+ }
483
+ else {
484
+ handler(data);
485
+ }
486
+ });
487
+ }
488
+ static parse(type, content) {
489
+ return type.decode(base64.toByteArray(content));
490
+ }
491
+ subscribeText(channelName, handler, register = true) {
492
+ if (register)
493
+ this.registerChannelUsage(channelName, false, "text");
494
+ return this.doSubscribe(channelName, handler);
495
+ }
496
+ subscribeJSON(channelName, handler, register = true) {
497
+ if (register)
498
+ this.registerChannelUsage(channelName, false, "json");
499
+ return this.doSubscribe(channelName, (message) => {
500
+ const data = JSON.parse(message);
501
+ if (handler.length == 2) {
502
+ handler(channelName, data);
503
+ }
504
+ else {
505
+ handler(data);
506
+ }
507
+ });
508
+ }
509
+ doSubscribe(channelName, handler) {
510
+ const channel = this.socket.subscribe((this.federation && channelName != RTIchannel.federations ? "//" + this.federation + "/" : "") + channelName);
511
+ const wrappedHandler = (data) => {
512
+ try {
513
+ if (handler.length == 2) {
514
+ handler(channelName, data);
515
+ }
516
+ else {
517
+ handler(data);
518
+ }
519
+ }
520
+ catch (error) {
521
+ this.emit("error", error);
522
+ }
523
+ };
524
+ if (!(channelName in this._handlers))
525
+ this._handlers[channelName] = [];
526
+ this._handlers[channelName].push(handler);
527
+ this.forAwait(channel, (data) => {
528
+ if (this._handlers[channelName].indexOf(handler) < 0)
529
+ return false;
530
+ wrappedHandler(data);
531
+ });
532
+ return { channel, handler };
533
+ }
534
+ unsubscribe(channelNameOrSubscription) {
535
+ if (typeof channelNameOrSubscription === "string") {
536
+ this.socket.unsubscribe(channelNameOrSubscription);
537
+ }
538
+ else {
539
+ const subscription = channelNameOrSubscription;
540
+ const channel = subscription.channel;
541
+ if (channel) {
542
+ const handlers = this._handlers[channel.name];
543
+ if (handlers && handlers.length > 0) {
544
+ const index = handlers.indexOf(subscription.handler);
545
+ if (index >= 0)
546
+ handlers.splice(index, 1);
547
+ if (handlers.length == 0)
548
+ channel.unsubscribe();
549
+ }
550
+ }
551
+ }
552
+ }
553
+ publish(channelName, typeOrEncode, data, register = true, typeName) {
554
+ if (register)
555
+ this.registerChannelUsage(channelName, true, typeName || "proto");
556
+ let bytes;
557
+ if (typeOrEncode.encode) {
558
+ if (typeOrEncode.fromPartial)
559
+ data = typeOrEncode.fromPartial(data);
560
+ bytes = typeOrEncode.encode(data);
561
+ }
562
+ else {
563
+ bytes = typeOrEncode;
564
+ }
565
+ if (bytes.finish)
566
+ bytes = bytes.finish();
567
+ this.doPublish(channelName, base64.fromByteArray(bytes));
568
+ }
569
+ publishBytes(channelName, bytes, register = true, typeName) {
570
+ if (register)
571
+ this.registerChannelUsage(channelName, true, typeName || "bytes");
572
+ if (bytes.finish)
573
+ bytes = bytes.finish();
574
+ this.doPublish(channelName, base64.fromByteArray(bytes));
575
+ }
576
+ publishText(channelName, message, register = true) {
577
+ if (register)
578
+ this.registerChannelUsage(channelName, true, "text");
579
+ this.doPublish(channelName, message);
580
+ }
581
+ publishJSON(channelName, message, register = true) {
582
+ if (register)
583
+ this.registerChannelUsage(channelName, true, "json");
584
+ this.doPublish(channelName, JSON.stringify(message));
585
+ }
586
+ doPublish(channelName, message) {
587
+ if (this.federation && !channelName.startsWith("@") && channelName != RTIchannel.federations)
588
+ channelName = "//" + this.federation + "/" + channelName;
589
+ this.socket.transmitPublish(channelName, message);
590
+ }
591
+ connect() {
592
+ this.socket.connect();
593
+ }
594
+ disconnect() {
595
+ this.socket.disconnect();
596
+ }
597
+ setCredentials(user, password) {
598
+ this._user = user;
599
+ this._password = password;
600
+ this.socket.deauthenticate();
601
+ }
602
+ logout() {
603
+ this._user = undefined;
604
+ this._password = undefined;
605
+ this.socket.deauthenticate();
606
+ this.socket.disconnect();
607
+ setTimeout(() => this.socket.connect(), 100);
608
+ }
609
+ registerChannelUsage(channelName, usePublish, type) {
610
+ let channel;
611
+ if (typeof channelName === "string") {
612
+ if (channelName in this._knownChannels) {
613
+ channel = this._knownChannels[channelName];
614
+ }
615
+ else {
616
+ channel = Channel.create({ name: channelName });
617
+ }
618
+ }
619
+ else {
620
+ channel = channelName;
621
+ }
622
+ if (type)
623
+ channel.dataType = type;
624
+ if (channel.name.startsWith("@"))
625
+ return;
626
+ let use = this._usedChannels[channel.name];
627
+ if (!use)
628
+ use = ChannelUse.create();
629
+ use.channel = channel;
630
+ this._usedChannels[channel.name] = use;
631
+ if (usePublish)
632
+ use.publish = true;
633
+ else
634
+ use.subscribe = true;
635
+ if (!(channel.name in this._knownChannels))
636
+ this.registerChannel(channel);
637
+ }
638
+ registerChannel(channel) {
639
+ if (channel.name.startsWith("@"))
640
+ return;
641
+ this._knownChannels[channel.name] = channel;
642
+ if (!(channel.name in this._usedChannels))
643
+ this._usedChannels[channel.name] = ChannelUse.create();
644
+ const use = this._usedChannels[channel.name];
645
+ use.channel = channel;
646
+ if (this.connected && !this.incognito) {
647
+ this.publish(RTIchannel.channels, Channels, { channel }, false);
648
+ }
649
+ }
650
+ discoverChannel(channel) {
651
+ if (!(channel.name in this._knownChannels)) {
652
+ this._knownChannels[channel.name] = channel;
653
+ }
654
+ else {
655
+ const known = this._knownChannels[channel.name];
656
+ if (channel.dataType && !known.dataType)
657
+ known.dataType = channel.dataType;
658
+ if (channel.ephemeral)
659
+ known.ephemeral = true;
660
+ if (channel.state)
661
+ known.state = true;
662
+ if (channel.firstFieldId)
663
+ known.firstFieldId = true;
664
+ }
665
+ }
666
+ registerParticipant(participant, role, fullName) {
667
+ const registration = ParticipantRegistration.create({
668
+ participant,
669
+ role,
670
+ fullName,
671
+ clientId: this.clientId,
672
+ station: this.station,
673
+ host: this.host,
674
+ });
675
+ this.publish(RTIchannel.clients, Clients, { registerParticipant: registration }, false);
676
+ this._participant = participant;
677
+ this._role = role;
678
+ this._fullName = fullName;
679
+ this.publishClient();
680
+ }
681
+ registerMeasure(measure) {
682
+ measure.application = this.application;
683
+ this._usedMeasures[measure.id] = measure;
684
+ if (!(measure.id in this._knownMeasures)) {
685
+ this._knownMeasures[measure.id] = measure;
686
+ if (this.connected && !this.incognito) {
687
+ this.publish(RTIchannel.measures, Measures, { measure }, false);
688
+ }
689
+ }
690
+ }
691
+ measure(measureOrId, value) {
692
+ let measure = undefined;
693
+ if (typeof measureOrId == "string") {
694
+ measure = this._usedMeasures[measureOrId];
695
+ if (!measure)
696
+ measure = this._knownMeasures[measureOrId];
697
+ if (!measure) {
698
+ measure = Measure.create({ id: measureOrId, application: this.application });
699
+ }
700
+ }
701
+ else {
702
+ measure = measureOrId;
703
+ }
704
+ if (!measure)
705
+ return;
706
+ if (!(measure.id in this._usedMeasures))
707
+ this.registerMeasure(measure);
708
+ if (measure.interval > 1e-5) {
709
+ if (!this.collectMeasurementsInterval)
710
+ this.collectMeasurementsInterval = setInterval(() => this.collectMeasurements(), 500);
711
+ if (!(measure.id in this.collectQueue))
712
+ this.collectQueue[measure.id] = [];
713
+ this.collectQueue[measure.id].push(value);
714
+ }
715
+ else {
716
+ const measurement = Measurement.create({ measureId: measure.id, clientId: this.clientId, value });
717
+ const channel = measure.channel || RTIchannel.measurement;
718
+ if (this.connected)
719
+ this.publish(channel, Measurement, measurement, false);
720
+ }
721
+ }
722
+ collectMeasurements() {
723
+ if (!this.connected && this.collectMeasurementsInterval) {
724
+ clearInterval(this.collectMeasurementsInterval);
725
+ this.collectMeasurementsInterval = undefined;
726
+ return;
727
+ }
728
+ for (const id in this.collectQueue) {
729
+ if (!(id in this.lastCollect)) {
730
+ this.lastCollect[id] = new Date().getTime();
731
+ }
732
+ else {
733
+ const measure = this._knownMeasures[id];
734
+ if ((new Date().getTime() - this.lastCollect[id]) * this.measurementIntervalTimeScale > measure.interval * 1000) {
735
+ const channel = measure.channel || RTIchannel.measurement;
736
+ const measurement = Measurement.create({ measureId: id, clientId: this.clientId });
737
+ const values = this.collectQueue[id];
738
+ if (values.length == 1) {
739
+ measurement.value = values.pop();
740
+ this.publish(channel, Measurement, measurement, false);
741
+ }
742
+ else if (values.length > 1) {
743
+ const window = Measurement_Window.create({ min: Infinity, max: -Infinity, count: values.length });
744
+ for (const value of values) {
745
+ window.mean += value;
746
+ if (value > window.max)
747
+ window.max = value;
748
+ if (value < window.min)
749
+ window.min = value;
750
+ }
751
+ this.collectQueue[id] = [];
752
+ if (window.count > 0)
753
+ window.mean /= window.count;
754
+ window.duration = (new Date().getTime() - this.lastCollect[id]) * this.measurementIntervalTimeScale;
755
+ measurement.window = window;
756
+ this.publish(channel, Measurement, measurement, false);
757
+ }
758
+ this.lastCollect[id] = new Date().getTime();
759
+ }
760
+ }
761
+ }
762
+ }
763
+ whenConnected(callback) {
764
+ if (this.connected)
765
+ callback();
766
+ else
767
+ this.once("connect", () => callback());
768
+ }
769
+ resetKnown() {
770
+ this._knownChannels = {};
771
+ this._knownClients = {};
772
+ this._knownMeasures = {};
773
+ }
774
+ }
775
+ //# sourceMappingURL=rticlient.js.map