@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.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.2.2";
401
+ var version = "3.2.4";
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";
@@ -522,175 +522,210 @@ function doCallback(id, body, err) {
522
522
  return true;
523
523
  }
524
524
 
525
-
526
- /*
527
- retentionTime: retention time, default 48 hours
528
- */
529
525
  class Database {
530
526
  KEY_TYPE_TEXT = "TEXT";
531
527
  KEY_TYPE_INTEGER = "INTEGER";
532
528
 
533
- constructor(db, callback) {
534
- // sub class
535
- let database = this;
536
- this.Table = class {
537
- constructor(db, name, callback, retentionTime, keyType, keyName) {
538
- this.db = db;
539
- this.name = name;
529
+ /**
530
+ * The constructor of Database
531
+ * @param {string} db - name of database, can be null
532
+ */
533
+ constructor(db) {
534
+ this.db = db;
535
+ }
540
536
 
541
- let body = {
542
- db: this.db, cmd: "db.table.create", name: name
543
- };
544
- if (retentionTime) {
545
- body.retentionTime = retentionTime;
546
- }
547
- if (keyType === 'INTEGER' || keyType === 'TEXT') {
548
- body.keyName = keyName;
549
- body.keyType = keyType;
550
- } else {
551
- throw new Error("keyType MUST be INTEGER or TEXT");
537
+ connect(callback) {
538
+ let body = {
539
+ cmd: "db.connect", db: this.db
540
+ };
541
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
542
+ }
552
543
 
553
- }
554
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
555
- }
544
+ run(sql, callback) {
545
+ let body = {
546
+ db: this.db, cmd: "db.run", sql: sql
547
+ };
548
+ return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
549
+ }
556
550
 
557
- insert(value, callback, tid) {
558
- let body = {
559
- db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: tid
560
- };
561
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
562
- }
551
+ setupMirror(gwIPC, lwaName, confirm = false) {
552
+ this.mirror = {gwIPC, lwaName, confirm};
553
+ }
554
+ }
563
555
 
564
- insertById(id, value, callback, tid) {
565
- let body = {
566
- db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: tid
567
- };
568
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
569
- }
556
+ Database.Table = class {
557
+ constructor(name, database) {
558
+ this.name = name;
559
+ this.database = database;
560
+ this.db = database.db;
570
561
 
571
- insertByKey(key, value, callback, tid) {
572
- let body = {
573
- db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: tid
574
- };
575
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
576
- }
562
+ if (!this.name || !this.database) {
563
+ throw new Error("name or database cannot be empty.");
564
+ }
565
+ }
577
566
 
578
- update(id, value, callback) {
579
- let body = {
580
- db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
581
- };
582
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
583
- }
567
+ create(opts, callback) {
568
+ if (typeof opts === "function") {
569
+ callback = opts;
570
+ opts = {};
571
+ }
572
+ if (!opts) {
573
+ opts = {};
574
+ }
575
+ opts.keyName = opts.keyName ? opts.keyName : 'id';
576
+ opts.keyType = opts.keyType ? opts.keyType : 'INTEGER';
577
+ if (opts.keyType !== 'INTEGER' && opts.keyType !== 'TEXT') {
578
+ throw new Error("keyType MUST be INTEGER or TEXT");
579
+ }
580
+ this.opts = opts;
584
581
 
585
- updateByKey(key, value, callback) {
586
- let body = {
587
- db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
588
- };
589
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
590
- }
582
+ let body = {
583
+ db: this.db, cmd: "db.table.create", name: this.name,
584
+ retentionTime: opts.retentionTime,
585
+ keyType: opts.keyType,
586
+ keyName: opts.keyName
587
+ };
591
588
 
592
- delete(id, callback) {
593
- let body = {
594
- db: this.db, cmd: "db.table.delete", name: this.name, id: id
595
- };
596
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
597
- }
589
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
590
+ }
598
591
 
599
- query(id, callback, count = 100) {
600
- let body = {
601
- db: this.db, cmd: "db.table.query", name: this.name, id: id
602
- };
603
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
604
- }
592
+ insert(value, opts, callback) {
593
+ if (typeof opts === "function") {
594
+ callback = opts;
595
+ opts = {};
596
+ }
605
597
 
606
- queryByKey(key, callback, count = 100) {
607
- let body = {
608
- db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
609
- };
610
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
611
- }
598
+ let body = {
599
+ db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: opts?.tid
600
+ };
601
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
602
+ }
612
603
 
613
- queryByTid(tid, callback, limit = 100) {
614
- let body = {
615
- db: this.db, cmd: "db.table.queryByTid", name: this.name, limit: limit, tid: tid
616
- };
617
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
618
- }
604
+ insertById(id, value, opts, callback) {
605
+ if (typeof opts === "function") {
606
+ callback = opts;
607
+ opts = {};
608
+ }
619
609
 
620
- queryUnsynced(callback, limit = 100) {
621
- let body = {
622
- db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: limit
623
- };
624
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
625
- }
610
+ let body = {
611
+ db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: opts?.tid
612
+ };
613
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
614
+ }
626
615
 
627
- queryAll(callback) {
628
- let body = {
629
- db: this.db, cmd: "db.table.queryAll", name: this.name
630
- };
631
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
632
- }
616
+ insertByKey(key, value, opts, callback) {
617
+ if (typeof opts === "function") {
618
+ callback = opts;
619
+ opts = {};
620
+ }
633
621
 
634
- sync(id, callback) {
635
- let body = {
636
- db: this.db, cmd: "db.table.sync", name: this.name, id: id
637
- };
638
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
639
- }
622
+ let body = {
623
+ db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: opts?.tid
624
+ };
625
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
626
+ }
640
627
 
641
- publishRequestMirror(topic, callback, body) {
642
- let mirror = database.mirror;
643
- let cb = callback;
644
- if (mirror && mirror.gwIPC) { // mirror enabled
645
- cb = (dbResult) => {
646
- let confirmCallback = !mirror.confirm ? null : (gwResult) => {
647
- if (gwResult.status === "success") {
648
- this.sync(dbResult.row);
649
- }
650
- };
651
- if (dbResult.status === true) {
652
- switch (body.cmd) {
653
- case "db.table.create":
654
- break;
655
- case "db.table.insert":
656
- body.id = dbResult.msg.row;
657
- body.timestamp = dbResult.msg.timestamp;
658
- break;
659
- case "db.table.insertById":
660
- case "db.table.insertByKey":
661
- body.timestamp = dbResult.msg.timestamp;
662
- break;
663
- }
664
- mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
665
- }
666
- callback && callback(dbResult);
667
- };
668
- }
669
- return publishRequest(topic, cb, body);
670
- }
628
+ update(id, value, callback) {
629
+ let body = {
630
+ db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
671
631
  };
672
- this.db = db;
632
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
633
+ }
634
+
635
+ updateByKey(key, value, callback) {
673
636
  let body = {
674
- cmd: "db.connect", db: this.db
637
+ db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
675
638
  };
676
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
639
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
677
640
  }
678
641
 
679
- run(sql, callback) {
642
+ delete(id, callback) {
680
643
  let body = {
681
- db: this.db, cmd: "db.run", sql: sql
644
+ db: this.db, cmd: "db.table.delete", name: this.name, id: id
682
645
  };
683
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
646
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
684
647
  }
685
648
 
686
- createTable(name, callback, retentionTime, keyType = 'INTEGER', keyName = 'id') {
687
- return new this.Table(null, name, callback, retentionTime, keyType, keyName);
649
+ query(id, callback) {
650
+ let body = {
651
+ db: this.db, cmd: "db.table.query", name: this.name, id: id
652
+ };
653
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
688
654
  }
689
655
 
690
- setupMirror(gwIPC, lwaName, confirm = false) {
691
- this.mirror = {gwIPC, lwaName, confirm};
656
+ queryByKey(key, callback) {
657
+ let body = {
658
+ db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
659
+ };
660
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
661
+ }
662
+
663
+ queryByTid(tid, callback) {
664
+ let body = {
665
+ db: this.db, cmd: "db.table.queryByTid", name: this.name, tid: tid
666
+ };
667
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
668
+ }
669
+
670
+ queryUnsynced(opts, callback) {
671
+ if (typeof opts === "function") {
672
+ callback = opts;
673
+ opts = {limit: 100};
674
+ }
675
+ if (!opts) opts = {};
676
+ if (opts.limit <= 0) opts.limit = 100;
677
+
678
+ let body = {
679
+ db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: opts.limit
680
+ };
681
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
692
682
  }
693
- }
683
+
684
+ queryAll(callback) {
685
+ let body = {
686
+ db: this.db, cmd: "db.table.queryAll", name: this.name
687
+ };
688
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
689
+ }
690
+
691
+ sync(id, callback) {
692
+ let body = {
693
+ db: this.db, cmd: "db.table.sync", name: this.name, id: id
694
+ };
695
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
696
+ }
697
+
698
+ publishRequestMirror(topic, callback, body) {
699
+ let mirror = this.database.mirror;
700
+ let cb = callback;
701
+ if (mirror && mirror.gwIPC) { // mirror enabled
702
+ cb = (dbResult) => {
703
+ let confirmCallback = !mirror.confirm ? null : (gwResult) => {
704
+ if (gwResult.status === "success") {
705
+ this.sync(dbResult.row);
706
+ }
707
+ };
708
+ if (dbResult.status === true) {
709
+ switch (body.cmd) {
710
+ case "db.table.create":
711
+ break;
712
+ case "db.table.insert":
713
+ body.id = dbResult.msg.row;
714
+ body.timestamp = dbResult.msg.timestamp;
715
+ break;
716
+ case "db.table.insertById":
717
+ case "db.table.insertByKey":
718
+ body.timestamp = dbResult.msg.timestamp;
719
+ break;
720
+ }
721
+ mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
722
+ }
723
+ callback && callback(dbResult);
724
+ };
725
+ }
726
+ return publishRequest(topic, cb, body);
727
+ }
728
+ };
694
729
 
695
730
  class Logger {
696
731
  constructor() {
@@ -807,10 +842,8 @@ class Device {
807
842
  *
808
843
  * @param name
809
844
  * @param type
810
- * @param callback(result, error)
811
- * @param opts
812
845
  */
813
- constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT, callback, opts) {
846
+ constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT) {
814
847
  this.name = name;
815
848
  this.type = type;
816
849
  this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
@@ -837,26 +870,35 @@ class Device {
837
870
  };
838
871
  listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
839
872
  }
840
- // subscribe callback
873
+ // subscribe command callback
841
874
  client.subscribe(this.topicCommand + '-callback');
842
- this.init(name, type, callback, opts);
843
875
  }
844
876
 
845
- init(name, type, callback, opts) {
877
+ /**
878
+ *
879
+ * @param callback(result, error)
880
+ * @param opts
881
+ */
882
+ init(opts, callback) {
883
+ if (typeof opts === "function") {
884
+ callback = opts;
885
+ opts = null;
886
+ }
887
+
846
888
  let command = {};
847
889
  command.cmd = "init";
848
- command.args = {name, type, opts};
890
+ command.args = {name: this.name, type: this.type, opts};
849
891
  command.topicResponse = this.topicResponse;
850
892
  let defaultTopic = cutosAPI$1.DEVICE.CHANNEL + '-' + cutosAPI$1.DEVICE.DEFAULT + '-command';
851
893
  publishRequest(defaultTopic, null, command);
852
894
 
853
- // response callback
895
+ // subscribe response callback, currently only used for init
854
896
  client.subscribe(this.topicResponse); // receive response from device
855
- listenerMap.set(this.topicResponse, ({status, msg}) => {
856
- if (status) {
897
+ listenerMap.set(this.topicResponse, (result) => {
898
+ if (result.status) {
857
899
  callback && callback(`Driver ${this.type} loaded`);
858
900
  } else {
859
- callback && callback(null, msg);
901
+ callback && callback(null, result.msg);
860
902
  }
861
903
  });
862
904
  }
@@ -871,8 +913,8 @@ class Device {
871
913
 
872
914
  /**
873
915
  *
874
- * @param command
875
- * @param callback(result)
916
+ * @param command - command description object {cmd: 'command', args: (''|{}) }
917
+ * @param callback - (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
876
918
  */
877
919
  sendCommand(command, callback) {
878
920
  // exam: command = {cmd: "connect", args: ""}
@@ -894,10 +936,13 @@ class Device {
894
936
  return this.data;
895
937
  }
896
938
 
897
- readDeviceInfo(callback, context) {
939
+ /**
940
+ *
941
+ * @param callback (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
942
+ */
943
+ readDeviceInfo(callback) {
898
944
  let msg = {};
899
945
  msg.cmd = "read-device-info";
900
- msg.context = context;
901
946
  this.sendCommand(msg, callback);
902
947
  }
903
948
  }
@@ -1036,6 +1081,7 @@ const CoreAPI$2 = {
1036
1081
  console.error("CUTOS CORE error:", err.message);
1037
1082
  });
1038
1083
  },
1084
+
1039
1085
  connected() {
1040
1086
  return !!client && client.connected;
1041
1087
  },
@@ -1090,10 +1136,6 @@ const CoreAPI$2 = {
1090
1136
  return publishRequest(cutosAPI$1.CORE.SHELL, callback, command);
1091
1137
  },
1092
1138
 
1093
- getDatabase(db, callback) {
1094
- return new Database(db, callback);
1095
- },
1096
-
1097
1139
  getVolume(callback) {
1098
1140
  return publishRequest(cutosAPI$1.CORE.GET_VOLUME, callback);
1099
1141
  },
@@ -1112,7 +1154,7 @@ cutos.CoreDefine = {
1112
1154
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
1113
1155
  };
1114
1156
  cutos.CoreClass = {
1115
- Device, Driver
1157
+ Device, Driver, Database
1116
1158
  };
1117
1159
 
1118
1160
  cutos.cutosAPI = cutosAPI$1;