@cutos/core 3.0.1 → 3.0.3

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 CHANGED
@@ -85,7 +85,7 @@ CoreAPI.getVersion();
85
85
  - Return result example:
86
86
 
87
87
  ```
88
- 3.0.0
88
+ 3.0.3
89
89
  ```
90
90
 
91
91
  ### CoreAPI.getPlatform
@@ -175,10 +175,9 @@ CoreAPI.getBoxInfo(result => {
175
175
  ```json
176
176
  {
177
177
  "id": 1004,
178
- "macAddress": "54-48-10-D9-BB-27_D8-9C-67-6D-DA-B3_D8-9C-67-6D-DA-B4",
178
+ "macAddress": "54-48-10-D9-BB-27",
179
179
  "ipAddress": "192.168.1.136",
180
- "masterIpAddress": "",
181
- "status": 1
180
+ "masterIpAddress": ""
182
181
  }
183
182
  ```
184
183
  -Return results:
@@ -205,7 +204,7 @@ CoreAPI.getVolume(result => {
205
204
  - Return result example:
206
205
 
207
206
  ```js
208
- 10
207
+ 10
209
208
  ```
210
209
 
211
210
  ### CoreAPI.setVolume
@@ -220,7 +219,7 @@ CoreAPI.setVolume(value,callback)
220
219
  ##### Example:
221
220
 
222
221
  ```js
223
- CoreAPI.setVolume(70,result => {
222
+ CoreAPI.setVolume(70, result => {
224
223
  console.log(result)
225
224
  })
226
225
  ```
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.1";
401
+ var version = "3.0.3";
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 = 5000; // 5s超时
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,9 @@ 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;
461
+ let _logger = null;
462
+ let _notification = null;
459
463
 
460
464
  function publishRequest(topic, callback, body, destAddress) {
461
465
  let request = {};
@@ -466,7 +470,7 @@ function publishRequest(topic, callback, body, destAddress) {
466
470
  if (typeof callback === "function") {
467
471
  let callbackItem = {};
468
472
  callbackItem.callback = callback;
469
- callbackItem.timeStamp = Date.now();
473
+ callbackItem.timestamp = Date.now();
470
474
  saveCallback(request.context.id, callbackItem);
471
475
  }
472
476
  client.publish(topic, JSON.stringify(request));
@@ -491,17 +495,14 @@ function saveCallback(id, item) {
491
495
  return;
492
496
  }
493
497
 
494
- // 处理超时callback
498
+ // process timeout callback
495
499
  callbackTimer = setInterval(() => {
496
500
  let currentTimestamp = new Date().getTime();
497
501
  callbackMap.forEach((callbackItem, id) => {
498
- if (currentTimestamp - callbackItem.timestamp > callbackInterval) {
502
+ if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
499
503
  callbackItem.callback({
500
- context: {id: id},
501
- response: {
502
- result: "failure",
503
- reason: "callback is timeout!"
504
- }
504
+ status: false,
505
+ msg: "callback is timeout!"
505
506
  });
506
507
  callbackMap.delete(id);
507
508
  }
@@ -859,7 +860,7 @@ class Device {
859
860
  }
860
861
 
861
862
  onData(listener) {
862
- if(!listenerMap.has(this.topicData)) {
863
+ if (!listenerMap.has(this.topicData)) {
863
864
  client.subscribe(this.topicData); // receive data from device
864
865
  }
865
866
  let _this = this;
@@ -935,11 +936,25 @@ class Driver {
935
936
  }
936
937
 
937
938
  const CoreAPI$2 = {
938
- init(host = 'ws://localhost:1883', callback) {
939
- if (!host) {
940
- host = 'ws://localhost:1883';
939
+ init(host = 'localhost', callback) {
940
+ let brokerUrl;
941
+ if (host && host.startsWith('ws://') && host.endsWith(':1883')) {
942
+ brokerUrl = host;
943
+ } else {
944
+ if (!host) {
945
+ host = 'localhost';
946
+ }
947
+ if (host !== 'localhost' && !host.match(ipformat)) {
948
+ throw new Error("invalid host address");
949
+ }
950
+
951
+ brokerUrl = `ws://${host}:1883`;
952
+ }
953
+
954
+ if (callback && typeof (callback) !== 'function') {
955
+ throw new Error("callback must be a function");
941
956
  }
942
- client = mqtt.connect(host);
957
+ client = mqtt.connect(brokerUrl);
943
958
 
944
959
  client.on('connect', function () {
945
960
  console.log("CUTOS CORE connected.");
@@ -952,12 +967,9 @@ const CoreAPI$2 = {
952
967
  for (let api in cutosAPI$1.CORE_APP) {
953
968
  client.subscribe(cutosAPI$1.CORE_APP[api]);
954
969
  }
955
- if (typeof (callback) === 'function') {
956
- callback({
957
- status: true,
958
- msg: 'CUTOS CORE connected.'
959
- });
960
- }
970
+
971
+ !coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
972
+ coreApiInitialized = true;
961
973
  });
962
974
 
963
975
  client.on('message', function (topic, message, context) {
@@ -984,14 +996,13 @@ const CoreAPI$2 = {
984
996
  console.log("CUTOS CORE closed.");
985
997
  });
986
998
 
987
- client.on("error", (e) => {
988
- if (typeof (callback) === 'function') {
989
- callback({
990
- status: false,
991
- msg: e.message
992
- });
993
- }
994
- console.error("CUTOS CORE error: ", e.message);
999
+ client.on("error", (err) => {
1000
+ callback && callback({status: false, msg: err.message});
1001
+ console.error("CUTOS CORE error:", err.message);
1002
+ });
1003
+ client.stream.on("error", (err) => {
1004
+ callback && callback({status: false, msg: err.message});
1005
+ console.error("CUTOS CORE error:", err.message);
995
1006
  });
996
1007
  },
997
1008
  connected() {
@@ -1031,11 +1042,17 @@ const CoreAPI$2 = {
1031
1042
  },
1032
1043
 
1033
1044
  getNotification() {
1034
- return new Notification();
1045
+ if (!_notification) {
1046
+ _notification = new Notification();
1047
+ }
1048
+ return _notification;
1035
1049
  },
1036
1050
 
1037
1051
  getLogger() {
1038
- return new Logger();
1052
+ if (!_logger) {
1053
+ _logger = new Logger();
1054
+ }
1055
+ return _logger;
1039
1056
  },
1040
1057
 
1041
1058
  shell(command, callback) {
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.1";
693
+ var version = "3.0.3";
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 = 5000; // 5s超时
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,9 @@ 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;
753
+ let _logger = null;
754
+ let _notification = null;
751
755
 
752
756
  function publishRequest(topic, callback, body, destAddress) {
753
757
  let request = {};
@@ -758,7 +762,7 @@ function publishRequest(topic, callback, body, destAddress) {
758
762
  if (typeof callback === "function") {
759
763
  let callbackItem = {};
760
764
  callbackItem.callback = callback;
761
- callbackItem.timeStamp = Date.now();
765
+ callbackItem.timestamp = Date.now();
762
766
  saveCallback(request.context.id, callbackItem);
763
767
  }
764
768
  client.publish(topic, JSON.stringify(request));
@@ -783,17 +787,14 @@ function saveCallback(id, item) {
783
787
  return;
784
788
  }
785
789
 
786
- // 处理超时callback
790
+ // process timeout callback
787
791
  callbackTimer = setInterval(() => {
788
792
  let currentTimestamp = new Date().getTime();
789
793
  callbackMap.forEach((callbackItem, id) => {
790
- if (currentTimestamp - callbackItem.timestamp > callbackInterval) {
794
+ if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
791
795
  callbackItem.callback({
792
- context: {id: id},
793
- response: {
794
- result: "failure",
795
- reason: "callback is timeout!"
796
- }
796
+ status: false,
797
+ msg: "callback is timeout!"
797
798
  });
798
799
  callbackMap.delete(id);
799
800
  }
@@ -1151,7 +1152,7 @@ class Device {
1151
1152
  }
1152
1153
 
1153
1154
  onData(listener) {
1154
- if(!listenerMap.has(this.topicData)) {
1155
+ if (!listenerMap.has(this.topicData)) {
1155
1156
  client.subscribe(this.topicData); // receive data from device
1156
1157
  }
1157
1158
  let _this = this;
@@ -1227,11 +1228,25 @@ class Driver {
1227
1228
  }
1228
1229
 
1229
1230
  const CoreAPI$2 = {
1230
- init(host = 'ws://localhost:1883', callback) {
1231
- if (!host) {
1232
- host = 'ws://localhost:1883';
1231
+ init(host = 'localhost', callback) {
1232
+ let brokerUrl;
1233
+ if (host && host.startsWith('ws://') && host.endsWith(':1883')) {
1234
+ brokerUrl = host;
1235
+ } else {
1236
+ if (!host) {
1237
+ host = 'localhost';
1238
+ }
1239
+ if (host !== 'localhost' && !host.match(ipformat)) {
1240
+ throw new Error("invalid host address");
1241
+ }
1242
+
1243
+ brokerUrl = `ws://${host}:1883`;
1244
+ }
1245
+
1246
+ if (callback && typeof (callback) !== 'function') {
1247
+ throw new Error("callback must be a function");
1233
1248
  }
1234
- client = mqtt.connect(host);
1249
+ client = mqtt.connect(brokerUrl);
1235
1250
 
1236
1251
  client.on('connect', function () {
1237
1252
  console.log("CUTOS CORE connected.");
@@ -1244,12 +1259,9 @@ const CoreAPI$2 = {
1244
1259
  for (let api in cutosAPI$1.CORE_APP) {
1245
1260
  client.subscribe(cutosAPI$1.CORE_APP[api]);
1246
1261
  }
1247
- if (typeof (callback) === 'function') {
1248
- callback({
1249
- status: true,
1250
- msg: 'CUTOS CORE connected.'
1251
- });
1252
- }
1262
+
1263
+ !coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
1264
+ coreApiInitialized = true;
1253
1265
  });
1254
1266
 
1255
1267
  client.on('message', function (topic, message, context) {
@@ -1276,14 +1288,13 @@ const CoreAPI$2 = {
1276
1288
  console.log("CUTOS CORE closed.");
1277
1289
  });
1278
1290
 
1279
- client.on("error", (e) => {
1280
- if (typeof (callback) === 'function') {
1281
- callback({
1282
- status: false,
1283
- msg: e.message
1284
- });
1285
- }
1286
- console.error("CUTOS CORE error: ", e.message);
1291
+ client.on("error", (err) => {
1292
+ callback && callback({status: false, msg: err.message});
1293
+ console.error("CUTOS CORE error:", err.message);
1294
+ });
1295
+ client.stream.on("error", (err) => {
1296
+ callback && callback({status: false, msg: err.message});
1297
+ console.error("CUTOS CORE error:", err.message);
1287
1298
  });
1288
1299
  },
1289
1300
  connected() {
@@ -1323,11 +1334,17 @@ const CoreAPI$2 = {
1323
1334
  },
1324
1335
 
1325
1336
  getNotification() {
1326
- return new Notification();
1337
+ if (!_notification) {
1338
+ _notification = new Notification();
1339
+ }
1340
+ return _notification;
1327
1341
  },
1328
1342
 
1329
1343
  getLogger() {
1330
- return new Logger();
1344
+ if (!_logger) {
1345
+ _logger = new Logger();
1346
+ }
1347
+ return _logger;
1331
1348
  },
1332
1349
 
1333
1350
  shell(command, callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
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",