@openremote/core 1.0.2 → 1.2.0-snapshot.20240512154942
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -8
- package/lib/asset-mixin.d.ts +46 -0
- package/lib/asset-mixin.js +1 -0
- package/lib/asset-mixin.js.map +1 -0
- package/{dist → lib}/console.d.ts +21 -12
- package/lib/console.js +1 -0
- package/lib/console.js.map +1 -0
- package/lib/defaults.d.ts +15 -0
- package/lib/defaults.js +1 -0
- package/lib/defaults.js.map +1 -0
- package/{dist → lib}/event.d.ts +30 -16
- package/lib/event.js +1 -0
- package/lib/event.js.map +1 -0
- package/lib/index.d.ts +150 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/util.d.ts +92 -0
- package/lib/util.js +1 -0
- package/lib/util.js.map +1 -0
- package/package.json +31 -14
- package/dist/asset-mixin.d.ts +0 -25
- package/dist/asset-mixin.js +0 -115
- package/dist/asset-mixin.js.map +0 -1
- package/dist/console.js +0 -446
- package/dist/console.js.map +0 -1
- package/dist/event.js +0 -454
- package/dist/event.js.map +0 -1
- package/dist/index.d.ts +0 -137
- package/dist/index.js +0 -683
- package/dist/index.js.map +0 -1
- package/dist/util.d.ts +0 -17
- package/dist/util.js +0 -72
- package/dist/util.js.map +0 -1
- package/src/asset-mixin.ts +0 -132
- package/src/console.d.ts +0 -56
- package/src/console.js +0 -451
- package/src/console.js.map +0 -1
- package/src/console.ts +0 -530
- package/src/event.d.ts +0 -75
- package/src/event.js +0 -410
- package/src/event.js.map +0 -1
- package/src/event.ts +0 -584
- package/src/index.d.ts +0 -110
- package/src/index.js +0 -525
- package/src/index.js.map +0 -1
- package/src/index.ts +0 -803
- package/src/util.d.ts +0 -15
- package/src/util.js +0 -46
- package/src/util.js.map +0 -1
- package/src/util.ts +0 -94
- package/tsconfig.json +0 -14
- package/tsconfig.tsbuildinfo +0 -9788
package/src/console.ts
DELETED
|
@@ -1,530 +0,0 @@
|
|
|
1
|
-
import {ConsoleRegistration} from "@openremote/model";
|
|
2
|
-
import rest from "@openremote/rest";
|
|
3
|
-
import openremote from "./index";
|
|
4
|
-
import {AxiosResponse} from "axios";
|
|
5
|
-
|
|
6
|
-
declare function require(name: string): any;
|
|
7
|
-
|
|
8
|
-
// No ES6 module support in platform lib
|
|
9
|
-
let platform = require('platform');
|
|
10
|
-
|
|
11
|
-
export interface ProviderMessage {
|
|
12
|
-
provider: string;
|
|
13
|
-
action: string;
|
|
14
|
-
data?: any;
|
|
15
|
-
[x: string]: any;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface ProviderInitialiseResponse extends ProviderMessage {
|
|
19
|
-
version: string;
|
|
20
|
-
requiresPermission: boolean;
|
|
21
|
-
hasPermission: boolean;
|
|
22
|
-
success: boolean;
|
|
23
|
-
enabled: boolean;
|
|
24
|
-
disabled: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface ProviderEnableRequest extends ProviderMessage {
|
|
28
|
-
consoleId: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface ProviderEnableResponse extends ProviderMessage {
|
|
32
|
-
hasPermission: boolean;
|
|
33
|
-
success: boolean;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class Console {
|
|
37
|
-
|
|
38
|
-
protected _realm: string;
|
|
39
|
-
protected _registration: ConsoleRegistration;
|
|
40
|
-
protected _autoEnable: boolean = false;
|
|
41
|
-
protected _initialised: boolean = false;
|
|
42
|
-
protected _initialiseInProgress: boolean = false;
|
|
43
|
-
protected _pendingProviderPromises: { [name: string]: ((response: any) => void) | null } = {};
|
|
44
|
-
protected _pendingProviderEnables: string[] = [];
|
|
45
|
-
protected _enableCompleteCallback: (() => void) | null;
|
|
46
|
-
protected _registrationTimer: number | null = null;
|
|
47
|
-
|
|
48
|
-
constructor(realm: string, autoEnable: boolean, enableComplete: () => void) {
|
|
49
|
-
|
|
50
|
-
this._realm = realm;
|
|
51
|
-
this._autoEnable = autoEnable;
|
|
52
|
-
this._enableCompleteCallback = enableComplete;
|
|
53
|
-
|
|
54
|
-
// Export this to the window to make it accessible from mobile webview code
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
window.OpenRemoteConsole = this;
|
|
57
|
-
|
|
58
|
-
// Check for query parameters to override values
|
|
59
|
-
let queryParams = new URLSearchParams(window.location.search);
|
|
60
|
-
let consoleName = queryParams.get("consoleName");
|
|
61
|
-
let consoleVersion = queryParams.get("consoleVersion");
|
|
62
|
-
let consolePlatform = queryParams.get("consolePlatform");
|
|
63
|
-
let consoleProviders = queryParams.get("consoleProviders");
|
|
64
|
-
let autoEnableStr = queryParams.get("consoleAutoEnable");
|
|
65
|
-
|
|
66
|
-
let requestedProviders = consoleProviders && consoleProviders.length > 0 ? consoleProviders.split(" ") : ["push", "storage"];
|
|
67
|
-
|
|
68
|
-
if (requestedProviders.indexOf("storage") < 0) {
|
|
69
|
-
requestedProviders.push("storage"); // Storage provider is essential to operation and should always be available
|
|
70
|
-
}
|
|
71
|
-
this._pendingProviderEnables = consoleProviders && consoleProviders.length > 0 ? consoleProviders.split(" ") : [];
|
|
72
|
-
|
|
73
|
-
// Look for existing console registration in local storage or just create a new one
|
|
74
|
-
let consoleReg: ConsoleRegistration = Console._createConsoleRegistration();
|
|
75
|
-
|
|
76
|
-
let consoleRegStr = window.localStorage.getItem("OpenRemoteConsole:" + realm);
|
|
77
|
-
if (consoleRegStr) {
|
|
78
|
-
try {
|
|
79
|
-
let storedRegObj = JSON.parse(consoleRegStr);
|
|
80
|
-
let storedReg = storedRegObj as ConsoleRegistration;
|
|
81
|
-
if (storedReg.id) {
|
|
82
|
-
consoleReg.id = storedReg.id;
|
|
83
|
-
}
|
|
84
|
-
if (storedReg.name) {
|
|
85
|
-
consoleReg.name = storedReg.name;
|
|
86
|
-
}
|
|
87
|
-
if (storedReg.providers) {
|
|
88
|
-
consoleReg.providers = storedReg.providers;
|
|
89
|
-
}
|
|
90
|
-
if (storedReg.apps) {
|
|
91
|
-
consoleReg.apps = storedReg.apps;
|
|
92
|
-
}
|
|
93
|
-
} catch (e) {
|
|
94
|
-
console.error("Failed to deserialise console registration");
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
let oldProviders = consoleReg.providers;
|
|
99
|
-
consoleReg.providers = {};
|
|
100
|
-
|
|
101
|
-
for (let providerName of requestedProviders) {
|
|
102
|
-
let provider = oldProviders && oldProviders.hasOwnProperty(providerName) ? oldProviders[providerName] : {
|
|
103
|
-
enabled: false,
|
|
104
|
-
disabled: false
|
|
105
|
-
};
|
|
106
|
-
consoleReg.providers[providerName] = provider;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let appName = openremote.getAppName();
|
|
110
|
-
if (appName.length > 0 && consoleReg.apps!.indexOf(appName) < 0) {
|
|
111
|
-
consoleReg.apps!.push(appName);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
this._registration = consoleReg;
|
|
115
|
-
|
|
116
|
-
if (consoleName) {
|
|
117
|
-
consoleReg.name = consoleName;
|
|
118
|
-
}
|
|
119
|
-
if (consoleVersion) {
|
|
120
|
-
consoleReg.version = consoleVersion;
|
|
121
|
-
}
|
|
122
|
-
if (consolePlatform) {
|
|
123
|
-
consoleReg.platform = consolePlatform;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (autoEnableStr) {
|
|
127
|
-
this._autoEnable = autoEnableStr === "TRUE" || autoEnableStr === "true";
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
get registration(): ConsoleRegistration {
|
|
132
|
-
return this._registration;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
public get autoEnable(): boolean {
|
|
136
|
-
return this._autoEnable;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
get pendingProviderEnables(): string[] {
|
|
140
|
-
return this._pendingProviderEnables.slice(0);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public get shellApple(): boolean {
|
|
144
|
-
// @ts-ignore
|
|
145
|
-
return navigator.platform.substr(0, 2) === 'iP' && window.webkit && window.webkit.messageHandlers;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public get shellAndroid(): boolean {
|
|
149
|
-
// @ts-ignore
|
|
150
|
-
return !!window.MobileInterface;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public get isMobile(): boolean {
|
|
154
|
-
return this.shellApple || this.shellAndroid;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public async initialise(): Promise<void> {
|
|
158
|
-
if (this._initialised || this._initialiseInProgress) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
console.debug("Console: initialising");
|
|
163
|
-
this._initialiseInProgress = true;
|
|
164
|
-
|
|
165
|
-
try {
|
|
166
|
-
// Get an ID for this console if it doesn't have one
|
|
167
|
-
if (!this._registration.id) {
|
|
168
|
-
await this.sendRegistration(0);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (this._registration.providers) {
|
|
172
|
-
for (let providerName of Object.keys(this._registration.providers)) {
|
|
173
|
-
await this._initialiseProvider(providerName);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
this._initialised = true;
|
|
178
|
-
this._initialiseInProgress = false;
|
|
179
|
-
|
|
180
|
-
if (this._pendingProviderEnables.length === 0) {
|
|
181
|
-
this.sendRegistration();
|
|
182
|
-
this._callCompletedCallback();
|
|
183
|
-
} else if (this._autoEnable) {
|
|
184
|
-
await this.enableProviders();
|
|
185
|
-
}
|
|
186
|
-
} catch (e) {
|
|
187
|
-
console.error(e);
|
|
188
|
-
} finally {
|
|
189
|
-
this._initialiseInProgress = false;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// This mechanism doesn't support sending extra data to the providers being enabled and it doesn't allow data
|
|
195
|
-
// to be retrieved from the replies; if that is required then providers should be manually enabled from the calling app
|
|
196
|
-
public async enableProviders(): Promise<void> {
|
|
197
|
-
if (!this._initialised) {
|
|
198
|
-
throw new Error("Console must be initialised before enabling providers");
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
for (let providerName of this._pendingProviderEnables) {
|
|
202
|
-
await this.enableProvider(providerName);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
public async enableProvider(providerName: string, data?: any): Promise<ProviderEnableResponse> {
|
|
207
|
-
if (!this._initialised) {
|
|
208
|
-
console.debug("Console must be initialised before disabling providers");
|
|
209
|
-
throw new Error("Console must be initialised before enabling providers");
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (!this._registration.providers!.hasOwnProperty(providerName)) {
|
|
213
|
-
console.debug("Invalid console provider '" + providerName + "'");
|
|
214
|
-
throw new Error("Invalid console provider '" + providerName + "'");
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
console.debug("Console: enabling provider '" + providerName + "'");
|
|
218
|
-
let msg: ProviderEnableRequest = {
|
|
219
|
-
provider: providerName,
|
|
220
|
-
action: "PROVIDER_ENABLE",
|
|
221
|
-
consoleId: this._registration.id!,
|
|
222
|
-
data
|
|
223
|
-
};
|
|
224
|
-
let response = await this.sendProviderMessage(msg, true);
|
|
225
|
-
|
|
226
|
-
this._registration.providers![providerName].hasPermission = response.hasPermission;
|
|
227
|
-
this._registration.providers![providerName].success = response.success;
|
|
228
|
-
this._registration.providers![providerName].enabled = response.success;
|
|
229
|
-
|
|
230
|
-
let index = this._pendingProviderEnables.indexOf(providerName);
|
|
231
|
-
|
|
232
|
-
if (index >= 0) {
|
|
233
|
-
this._pendingProviderEnables.splice(index, 1);
|
|
234
|
-
if (this._pendingProviderEnables.length === 0) {
|
|
235
|
-
this.sendRegistration();
|
|
236
|
-
this._callCompletedCallback();
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return response;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
public async disableProvider(provider: string): Promise<ProviderMessage> {
|
|
244
|
-
if (!this._initialised) {
|
|
245
|
-
console.debug("Console must be initialised before disabling providers");
|
|
246
|
-
throw new Error("Console must be initialised before disabling providers");
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (!this._registration.providers!.hasOwnProperty(provider)) {
|
|
250
|
-
console.debug("Invalid console provider '" + provider + "'");
|
|
251
|
-
throw new Error("Invalid console provider '" + provider + "'");
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
console.debug("Console: disabling provider '" + provider + "'");
|
|
255
|
-
|
|
256
|
-
let response = await this.sendProviderMessage({
|
|
257
|
-
provider: provider,
|
|
258
|
-
action: "PROVIDER_DISABLE"
|
|
259
|
-
}, true);
|
|
260
|
-
this._registration.providers![provider].disabled = true;
|
|
261
|
-
this._registration.providers![provider].enabled = false;
|
|
262
|
-
return response;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
public async sendProviderMessage(message: ProviderMessage, waitForResponse: boolean): Promise<any | null> {
|
|
266
|
-
if (!this._registration.providers!.hasOwnProperty(message.provider)) {
|
|
267
|
-
console.debug("Invalid console provider '" + message.provider + "'");
|
|
268
|
-
throw new Error("Invalid console provider '" + message.provider + "'");
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (!waitForResponse) {
|
|
272
|
-
this._doSendProviderMessage(message);
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
let promiseName = message.provider + message.action;
|
|
277
|
-
|
|
278
|
-
if (this._pendingProviderPromises[promiseName]) {
|
|
279
|
-
throw new Error("Message already pending for provider '" + name + "' with action '" + message.action + "'");
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
return await new Promise(resolve => {
|
|
283
|
-
this._pendingProviderPromises[promiseName] = resolve;
|
|
284
|
-
this._doSendProviderMessage(message);
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// Uses a delayed mechanism to avoid excessive calls to the server during enabling providers
|
|
289
|
-
public sendRegistration(delay?: number): Promise<void> {
|
|
290
|
-
|
|
291
|
-
if (this._registrationTimer) {
|
|
292
|
-
window.clearTimeout(this._registrationTimer);
|
|
293
|
-
this._registrationTimer = null;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
delay = delay !== undefined ? delay : 2000;
|
|
297
|
-
|
|
298
|
-
console.debug("Sending registration in: " + delay + "ms");
|
|
299
|
-
|
|
300
|
-
return new Promise((resolve, reject) => {
|
|
301
|
-
this._registrationTimer = window.setTimeout(() => {
|
|
302
|
-
this._registrationTimer = null;
|
|
303
|
-
console.debug("Console: updating registration");
|
|
304
|
-
|
|
305
|
-
try {
|
|
306
|
-
rest.api.ConsoleResource.register(this._registration).then((response: AxiosResponse<ConsoleRegistration>) => {
|
|
307
|
-
if (response.status !== 200) {
|
|
308
|
-
throw new Error("Failed to register console");
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
this._registration = response.data;
|
|
312
|
-
console.debug("Console: registration successful");
|
|
313
|
-
console.debug("Console: updating locally stored registration");
|
|
314
|
-
window.localStorage.setItem("OpenRemoteConsole:" + this._realm, JSON.stringify(this._registration));
|
|
315
|
-
resolve();
|
|
316
|
-
});
|
|
317
|
-
} catch (e) {
|
|
318
|
-
console.error("Failed to register console");
|
|
319
|
-
reject("Failed to register console");
|
|
320
|
-
}
|
|
321
|
-
},);
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
public storeData(key: string, value: string | null) {
|
|
326
|
-
this.sendProviderMessage({
|
|
327
|
-
provider: "storage",
|
|
328
|
-
action: "STORE",
|
|
329
|
-
key: key,
|
|
330
|
-
value: value
|
|
331
|
-
}, false);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
public async retrieveData(key: string): Promise<string | null> {
|
|
336
|
-
let response = await this.sendProviderMessage({
|
|
337
|
-
provider: "storage",
|
|
338
|
-
action: "RETRIEVE",
|
|
339
|
-
key: key
|
|
340
|
-
}, true);
|
|
341
|
-
|
|
342
|
-
if (response && response.hasOwnProperty("value")) {
|
|
343
|
-
return JSON.parse(response.value);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
return null;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
protected _postNativeShellMessage(jsonMessage: any) {
|
|
350
|
-
if (this.shellAndroid) {
|
|
351
|
-
// @ts-ignore
|
|
352
|
-
return window.MobileInterface.postMessage(JSON.stringify(jsonMessage));
|
|
353
|
-
}
|
|
354
|
-
if (this.shellApple) {
|
|
355
|
-
// @ts-ignore
|
|
356
|
-
return window.webkit.messageHandlers.int.postMessage(jsonMessage);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
protected _doSendProviderMessage(msg: any) {
|
|
361
|
-
if (this.isMobile) {
|
|
362
|
-
this._postNativeShellMessage({type: "provider", data: msg});
|
|
363
|
-
} else {
|
|
364
|
-
if (!msg.provider || !msg.action) {
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
switch (msg.provider.trim().toUpperCase()) {
|
|
368
|
-
// TODO: Implement web browser provider handling (web push etc.)
|
|
369
|
-
case "PUSH":
|
|
370
|
-
|
|
371
|
-
switch (msg.action.trim().toUpperCase()) {
|
|
372
|
-
case "PROVIDER_INIT":
|
|
373
|
-
let initResponse: ProviderInitialiseResponse = {
|
|
374
|
-
action: "PROVIDER_INIT",
|
|
375
|
-
provider: "push",
|
|
376
|
-
version: "web",
|
|
377
|
-
enabled: true,
|
|
378
|
-
disabled: false,
|
|
379
|
-
hasPermission: true,
|
|
380
|
-
requiresPermission: false,
|
|
381
|
-
success: true
|
|
382
|
-
};
|
|
383
|
-
this._handleProviderResponse(JSON.stringify(initResponse));
|
|
384
|
-
break;
|
|
385
|
-
case "PROVIDER_ENABLE":
|
|
386
|
-
let enableResponse: ProviderEnableResponse = {
|
|
387
|
-
action: "PROVIDER_ENABLE",
|
|
388
|
-
provider: "push",
|
|
389
|
-
hasPermission: true,
|
|
390
|
-
success: true
|
|
391
|
-
};
|
|
392
|
-
this._handleProviderResponse(JSON.stringify(enableResponse));
|
|
393
|
-
break;
|
|
394
|
-
default:
|
|
395
|
-
throw new Error("Unsupported provider '" + msg.provider + "' and action '" + msg.action + "'");
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
break;
|
|
399
|
-
|
|
400
|
-
case "STORAGE":
|
|
401
|
-
|
|
402
|
-
switch (msg.action) {
|
|
403
|
-
case "PROVIDER_INIT":
|
|
404
|
-
let initResponse: ProviderInitialiseResponse = {
|
|
405
|
-
action: "PROVIDER_INIT",
|
|
406
|
-
provider: "storage",
|
|
407
|
-
version: "1.0.0",
|
|
408
|
-
disabled: false,
|
|
409
|
-
enabled: true,
|
|
410
|
-
hasPermission: true,
|
|
411
|
-
requiresPermission: false,
|
|
412
|
-
success: true
|
|
413
|
-
};
|
|
414
|
-
this._handleProviderResponse(JSON.stringify(initResponse));
|
|
415
|
-
break;
|
|
416
|
-
case "PROVIDER_ENABLE":
|
|
417
|
-
let enableResponse: ProviderEnableResponse = {
|
|
418
|
-
action: "PROVIDER_ENABLE",
|
|
419
|
-
provider: "storage",
|
|
420
|
-
hasPermission: true,
|
|
421
|
-
success: true
|
|
422
|
-
};
|
|
423
|
-
this._handleProviderResponse(JSON.stringify(enableResponse));
|
|
424
|
-
break;
|
|
425
|
-
case "STORE":
|
|
426
|
-
let keyValue = msg.key ? msg.key.trim() : null;
|
|
427
|
-
|
|
428
|
-
if (!keyValue || keyValue.length === 0) {
|
|
429
|
-
throw new Error("Storage provider 'store' action requires a `key`");
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
if (msg.hasOwnProperty("value")) {
|
|
433
|
-
if (msg.value === null) {
|
|
434
|
-
window.localStorage.removeItem(keyValue);
|
|
435
|
-
} else {
|
|
436
|
-
window.localStorage.setItem(keyValue, msg.value);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
break;
|
|
441
|
-
case "RETRIEVE":
|
|
442
|
-
keyValue = msg.key ? msg.key.trim() : null;
|
|
443
|
-
|
|
444
|
-
if (!keyValue || keyValue.length === 0) {
|
|
445
|
-
throw new Error("Storage provider 'retrieve' action requires a `key`");
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
this._handleProviderResponse(JSON.stringify({
|
|
449
|
-
action: "RETRIEVE",
|
|
450
|
-
provider: "storage",
|
|
451
|
-
key: keyValue,
|
|
452
|
-
value: window.localStorage.getItem(keyValue)
|
|
453
|
-
}));
|
|
454
|
-
break;
|
|
455
|
-
default:
|
|
456
|
-
throw new Error("Unsupported provider '" + msg.provider + "' and action '" + msg.action + "'");
|
|
457
|
-
}
|
|
458
|
-
break;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
// This is called by native web view code
|
|
464
|
-
protected _handleProviderResponse(msg: any) {
|
|
465
|
-
if (!msg) {
|
|
466
|
-
return;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
let msgJson = JSON.parse(msg);
|
|
470
|
-
let name = msgJson.provider;
|
|
471
|
-
let action = msgJson.action;
|
|
472
|
-
|
|
473
|
-
let resolve = this._pendingProviderPromises[name + action];
|
|
474
|
-
|
|
475
|
-
if (resolve != null) {
|
|
476
|
-
this._pendingProviderPromises[name + action] = null;
|
|
477
|
-
resolve(msgJson);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
protected _callCompletedCallback() {
|
|
482
|
-
let callback = this._enableCompleteCallback;
|
|
483
|
-
this._enableCompleteCallback = null;
|
|
484
|
-
if (callback) {
|
|
485
|
-
window.setTimeout(() => {
|
|
486
|
-
callback!()
|
|
487
|
-
}, 0);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
protected static _createConsoleRegistration(): ConsoleRegistration {
|
|
492
|
-
let reg: ConsoleRegistration = {
|
|
493
|
-
name: platform.name,
|
|
494
|
-
version: platform.version,
|
|
495
|
-
platform: platform.os.toString(),
|
|
496
|
-
apps: [],
|
|
497
|
-
model: ((platform.manufacturer ? platform.manufacturer + " " : "") + (platform.product ? platform.product : "")).trim()
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
return reg;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
protected async _initialiseProvider(providerName: string): Promise<void> {
|
|
504
|
-
console.debug("Console: initialising provider '" + providerName + "'");
|
|
505
|
-
let initResponse = await this.sendProviderMessage({
|
|
506
|
-
provider: providerName,
|
|
507
|
-
action: "PROVIDER_INIT"
|
|
508
|
-
}, true) as ProviderInitialiseResponse;
|
|
509
|
-
|
|
510
|
-
this._registration.providers![providerName].version = initResponse.version;
|
|
511
|
-
this._registration.providers![providerName].requiresPermission = initResponse.requiresPermission;
|
|
512
|
-
this._registration.providers![providerName].hasPermission = initResponse.hasPermission;
|
|
513
|
-
this._registration.providers![providerName].success = initResponse.success;
|
|
514
|
-
this._registration.providers![providerName].enabled = initResponse.enabled;
|
|
515
|
-
this._registration.providers![providerName].disabled = initResponse.disabled;
|
|
516
|
-
|
|
517
|
-
if (!initResponse.success) {
|
|
518
|
-
console.debug("Provider initialisation failed: '" + providerName + "'");
|
|
519
|
-
initResponse.disabled = true;
|
|
520
|
-
this._registration.providers![providerName].disabled = true;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
if (initResponse.disabled || initResponse.enabled) {
|
|
524
|
-
let index = this._pendingProviderEnables.indexOf(providerName);
|
|
525
|
-
if (index >= 0) {
|
|
526
|
-
this._pendingProviderEnables.splice(index, 1);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
}
|
package/src/event.d.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { Deferred } from "./util";
|
|
2
|
-
import { AttributeEvent, EventSubscription, SharedEvent } from "@openremote/model";
|
|
3
|
-
export declare enum EventProviderStatus {
|
|
4
|
-
DISCONNECTED = "DISCONNECTED",
|
|
5
|
-
CONNECTED = "CONNECTED",
|
|
6
|
-
CONNECTING = "CONNECTING"
|
|
7
|
-
}
|
|
8
|
-
export interface EventProvider {
|
|
9
|
-
status: EventProviderStatus;
|
|
10
|
-
connect(): Promise<boolean>;
|
|
11
|
-
disconnect(): void;
|
|
12
|
-
subscribe<T extends SharedEvent>(eventSubscription: EventSubscription<T>, callback: (event: T) => void): Promise<string>;
|
|
13
|
-
unsubscribe<T extends SharedEvent>(subscriptionId: string): void;
|
|
14
|
-
subscribeAttributeEvents(assetIds: string[], callback: (event: AttributeEvent) => void): Promise<string>;
|
|
15
|
-
sendEvent<T extends SharedEvent>(event: T): void;
|
|
16
|
-
}
|
|
17
|
-
interface EventSubscriptionInfo<T extends SharedEvent> {
|
|
18
|
-
eventSubscription: EventSubscription<T>;
|
|
19
|
-
callback: (event: T) => void;
|
|
20
|
-
deferred: Deferred<string> | null;
|
|
21
|
-
}
|
|
22
|
-
declare abstract class EventProviderImpl implements EventProvider {
|
|
23
|
-
protected static MIN_RECONNECT_DELAY: number;
|
|
24
|
-
protected static MAX_RECONNECT_DELAY: number;
|
|
25
|
-
protected _disconnectRequested: boolean;
|
|
26
|
-
protected _reconnectDelayMillis: number;
|
|
27
|
-
protected _reconnectTimer: number | null;
|
|
28
|
-
protected _statusCallback: (status: EventProviderStatus) => void;
|
|
29
|
-
protected _status: EventProviderStatus;
|
|
30
|
-
protected _connectingDeferred: Deferred<boolean> | null;
|
|
31
|
-
protected _pendingSubscription: EventSubscriptionInfo<SharedEvent> | null;
|
|
32
|
-
protected _queuedSubscriptions: EventSubscriptionInfo<SharedEvent>[];
|
|
33
|
-
protected _subscriptionMap: {
|
|
34
|
-
[id: string]: EventSubscriptionInfo<SharedEvent>;
|
|
35
|
-
};
|
|
36
|
-
abstract readonly endpointUrl: string;
|
|
37
|
-
readonly status: EventProviderStatus;
|
|
38
|
-
protected constructor(statusCallback: (status: EventProviderStatus) => void);
|
|
39
|
-
connect(): Promise<boolean>;
|
|
40
|
-
disconnect(): void;
|
|
41
|
-
subscribe<T extends SharedEvent>(eventSubscription: EventSubscription<T>, callback: (event: T) => void): Promise<string>;
|
|
42
|
-
unsubscribe<T extends SharedEvent>(subscriptionId: string): void;
|
|
43
|
-
sendEvent<T extends SharedEvent>(event: T): void;
|
|
44
|
-
subscribeAttributeEvents(assetIds: string[], callback: (event: AttributeEvent) => void): Promise<string>;
|
|
45
|
-
protected _processNextSubscription(): void;
|
|
46
|
-
private _onStatusChanged;
|
|
47
|
-
protected _onMessageReceived(subscriptionId: string, event: SharedEvent): void;
|
|
48
|
-
protected _onConnect(): void;
|
|
49
|
-
protected _onDisconnect(): void;
|
|
50
|
-
protected _scheduleReconnect(): void;
|
|
51
|
-
protected abstract _doSend<T extends SharedEvent>(event: T): void;
|
|
52
|
-
protected abstract _doConnect(): Promise<boolean>;
|
|
53
|
-
protected abstract _doDisconnect(): void;
|
|
54
|
-
protected abstract _doSubscribe<T extends SharedEvent>(subscription: EventSubscription<T>): Promise<string>;
|
|
55
|
-
protected abstract _doUnsubscribe(subscriptionId: string): void;
|
|
56
|
-
}
|
|
57
|
-
export declare class WebSocketEventProvider extends EventProviderImpl {
|
|
58
|
-
protected static _subscriptionCounter: number;
|
|
59
|
-
protected static _subscriptionRenewalMillis: number;
|
|
60
|
-
private readonly _endpointUrl;
|
|
61
|
-
protected _webSocket: WebSocket | undefined;
|
|
62
|
-
protected _connectDeferred: Deferred<boolean> | null;
|
|
63
|
-
protected _subscribeDeferred: Deferred<string> | null;
|
|
64
|
-
protected _renewalTimer: number | null;
|
|
65
|
-
readonly endpointUrl: string;
|
|
66
|
-
constructor(managerUrl: string, statusCallback: (connected: EventProviderStatus) => void);
|
|
67
|
-
protected _doConnect(): Promise<boolean>;
|
|
68
|
-
protected _beforeDisconnect(): void;
|
|
69
|
-
protected _doDisconnect(): void;
|
|
70
|
-
protected _doSubscribe<T extends SharedEvent>(subscription: EventSubscription<T>): Promise<string>;
|
|
71
|
-
protected _doUnsubscribe(subscriptionId: string): void;
|
|
72
|
-
protected _doRenewal(): void;
|
|
73
|
-
protected _doSend<T extends SharedEvent>(event: T): void;
|
|
74
|
-
}
|
|
75
|
-
export {};
|