Device&Diver API
Driver
Driver
Constructor
const driver = new Driver(name = 'default-driver-name', type = cutosAPI.DEVICE.DEFAULT, counter = 10)
- name: driver name, default is 'default-driver-name'
- type: device type, default is cutosAPI.DEVICE.DEFAULT
- counter: heartbeat interval, default is 10s
Driver.sendData
Send data to the device
driver.sendData(data)
- data: data
Driver.onCommand
Receive device commands
driver.onCommand(listener)
- listener: listen callback function (command, [callback]), call callback after processing command
- command: command, example: {cmd: "connect", args: ""}
- callback: callback function (result)
SDK
Device
Constructor
const device = new (name = 'default-device-name', type = cutosAPI.DEVICE.DEFAULT)
- name: device name, default is 'default-device-name'
- type: device type, default is cutosAPI.DEVICE.DEFAULT
Device.init
Device initialization
device.init([opts], callback)
- opts: optional parameters, passed to the driver loading process, refer to Driver Template
- callback: callback function
Example:
device.init((result, error) => {
if (error) {
console.log(error)
return;
}
console.log(result)
});
Device.sendCommand
Send a command to the driver
device.sendCommand(command, callback)
- command: command, example: command = {cmd: "connect", args: ""}
- callback: callback function, you can pass a listening callback function to listen to the device status
Example:
device.sendCommand({cmd: "connect", args: ""}, Listener(msg))
Device.onData
Receive data sent by the driver
device.onData(listener)
- listener: listener callback function (data)
- data: data
Device driver relationship
1. onData and sendData
2. sendCommand (without callback function) and onCommand
3. sendCommand (with callback function) and onCommand
Device driver management template
Download project template
Name | Description | Download |
---|---|---|
driver-template | Device driver template | Download |
Project structure
├── driver/ # Driver
│ ├── src/ # Driver source directory
│ ├── config.json # Configuration file, configure related parameters
│ ├── driver-template.js # Driver template
│ ├── driver-template-def.js # Device driver data definition (same as in SDK)
│ ├── index.js # Driver loading file
│ ├── test/ # Test directory
│ ├── main.js # Test entry file, simulate cutos, create a driver service
│ ├── package.json # Configuration information for this project
│ ├── gulpfile.js # Packaging file
│ ├── readme.txt # Instructions
├── sdk/ # Interface SDK
│ ├── src/ # Source code directory
│ ├── driver-template.js # Device template
│ ├── driver-template-def.js # Device driver data definition (same as in driver)
│ ├── test/ # Test directory
│ ├── main.js # Test entry file
│ ├── package.json # Configuration information for this project
│ ├── gulpfile.mjs # Packaging file
│ ├── readme.txt # Instructions
Installation dependencies
Note: node version >= 16
cd driver-template/driver
npm install
cd driver-template/sdk
npm install
Project startup
You need to start the driver first, then start the SDK.
npm start
If the startup is successful, the console will return the following content
- driver
CUTOS CORE connected.
CUTOS Simulator started and listening on port 1883
drvDefault onCommand
{
"cmd":"init",
"args":{"name":"driver-template","type":"driver-template","development":true},
"topicResponse":"device-channel-driver-template-response"
}
connect: received.
cmd: custom-cmd received.
cmd: unknown is unsupported.
- SDK
CUTOS CORE connected.
driver template init true
connect: { msg: 'return success.', status: true }
Packaging and Release
Packaging Driver
cd driver-template/driver
npm run build
├── driver/
│ ├── dist/ # Packaging directory
│ ├── driver-template-v1.0.1.drv # Packaging file
Release the driver
Upload the package file to https://www.cut-os.com/ and publish Hardware/Driver--Add
Packaged Device SDK
cd driver-template/sdk
npm run build
├── sdk/
│ ├── dist/ # Packaging directory
│ ├── driver-template-sdk-v1.0.2.zip # Packaging files
Note:
1.The template device-template.js references TYPE, CMD exported in device-template-def.js
import {CMD, TYPE} from './device-template-def.js';
Device type (TYPE):
Type constant: key "TEMPLATE", value "Device template".
- Indicates the template type, identifying the category of the device.
Command type (CMD):
CMD constant with two pairs:
CONNECT: 'connect' represents the connection command.
- CUSTOM_CMD: "custom-cmd" represents a custom command for a specific device.
2.Configuration requirements: Keep the TYPE parameter 3 in the template consistent so that the driver matches the corresponding SDK.
let device = new DeviceTemplate('device-template',callback)
//device-template.js
const TYPE = 'device-template';
//device-template-def.js
{"name": "device-template",...}
//config.json
Review
The complete code is as follows:
driver
// driver/src/driver-template.js
const {CoreDefine, CoreClass} = require('@cutos/core');
const {TYPE, CMD} = require('./driver-template-def.js');
const config = require('./config.json');
class DriverTemplate extends CoreClass.Driver {
constructor(args) {
// check
if (TYPE !== config.type) {
throw "Error: the 'type' value in config.js and *-def.js MUST be identical.";
}
super(args.name, TYPE);
this.startBeat();
this.updateStatusInfo(CoreDefine.HEARTBEAT_STATUS.ALIVE, "alive");
this.onCommand(({cmd, args}, callback) => {
// Listen to the messages sent by the device SDK, process the different commands in the message body,
// and respond back to the device SDK through the callback function.
switch (cmd) {
case CMD.CONNECT:
this.connect(args, callback);
break;
case CMD.CUSTOM_CMD:
this.customCmd(cmd, args);
break;
default:
this.unsupported(cmd, args, callback);
break;
}
});
}
unsupported(cmd, args, callback) {
let result = {};
result.msg = "cmd: " + cmd + " is unsupported.";
result.status = false;
console.log(result.msg);
callback(result);
}
connect(args, callback) {
console.log("connect: received.");
let result = {};
result.msg = "return success.";
result.status = true;
callback(result);
}
// no response
customCmd(cmd, args) {
console.log("cmd: " + cmd + args, " received.");
// process cmd below
}
sendCustomData() {
let data = {};
data.type = "custom-type"; // custom defined data type
data.values = {}; // values from template driver
data.values.val = "any value";
data.values.timeStamp = Date.now();
this.sendData(data);
}
}
module.exports = DriverTemplate;
SDK
// sdk/src/driver-template.js
import {CoreClass} from '@cutos/core';
import {CMD, TYPE} from './driver-template-def.js';
class DriverTemplate extends CoreClass.Device {
constructor(name, callback, opts = {development: true}) {
super(name, TYPE, callback, opts);
this.onData((data) => {
console.log("on data", data)
});
}
connect(callback) {
let cmdMessage = {cmd: CMD.CONNECT};
this.sendCommand(cmdMessage, callback);
}
customCmd() {
let cmdMessage = {cmd: CMD.CUSTOM_CMD, args: ""};
this.sendCommand(cmdMessage);
}
}
export {DriverTemplate};
// sdk/test/main.js
import {CoreAPI} from '@cutos/core';
import {DriverTemplate} from '../src/driver-template.js';
CoreAPI.init(null, () => {
//
let device = new DriverTemplate('driver-template', ({result, msg}) => {
console.log("driver template init", result, msg ? " error:" + msg : "");
device.onData((data) => {
console.log("on data", data)
});
device.connect((result) => {
console.log("connect: ", result)
});
device.customCmd();
device.sendCommand({cmd: "unknown", args: ""});
});
});