@cutos/core 3.3.0 → 3.4.0
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/README.md +87 -74
- package/dist/cutos.cjs.js +4 -4
- package/dist/cutos.esm.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,7 +112,7 @@ CoreAPI.getVersion();
|
|
|
112
112
|
- Return result example:
|
|
113
113
|
|
|
114
114
|
```
|
|
115
|
-
"3.
|
|
115
|
+
"3.4.0"
|
|
116
116
|
```
|
|
117
117
|
|
|
118
118
|
### CoreAPI.getPlatform
|
|
@@ -388,6 +388,18 @@ CoreAPI.shell('pwd', (result, error) => {
|
|
|
388
388
|
|
|
389
389
|
# IPC API
|
|
390
390
|
|
|
391
|
+
### Installation
|
|
392
|
+
|
|
393
|
+
`
|
|
394
|
+
npm install @cutos/core
|
|
395
|
+
`
|
|
396
|
+
|
|
397
|
+
### Introduce dependencies
|
|
398
|
+
|
|
399
|
+
```js
|
|
400
|
+
import {CoreAPI} from '@cutos/core';
|
|
401
|
+
```
|
|
402
|
+
|
|
391
403
|
### CoreAPI.getIPC
|
|
392
404
|
|
|
393
405
|
Get IPC instance object
|
|
@@ -396,7 +408,7 @@ Get IPC instance object
|
|
|
396
408
|
let ipc = CoreAPI.getIPC([destAddress]);
|
|
397
409
|
```
|
|
398
410
|
|
|
399
|
-
* destAddress:
|
|
411
|
+
* destAddress: Optional parameter, target IP address, used for inter-device process communication
|
|
400
412
|
|
|
401
413
|

|
|
402
414
|
|
|
@@ -409,104 +421,107 @@ ipc.sendTo(channel, args, callback = null);
|
|
|
409
421
|
```
|
|
410
422
|
|
|
411
423
|
* channel: channel name
|
|
412
|
-
* args: information to
|
|
424
|
+
* args: information to be sent
|
|
413
425
|
* callback: callback function
|
|
414
426
|
|
|
415
427
|
### ipc.on
|
|
416
428
|
|
|
417
|
-
|
|
429
|
+
Monitor IPC
|
|
418
430
|
|
|
419
431
|
```js
|
|
420
432
|
ipc.on(channel, listener);
|
|
421
433
|
```
|
|
422
434
|
|
|
423
435
|
* channel: channel name
|
|
424
|
-
* listener: listener function (args,
|
|
436
|
+
* listener: listener function (args, respond)
|
|
425
437
|
|
|
426
|
-
|
|
438
|
+
##### Local device communication example:
|
|
427
439
|
|
|
428
|
-
|
|
440
|
+
##### Example 1 (without return value):
|
|
429
441
|
|
|
430
442
|
```js
|
|
431
|
-
|
|
443
|
+
import {CoreAPI} from '@cutos/core';
|
|
444
|
+
|
|
445
|
+
CoreAPI.init();
|
|
446
|
+
|
|
447
|
+
let channel1 = 'channel1';
|
|
448
|
+
|
|
449
|
+
let ipc = CoreAPI.getIPC();
|
|
450
|
+
//Listen for IPC information
|
|
451
|
+
ipc.on(channel1, (args) => {
|
|
452
|
+
console.log('on', channel1, 'received', args);
|
|
453
|
+
})
|
|
454
|
+
//Send IPC information
|
|
455
|
+
ipc.sendTo(channel1, 'no callback');
|
|
432
456
|
```
|
|
433
457
|
|
|
434
|
-
|
|
435
|
-
* result: reply message content
|
|
436
|
-
* error: error message
|
|
458
|
+
- Return result example:
|
|
437
459
|
|
|
438
|
-
|
|
460
|
+
```
|
|
461
|
+
on channel1 received: "no callback"
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
##### Example 2 (with return value):
|
|
439
465
|
|
|
440
466
|
```js
|
|
441
467
|
import {CoreAPI} from '@cutos/core';
|
|
442
468
|
|
|
443
469
|
CoreAPI.init();
|
|
444
|
-
|
|
470
|
+
let channel2 = 'channel2';
|
|
445
471
|
let ipc = CoreAPI.getIPC();
|
|
446
|
-
//
|
|
447
|
-
ipc.on(
|
|
448
|
-
|
|
472
|
+
//Monitor IPC information
|
|
473
|
+
ipc.on(channel2, (args, respond) => {
|
|
474
|
+
console.log('on ' + channel2 + ' received: ' + JSON.stringify(args));
|
|
475
|
+
respond({sum: args.opt1 + args.opt2})
|
|
476
|
+
})
|
|
477
|
+
//Sending IPC information
|
|
478
|
+
ipc.sendTo(channel2, {opt1: 2, opt2: 2}, (result) => {
|
|
479
|
+
console.log('on ' + channel2 + ' callback: ' + JSON.stringify(result))
|
|
449
480
|
});
|
|
450
|
-
//Send IPC information
|
|
451
|
-
ipc.sendTo("channel-1", {event: "event-1", msg: "msg-1"});
|
|
452
481
|
```
|
|
453
|
-
|
|
454
482
|
- Return result example:
|
|
455
483
|
|
|
456
|
-
```
|
|
457
|
-
{
|
|
458
|
-
|
|
459
|
-
"msg": "msg-1"
|
|
460
|
-
}
|
|
484
|
+
```
|
|
485
|
+
on channel2 received: {"opt1":2,"opt2":2}
|
|
486
|
+
on channel2 callback: {"sum":4}
|
|
461
487
|
```
|
|
462
488
|
|
|
463
489
|
##### Example of inter-device process communication:
|
|
464
490
|
|
|
465
|
-
##### Sender
|
|
466
|
-
|
|
491
|
+
##### Sender (IP: 192.168.1.136)
|
|
492
|
+
|
|
467
493
|
```js
|
|
468
494
|
import {CoreAPI} from '@cutos/core';
|
|
469
495
|
|
|
470
496
|
CoreAPI.init();
|
|
471
|
-
|
|
497
|
+
let channel1 = 'channel1';
|
|
498
|
+
//Destination IP: 192.168.1.149
|
|
472
499
|
let ipc = CoreAPI.getIPC("192.168.1.149");
|
|
473
500
|
//Send IPC information
|
|
474
|
-
ipc.sendTo(
|
|
475
|
-
if (!error) {
|
|
476
|
-
console.log(result)
|
|
477
|
-
}
|
|
478
|
-
});
|
|
501
|
+
ipc.sendTo(channel1, 'no callback');
|
|
479
502
|
```
|
|
480
|
-
- Return result example:
|
|
481
503
|
|
|
482
|
-
|
|
483
|
-
"Received"
|
|
484
|
-
```
|
|
504
|
+
##### Receiver (IP: 192.168.1.149)
|
|
485
505
|
|
|
486
|
-
##### Receiver
|
|
487
|
-
IP:192.168.1.149
|
|
488
506
|
```js
|
|
489
507
|
import {CoreAPI} from '@cutos/core';
|
|
490
508
|
|
|
491
509
|
CoreAPI.init();
|
|
492
|
-
|
|
493
|
-
let ipc = CoreAPI.getIPC(
|
|
510
|
+
let channel1 = 'channel1';
|
|
511
|
+
let ipc = CoreAPI.getIPC();
|
|
494
512
|
//Listen for IPC information
|
|
495
|
-
let ipcListener = function (args
|
|
496
|
-
|
|
497
|
-
ipc.sendResponse(context, "Received");
|
|
513
|
+
let ipcListener = function (args) {
|
|
514
|
+
console.log('on', channel1, 'received', args);
|
|
498
515
|
}
|
|
499
|
-
ipc.on(
|
|
516
|
+
ipc.on(channel1, ipcListener);
|
|
500
517
|
```
|
|
501
518
|
|
|
502
519
|
- Return result example:
|
|
503
520
|
|
|
504
|
-
```json
|
|
505
|
-
{
|
|
506
|
-
"event": "event-2",
|
|
507
|
-
"msg": "msg-2"
|
|
508
|
-
}
|
|
509
521
|
```
|
|
522
|
+
on channel1 received: "no callback"
|
|
523
|
+
```
|
|
524
|
+
|
|
510
525
|
|
|
511
526
|
|
|
512
527
|
# Database API
|
|
@@ -973,9 +988,9 @@ Receive device commands
|
|
|
973
988
|
driver.onCommand(listener)
|
|
974
989
|
```
|
|
975
990
|
|
|
976
|
-
- listener:
|
|
991
|
+
- listener: function listener(command, respond), call respond after processing command
|
|
977
992
|
- command: command, example: {cmd: "connect", args: ""}
|
|
978
|
-
-
|
|
993
|
+
- respond: used to send data back to Device.sendCommand's callback function
|
|
979
994
|
|
|
980
995
|
## SDK
|
|
981
996
|
|
|
@@ -1000,7 +1015,7 @@ device.init([opts], callback)
|
|
|
1000
1015
|
|
|
1001
1016
|
* opts: optional parameters, passed to the driver loading process, refer
|
|
1002
1017
|
to [Driver Template](#download-project-template)
|
|
1003
|
-
* callback:
|
|
1018
|
+
* callback: function(result, error)
|
|
1004
1019
|
|
|
1005
1020
|
##### Example:
|
|
1006
1021
|
|
|
@@ -1023,7 +1038,7 @@ device.sendCommand(command, callback)
|
|
|
1023
1038
|
```
|
|
1024
1039
|
|
|
1025
1040
|
- command: command, example: command = {cmd: "connect", args: ""}
|
|
1026
|
-
- callback:
|
|
1041
|
+
- callback: function callback(result), used to receive the response from driver
|
|
1027
1042
|
|
|
1028
1043
|
##### Example:
|
|
1029
1044
|
|
|
@@ -1039,8 +1054,7 @@ Receive data sent by the driver
|
|
|
1039
1054
|
device.onData(listener)
|
|
1040
1055
|
```
|
|
1041
1056
|
|
|
1042
|
-
- listener:
|
|
1043
|
-
- data: data
|
|
1057
|
+
- listener: function listener(data)
|
|
1044
1058
|
|
|
1045
1059
|
## Device driver relationship
|
|
1046
1060
|
|
|
@@ -1220,37 +1234,37 @@ class DriverTemplate extends CoreClass.Driver {
|
|
|
1220
1234
|
this.startBeat();
|
|
1221
1235
|
this.updateStatusInfo(CoreDefine.HEARTBEAT_STATUS.ALIVE, "alive");
|
|
1222
1236
|
|
|
1223
|
-
this.onCommand(({cmd, args},
|
|
1237
|
+
this.onCommand(({cmd, args}, respond) => {
|
|
1224
1238
|
// Listen to the messages sent by the device SDK, process the different commands in the message body,
|
|
1225
1239
|
// and respond back to the device SDK through the callback function.
|
|
1226
1240
|
switch (cmd) {
|
|
1227
1241
|
case CMD.CONNECT:
|
|
1228
|
-
this.connect(args,
|
|
1242
|
+
this.connect(args, respond);
|
|
1229
1243
|
break;
|
|
1230
1244
|
case CMD.CUSTOM_CMD:
|
|
1231
1245
|
this.customCmd(cmd, args);
|
|
1232
1246
|
break;
|
|
1233
1247
|
default:
|
|
1234
|
-
this.unsupported(cmd, args,
|
|
1248
|
+
this.unsupported(cmd, args, respond);
|
|
1235
1249
|
break;
|
|
1236
1250
|
}
|
|
1237
1251
|
});
|
|
1238
1252
|
}
|
|
1239
1253
|
|
|
1240
|
-
unsupported(cmd, args,
|
|
1254
|
+
unsupported(cmd, args, respond) {
|
|
1241
1255
|
let result = {};
|
|
1242
1256
|
result.msg = "cmd: " + cmd + " is unsupported.";
|
|
1243
1257
|
result.status = false;
|
|
1244
1258
|
console.log(result.msg);
|
|
1245
|
-
|
|
1259
|
+
respond(result);
|
|
1246
1260
|
}
|
|
1247
1261
|
|
|
1248
|
-
connect(args,
|
|
1262
|
+
connect(args, respond) {
|
|
1249
1263
|
console.log("connect: received.");
|
|
1250
1264
|
let result = {};
|
|
1251
1265
|
result.msg = "return success.";
|
|
1252
1266
|
result.status = true;
|
|
1253
|
-
|
|
1267
|
+
respond(result);
|
|
1254
1268
|
}
|
|
1255
1269
|
|
|
1256
1270
|
// no response
|
|
@@ -1309,20 +1323,19 @@ import {CoreAPI} from '@cutos/core';
|
|
|
1309
1323
|
import {DriverTemplate} from '../src/driver-template.js';
|
|
1310
1324
|
|
|
1311
1325
|
CoreAPI.init(null, () => {
|
|
1312
|
-
|
|
1313
|
-
let device = new DriverTemplate('driver-template', ({result, msg}) => {
|
|
1326
|
+
let device = new DriverTemplate('driver-template')
|
|
1314
1327
|
console.log("driver template init", result, msg ? " error:" + msg : "");
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1328
|
+
device.init((result, error) => {
|
|
1329
|
+
device.onData((data) => {
|
|
1330
|
+
console.log("on data", data)
|
|
1331
|
+
});
|
|
1332
|
+
|
|
1333
|
+
device.connect((result) => {
|
|
1334
|
+
console.log("connect: ", result)
|
|
1335
|
+
});
|
|
1336
|
+
device.customCmd();
|
|
1337
|
+
device.sendCommand({cmd: "unknown", args: ""});
|
|
1322
1338
|
});
|
|
1323
|
-
device.customCmd();
|
|
1324
|
-
device.sendCommand({cmd: "unknown", args: ""});
|
|
1325
|
-
});
|
|
1326
1339
|
});
|
|
1327
1340
|
```
|
|
1328
1341
|
|
package/dist/cutos.cjs.js
CHANGED
|
@@ -385,7 +385,7 @@ const DEVICE = Object.freeze({ // core level api
|
|
|
385
385
|
NFC: 'device-nfc',
|
|
386
386
|
FINGERPRINT: 'device-fingerprint',
|
|
387
387
|
QR_CODE_SCANNER: 'device-qr-code-scanner',
|
|
388
|
-
PRINTER: 'device-printer'
|
|
388
|
+
PRINTER: 'device-printer',
|
|
389
389
|
});
|
|
390
390
|
|
|
391
391
|
const HEARTBEAT_STATUS = Object.freeze({
|
|
@@ -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.
|
|
401
|
+
var version = "3.4.0";
|
|
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";
|
|
@@ -918,7 +918,7 @@ class IPC {
|
|
|
918
918
|
/**
|
|
919
919
|
* listening channel
|
|
920
920
|
* @param channel
|
|
921
|
-
* @param listener (args,
|
|
921
|
+
* @param listener (args, respond(result)) args is the data from sender, respond is used to send data back to sender's callback
|
|
922
922
|
*/
|
|
923
923
|
on(channel, listener) {
|
|
924
924
|
if (listener) {
|
|
@@ -1189,7 +1189,7 @@ class Driver {
|
|
|
1189
1189
|
|
|
1190
1190
|
/**
|
|
1191
1191
|
* Receive device commands
|
|
1192
|
-
* @param listener(command,
|
|
1192
|
+
* @param listener(command, respond(result))
|
|
1193
1193
|
*/
|
|
1194
1194
|
onCommand(listener) {
|
|
1195
1195
|
if (listener) {
|
package/dist/cutos.esm.js
CHANGED
|
@@ -677,7 +677,7 @@ const DEVICE = Object.freeze({ // core level api
|
|
|
677
677
|
NFC: 'device-nfc',
|
|
678
678
|
FINGERPRINT: 'device-fingerprint',
|
|
679
679
|
QR_CODE_SCANNER: 'device-qr-code-scanner',
|
|
680
|
-
PRINTER: 'device-printer'
|
|
680
|
+
PRINTER: 'device-printer',
|
|
681
681
|
});
|
|
682
682
|
|
|
683
683
|
const HEARTBEAT_STATUS = Object.freeze({
|
|
@@ -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.
|
|
693
|
+
var version = "3.4.0";
|
|
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";
|
|
@@ -1210,7 +1210,7 @@ class IPC {
|
|
|
1210
1210
|
/**
|
|
1211
1211
|
* listening channel
|
|
1212
1212
|
* @param channel
|
|
1213
|
-
* @param listener (args,
|
|
1213
|
+
* @param listener (args, respond(result)) args is the data from sender, respond is used to send data back to sender's callback
|
|
1214
1214
|
*/
|
|
1215
1215
|
on(channel, listener) {
|
|
1216
1216
|
if (listener) {
|
|
@@ -1481,7 +1481,7 @@ class Driver {
|
|
|
1481
1481
|
|
|
1482
1482
|
/**
|
|
1483
1483
|
* Receive device commands
|
|
1484
|
-
* @param listener(command,
|
|
1484
|
+
* @param listener(command, respond(result))
|
|
1485
1485
|
*/
|
|
1486
1486
|
onCommand(listener) {
|
|
1487
1487
|
if (listener) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cutos/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
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",
|