@cutos/core 4.0.2 → 4.0.4

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 CHANGED
@@ -32,7 +32,7 @@ function getAugmentedNamespace(n) {
32
32
 
33
33
  var src = {};
34
34
 
35
- var cutos$3 = {};
35
+ var cutos = {};
36
36
 
37
37
  const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
38
38
 
@@ -398,7 +398,7 @@ const cutosAPI$2 = {
398
398
  var cutosApi = cutosAPI$2;
399
399
 
400
400
  var name = "@cutos/core";
401
- var version = "4.0.2";
401
+ var version = "4.0.4";
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";
@@ -1117,6 +1117,38 @@ class Heartbeat {
1117
1117
  }
1118
1118
  }
1119
1119
 
1120
+ const deviceStatusListenerMap = new Map();
1121
+ let deviceHeartbeatListenerRegistered = false;
1122
+
1123
+ function addDeviceStatusListener(type, listener) {
1124
+ if (!deviceStatusListenerMap.has(type)) {
1125
+ deviceStatusListenerMap.set(type, new Set());
1126
+ }
1127
+ deviceStatusListenerMap.get(type).add(listener);
1128
+
1129
+ if (!deviceHeartbeatListenerRegistered) {
1130
+ listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, msg => {
1131
+ deviceStatusListenerMap.forEach((listeners, listenerType) => {
1132
+ if (listenerType === cutosAPI$1.DEVICE.DEFAULT || listenerType === msg.pinger) {
1133
+ listeners.forEach(statusListener => statusListener(msg));
1134
+ }
1135
+ });
1136
+ });
1137
+ deviceHeartbeatListenerRegistered = true;
1138
+ }
1139
+
1140
+ return () => {
1141
+ const listeners = deviceStatusListenerMap.get(type);
1142
+ if (!listeners) {
1143
+ return;
1144
+ }
1145
+ listeners.delete(listener);
1146
+ if (listeners.size === 0) {
1147
+ deviceStatusListenerMap.delete(type);
1148
+ }
1149
+ };
1150
+ }
1151
+
1120
1152
  class Device {
1121
1153
  /**
1122
1154
  * The constructor of Device
@@ -1129,27 +1161,6 @@ class Device {
1129
1161
  this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
1130
1162
  this.topicResponse = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-response';
1131
1163
  this.topicCommand = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-command';
1132
- this.data = {};
1133
-
1134
- if (Device.objectMap === undefined) {
1135
- Device.objectMap = new Map();
1136
- }
1137
- Device.objectMap.set(this.type, this);
1138
-
1139
- if (Device.heartbeatListener === undefined) {
1140
- Device.heartbeatListener = function (msg) {
1141
- for (const [key, obj] of Device.objectMap) {
1142
- if (obj.statusListener !== undefined) {
1143
- if (obj.type === "default-device" || obj.type === msg.pinger) {
1144
- obj.statusListener(msg);
1145
- obj.msg = msg;
1146
- }
1147
- }
1148
- }
1149
- //console.debug(JSON.stringify(msg));
1150
- };
1151
- listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
1152
- }
1153
1164
  // subscribe command callback
1154
1165
  client.subscribe(this.topicCommand + '-callback');
1155
1166
  }
@@ -1190,22 +1201,6 @@ class Device {
1190
1201
  });
1191
1202
  }
1192
1203
 
1193
- /**
1194
- * Get device status
1195
- * @param {Function} listener
1196
- */
1197
- onStatus(listener) {
1198
- this.statusListener = listener;
1199
- }
1200
-
1201
- /**
1202
- * Get device status
1203
- * @param {Function} listener
1204
- */
1205
- getCurrentStatus() {
1206
- return this.msg;
1207
- }
1208
-
1209
1204
  /**
1210
1205
  *
1211
1206
  * @param command - command description object {cmd: 'command', args: (''|{}) }
@@ -1223,40 +1218,6 @@ class Device {
1223
1218
  }
1224
1219
  return maybePromise(callback, cb => publishRequest(this.topicCommand, cb, command, undefined, opts))
1225
1220
  }
1226
-
1227
- /**
1228
- * Receive data sent by the driver
1229
- * @param listener - listener(data)
1230
- */
1231
- onData(listener) {
1232
- if (!listenerMap.has(this.topicData)) {
1233
- client.subscribe(this.topicData); // receive data from device
1234
- }
1235
-
1236
- listenerMap.set(this.topicData, (data) => {
1237
- this.data = data;
1238
- listener(data);
1239
- });
1240
- return () => this.offData(listener);
1241
- }
1242
-
1243
- offData(listener = null) {
1244
- listenerMap.delete(this.topicData);
1245
- }
1246
-
1247
- getCurrentData() {
1248
- return this.data;
1249
- }
1250
-
1251
- /**
1252
- *
1253
- * @param callback (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
1254
- */
1255
- readDeviceInfo(callback) {
1256
- let msg = {};
1257
- msg.cmd = "read-device-info";
1258
- return this.sendCommand(msg, callback);
1259
- }
1260
1221
  }
