@cutos/core 0.0.14 → 0.0.16
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 +38 -11
- package/dist/cutos.esm.js +38 -11
- package/package.json +1 -1
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.
|
|
414
|
+
var version = "0.0.16";
|
|
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(
|
|
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(
|
|
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
|
-
|
|
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,15 +890,28 @@ 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,
|
|
905
|
+
listenerMap.set(this.topicCommand, cmdListener);
|
|
883
906
|
}
|
|
884
907
|
}
|
|
885
908
|
}
|
|
886
909
|
|
|
887
910
|
const CoreAPI = {
|
|
888
911
|
init(mqttHost = 'ws://localhost:1883', callback) {
|
|
912
|
+
if (!mqttHost) {
|
|
913
|
+
mqttHost = 'ws://localhost:1883';
|
|
914
|
+
}
|
|
889
915
|
client = mqtt.connect(mqttHost);
|
|
890
916
|
|
|
891
917
|
client.on('connect', function () {
|
|
@@ -899,7 +925,7 @@ const CoreAPI = {
|
|
|
899
925
|
for (let api in cutosAPI.CORE_APP) {
|
|
900
926
|
client.subscribe(cutosAPI.CORE_APP[api]);
|
|
901
927
|
}
|
|
902
|
-
if(typeof(callback) === 'function') {
|
|
928
|
+
if (typeof (callback) === 'function') {
|
|
903
929
|
callback({
|
|
904
930
|
status: true,
|
|
905
931
|
msg: 'CUTOS CORE connected.'
|
|
@@ -916,7 +942,8 @@ const CoreAPI = {
|
|
|
916
942
|
if (listener !== undefined) {
|
|
917
943
|
listener(_message.body.args, _message.context);
|
|
918
944
|
}
|
|
919
|
-
} else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER
|
|
945
|
+
} else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER
|
|
946
|
+
|| topic.startsWith(cutosAPI.DEVICE.CHANNEL) && !topic.endsWith('-callback')) {
|
|
920
947
|
let listener = listenerMap.get(topic);
|
|
921
948
|
if (listener !== undefined) {
|
|
922
949
|
listener(_message);
|
|
@@ -935,7 +962,7 @@ const CoreAPI = {
|
|
|
935
962
|
});
|
|
936
963
|
|
|
937
964
|
client.on("error", (e) => {
|
|
938
|
-
if(typeof(callback) === 'function') {
|
|
965
|
+
if (typeof (callback) === 'function') {
|
|
939
966
|
callback({
|
|
940
967
|
status: false,
|
|
941
968
|
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.
|
|
706
|
+
var version = "0.0.16";
|
|
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(
|
|
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(
|
|
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
|
-
|
|
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,15 +1182,28 @@ 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,
|
|
1197
|
+
listenerMap.set(this.topicCommand, cmdListener);
|
|
1175
1198
|
}
|
|
1176
1199
|
}
|
|
1177
1200
|
}
|
|
1178
1201
|
|
|
1179
1202
|
const CoreAPI = {
|
|
1180
1203
|
init(mqttHost = 'ws://localhost:1883', callback) {
|
|
1204
|
+
if (!mqttHost) {
|
|
1205
|
+
mqttHost = 'ws://localhost:1883';
|
|
1206
|
+
}
|
|
1181
1207
|
client = mqtt.connect(mqttHost);
|
|
1182
1208
|
|
|
1183
1209
|
client.on('connect', function () {
|
|
@@ -1191,7 +1217,7 @@ const CoreAPI = {
|
|
|
1191
1217
|
for (let api in cutosAPI.CORE_APP) {
|
|
1192
1218
|
client.subscribe(cutosAPI.CORE_APP[api]);
|
|
1193
1219
|
}
|
|
1194
|
-
if(typeof(callback) === 'function') {
|
|
1220
|
+
if (typeof (callback) === 'function') {
|
|
1195
1221
|
callback({
|
|
1196
1222
|
status: true,
|
|
1197
1223
|
msg: 'CUTOS CORE connected.'
|
|
@@ -1208,7 +1234,8 @@ const CoreAPI = {
|
|
|
1208
1234
|
if (listener !== undefined) {
|
|
1209
1235
|
listener(_message.body.args, _message.context);
|
|
1210
1236
|
}
|
|
1211
|
-
} else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER
|
|
1237
|
+
} else if (topic === cutosAPI.CORE_APP.NOTIFICATION || topic === cutosAPI.CORE_APP.HEARTBEAT_LISTENER
|
|
1238
|
+
|| topic.startsWith(cutosAPI.DEVICE.CHANNEL) && !topic.endsWith('-callback')) {
|
|
1212
1239
|
let listener = listenerMap.get(topic);
|
|
1213
1240
|
if (listener !== undefined) {
|
|
1214
1241
|
listener(_message);
|
|
@@ -1227,7 +1254,7 @@ const CoreAPI = {
|
|
|
1227
1254
|
});
|
|
1228
1255
|
|
|
1229
1256
|
client.on("error", (e) => {
|
|
1230
|
-
if(typeof(callback) === 'function') {
|
|
1257
|
+
if (typeof (callback) === 'function') {
|
|
1231
1258
|
callback({
|
|
1232
1259
|
status: false,
|
|
1233
1260
|
msg: e.message
|