@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/dist/cutos.esm.js CHANGED
@@ -27,7 +27,7 @@ function getAugmentedNamespace(n) {
27
27
 
28
28
  var src = {};
29
29
 
30
- var cutos = {};
30
+ var cutos$2 = {};
31
31
 
32
32
  // Unique ID creation requires a high quality random # generator. In the browser we therefore
33
33
  // require the crypto API and do not support built-in fallback to lower quality random number
@@ -677,7 +677,7 @@ const DEVICE = Object.freeze({ // core level api
677
677
  NFC: 'device-nfc',
678
678
  FINGERPRINT: 'device-fingerprint',
679
679
  QR_CODE_SCANNER: 'device-qr-code-scanner',
680
- PRINTER: 'device-printer'
680
+ PRINTER: 'device-printer',
681
681
  });
682
682
 
683
683
  const HEARTBEAT_STATUS = Object.freeze({
@@ -690,10 +690,11 @@ const cutosAPI$2 = {
690
690
  var cutosApi = cutosAPI$2;
691
691
 
692
692
  var name = "@cutos/core";
693
- var version = "3.3.1";
693
+ var version = "4.0.0";
694
694
  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. ";
695
695
  var main = "dist/cutos.cjs.js";
696
696
  var module = "dist/cutos.esm.js";
697
+ var types = "types/index.d.ts";
697
698
  var scripts = {
698
699
  "build:rollup": "rollup -c rollup.config.cjs"
699
700
  };
@@ -720,6 +721,7 @@ var devDependencies = {
720
721
  var files = [
721
722
  "dist/cutos.cjs.js",
722
723
  "dist/cutos.esm.js",
724
+ "types/index.d.ts",
723
725
  "package.json"
724
726
  ];
725
727
  var require$$3 = {
@@ -728,6 +730,7 @@ var require$$3 = {
728
730
  description: description,
729
731
  main: main,
730
732
  module: module,
733
+ types: types,
731
734
  scripts: scripts,
732
735
  keywords: keywords,
733
736
  author: author,
@@ -759,7 +762,69 @@ let coreApiInitialized = false;
759
762
  let _logger = null;
760
763
  let _notification = null;
761
764
 
762
- function publishRequest(topic, callback, body, destAddress) {
765
+ let CutosError$1 = class CutosError extends Error {
766
+ constructor(code = 'CUTOS_ERROR', message = 'CUTOS error', detail = undefined) {
767
+ super(message);
768
+ this.name = code;
769
+ this.code = code;
770
+ this.message = message;
771
+ this.detail = detail;
772
+ }
773
+ };
774
+
775
+ function normalizeError(error, fallbackCode = 'CUTOS_ERROR') {
776
+ if (error instanceof CutosError$1) {
777
+ return error;
778
+ }
779
+ if (error && error.error) {
780
+ return normalizeError(error.error, fallbackCode);
781
+ }
782
+ if (error && typeof error === "object") {
783
+ const code = error.code || error.name || fallbackCode;
784
+ const message = error.message || error.msg || JSON.stringify(error);
785
+ const detail = error.detail !== undefined ? error.detail : error;
786
+ return new CutosError$1(code, message, detail);
787
+ }
788
+ return new CutosError$1(fallbackCode, error === undefined || error === null ? 'CUTOS error' : String(error));
789
+ }
790
+
791
+ function createResult(data) {
792
+ return {status: true, msg: data};
793
+ }
794
+
795
+ function createErrorResult(error, fallbackCode = 'CUTOS_ERROR') {
796
+ return {status: false, msg: normalizeError(error, fallbackCode)};
797
+ }
798
+
799
+ function normalizeResult(result, error) {
800
+ if (error) {
801
+ throw normalizeError(error);
802
+ }
803
+ if (result && result.status !== undefined && result.msg !== undefined) {
804
+ if (result.status) {
805
+ return result.msg;
806
+ }
807
+ throw normalizeError(result.msg);
808
+ }
809
+ return result;
810
+ }
811
+
812
+ function maybePromise(callback, executor) {
813
+ if (typeof callback === "function") {
814
+ return executor(callback);
815
+ }
816
+ return new Promise((resolve, reject) => {
817
+ executor((result, error) => {
818
+ try {
819
+ resolve(normalizeResult(result, error));
820
+ } catch (e) {
821
+ reject(e);
822
+ }
823
+ });
824
+ });
825
+ }
826
+
827
+ function publishRequest(topic, callback, body, destAddress, opts = {}) {
763
828
  let request = {};
764
829
  request.context = {};
765
830
  request.context.id = uuidv4();
@@ -769,6 +834,7 @@ function publishRequest(topic, callback, body, destAddress) {
769
834
  let callbackItem = {};
770
835
  callbackItem.callback = callback;
771
836
  callbackItem.timestamp = Date.now();
837
+ callbackItem.timeout = opts.timeout || callbackTimeout;
772
838
  saveCallback(request.context.id, callbackItem);
773
839
  }
774
840
  client.publish(topic, JSON.stringify(request));
@@ -797,9 +863,9 @@ function saveCallback(id, item) {
797
863
  callbackTimer = setInterval(() => {
798
864
  let currentTimestamp = new Date().getTime();
799
865
  callbackMap.forEach((callbackItem, id) => {
800
- if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
801
- console.warn("callback is timeout");
866
+ if (currentTimestamp - callbackItem.timestamp > callbackItem.timeout) {
802
867
  callbackMap.delete(id);
868
+ callbackItem.callback(null, new CutosError$1('CUTOS_TIMEOUT', 'Response is timeout!'));
803
869
  }
804
870
  });
805
871
  }, callbackInterval);
@@ -811,7 +877,7 @@ function doCallback(id, body, err) {
811
877
  }
812
878
  try {
813
879
  if (err) {
814
- callbackMap.get(id).callback(null, new CutosError('CUTOS CORE error', err));
880
+ callbackMap.get(id).callback(null, new CutosError$1('CUTOS_CORE_ERROR', err));
815
881
  } else {
816
882
  callbackMap.get(id).callback(body);
817
883
  }
@@ -847,7 +913,7 @@ class Database {
847
913
  let body = {
848
914
  cmd: "db.connect", db: this.db
849
915
  };
850
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
916
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
851
917
  }
852
918
 
853
919
  /**
@@ -859,7 +925,7 @@ class Database {
859
925
  let body = {
860
926
  db: this.db, cmd: "db.run", sql: sql
861
927
  };
862
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
928
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
863
929
  }
864
930
 
865
931
  setupMirror(gwIPC, lwaName, confirm = false) {
@@ -911,7 +977,7 @@ Database.Table = class {
911
977
  keyName: opts.keyName
912
978
  };
913
979
 
914
- this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
980
+ return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
915
981
  }
916
982
 
917
983
  /**
@@ -929,7 +995,7 @@ Database.Table = class {
929
995
  let body = {
930
996
  db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: opts?.tid
931
997
  };
932
- this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
998
+ return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
933
999
  }
934
1000
 
935
1001
  insertById(id, value, opts, callback) {
@@ -941,7 +1007,7 @@ Database.Table = class {
941
1007
  let body = {
942
1008
  db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: opts?.tid
943
1009
  };
944
- this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
1010
+ return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
945
1011
  }
946
1012
 
947
1013
  /**
@@ -960,7 +1026,7 @@ Database.Table = class {
960
1026
  let body = {
961
1027
  db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: opts?.tid
962
1028
  };
963
- this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
1029
+ return maybePromise(callback, cb => this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, cb, body));
964
1030
  }
965
1031
 
966
1032
  /**
@@ -973,14 +1039,14 @@ Database.Table = class {
973
1039
  let body = {
974
1040
  db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
975
1041
  };
976
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1042
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
977
1043
  }
978
1044
 
979
1045
  updateByKey(key, value, callback) {
980
1046
  let body = {
981
1047
  db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
982
1048
  };
983
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1049
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
984
1050
  }
985
1051
 
986
1052
  /**
@@ -992,7 +1058,7 @@ Database.Table = class {
992
1058
  let body = {
993
1059
  db: this.db, cmd: "db.table.delete", name: this.name, id: id
994
1060
  };
995
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1061
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
996
1062
  }
997
1063
 
998
1064
  /**
@@ -1004,7 +1070,7 @@ Database.Table = class {
1004
1070
  let body = {
1005
1071
  db: this.db, cmd: "db.table.query", name: this.name, id: id
1006
1072
  };
1007
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1073
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
1008
1074
  }
1009
1075
 
1010
1076
  /**
@@ -1016,7 +1082,7 @@ Database.Table = class {
1016
1082
  let body = {
1017
1083
  db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
1018
1084
  };
1019
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1085
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
1020
1086
  }
1021
1087
 
1022
1088
  /**
@@ -1028,7 +1094,7 @@ Database.Table = class {
1028
1094
  let body = {
1029
1095
  db: this.db, cmd: "db.table.queryByTid", name: this.name, tid: tid
1030
1096
  };
1031
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1097
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
1032
1098
  }
1033
1099
 
1034
1100
  /**
@@ -1047,7 +1113,7 @@ Database.Table = class {
1047
1113
  let body = {
1048
1114
  db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: opts.limit
1049
1115
  };
1050
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1116
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
1051
1117
  }
1052
1118
 
1053
1119
  /**
@@ -1058,7 +1124,7 @@ Database.Table = class {
1058
1124
  let body = {
1059
1125
  db: this.db, cmd: "db.table.queryAll", name: this.name
1060
1126
  };
1061
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1127
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
1062
1128
  }
1063
1129
 
1064
1130
  /**
@@ -1070,7 +1136,7 @@ Database.Table = class {
1070
1136
  let body = {
1071
1137
  db: this.db, cmd: "db.table.sync", name: this.name, id: id
1072
1138
  };
1073
- publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
1139
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DATABASE, cb, body));
1074
1140
  }
1075
1141
 
1076
1142
  publishRequestMirror(topic, callback, body) {
@@ -1192,6 +1258,37 @@ class IPC {
1192
1258
  return publishRequest(topic, callback, body, this.destAddress);
1193
1259
  }
1194
1260
 
1261
+ sendToWithOptions(channel, args, opts = {}, callback = null) {
1262
+ if (typeof opts === "function") {
1263
+ callback = opts;
1264
+ opts = {};
1265
+ }
1266
+ let body = {};
1267
+ body.channel = channel;
1268
+ body.args = args;
1269
+ let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
1270
+ return publishRequest(topic, callback, body, this.destAddress, opts);
1271
+ }
1272
+
1273
+ /**
1274
+ * Call a channel and wait for the receiver response.
1275
+ * @param channel
1276
+ * @param args
1277
+ * @param callback optional callback(result, error)
1278
+ * @returns {Promise<*>|boolean}
1279
+ */
1280
+ call(channel, args, callback = null) {
1281
+ return maybePromise(callback, cb => this.sendTo(channel, args, cb));
1282
+ }
1283
+
1284
+ callWithOptions(channel, args, opts = {}, callback = null) {
1285
+ if (typeof opts === "function") {
1286
+ callback = opts;
1287
+ opts = {};
1288
+ }
1289
+ return maybePromise(callback, cb => this.sendToWithOptions(channel, args, opts, cb));
1290
+ }
1291
+
1195
1292
  /**
1196
1293
  * internal function, used to response sender with the result
1197
1294
  * @param context contain sender information
@@ -1225,6 +1322,7 @@ class IPC {
1225
1322
  };
1226
1323
  ipcListenerMap.get(channel).set(listener, innerListener);
1227
1324
  }
1325
+ return () => this.removeListener(channel, listener);
1228
1326
  }
1229
1327
  }
1230
1328
 
@@ -1359,21 +1457,28 @@ class Device {
1359
1457
  opts = null;
1360
1458
  }
1361
1459
 
1362
- let command = {};
1363
- command.cmd = "init";
1364
- command.args = {name: this.name, type: this.type, opts};
1365
- command.topicResponse = this.topicResponse;
1366
- let defaultTopic = cutosAPI$1.DEVICE.CHANNEL + '-' + cutosAPI$1.DEVICE.DEFAULT + '-command';
1367
- publishRequest(defaultTopic, null, command);
1368
-
1369
- // subscribe response callback, currently only used for init
1370
- client.subscribe(this.topicResponse); // receive response from device
1371
- listenerMap.set(this.topicResponse, (result) => {
1372
- if (result.status) {
1373
- callback && callback(`Driver ${this.type} loaded`);
1374
- } else {
1375
- callback && callback(null, result.msg);
1376
- }
1460
+ return maybePromise(callback, cb => {
1461
+ let command = {};
1462
+ command.cmd = "init";
1463
+ command.args = {name: this.name, type: this.type, opts};
1464
+ command.topicResponse = this.topicResponse;
1465
+ let defaultTopic = cutosAPI$1.DEVICE.CHANNEL + '-' + cutosAPI$1.DEVICE.DEFAULT + '-command';
1466
+ publishRequest(defaultTopic, null, command);
1467
+
1468
+ // subscribe response callback, currently only used for init
1469
+ client.subscribe(this.topicResponse); // receive response from device
1470
+ const timer = setTimeout(() => {
1471
+ listenerMap.delete(this.topicResponse);
1472
+ cb(null, new CutosError$1('DRIVER_INIT_TIMEOUT', `Driver ${this.type} init timeout!`));
1473
+ }, callbackTimeout);
1474
+ listenerMap.set(this.topicResponse, (result) => {
1475
+ clearTimeout(timer);
1476
+ if (result.status) {
1477
+ cb(`Driver ${this.type} loaded`);
1478
+ } else {
1479
+ cb(null, normalizeError(result.msg, 'DRIVER_INIT_ERROR'));
1480
+ }
1481
+ });
1377
1482
  });
1378
1483
  }
1379
1484
 
@@ -1400,7 +1505,15 @@ class Device {
1400
1505
  */
1401
1506
  sendCommand(command, callback) {
1402
1507
  // exam: command = {cmd: "connect", args: ""}
1403
- publishRequest(this.topicCommand, callback, command);
1508
+ return maybePromise(callback, cb => publishRequest(this.topicCommand, cb, command))
1509
+ }
1510
+
1511
+ sendCommandWithOptions(command, opts = {}, callback = null) {
1512
+ if (typeof opts === "function") {
1513
+ callback = opts;
1514
+ opts = {};
1515
+ }
1516
+ return maybePromise(callback, cb => publishRequest(this.topicCommand, cb, command, undefined, opts))
1404
1517
  }
1405
1518
 
1406
1519
  /**
@@ -1416,6 +1529,11 @@ class Device {
1416
1529
  this.data = data;
1417
1530
  listener(data);
1418
1531
  });
1532
+ return () => this.offData(listener);
1533
+ }
1534
+
1535
+ offData(listener = null) {
1536
+ listenerMap.delete(this.topicData);
1419
1537
  }
1420
1538
 
1421
1539
  getCurrentData() {
@@ -1429,7 +1547,7 @@ class Device {
1429
1547
  readDeviceInfo(callback) {
1430
1548
  let msg = {};
1431
1549
  msg.cmd = "read-device-info";
1432
- this.sendCommand(msg, callback);
1550
+ return this.sendCommand(msg, callback);
1433
1551
  }
1434
1552
  }
1435
1553
 
@@ -1497,17 +1615,200 @@ class Driver {
1497
1615
 
1498
1616
  client.subscribe(this.topicCommand); // receive command from app
1499
1617
  listenerMap.set(this.topicCommand, cmdListener);
1618
+ return () => this.offCommand(listener);
1500
1619
  }
1501
1620
  }
1621
+
1622
+ offCommand(listener = null) {
1623
+ listenerMap.delete(this.topicCommand);
1624
+ }
1502
1625
  }
1503
1626
 
1504
- class CutosError {
1505
- constructor(name, message) {
1627
+ class Capability {
1628
+ constructor(name, opts = {}) {
1629
+ if (!name) {
1630
+ throw new Error("capability name cannot be empty.");
1631
+ }
1506
1632
  this.name = name;
1507
- this.message = message;
1633
+ this.type = opts.type || name;
1634
+ this.device = opts.device || new Device(name, this.type);
1635
+ this.eventListeners = new Map();
1636
+ this.dataListenerRegistered = false;
1637
+ }
1638
+
1639
+ call(method, params = {}, options = null, callback = null) {
1640
+ if (!method) {
1641
+ throw new Error("capability method cannot be empty.");
1642
+ }
1643
+ if (typeof options === "function") {
1644
+ callback = options;
1645
+ options = null;
1646
+ }
1647
+
1648
+ const command = {cmd: method, args: params};
1649
+ if (options) {
1650
+ command.options = options;
1651
+ }
1652
+ return this.device.sendCommandWithOptions(command, options || {}, callback);
1653
+ }
1654
+
1655
+ on(event, listener) {
1656
+ if (!event) {
1657
+ throw new Error("capability event cannot be empty.");
1658
+ }
1659
+ if (typeof listener !== "function") {
1660
+ throw new Error("capability event listener must be a function.");
1661
+ }
1662
+
1663
+ if (!this.eventListeners.has(event)) {
1664
+ this.eventListeners.set(event, new Set());
1665
+ }
1666
+ this.eventListeners.get(event).add(listener);
1667
+
1668
+ if (!this.dataListenerRegistered) {
1669
+ this.unsubscribeData = this.device.onData(message => this._dispatchEvent(message));
1670
+ this.dataListenerRegistered = true;
1671
+ }
1672
+
1673
+ return () => this.off(event, listener);
1674
+ }
1675
+
1676
+ dispose() {
1677
+ this.eventListeners.clear();
1678
+ if (this.unsubscribeData) {
1679
+ this.unsubscribeData();
1680
+ this.unsubscribeData = null;
1681
+ }
1682
+ this.dataListenerRegistered = false;
1683
+ return true;
1684
+ }
1685
+
1686
+ off(event, listener = null) {
1687
+ if (!this.eventListeners.has(event)) {
1688
+ return;
1689
+ }
1690
+ if (!listener) {
1691
+ this.eventListeners.delete(event);
1692
+ return;
1693
+ }
1694
+ const listeners = this.eventListeners.get(event);
1695
+ listeners.delete(listener);
1696
+ if (listeners.size === 0) {
1697
+ this.eventListeners.delete(event);
1698
+ }
1699
+ }
1700
+
1701
+ _dispatchEvent(message) {
1702
+ const event = message && (message.event || message.type);
1703
+ const payload = message && Object.prototype.hasOwnProperty.call(message, "data") ? message.data : message;
1704
+ const dispatch = listeners => listeners && listeners.forEach(listener => listener(payload, message));
1705
+
1706
+ dispatch(this.eventListeners.get(event));
1707
+ dispatch(this.eventListeners.get("*"));
1708
+ }
1709
+ }
1710
+
1711
+ class Provider {
1712
+ constructor(name, opts = {}) {
1713
+ if (!name) {
1714
+ throw new Error("provider name cannot be empty.");
1715
+ }
1716
+ this.name = name;
1717
+ this.type = opts.type || name;
1718
+ this.driver = opts.driver || new Driver(name, this.type, opts.counter);
1719
+ this.methods = new Map();
1720
+ this.commandListenerRegistered = false;
1721
+ this.unsubscribeCommand = null;
1722
+ }
1723
+
1724
+ method(method, handler) {
1725
+ if (!method) {
1726
+ throw new Error("provider method cannot be empty.");
1727
+ }
1728
+ if (typeof handler !== "function") {
1729
+ throw new Error("provider method handler must be a function.");
1730
+ }
1731
+
1732
+ this.methods.set(method, handler);
1733
+ if (!this.commandListenerRegistered) {
1734
+ this.unsubscribeCommand = this.driver.onCommand((command, respond) => this._dispatchCommand(command, respond));
1735
+ this.commandListenerRegistered = true;
1736
+ }
1737
+ return this;
1738
+ }
1739
+
1740
+ emit(event, data) {
1741
+ if (!event) {
1742
+ throw new Error("provider event cannot be empty.");
1743
+ }
1744
+ this.driver.sendData({event, data});
1745
+ return true;
1746
+ }
1747
+
1748
+ dispose() {
1749
+ this.methods.clear();
1750
+ if (this.unsubscribeCommand) {
1751
+ this.unsubscribeCommand();
1752
+ this.unsubscribeCommand = null;
1753
+ } else {
1754
+ this.driver.offCommand();
1755
+ }
1756
+ this.commandListenerRegistered = false;
1757
+ return true;
1758
+ }
1759
+
1760
+ async _dispatchCommand(command, respond) {
1761
+ const method = command && command.cmd;
1762
+ const handler = this.methods.get(method);
1763
+ if (!handler) {
1764
+ respond(createErrorResult(
1765
+ new CutosError$1('METHOD_NOT_FOUND', `Provider method ${method} is not registered.`)
1766
+ ));
1767
+ return;
1768
+ }
1769
+
1770
+ try {
1771
+ const result = await handler(command.args, {command, provider: this});
1772
+ if (result && result.status !== undefined && result.msg !== undefined) {
1773
+ respond(result);
1774
+ return;
1775
+ }
1776
+ respond(createResult(result));
1777
+ } catch (error) {
1778
+ respond(createErrorResult(error, 'PROVIDER_ERROR'));
1779
+ }
1508
1780
  }
1509
1781
  }
1510
1782
 
1783
+ const capabilityMap = new Map();
1784
+ const providerMap = new Map();
1785
+
1786
+ const cutos$1 = {
1787
+ capability(name, opts = {}) {
1788
+ const key = opts.type || name;
1789
+ if (!capabilityMap.has(key)) {
1790
+ capabilityMap.set(key, new Capability(name, opts));
1791
+ }
1792
+ return capabilityMap.get(key);
1793
+ },
1794
+
1795
+ provider(name, opts = {}) {
1796
+ const key = opts.type || name;
1797
+ if (!providerMap.has(key)) {
1798
+ providerMap.set(key, new Provider(name, opts));
1799
+ }
1800
+ return providerMap.get(key);
1801
+ },
1802
+
1803
+ device(name, opts = {}) {
1804
+ return this.capability(name, opts);
1805
+ },
1806
+
1807
+ driver(name, opts = {}) {
1808
+ return this.provider(name, opts);
1809
+ }
1810
+ };
1811
+
1511
1812
  const CoreAPI$2 = {
1512
1813
  /**
1513
1814
  * Initialization of CUTOS core
@@ -1515,6 +1816,14 @@ const CoreAPI$2 = {
1515
1816
  * @param {Function} callback callback(result, error)
1516
1817
  */
1517
1818
  init(host = 'localhost', callback) {
1819
+ if (typeof host === "function") {
1820
+ callback = host;
1821
+ host = 'localhost';
1822
+ }
1823
+ return maybePromise(callback, cb => CoreAPI$2._init(host, cb));
1824
+ },
1825
+
1826
+ _init(host = 'localhost', callback) {
1518
1827
  let brokerUrl;
1519
1828
  if (host && host.startsWith('ws://') && host.endsWith(':1883')) {
1520
1829
  brokerUrl = host;
@@ -1580,11 +1889,11 @@ const CoreAPI$2 = {
1580
1889
  });
1581
1890
 
1582
1891
  client.on("error", (err) => {
1583
- callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1892
+ callback && callback(null, new CutosError$1('CUTOS_CORE_ERROR', err.message));
1584
1893
  console.error("CUTOS CORE error:", err.message);
1585
1894
  });
1586
1895
  client.stream.on("error", (err) => {
1587
- callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1896
+ callback && callback(null, new CutosError$1('CUTOS_CORE_ERROR', err.message));
1588
1897
  console.error("CUTOS CORE error:", err.message);
1589
1898
  });
1590
1899
  },
@@ -1605,7 +1914,7 @@ const CoreAPI$2 = {
1605
1914
  * @param {Function} callback - callback(result)
1606
1915
  */
1607
1916
  getPlatform(callback) {
1608
- return publishRequest(cutosAPI$1.CORE.PLATFORM, callback);
1917
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.PLATFORM, cb));
1609
1918
  },
1610
1919
 
1611
1920
  /**
@@ -1613,7 +1922,7 @@ const CoreAPI$2 = {
1613
1922
  * @param {Function} callback - callback(result)
1614
1923
  */
1615
1924
  getConfig(callback) {
1616
- return publishRequest(cutosAPI$1.CORE.CONFIG, callback);
1925
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.CONFIG, cb));
1617
1926
  },
1618
1927
 
1619
1928
  /**
@@ -1621,7 +1930,7 @@ const CoreAPI$2 = {
1621
1930
  * @param {Function} callback - callback(result)
1622
1931
  */
1623
1932
  getBoxInfo(callback) {
1624
- return publishRequest(cutosAPI$1.CORE.BOX_INFO, callback);
1933
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.BOX_INFO, cb));
1625
1934
  },
1626
1935
 
1627
1936
  /**
@@ -1629,7 +1938,7 @@ const CoreAPI$2 = {
1629
1938
  * @param {Function} callback - callback(result)
1630
1939
  */
1631
1940
  getSignInfo(callback) {
1632
- return publishRequest(cutosAPI$1.CORE.SIGN_INFO, callback);
1941
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SIGN_INFO, cb));
1633
1942
  },
1634
1943
 
1635
1944
  /**
@@ -1637,11 +1946,11 @@ const CoreAPI$2 = {
1637
1946
  * @param {Function} callback - callback(result)
1638
1947
  */
1639
1948
  getDeviceInfo(callback) {
1640
- return publishRequest(cutosAPI$1.CORE.DEVICE_INFO, callback);
1949
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.DEVICE_INFO, cb));
1641
1950
  },
1642
1951
 
1643
1952
  getSignGroupInfo(callback) {
1644
- return publishRequest(cutosAPI$1.CORE.SIGN_GROUP_INFO, callback);
1953
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SIGN_GROUP_INFO, cb));
1645
1954
  },
