@cutos/core 0.0.14 → 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
@@ -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.14";
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";
@@ -471,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]|
471
471
 
472
472
  let client = null;
473
473
 
474
- function publishRequest(api, callback, body, destAddress) {
474
+ function publishRequest(topic, callback, body, destAddress) {
475
475
  let request = {};
476
476
  request.context = {};
477
477
  request.context.id = uuidv4();
@@ -480,7 +480,18 @@ function publishRequest(api, callback, body, destAddress) {
480
480
  if (typeof callback === "function") {
481
481
  callbackMap.set(request.context.id, callback);
482
482
  }
483
- 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));
484
495
  return true;
485
496
  }
486
497
 
@@ -791,6 +802,8 @@ class Device {
791
802
  };
792
803
  listenerMap.set(cutosAPI.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
793
804
  }
805
+ // subscribe callback
806
+ client.subscribe(this.topicCommand + '-callback');
794
807
  this.init(name, type, callback);
795
808
  }
796
809
 
@@ -815,8 +828,9 @@ class Device {
815
828
  return this.msg;
816
829
  }
817
830
 
818
- sendCommand(command) {
819
- client.publish(this.topicCommand, JSON.stringify(command));
831
+ sendCommand(command, callback) {
832
+ // exam: command = {cmd: "connect", args: ""}
833
+ publishRequest(this.topicCommand, callback, command);
820
834
  }
821
835
 
822
836
  onData(listener) {
@@ -850,7 +864,6 @@ class Driver {
850
864
  this.topicData = cutosAPI.DEVICE.CHANNEL + '-' + type + '-data';
851
865
  this.topicResponse = cutosAPI.DEVICE.CHANNEL + '-' + type + '-response';
852
866
  this.topicCommand = cutosAPI.DEVICE.CHANNEL + '-' + type + '-command';
853
-
854
867
  }
855
868
 
856
869
  startBeat() {
@@ -877,9 +890,19 @@ class Driver {
877
890
  }
878
891
 
879
892
  onCommand(listener) {
880
- 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
+
881
904
  client.subscribe(this.topicCommand); // receive command from app
882
- listenerMap.set(this.topicCommand, listener);
905
+ listenerMap.set(this.topicCommand, cmdListener);
883
906
  }
884
907
  }
885
908
  }
@@ -899,7 +922,7 @@ const CoreAPI = {
899
922
  for (let api in cutosAPI.CORE_APP) {
900
923
  client.subscribe(cutosAPI.CORE_APP[api]);
901
924
  }
902
- if(typeof(callback) === 'function') {
925
+ if (typeof (callback) === 'function') {
903
926
  callback({
904
927
  status: true,
905
928
  msg: 'CUTOS CORE connected.'
@@ -916,7 +939,8 @@ const CoreAPI = {
916
939
  if (listener !== undefined) {
917
940
  listener(_message.body.args, _message.context);
918
941
  }
919
- } 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')) {
920
944
  let listener = listenerMap.get(topic);
921
945
  if (listener !== undefined) {
922
946
  listener(_message);
@@ -935,7 +959,7 @@ const CoreAPI = {
935
959
  });
936
960
 
937
961
  client.on("error", (e) => {
938
- if(typeof(callback) === 'function') {
962
+ if (typeof (callback) === 'function') {
939
963
  callback({
940
964
  status: false,
941
965
  msg: e.message
package/dist/cutos.esm.js CHANGED
@@ -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.14";
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";
@@ -763,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]|
763
763
 
764
764
  let client = null;
765
765
 
766
- function publishRequest(api, callback, body, destAddress) {
766
+ function publishRequest(topic, callback, body, destAddress) {
767
767
  let request = {};
768
768
  request.context = {};
769
769
  request.context.id = uuidv4();
@@ -772,7 +772,18 @@ function publishRequest(api, callback, body, destAddress) {
772
772
  if (typeof callback === "function") {
773
773
  callbackMap.set(request.context.id, callback);
774
774
  }
775
- 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));
776
787
  return true;
777
788
  }
778
789
 
@@ -1083,6 +1094,8 @@ class Device {
1083
1094
  };
1084
1095
  listenerMap.set(cutosAPI.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
1085
1096
  }
1097
+ // subscribe callback
1098
+ client.subscribe(this.topicCommand + '-callback');
1086
1099
  this.init(name, type, callback);
1087
1100
  }
1088
1101
 
@@ -1107,8 +1120,9 @@ class Device {
1107
1120
  return this.msg;
1108
1121
  }
1109
1122
 
1110
- sendCommand(command) {
1111
- client.publish(this.topicCommand, JSON.stringify(command));
1123
+ sendCommand(command, callback) {
1124
+ // exam: command = {cmd: "connect", args: ""}
1125
+ publishRequest(this.topicCommand, callback, command);
1112
1126
  }
1113
1127
 
1114
1128
  onData(listener) {
@@ -1142,7 +1156,6 @@ class Driver {
1142
1156
  this.topicData = cutosAPI.DEVICE.CHANNEL + '-' + type + '-data';
1143
1157
  this.topicResponse = cutosAPI.DEVICE.CHANNEL + '-' + type + '-response';
1144
1158
  this.topicCommand = cutosAPI.DEVICE.CHANNEL + '-' + type + '-command';
1145
-
1146
1159
  }
1147
1160
 
1148
1161
  startBeat() {
@@ -1169,9 +1182,19 @@ class Driver {
1169
1182
  }
1170
1183
 
1171
1184
  onCommand(listener) {
1172
- 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
+
1173
1196
  client.subscribe(this.topicCommand); // receive command from app
1174
- listenerMap.set(this.topicCommand, listener);
1197
+ listenerMap.set(this.topicCommand, cmdListener);
1175
1198
  }
1176
1199
  }
1177
1200
  }
@@ -1191,7 +1214,7 @@ const CoreAPI = {
1191
1214
  for (let api in cutosAPI.CORE_APP) {
1192
1215
  client.subscribe(cutosAPI.CORE_APP[api]);
1193
1216
  }
1194
- if(typeof(callback) === 'function') {
1217
+ if (typeof (callback) === 'function') {
1195
1218
  callback({
1196
1219
  status: true,
1197
1220
  msg: 'CUTOS CORE connected.'
@@ -1208,7 +1231,8 @@ const CoreAPI = {
1208
1231
  if (listener !== undefined) {
1209
1232
  listener(_message.body.args, _message.context);
1210
1233
  }
1211
- } 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')) {
1212
1236
  let listener = listenerMap.get(topic);
1213
1237
  if (listener !== undefined) {
1214
1238
  listener(_message);
@@ -1227,7 +1251,7 @@ const CoreAPI = {
1227
1251
  });
1228
1252
 
1229
1253
  client.on("error", (e) => {
1230
- if(typeof(callback) === 'function') {
1254
+ if (typeof (callback) === 'function') {
1231
1255
  callback({
1232
1256
  status: false,
1233
1257
  msg: e.message
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "",
5
5
  "main": "dist/cutos.cjs.js",
6
6
  "module": "dist/cutos.esm.js",