@cutos/core 4.0.2 → 4.0.5
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 +33 -192
- package/dist/cutos.cjs.js +181 -272
- package/dist/cutos.esm.js +180 -272
- package/package.json +3 -2
- package/types/index.d.ts +78 -19
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 = {};
|
|
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
|
|
@@ -690,13 +690,14 @@ const cutosAPI$2 = {
|
|
|
690
690
|
var cutosApi = cutosAPI$2;
|
|
691
691
|
|
|
692
692
|
var name = "@cutos/core";
|
|
693
|
-
var version = "4.0.
|
|
693
|
+
var version = "4.0.5";
|
|
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
697
|
var types = "types/index.d.ts";
|
|
698
698
|
var scripts = {
|
|
699
|
-
"build:rollup": "rollup -c rollup.config.cjs"
|
|
699
|
+
"build:rollup": "rollup -c rollup.config.cjs",
|
|
700
|
+
test: "node test/device-provider-base.mjs"
|
|
700
701
|
};
|
|
701
702
|
var keywords = [
|
|
702
703
|
"CUTOS",
|
|
@@ -1409,6 +1410,38 @@ class Heartbeat {
|
|
|
1409
1410
|
}
|
|
1410
1411
|
}
|
|
1411
1412
|
|
|
1413
|
+
const deviceStatusListenerMap = new Map();
|
|
1414
|
+
let deviceHeartbeatListenerRegistered = false;
|
|
1415
|
+
|
|
1416
|
+
function addDeviceStatusListener(type, listener) {
|
|
1417
|
+
if (!deviceStatusListenerMap.has(type)) {
|
|
1418
|
+
deviceStatusListenerMap.set(type, new Set());
|
|
1419
|
+
}
|
|
1420
|
+
deviceStatusListenerMap.get(type).add(listener);
|
|
1421
|
+
|
|
1422
|
+
if (!deviceHeartbeatListenerRegistered) {
|
|
1423
|
+
listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, msg => {
|
|
1424
|
+
deviceStatusListenerMap.forEach((listeners, listenerType) => {
|
|
1425
|
+
if (listenerType === cutosAPI$1.DEVICE.DEFAULT || listenerType === msg.pinger) {
|
|
1426
|
+
listeners.forEach(statusListener => statusListener(msg));
|
|
1427
|
+
}
|
|
1428
|
+
});
|
|
1429
|
+
});
|
|
1430
|
+
deviceHeartbeatListenerRegistered = true;
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
return () => {
|
|
1434
|
+
const listeners = deviceStatusListenerMap.get(type);
|
|
1435
|
+
if (!listeners) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
listeners.delete(listener);
|
|
1439
|
+
if (listeners.size === 0) {
|
|
1440
|
+
deviceStatusListenerMap.delete(type);
|
|
1441
|
+
}
|
|
1442
|
+
};
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1412
1445
|
class Device {
|
|
1413
1446
|
/**
|
|
1414
1447
|
* The constructor of Device
|
|
@@ -1421,27 +1454,6 @@ class Device {
|
|
|
1421
1454
|
this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
|
|
1422
1455
|
this.topicResponse = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-response';
|
|
1423
1456
|
this.topicCommand = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-command';
|
|
1424
|
-
this.data = {};
|
|
1425
|
-
|
|
1426
|
-
if (Device.objectMap === undefined) {
|
|
1427
|
-
Device.objectMap = new Map();
|
|
1428
|
-
}
|
|
1429
|
-
Device.objectMap.set(this.type, this);
|
|
1430
|
-
|
|
1431
|
-
if (Device.heartbeatListener === undefined) {
|
|
1432
|
-
Device.heartbeatListener = function (msg) {
|
|
1433
|
-
for (const [key, obj] of Device.objectMap) {
|
|
1434
|
-
if (obj.statusListener !== undefined) {
|
|
1435
|
-
if (obj.type === "default-device" || obj.type === msg.pinger) {
|
|
1436
|
-
obj.statusListener(msg);
|
|
1437
|
-
obj.msg = msg;
|
|
1438
|
-
}
|
|
1439
|
-
}
|
|
1440
|
-
}
|
|
1441
|
-
//console.debug(JSON.stringify(msg));
|
|
1442
|
-
};
|
|
1443
|
-
listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
|
|
1444
|
-
}
|
|
1445
1457
|
// subscribe command callback
|
|
1446
1458
|
client.subscribe(this.topicCommand + '-callback');
|
|
1447
1459
|
}
|
|
@@ -1482,22 +1494,6 @@ class Device {
|
|
|
1482
1494
|
});
|
|
1483
1495
|
}
|
|
1484
1496
|
|
|
1485
|
-
/**
|
|
1486
|
-
* Get device status
|
|
1487
|
-
* @param {Function} listener
|
|
1488
|
-
*/
|
|
1489
|
-
onStatus(listener) {
|
|
1490
|
-
this.statusListener = listener;
|
|
1491
|
-
}
|
|
1492
|
-
|
|
1493
|
-
/**
|
|
1494
|
-
* Get device status
|
|
1495
|
-
* @param {Function} listener
|
|
1496
|
-
*/
|
|
1497
|
-
getCurrentStatus() {
|
|
1498
|
-
return this.msg;
|
|
1499
|
-
}
|
|
1500
|
-
|
|
1501
1497
|
/**
|
|
1502
1498
|
*
|
|
1503
1499
|
* @param command - command description object {cmd: 'command', args: (''|{}) }
|
|
@@ -1515,40 +1511,6 @@ class Device {
|
|
|
1515
1511
|
}
|
|
1516
1512
|
return maybePromise(callback, cb => publishRequest(this.topicCommand, cb, command, undefined, opts))
|
|
1517
1513
|
}
|
|
1518
|
-
|
|
1519
|
-
/**
|
|
1520
|
-
* Receive data sent by the driver
|
|
1521
|
-
* @param listener - listener(data)
|
|
1522
|
-
*/
|
|
1523
|
-
onData(listener) {
|
|
1524
|
-
if (!listenerMap.has(this.topicData)) {
|
|
1525
|
-
client.subscribe(this.topicData); // receive data from device
|
|
1526
|
-
}
|
|
1527
|
-
|
|
1528
|
-
listenerMap.set(this.topicData, (data) => {
|
|
1529
|
-
this.data = data;
|
|
1530
|
-
listener(data);
|
|
1531
|
-
});
|
|
1532
|
-
return () => this.offData(listener);
|
|
1533
|
-
}
|
|
1534
|
-
|
|
1535
|
-
offData(listener = null) {
|
|
1536
|
-
listenerMap.delete(this.topicData);
|
|
1537
|
-
}
|
|
1538
|
-
|
|
1539
|
-
getCurrentData() {
|
|
1540
|
-
return this.data;
|
|
1541
|
-
}
|
|
1542
|
-
|
|
1543
|
-
/**
|
|
1544
|
-
*
|
|
1545
|
-
* @param callback (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
|
|
1546
|
-
*/
|
|
1547
|
-
readDeviceInfo(callback) {
|
|
1548
|
-
let msg = {};
|
|
1549
|
-
msg.cmd = "read-device-info";
|
|
1550
|
-
return this.sendCommand(msg, callback);
|
|
1551
|
-
}
|
|
1552
1514
|
}
|
|
1553
1515
|
|
|
1554
1516
|
class Driver {
|
|
@@ -1568,12 +1530,16 @@ class Driver {
|
|
|
1568
1530
|
this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
|
|
1569
1531
|
this.topicResponse = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-response';
|
|
1570
1532
|
this.topicCommand = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-command';
|
|
1533
|
+
this.beatTimer = null;
|
|
1571
1534
|
}
|
|
1572
1535
|
|
|
1573
1536
|
startBeat() {
|
|
1537
|
+
if (this.beatTimer) {
|
|
1538
|
+
return;
|
|
1539
|
+
}
|
|
1574
1540
|
this.heartbeat.register();
|
|
1575
1541
|
let _this = this;
|
|
1576
|
-
setInterval(function () {
|
|
1542
|
+
this.beatTimer = setInterval(function () {
|
|
1577
1543
|
if (_this.statusInfo.status !== undefined && _this.statusInfo.status !== _this.heartbeat.STATUS.REGISTER) {
|
|
1578
1544
|
_this.heartbeat.ping(_this.statusInfo.status, _this.statusInfo.info);
|
|
1579
1545
|
}
|
|
@@ -1666,7 +1632,11 @@ class Capability {
|
|
|
1666
1632
|
this.eventListeners.get(event).add(listener);
|
|
1667
1633
|
|
|
1668
1634
|
if (!this.dataListenerRegistered) {
|
|
1669
|
-
|
|
1635
|
+
if (!listenerMap.has(this.device.topicData)) {
|
|
1636
|
+
client.subscribe(this.device.topicData);
|
|
1637
|
+
}
|
|
1638
|
+
listenerMap.set(this.device.topicData, message => this._dispatchEvent(message));
|
|
1639
|
+
this.unsubscribeData = () => listenerMap.delete(this.device.topicData);
|
|
1670
1640
|
this.dataListenerRegistered = true;
|
|
1671
1641
|
}
|
|
1672
1642
|
|
|
@@ -1699,8 +1669,9 @@ class Capability {
|
|
|
1699
1669
|
}
|
|
1700
1670
|
|
|
1701
1671
|
_dispatchEvent(message) {
|
|
1702
|
-
const
|
|
1703
|
-
const
|
|
1672
|
+
const hasDataPayload = message && Object.prototype.hasOwnProperty.call(message, "data");
|
|
1673
|
+
const event = hasDataPayload ? (message.event || message.type || "data") : "data";
|
|
1674
|
+
const payload = hasDataPayload ? message.data : message;
|
|
1704
1675
|
const dispatch = listeners => listeners && listeners.forEach(listener => listener(payload, message));
|
|
1705
1676
|
|
|
1706
1677
|
dispatch(this.eventListeners.get(event));
|
|
@@ -1780,32 +1751,119 @@ class Provider {
|
|
|
1780
1751
|
}
|
|
1781
1752
|
}
|
|
1782
1753
|
|
|
1783
|
-
|
|
1784
|
-
|
|
1754
|
+
let DeviceCapabilityBase$1 = class DeviceCapabilityBase extends Capability {
|
|
1755
|
+
constructor(name, opts = {}) {
|
|
1756
|
+
super(name, {...opts, type: opts.type || name});
|
|
1757
|
+
}
|
|
1785
1758
|
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
capabilityMap.set(key, new Capability(name, opts));
|
|
1759
|
+
init(opts = null, callback) {
|
|
1760
|
+
if (typeof opts === "function") {
|
|
1761
|
+
callback = opts;
|
|
1762
|
+
opts = null;
|
|
1791
1763
|
}
|
|
1792
|
-
return
|
|
1793
|
-
}
|
|
1764
|
+
return this.device.init(opts, callback);
|
|
1765
|
+
}
|
|
1794
1766
|
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
providerMap.set(key, new Provider(name, opts));
|
|
1799
|
-
}
|
|
1800
|
-
return providerMap.get(key);
|
|
1801
|
-
},
|
|
1767
|
+
connect(params = {}, options = null, callback = null) {
|
|
1768
|
+
return this.call("connect", params, options, callback);
|
|
1769
|
+
}
|
|
1802
1770
|
|
|
1803
|
-
|
|
1804
|
-
return this.
|
|
1805
|
-
}
|
|
1771
|
+
disconnect(params = {}, options = null, callback = null) {
|
|
1772
|
+
return this.call("disconnect", params, options, callback);
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
readDeviceInfo(params = {}, options = null, callback = null) {
|
|
1776
|
+
return this.call("readDeviceInfo", params, options, callback);
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
onData(listener) {
|
|
1780
|
+
return this.on("data", listener);
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
onError(listener) {
|
|
1784
|
+
return this.on("error", listener);
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
onStatus(listener) {
|
|
1788
|
+
let active = true;
|
|
1789
|
+
const statusListener = (status, raw) => {
|
|
1790
|
+
if (active) {
|
|
1791
|
+
listener(status, raw);
|
|
1792
|
+
}
|
|
1793
|
+
};
|
|
1794
|
+
const unsubscribeStatus = this.on("status", statusListener);
|
|
1795
|
+
const unsubscribeHeartbeatStatus = addDeviceStatusListener(this.type, statusListener);
|
|
1796
|
+
return () => {
|
|
1797
|
+
active = false;
|
|
1798
|
+
unsubscribeStatus();
|
|
1799
|
+
unsubscribeHeartbeatStatus();
|
|
1800
|
+
};
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
};
|
|
1804
|
+
|
|
1805
|
+
let DeviceProviderBase$1 = class DeviceProviderBase extends Provider {
|
|
1806
|
+
constructor(name, opts = {}) {
|
|
1807
|
+
super(name, {...opts, type: opts.type || name});
|
|
1808
|
+
|
|
1809
|
+
this.method("connect", (params, context) => this.connect(params, context));
|
|
1810
|
+
this.method("disconnect", (params, context) => this.disconnect(params, context));
|
|
1811
|
+
this.method("readDeviceInfo", (params, context) => this.readDeviceInfo(params, context));
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
connect(params, context) {
|
|
1815
|
+
throw new CutosError$2(
|
|
1816
|
+
"DEVICE_CONNECT_NOT_IMPLEMENTED",
|
|
1817
|
+
"connect() is not implemented."
|
|
1818
|
+
);
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
disconnect(params, context) {
|
|
1822
|
+
throw new CutosError$2(
|
|
1823
|
+
"DEVICE_DISCONNECT_NOT_IMPLEMENTED",
|
|
1824
|
+
"disconnect() is not implemented."
|
|
1825
|
+
);
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
readDeviceInfo(params, context) {
|
|
1829
|
+
throw new CutosError$2(
|
|
1830
|
+
"DEVICE_INFO_NOT_IMPLEMENTED",
|
|
1831
|
+
"readDeviceInfo() is not implemented."
|
|
1832
|
+
);
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
startBeat() {
|
|
1836
|
+
this.driver.startBeat();
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
setBeatInterval(interval) {
|
|
1840
|
+
this.driver.beatInterval = interval;
|
|
1841
|
+
return this;
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
setStatus(status = "ready", info = undefined) {
|
|
1845
|
+
const payload = {
|
|
1846
|
+
status,
|
|
1847
|
+
info,
|
|
1848
|
+
updatedAt: Date.now()
|
|
1849
|
+
};
|
|
1850
|
+
const heartbeatStatus = status === "error" || status === "offline"
|
|
1851
|
+
? cutosAPI$1.HEARTBEAT_STATUS.ERROR
|
|
1852
|
+
: cutosAPI$1.HEARTBEAT_STATUS.ALIVE;
|
|
1806
1853
|
|
|
1807
|
-
|
|
1808
|
-
|
|
1854
|
+
this.driver.updateStatusInfo(heartbeatStatus, payload);
|
|
1855
|
+
this.emit("status", payload);
|
|
1856
|
+
return this;
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
emitData(data) {
|
|
1860
|
+
return this.emit("data", data);
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
emitError(error) {
|
|
1864
|
+
const payload = normalizeError(error);
|
|
1865
|
+
this.setStatus("error", payload);
|
|
1866
|
+
return this.emit("error", payload);
|
|
1809
1867
|
}
|
|
1810
1868
|
};
|
|
1811
1869
|
|
|
@@ -2017,37 +2075,23 @@ const CoreAPI$3 = {
|
|
|
2017
2075
|
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SET_HTTP_PROXY, cb, {api, target}));
|
|
2018
2076
|
},
|
|
2019
2077
|
|
|
2020
|
-
capability(name, opts = {}) {
|
|
2021
|
-
return cutos$2.capability(name, opts);
|
|
2022
|
-
},
|
|
2023
|
-
|
|
2024
|
-
provider(name, opts = {}) {
|
|
2025
|
-
return cutos$2.provider(name, opts);
|
|
2026
|
-
},
|
|
2027
|
-
|
|
2028
|
-
device(name, opts = {}) {
|
|
2029
|
-
return cutos$2.device(name, opts);
|
|
2030
|
-
},
|
|
2031
|
-
|
|
2032
|
-
driver(name, opts = {}) {
|
|
2033
|
-
return cutos$2.driver(name, opts);
|
|
2034
|
-
}
|
|
2035
2078
|
};
|
|
2036
2079
|
|
|
2037
|
-
cutos
|
|
2038
|
-
cutos
|
|
2080
|
+
cutos.CoreAPI = CoreAPI$3;
|
|
2081
|
+
cutos.CoreDefine = {
|
|
2039
2082
|
DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
|
|
2040
2083
|
};
|
|
2041
|
-
cutos
|
|
2042
|
-
Device, Driver, Database, Capability, Provider, CutosError: CutosError$2
|
|
2084
|
+
cutos.CoreClass = {
|
|
2085
|
+
Device, Driver, Database, Capability, Provider, DeviceCapabilityBase: DeviceCapabilityBase$1, DeviceProviderBase: DeviceProviderBase$1, CutosError: CutosError$2
|
|
2043
2086
|
};
|
|
2044
2087
|
|
|
2045
|
-
cutos
|
|
2046
|
-
cutos
|
|
2047
|
-
cutos
|
|
2088
|
+
cutos.cutosAPI = cutosAPI$1;
|
|
2089
|
+
cutos.CutosError = CutosError$2;
|
|
2090
|
+
cutos.DeviceCapabilityBase = DeviceCapabilityBase$1;
|
|
2091
|
+
cutos.DeviceProviderBase = DeviceProviderBase$1;
|
|
2048
2092
|
|
|
2049
2093
|
// simulate cutos core for development
|
|
2050
|
-
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos
|
|
2094
|
+
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos;
|
|
2051
2095
|
|
|
2052
2096
|
const CoreSimulator$1 = {
|
|
2053
2097
|
start: function (driver, port = 1883) {
|
|
@@ -2079,7 +2123,7 @@ const CoreSimulator$1 = {
|
|
|
2079
2123
|
|
|
2080
2124
|
var coreSimulator = {CoreSimulator: CoreSimulator$1};
|
|
2081
2125
|
|
|
2082
|
-
const {CoreAPI: CoreAPI$1,
|
|
2126
|
+
const {CoreAPI: CoreAPI$1, CutosError: CutosError$1} = cutos;
|
|
2083
2127
|
|
|
2084
2128
|
const original = {
|
|
2085
2129
|
init: CoreAPI$1.init,
|
|
@@ -2093,13 +2137,7 @@ const original = {
|
|
|
2093
2137
|
shell: CoreAPI$1.shell,
|
|
2094
2138
|
getVolume: CoreAPI$1.getVolume,
|
|
2095
2139
|
setVolume: CoreAPI$1.setVolume,
|
|
2096
|
-
setHttpProxy: CoreAPI$1.setHttpProxy
|
|
2097
|
-
capability: CoreAPI$1.capability,
|
|
2098
|
-
provider: CoreAPI$1.provider,
|
|
2099
|
-
cutosCapability: cutos$1.capability,
|
|
2100
|
-
cutosProvider: cutos$1.provider,
|
|
2101
|
-
cutosDevice: cutos$1.device,
|
|
2102
|
-
cutosDriver: cutos$1.driver
|
|
2140
|
+
setHttpProxy: CoreAPI$1.setHttpProxy
|
|
2103
2141
|
};
|
|
2104
2142
|
|
|
2105
2143
|
const mockState = {
|
|
@@ -2150,130 +2188,6 @@ function registerCapability(name, definition = {}) {
|
|
|
2150
2188
|
return record;
|
|
2151
2189
|
}
|
|
2152
2190
|
|
|
2153
|
-
class MockCapability {
|
|
2154
|
-
constructor(name) {
|
|
2155
|
-
if (!name) {
|
|
2156
|
-
throw new Error('capability name cannot be empty.');
|
|
2157
|
-
}
|
|
2158
|
-
this.name = name;
|
|
2159
|
-
this.type = name;
|
|
2160
|
-
ensureCapabilityRecord(name);
|
|
2161
|
-
}
|
|
2162
|
-
|
|
2163
|
-
call(method, params = {}, options = null, callback = null) {
|
|
2164
|
-
if (typeof options === 'function') {
|
|
2165
|
-
callback = options;
|
|
2166
|
-
options = null;
|
|
2167
|
-
}
|
|
2168
|
-
|
|
2169
|
-
const record = ensureCapabilityRecord(this.name);
|
|
2170
|
-
const handler = record.methods.get(method);
|
|
2171
|
-
|
|
2172
|
-
if (!handler) {
|
|
2173
|
-
const error = new CutosError$1('METHOD_NOT_FOUND', `Mock capability method ${this.name}.${method} is not registered.`);
|
|
2174
|
-
return resolveCallback(callback, null, error);
|
|
2175
|
-
}
|
|
2176
|
-
|
|
2177
|
-
try {
|
|
2178
|
-
const result = handler(params, {capability: this, method, options});
|
|
2179
|
-
if (result && typeof result.then === 'function') {
|
|
2180
|
-
return typeof callback === 'function'
|
|
2181
|
-
? (result.then(value => callback(value)).catch(error => callback(null, error)), true)
|
|
2182
|
-
: result;
|
|
2183
|
-
}
|
|
2184
|
-
return resolveCallback(callback, result);
|
|
2185
|
-
} catch (error) {
|
|
2186
|
-
return resolveCallback(callback, null, error);
|
|
2187
|
-
}
|
|
2188
|
-
}
|
|
2189
|
-
|
|
2190
|
-
on(event, listener) {
|
|
2191
|
-
if (typeof listener !== 'function') {
|
|
2192
|
-
throw new Error('capability event listener must be a function.');
|
|
2193
|
-
}
|
|
2194
|
-
|
|
2195
|
-
const record = ensureCapabilityRecord(this.name);
|
|
2196
|
-
if (!record.listeners.has(event)) {
|
|
2197
|
-
record.listeners.set(event, new Set());
|
|
2198
|
-
}
|
|
2199
|
-
record.listeners.get(event).add(listener);
|
|
2200
|
-
return () => this.off(event, listener);
|
|
2201
|
-
}
|
|
2202
|
-
|
|
2203
|
-
off(event, listener = null) {
|
|
2204
|
-
const record = ensureCapabilityRecord(this.name);
|
|
2205
|
-
if (!record.listeners.has(event)) {
|
|
2206
|
-
return;
|
|
2207
|
-
}
|
|
2208
|
-
if (!listener) {
|
|
2209
|
-
record.listeners.delete(event);
|
|
2210
|
-
return;
|
|
2211
|
-
}
|
|
2212
|
-
|
|
2213
|
-
const listeners = record.listeners.get(event);
|
|
2214
|
-
listeners.delete(listener);
|
|
2215
|
-
if (listeners.size === 0) {
|
|
2216
|
-
record.listeners.delete(event);
|
|
2217
|
-
}
|
|
2218
|
-
}
|
|
2219
|
-
|
|
2220
|
-
dispose() {
|
|
2221
|
-
const record = ensureCapabilityRecord(this.name);
|
|
2222
|
-
record.listeners.clear();
|
|
2223
|
-
return true;
|
|
2224
|
-
}
|
|
2225
|
-
}
|
|
2226
|
-
|
|
2227
|
-
class MockProvider {
|
|
2228
|
-
constructor(name) {
|
|
2229
|
-
if (!name) {
|
|
2230
|
-
throw new Error('provider name cannot be empty.');
|
|
2231
|
-
}
|
|
2232
|
-
this.name = name;
|
|
2233
|
-
this.type = name;
|
|
2234
|
-
ensureCapabilityRecord(name);
|
|
2235
|
-
}
|
|
2236
|
-
|
|
2237
|
-
method(method, handler) {
|
|
2238
|
-
if (typeof handler !== 'function') {
|
|
2239
|
-
throw new Error('provider method handler must be a function.');
|
|
2240
|
-
}
|
|
2241
|
-
ensureCapabilityRecord(this.name).methods.set(method, handler);
|
|
2242
|
-
return this;
|
|
2243
|
-
}
|
|
2244
|
-
|
|
2245
|
-
emit(event, data) {
|
|
2246
|
-
const record = ensureCapabilityRecord(this.name);
|
|
2247
|
-
const listeners = record.listeners.get(event);
|
|
2248
|
-
const allListeners = record.listeners.get('*');
|
|
2249
|
-
const dispatch = listener => listener(data, {event, data});
|
|
2250
|
-
|
|
2251
|
-
listeners && listeners.forEach(dispatch);
|
|
2252
|
-
allListeners && allListeners.forEach(dispatch);
|
|
2253
|
-
return true;
|
|
2254
|
-
}
|
|
2255
|
-
|
|
2256
|
-
dispose() {
|
|
2257
|
-
const record = ensureCapabilityRecord(this.name);
|
|
2258
|
-
record.methods.clear();
|
|
2259
|
-
return true;
|
|
2260
|
-
}
|
|
2261
|
-
}
|
|
2262
|
-
|
|
2263
|
-
function getMockCapability(name) {
|
|
2264
|
-
if (!mockState.capabilities.has(name)) {
|
|
2265
|
-
registerCapability(name);
|
|
2266
|
-
}
|
|
2267
|
-
return new MockCapability(name);
|
|
2268
|
-
}
|
|
2269
|
-
|
|
2270
|
-
function getMockProvider(name) {
|
|
2271
|
-
if (!mockState.providers.has(name)) {
|
|
2272
|
-
mockState.providers.set(name, new MockProvider(name));
|
|
2273
|
-
}
|
|
2274
|
-
return mockState.providers.get(name);
|
|
2275
|
-
}
|
|
2276
|
-
|
|
2277
2191
|
function installCutosMock$1(options = {}) {
|
|
2278
2192
|
mockState.options = options;
|
|
2279
2193
|
mockState.connected = false;
|
|
@@ -2305,13 +2219,6 @@ function installCutosMock$1(options = {}) {
|
|
|
2305
2219
|
return resolveCallback(callback, vol);
|
|
2306
2220
|
};
|
|
2307
2221
|
CoreAPI$1.setHttpProxy = (api, target, callback) => resolveCallback(callback, {api, target});
|
|
2308
|
-
CoreAPI$1.capability = (name) => getMockCapability(name);
|
|
2309
|
-
CoreAPI$1.provider = (name) => getMockProvider(name);
|
|
2310
|
-
|
|
2311
|
-
cutos$1.capability = (name) => getMockCapability(name);
|
|
2312
|
-
cutos$1.provider = (name) => getMockProvider(name);
|
|
2313
|
-
cutos$1.device = (name) => getMockCapability(name);
|
|
2314
|
-
cutos$1.driver = (name) => getMockProvider(name);
|
|
2315
2222
|
|
|
2316
2223
|
mockState.installed = true;
|
|
2317
2224
|
return CutosMock$1;
|
|
@@ -2330,12 +2237,6 @@ function uninstallCutosMock$1() {
|
|
|
2330
2237
|
CoreAPI$1.getVolume = original.getVolume;
|
|
2331
2238
|
CoreAPI$1.setVolume = original.setVolume;
|
|
2332
2239
|
CoreAPI$1.setHttpProxy = original.setHttpProxy;
|
|
2333
|
-
CoreAPI$1.capability = original.capability;
|
|
2334
|
-
CoreAPI$1.provider = original.provider;
|
|
2335
|
-
cutos$1.capability = original.cutosCapability;
|
|
2336
|
-
cutos$1.provider = original.cutosProvider;
|
|
2337
|
-
cutos$1.device = original.cutosDevice;
|
|
2338
|
-
cutos$1.driver = original.cutosDriver;
|
|
2339
2240
|
|
|
2340
2241
|
mockState.connected = false;
|
|
2341
2242
|
mockState.installed = false;
|
|
@@ -2347,14 +2248,20 @@ const CutosMock$1 = {
|
|
|
2347
2248
|
install: installCutosMock$1,
|
|
2348
2249
|
uninstall: uninstallCutosMock$1,
|
|
2349
2250
|
registerCapability,
|
|
2350
|
-
capability: getMockCapability,
|
|
2351
|
-
provider: getMockProvider,
|
|
2352
2251
|
isInstalled: () => mockState.installed
|
|
2353
2252
|
};
|
|
2354
2253
|
|
|
2355
2254
|
var mock = {CutosMock: CutosMock$1, installCutosMock: installCutosMock$1, uninstallCutosMock: uninstallCutosMock$1};
|
|
2356
2255
|
|
|
2357
|
-
const {
|
|
2256
|
+
const {
|
|
2257
|
+
CoreAPI,
|
|
2258
|
+
CoreDefine,
|
|
2259
|
+
CoreClass,
|
|
2260
|
+
cutosAPI,
|
|
2261
|
+
CutosError,
|
|
2262
|
+
DeviceCapabilityBase,
|
|
2263
|
+
DeviceProviderBase
|
|
2264
|
+
} = cutos;
|
|
2358
2265
|
const {CoreSimulator} = coreSimulator;
|
|
2359
2266
|
const {CutosMock, installCutosMock, uninstallCutosMock} = mock;
|
|
2360
2267
|
|
|
@@ -2376,11 +2283,12 @@ var CoreAPI_1 = src.CoreAPI = CoreAPI;
|
|
|
2376
2283
|
var CoreDefine_1 = src.CoreDefine = CoreDefine;
|
|
2377
2284
|
var CoreClass_1 = src.CoreClass = CoreClass;
|
|
2378
2285
|
var cutosAPI_1 = src.cutosAPI = cutosAPI;
|
|
2379
|
-
var cutos_1 = src.cutos = cutos;
|
|
2380
2286
|
var CutosError_1 = src.CutosError = CutosError;
|
|
2287
|
+
var DeviceCapabilityBase_1 = src.DeviceCapabilityBase = DeviceCapabilityBase;
|
|
2288
|
+
var DeviceProviderBase_1 = src.DeviceProviderBase = DeviceProviderBase;
|
|
2381
2289
|
var CoreSimulator_1 = src.CoreSimulator = CoreSimulator;
|
|
2382
2290
|
var CutosMock_1 = src.CutosMock = CutosMock;
|
|
2383
2291
|
var installCutosMock_1 = src.installCutosMock = installCutosMock;
|
|
2384
2292
|
var uninstallCutosMock_1 = src.uninstallCutosMock = uninstallCutosMock;
|
|
2385
2293
|
|
|
2386
|
-
export { CoreAPI_1 as CoreAPI, CoreClass_1 as CoreClass, CoreDefine_1 as CoreDefine, CoreSimulator_1 as CoreSimulator, CutosError_1 as CutosError, CutosMock_1 as CutosMock,
|
|
2294
|
+
export { CoreAPI_1 as CoreAPI, CoreClass_1 as CoreClass, CoreDefine_1 as CoreDefine, CoreSimulator_1 as CoreSimulator, CutosError_1 as CutosError, CutosMock_1 as CutosMock, DeviceCapabilityBase_1 as DeviceCapabilityBase, DeviceProviderBase_1 as DeviceProviderBase, cutosAPI_1 as cutosAPI, src as default, installCutosMock_1 as installCutosMock, uninstallCutosMock_1 as uninstallCutosMock };
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cutos/core",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"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. ",
|
|
5
5
|
"main": "dist/cutos.cjs.js",
|
|
6
6
|
"module": "dist/cutos.esm.js",
|
|
7
7
|
"types": "types/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"build:rollup": "rollup -c rollup.config.cjs"
|
|
9
|
+
"build:rollup": "rollup -c rollup.config.cjs",
|
|
10
|
+
"test": "node test/device-provider-base.mjs"
|
|
10
11
|
},
|
|
11
12
|
"keywords": [
|
|
12
13
|
"CUTOS", "core", "database", "IPC", "notification", "logging", "device&driver"
|