@cutos/core 3.3.1 → 4.0.0
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 +368 -1266
- package/dist/cutos.cjs.js +392 -61
- package/dist/cutos.esm.js +391 -62
- package/package.json +4 -1
- package/types/index.d.ts +328 -0
package/dist/cutos.cjs.js
CHANGED
|
@@ -32,7 +32,7 @@ function getAugmentedNamespace(n) {
|
|
|
32
32
|
|
|
33
33
|
var src = {};
|
|
34
34
|
|
|
35
|
-
var cutos = {};
|
|
35
|
+
var cutos$2 = {};
|
|
36
36
|
|
|
37
37
|
const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
|
|
38
38
|
|
|
@@ -385,7 +385,7 @@ const DEVICE = Object.freeze({ // core level api
|
|
|
385
385
|
NFC: 'device-nfc',
|
|
386
386
|
FINGERPRINT: 'device-fingerprint',
|
|
387
387
|
QR_CODE_SCANNER: 'device-qr-code-scanner',
|
|
388
|
-
PRINTER: 'device-printer'
|
|
388
|
+
PRINTER: 'device-printer',
|
|
389
389
|
});
|
|
390
390
|
|
|
391
391
|
const HEARTBEAT_STATUS = Object.freeze({
|
|
@@ -398,10 +398,11 @@ const cutosAPI$2 = {
|
|
|
398
398
|
var cutosApi = cutosAPI$2;
|
|
399
399
|
|
|
400
400
|
var name = "@cutos/core";
|
|
401
|
-
var version = "
|
|
401
|
+
var version = "4.0.0";
|
|
402
402
|
var description = "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ";
|
|
403
403
|
var main = "dist/cutos.cjs.js";
|
|
404
404
|
var module$1 = "dist/cutos.esm.js";
|
|
405
|
+
var types = "types/index.d.ts";
|
|
405
406
|
var scripts = {
|
|
406
407
|
"build:rollup": "rollup -c rollup.config.cjs"
|
|
407
408
|
};
|
|
@@ -428,6 +429,7 @@ var devDependencies = {
|
|
|
428
429
|
var files = [
|
|
429
430
|
"dist/cutos.cjs.js",
|
|
430
431
|
"dist/cutos.esm.js",
|
|
432
|
+
"types/index.d.ts",
|
|
431
433
|
"package.json"
|
|
432
434
|
];
|
|
433
435
|
var require$$3 = {
|
|
@@ -436,6 +438,7 @@ var require$$3 = {
|
|
|
436
438
|
description: description,
|
|
437
439
|
main: main,
|
|
438
440
|
module: module$1,
|
|
441
|
+
types: types,
|
|
439
442
|
scripts: scripts,
|
|
440
443
|
keywords: keywords,
|
|
441
444
|
author: author,
|
|
@@ -467,7 +470,69 @@ let coreApiInitialized = false;
|
|
|
467
470
|
let _logger = null;
|
|
468
471
|
let _notification = null;
|
|
469
472
|
|
|
470
|
-
|
|
473
|
+
let CutosError$1 = class CutosError extends Error {
|
|
474
|
+
constructor(code = 'CUTOS_ERROR', message = 'CUTOS error', detail = undefined) {
|
|
475
|
+
super(message);
|
|
476
|
+
this.name = code;
|
|
477
|
+
this.code = code;
|
|
478
|
+
this.message = message;
|
|
479
|
+
this.detail = detail;
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
function normalizeError(error, fallbackCode = 'CUTOS_ERROR') {
|
|
484
|
+
if (error instanceof CutosError$1) {
|
|
485
|
+
return error;
|
|
486
|
+
}
|
|
487
|
+
if (error && error.error) {
|
|
488
|
+
return normalizeError(error.error, fallbackCode);
|
|
489
|
+
}
|
|
490
|
+
if (error && typeof error === "object") {
|
|
491
|
+
const code = error.code || error.name || fallbackCode;
|
|
492
|
+
const message = error.message || error.msg || JSON.stringify(error);
|
|
493
|
+
const detail = error.detail !== undefined ? error.detail : error;
|
|
494
|
+
return new CutosError$1(code, message, detail);
|
|
495
|
+
}
|
|
496
|
+
return new CutosError$1(fallbackCode, error === undefined || error === null ? 'CUTOS error' : String(error));
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function createResult(data) {
|
|
500
|
+
return {status: true, msg: data};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function createErrorResult(error, fallbackCode = 'CUTOS_ERROR') {
|
|
504
|
+
return {status: false, msg: normalizeError(error, fallbackCode)};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function normalizeResult(result, error) {
|
|
508
|
+
if (error) {
|
|
509
|
+
throw normalizeError(error);
|
|
510
|
+
}
|
|
511
|
+
if (result && result.status !== undefined && result.msg !== undefined) {
|
|
512
|
+
if (result.status) {
|
|
513
|
+
return result.msg;
|
|
514
|
+
}
|
|
515
|
+
throw normalizeError(result.msg);
|
|
516
|
+
}
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function maybePromise(callback, executor) {
|
|
521
|
+
if (typeof callback === "function") {
|
|
522
|
+
return executor(callback);
|
|
523
|
+
}
|
|
524
|
+
return new Promise((resolve, reject) => {
|
|
525
|
+
executor((result, error) => {
|
|
526
|
+
try {
|
|
527
|
+
resolve(normalizeResult(result, error));
|
|
528
|
+
} catch (e) {
|
|
529
|
+
reject(e);
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function publishRequest(topic, callback, body, destAddress, opts = {}) {
|
|
471
536
|
let request = {};
|
|
472
537
|
request.context = {};
|
|
473
538
|
request.context.id = uuidv4();
|
|
@@ -477,6 +542,7 @@ function publishRequest(topic, callback, body, destAddress) {
|
|
|
477
542
|
let callbackItem = {};
|
|
478
543
|
callbackItem.callback = callback;
|
|
479
544
|
callbackItem.timestamp = Date.now();
|
|
545
|
+
callbackItem.timeout = opts.timeout || callbackTimeout;
|
|
480
546
|
saveCallback(request.context.id, callbackItem);
|
|
481
547
|
}
|
|
482
548
|
client.publish(topic, JSON.stringify(request));
|
|
@@ -505,9 +571,9 @@ function saveCallback(id, item) {
|
|
|
505
571
|
callbackTimer = setInterval(() => {
|
|
506
572
|
let currentTimestamp = new Date().getTime();
|
|
507
573
|
callbackMap.forEach((callbackItem, id) => {
|
|
508
|
-
if (currentTimestamp - callbackItem.timestamp >
|
|
509
|
-
console.warn("callback is timeout");
|
|
574
|
+
if (currentTimestamp - callbackItem.timestamp > callbackItem.timeout) {
|
|
510
575
|
callbackMap.delete(id);
|
|
576
|
+
callbackItem.callback(null, new CutosError$1('CUTOS_TIMEOUT', 'Response is timeout!'));
|
|
511
577
|
}
|
|
512
578
|
});
|
|
513
579
|
}, callbackInterval);
|
|
@@ -519,7 +585,7 @@ function doCallback(id, body, err) {
|
|
|
519
585
|
}
|
|
520
586
|
try {
|
|
521
587
|
if (err) {
|
|
522
|
-
callbackMap.get(id).callback(null, new CutosError('
|
|
588
|
+
callbackMap.get(id).callback(null, new CutosError$1('CUTOS_CORE_ERROR', err));
|
|
523
589
|
} else {
|
|
524
590
|
callbackMap.get(id).callback(body);
|
|
525
591
|
}
|
|
@@ -555,7 +621,7 @@ class Database {
|
|
|
555
621
|
let body = {
|
|
556
622
|
cmd: "db.connect", db: this.db
|
|
557
623
|
};
|
|
558
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
624
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
559
625
|
}
|
|
560
626
|
|
|
561
627
|
/**
|
|
@@ -567,7 +633,7 @@ class Database {
|
|
|
567
633
|
let body = {
|
|
568
634
|
db: this.db, cmd: "db.run", sql: sql
|
|
569
635
|
};
|
|
570
|
-
return publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
636
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
571
637
|
}
|
|
572
638
|
|
|
573
639
|
setupMirror(gwIPC, lwaName, confirm = false) {
|
|
@@ -619,7 +685,7 @@ Database.Table = class {
|
|
|
619
685
|
keyName: opts.keyName
|
|
620
686
|
};
|
|
621
687
|
|
|
622
|
-
this.publishRequestMirror(cutosAPI$1.CORE.DATABASE,
|
|
688
|
+
return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
623
689
|
}
|
|
624
690
|
|
|
625
691
|
/**
|
|
@@ -637,7 +703,7 @@ Database.Table = class {
|
|
|
637
703
|
let body = {
|
|
638
704
|
db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: opts?.tid
|
|
639
705
|
};
|
|
640
|
-
this.publishRequestMirror(cutosAPI$1.CORE.DATABASE,
|
|
706
|
+
return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
641
707
|
}
|
|
642
708
|
|
|
643
709
|
insertById(id, value, opts, callback) {
|
|
@@ -649,7 +715,7 @@ Database.Table = class {
|
|
|
649
715
|
let body = {
|
|
650
716
|
db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: opts?.tid
|
|
651
717
|
};
|
|
652
|
-
this.publishRequestMirror(cutosAPI$1.CORE.DATABASE,
|
|
718
|
+
return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
653
719
|
}
|
|
654
720
|
|
|
655
721
|
/**
|
|
@@ -668,7 +734,7 @@ Database.Table = class {
|
|
|
668
734
|
let body = {
|
|
669
735
|
db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: opts?.tid
|
|
670
736
|
};
|
|
671
|
-
this.publishRequestMirror(cutosAPI$1.CORE.DATABASE,
|
|
737
|
+
return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
672
738
|
}
|
|
673
739
|
|
|
674
740
|
/**
|
|
@@ -681,14 +747,14 @@ Database.Table = class {
|
|
|
681
747
|
let body = {
|
|
682
748
|
db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
|
|
683
749
|
};
|
|
684
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
750
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
685
751
|
}
|
|
686
752
|
|
|
687
753
|
updateByKey(key, value, callback) {
|
|
688
754
|
let body = {
|
|
689
755
|
db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
|
|
690
756
|
};
|
|
691
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
757
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
692
758
|
}
|
|
693
759
|
|
|
694
760
|
/**
|
|
@@ -700,7 +766,7 @@ Database.Table = class {
|
|
|
700
766
|
let body = {
|
|
701
767
|
db: this.db, cmd: "db.table.delete", name: this.name, id: id
|
|
702
768
|
};
|
|
703
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
769
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
704
770
|
}
|
|
705
771
|
|
|
706
772
|
/**
|
|
@@ -712,7 +778,7 @@ Database.Table = class {
|
|
|
712
778
|
let body = {
|
|
713
779
|
db: this.db, cmd: "db.table.query", name: this.name, id: id
|
|
714
780
|
};
|
|
715
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
781
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
716
782
|
}
|
|
717
783
|
|
|
718
784
|
/**
|
|
@@ -724,7 +790,7 @@ Database.Table = class {
|
|
|
724
790
|
let body = {
|
|
725
791
|
db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
|
|
726
792
|
};
|
|
727
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
793
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
728
794
|
}
|
|
729
795
|
|
|
730
796
|
/**
|
|
@@ -736,7 +802,7 @@ Database.Table = class {
|
|
|
736
802
|
let body = {
|
|
737
803
|
db: this.db, cmd: "db.table.queryByTid", name: this.name, tid: tid
|
|
738
804
|
};
|
|
739
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
805
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
740
806
|
}
|
|
741
807
|
|
|
742
808
|
/**
|
|
@@ -755,7 +821,7 @@ Database.Table = class {
|
|
|
755
821
|
let body = {
|
|
756
822
|
db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: opts.limit
|
|
757
823
|
};
|
|
758
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
824
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
759
825
|
}
|
|
760
826
|
|
|
761
827
|
/**
|
|
@@ -766,7 +832,7 @@ Database.Table = class {
|
|
|
766
832
|
let body = {
|
|
767
833
|
db: this.db, cmd: "db.table.queryAll", name: this.name
|
|
768
834
|
};
|
|
769
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
835
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
770
836
|
}
|
|
771
837
|
|
|
772
838
|
/**
|
|
@@ -778,7 +844,7 @@ Database.Table = class {
|
|
|
778
844
|
let body = {
|
|
779
845
|
db: this.db, cmd: "db.table.sync", name: this.name, id: id
|
|
780
846
|
};
|
|
781
|
-
publishRequest(cutosAPI$1.CORE.DATABASE,
|
|
847
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
|
|
782
848
|
}
|
|
783
849
|
|
|
784
850
|
publishRequestMirror(topic, callback, body) {
|
|
@@ -900,6 +966,37 @@ class IPC {
|
|
|
900
966
|
return publishRequest(topic, callback, body, this.destAddress);
|
|
901
967
|
}
|
|
902
968
|
|
|
969
|
+
sendToWithOptions(channel, args, opts = {}, callback = null) {
|
|
970
|
+
if (typeof opts === "function") {
|
|
971
|
+
callback = opts;
|
|
972
|
+
opts = {};
|
|
973
|
+
}
|
|
974
|
+
let body = {};
|
|
975
|
+
body.channel = channel;
|
|
976
|
+
body.args = args;
|
|
977
|
+
let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
|
|
978
|
+
return publishRequest(topic, callback, body, this.destAddress, opts);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
* Call a channel and wait for the receiver response.
|
|
983
|
+
* @param channel
|
|
984
|
+
* @param args
|
|
985
|
+
* @param callback optional callback(result, error)
|
|
986
|
+
* @returns {Promise<*>|boolean}
|
|
987
|
+
*/
|
|
988
|
+
call(channel, args, callback = null) {
|
|
989
|
+
return maybePromise(callback, cb => this.sendTo(channel, args, cb));
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
callWithOptions(channel, args, opts = {}, callback = null) {
|
|
993
|
+
if (typeof opts === "function") {
|
|
994
|
+
callback = opts;
|
|
995
|
+
opts = {};
|
|
996
|
+
}
|
|
997
|
+
return maybePromise(callback, cb => this.sendToWithOptions(channel, args, opts, cb));
|
|
998
|
+
}
|
|
999
|
+
|
|
903
1000
|
/**
|
|
904
1001
|
* internal function, used to response sender with the result
|
|
905
1002
|
* @param context contain sender information
|
|
@@ -933,6 +1030,7 @@ class IPC {
|
|
|
933
1030
|
};
|
|
934
1031
|
ipcListenerMap.get(channel).set(listener, innerListener);
|
|
935
1032
|
}
|
|
1033
|
+
return () => this.removeListener(channel, listener);
|
|
936
1034
|
}
|
|
937
1035
|
}
|
|
938
1036
|
|
|
@@ -1067,21 +1165,28 @@ class Device {
|
|
|
1067
1165
|
opts = null;
|
|
1068
1166
|
}
|
|
1069
1167
|
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1168
|
+
return maybePromise(callback, cb => {
|
|
1169
|
+
let command = {};
|
|
1170
|
+
command.cmd = "init";
|
|
1171
|
+
command.args = {name: this.name, type: this.type, opts};
|
|
1172
|
+
command.topicResponse = this.topicResponse;
|
|
1173
|
+
let defaultTopic = cutosAPI$1.DEVICE.CHANNEL + '-' + cutosAPI$1.DEVICE.DEFAULT + '-command';
|
|
1174
|
+
publishRequest(defaultTopic, null, command);
|
|
1175
|
+
|
|
1176
|
+
// subscribe response callback, currently only used for init
|
|
1177
|
+
client.subscribe(this.topicResponse); // receive response from device
|
|
1178
|
+
const timer = setTimeout(() => {
|
|
1179
|
+
listenerMap.delete(this.topicResponse);
|
|
1180
|
+
cb(null, new CutosError$1('DRIVER_INIT_TIMEOUT', `Driver ${this.type} init timeout!`));
|
|
1181
|
+
}, callbackTimeout);
|
|
1182
|
+
listenerMap.set(this.topicResponse, (result) => {
|
|
1183
|
+
clearTimeout(timer);
|
|
1184
|
+
if (result.status) {
|
|
1185
|
+
cb(`Driver ${this.type} loaded`);
|
|
1186
|
+
} else {
|
|
1187
|
+
cb(null, normalizeError(result.msg, 'DRIVER_INIT_ERROR'));
|
|
1188
|
+
}
|
|
1189
|
+
});
|
|
1085
1190
|
});
|
|
1086
1191
|
}
|
|
1087
1192
|
|
|
@@ -1108,7 +1213,15 @@ class Device {
|
|
|
1108
1213
|
*/
|
|
1109
1214
|
sendCommand(command, callback) {
|
|
1110
1215
|
// exam: command = {cmd: "connect", args: ""}
|
|
1111
|
-
publishRequest(this.topicCommand,
|
|
1216
|
+
return maybePromise(callback, cb => publishRequest(this.topicCommand, cb, command))
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
sendCommandWithOptions(command, opts = {}, callback = null) {
|
|
1220
|
+
if (typeof opts === "function") {
|
|
1221
|
+
callback = opts;
|
|
1222
|
+
opts = {};
|
|
1223
|
+
}
|
|
1224
|
+
return maybePromise(callback, cb => publishRequest(this.topicCommand, cb, command, undefined, opts))
|
|
1112
1225
|
}
|
|
1113
1226
|
|
|
1114
1227
|
/**
|
|
@@ -1124,6 +1237,11 @@ class Device {
|
|
|
1124
1237
|
this.data = data;
|
|
1125
1238
|
listener(data);
|
|
1126
1239
|
});
|
|
1240
|
+
return () => this.offData(listener);
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
offData(listener = null) {
|
|
1244
|
+
listenerMap.delete(this.topicData);
|
|
1127
1245
|
}
|
|
1128
1246
|
|
|
1129
1247
|
getCurrentData() {
|
|
@@ -1137,7 +1255,7 @@ class Device {
|
|
|
1137
1255
|
readDeviceInfo(callback) {
|
|
1138
1256
|
let msg = {};
|
|
1139
1257
|
msg.cmd = "read-device-info";
|
|
1140
|
-
this.sendCommand(msg, callback);
|
|
1258
|
+
return this.sendCommand(msg, callback);
|
|
1141
1259
|
}
|
|
1142
1260
|
}
|
|
1143
1261
|
|
|
@@ -1205,17 +1323,200 @@ class Driver {
|
|
|
1205
1323
|
|
|
1206
1324
|
client.subscribe(this.topicCommand); // receive command from app
|
|
1207
1325
|
listenerMap.set(this.topicCommand, cmdListener);
|
|
1326
|
+
return () => this.offCommand(listener);
|
|
1208
1327
|
}
|
|
1209
1328
|
}
|
|
1329
|
+
|
|
1330
|
+
offCommand(listener = null) {
|
|
1331
|
+
listenerMap.delete(this.topicCommand);
|
|
1332
|
+
}
|
|
1210
1333
|
}
|
|
1211
1334
|
|
|
1212
|
-
class
|
|
1213
|
-
constructor(name,
|
|
1335
|
+
class Capability {
|
|
1336
|
+
constructor(name, opts = {}) {
|
|
1337
|
+
if (!name) {
|
|
1338
|
+
throw new Error("capability name cannot be empty.");
|
|
1339
|
+
}
|
|
1214
1340
|
this.name = name;
|
|
1215
|
-
this.
|
|
1341
|
+
this.type = opts.type || name;
|
|
1342
|
+
this.device = opts.device || new Device(name, this.type);
|
|
1343
|
+
this.eventListeners = new Map();
|
|
1344
|
+
this.dataListenerRegistered = false;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
call(method, params = {}, options = null, callback = null) {
|
|
1348
|
+
if (!method) {
|
|
1349
|
+
throw new Error("capability method cannot be empty.");
|
|
1350
|
+
}
|
|
1351
|
+
if (typeof options === "function") {
|
|
1352
|
+
callback = options;
|
|
1353
|
+
options = null;
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
const command = {cmd: method, args: params};
|
|
1357
|
+
if (options) {
|
|
1358
|
+
command.options = options;
|
|
1359
|
+
}
|
|
1360
|
+
return this.device.sendCommandWithOptions(command, options || {}, callback);
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
on(event, listener) {
|
|
1364
|
+
if (!event) {
|
|
1365
|
+
throw new Error("capability event cannot be empty.");
|
|
1366
|
+
}
|
|
1367
|
+
if (typeof listener !== "function") {
|
|
1368
|
+
throw new Error("capability event listener must be a function.");
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
if (!this.eventListeners.has(event)) {
|
|
1372
|
+
this.eventListeners.set(event, new Set());
|
|
1373
|
+
}
|
|
1374
|
+
this.eventListeners.get(event).add(listener);
|
|
1375
|
+
|
|
1376
|
+
if (!this.dataListenerRegistered) {
|
|
1377
|
+
this.unsubscribeData = this.device.onData(message => this._dispatchEvent(message));
|
|
1378
|
+
this.dataListenerRegistered = true;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
return () => this.off(event, listener);
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
dispose() {
|
|
1385
|
+
this.eventListeners.clear();
|
|
1386
|
+
if (this.unsubscribeData) {
|
|
1387
|
+
this.unsubscribeData();
|
|
1388
|
+
this.unsubscribeData = null;
|
|
1389
|
+
}
|
|
1390
|
+
this.dataListenerRegistered = false;
|
|
1391
|
+
return true;
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
off(event, listener = null) {
|
|
1395
|
+
if (!this.eventListeners.has(event)) {
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
if (!listener) {
|
|
1399
|
+
this.eventListeners.delete(event);
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
const listeners = this.eventListeners.get(event);
|
|
1403
|
+
listeners.delete(listener);
|
|
1404
|
+
if (listeners.size === 0) {
|
|
1405
|
+
this.eventListeners.delete(event);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
_dispatchEvent(message) {
|
|
1410
|
+
const event = message && (message.event || message.type);
|
|
1411
|
+
const payload = message && Object.prototype.hasOwnProperty.call(message, "data") ? message.data : message;
|
|
1412
|
+
const dispatch = listeners => listeners && listeners.forEach(listener => listener(payload, message));
|
|
1413
|
+
|
|
1414
|
+
dispatch(this.eventListeners.get(event));
|
|
1415
|
+
dispatch(this.eventListeners.get("*"));
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
class Provider {
|
|
1420
|
+
constructor(name, opts = {}) {
|
|
1421
|
+
if (!name) {
|
|
1422
|
+
throw new Error("provider name cannot be empty.");
|
|
1423
|
+
}
|
|
1424
|
+
this.name = name;
|
|
1425
|
+
this.type = opts.type || name;
|
|
1426
|
+
this.driver = opts.driver || new Driver(name, this.type, opts.counter);
|
|
1427
|
+
this.methods = new Map();
|
|
1428
|
+
this.commandListenerRegistered = false;
|
|
1429
|
+
this.unsubscribeCommand = null;
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
method(method, handler) {
|
|
1433
|
+
if (!method) {
|
|
1434
|
+
throw new Error("provider method cannot be empty.");
|
|
1435
|
+
}
|
|
1436
|
+
if (typeof handler !== "function") {
|
|
1437
|
+
throw new Error("provider method handler must be a function.");
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
this.methods.set(method, handler);
|
|
1441
|
+
if (!this.commandListenerRegistered) {
|
|
1442
|
+
this.unsubscribeCommand = this.driver.onCommand((command, respond) => this._dispatchCommand(command, respond));
|
|
1443
|
+
this.commandListenerRegistered = true;
|
|
1444
|
+
}
|
|
1445
|
+
return this;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
emit(event, data) {
|
|
1449
|
+
if (!event) {
|
|
1450
|
+
throw new Error("provider event cannot be empty.");
|
|
1451
|
+
}
|
|
1452
|
+
this.driver.sendData({event, data});
|
|
1453
|
+
return true;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
dispose() {
|
|
1457
|
+
this.methods.clear();
|
|
1458
|
+
if (this.unsubscribeCommand) {
|
|
1459
|
+
this.unsubscribeCommand();
|
|
1460
|
+
this.unsubscribeCommand = null;
|
|
1461
|
+
} else {
|
|
1462
|
+
this.driver.offCommand();
|
|
1463
|
+
}
|
|
1464
|
+
this.commandListenerRegistered = false;
|
|
1465
|
+
return true;
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
async _dispatchCommand(command, respond) {
|
|
1469
|
+
const method = command && command.cmd;
|
|
1470
|
+
const handler = this.methods.get(method);
|
|
1471
|
+
if (!handler) {
|
|
1472
|
+
respond(createErrorResult(
|
|
1473
|
+
new CutosError$1('METHOD_NOT_FOUND', `Provider method ${method} is not registered.`)
|
|
1474
|
+
));
|
|
1475
|
+
return;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
try {
|
|
1479
|
+
const result = await handler(command.args, {command, provider: this});
|
|
1480
|
+
if (result && result.status !== undefined && result.msg !== undefined) {
|
|
1481
|
+
respond(result);
|
|
1482
|
+
return;
|
|
1483
|
+
}
|
|
1484
|
+
respond(createResult(result));
|
|
1485
|
+
} catch (error) {
|
|
1486
|
+
respond(createErrorResult(error, 'PROVIDER_ERROR'));
|
|
1487
|
+
}
|
|
1216
1488
|
}
|
|
1217
1489
|
}
|
|
1218
1490
|
|
|
1491
|
+
const capabilityMap = new Map();
|
|
1492
|
+
const providerMap = new Map();
|
|
1493
|
+
|
|
1494
|
+
const cutos$1 = {
|
|
1495
|
+
capability(name, opts = {}) {
|
|
1496
|
+
const key = opts.type || name;
|
|
1497
|
+
if (!capabilityMap.has(key)) {
|
|
1498
|
+
capabilityMap.set(key, new Capability(name, opts));
|
|
1499
|
+
}
|
|
1500
|
+
return capabilityMap.get(key);
|
|
1501
|
+
},
|
|
1502
|
+
|
|
1503
|
+
provider(name, opts = {}) {
|
|
1504
|
+
const key = opts.type || name;
|
|
1505
|
+
if (!providerMap.has(key)) {
|
|
1506
|
+
providerMap.set(key, new Provider(name, opts));
|
|
1507
|
+
}
|
|
1508
|
+
return providerMap.get(key);
|
|
1509
|
+
},
|
|
1510
|
+
|
|
1511
|
+
device(name, opts = {}) {
|
|
1512
|
+
return this.capability(name, opts);
|
|
1513
|
+
},
|
|
1514
|
+
|
|
1515
|
+
driver(name, opts = {}) {
|
|
1516
|
+
return this.provider(name, opts);
|
|
1517
|
+
}
|
|
1518
|
+
};
|
|
1519
|
+
|
|
1219
1520
|
const CoreAPI$2 = {
|
|
1220
1521
|
/**
|
|
1221
1522
|
* Initialization of CUTOS core
|
|
@@ -1223,6 +1524,14 @@ const CoreAPI$2 = {
|
|
|
1223
1524
|
* @param {Function} callback callback(result, error)
|
|
1224
1525
|
*/
|
|
1225
1526
|
init(host = 'localhost', callback) {
|
|
1527
|
+
if (typeof host === "function") {
|
|
1528
|
+
callback = host;
|
|
1529
|
+
host = 'localhost';
|
|
1530
|
+
}
|
|
1531
|
+
return maybePromise(callback, cb => CoreAPI$2._init(host, cb));
|
|
1532
|
+
},
|
|
1533
|
+
|
|
1534
|
+
_init(host = 'localhost', callback) {
|
|
1226
1535
|
let brokerUrl;
|
|
1227
1536
|
if (host && host.startsWith('ws://') && host.endsWith(':1883')) {
|
|
1228
1537
|
brokerUrl = host;
|
|
@@ -1288,11 +1597,11 @@ const CoreAPI$2 = {
|
|
|
1288
1597
|
});
|
|
1289
1598
|
|
|
1290
1599
|
client.on("error", (err) => {
|
|
1291
|
-
callback && callback(null, new CutosError('
|
|
1600
|
+
callback && callback(null, new CutosError$1('CUTOS_CORE_ERROR', err.message));
|
|
1292
1601
|
console.error("CUTOS CORE error:", err.message);
|
|
1293
1602
|
});
|
|
1294
1603
|
client.stream.on("error", (err) => {
|
|
1295
|
-
callback && callback(null, new CutosError('
|
|
1604
|
+
callback && callback(null, new CutosError$1('CUTOS_CORE_ERROR', err.message));
|
|
1296
1605
|
console.error("CUTOS CORE error:", err.message);
|
|
1297
1606
|
});
|
|
1298
1607
|
},
|
|
@@ -1313,7 +1622,7 @@ const CoreAPI$2 = {
|
|
|
1313
1622
|
* @param {Function} callback - callback(result)
|
|
1314
1623
|
*/
|
|
1315
1624
|
getPlatform(callback) {
|
|
1316
|
-
return publishRequest(cutosAPI$1.CORE.PLATFORM,
|
|
1625
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.PLATFORM, cb));
|
|
1317
1626
|
},
|
|
1318
1627
|
|
|
1319
1628
|
/**
|
|
@@ -1321,7 +1630,7 @@ const CoreAPI$2 = {
|
|
|
1321
1630
|
* @param {Function} callback - callback(result)
|
|
1322
1631
|
*/
|
|
1323
1632
|
getConfig(callback) {
|
|
1324
|
-
return publishRequest(cutosAPI$1.CORE.CONFIG,
|
|
1633
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.CONFIG, cb));
|
|
1325
1634
|
},
|
|
1326
1635
|
|
|
1327
1636
|
/**
|
|
@@ -1329,7 +1638,7 @@ const CoreAPI$2 = {
|
|
|
1329
1638
|
* @param {Function} callback - callback(result)
|
|
1330
1639
|
*/
|
|
1331
1640
|
getBoxInfo(callback) {
|
|
1332
|
-
return publishRequest(cutosAPI$1.CORE.BOX_INFO,
|
|
1641
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.BOX_INFO, cb));
|
|
1333
1642
|
},
|
|
1334
1643
|
|
|
1335
1644
|
/**
|
|
@@ -1337,7 +1646,7 @@ const CoreAPI$2 = {
|
|
|
1337
1646
|
* @param {Function} callback - callback(result)
|
|
1338
1647
|
*/
|
|
1339
1648
|
getSignInfo(callback) {
|
|
1340
|
-
return publishRequest(cutosAPI$1.CORE.SIGN_INFO,
|
|
1649
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SIGN_INFO, cb));
|
|
1341
1650
|
},
|
|
1342
1651
|
|
|
1343
1652
|
/**
|
|
@@ -1345,11 +1654,11 @@ const CoreAPI$2 = {
|
|
|
1345
1654
|
* @param {Function} callback - callback(result)
|
|
1346
1655
|
*/
|
|
1347
1656
|
getDeviceInfo(callback) {
|
|
1348
|
-
return publishRequest(cutosAPI$1.CORE.DEVICE_INFO,
|
|
1657
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DEVICE_INFO, cb));
|
|
1349
1658
|
},
|
|
1350
1659
|
|
|
1351
1660
|
getSignGroupInfo(callback) {
|
|
1352
|
-
return publishRequest(cutosAPI$1.CORE.SIGN_GROUP_INFO,
|
|
1661
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SIGN_GROUP_INFO, cb));
|
|
1353
1662
|
},
|
|
1354
1663
|
|
|
1355
1664
|
/**
|
|
@@ -1386,7 +1695,7 @@ const CoreAPI$2 = {
|
|
|
1386
1695
|
* @param {Function} callback - callback(result, error)
|
|
1387
1696
|
*/
|
|
1388
1697
|
shell(command, callback) {
|
|
1389
|
-
return publishRequest(cutosAPI$1.CORE.SHELL,
|
|
1698
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SHELL, cb, command));
|
|
1390
1699
|
},
|
|
1391
1700
|
|
|
1392
1701
|
/**
|
|
@@ -1394,7 +1703,7 @@ const CoreAPI$2 = {
|
|
|
1394
1703
|
* @param {Function} callback - callback(result)
|
|
1395
1704
|
*/
|
|
1396
1705
|
getVolume(callback) {
|
|
1397
|
-
return publishRequest(cutosAPI$1.CORE.GET_VOLUME,
|
|
1706
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.GET_VOLUME, cb));
|
|
1398
1707
|
},
|
|
1399
1708
|
|
|
1400
1709
|
/**
|
|
@@ -1403,7 +1712,7 @@ const CoreAPI$2 = {
|
|
|
1403
1712
|
* @param {Function} callback - callback(result)
|
|
1404
1713
|
*/
|
|
1405
1714
|
setVolume(vol, callback) {
|
|
1406
|
-
return publishRequest(cutosAPI$1.CORE.SET_VOLUME,
|
|
1715
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SET_VOLUME, cb, vol));
|
|
1407
1716
|
},
|
|
1408
1717
|
|
|
1409
1718
|
/**
|
|
@@ -1413,22 +1722,40 @@ const CoreAPI$2 = {
|
|
|
1413
1722
|
* @param {Function} callback - callback(result, error)
|
|
1414
1723
|
*/
|
|
1415
1724
|
setHttpProxy(api, target, callback) {
|
|
1416
|
-
return publishRequest(cutosAPI$1.CORE.SET_HTTP_PROXY,
|
|
1725
|
+
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SET_HTTP_PROXY, cb, {api, target}));
|
|
1726
|
+
},
|
|
1727
|
+
|
|
1728
|
+
capability(name, opts = {}) {
|
|
1729
|
+
return cutos$1.capability(name, opts);
|
|
1730
|
+
},
|
|
1731
|
+
|
|
1732
|
+
provider(name, opts = {}) {
|
|
1733
|
+
return cutos$1.provider(name, opts);
|
|
1734
|
+
},
|
|
1735
|
+
|
|
1736
|
+
device(name, opts = {}) {
|
|
1737
|
+
return cutos$1.device(name, opts);
|
|
1738
|
+
},
|
|
1739
|
+
|
|
1740
|
+
driver(name, opts = {}) {
|
|
1741
|
+
return cutos$1.driver(name, opts);
|
|
1417
1742
|
}
|
|
1418
1743
|
};
|
|
1419
1744
|
|
|
1420
|
-
cutos.CoreAPI = CoreAPI$2;
|
|
1421
|
-
cutos.CoreDefine = {
|
|
1745
|
+
cutos$2.CoreAPI = CoreAPI$2;
|
|
1746
|
+
cutos$2.CoreDefine = {
|
|
1422
1747
|
DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
|
|
1423
1748
|
};
|
|
1424
|
-
cutos.CoreClass = {
|
|
1425
|
-
Device, Driver, Database
|
|
1749
|
+
cutos$2.CoreClass = {
|
|
1750
|
+
Device, Driver, Database, Capability, Provider, CutosError: CutosError$1
|
|
1426
1751
|
};
|
|
1427
1752
|
|
|
1428
|
-
cutos.cutosAPI = cutosAPI$1;
|
|
1753
|
+
cutos$2.cutosAPI = cutosAPI$1;
|
|
1754
|
+
cutos$2.cutos = cutos$1;
|
|
1755
|
+
cutos$2.CutosError = CutosError$1;
|
|
1429
1756
|
|
|
1430
1757
|
// simulate cutos core for development
|
|
1431
|
-
const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1} = cutos;
|
|
1758
|
+
const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1} = cutos$2;
|
|
1432
1759
|
|
|
1433
1760
|
const CoreSimulator$1 = {
|
|
1434
1761
|
start: function (driver, port = 1883) {
|
|
@@ -1460,7 +1787,7 @@ const CoreSimulator$1 = {
|
|
|
1460
1787
|
|
|
1461
1788
|
var coreSimulator = {CoreSimulator: CoreSimulator$1};
|
|
1462
1789
|
|
|
1463
|
-
const {CoreAPI, CoreDefine, CoreClass, cutosAPI} = cutos;
|
|
1790
|
+
const {CoreAPI, CoreDefine, CoreClass, cutosAPI, cutos, CutosError} = cutos$2;
|
|
1464
1791
|
const {CoreSimulator} = coreSimulator;
|
|
1465
1792
|
|
|
1466
1793
|
|
|
@@ -1468,11 +1795,15 @@ var CoreAPI_1 = src.CoreAPI = CoreAPI;
|
|
|
1468
1795
|
var CoreDefine_1 = src.CoreDefine = CoreDefine;
|
|
1469
1796
|
var CoreClass_1 = src.CoreClass = CoreClass;
|
|
1470
1797
|
var cutosAPI_1 = src.cutosAPI = cutosAPI;
|
|
1798
|
+
var cutos_1 = src.cutos = cutos;
|
|
1799
|
+
var CutosError_1 = src.CutosError = CutosError;
|
|
1471
1800
|
var CoreSimulator_1 = src.CoreSimulator = CoreSimulator;
|
|
1472
1801
|
|
|
1473
1802
|
exports.CoreAPI = CoreAPI_1;
|
|
1474
1803
|
exports.CoreClass = CoreClass_1;
|
|
1475
1804
|
exports.CoreDefine = CoreDefine_1;
|
|
1476
1805
|
exports.CoreSimulator = CoreSimulator_1;
|
|
1806
|
+
exports.CutosError = CutosError_1;
|
|
1807
|
+
exports.cutos = cutos_1;
|
|
1477
1808
|
exports.cutosAPI = cutosAPI_1;
|
|
1478
1809
|
exports.default = src;
|