Simulators
Simulators provide a virtual environment in which we can run and test various operating systems and software configurations without using actual physical hardware or executing in a real operating system environment.
The simulators used when developing drivers using simulators are mainly divided into two categories:
Serial port simulator: serialport simulator
Native library simulator: native lib simulator
The following article introduces the development of simulators using NFC driver as an example
Name | Description | Download |
---|---|---|
device-nfc-v3.3.0 | NFC device driver | Download |
Driver
- Add simulator file simulator/device-nfc-simulator.js
"use strict";
const fs = require('node:fs')
const path = require('node:path')
const {SerialPortSimulator} = require('@cutos/drivers')
const config = require('../config.json')
class DeviceNFCSimulator extends SerialPortSimulator {
constructor() {
super(config);
}
getInfo() {
let readme = fs.readFileSync(path.join(__dirname, 'README.md'), 'utf-8')
return {readme}
}
}
module.exports = {DeviceNFCSimulator};
- Driver file device-nfc.js
"use strict";
const {CoreDefine, CoreClass} = require('@cutos/core');
const {SerialPort} = require("@cutos/drivers");
const {TYPE, CMD} = require('./device-nfc-def.js');
const {DeviceNFCSimulator} = require("./simulator/device-nfc-simulator");//导入模拟器
const {opts} = require('./config.json');
class DeviceNFC extends CoreClass.Driver {
constructor(name) {
super(name, TYPE);
//name is 'mock' to use the simulator
if (name === 'mock') {
this.simulator = new DeviceNFCSimulator()
}
this.startBeat();
this.updateStatusInfo(CoreDefine.HEARTBEAT_STATUS.ALIVE, "alive");
this.onCommand(({cmd, args}, callback) => {
switch (cmd) {
case CMD.CONNECT:
this.connect(args, callback);
break;
}
});
}
connect(args, callback) {
let params = {...opts, ...args}
if (this.port) {
callback({status: true, msg: params})
}
//Pass in the simulator parameter to associate the simulator with the serial port
this.port = new SerialPort(params.path, params.baudRate, err => {
if (err) {
callback({status: false, msg: err.message})
} else {
callback({status: true, msg: params})
}
}, this.simulator)
this.port.on('data', data => this.onSerialData(data))
}
onSerialData(data) {
let code = ''
data = data.slice(2,)
for (let d of data) {
let hex = Number(d).toString(16)
hex = ('0' + hex).substring(hex.length - 1)
code += hex
}
this.sendData({id: code})
}
}
module.exports = DeviceNFC;
sdk
import {CoreAPI} from '@cutos/core';
import {DeviceNFC} from '../src/device-nfc.js';
CoreAPI.init('localhost', () => {
//Instantiate and pass in `'mock'` to start the simulator
let device = new DeviceNFC('mock');
device.init((result, error) => {
if (error) {
console.log(error)
return;
}
device.connect('/dev/ttySS2', (result, error) => {
console.log('Connect', result, error)
});
device.onData(data => {
console.log(data)
})
})
});
Simulator console
CUTOS uploads the driver, and after publishing lwa, you can open the simulator console: http://+ CUTOS IP address +:3000/webapps/simulator/
, for example
http://192.168.1.105:3000/webapps/simulator/ Select the corresponding simulator to send simulation instructions.
Simulator list
Select the corresponding simulator to send simulation instructions