@cutos/core 3.2.2 → 3.2.4

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.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.2.2";
693
+ var version = "3.2.4";
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";
@@ -814,175 +814,210 @@ function doCallback(id, body, err) {
814
814
  return true;
815
815
  }
816
816
 
817
-
818
- /*
819
- retentionTime: retention time, default 48 hours
820
- */
821
817
  class Database {
822
818
  KEY_TYPE_TEXT = "TEXT";
823
819
  KEY_TYPE_INTEGER = "INTEGER";
824
820
 
825
- constructor(db, callback) {
826
- // sub class
827
- let database = this;
828
- this.Table = class {
829
- constructor(db, name, callback, retentionTime, keyType, keyName) {
830
- this.db = db;
831
- this.name = name;
821
+ /**
822
+ * The constructor of Database
823
+ * @param {string} db - name of database, can be null
824
+ */
825
+ constructor(db) {
826
+ this.db = db;
827
+ }
832
828
 
833
- let body = {
834
- db: this.db, cmd: "db.table.create", name: name
835
- };
836
- if (retentionTime) {
837
- body.retentionTime = retentionTime;
838
- }
839
- if (keyType === 'INTEGER' || keyType === 'TEXT') {
840
- body.keyName = keyName;
841
- body.keyType = keyType;
842
- } else {
843
- throw new Error("keyType MUST be INTEGER or TEXT");
829
+ connect(callback) {
830
+ let body = {
831
+ cmd: "db.connect", db: this.db
832
+ };
833
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
834
+ }
844
835
 
845
- }
846
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
847
- }
836
+ run(sql, callback) {
837
+ let body = {
838
+ db: this.db, cmd: "db.run", sql: sql
839
+ };
840
+ return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
841
+ }
848
842
 
849
- insert(value, callback, tid) {
850
- let body = {
851
- db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: tid
852
- };
853
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
854
- }
843
+ setupMirror(gwIPC, lwaName, confirm = false) {
844
+ this.mirror = {gwIPC, lwaName, confirm};
845
+ }
846
+ }
855
847
 
856
- insertById(id, value, callback, tid) {
857
- let body = {
858
- db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: tid
859
- };
860
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
861
- }
848
+ Database.Table = class {
849
+ constructor(name, database) {
850
+ this.name = name;
851
+ this.database = database;
852
+ this.db = database.db;
862
853
 
863
- insertByKey(key, value, callback, tid) {
864
- let body = {
865
- db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: tid
866
- };
867
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
868
- }
854
+ if (!this.name || !this.database) {
855
+ throw new Error("name or database cannot be empty.");
856
+ }
857
+ }
869
858
 
870
- update(id, value, callback) {
871
- let body = {
872
- db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
873
- };
874
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
875
- }
859
+ create(opts, callback) {
860
+ if (typeof opts === "function") {
861
+ callback = opts;
862
+ opts = {};
863
+ }
864
+ if (!opts) {
865
+ opts = {};
866
+ }
867
+ opts.keyName = opts.keyName ? opts.keyName : 'id';
868
+ opts.keyType = opts.keyType ? opts.keyType : 'INTEGER';
869
+ if (opts.keyType !== 'INTEGER' && opts.keyType !== 'TEXT') {
870
+ throw new Error("keyType MUST be INTEGER or TEXT");
871
+ }
872
+ this.opts = opts;
876
873
 
877
- updateByKey(key, value, callback) {
878
- let body = {
879
- db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
880
- };
881
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
882
- }
874
+ let body = {
875
+ db: this.db, cmd: "db.table.create", name: this.name,
876
+ retentionTime: opts.retentionTime,
877
+ keyType: opts.keyType,
878
+ keyName: opts.keyName
879
+ };
883
880
 
884
- delete(id, callback) {
885
- let body = {
886
- db: this.db, cmd: "db.table.delete", name: this.name, id: id
887
- };
888
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
889
- }
881
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
882
+ }
890
883
 
891
- query(id, callback, count = 100) {
892
- let body = {
893
- db: this.db, cmd: "db.table.query", name: this.name, id: id
894
- };
895
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
896
- }
884
+ insert(value, opts, callback) {
885
+ if (typeof opts === "function") {
886
+ callback = opts;
887
+ opts = {};
888
+ }
897
889
 
898
- queryByKey(key, callback, count = 100) {
899
- let body = {
900
- db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
901
- };
902
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
903
- }
890
+ let body = {
891
+ db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: opts?.tid
892
+ };
893
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
894
+ }
904
895
 
905
- queryByTid(tid, callback, limit = 100) {
906
- let body = {
907
- db: this.db, cmd: "db.table.queryByTid", name: this.name, limit: limit, tid: tid
908
- };
909
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
910
- }
896
+ insertById(id, value, opts, callback) {
897
+ if (typeof opts === "function") {
898
+ callback = opts;
899
+ opts = {};
900
+ }
911
901
 
912
- queryUnsynced(callback, limit = 100) {
913
- let body = {
914
- db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: limit
915
- };
916
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
917
- }
902
+ let body = {
903
+ db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: opts?.tid
904
+ };
905
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
906
+ }
918
907
 
919
- queryAll(callback) {
920
- let body = {
921
- db: this.db, cmd: "db.table.queryAll", name: this.name
922
- };
923
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
924
- }
908
+ insertByKey(key, value, opts, callback) {
909
+ if (typeof opts === "function") {
910
+ callback = opts;
911
+ opts = {};
912
+ }
925
913
 
926
- sync(id, callback) {
927
- let body = {
928
- db: this.db, cmd: "db.table.sync", name: this.name, id: id
929
- };
930
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
931
- }
914
+ let body = {
915
+ db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: opts?.tid
916
+ };
917
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
918
+ }
932
919
 
933
- publishRequestMirror(topic, callback, body) {
934
- let mirror = database.mirror;
935
- let cb = callback;
936
- if (mirror && mirror.gwIPC) { // mirror enabled
937
- cb = (dbResult) => {
938
- let confirmCallback = !mirror.confirm ? null : (gwResult) => {
939
- if (gwResult.status === "success") {
940
- this.sync(dbResult.row);
941
- }
942
- };
943
- if (dbResult.status === true) {
944
- switch (body.cmd) {
945
- case "db.table.create":
946
- break;
947
- case "db.table.insert":
948
- body.id = dbResult.msg.row;
949
- body.timestamp = dbResult.msg.timestamp;
950
- break;
951
- case "db.table.insertById":
952
- case "db.table.insertByKey":
953
- body.timestamp = dbResult.msg.timestamp;
954
- break;
955
- }
956
- mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
957
- }
958
- callback && callback(dbResult);
959
- };
960
- }
961
- return publishRequest(topic, cb, body);
962
- }
920
+ update(id, value, callback) {
921
+ let body = {
922
+ db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
963
923
  };
964
- this.db = db;
924
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
925
+ }
926
+
927
+ updateByKey(key, value, callback) {
965
928
  let body = {
966
- cmd: "db.connect", db: this.db
929
+ db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
967
930
  };
968
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
931
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
969
932
  }
970
933
 
971
- run(sql, callback) {
934
+ delete(id, callback) {
972
935
  let body = {
973
- db: this.db, cmd: "db.run", sql: sql
936
+ db: this.db, cmd: "db.table.delete", name: this.name, id: id
974
937
  };
975
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
938
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
976
939
  }
977
940
 
978
- createTable(name, callback, retentionTime, keyType = 'INTEGER', keyName = 'id') {
979
- return new this.Table(null, name, callback, retentionTime, keyType, keyName);
941
+ query(id, callback) {
942
+ let body = {
943
+ db: this.db, cmd: "db.table.query", name: this.name, id: id
944
+ };
945
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
980
946
  }
981
947
 
982
- setupMirror(gwIPC, lwaName, confirm = false) {
983
- this.mirror = {gwIPC, lwaName, confirm};
948
+ queryByKey(key, callback) {
949
+ let body = {
950
+ db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
951
+ };
952
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
953
+ }
954
+
955
+ queryByTid(tid, callback) {
956
+ let body = {
957
+ db: this.db, cmd: "db.table.queryByTid", name: this.name, tid: tid
958
+ };
959
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
960
+ }
961
+
962
+ queryUnsynced(opts, callback) {
963
+ if (typeof opts === "function") {
964
+ callback = opts;
965
+ opts = {limit: 100};
966
+ }
967
+ if (!opts) opts = {};
968
+ if (opts.limit <= 0) opts.limit = 100;
969
+
970
+ let body = {
971
+ db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: opts.limit
972
+ };
973
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
984
974
  }
985
- }
975
+
976
+ queryAll(callback) {
977
+ let body = {
978
+ db: this.db, cmd: "db.table.queryAll", name: this.name
979
+ };
980
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
981
+ }
982
+
983
+ sync(id, callback) {
984
+ let body = {
985
+ db: this.db, cmd: "db.table.sync", name: this.name, id: id
986
+ };
987
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
988
+ }
989
+
990
+ publishRequestMirror(topic, callback, body) {
991
+ let mirror = this.database.mirror;
992
+ let cb = callback;
993
+ if (mirror && mirror.gwIPC) { // mirror enabled
994
+ cb = (dbResult) => {
995
+ let confirmCallback = !mirror.confirm ? null : (gwResult) => {
996
+ if (gwResult.status === "success") {
997
+ this.sync(dbResult.row);
998
+ }
999
+ };
1000
+ if (dbResult.status === true) {
1001
+ switch (body.cmd) {
1002
+ case "db.table.create":
1003
+ break;
1004
+ case "db.table.insert":
1005
+ body.id = dbResult.msg.row;
1006
+ body.timestamp = dbResult.msg.timestamp;
1007
+ break;
1008
+ case "db.table.insertById":
1009
+ case "db.table.insertByKey":
1010
+ body.timestamp = dbResult.msg.timestamp;
1011
+ break;
1012
+ }
1013
+ mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
1014
+ }
1015
+ callback && callback(dbResult);
1016
+ };
1017
+ }
1018
+ return publishRequest(topic, cb, body);
1019
+ }
1020
+ };
986
1021
 
987
1022
  class Logger {
988
1023
  constructor() {
@@ -1099,10 +1134,8 @@ class Device {
1099
1134
  *
1100
1135
  * @param name
1101
1136
  * @param type
1102
- * @param callback(result, error)
1103
- * @param opts
1104
1137
  */
1105
- constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT, callback, opts) {
1138
+ constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT) {
1106
1139
  this.name = name;
1107
1140
  this.type = type;
1108
1141
  this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
@@ -1129,26 +1162,35 @@ class Device {
1129
1162
  };
1130
1163
  listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
1131
1164
  }
1132
- // subscribe callback
1165
+ // subscribe command callback
1133
1166
  client.subscribe(this.topicCommand + '-callback');
1134
- this.init(name, type, callback, opts);
1135
1167
  }
1136
1168
 
1137
- init(name, type, callback, opts) {
1169
+ /**
1170
+ *
1171
+ * @param callback(result, error)
1172
+ * @param opts
1173
+ */
1174
+ init(opts, callback) {
1175
+ if (typeof opts === "function") {
1176
+ callback = opts;
1177
+ opts = null;
1178
+ }
1179
+
1138
1180
  let command = {};
1139
1181
  command.cmd = "init";
1140
- command.args = {name, type, opts};
1182
+ command.args = {name: this.name, type: this.type, opts};
1141
1183
  command.topicResponse = this.topicResponse;
1142
1184
  let defaultTopic = cutosAPI$1.DEVICE.CHANNEL + '-' + cutosAPI$1.DEVICE.DEFAULT + '-command';
1143
1185
  publishRequest(defaultTopic, null, command);
1144
1186
 
1145
- // response callback
1187
+ // subscribe response callback, currently only used for init
1146
1188
  client.subscribe(this.topicResponse); // receive response from device
1147
- listenerMap.set(this.topicResponse, ({status, msg}) => {
1148
- if (status) {
1189
+ listenerMap.set(this.topicResponse, (result) => {
1190
+ if (result.status) {
1149
1191
  callback && callback(`Driver ${this.type} loaded`);
1150
1192
  } else {
1151
- callback && callback(null, msg);
1193
+ callback && callback(null, result.msg);
1152
1194
  }
1153
1195
  });
1154
1196
  }
@@ -1163,8 +1205,8 @@ class Device {
1163
1205
 
1164
1206
  /**
1165
1207
  *
1166
- * @param command
1167
- * @param callback(result)
1208
+ * @param command - command description object {cmd: 'command', args: (''|{}) }
1209
+ * @param callback - (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
1168
1210
  */
1169
1211
  sendCommand(command, callback) {
1170
1212
  // exam: command = {cmd: "connect", args: ""}
@@ -1186,10 +1228,13 @@ class Device {
1186
1228
  return this.data;
1187
1229
  }
1188
1230
 
1189
- readDeviceInfo(callback, context) {
1231
+ /**
1232
+ *
1233
+ * @param callback (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
1234
+ */
1235
+ readDeviceInfo(callback) {
1190
1236
  let msg = {};
1191
1237
  msg.cmd = "read-device-info";
1192
- msg.context = context;
1193
1238
  this.sendCommand(msg, callback);
1194
1239
  }
1195
1240
  }
@@ -1328,6 +1373,7 @@ const CoreAPI$2 = {
1328
1373
  console.error("CUTOS CORE error:", err.message);
1329
1374
  });
1330
1375
  },
1376
+
1331
1377
  connected() {
1332
1378
  return !!client && client.connected;
1333
1379
  },
@@ -1382,10 +1428,6 @@ const CoreAPI$2 = {
1382
1428
  return publishRequest(cutosAPI$1.CORE.SHELL, callback, command);
1383
1429
  },
1384
1430
 
1385
- getDatabase(db, callback) {
1386
- return new Database(db, callback);
1387
- },
1388
-
1389
1431
  getVolume(callback) {
1390
1432
  return publishRequest(cutosAPI$1.CORE.GET_VOLUME, callback);
1391
1433
  },
@@ -1404,7 +1446,7 @@ cutos.CoreDefine = {
1404
1446
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
1405
1447
  };
1406
1448
  cutos.CoreClass = {
1407
- Device, Driver
1449
+ Device, Driver, Database
1408
1450
  };
1409
1451
 
1410
1452
  cutos.cutosAPI = cutosAPI$1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "3.2.2",
3
+ "version": "3.2.4",
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",