Skip to content

Commit

Permalink
add command line
Browse files Browse the repository at this point in the history
  • Loading branch information
lsongdev committed Sep 13, 2018
1 parent 3db0200 commit 64ac734
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 10 deletions.
75 changes: 70 additions & 5 deletions HCHO.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,54 @@ const assert = require('assert');
const SerialPort = require('serialport');
const createReader = require('./reader');

const COMMANDS = {
QUERY: 0x01
};

const TYPES = {
0x00: 'NoSensor',
0x01: 'CO',
0x02: 'H2S',
0x03: 'CH4',
0x04: 'CL2',
0x05: 'HCL',
0x06: 'F2',
0x07: 'HF',
0x08: 'NH3',
0x09: 'HCN',
0x0a: 'PH3',
0x0b: 'NO',
0x0c: 'NO2',
0x0d: 'O3',
0x0e: 'O2',
0x0f: 'SO2',
0x10: 'CLO2',
0x11: 'COCL2',
0x12: 'PH3',
0x13: 'SiH4',
0x14: 'HCHO',
0x15: 'CO2',
0x16: 'VOC',
0x17: 'ETO',
0x18: 'C2H4',
0x19: 'C2H2',
0x1a: 'SF6',
0x1b: 'AsH3',
0x1c: 'H2',
0x1d: 'TOX1',
0x1e: 'TOX2',
0x1f: 'L/M',
0x20: 'BattryLevel',
};

const UNITS = {
0x01: 'ppm',
0x02: 'VOL',
0x03: 'LEL',
0x04: 'Ppb',
0x05: 'Mg/m3',
};

function parse(buffer){
const data = {
HEAD : buffer.readUInt16BE(0),
Expand All @@ -18,7 +66,15 @@ function parse(buffer){
}
assert.equal(data.HEAD, 0x424d, 'Invalid data header');
assert.equal(checksum, data.CHECKSUM, 'Checksum error');
return data;
return {
data,
type: TYPES[data.TYPE],
unit: UNITS[data.UNIT],
value: data.VALUE / 10 ** data.VH,
toString(){
return `${this.type}: ${this.value}${this.unit}`;
}
};
}

class HCHO extends SerialPort {
Expand All @@ -34,11 +90,20 @@ class HCHO extends SerialPort {
}
send(cmd, data){
let checksum = 0;
if(typeof cmd === 'undefined')
throw new TypeError('cmd must be a number or string');
if(typeof data === 'undefined')
throw new TypeError('data must be a number or string');
if(typeof cmd === 'string'){
cmd = COMMANDS[`${cmd}`.toUpperCase()];
}
cmd = parseInt(cmd);
data = parseInt(data);
const packet = Buffer.alloc(7);
packet.writeUIntBE(0x42, 0); checksum+=0x42;
packet.writeUIntBE(0x4d, 1); checksum+=0x4d;
packet.writeUIntBE(cmd, 2); checksum+=cmd;
packet.writeUInt16BE(data, 3); checksum+=data;
packet.writeUIntBE(0x42, 0, 1); checksum+=0x42;
packet.writeUIntBE(0x4d, 1, 1); checksum+=0x4d;
packet.writeUIntBE(cmd, 2, 1); checksum+=cmd;
packet.writeUInt16BE(data, 3); checksum+=data;
packet.writeUInt16BE(checksum, 5);
return this.write(packet);
}
Expand Down
29 changes: 29 additions & 0 deletions bin/dsensor
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env node

const program = require('kelp-argv');

program()
.command('help', () => {
console.log();
console.log('~$ dsensor <command> [options]');
console.log('~$ dsensor send --type=HCHO --device=/dev/cu.SLAB_USBtoUART --command=query --data=0x00');
})
.command('send', ({ type, device, command, data, watch }) => {
const Adapter = require(`../${type}`);
device = new Adapter(device);
device.on('message', message => {
console.log(message.toString());
if(!watch) device.close();
});
device.on('open', () => {
if(watch){
setInterval(() => {
device.send(command, data);
}, 3000);
}else{
device.send(command, data);
}
});
})
.parse();

3 changes: 0 additions & 3 deletions bin/sensor

This file was deleted.

9 changes: 8 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ const hc = new HCHO("/dev/cu.SLAB_USBtoUART");

hc.on('message', message => {
const { VALUE, VH } = message;
console.log('HCHO: %smg/m3', (VALUE / 10**VH).toFixed(2));
console.log(VALUE / 10 ** VH, VALUE);
});

hc.on("open", () => {
setInterval(() => {
hc.send(0x01, 0x00);
}, 3000)
});



function add(a, b, c){
c = a + b;
return c;
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
"name": "dsensor",
"version": "1.0.0",
"main": "index.js",
"bin": {
"dsensor": "bin/dsensor"
},
"repository": {
"url": "git@github.com:song940/dsensor.git",
"type": "git"
},
"author": "Lsong <song940@gmail.com>",
"license": "MIT",
"dependencies": {
"serialport": "^7.0.2"
"kelp-argv": "latest",
"serialport": "latest"
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,16 @@ isobject@^3.0.0, isobject@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"

kelp-argv@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/kelp-argv/-/kelp-argv-1.0.1.tgz#c1495ef2ccaf1514899d1dd40612e0e4781433a0"
dependencies:
kelp-cli "^1.0.0"

kelp-cli@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/kelp-cli/-/kelp-cli-1.0.2.tgz#ba1b6034d50bf6f4f2f3be2984d8fce983f05b29"

kind-of@^3.0.2, kind-of@^3.0.3:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
Expand Down

0 comments on commit 64ac734

Please sign in to comment.