1646
1955
 
1647
1956
  /**
@@ -1678,7 +1987,7 @@ const CoreAPI$2 = {
1678
1987
  * @param {Function} callback - callback(result, error)
1679
1988
  */
1680
1989
  shell(command, callback) {
1681
- return publishRequest(cutosAPI$1.CORE.SHELL, callback, command);
1990
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SHELL, cb, command));
1682
1991
  },
1683
1992
 
1684
1993
  /**
@@ -1686,7 +1995,7 @@ const CoreAPI$2 = {
1686
1995
  * @param {Function} callback - callback(result)
1687
1996
  */
1688
1997
  getVolume(callback) {
1689
- return publishRequest(cutosAPI$1.CORE.GET_VOLUME, callback);
1998
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.GET_VOLUME, cb));
1690
1999
  },
1691
2000
 
1692
2001
  /**
@@ -1695,7 +2004,7 @@ const CoreAPI$2 = {
1695
2004
  * @param {Function} callback - callback(result)
1696
2005
  */
1697
2006
  setVolume(vol, callback) {
1698
- return publishRequest(cutosAPI$1.CORE.SET_VOLUME, callback, vol);
2007
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SET_VOLUME, cb, vol));
1699
2008
  },
1700
2009
 
1701
2010
  /**
@@ -1705,22 +2014,40 @@ const CoreAPI$2 = {
1705
2014
  * @param {Function} callback - callback(result, error)
1706
2015
  */
