@cutos/core 3.0.3 → 3.2.1

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
@@ -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.3";
401
+ var version = "3.2.1";
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";
@@ -500,22 +500,24 @@ function saveCallback(id, item) {
500
500
  let currentTimestamp = new Date().getTime();
501
501
  callbackMap.forEach((callbackItem, id) => {
502
502
  if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
503
- callbackItem.callback({
504
- status: false,
505
- msg: "callback is timeout!"
506
- });
503
+ callbackItem.callback(null, new CutosError('CUTOS CORE error', 'callback is timeout'));
507
504
  callbackMap.delete(id);
508
505
  }
509
506
  });
510
507
  }, callbackInterval);
511
508
  }
512
509
 
513
- function doCallback(id, body) {
510
+ function doCallback(id, body, err) {
514
511
  if (!callbackMap.has(id)) {
515
512
  return false;
516
513
  }
517
514
 
518
- 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
+
519
521
  callbackMap.delete(id);
520
522
  return true;
521
523
  }
@@ -651,12 +653,12 @@ class Database {
651
653
  case "db.table.create":
652
654
  break;
653
655
  case "db.table.insert":
654
- body.id = dbResult.row;
655
- body.timestamp = dbResult.timestamp;
656
+ body.id = dbResult.msg.row;
657
+ body.timestamp = dbResult.msg.timestamp;
656
658
  break;
657
659
  case "db.table.insertById":
658
660
  case "db.table.insertByKey":
659
- body.timestamp = dbResult.timestamp;
661
+ body.timestamp = dbResult.msg.timestamp;
660
662
  break;
661
663
  }
662
664
  mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
@@ -801,6 +803,13 @@ class Heartbeat {
801
803
  }
802
804
 
803
805
  class Device {
806
+ /**
807
+ *
808
+ * @param name
809
+ * @param type
810
+ * @param callback(result, error)
811
+ * @param opts
812
+ */
804
813
  constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT, callback, opts) {
805
814
  this.name = name;
806
815
  this.type = type;
@@ -843,7 +852,13 @@ class Device {
843
852
 
844
853
  // response callback
845
854
  client.subscribe(this.topicResponse); // receive response from device
846
- listenerMap.set(this.topicResponse, callback);
855
+ listenerMap.set(this.topicResponse, ({status, msg}) => {
856
+ if (status) {
857
+ callback && callback(`Driver ${this.type} loaded`);
858
+ } else {
859
+ callback && callback(null, msg);
860
+ }
861
+ });
847
862
  }
848
863
 
849
864
  onStatus(listener) {
@@ -854,6 +869,11 @@ class Device {
854
869
  return this.msg;
855
870
  }
856
871
 
872
+ /**
873
+ *
874
+ * @param command
875
+ * @param callback(result)
876
+ */
857
877
  sendCommand(command, callback) {
858
878
  // exam: command = {cmd: "connect", args: ""}
859
879
  publishRequest(this.topicCommand, callback, command);
@@ -863,9 +883,9 @@ class Device {
863
883
  if (!listenerMap.has(this.topicData)) {
864
884
  client.subscribe(this.topicData); // receive data from device
865
885
  }
866
- let _this = this;
867
- listenerMap.set(this.topicData, function (data) {
868
- _this.data = data;
886
+
887
+ listenerMap.set(this.topicData, (data) => {
888
+ this.data = data;
869
889
  listener(data);
870
890
  });
871
891
  }
@@ -917,13 +937,17 @@ class Driver {
917
937
  client.publish(topic, JSON.stringify(data));
918
938
  }
919
939
 
940
+ /**
941
+ *
942
+ * @param listener(command, callback(result))
943
+ */
920
944
  onCommand(listener) {
921
945
  if (listener) {
922
946
  let cmdListener = (message) => {
923
947
  // process command callback
924
948
  // message.context
925
949
  // message.context.id
926
- // message.body
950
+ // message.body
927
951
  listener(message.body, result => {
928
952
  publishResponse(this.topicCommand, message, result);
929
953
  });
@@ -935,6 +959,13 @@ class Driver {
935
959
  }
936
960
  }
937
961
 
962
+ class CutosError {
963
+ constructor(name, message) {
964
+ this.name = name;
965
+ this.message = message;
966
+ }
967
+ }
968
+
938
969
  const CoreAPI$2 = {
939
970
  init(host = 'localhost', callback) {
940
971
  let brokerUrl;
@@ -968,7 +999,7 @@ const CoreAPI$2 = {
968
999
  client.subscribe(cutosAPI$1.CORE_APP[api]);
969
1000
  }
970
1001
 
971
- !coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
1002
+ !coreApiInitialized && callback && callback('CoreAPI init success');
972
1003
  coreApiInitialized = true;
973
1004
  });
974
1005
 
@@ -988,7 +1019,7 @@ const CoreAPI$2 = {
988
1019
  listener(_message);
989
1020
  }
990
1021
  } else { // cutos core api callback
991
- doCallback(_message.id, _message.result);
1022
+ doCallback(_message.id, _message.result, _message.err);
992
1023
  }
993
1024
  });
994
1025
 
@@ -997,11 +1028,11 @@ const CoreAPI$2 = {
997
1028
  });
998
1029
 
999
1030
  client.on("error", (err) => {
1000
- callback && callback({status: false, msg: err.message});
1031
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1001
1032
  console.error("CUTOS CORE error:", err.message);
1002
1033
  });
1003
1034
  client.stream.on("error", (err) => {
1004
- callback && callback({status: false, msg: err.message});
1035
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1005
1036
  console.error("CUTOS CORE error:", err.message);
1006
1037
  });
1007
1038
  },
@@ -1091,21 +1122,26 @@ const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1} = cutos;
1091
1122
 
1092
1123
  const CoreSimulator$1 = {
1093
1124
  start: function (driver, port = 1883) {
1094
- CoreAPI$1.init(`ws://localhost:${port}`, result => {
1125
+ CoreAPI$1.init(`ws://localhost:${port}`, (result, error) => {
1126
+ if (error) {
1127
+ console.log('CUTOS Simulator error', error.message);
1128
+ return;
1129
+ }
1130
+
1095
1131
  console.log('CUTOS Simulator started and listening on port ', port);
1096
1132
 
1097
1133
  let drvDefault = new CoreClass$1.Driver();
1098
1134
  drvDefault.onCommand((command) => {
1099
1135
  console.log('drvDefault onCommand', JSON.stringify(command));
1100
1136
  if (command.cmd === "init") {
1101
- let rsp = {result: true};
1137
+ let result = {status: true};
1102
1138
  try {
1103
1139
  driver.init(command.args);
1104
1140
  } catch (e) {
1105
- rsp.msg = e.message;
1106
- rsp.result = false;
1141
+ result.msg = e.message;
1142
+ result.status = false;
1107
1143
  }
1108
- drvDefault.sendResponse(rsp, command.topicResponse);
1144
+ drvDefault.sendResponse(result, command.topicResponse);
1109
1145
  }
1110
1146
  });
1111
1147
  });
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.3";
693
+ var version = "3.2.1";
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";
@@ -792,22 +792,24 @@ function saveCallback(id, item) {
792
792
  let currentTimestamp = new Date().getTime();
793
793
  callbackMap.forEach((callbackItem, id) => {
794
794
  if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
795
- callbackItem.callback({
796
- status: false,
797
- msg: "callback is timeout!"
798
- });
795
+ callbackItem.callback(null, new CutosError('CUTOS CORE error', 'callback is timeout'));
799
796
  callbackMap.delete(id);
800
797
  }
801
798
  });
802
799
  }, callbackInterval);
803
800
  }
804
801
 
805
- function doCallback(id, body) {
802
+ function doCallback(id, body, err) {
806
803
  if (!callbackMap.has(id)) {
807
804
  return false;
808
805
  }
809
806
 
810
- 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
+
811
813
  callbackMap.delete(id);
812
814
  return true;
813
815
  }
@@ -943,12 +945,12 @@ class Database {
943
945
  case "db.table.create":
944
946
  break;
945
947
  case "db.table.insert":
946
- body.id = dbResult.row;
947
- body.timestamp = dbResult.timestamp;
948
+ body.id = dbResult.msg.row;
949
+ body.timestamp = dbResult.msg.timestamp;
948
950
  break;
949
951
  case "db.table.insertById":
950
952
  case "db.table.insertByKey":
951
- body.timestamp = dbResult.timestamp;
953
+ body.timestamp = dbResult.msg.timestamp;
952
954
  break;
953
955
  }
954
956
  mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
@@ -1093,6 +1095,13 @@ class Heartbeat {
1093
1095
  }
1094
1096
 
1095
1097
  class Device {
1098
+ /**
1099
+ *
1100
+ * @param name
1101
+ * @param type
1102
+ * @param callback(result, error)
1103
+ * @param opts
1104
+ */
1096
1105
  constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT, callback, opts) {
1097
1106
  this.name = name;
1098
1107
  this.type = type;
@@ -1135,7 +1144,13 @@ class Device {
1135
1144
 
1136
1145
  // response callback
1137
1146
  client.subscribe(this.topicResponse); // receive response from device
1138
- listenerMap.set(this.topicResponse, callback);
1147
+ listenerMap.set(this.topicResponse, ({status, msg}) => {
1148
+ if (status) {
1149
+ callback && callback(`Driver ${this.type} loaded`);
1150
+ } else {
1151
+ callback && callback(null, msg);
1152
+ }
1153
+ });
1139
1154
  }
1140
1155
 
1141
1156
  onStatus(listener) {
@@ -1146,6 +1161,11 @@ class Device {
1146
1161
  return this.msg;
1147
1162
  }
1148
1163
 
1164
+ /**
1165
+ *
1166
+ * @param command
1167
+ * @param callback(result)
1168
+ */
1149
1169
  sendCommand(command, callback) {
1150
1170
  // exam: command = {cmd: "connect", args: ""}
1151
1171
  publishRequest(this.topicCommand, callback, command);
@@ -1155,9 +1175,9 @@ class Device {
1155
1175
  if (!listenerMap.has(this.topicData)) {
1156
1176
  client.subscribe(this.topicData); // receive data from device
1157
1177
  }
1158
- let _this = this;
1159
- listenerMap.set(this.topicData, function (data) {
1160
- _this.data = data;
1178
+
1179
+ listenerMap.set(this.topicData, (data) => {
1180
+ this.data = data;
1161
1181
  listener(data);
1162
1182
  });
1163
1183
  }
@@ -1209,13 +1229,17 @@ class Driver {
1209
1229
  client.publish(topic, JSON.stringify(data));
1210
1230
  }
1211
1231
 
1232
+ /**
1233
+ *
1234
+ * @param listener(command, callback(result))
1235
+ */
1212
1236
  onCommand(listener) {
1213
1237
  if (listener) {
1214
1238
  let cmdListener = (message) => {
1215
1239
  // process command callback
1216
1240
  // message.context
1217
1241
  // message.context.id
1218
- // message.body
1242
+ // message.body
1219
1243
  listener(message.body, result => {
1220
1244
  publishResponse(this.topicCommand, message, result);
1221
1245
  });
@@ -1227,6 +1251,13 @@ class Driver {
1227
1251
  }
1228
1252
  }
1229
1253
 
1254
+ class CutosError {
1255
+ constructor(name, message) {
1256
+ this.name = name;
1257
+ this.message = message;
1258
+ }
1259
+ }
1260
+
1230
1261
  const CoreAPI$2 = {
1231
1262
  init(host = 'localhost', callback) {
1232
1263
  let brokerUrl;
@@ -1260,7 +1291,7 @@ const CoreAPI$2 = {
1260
1291
  client.subscribe(cutosAPI$1.CORE_APP[api]);
1261
1292
  }
1262
1293
 
1263
- !coreApiInitialized && callback && callback({status: true, msg: 'CUTOS CORE connected.'});
1294
+ !coreApiInitialized && callback && callback('CoreAPI init success');
1264
1295
  coreApiInitialized = true;
1265
1296
  });
1266
1297
 
@@ -1280,7 +1311,7 @@ const CoreAPI$2 = {
1280
1311
  listener(_message);
1281
1312
  }
1282
1313
  } else { // cutos core api callback
1283
- doCallback(_message.id, _message.result);
1314
+ doCallback(_message.id, _message.result, _message.err);
1284
1315
  }
1285
1316
  });
1286
1317
 
@@ -1289,11 +1320,11 @@ const CoreAPI$2 = {
1289
1320
  });
1290
1321
 
1291
1322
  client.on("error", (err) => {
1292
- callback && callback({status: false, msg: err.message});
1323
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1293
1324
  console.error("CUTOS CORE error:", err.message);
1294
1325
  });
1295
1326
  client.stream.on("error", (err) => {
1296
- callback && callback({status: false, msg: err.message});
1327
+ callback && callback(null, new CutosError('CUTOS CORE error', err.message));
1297
1328
  console.error("CUTOS CORE error:", err.message);
1298
1329
  });
1299
1330
  },
@@ -1383,21 +1414,26 @@ const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1} = cutos;
1383
1414
 
1384
1415
  const CoreSimulator$1 = {
1385
1416
  start: function (driver, port = 1883) {
1386
- CoreAPI$1.init(`ws://localhost:${port}`, result => {
1417
+ CoreAPI$1.init(`ws://localhost:${port}`, (result, error) => {
1418
+ if (error) {
1419
+ console.log('CUTOS Simulator error', error.message);
1420
+ return;
1421
+ }
1422
+
1387
1423
  console.log('CUTOS Simulator started and listening on port ', port);
1388
1424
 
1389
1425
  let drvDefault = new CoreClass$1.Driver();
1390
1426
  drvDefault.onCommand((command) => {
1391
1427
  console.log('drvDefault onCommand', JSON.stringify(command));
1392
1428
  if (command.cmd === "init") {
1393
- let rsp = {result: true};
1429
+ let result = {status: true};
1394
1430
  try {
1395
1431
  driver.init(command.args);
1396
1432
  } catch (e) {
1397
- rsp.msg = e.message;
1398
- rsp.result = false;
1433
+ result.msg = e.message;
1434
+ result.status = false;
1399
1435
  }
1400
- drvDefault.sendResponse(rsp, command.topicResponse);
1436
+ drvDefault.sendResponse(result, command.topicResponse);
1401
1437
  }
1402
1438
  });
1403
1439
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "3.0.3",
3
+ "version": "3.2.1",
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",