Device&Diver API

驱动

安装

npm install @cutos/core 本地开发时安装,运行时CUTOS系统自带Core,无需安装;

引入依赖

const {CoreDefine, CoreClass} = require('@cutos/core');

Driver

构造函数

const driver = new Driver(name = 'default-driver-name', type = cutosAPI.DEVICE.DEFAULT, counter = 10)
  • name: 驱动名称,默认为'default-driver-name'
  • type: 设备类型,默认为cutosAPI.DEVICE.DEFAULT
  • counter: 心跳间隔,默认10s

Driver.sendData

向设备发送数据

driver.sendData(data)
  • data: 数据

Driver.onCommand

接收设备命令

driver.onCommand(listener)
  • listener: 监听回调函数(command, [callback]), 处理command后调用callback
    • command: 命令, 举例: {cmd: "connect", args: ""}
    • callback: 回调函数(result)

SDK

安装

npm install @cutos/core

引入依赖

const {CoreDefine, CoreClass} = require('@cutos/core');

Device

构造函数

const device = new (name = 'default-device-name', type = cutosAPI.DEVICE.DEFAULT)
  • name: 设备名,默认为'default-device-name'
  • type: 设备类型,默认为cutosAPI.DEVICE.DEFAULT

Device.init

设备初始化

  device.init([opts], callback)
  • opts: 可选参数, 传递给驱动加载过程,参考驱动模版
  • callback: 回调函数
举例:
  device.init((result, error) => {
  if (error) {
    console.log(error)
    return;
  }
  console.log(result)
});

Device.sendCommand

向驱动发送命令

 device.sendCommand(command, callback)
  • command:命令,举例: command = {cmd: "connect", args: ""}
  • callback:回调函数,可以传递一个监听的回调函数,监听设备状态
举例:
 device.sendCommand({cmd: "connect", args: ""}, Listener(msg))

Device.onData

接收驱动发来的数据

 device.onData(listener)
  • listener: 监听回调函数(data)
  • data: 数据

设备&驱动 关系

1. onData与sendData

ddr1.png

2.sendCommand(不含回调函数)与onCommand

ddr2.png

3.sendCommand(含回调函数)与onCommand

ddr3.png

设备&驱动 模版

下载项目模版

名称 描述 下载
driver-template 设备驱动模版 下载

项目结构

├── driver/                         # 驱动
│   ├── src/                        # 驱动源码目录
│       ├── config.json             # 配置文件,配置相关参数
│       ├── driver-template.js      # 驱动模版
│       ├── driver-template-def.js  # 设备驱动数据定义(与sdk中一样)
│       ├── index.js                # 驱动加载文件
│   ├── test/                       # 测试目录
│       ├── main.js                 # 测试入口文件,模拟cutos,创建一个驱动服务  
│   ├── package.json                # 本项目的配置信息
│   ├── gulpfile.js                 # 打包文件
│   ├── readme.txt                  # 说明书
├── sdk/                            # 接口SDK
│   ├── src/                        # 源码目录
│       ├── driver-template.js      # 设备模版
│       ├── driver-template-def.js  # 设备驱动数据定义(与driver中一样)
│   ├── test/                     # 测试目录  
│       ├── main.js                 # 测试入口文件
│   ├── package.json                # 本项目的配置信息
│   ├── gulpfile.mjs                # 打包文件
│   ├── readme.txt                  # 说明书

安装依赖

注意: node 版本 >= 16

cd driver-template/driver
npm install
cd driver-template/sdk
npm install

项目启动

需要先启动driver,再启动SDK。

npm start

启动成功,控制台将返回以下内容

  • 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 }

打包与发布

打包驱动
cd driver-template/driver
npm run build
├── driver/ 
│   ├── dist/                               # 打包目录
│       ├── driver-template-v1.0.1.drv      # 打包文件
发布驱动

将打包文件上传 https://www.cut-os.com/ 并发布 硬件/驱动--添加

driverUpload.png driverUpload1.png

打包设备SDK
cd driver-template/sdk
npm run build
├── sdk/ 
│   ├── dist/                               # 打包目录
│       ├── driver-template-sdk-v1.0.2.zip  # 打包文件
注意:

1.模版 driver-template.js 引用 driver-template-def.js 中导出的TYPE, CMD

import {CMD, TYPE} from './driver-template-def.js';

  • 设备类型(TYPE):

  • 类型常量: 键“TEMPLATE”,值“设备模板”。

    • 指示模板类型,标识设备的类别。
  • 命令类型(CMD):

  • 具有两对的 CMD 常数:

  • CONNECT: 'connect'代表连接命令。

     - CUSTOM_CMD: “custom-cmd”表示针对特定设备的自定义命令。
    

2.配置要求: 模版中TYPE参数3处保持一致,使driver与对应SDK相匹配。

let device = new DriverTemplate('driver-template') //driver-template.js

const TYPE = 'driver-template';//driver-template-def.js

{"name": "driver-template",...}//config.json

回顾

完整代码如下:

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: ""});
  });
});

results matching ""

    No results matching ""