@cutos/core 0.0.1
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/dist/cutos.cjs.js +1200 -0
- package/dist/cutos.esm.js +1192 -0
- package/package.json +31 -0
|
@@ -0,0 +1,1192 @@
|
|
|
1
|
+
import require$$1 from 'mqtt';
|
|
2
|
+
|
|
3
|
+
function getAugmentedNamespace(n) {
|
|
4
|
+
if (n.__esModule) return n;
|
|
5
|
+
var f = n.default;
|
|
6
|
+
if (typeof f == "function") {
|
|
7
|
+
var a = function a () {
|
|
8
|
+
if (this instanceof a) {
|
|
9
|
+
return Reflect.construct(f, arguments, this.constructor);
|
|
10
|
+
}
|
|
11
|
+
return f.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
a.prototype = f.prototype;
|
|
14
|
+
} else a = {};
|
|
15
|
+
Object.defineProperty(a, '__esModule', {value: true});
|
|
16
|
+
Object.keys(n).forEach(function (k) {
|
|
17
|
+
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
18
|
+
Object.defineProperty(a, k, d.get ? d : {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return n[k];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
return a;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var cutos = {};
|
|
29
|
+
|
|
30
|
+
// Unique ID creation requires a high quality random # generator. In the browser we therefore
|
|
31
|
+
// require the crypto API and do not support built-in fallback to lower quality random number
|
|
32
|
+
// generators (like Math.random()).
|
|
33
|
+
let getRandomValues;
|
|
34
|
+
const rnds8 = new Uint8Array(16);
|
|
35
|
+
function rng() {
|
|
36
|
+
// lazy load so that environments that need to polyfill have a chance to do so
|
|
37
|
+
if (!getRandomValues) {
|
|
38
|
+
// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
|
|
39
|
+
getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
|
|
40
|
+
|
|
41
|
+
if (!getRandomValues) {
|
|
42
|
+
throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return getRandomValues(rnds8);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
50
|
+
|
|
51
|
+
function validate(uuid) {
|
|
52
|
+
return typeof uuid === 'string' && REGEX.test(uuid);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Convert array of 16 byte values to UUID string format of the form:
|
|
57
|
+
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
const byteToHex = [];
|
|
61
|
+
|
|
62
|
+
for (let i = 0; i < 256; ++i) {
|
|
63
|
+
byteToHex.push((i + 0x100).toString(16).slice(1));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function unsafeStringify(arr, offset = 0) {
|
|
67
|
+
// Note: Be careful editing this code! It's been tuned for performance
|
|
68
|
+
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
|
|
69
|
+
return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function stringify(arr, offset = 0) {
|
|
73
|
+
const uuid = unsafeStringify(arr, offset); // Consistency check for valid UUID. If this throws, it's likely due to one
|
|
74
|
+
// of the following:
|
|
75
|
+
// - One or more input array values don't map to a hex octet (leading to
|
|
76
|
+
// "undefined" in the uuid)
|
|
77
|
+
// - Invalid input values for the RFC `version` or `variant` fields
|
|
78
|
+
|
|
79
|
+
if (!validate(uuid)) {
|
|
80
|
+
throw TypeError('Stringified UUID is invalid');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return uuid;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//
|
|
87
|
+
// Inspired by https://github.com/LiosK/UUID.js
|
|
88
|
+
// and http://docs.python.org/library/uuid.html
|
|
89
|
+
|
|
90
|
+
let _nodeId;
|
|
91
|
+
|
|
92
|
+
let _clockseq; // Previous uuid creation time
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
let _lastMSecs = 0;
|
|
96
|
+
let _lastNSecs = 0; // See https://github.com/uuidjs/uuid for API details
|
|
97
|
+
|
|
98
|
+
function v1(options, buf, offset) {
|
|
99
|
+
let i = buf && offset || 0;
|
|
100
|
+
const b = buf || new Array(16);
|
|
101
|
+
options = options || {};
|
|
102
|
+
let node = options.node || _nodeId;
|
|
103
|
+
let clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; // node and clockseq need to be initialized to random values if they're not
|
|
104
|
+
// specified. We do this lazily to minimize issues related to insufficient
|
|
105
|
+
// system entropy. See #189
|
|
106
|
+
|
|
107
|
+
if (node == null || clockseq == null) {
|
|
108
|
+
const seedBytes = options.random || (options.rng || rng)();
|
|
109
|
+
|
|
110
|
+
if (node == null) {
|
|
111
|
+
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
|
|
112
|
+
node = _nodeId = [seedBytes[0] | 0x01, seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (clockseq == null) {
|
|
116
|
+
// Per 4.2.2, randomize (14 bit) clockseq
|
|
117
|
+
clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
|
|
118
|
+
}
|
|
119
|
+
} // UUID timestamps are 100 nano-second units since the Gregorian epoch,
|
|
120
|
+
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
|
|
121
|
+
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
|
|
122
|
+
// (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
let msecs = options.msecs !== undefined ? options.msecs : Date.now(); // Per 4.2.1.2, use count of uuid's generated during the current clock
|
|
126
|
+
// cycle to simulate higher resolution clock
|
|
127
|
+
|
|
128
|
+
let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; // Time since last uuid creation (in msecs)
|
|
129
|
+
|
|
130
|
+
const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; // Per 4.2.1.2, Bump clockseq on clock regression
|
|
131
|
+
|
|
132
|
+
if (dt < 0 && options.clockseq === undefined) {
|
|
133
|
+
clockseq = clockseq + 1 & 0x3fff;
|
|
134
|
+
} // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
|
|
135
|
+
// time interval
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
|
|
139
|
+
nsecs = 0;
|
|
140
|
+
} // Per 4.2.1.2 Throw error if too many uuids are requested
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if (nsecs >= 10000) {
|
|
144
|
+
throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_lastMSecs = msecs;
|
|
148
|
+
_lastNSecs = nsecs;
|
|
149
|
+
_clockseq = clockseq; // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
|
|
150
|
+
|
|
151
|
+
msecs += 12219292800000; // `time_low`
|
|
152
|
+
|
|
153
|
+
const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
|
|
154
|
+
b[i++] = tl >>> 24 & 0xff;
|
|
155
|
+
b[i++] = tl >>> 16 & 0xff;
|
|
156
|
+
b[i++] = tl >>> 8 & 0xff;
|
|
157
|
+
b[i++] = tl & 0xff; // `time_mid`
|
|
158
|
+
|
|
159
|
+
const tmh = msecs / 0x100000000 * 10000 & 0xfffffff;
|
|
160
|
+
b[i++] = tmh >>> 8 & 0xff;
|
|
161
|
+
b[i++] = tmh & 0xff; // `time_high_and_version`
|
|
162
|
+
|
|
163
|
+
b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
|
|
164
|
+
|
|
165
|
+
b[i++] = tmh >>> 16 & 0xff; // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
|
|
166
|
+
|
|
167
|
+
b[i++] = clockseq >>> 8 | 0x80; // `clock_seq_low`
|
|
168
|
+
|
|
169
|
+
b[i++] = clockseq & 0xff; // `node`
|
|
170
|
+
|
|
171
|
+
for (let n = 0; n < 6; ++n) {
|
|
172
|
+
b[i + n] = node[n];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return buf || unsafeStringify(b);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function parse(uuid) {
|
|
179
|
+
if (!validate(uuid)) {
|
|
180
|
+
throw TypeError('Invalid UUID');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let v;
|
|
184
|
+
const arr = new Uint8Array(16); // Parse ########-....-....-....-............
|
|
185
|
+
|
|
186
|
+
arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
|
|
187
|
+
arr[1] = v >>> 16 & 0xff;
|
|
188
|
+
arr[2] = v >>> 8 & 0xff;
|
|
189
|
+
arr[3] = v & 0xff; // Parse ........-####-....-....-............
|
|
190
|
+
|
|
191
|
+
arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
|
|
192
|
+
arr[5] = v & 0xff; // Parse ........-....-####-....-............
|
|
193
|
+
|
|
194
|
+
arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
|
|
195
|
+
arr[7] = v & 0xff; // Parse ........-....-....-####-............
|
|
196
|
+
|
|
197
|
+
arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
|
|
198
|
+
arr[9] = v & 0xff; // Parse ........-....-....-....-############
|
|
199
|
+
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
|
|
200
|
+
|
|
201
|
+
arr[10] = (v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000 & 0xff;
|
|
202
|
+
arr[11] = v / 0x100000000 & 0xff;
|
|
203
|
+
arr[12] = v >>> 24 & 0xff;
|
|
204
|
+
arr[13] = v >>> 16 & 0xff;
|
|
205
|
+
arr[14] = v >>> 8 & 0xff;
|
|
206
|
+
arr[15] = v & 0xff;
|
|
207
|
+
return arr;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function stringToBytes(str) {
|
|
211
|
+
str = unescape(encodeURIComponent(str)); // UTF8 escape
|
|
212
|
+
|
|
213
|
+
const bytes = [];
|
|
214
|
+
|
|
215
|
+
for (let i = 0; i < str.length; ++i) {
|
|
216
|
+
bytes.push(str.charCodeAt(i));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return bytes;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
|
223
|
+
const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
|
|
224
|
+
function v35(name, version, hashfunc) {
|
|
225
|
+
function generateUUID(value, namespace, buf, offset) {
|
|
226
|
+
var _namespace;
|
|
227
|
+
|
|
228
|
+
if (typeof value === 'string') {
|
|
229
|
+
value = stringToBytes(value);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (typeof namespace === 'string') {
|
|
233
|
+
namespace = parse(namespace);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (((_namespace = namespace) === null || _namespace === void 0 ? void 0 : _namespace.length) !== 16) {
|
|
237
|
+
throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
|
|
238
|
+
} // Compute hash of namespace and value, Per 4.3
|
|
239
|
+
// Future: Use spread syntax when supported on all platforms, e.g. `bytes =
|
|
240
|
+
// hashfunc([...namespace, ... value])`
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
let bytes = new Uint8Array(16 + value.length);
|
|
244
|
+
bytes.set(namespace);
|
|
245
|
+
bytes.set(value, namespace.length);
|
|
246
|
+
bytes = hashfunc(bytes);
|
|
247
|
+
bytes[6] = bytes[6] & 0x0f | version;
|
|
248
|
+
bytes[8] = bytes[8] & 0x3f | 0x80;
|
|
249
|
+
|
|
250
|
+
if (buf) {
|
|
251
|
+
offset = offset || 0;
|
|
252
|
+
|
|
253
|
+
for (let i = 0; i < 16; ++i) {
|
|
254
|
+
buf[offset + i] = bytes[i];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return buf;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return unsafeStringify(bytes);
|
|
261
|
+
} // Function#name is not settable on some platforms (#270)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
try {
|
|
265
|
+
generateUUID.name = name; // eslint-disable-next-line no-empty
|
|
266
|
+
} catch (err) {} // For CommonJS default export support
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
generateUUID.DNS = DNS;
|
|
270
|
+
generateUUID.URL = URL;
|
|
271
|
+
return generateUUID;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/*
|
|
275
|
+
* Browser-compatible JavaScript MD5
|
|
276
|
+
*
|
|
277
|
+
* Modification of JavaScript MD5
|
|
278
|
+
* https://github.com/blueimp/JavaScript-MD5
|
|
279
|
+
*
|
|
280
|
+
* Copyright 2011, Sebastian Tschan
|
|
281
|
+
* https://blueimp.net
|
|
282
|
+
*
|
|
283
|
+
* Licensed under the MIT license:
|
|
284
|
+
* https://opensource.org/licenses/MIT
|
|
285
|
+
*
|
|
286
|
+
* Based on
|
|
287
|
+
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
|
|
288
|
+
* Digest Algorithm, as defined in RFC 1321.
|
|
289
|
+
* Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
|
|
290
|
+
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
|
291
|
+
* Distributed under the BSD License
|
|
292
|
+
* See http://pajhome.org.uk/crypt/md5 for more info.
|
|
293
|
+
*/
|
|
294
|
+
function md5(bytes) {
|
|
295
|
+
if (typeof bytes === 'string') {
|
|
296
|
+
const msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
|
|
297
|
+
|
|
298
|
+
bytes = new Uint8Array(msg.length);
|
|
299
|
+
|
|
300
|
+
for (let i = 0; i < msg.length; ++i) {
|
|
301
|
+
bytes[i] = msg.charCodeAt(i);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return md5ToHexEncodedArray(wordsToMd5(bytesToWords(bytes), bytes.length * 8));
|
|
306
|
+
}
|
|
307
|
+
/*
|
|
308
|
+
* Convert an array of little-endian words to an array of bytes
|
|
309
|
+
*/
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
function md5ToHexEncodedArray(input) {
|
|
313
|
+
const output = [];
|
|
314
|
+
const length32 = input.length * 32;
|
|
315
|
+
const hexTab = '0123456789abcdef';
|
|
316
|
+
|
|
317
|
+
for (let i = 0; i < length32; i += 8) {
|
|
318
|
+
const x = input[i >> 5] >>> i % 32 & 0xff;
|
|
319
|
+
const hex = parseInt(hexTab.charAt(x >>> 4 & 0x0f) + hexTab.charAt(x & 0x0f), 16);
|
|
320
|
+
output.push(hex);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return output;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Calculate output length with padding and bit length
|
|
327
|
+
*/
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
function getOutputLength(inputLength8) {
|
|
331
|
+
return (inputLength8 + 64 >>> 9 << 4) + 14 + 1;
|
|
332
|
+
}
|
|
333
|
+
/*
|
|
334
|
+
* Calculate the MD5 of an array of little-endian words, and a bit length.
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
function wordsToMd5(x, len) {
|
|
339
|
+
/* append padding */
|
|
340
|
+
x[len >> 5] |= 0x80 << len % 32;
|
|
341
|
+
x[getOutputLength(len) - 1] = len;
|
|
342
|
+
let a = 1732584193;
|
|
343
|
+
let b = -271733879;
|
|
344
|
+
let c = -1732584194;
|
|
345
|
+
let d = 271733878;
|
|
346
|
+
|
|
347
|
+
for (let i = 0; i < x.length; i += 16) {
|
|
348
|
+
const olda = a;
|
|
349
|
+
const oldb = b;
|
|
350
|
+
const oldc = c;
|
|
351
|
+
const oldd = d;
|
|
352
|
+
a = md5ff(a, b, c, d, x[i], 7, -680876936);
|
|
353
|
+
d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
|
|
354
|
+
c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
|
|
355
|
+
b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
|
|
356
|
+
a = md5ff(a, b, c, d, x[i + 4], 7, -176418897);
|
|
357
|
+
d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
|
|
358
|
+
c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
|
|
359
|
+
b = md5ff(b, c, d, a, x[i + 7], 22, -45705983);
|
|
360
|
+
a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
|
|
361
|
+
d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
|
|
362
|
+
c = md5ff(c, d, a, b, x[i + 10], 17, -42063);
|
|
363
|
+
b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
|
|
364
|
+
a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
|
|
365
|
+
d = md5ff(d, a, b, c, x[i + 13], 12, -40341101);
|
|
366
|
+
c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
|
|
367
|
+
b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
|
|
368
|
+
a = md5gg(a, b, c, d, x[i + 1], 5, -165796510);
|
|
369
|
+
d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
|
|
370
|
+
c = md5gg(c, d, a, b, x[i + 11], 14, 643717713);
|
|
371
|
+
b = md5gg(b, c, d, a, x[i], 20, -373897302);
|
|
372
|
+
a = md5gg(a, b, c, d, x[i + 5], 5, -701558691);
|
|
373
|
+
d = md5gg(d, a, b, c, x[i + 10], 9, 38016083);
|
|
374
|
+
c = md5gg(c, d, a, b, x[i + 15], 14, -660478335);
|
|
375
|
+
b = md5gg(b, c, d, a, x[i + 4], 20, -405537848);
|
|
376
|
+
a = md5gg(a, b, c, d, x[i + 9], 5, 568446438);
|
|
377
|
+
d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
|
|
378
|
+
c = md5gg(c, d, a, b, x[i + 3], 14, -187363961);
|
|
379
|
+
b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
|
|
380
|
+
a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
|
|
381
|
+
d = md5gg(d, a, b, c, x[i + 2], 9, -51403784);
|
|
382
|
+
c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
|
|
383
|
+
b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
|
|
384
|
+
a = md5hh(a, b, c, d, x[i + 5], 4, -378558);
|
|
385
|
+
d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
|
|
386
|
+
c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
|
|
387
|
+
b = md5hh(b, c, d, a, x[i + 14], 23, -35309556);
|
|
388
|
+
a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
|
|
389
|
+
d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
|
|
390
|
+
c = md5hh(c, d, a, b, x[i + 7], 16, -155497632);
|
|
391
|
+
b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
|
|
392
|
+
a = md5hh(a, b, c, d, x[i + 13], 4, 681279174);
|
|
393
|
+
d = md5hh(d, a, b, c, x[i], 11, -358537222);
|
|
394
|
+
c = md5hh(c, d, a, b, x[i + 3], 16, -722521979);
|
|
395
|
+
b = md5hh(b, c, d, a, x[i + 6], 23, 76029189);
|
|
396
|
+
a = md5hh(a, b, c, d, x[i + 9], 4, -640364487);
|
|
397
|
+
d = md5hh(d, a, b, c, x[i + 12], 11, -421815835);
|
|
398
|
+
c = md5hh(c, d, a, b, x[i + 15], 16, 530742520);
|
|
399
|
+
b = md5hh(b, c, d, a, x[i + 2], 23, -995338651);
|
|
400
|
+
a = md5ii(a, b, c, d, x[i], 6, -198630844);
|
|
401
|
+
d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
|
|
402
|
+
c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
|
|
403
|
+
b = md5ii(b, c, d, a, x[i + 5], 21, -57434055);
|
|
404
|
+
a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
|
|
405
|
+
d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
|
|
406
|
+
c = md5ii(c, d, a, b, x[i + 10], 15, -1051523);
|
|
407
|
+
b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
|
|
408
|
+
a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
|
|
409
|
+
d = md5ii(d, a, b, c, x[i + 15], 10, -30611744);
|
|
410
|
+
c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
|
|
411
|
+
b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
|
|
412
|
+
a = md5ii(a, b, c, d, x[i + 4], 6, -145523070);
|
|
413
|
+
d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
|
|
414
|
+
c = md5ii(c, d, a, b, x[i + 2], 15, 718787259);
|
|
415
|
+
b = md5ii(b, c, d, a, x[i + 9], 21, -343485551);
|
|
416
|
+
a = safeAdd(a, olda);
|
|
417
|
+
b = safeAdd(b, oldb);
|
|
418
|
+
c = safeAdd(c, oldc);
|
|
419
|
+
d = safeAdd(d, oldd);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return [a, b, c, d];
|
|
423
|
+
}
|
|
424
|
+
/*
|
|
425
|
+
* Convert an array bytes to an array of little-endian words
|
|
426
|
+
* Characters >255 have their high-byte silently ignored.
|
|
427
|
+
*/
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
function bytesToWords(input) {
|
|
431
|
+
if (input.length === 0) {
|
|
432
|
+
return [];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const length8 = input.length * 8;
|
|
436
|
+
const output = new Uint32Array(getOutputLength(length8));
|
|
437
|
+
|
|
438
|
+
for (let i = 0; i < length8; i += 8) {
|
|
439
|
+
output[i >> 5] |= (input[i / 8] & 0xff) << i % 32;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return output;
|
|
443
|
+
}
|
|
444
|
+
/*
|
|
445
|
+
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
|
|
446
|
+
* to work around bugs in some JS interpreters.
|
|
447
|
+
*/
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
function safeAdd(x, y) {
|
|
451
|
+
const lsw = (x & 0xffff) + (y & 0xffff);
|
|
452
|
+
const msw = (x >> 16) + (y >> 16) + (lsw >> 16);
|
|
453
|
+
return msw << 16 | lsw & 0xffff;
|
|
454
|
+
}
|
|
455
|
+
/*
|
|
456
|
+
* Bitwise rotate a 32-bit number to the left.
|
|
457
|
+
*/
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
function bitRotateLeft(num, cnt) {
|
|
461
|
+
return num << cnt | num >>> 32 - cnt;
|
|
462
|
+
}
|
|
463
|
+
/*
|
|
464
|
+
* These functions implement the four basic operations the algorithm uses.
|
|
465
|
+
*/
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
function md5cmn(q, a, b, x, s, t) {
|
|
469
|
+
return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function md5ff(a, b, c, d, x, s, t) {
|
|
473
|
+
return md5cmn(b & c | ~b & d, a, b, x, s, t);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function md5gg(a, b, c, d, x, s, t) {
|
|
477
|
+
return md5cmn(b & d | c & ~d, a, b, x, s, t);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function md5hh(a, b, c, d, x, s, t) {
|
|
481
|
+
return md5cmn(b ^ c ^ d, a, b, x, s, t);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function md5ii(a, b, c, d, x, s, t) {
|
|
485
|
+
return md5cmn(c ^ (b | ~d), a, b, x, s, t);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const v3 = v35('v3', 0x30, md5);
|
|
489
|
+
var v3$1 = v3;
|
|
490
|
+
|
|
491
|
+
const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
492
|
+
var native = {
|
|
493
|
+
randomUUID
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
function v4(options, buf, offset) {
|
|
497
|
+
if (native.randomUUID && !buf && !options) {
|
|
498
|
+
return native.randomUUID();
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
options = options || {};
|
|
502
|
+
const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
|
|
503
|
+
|
|
504
|
+
rnds[6] = rnds[6] & 0x0f | 0x40;
|
|
505
|
+
rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
|
|
506
|
+
|
|
507
|
+
if (buf) {
|
|
508
|
+
offset = offset || 0;
|
|
509
|
+
|
|
510
|
+
for (let i = 0; i < 16; ++i) {
|
|
511
|
+
buf[offset + i] = rnds[i];
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return buf;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return unsafeStringify(rnds);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Adapted from Chris Veness' SHA1 code at
|
|
521
|
+
// http://www.movable-type.co.uk/scripts/sha1.html
|
|
522
|
+
function f(s, x, y, z) {
|
|
523
|
+
switch (s) {
|
|
524
|
+
case 0:
|
|
525
|
+
return x & y ^ ~x & z;
|
|
526
|
+
|
|
527
|
+
case 1:
|
|
528
|
+
return x ^ y ^ z;
|
|
529
|
+
|
|
530
|
+
case 2:
|
|
531
|
+
return x & y ^ x & z ^ y & z;
|
|
532
|
+
|
|
533
|
+
case 3:
|
|
534
|
+
return x ^ y ^ z;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function ROTL(x, n) {
|
|
539
|
+
return x << n | x >>> 32 - n;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function sha1(bytes) {
|
|
543
|
+
const K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
|
|
544
|
+
const H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
|
|
545
|
+
|
|
546
|
+
if (typeof bytes === 'string') {
|
|
547
|
+
const msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
|
|
548
|
+
|
|
549
|
+
bytes = [];
|
|
550
|
+
|
|
551
|
+
for (let i = 0; i < msg.length; ++i) {
|
|
552
|
+
bytes.push(msg.charCodeAt(i));
|
|
553
|
+
}
|
|
554
|
+
} else if (!Array.isArray(bytes)) {
|
|
555
|
+
// Convert Array-like to Array
|
|
556
|
+
bytes = Array.prototype.slice.call(bytes);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
bytes.push(0x80);
|
|
560
|
+
const l = bytes.length / 4 + 2;
|
|
561
|
+
const N = Math.ceil(l / 16);
|
|
562
|
+
const M = new Array(N);
|
|
563
|
+
|
|
564
|
+
for (let i = 0; i < N; ++i) {
|
|
565
|
+
const arr = new Uint32Array(16);
|
|
566
|
+
|
|
567
|
+
for (let j = 0; j < 16; ++j) {
|
|
568
|
+
arr[j] = bytes[i * 64 + j * 4] << 24 | bytes[i * 64 + j * 4 + 1] << 16 | bytes[i * 64 + j * 4 + 2] << 8 | bytes[i * 64 + j * 4 + 3];
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
M[i] = arr;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
M[N - 1][14] = (bytes.length - 1) * 8 / Math.pow(2, 32);
|
|
575
|
+
M[N - 1][14] = Math.floor(M[N - 1][14]);
|
|
576
|
+
M[N - 1][15] = (bytes.length - 1) * 8 & 0xffffffff;
|
|
577
|
+
|
|
578
|
+
for (let i = 0; i < N; ++i) {
|
|
579
|
+
const W = new Uint32Array(80);
|
|
580
|
+
|
|
581
|
+
for (let t = 0; t < 16; ++t) {
|
|
582
|
+
W[t] = M[i][t];
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
for (let t = 16; t < 80; ++t) {
|
|
586
|
+
W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
let a = H[0];
|
|
590
|
+
let b = H[1];
|
|
591
|
+
let c = H[2];
|
|
592
|
+
let d = H[3];
|
|
593
|
+
let e = H[4];
|
|
594
|
+
|
|
595
|
+
for (let t = 0; t < 80; ++t) {
|
|
596
|
+
const s = Math.floor(t / 20);
|
|
597
|
+
const T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0;
|
|
598
|
+
e = d;
|
|
599
|
+
d = c;
|
|
600
|
+
c = ROTL(b, 30) >>> 0;
|
|
601
|
+
b = a;
|
|
602
|
+
a = T;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
H[0] = H[0] + a >>> 0;
|
|
606
|
+
H[1] = H[1] + b >>> 0;
|
|
607
|
+
H[2] = H[2] + c >>> 0;
|
|
608
|
+
H[3] = H[3] + d >>> 0;
|
|
609
|
+
H[4] = H[4] + e >>> 0;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
return [H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff, H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff, H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff, H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff, H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff];
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
const v5 = v35('v5', 0x50, sha1);
|
|
616
|
+
var v5$1 = v5;
|
|
617
|
+
|
|
618
|
+
var nil = '00000000-0000-0000-0000-000000000000';
|
|
619
|
+
|
|
620
|
+
function version(uuid) {
|
|
621
|
+
if (!validate(uuid)) {
|
|
622
|
+
throw TypeError('Invalid UUID');
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
return parseInt(uuid.slice(14, 15), 16);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
var esmBrowser = /*#__PURE__*/Object.freeze({
|
|
629
|
+
__proto__: null,
|
|
630
|
+
NIL: nil,
|
|
631
|
+
parse: parse,
|
|
632
|
+
stringify: stringify,
|
|
633
|
+
v1: v1,
|
|
634
|
+
v3: v3$1,
|
|
635
|
+
v4: v4,
|
|
636
|
+
v5: v5$1,
|
|
637
|
+
validate: validate,
|
|
638
|
+
version: version
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
var require$$0 = /*@__PURE__*/getAugmentedNamespace(esmBrowser);
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Copyright (C) 2010-2023 - SipTimes Technologies Corporation - All rights reserved.
|
|
645
|
+
*/
|
|
646
|
+
|
|
647
|
+
const CORE = Object.freeze({ // core level api
|
|
648
|
+
PLATFORM: 'core-platform',
|
|
649
|
+
CONFIG: 'core-config',
|
|
650
|
+
BOX_INFO: 'core-box-info',
|
|
651
|
+
SIGN_INFO: 'core-sign-info',
|
|
652
|
+
SIGN_GROUP_INFO: 'core-sign-group-info',
|
|
653
|
+
SHELL: 'core-shell',
|
|
654
|
+
DATABASE: 'core-database',
|
|
655
|
+
GET_VOLUME: 'core-get-volume',
|
|
656
|
+
SET_VOLUME: 'core-set-volume'
|
|
657
|
+
});
|
|
658
|
+
const CORE_NORESP = Object.freeze({ // core level api with no response
|
|
659
|
+
IPC: 'core-ipc',
|
|
660
|
+
HEARTBEAT_PING: 'core-heartbeat-ping',
|
|
661
|
+
HEARTBEAT_REGISTER: 'core-heartbeat-register',
|
|
662
|
+
HEARTBEAT_UNREGISTER: 'core-heartbeat-unregister',
|
|
663
|
+
LOGGER: 'core-logger'
|
|
664
|
+
});
|
|
665
|
+
const CORE_APP = Object.freeze({ // application level api, notification from core
|
|
666
|
+
NOTIFICATION: 'core-notification', HEARTBEAT_LISTENER: 'core-heartbeat-listener'
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
const DEVICE = Object.freeze({ // core level api
|
|
670
|
+
CHANNEL: 'device-channel',
|
|
671
|
+
DEFAULT: 'device-default',
|
|
672
|
+
ID_CARD_READER: 'device-id-card-reader',
|
|
673
|
+
NFC: 'device-nfc',
|
|
674
|
+
FINGERPRINT_READER: 'device-fingerprint-reader',
|
|
675
|
+
QR_CODE_SCANNER: 'device-qr-code-scanner',
|
|
676
|
+
PRINTER: 'device-printer'
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
const HEARTBEAT_STATUS = Object.freeze({
|
|
680
|
+
REGISTER: "register", ALIVE: "alive", ERROR: 'error', NOT_FOUND: 'not-found', SUCCESS: 'success'
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
const cutosAPI$1 = {
|
|
684
|
+
version: '2.0.1', CORE, CORE_NORESP, CORE_APP, DEVICE, HEARTBEAT_STATUS
|
|
685
|
+
};
|
|
686
|
+
var cutosApi = cutosAPI$1;
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Copyright (C) 2010-2023 - SipTimes Technologies Corporation - All rights reserved.
|
|
690
|
+
*/
|
|
691
|
+
|
|
692
|
+
const {v4: uuidv4} = require$$0;
|
|
693
|
+
const mqtt = require$$1;
|
|
694
|
+
const cutosAPI = cutosApi;
|
|
695
|
+
const callbackMap = new Map();
|
|
696
|
+
const listenerMap = new Map();
|
|
697
|
+
|
|
698
|
+
const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
699
|
+
|
|
700
|
+
let client = null;
|
|
701
|
+
|
|
702
|
+
function publishRequest(api, callback, body, destAddress) {
|
|
703
|
+
let request = {};
|
|
704
|
+
request.context = {};
|
|
705
|
+
request.context.id = uuidv4();
|
|
706
|
+
request.context.destAddress = destAddress;
|
|
707
|
+
request.body = body;
|
|
708
|
+
if (typeof callback === "function") {
|
|
709
|
+
callbackMap.set(request.context.id, callback);
|
|
710
|
+
}
|
|
711
|
+
client.publish(api, JSON.stringify(request));
|
|
712
|
+
return true;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/*
|
|
716
|
+
retentionTime: retention time, default 48 hours
|
|
717
|
+
*/
|
|
718
|
+
class Database {
|
|
719
|
+
KEY_TYPE_TEXT = "TEXT";
|
|
720
|
+
KEY_TYPE_INTEGER = "INTEGER";
|
|
721
|
+
|
|
722
|
+
constructor(db, callback, retentionTime = 48) {
|
|
723
|
+
// sub class
|
|
724
|
+
this.Table = class {
|
|
725
|
+
constructor(db, name, callback, retentionTime, keyType, keyName) {
|
|
726
|
+
this.db = db;
|
|
727
|
+
this.name = name;
|
|
728
|
+
let body = {
|
|
729
|
+
db: this.db, cmd: "db.table.create", name: name
|
|
730
|
+
};
|
|
731
|
+
if (retentionTime) {
|
|
732
|
+
body.retentionTime = retentionTime;
|
|
733
|
+
}
|
|
734
|
+
if (keyType === 'INTEGER' || keyType === 'TEXT') {
|
|
735
|
+
body.keyName = keyName;
|
|
736
|
+
body.keyType = keyType;
|
|
737
|
+
} else {
|
|
738
|
+
throw new Error("keyType MUST be INTEGER or TEXT");
|
|
739
|
+
|
|
740
|
+
}
|
|
741
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
insert(value, callback, tid) {
|
|
745
|
+
let body = {
|
|
746
|
+
db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: tid
|
|
747
|
+
};
|
|
748
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
insertById(id, value, callback, tid) {
|
|
752
|
+
let body = {
|
|
753
|
+
db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: tid
|
|
754
|
+
};
|
|
755
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
insertByKey(key, value, callback, tid) {
|
|
759
|
+
let body = {
|
|
760
|
+
db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: tid
|
|
761
|
+
};
|
|
762
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
update(id, value, callback) {
|
|
766
|
+
let body = {
|
|
767
|
+
db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
|
|
768
|
+
};
|
|
769
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
updateByKey(key, value, callback) {
|
|
773
|
+
let body = {
|
|
774
|
+
db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
|
|
775
|
+
};
|
|
776
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
delete(id, callback) {
|
|
780
|
+
let body = {
|
|
781
|
+
db: this.db, cmd: "db.table.delete", name: this.name, id: id
|
|
782
|
+
};
|
|
783
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
query(id, callback, count = 100) {
|
|
787
|
+
let body = {
|
|
788
|
+
db: this.db, cmd: "db.table.query", name: this.name, id: id
|
|
789
|
+
};
|
|
790
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
queryByKey(key, callback, count = 100) {
|
|
794
|
+
let body = {
|
|
795
|
+
db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
|
|
796
|
+
};
|
|
797
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
queryByTid(tid, callback, limit = 100) {
|
|
801
|
+
let body = {
|
|
802
|
+
db: this.db, cmd: "db.table.queryByTid", name: this.name, limit: limit, tid: tid
|
|
803
|
+
};
|
|
804
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
queryUnsynced(callback, limit = 100) {
|
|
808
|
+
let body = {
|
|
809
|
+
db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: limit
|
|
810
|
+
};
|
|
811
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
queryAll(callback) {
|
|
815
|
+
let body = {
|
|
816
|
+
db: this.db, cmd: "db.table.queryAll", name: this.name
|
|
817
|
+
};
|
|
818
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
sync(id, callback) {
|
|
822
|
+
let body = {
|
|
823
|
+
db: this.db, cmd: "db.table.sync", name: this.name, id: id
|
|
824
|
+
};
|
|
825
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
826
|
+
}
|
|
827
|
+
};
|
|
828
|
+
this.db = db;
|
|
829
|
+
this.retentionTime = retentionTime;
|
|
830
|
+
let body = {
|
|
831
|
+
cmd: "db.connect", db: this.db
|
|
832
|
+
};
|
|
833
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
run(sql, callback) {
|
|
837
|
+
let body = {
|
|
838
|
+
db: this.db, cmd: "db.run", sql: sql
|
|
839
|
+
};
|
|
840
|
+
return publishRequest(cutosAPI.CORE.DATABASE, callback, body);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
createTable(name, callback, retentionTime, keyType = 'INTEGER', keyName = 'id') {
|
|
844
|
+
return new this.Table(null, name, callback, retentionTime, keyType, keyName);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
class Logger {
|
|
849
|
+
constructor() {
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
info(remark, content, type = "LWA") {
|
|
853
|
+
let body = {
|
|
854
|
+
type: type, level: "info", remark: remark, content: content
|
|
855
|
+
};
|
|
856
|
+
return publishRequest(cutosAPI.CORE_NORESP.LOGGER, null, body);
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
warning(remark, content, type = "LWA") {
|
|
860
|
+
let body = {
|
|
861
|
+
type: type, level: "warning", remark: remark, content: content
|
|
862
|
+
};
|
|
863
|
+
return publishRequest(cutosAPI.CORE_NORESP.LOGGER, null, body);
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
error(remark, content, type = "LWA") {
|
|
867
|
+
let body = {
|
|
868
|
+
type: type, level: "error", remark: remark, content: content
|
|
869
|
+
};
|
|
870
|
+
return publishRequest(cutosAPI.CORE_NORESP.LOGGER, null, body);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
debug(remark, content, type = "LWA") {
|
|
874
|
+
let body = {
|
|
875
|
+
type: type, level: "debug", remark: remark, content: content
|
|
876
|
+
};
|
|
877
|
+
return publishRequest(cutosAPI.CORE_NORESP.LOGGER, null, body);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
class IPC {
|
|
882
|
+
constructor(destAddress) {
|
|
883
|
+
if (destAddress === undefined || destAddress === null || destAddress.length === 0 || destAddress === 'localhost' || destAddress === '127.0.0.1') {
|
|
884
|
+
this.destAddress = undefined;
|
|
885
|
+
} else if (destAddress.match(ipformat)) {
|
|
886
|
+
this.destAddress = destAddress;
|
|
887
|
+
} else {
|
|
888
|
+
throw new TypeError(destAddress + " is NOT an ip address.");
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
sendTo(channel, args) {
|
|
893
|
+
let body = {};
|
|
894
|
+
body.channel = channel;
|
|
895
|
+
body.args = args;
|
|
896
|
+
return publishRequest(cutosAPI.CORE_NORESP.IPC, null, body, this.destAddress);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
on(channel, listener) {
|
|
900
|
+
listenerMap.set(channel, listener);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
removeListener(channel) {
|
|
904
|
+
listenerMap.delete(channel);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
class Notification {
|
|
909
|
+
constructor() {
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
register(listener) {
|
|
913
|
+
listenerMap.set(cutosAPI.CORE_APP.NOTIFICATION, listener);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
unregister() {
|
|
917
|
+
listenerMap.delete(cutosAPI.CORE_APP.NOTIFICATION);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
emit(event, msg) {
|
|
921
|
+
client.publish(cutosAPI.CORE_APP.NOTIFICATION, JSON.stringify({event: event, msg: msg}));
|
|
922
|
+
return true;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
class Heartbeat {
|
|
927
|
+
STATUS = cutosAPI.HEARTBEAT_STATUS;
|
|
928
|
+
|
|
929
|
+
constructor(pinger, counter) {
|
|
930
|
+
this.registered = false;
|
|
931
|
+
this.request = {};
|
|
932
|
+
this.request.counter = counter;
|
|
933
|
+
this.request.pinger = pinger;
|
|
934
|
+
this.request.subscriber = cutosAPI.CORE_NORESP.HEARTBEAT_PING + '-' + pinger;
|
|
935
|
+
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
register() {
|
|
939
|
+
this.request.msg = {};
|
|
940
|
+
this.request.msg.status = this.STATUS.REGISTER;
|
|
941
|
+
this.request.msg.info = `Start to monitor ${this.request.pinger}.`;
|
|
942
|
+
|
|
943
|
+
client.publish(cutosAPI.CORE_NORESP.HEARTBEAT_REGISTER, JSON.stringify(this.request));
|
|
944
|
+
this.registered = true;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
ping(status, info = {}) {
|
|
948
|
+
if (!this.registered) {
|
|
949
|
+
throw ("The heartbeat is NOT registered!")
|
|
950
|
+
}
|
|
951
|
+
this.request.msg = {};
|
|
952
|
+
this.request.msg.status = status;
|
|
953
|
+
this.request.msg.info = info;
|
|
954
|
+
client.publish(cutosAPI.CORE_NORESP.HEARTBEAT_PING, JSON.stringify(this.request));
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
class Device {
|
|
959
|
+
constructor(name = 'default-device-name', type = cutosAPI.DEVICE.DEFAULT, callback) {
|
|
960
|
+
this.name = name;
|
|
961
|
+
this.type = type;
|
|
962
|
+
this.topicData = cutosAPI.DEVICE.CHANNEL + '-' + type + '-data';
|
|
963
|
+
this.topicResponse = cutosAPI.DEVICE.CHANNEL + '-' + type + '-response';
|
|
964
|
+
this.topicCommand = cutosAPI.DEVICE.CHANNEL + '-' + type + '-command';
|
|
965
|
+
this.data = {};
|
|
966
|
+
|
|
967
|
+
if (Device.objectMap === undefined) {
|
|
968
|
+
Device.objectMap = new Map();
|
|
969
|
+
}
|
|
970
|
+
Device.objectMap.set(this.type, this);
|
|
971
|
+
|
|
972
|
+
if (Device.heartbeatListener === undefined) {
|
|
973
|
+
Device.heartbeatListener = function (msg) {
|
|
974
|
+
for (const [key, obj] of Device.objectMap) {
|
|
975
|
+
if (obj.statusListener !== undefined) {
|
|
976
|
+
if (obj.type === "default-device" || obj.type === msg.pinger) {
|
|
977
|
+
obj.statusListener(msg);
|
|
978
|
+
obj.msg = msg;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
//console.debug(JSON.stringify(msg));
|
|
983
|
+
};
|
|
984
|
+
listenerMap.set(cutosAPI.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
|
|
985
|
+
}
|
|
986
|
+
this.init(name, type, callback);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
init(name, type, callback) {
|
|
990
|
+
let command = {};
|
|
991
|
+
command.cmd = "init";
|
|
992
|
+
command.args = {name: name, type: type};
|
|
993
|
+
command.topicResponse = this.topicResponse;
|
|
994
|
+
let defaultTopic = cutosAPI.DEVICE.CHANNEL + '-' + cutosAPI.DEVICE.DEFAULT + '-command';
|
|
995
|
+
client.publish(defaultTopic, JSON.stringify(command));
|
|
996
|
+
|
|
997
|
+
// response callback
|
|
998
|
+
client.subscribe(this.topicResponse); // receive response from device
|
|
999
|
+
listenerMap.set(this.topicResponse, callback);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
onStatus(listener) {
|
|
1003
|
+
this.statusListener = listener;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
getCurrentStatus() {
|
|
1007
|
+
return this.msg;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
sendCommand(command) {
|
|
1011
|
+
client.publish(this.topicCommand, JSON.stringify(command));
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
onData(listener) {
|
|
1015
|
+
client.subscribe(this.topicData); // receive data from device
|
|
1016
|
+
let _this = this;
|
|
1017
|
+
listenerMap.set(this.topicData, function (data) {
|
|
1018
|
+
_this.data = data;
|
|
1019
|
+
listener(data);
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
getCurrentData() {
|
|
1024
|
+
return this.data;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
readDeviceInfo(context) {
|
|
1028
|
+
let msg = {};
|
|
1029
|
+
msg.cmd = "read-device-info";
|
|
1030
|
+
msg.context = context;
|
|
1031
|
+
this.sendCommand(msg);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
class Driver {
|
|
1036
|
+
constructor(name = 'default-driver-name', type = cutosAPI.DEVICE.DEFAULT, counter = 10) {
|
|
1037
|
+
this.name = name;
|
|
1038
|
+
this.type = type;
|
|
1039
|
+
this.heartbeat = new Heartbeat(type, counter);
|
|
1040
|
+
this.statusInfo = {};
|
|
1041
|
+
this.beatInterval = 3000;
|
|
1042
|
+
this.topicData = cutosAPI.DEVICE.CHANNEL + '-' + type + '-data';
|
|
1043
|
+
this.topicResponse = cutosAPI.DEVICE.CHANNEL + '-' + type + '-response';
|
|
1044
|
+
this.topicCommand = cutosAPI.DEVICE.CHANNEL + '-' + type + '-command';
|
|
1045
|
+
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
startBeat() {
|
|
1049
|
+
this.heartbeat.register();
|
|
1050
|
+
let _this = this;
|
|
1051
|
+
setInterval(function () {
|
|
1052
|
+
if (_this.statusInfo.status !== undefined && _this.statusInfo.status !== _this.heartbeat.STATUS.REGISTER) {
|
|
1053
|
+
_this.heartbeat.ping(_this.statusInfo.status, _this.statusInfo.info);
|
|
1054
|
+
}
|
|
1055
|
+
}, _this.beatInterval);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
updateStatusInfo(status = 'default-status', info = 'default-info') {
|
|
1059
|
+
this.statusInfo.status = status;
|
|
1060
|
+
this.statusInfo.info = info;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
sendData(data) {
|
|
1064
|
+
client.publish(this.topicData, JSON.stringify(data));
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
sendResponse(data, topic = this.topicResponse) {
|
|
1068
|
+
client.publish(topic, JSON.stringify(data));
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
onCommand(listener) {
|
|
1072
|
+
client.subscribe(this.topicCommand); // receive command from app
|
|
1073
|
+
listenerMap.set(this.topicCommand, listener);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
class CoreAPI {
|
|
1078
|
+
constructor(mqttHost = 'ws://localhost:1883') {
|
|
1079
|
+
client = mqtt.connect(mqttHost);
|
|
1080
|
+
|
|
1081
|
+
client.on('connect', function () {
|
|
1082
|
+
console.log("CUTOS CORE connected.");
|
|
1083
|
+
for (let api in cutosAPI.CORE) {
|
|
1084
|
+
client.subscribe(cutosAPI.CORE[api] + '-callback');
|
|
1085
|
+
}
|
|
1086
|
+
for (let api in cutosAPI.CORE_NORESP) {
|
|
1087
|
+
client.subscribe(cutosAPI.CORE_NORESP[api] + '-endpoint');
|
|
1088
|
+
}
|
|
1089
|
+
for (let api in cutosAPI.CORE_APP) {
|
|
1090
|
+
client.subscribe(cutosAPI.CORE_APP[api]);
|
|
1091
|
+
}
|
|
1092
|
+
});
|
|
1093
|
+
|
|
1094
|
+
client.on('message', function (topic, message, context) {
|
|
1095
|
+
// message is Buffer
|
|
1096
|
+
// console.debug(topic.toString() + message.toString());
|
|
1097
|
+
let _message = JSON.parse(message);
|
|
1098
|
+
if (topic === cutosAPI.CORE_NORESP.IPC + '-endpoint') { // ipc
|
|
1099
|
+
let listener = listenerMap.get(_message.body.channel);
|
|
1100
|
+
if (listener !== undefined) {
|
|
1101
|
+
listener(_message.body.args, _message.context);
|
|
1102
|
+
}
|
|
1103
|
+
} else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER || topic.startsWith(cutosAPI.DEVICE.CHANNEL)) {
|
|
1104
|
+
let listener = listenerMap.get(topic);
|
|
1105
|
+
if (listener !== undefined) {
|
|
1106
|
+
listener(_message);
|
|
1107
|
+
}
|
|
1108
|
+
} else { // cutos core api callback
|
|
1109
|
+
let callback = callbackMap.get(_message.id);
|
|
1110
|
+
if (typeof callback === "function") {
|
|
1111
|
+
callbackMap.delete(_message.id);
|
|
1112
|
+
callback(_message.result);
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
client.on("close", () => {
|
|
1118
|
+
console.log("CUTOS CORE closed.");
|
|
1119
|
+
});
|
|
1120
|
+
|
|
1121
|
+
client.on("error", (e) => {
|
|
1122
|
+
console.error("CUTOS CORE error: ", e.message);
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
connected() {
|
|
1127
|
+
return client.connected;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
getVersion() {
|
|
1131
|
+
return cutosAPI.version;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
getPlatform(callback) {
|
|
1135
|
+
return publishRequest(cutosAPI.CORE.PLATFORM, callback);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
getConfig(callback) {
|
|
1139
|
+
return publishRequest(cutosAPI.CORE.CONFIG, callback);
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
getBoxInfo(callback) {
|
|
1143
|
+
return publishRequest(cutosAPI.CORE.BOX_INFO, callback);
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
getSignInfo(callback) {
|
|
1147
|
+
return publishRequest(cutosAPI.CORE.SIGN_INFO, callback);
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
getSignGroupInfo(callback) {
|
|
1151
|
+
return publishRequest(cutosAPI.CORE.SIGN_GROUP_INFO, callback);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
getIPC(destAddress) {
|
|
1155
|
+
return new IPC(destAddress);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
getNotification() {
|
|
1159
|
+
return new Notification();
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
getLogger() {
|
|
1163
|
+
return new Logger();
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
shell(command, callback) {
|
|
1167
|
+
return publishRequest(cutosAPI.CORE.SHELL, callback, command);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
getDatabase(db, callback) {
|
|
1171
|
+
return new Database(db, callback);
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
getVolume(callback) {
|
|
1175
|
+
return publishRequest(cutosAPI.CORE.GET_VOLUME, callback);
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
setVolume(vol, callback) {
|
|
1179
|
+
return publishRequest(cutosAPI.CORE.SET_VOLUME, callback, vol);
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
var CoreAPI_1 = cutos.CoreAPI = CoreAPI;
|
|
1184
|
+
var CoreDefine = cutos.CoreDefine = {
|
|
1185
|
+
DEVICE: cutosAPI.DEVICE, HEARTBEAT_STATUS: cutosAPI.HEARTBEAT_STATUS
|
|
1186
|
+
};
|
|
1187
|
+
var CoreClass = cutos.CoreClass = {
|
|
1188
|
+
Device, Driver
|
|
1189
|
+
};
|
|
1190
|
+
var cutosAPI_1 = cutos.cutosAPI = cutosAPI;
|
|
1191
|
+
|
|
1192
|
+
export { CoreAPI_1 as CoreAPI, CoreClass, CoreDefine, cutosAPI_1 as cutosAPI, cutos as default };
|