@cutos/core 0.0.13 → 0.0.15

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
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var crypto = require('crypto');
6
6
  var require$$1 = require('mqtt');
7
- var require$$2 = require('lodash');
7
+ require('lodash');
8
8
  var require$$0$1 = require('qs');
9
9
 
10
10
  function getAugmentedNamespace(n) {
@@ -411,7 +411,7 @@ if(typeof window !== 'undefined') {
411
411
  var utils = config$1;
412
412
 
413
413
  var name = "@cutos/core";
414
- var version = "0.0.13";
414
+ var version = "0.0.15";
415
415
  var description = "";
416
416
  var main = "dist/cutos.cjs.js";
417
417
  var module$1 = "dist/cutos.esm.js";
@@ -461,7 +461,6 @@ var require$$5 = {
461
461
 
462
462
  const {v4: uuidv4} = require$$0;
463
463
  const mqtt = require$$1;
464
- const _ = require$$2;
465
464
  const cutosAPI = cutosApi;
466
465
  const config = utils;
467
466
  const callbackMap = new Map();
@@ -472,7 +471,7 @@ const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|
472
471
 
473
472
  let client = null;
474
473
 
475
- function publishRequest(api, callback, body, destAddress) {
474
+ function publishRequest(topic, callback, body, destAddress) {
476
475
  let request = {};
477
476
  request.context = {};
478
477
  request.context.id = uuidv4();
@@ -481,7 +480,18 @@ function publishRequest(api, callback, body, destAddress) {
481
480
  if (typeof callback === "function") {
482
481
  callbackMap.set(request.context.id, callback);
483
482
  }
484
- client.publish(api, JSON.stringify(request));
483
+ client.publish(topic, JSON.stringify(request));
484
+ return true;
485
+ }
486
+
487
+ function publishResponse(topic, request, result) {
488
+ if (!request.context || !request.context.id) {
489
+ return false;
490
+ }
491
+ let response = {};
492
+ response.id = request.context.id;
493
+ response.result = result;
494
+ client.publish(topic + '-callback', JSON.stringify(response));
485
495
  return true;
486
496
  }
487
497
 
@@ -492,14 +502,7 @@ class Database {
492
502
  KEY_TYPE_TEXT = "TEXT";
493
503
  KEY_TYPE_INTEGER = "INTEGER";
494
504
 
495
- constructor(db, callback, opts = {
496
- retentionTime: 48,
497
- mirror: {
498
- gwIPC: null,
499
- lwaName: "lwa-default",
500
- confirm: false
501
- }
502
- }) {
505
+ constructor(db, callback) {
503
506
  // sub class
504
507
  let database = this;
505
508
  this.Table = class {
@@ -610,7 +613,7 @@ class Database {
610
613
  publishRequestMirror(topic, callback, body) {
611
614
  let mirror = database.mirror;
612
615
  let cb = callback;
613
- if (mirror.gwIPC) { // mirror enabled
616
+ if (mirror && mirror.gwIPC) { // mirror enabled
614
617
  cb = (dbResult) => {
615
618
  let confirmCallback = !mirror.confirm ? null : (gwResult) => {
616
619
  if (gwResult.status === "success") {
@@ -639,8 +642,6 @@ class Database {
639
642
  }
640
643
  };
641
644
  this.db = db;
642
- this.retentionTime = opts.retentionTime ? opts.retentionTime : 48;// default 48 hours
643
- this.mirror = opts.mirror;
644
645
  let body = {
645
646
  cmd: "db.connect", db: this.db
646
647
  };
@@ -657,6 +658,10 @@ class Database {
657
658
  createTable(name, callback, retentionTime, keyType = 'INTEGER', keyName = 'id') {
658
659
  return new this.Table(null, name, callback, retentionTime, keyType, keyName);
659
660
  }
661
+
662
+ setupMirror(gwIPC, lwaName, confirm = false) {
663
+ this.mirror = {gwIPC, lwaName, confirm};
664
+ }
660
665
  }
661
666
 
662
667
  class Logger {
@@ -797,6 +802,8 @@ class Device {
797
802
  };
798
803
  listenerMap.set(cutosAPI.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
799
804
  }
805
+ // subscribe callback
806
+ client.subscribe(this.topicCommand + '-callback');
800
807
  this.init(name, type, callback);
801
808
  }
802
809
 
@@ -821,8 +828,9 @@ class Device {
821
828
  return this.msg;
822
829
  }
823
830
 
824
- sendCommand(command) {
825
- client.publish(this.topicCommand, JSON.stringify(command));
831
+ sendCommand(command, callback) {
832
+ // exam: command = {cmd: "connect", args: ""}
833
+ publishRequest(this.topicCommand, callback, command);
826
834
  }
827
835
 
828
836
  onData(listener) {
@@ -856,7 +864,6 @@ class Driver {
856
864
  this.topicData = cutosAPI.DEVICE.CHANNEL + '-' + type + '-data';
857
865
  this.topicResponse = cutosAPI.DEVICE.CHANNEL + '-' + type + '-response';
858
866
  this.topicCommand = cutosAPI.DEVICE.CHANNEL + '-' + type + '-command';
859
-
860
867
  }
861
868
 
862
869
  startBeat() {
@@ -883,9 +890,19 @@ class Driver {
883
890
  }
884
891
 
885
892
  onCommand(listener) {
886
- if(listener) {
893
+ if (listener) {
894
+ let cmdListener = (message) => {
895
+ // process command callback
896
+ // message.context
897
+ // message.context.id
898
+ // message.body
899
+ listener(message.body, result => {
900
+ publishResponse(this.topicCommand, message, result);
901
+ });
902
+ };
903
+
887
904
  client.subscribe(this.topicCommand); // receive command from app
888
- listenerMap.set(this.topicCommand, listener);
905
+ listenerMap.set(this.topicCommand, cmdListener);
889
906
  }
890
907
  }
891
908
  }
@@ -905,7 +922,7 @@ const CoreAPI = {
905
922
  for (let api in cutosAPI.CORE_APP) {
906
923
  client.subscribe(cutosAPI.CORE_APP[api]);
907
924
  }
908
- if(typeof(callback) === 'function') {
925
+ if (typeof (callback) === 'function') {
909
926
  callback({
910
927
  status: true,
911
928
  msg: 'CUTOS CORE connected.'
@@ -922,7 +939,8 @@ const CoreAPI = {
922
939
  if (listener !== undefined) {
923
940
  listener(_message.body.args, _message.context);
924
941
  }
925
- } else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER || topic.startsWith(cutosAPI.DEVICE.CHANNEL)) {
942
+ } else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER
943
+ || topic.startsWith(cutosAPI.DEVICE.CHANNEL) && !topic.endsWith('-callback')) {
926
944
  let listener = listenerMap.get(topic);
927
945
  if (listener !== undefined) {
928
946
  listener(_message);
@@ -941,7 +959,7 @@ const CoreAPI = {
941
959
  });
942
960
 
943
961
  client.on("error", (e) => {
944
- if(typeof(callback) === 'function') {
962
+ if (typeof (callback) === 'function') {
945
963
  callback({
946
964
  status: false,
947
965
  msg: e.message
@@ -974,6 +992,10 @@ const CoreAPI = {
974
992
  return publishRequest(cutosAPI.CORE.SIGN_INFO, callback);
975
993
  },
976
994
 
995
+ getDeviceInfo(callback) {
996
+ return publishRequest(cutosAPI.CORE.DEVICE_INFO, callback);
997
+ },
998
+
977
999
  getSignGroupInfo(callback) {
978
1000
  return publishRequest(cutosAPI.CORE.SIGN_GROUP_INFO, callback);
979
1001
  },
@@ -995,19 +1017,7 @@ const CoreAPI = {
995
1017
  },
996
1018
 
997
1019
  getDatabase(db, callback) {
998
- let opts = {
999
- retentionTime: 48,
1000
- mirror: {
1001
- gwIPC: null,
1002
- lwaName: "lwa-default",
1003
- confirm: false
1004
- }
1005
- };
1006
- if (typeof db === 'object') {
1007
- _.merge(opts, db);
1008
- db = opts.db;
1009
- }
1010
- return new Database(db, callback, opts);
1020
+ return new Database(db, callback);
1011
1021
  },
1012
1022
 
1013
1023
  getVolume(callback) {
package/dist/cutos.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import require$$1 from 'mqtt/dist/mqtt';
2
- import require$$2 from 'lodash';
2
+ import 'lodash';
3
3
  import require$$0$1 from 'qs';
4
4
 
5
5
  function getAugmentedNamespace(n) {
@@ -703,7 +703,7 @@ if(typeof window !== 'undefined') {
703
703
  var utils = config$1;
704
704
 
705
705
  var name = "@cutos/core";
706
- var version = "0.0.13";
706
+ var version = "0.0.15";
707
707
  var description = "";
708
708
  var main = "dist/cutos.cjs.js";
709
709
  var module = "dist/cutos.esm.js";
@@ -753,7 +753,6 @@ var require$$5 = {
753
753
 
754
754
  const {v4: uuidv4} = require$$0;
755
755
  const mqtt = require$$1;
756
- const _ = require$$2;
757
756
  const cutosAPI = cutosApi;
758
757
  const config = utils;
759
758
  const callbackMap = new Map();
@@ -764,7 +763,7 @@ const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|
764
763
 
765
764
  let client = null;
766
765
 
767
- function publishRequest(api, callback, body, destAddress) {
766
+ function publishRequest(topic, callback, body, destAddress) {
768
767
  let request = {};
769
768
  request.context = {};
770
769
  request.context.id = uuidv4();
@@ -773,7 +772,18 @@ function publishRequest(api, callback, body, destAddress) {
773
772
  if (typeof callback === "function") {
774
773
  callbackMap.set(request.context.id, callback);
775
774
  }
776
- client.publish(api, JSON.stringify(request));
775
+ client.publish(topic, JSON.stringify(request));
776
+ return true;
777
+ }
778
+
779
+ function publishResponse(topic, request, result) {
780
+ if (!request.context || !request.context.id) {
781
+ return false;
782
+ }
783
+ let response = {};
784
+ response.id = request.context.id;
785
+ response.result = result;
786
+ client.publish(topic + '-callback', JSON.stringify(response));
777
787
  return true;
778
788
  }
779
789
 
@@ -784,14 +794,7 @@ class Database {
784
794
  KEY_TYPE_TEXT = "TEXT";
785
795
  KEY_TYPE_INTEGER = "INTEGER";
786
796
 
787
- constructor(db, callback, opts = {
788
- retentionTime: 48,
789
- mirror: {
790
- gwIPC: null,
791
- lwaName: "lwa-default",
792
- confirm: false
793
- }
794
- }) {
797
+ constructor(db, callback) {
795
798
  // sub class
796
799
  let database = this;
797
800
  this.Table = class {
@@ -902,7 +905,7 @@ class Database {
902
905
  publishRequestMirror(topic, callback, body) {
903
906
  let mirror = database.mirror;
904
907
  let cb = callback;
905
- if (mirror.gwIPC) { // mirror enabled
908
+ if (mirror && mirror.gwIPC) { // mirror enabled
906
909
  cb = (dbResult) => {
907
910
  let confirmCallback = !mirror.confirm ? null : (gwResult) => {
908
911
  if (gwResult.status === "success") {
@@ -931,8 +934,6 @@ class Database {
931
934
  }
932
935
  };
933
936
  this.db = db;
934
- this.retentionTime = opts.retentionTime ? opts.retentionTime : 48;// default 48 hours
935
- this.mirror = opts.mirror;
936
937
  let body = {
937
938
  cmd: "db.connect", db: this.db
938
939
  };
@@ -949,6 +950,10 @@ class Database {
949
950
  createTable(name, callback, retentionTime, keyType = 'INTEGER', keyName = 'id') {
950
951
  return new this.Table(null, name, callback, retentionTime, keyType, keyName);
951
952
  }
953
+
954
+ setupMirror(gwIPC, lwaName, confirm = false) {
955
+ this.mirror = {gwIPC, lwaName, confirm};
956
+ }
952
957
  }
953
958
 
954
959
  class Logger {
@@ -1089,6 +1094,8 @@ class Device {
1089
1094
  };
1090
1095
  listenerMap.set(cutosAPI.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
1091
1096
  }
1097
+ // subscribe callback
1098
+ client.subscribe(this.topicCommand + '-callback');
1092
1099
  this.init(name, type, callback);
1093
1100
  }
1094
1101
 
@@ -1113,8 +1120,9 @@ class Device {
1113
1120
  return this.msg;
1114
1121
  }
1115
1122
 
1116
- sendCommand(command) {
1117
- client.publish(this.topicCommand, JSON.stringify(command));
1123
+ sendCommand(command, callback) {
1124
+ // exam: command = {cmd: "connect", args: ""}
1125
+ publishRequest(this.topicCommand, callback, command);
1118
1126
  }
1119
1127
 
1120
1128
  onData(listener) {
@@ -1148,7 +1156,6 @@ class Driver {
1148
1156
  this.topicData = cutosAPI.DEVICE.CHANNEL + '-' + type + '-data';
1149
1157
  this.topicResponse = cutosAPI.DEVICE.CHANNEL + '-' + type + '-response';
1150
1158
  this.topicCommand = cutosAPI.DEVICE.CHANNEL + '-' + type + '-command';
1151
-
1152
1159
  }
1153
1160
 
1154
1161
  startBeat() {
@@ -1175,9 +1182,19 @@ class Driver {
1175
1182
  }
1176
1183
 
1177
1184
  onCommand(listener) {
1178
- if(listener) {
1185
+ if (listener) {
1186
+ let cmdListener = (message) => {
1187
+ // process command callback
1188
+ // message.context
1189
+ // message.context.id
1190
+ // message.body
1191
+ listener(message.body, result => {
1192
+ publishResponse(this.topicCommand, message, result);
1193
+ });
1194
+ };
1195
+
1179
1196
  client.subscribe(this.topicCommand); // receive command from app
1180
- listenerMap.set(this.topicCommand, listener);
1197
+ listenerMap.set(this.topicCommand, cmdListener);
1181
1198
  }
1182
1199
  }
1183
1200
  }
@@ -1197,7 +1214,7 @@ const CoreAPI = {
1197
1214
  for (let api in cutosAPI.CORE_APP) {
1198
1215
  client.subscribe(cutosAPI.CORE_APP[api]);
1199
1216
  }
1200
- if(typeof(callback) === 'function') {
1217
+ if (typeof (callback) === 'function') {
1201
1218
  callback({
1202
1219
  status: true,
1203
1220
  msg: 'CUTOS CORE connected.'
@@ -1214,7 +1231,8 @@ const CoreAPI = {
1214
1231
  if (listener !== undefined) {
1215
1232
  listener(_message.body.args, _message.context);
1216
1233
  }
1217
- } else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER || topic.startsWith(cutosAPI.DEVICE.CHANNEL)) {
1234
+ } else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER
1235
+ || topic.startsWith(cutosAPI.DEVICE.CHANNEL) && !topic.endsWith('-callback')) {
1218
1236
  let listener = listenerMap.get(topic);
1219
1237
  if (listener !== undefined) {
1220
1238
  listener(_message);
@@ -1233,7 +1251,7 @@ const CoreAPI = {
1233
1251
  });
1234
1252
 
1235
1253
  client.on("error", (e) => {
1236
- if(typeof(callback) === 'function') {
1254
+ if (typeof (callback) === 'function') {
1237
1255
  callback({
1238
1256
  status: false,
1239
1257
  msg: e.message
@@ -1266,6 +1284,10 @@ const CoreAPI = {
1266
1284
  return publishRequest(cutosAPI.CORE.SIGN_INFO, callback);
1267
1285
  },
1268
1286
 
1287
+ getDeviceInfo(callback) {
1288
+ return publishRequest(cutosAPI.CORE.DEVICE_INFO, callback);
1289
+ },
1290
+
1269
1291
  getSignGroupInfo(callback) {
1270
1292
  return publishRequest(cutosAPI.CORE.SIGN_GROUP_INFO, callback);
1271
1293
  },
@@ -1287,19 +1309,7 @@ const CoreAPI = {
1287
1309
  },
1288
1310
 
1289
1311
  getDatabase(db, callback) {
1290
- let opts = {
1291
- retentionTime: 48,
1292
- mirror: {
1293
- gwIPC: null,
1294
- lwaName: "lwa-default",
1295
- confirm: false
1296
- }
1297
- };
1298
- if (typeof db === 'object') {
1299
- _.merge(opts, db);
1300
- db = opts.db;
1301
- }
1302
- return new Database(db, callback, opts);
1312
+ return new Database(db, callback);
1303
1313
  },
1304
1314
 
1305
1315
  getVolume(callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "",
5
5
  "main": "dist/cutos.cjs.js",
6
6
  "module": "dist/cutos.esm.js",