@cutos/core 3.0.0 → 3.0.2
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 +24 -24
- package/dist/cutos.esm.js +24 -24
- package/package.json +1 -1
package/dist/cutos.cjs.js
CHANGED
|
@@ -398,7 +398,7 @@ const cutosAPI$2 = {
|
|
|
398
398
|
var cutosApi = cutosAPI$2;
|
|
399
399
|
|
|
400
400
|
var name = "@cutos/core";
|
|
401
|
-
var version = "3.0.
|
|
401
|
+
var version = "3.0.2";
|
|
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";
|
|
@@ -448,7 +448,8 @@ const mqtt = require$$1;
|
|
|
448
448
|
|
|
449
449
|
const cutosAPI$1 = cutosApi;
|
|
450
450
|
const callbackMap = new Map();
|
|
451
|
-
const callbackInterval =
|
|
451
|
+
const callbackInterval = 500; // 500ms
|
|
452
|
+
const callbackTimeout = 5000; // 5s超时
|
|
452
453
|
const listenerMap = new Map();
|
|
453
454
|
const packageJson = require$$3;
|
|
454
455
|
|
|
@@ -456,6 +457,7 @@ const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|
|
|
|
456
457
|
|
|
457
458
|
let client = null;
|
|
458
459
|
let callbackTimer = null;
|
|
460
|
+
let coreApiInitialized = false;
|
|
459
461
|
|
|
460
462
|
function publishRequest(topic, callback, body, destAddress) {
|
|
461
463
|
let request = {};
|
|
@@ -466,7 +468,7 @@ function publishRequest(topic, callback, body, destAddress) {
|
|
|
466
468
|
if (typeof callback === "function") {
|
|
467
469
|
let callbackItem = {};
|
|
468
470
|
callbackItem.callback = callback;
|
|
469
|
-
callbackItem.
|
|
471
|
+
callbackItem.timestamp = Date.now();
|
|
470
472
|
saveCallback(request.context.id, callbackItem);
|
|
471
473
|
}
|
|
472
474
|
client.publish(topic, JSON.stringify(request));
|
|
@@ -495,13 +497,10 @@ function saveCallback(id, item) {
|
|
|
495
497
|
callbackTimer = setInterval(() => {
|
|
496
498
|
let currentTimestamp = new Date().getTime();
|
|
497
499
|
callbackMap.forEach((callbackItem, id) => {
|
|
498
|
-
if (currentTimestamp - callbackItem.timestamp >
|
|
500
|
+
if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
|
|
499
501
|
callbackItem.callback({
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
result: "failure",
|
|
503
|
-
reason: "callback is timeout!"
|
|
504
|
-
}
|
|
502
|
+
status: false,
|
|
503
|
+
msg: "callback is timeout!"
|
|
505
504
|
});
|
|
506
505
|
callbackMap.delete(id);
|
|
507
506
|
}
|
|
@@ -859,7 +858,9 @@ class Device {
|
|
|
859
858
|
}
|
|
860
859
|
|
|
861
860
|
onData(listener) {
|
|
862
|
-
|
|
861
|
+
if (!listenerMap.has(this.topicData)) {
|
|
862
|
+
client.subscribe(this.topicData); // receive data from device
|
|
863
|
+
}
|
|
863
864
|
let _this = this;
|
|
864
865
|
listenerMap.set(this.topicData, function (data) {
|
|
865
866
|
_this.data = data;
|
|
@@ -937,6 +938,9 @@ const CoreAPI$2 = {
|
|
|
937
938
|
if (!host) {
|
|
938
939
|
host = 'ws://localhost:1883';
|
|
939
940
|
}
|
|
941
|
+
if (callback && typeof (callback) !== 'function') {
|
|
942
|
+
throw new Error("callback must be a function");
|
|
943
|
+
}
|
|
940
944
|
client = mqtt.connect(host);
|
|
941
945
|
|
|
942
946
|
client.on('connect', function () {
|
|
@@ -950,12 +954,9 @@ const CoreAPI$2 = {
|
|
|
950
954
|
for (let api in cutosAPI$1.CORE_APP) {
|
|
951
955
|
client.subscribe(cutosAPI$1.CORE_APP[api]);
|
|
952
956
|
}
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
msg: 'CUTOS CORE connected.'
|
|
957
|
-
});
|
|
958
|
-
}
|
|
957
|
+
|
|
958
|
+
!coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
|
|
959
|
+
coreApiInitialized = true;
|
|
959
960
|
});
|
|
960
961
|
|
|
961
962
|
client.on('message', function (topic, message, context) {
|
|
@@ -982,14 +983,13 @@ const CoreAPI$2 = {
|
|
|
982
983
|
console.log("CUTOS CORE closed.");
|
|
983
984
|
});
|
|
984
985
|
|
|
985
|
-
client.on("error", (
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
console.error("CUTOS CORE error: ", e.message);
|
|
986
|
+
client.on("error", (err) => {
|
|
987
|
+
callback && callback({status: false, msg: err.message});
|
|
988
|
+
console.error("CUTOS CORE error:", err.message);
|
|
989
|
+
});
|
|
990
|
+
client.stream.on("error", (err) => {
|
|
991
|
+
callback && callback({status: false, msg: err.message});
|
|
992
|
+
console.error("CUTOS CORE error:", err.message);
|
|
993
993
|
});
|
|
994
994
|
},
|
|
995
995
|
connected() {
|
package/dist/cutos.esm.js
CHANGED
|
@@ -690,7 +690,7 @@ const cutosAPI$2 = {
|
|
|
690
690
|
var cutosApi = cutosAPI$2;
|
|
691
691
|
|
|
692
692
|
var name = "@cutos/core";
|
|
693
|
-
var version = "3.0.
|
|
693
|
+
var version = "3.0.2";
|
|
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";
|
|
@@ -740,7 +740,8 @@ const mqtt = require$$1;
|
|
|
740
740
|
|
|
741
741
|
const cutosAPI$1 = cutosApi;
|
|
742
742
|
const callbackMap = new Map();
|
|
743
|
-
const callbackInterval =
|
|
743
|
+
const callbackInterval = 500; // 500ms
|
|
744
|
+
const callbackTimeout = 5000; // 5s超时
|
|
744
745
|
const listenerMap = new Map();
|
|
745
746
|
const packageJson = require$$3;
|
|
746
747
|
|
|
@@ -748,6 +749,7 @@ const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|
|
|
|
748
749
|
|
|
749
750
|
let client = null;
|
|
750
751
|
let callbackTimer = null;
|
|
752
|
+
let coreApiInitialized = false;
|
|
751
753
|
|
|
752
754
|
function publishRequest(topic, callback, body, destAddress) {
|
|
753
755
|
let request = {};
|
|
@@ -758,7 +760,7 @@ function publishRequest(topic, callback, body, destAddress) {
|
|
|
758
760
|
if (typeof callback === "function") {
|
|
759
761
|
let callbackItem = {};
|
|
760
762
|
callbackItem.callback = callback;
|
|
761
|
-
callbackItem.
|
|
763
|
+
callbackItem.timestamp = Date.now();
|
|
762
764
|
saveCallback(request.context.id, callbackItem);
|
|
763
765
|
}
|
|
764
766
|
client.publish(topic, JSON.stringify(request));
|
|
@@ -787,13 +789,10 @@ function saveCallback(id, item) {
|
|
|
787
789
|
callbackTimer = setInterval(() => {
|
|
788
790
|
let currentTimestamp = new Date().getTime();
|
|
789
791
|
callbackMap.forEach((callbackItem, id) => {
|
|
790
|
-
if (currentTimestamp - callbackItem.timestamp >
|
|
792
|
+
if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
|
|
791
793
|
callbackItem.callback({
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
result: "failure",
|
|
795
|
-
reason: "callback is timeout!"
|
|
796
|
-
}
|
|
794
|
+
status: false,
|
|
795
|
+
msg: "callback is timeout!"
|
|
797
796
|
});
|
|
798
797
|
callbackMap.delete(id);
|
|
799
798
|
}
|
|
@@ -1151,7 +1150,9 @@ class Device {
|
|
|
1151
1150
|
}
|
|
1152
1151
|
|
|
1153
1152
|
onData(listener) {
|
|
1154
|
-
|
|
1153
|
+
if (!listenerMap.has(this.topicData)) {
|
|
1154
|
+
client.subscribe(this.topicData); // receive data from device
|
|
1155
|
+
}
|
|
1155
1156
|
let _this = this;
|
|
1156
1157
|
listenerMap.set(this.topicData, function (data) {
|
|
1157
1158
|
_this.data = data;
|
|
@@ -1229,6 +1230,9 @@ const CoreAPI$2 = {
|
|
|
1229
1230
|
if (!host) {
|
|
1230
1231
|
host = 'ws://localhost:1883';
|
|
1231
1232
|
}
|
|
1233
|
+
if (callback && typeof (callback) !== 'function') {
|
|
1234
|
+
throw new Error("callback must be a function");
|
|
1235
|
+
}
|
|
1232
1236
|
client = mqtt.connect(host);
|
|
1233
1237
|
|
|
1234
1238
|
client.on('connect', function () {
|
|
@@ -1242,12 +1246,9 @@ const CoreAPI$2 = {
|
|
|
1242
1246
|
for (let api in cutosAPI$1.CORE_APP) {
|
|
1243
1247
|
client.subscribe(cutosAPI$1.CORE_APP[api]);
|
|
1244
1248
|
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
msg: 'CUTOS CORE connected.'
|
|
1249
|
-
});
|
|
1250
|
-
}
|
|
1249
|
+
|
|
1250
|
+
!coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
|
|
1251
|
+
coreApiInitialized = true;
|
|
1251
1252
|
});
|
|
1252
1253
|
|
|
1253
1254
|
client.on('message', function (topic, message, context) {
|
|
@@ -1274,14 +1275,13 @@ const CoreAPI$2 = {
|
|
|
1274
1275
|
console.log("CUTOS CORE closed.");
|
|
1275
1276
|
});
|
|
1276
1277
|
|
|
1277
|
-
client.on("error", (
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
console.error("CUTOS CORE error: ", e.message);
|
|
1278
|
+
client.on("error", (err) => {
|
|
1279
|
+
callback && callback({status: false, msg: err.message});
|
|
1280
|
+
console.error("CUTOS CORE error:", err.message);
|
|
1281
|
+
});
|
|
1282
|
+
client.stream.on("error", (err) => {
|
|
1283
|
+
callback && callback({status: false, msg: err.message});
|
|
1284
|
+
console.error("CUTOS CORE error:", err.message);
|
|
1285
1285
|
});
|
|
1286
1286
|
},
|
|
1287
1287
|
connected() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cutos/core",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
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",
|