1707
2016
  setHttpProxy(api, target, callback) {
1708
- return publishRequest(cutosAPI$1.CORE.SET_HTTP_PROXY, callback, {api, target});
2017
+ return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SET_HTTP_PROXY, cb, {api, target}));
2018
+ },
2019
+
2020
+ capability(name, opts = {}) {
2021
+ return cutos$1.capability(name, opts);
2022
+ },
2023
+
2024
+ provider(name, opts = {}) {
2025
+ return cutos$1.provider(name, opts);
2026
+ },
2027
+
2028
+ device(name, opts = {}) {
2029
+ return cutos$1.device(name, opts);
2030
+ },
2031
+
2032
+ driver(name, opts = {}) {
2033
+ return cutos$1.driver(name, opts);
1709
2034
  }
1710
2035
  };
1711
2036
 
1712
- cutos.CoreAPI = CoreAPI$2;
1713
- cutos.CoreDefine = {
2037
+ cutos$2.CoreAPI = CoreAPI$2;
2038
+ cutos$2.CoreDefine = {
1714
2039
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
1715
2040
  };
1716
- cutos.CoreClass = {
1717
- Device, Driver, Database
2041
+ cutos$2.CoreClass = {
2042
+ Device, Driver, Database, Capability, Provider, CutosError: CutosError$1
1718
2043
  };
1719
2044
 
1720
- cutos.cutosAPI = cutosAPI$1;
2045
+ cutos$2.cutosAPI = cutosAPI$1;
2046
+ cutos$2.cutos = cutos$1;
2047
+ cutos$2.CutosError = CutosError$1;
1721
2048
 
1722
2049
  // simulate cutos core for development
1723
- const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1} = cutos;
2050
+ const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1} = cutos$2;
1724
2051
 
