@cutos/core 3.0.2 → 3.2.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/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.2";
401
+ var version = "3.2.0";
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";
@@ -449,7 +449,7 @@ const mqtt = require$$1;
449
449
  const cutosAPI$1 = cutosApi;
450
450
  const callbackMap = new Map();
451
451
  const callbackInterval = 500; // 500ms
452
- const callbackTimeout = 5000; // 5s超时
452
+ const callbackTimeout = 5000; // 5s
453
453
  const listenerMap = new Map();
454
454
  const packageJson = require$$3;
455
455
 
@@ -458,6 +458,8 @@ const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|
458
458
  let client = null;
459
459
  let callbackTimer = null;
460
460
  let coreApiInitialized = false;
461
+ let _logger = null;
462
+ let _notification = null;
461
463
 
462
464
  function publishRequest(topic, callback, body, destAddress) {
463
465
  let request = {};
@@ -493,27 +495,29 @@ function saveCallback(id, item) {
493
495
  return;
494
496
  }
495
497
 
496
- // 处理超时callback
498
+ // process timeout callback
497
499
  callbackTimer = setInterval(() => {
498
500
  let currentTimestamp = new Date().getTime();
499
501
  callbackMap.forEach((callbackItem, id) => {
500
502
  if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
501
- callbackItem.callback({
502
- status: false,
503
- msg: "callback is timeout!"
504
- });
503
+ callbackItem.callback(null, new CutosError('CUTOS CORE error', 'callback is timeout'));
505
504
  callbackMap.delete(id);
506
505
  }
507
506
  });
508
507
  }, callbackInterval);
509
508
  }
510
509
 
511
- function doCallback(id, body) {
510
+ function doCallback(id, body, err) {
512
511
  if (!callbackMap.has(id)) {
513
512
  return false;
514
513
  }
515
514
 
516
- callbackMap.get(id).callback(body);
515
+ if (err) {
516
+ callbackMap.get(id).callback(null, new CutosError('CUTOS CORE error', err));
517
+ } else {
518
+ callbackMap.get(id).callback(body);
519
+ }
520
+
517
521
  callbackMap.delete(id);
518
522
  return true;
519
523
  }
@@ -933,15 +937,33 @@ class Driver {
933
937
  }
934
938
  }
935
939
 
940
+ class CutosError extends Error {
941
+ constructor(name, message) {
942
+ super(message);
943
+ this.name = name;
944
+ }
945
+ }
946
+
936
947
  const CoreAPI$2 = {
937
- init(host = 'ws://localhost:1883', callback) {
938
- if (!host) {
939
- host = 'ws://localhost:1883';
948
+ init(host = 'localhost', callback) {
949
+ let brokerUrl;
950
+ if (host && host.startsWith('ws://') && host.endsWith(':1883')) {
951
+ brokerUrl = host;
952
+ } else {
953
+ if (!host) {
954
+ host = 'localhost';
955
+ }
956
+ if (host !== 'localhost' && !host.match(ipformat)) {
957
+ throw new Error("invalid host address");
958
+ }
959
+
960
+ brokerUrl = `ws://${host}:1883`;
940
961
  }
962
+
941
963
  if (callback && typeof (callback) !== 'function') {
942
964
  throw new Error("callback must be a function");
943
965
  }
944
- client = mqtt.connect(host);
966
+ client = mqtt.connect(brokerUrl);
945
967
 
946
968
  client.on('connect', function () {
947
969
  console.log("CUTOS CORE connected.");
@@ -955,7 +977,7 @@ const CoreAPI$2 = {
955
977
  client.subscribe(cutosAPI$1.CORE_APP[api]);
956
978
  }
957
979
 
958
- !coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
980
+ !coreApiInitialized && callback && callback('CUTOS CORE connected.');
959
981
  coreApiInitialized = true;
960
982
  });
961
983
 
@@ -975,7 +997,7 @@ const CoreAPI$2 = {
975
997
  listener(_message);
976
998
  }
977
999
  } else { // cutos core api callback
978
- doCallback(_message.id, _message.result);
1000
+ doCallback(_message.id, _message.result, _message.err);
979
1001
  }
980
1002
  });
981
1003
 
@@ -984,11 +1006,11 @@ const CoreAPI$2 = {
984
1006
  });
985
1007
 
986
1008
  client.on("error", (err) => {
987
- callback && callback({status: false, msg: err.message});
1009
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
988
1010
  console.error("CUTOS CORE error:", err.message);
989
1011
  });
990
1012
  client.stream.on("error", (err) => {
991
- callback && callback({status: false, msg: err.message});
1013
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
992
1014
  console.error("CUTOS CORE error:", err.message);
993
1015
  });
994
1016
  },
@@ -1029,11 +1051,17 @@ const CoreAPI$2 = {
1029
1051
  },
1030
1052
 
1031
1053
  getNotification() {
1032
- return new Notification();
1054
+ if (!_notification) {
1055
+ _notification = new Notification();
1056
+ }
1057
+ return _notification;
1033
1058
  },
1034
1059
 
1035
1060
  getLogger() {
1036
- return new Logger();
1061
+ if (!_logger) {
1062
+ _logger = new Logger();
1063
+ }
1064
+ return _logger;
1037
1065
  },
1038
1066
 
1039
1067
  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.2";
693
+ var version = "3.2.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";
@@ -741,7 +741,7 @@ const mqtt = require$$1;
741
741
  const cutosAPI$1 = cutosApi;