1261
1222
 
1262
1223
  class Driver {
@@ -1276,12 +1237,16 @@ class Driver {
1276
1237
  this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
1277
1238
  this.topicResponse = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-response';
1278
1239
  this.topicCommand = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-command';
1240
+ this.beatTimer = null;
1279
1241
  }
1280
1242
 
1281
1243
  startBeat() {
1244
+ if (this.beatTimer) {
1245
+ return;
1246
+ }
1282
1247
  this.heartbeat.register();
1283
1248
  let _this = this;
1284
- setInterval(function () {
1249
+ this.beatTimer = setInterval(function () {
1285
1250
  if (_this.statusInfo.status !== undefined && _this.statusInfo.status !== _this.heartbeat.STATUS.REGISTER) {
1286
1251
  _this.heartbeat.ping(_this.statusInfo.status, _this.statusInfo.info);
1287
1252
  }
@@ -1374,7 +1339,11 @@ class Capability {
1374
1339
  this.eventListeners.get(event).add(listener);
1375
1340
 
1376
1341
  if (!this.dataListenerRegistered) {
1377
- this.unsubscribeData = this.device.onData(message => this._dispatchEvent(message));
1342
+ if (!listenerMap.has(this.device.topicData)) {
1343
+ client.subscribe(this.device.topicData);
1344
+ }
1345
+ listenerMap.set(this.device.topicData, message => this._dispatchEvent(message));
1346
+ this.unsubscribeData = () => listenerMap.delete(this.device.topicData);
1378
1347
  this.dataListenerRegistered = true;
1379
1348
  }
1380
1349
 
@@ -1407,8 +1376,9 @@ class Capability {
1407
1376
  }
1408
1377
 
1409
1378
  _dispatchEvent(message) {
1410
- const event = message && (message.event || message.type);
1411
- const payload = message && Object.prototype.hasOwnProperty.call(message, "data") ? message.data : message;
1379
+ const hasDataPayload = message && Object.prototype.hasOwnProperty.call(message, "data");
1380
+ const event = hasDataPayload ? (message.event || message.type || "data") : "data";
1381
+ const payload = hasDataPayload ? message.data : message;
1412
1382
  const dispatch = listeners => listeners && listeners.forEach(listener => listener(payload, message));
1413
1383
 
1414
1384
  dispatch(this.eventListeners.get(event));
@@ -1488,32 +1458,102 @@ class Provider {
1488
1458
  }
1489
1459
  }
1490
1460
 
1491
- const capabilityMap = new Map();
1492
- const providerMap = new Map();
1461
+ let DeviceCapabilityBase$1 = class DeviceCapabilityBase extends Capability {
1462
+ constructor(name, opts = {}) {
1463
+ super(name, {...opts, type: opts.type || name});
1464
+ }
1493
1465
 
1494
- const cutos$2 = {
1495
- capability(name, opts = {}) {
1496
- const key = opts.type || name;
1497
- if (!capabilityMap.has(key)) {
1498
- capabilityMap.set(key, new Capability(name, opts));
1466
+ init(opts = null, callback) {
1467
+ if (typeof opts === "function") {
1468
+ callback = opts;
1469
+ opts = null;
1499
1470
  }
1500
- return capabilityMap.get(key);
1501
- },
1471
+ return this.device.init(opts, callback);
1472
+ }
1502
1473
 
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
- },
1474
+ connect(params = {}, options = null, callback = null) {
1475
+ return this.call("connect", params, options, callback);
1476
+ }
1510
1477
 
1511
- device(name, opts = {}) {
1512
- return this.capability(name, opts);
1513
- },
1478
+ disconnect(params = {}, options = null, callback = null) {
1479
+ return this.call("disconnect", params, options, callback);
1480
+ }
1481
+
1482
+ readDeviceInfo(params = {}, options = null, callback = null) {
1483
+ return this.call("readDeviceInfo", params, options, callback);
1484
+ }
1485
+
1486
+ onData(listener) {
1487
+ return this.on("data", listener);
1488
+ }
1514
1489
 
1515
- driver(name, opts = {}) {
1516
- return this.provider(name, opts);
1490
+ onError(listener) {
1491
+ return this.on("error", listener);
1492
+ }
1493
+
1494
+ onStatus(listener) {
1495
+ let active = true;
1496
+ const statusListener = (status, raw) => {
1497
+ if (active) {
1498
+ listener(status, raw);
1499
+ }
1500
+ };
1501
+ const unsubscribeStatus = this.on("status", statusListener);
1502
+ const unsubscribeHeartbeatStatus = addDeviceStatusListener(this.type, statusListener);
1503
+ return () => {
1504
+ active = false;
1505
+ unsubscribeStatus();
1506
+ unsubscribeHeartbeatStatus();
1507
+ };
1508
+ }
1509
+
1510
+ };
1511
+
1512
+ let DeviceProviderBase$1 = class DeviceProviderBase extends Provider {
1513
+ constructor(name, opts = {}) {
1514
+ super(name, {...opts, type: opts.type || name});
1515
+ }
1516
+
1517
+ connect(handler) {
1518
+ return this.method("connect", handler);
1519
+ }
1520
+
1521
+ disconnect(handler) {
1522
+ return this.method("disconnect", handler);
1523
+ }
1524
+
1525
+ startBeat() {
1526
+ this.driver.startBeat();
1527
+ }
1528
+
1529
+ setBeatInterval(interval) {
1530
+ this.driver.beatInterval = interval;
1531
+ return this;
1532
+ }
1533
+
1534
+ setStatus(status = "ready", info = undefined) {
1535
+ const payload = {
1536
+ status,
1537
+ info,
1538
+ updatedAt: Date.now()
1539
+ };
1540
+ const heartbeatStatus = status === "error" || status === "offline"
1541
+ ? cutosAPI$1.HEARTBEAT_STATUS.ERROR
1542
+ : cutosAPI$1.HEARTBEAT_STATUS.ALIVE;
1543
+
1544
+ this.driver.updateStatusInfo(heartbeatStatus, payload);
1545
+ this.emit("status", payload);
1546
+ return this;
1547
+ }
1548
+
1549
+ emitData(data) {
1550
+ return this.emit("data", data);
1551
+ }
1552
+
1553
+ emitError(error) {
1554
+ const payload = normalizeError(error);
1555
+ this.setStatus("error", payload);
1556
+ return this.emit("error", payload);
1517
1557
  }
1518
1558
  };
1519
1559
 
@@ -1725,37 +1765,23 @@ const CoreAPI$3 = {
1725
1765
  return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SET_HTTP_PROXY, cb, {api, target}));
1726
1766
  },
1727
1767
 
1728
- capability(name, opts = {}) {
1729
- return cutos$2.capability(name, opts);
1730
- },
1731
-
1732
- provider(name, opts = {}) {
1733
- return cutos$2.provider(name, opts);
1734
- },
1735
-
1736
- device(name, opts = {}) {
1737
- return cutos$2.device(name, opts);
1738
- },
1739
-
1740
- driver(name, opts = {}) {
1741
- return cutos$2.driver(name, opts);
1742
- }
1743
1768
  };
1744
1769
 
1745
- cutos$3.CoreAPI = CoreAPI$3;
1746
- cutos$3.CoreDefine = {
1770
+ cutos.CoreAPI = CoreAPI$3;
1771
+ cutos.CoreDefine = {
1747
1772
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
1748
1773
  };
1749
- cutos$3.CoreClass = {
1750
- Device, Driver, Database, Capability, Provider, CutosError: CutosError$2
1774
+ cutos.CoreClass = {
1775
+ Device, Driver, Database, Capability, Provider, DeviceCapabilityBase: DeviceCapabilityBase$1, DeviceProviderBase: DeviceProviderBase$1, CutosError: CutosError$2
1751
1776
  };
1752
1777
 
1753
- cutos$3.cutosAPI = cutosAPI$1;
1754
- cutos$3.cutos = cutos$2;
1755
- cutos$3.CutosError = CutosError$2;
1778
+ cutos.cutosAPI = cutosAPI$1;
1779
+ cutos.CutosError = CutosError$2;
1780
+ cutos.DeviceCapabilityBase = DeviceCapabilityBase$1;
1781
+ cutos.DeviceProviderBase = DeviceProviderBase$1;
1756
1782
 
1757
1783
  // simulate cutos core for development
1758
- const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos$3;
1784
+ const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos;
1759
1785
 
1760
1786
  const CoreSimulator$1 = {
1761
1787
  start: function (driver, port = 1883) {
@@ -1787,7 +1813,7 @@ const CoreSimulator$1 = {
1787
1813
 
1788
1814
  var coreSimulator = {CoreSimulator: CoreSimulator$1};
1789
1815
 
1790
- const {CoreAPI: CoreAPI$1, cutos: cutos$1, CutosError: CutosError$1} = cutos$3;
1816
+ const {CoreAPI: CoreAPI$1, CutosError: CutosError$1} = cutos;
1791
1817
 
1792
1818
  const original = {
1793
1819
  init: CoreAPI$1.init,
@@ -1801,13 +1827,7 @@ const original = {
1801
1827
  shell: CoreAPI$1.shell,
1802
1828
  getVolume: CoreAPI$1.getVolume,
1803
1829
  setVolume: CoreAPI$1.setVolume,
1804
- setHttpProxy: CoreAPI$1.setHttpProxy,
1805
- capability: CoreAPI$1.capability,
1806
- provider: CoreAPI$1.provider,
1807
- cutosCapability: cutos$1.capability,
1808
- cutosProvider: cutos$1.provider,
1809
- cutosDevice: cutos$1.device,
1810
- cutosDriver: cutos$1.driver
1830
+ setHttpProxy: CoreAPI$1.setHttpProxy
1811
1831
  };
1812
1832
 
1813
1833
  const mockState = {
@@ -1858,130 +1878,6 @@ function registerCapability(name, definition = {}) {
1858
1878
  return record;
1859
1879
  }
1860
1880
 
1861
- class MockCapability {
1862
- constructor(name) {
1863
- if (!name) {
1864
- throw new Error('capability name cannot be empty.');
1865
- }
1866
- this.name = name;
1867
- this.type = name;
1868
- ensureCapabilityRecord(name);
1869
- }
1870
-
1871
- call(method, params = {}, options = null, callback = null) {
1872
- if (typeof options === 'function') {
1873
- callback = options;
1874
- options = null;
1875
- }
1876
-
1877
- const record = ensureCapabilityRecord(this.name);
1878
- const handler = record.methods.get(method);
1879
-
1880
- if (!handler) {
1881
- const error = new CutosError$1('METHOD_NOT_FOUND', `Mock capability method ${this.name}.${method} is not registered.`);
1882
- return resolveCallback(callback, null, error);
1883
- }
1884
-
1885
- try {
1886
- const result = handler(params, {capability: this, method, options});
1887
- if (result && typeof result.then === 'function') {
1888
- return typeof callback === 'function'
1889
- ? (result.then(value => callback(value)).catch(error => callback(null, error)), true)
1890
- : result;
1891
- }
1892
- return resolveCallback(callback, result);
1893
- } catch (error) {
1894
- return resolveCallback(callback, null, error);
1895
- }
1896
- }
1897
-
1898
- on(event, listener) {
1899
- if (typeof listener !== 'function') {
1900
- throw new Error('capability event listener must be a function.');
1901
- }
1902
-
1903
- const record = ensureCapabilityRecord(this.name);
1904
- if (!record.listeners.has(event)) {
1905
- record.listeners.set(event, new Set());
1906
- }
1907
- record.listeners.get(event).add(listener);
1908
- return () => this.off(event, listener);
1909
- }
1910
-
1911
- off(event, listener = null) {
1912
- const record = ensureCapabilityRecord(this.name);
1913
- if (!record.listeners.has(event)) {
1914
- return;
1915
- }
1916
- if (!listener) {
1917
- record.listeners.delete(event);
1918
- return;
1919
- }
1920
-
1921
- const listeners = record.listeners.get(event);
1922
- listeners.delete(listener);
1923
- if (listeners.size === 0) {
1924
- record.listeners.delete(event);
1925
- }
1926
- }
1927
-
1928
- dispose() {
1929
- const record = ensureCapabilityRecord(this.name);
1930
- record.listeners.clear();
1931
- return true;
1932
- }
1933
- }
1934
-
1935
- class MockProvider {
1936
- constructor(name) {
1937
- if (!name) {
1938
- throw new Error('provider name cannot be empty.');
1939
- }
1940
- this.name = name;
1941
- this.type = name;
1942
- ensureCapabilityRecord(name);
1943
- }
1944
-
1945
- method(method, handler) {
1946
- if (typeof handler !== 'function') {
1947
- throw new Error('provider method handler must be a function.');
1948
- }
1949
- ensureCapabilityRecord(this.name).methods.set(method, handler);
1950
- return this;
1951
- }
1952
-
1953
- emit(event, data) {
1954
- const record = ensureCapabilityRecord(this.name);
1955
- const listeners = record.listeners.get(event);
1956
- const allListeners = record.listeners.get('*');
1957
- const dispatch = listener => listener(data, {event, data});
1958
-
1959
- listeners && listeners.forEach(dispatch);
1960
- allListeners && allListeners.forEach(dispatch);
1961
- return true;
1962
- }
1963
-
1964
- dispose() {
1965
- const record = ensureCapabilityRecord(this.name);
1966
- record.methods.clear();
1967
- return true;
1968
- }
1969
- }
1970
-
1971
- function getMockCapability(name) {
1972
- if (!mockState.capabilities.has(name)) {
1973
- registerCapability(name);
1974
- }
1975
- return new MockCapability(name);
1976
- }
1977
-
1978
- function getMockProvider(name) {
1979
- if (!mockState.providers.has(name)) {
1980
- mockState.providers.set(name, new MockProvider(name));
1981
- }
1982
- return mockState.providers.get(name);
1983
- }
1984
-
1985
1881
  function installCutosMock$1(options = {}) {
1986
1882
  mockState.options = options;
1987
1883
  mockState.connected = false;
@@ -2013,13 +1909,6 @@ function installCutosMock$1(options = {}) {
2013
1909
  return resolveCallback(callback, vol);
2014
1910
  };
2015
1911
  CoreAPI$1.setHttpProxy = (api, target, callback) => resolveCallback(callback, {api, target});
2016
- CoreAPI$1.capability = (name) => getMockCapability(name);
2017
- CoreAPI$1.provider = (name) => getMockProvider(name);
2018
-
2019
- cutos$1.capability = (name) => getMockCapability(name);
2020
- cutos$1.provider = (name) => getMockProvider(name);
2021
- cutos$1.device = (name) => getMockCapability(name);
2022
- cutos$1.driver = (name) => getMockProvider(name);
2023
1912
 
2024
1913
  mockState.installed = true;
2025
1914
  return CutosMock$1;
@@ -2038,12 +1927,6 @@ function uninstallCutosMock$1() {
2038
1927
  CoreAPI$1.getVolume = original.getVolume;
2039
1928
  CoreAPI$1.setVolume = original.setVolume;
2040
1929
  CoreAPI$1.setHttpProxy = original.setHttpProxy;
2041
- CoreAPI$1.capability = original.capability;
2042
- CoreAPI$1.provider = original.provider;
2043
- cutos$1.capability = original.cutosCapability;
2044
- cutos$1.provider = original.cutosProvider;
2045
- cutos$1.device = original.cutosDevice;
2046
- cutos$1.driver = original.cutosDriver;
2047
1930
 
2048
1931
  mockState.connected = false;
2049
1932
  mockState.installed = false;
@@ -2055,14 +1938,20 @@ const CutosMock$1 = {
2055
1938
  install: installCutosMock$1,
2056
1939
  uninstall: uninstallCutosMock$1,
2057
1940
  registerCapability,
2058
- capability: getMockCapability,
2059
- provider: getMockProvider,
2060
1941
  isInstalled: () => mockState.installed
2061
1942
  };
2062
1943
 
2063
1944
  var mock = {CutosMock: CutosMock$1, installCutosMock: installCutosMock$1, uninstallCutosMock: uninstallCutosMock$1};
2064
1945
 
2065
- const {CoreAPI, CoreDefine, CoreClass, cutosAPI, cutos, CutosError} = cutos$3;
1946
+ const {
1947
+ CoreAPI,
1948
+ CoreDefine,
1949
+ CoreClass,
1950
+ cutosAPI,
1951
+ CutosError,
1952
+ DeviceCapabilityBase,
1953
+ DeviceProviderBase
1954
+ } = cutos;
2066
1955
  const {CoreSimulator} = coreSimulator;
2067
1956
  const {CutosMock, installCutosMock, uninstallCutosMock} = mock;
2068
1957
 
@@ -2084,8 +1973,9 @@ var CoreAPI_1 = src.CoreAPI = CoreAPI;
2084
1973
  var CoreDefine_1 = src.CoreDefine = CoreDefine;
2085
1974
  var CoreClass_1 = src.CoreClass = CoreClass;
2086
1975
  var cutosAPI_1 = src.cutosAPI = cutosAPI;
2087
- var cutos_1 = src.cutos = cutos;
2088
1976
  var CutosError_1 = src.CutosError = CutosError;
1977
+ var DeviceCapabilityBase_1 = src.DeviceCapabilityBase = DeviceCapabilityBase;
1978
+ var DeviceProviderBase_1 = src.DeviceProviderBase = DeviceProviderBase;
2089
1979
  var CoreSimulator_1 = src.CoreSimulator = CoreSimulator;
2090
1980
  var CutosMock_1 = src.CutosMock = CutosMock;
2091
1981
  var installCutosMock_1 = src.installCutosMock = installCutosMock;
@@ -2097,7 +1987,8 @@ exports.CoreDefine = CoreDefine_1;
2097
1987
  exports.CoreSimulator = CoreSimulator_1;
2098
1988
  exports.CutosError = CutosError_1;
2099
1989
  exports.CutosMock = CutosMock_1;
2100
- exports.cutos = cutos_1;
1990
+ exports.DeviceCapabilityBase = DeviceCapabilityBase_1;
1991
+ exports.DeviceProviderBase = DeviceProviderBase_1;
2101
1992
  exports.cutosAPI = cutosAPI_1;
2102
1993
  exports.default = src;
2103
1994
  exports.installCutosMock = installCutosMock_1;