1725
2052
  const CoreSimulator$1 = {
1726
2053
  start: function (driver, port = 1883) {
@@ -1752,7 +2079,7 @@ const CoreSimulator$1 = {
1752
2079
 
1753
2080
  var coreSimulator = {CoreSimulator: CoreSimulator$1};
1754
2081
 
1755
- const {CoreAPI, CoreDefine, CoreClass, cutosAPI} = cutos;
2082
+ const {CoreAPI, CoreDefine, CoreClass, cutosAPI, cutos, CutosError} = cutos$2;
1756
2083
  const {CoreSimulator} = coreSimulator;
1757
2084
 
1758
2085
 
@@ -1760,6 +2087,8 @@ var CoreAPI_1 = src.CoreAPI = CoreAPI;
1760
2087
  var CoreDefine_1 = src.CoreDefine = CoreDefine;
1761
2088
  var CoreClass_1 = src.CoreClass = CoreClass;
1762
2089
  var cutosAPI_1 = src.cutosAPI = cutosAPI;
2090
+ var cutos_1 = src.cutos = cutos;
2091
+ var CutosError_1 = src.CutosError = CutosError;
1763
2092
  var CoreSimulator_1 = src.CoreSimulator = CoreSimulator;
1764
2093
 
1765
- export { CoreAPI_1 as CoreAPI, CoreClass_1 as CoreClass, CoreDefine_1 as CoreDefine, CoreSimulator_1 as CoreSimulator, cutosAPI_1 as cutosAPI, src as default };
2094
+ export { CoreAPI_1 as CoreAPI, CoreClass_1 as CoreClass, CoreDefine_1 as CoreDefine, CoreSimulator_1 as CoreSimulator, CutosError_1 as CutosError, cutos_1 as cutos, cutosAPI_1 as cutosAPI, src as default };