742
742
  const callbackMap = new Map();
743
743
  const callbackInterval = 500; // 500ms
744
- const callbackTimeout = 5000; // 5s超时
744
+ const callbackTimeout = 5000; // 5s
745
745
  const listenerMap = new Map();
746
746
  const packageJson = require$$3;
747
747
 
@@ -750,6 +750,8 @@ const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|
750
750
  let client = null;
751
751
  let callbackTimer = null;
752
752
  let coreApiInitialized = false;
753
+ let _logger = null;
754
+ let _notification = null;
753
755
 
754
756
  function publishRequest(topic, callback, body, destAddress) {
755
757
  let request = {};
@@ -785,27 +787,29 @@ function saveCallback(id, item) {
785
787
  return;
786
788
  }
787
789
 
788
- // 处理超时callback
790
+ // process timeout callback
789
791
  callbackTimer = setInterval(() => {
790
792
  let currentTimestamp = new Date().getTime();
791
793
  callbackMap.forEach((callbackItem, id) => {
792
794
  if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
793
- callbackItem.callback({
794
- status: false,
795
- msg: "callback is timeout!"
796
- });
795
+ callbackItem.callback(null, new CutosError('CUTOS CORE error', 'callback is timeout'));
797
796
  callbackMap.delete(id);
798
797
  }
799
798
  });
800
799
  }, callbackInterval);
801
800
  }
802
801
 
803
- function doCallback(id, body) {
802
+ function doCallback(id, body, err) {
804
803
  if (!callbackMap.has(id)) {
805
804
  return false;
806
805
  }
807
806
 
808
- callbackMap.get(id).callback(body);
807
+ if (err) {
808
+ callbackMap.get(id).callback(null, new CutosError('CUTOS CORE error', err));
809
+ } else {
810
+ callbackMap.get(id).callback(body);
811
+ }
812
+
809
813
  callbackMap.delete(id);
810
814
  return true;
811
815
  }
@@ -1225,15 +1229,33 @@ class Driver {
1225
1229
  }
1226
1230
  }
1227
1231
 
1232
+ class CutosError extends Error {
1233
+ constructor(name, message) {
1234
+ super(message);
1235
+ this.name = name;
1236
+ }
1237
+ }
1238
+
1228
1239
  const CoreAPI$2 = {
1229
- init(host = 'ws://localhost:1883', callback) {
1230
- if (!host) {
1231
- host = 'ws://localhost:1883';
1240
+ init(host = 'localhost', callback) {
1241
+ let brokerUrl;
1242
+ if (host && host.startsWith('ws://') && host.endsWith(':1883')) {
1243
+ brokerUrl = host;
1244
+ } else {
1245
+ if (!host) {
1246
+ host = 'localhost';
1247
+ }
1248
+ if (host !== 'localhost' && !host.match(ipformat)) {
1249
+ throw new Error("invalid host address");
1250
+ }
1251
+
1252
+ brokerUrl = `ws://${host}:1883`;
1232
1253
  }
1254
+
1233
1255
  if (callback && typeof (callback) !== 'function') {
1234
1256
  throw new Error("callback must be a function");
1235
1257
  }
1236
- client = mqtt.connect(host);
1258
+ client = mqtt.connect(brokerUrl);
1237
1259
 
1238
1260
  client.on('connect', function () {
1239
1261
  console.log("CUTOS CORE connected.");
@@ -1247,7 +1269,7 @@ const CoreAPI$2 = {
1247
1269
  client.subscribe(cutosAPI$1.CORE_APP[api]);
1248
1270
  }
1249
1271
 
1250
- !coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
1272
+ !coreApiInitialized && callback && callback('CUTOS CORE connected.');
1251
1273
  coreApiInitialized = true;
1252
1274
  });
1253
1275
 
@@ -1267,7 +1289,7 @@ const CoreAPI$2 = {
1267
1289
  listener(_message);
1268
1290
  }
1269
1291
  } else { // cutos core api callback
1270
- doCallback(_message.id, _message.result);
1292
+ doCallback(_message.id, _message.result, _message.err);
1271
1293
  }
1272
1294
  });
1273
1295
 
@@ -1276,11 +1298,11 @@ const CoreAPI$2 = {
1276
1298
  });
1277
1299
 
1278
1300
  client.on("error", (err) => {
1279
- callback && callback({status: false, msg: err.message});
1301
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1280
1302
  console.error("CUTOS CORE error:", err.message);
1281
1303
  });
1282
1304
  client.stream.on("error", (err) => {
1283
- callback && callback({status: false, msg: err.message});
1305
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1284
1306
  console.error("CUTOS CORE error:", err.message);
1285
1307
  });
1286
1308
  },
@@ -1321,11 +1343,17 @@ const CoreAPI$2 = {
1321
1343
  },
1322
1344
 
1323
1345
  getNotification() {
1324
- return new Notification();
1346
+ if (!_notification) {
1347
+ _notification = new Notification();
1348
+ }
1349
+ return _notification;
1325
1350
  },
1326
1351
 
1327
1352
  getLogger() {
1328
- return new Logger();
1353
+ if (!_logger) {
1354
+ _logger = new Logger();
1355
+ }
1356
+ return _logger;
1329
1357
  },
1330
1358
 
1331
1359
  shell(command, callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "3.0.2",
3
+ "version": "3.2.0",
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",