forked from bytesnz/vproweather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
241 lines (227 loc) · 6.46 KB
/
index.tsx
File metadata and controls
241 lines (227 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import chalk from "chalk";
import * as boxen from "boxen";
import yargs from "yargs/yargs";
import SerialPort from "serialport";
const log = console.log;
function logError(content: string) {
log(chalk`{redBright ${content}}`);
}
function logSuccess(content: string) {
log(chalk`{green ${content}}`);
}
function logWarn(content: string) {
log(chalk`{yellowBright ${content}}`);
}
const CR = 0x0d;
const LF = 0x0a;
const ACK = 0x06;
const ACK_STR = "\x06";
const NAK = 0x21;
const NAK_STR = "\x21";
const CANCEL = 0x18;
const CANCEL_STR = "\x18";
const ESC = 0x1b;
const ESC_STR = "\x1b";
const READ_WRITE_BUF_LEN = 4200;
let readBuffer: Buffer | undefined;
let readBufferIdx: number;
/**
* Remove trailing 0x00 from a buffer
*
* @param buf source buffer
*/
function trimBufferEnd(buf: Buffer): Buffer {
let idx = 0;
for (let i = buf.length - 1; i >= 0; i--) {
if (buf[i] !== 0x00) {
idx = i;
break;
}
}
return buf.slice(0, idx + 1);
}
/**
* Read bytes from serial until buffer is empty, return true while characters are available
*/
function readAllIncomingBytes(): Buffer | boolean {
if (!readBuffer) {
readBuffer = Buffer.alloc(READ_WRITE_BUF_LEN);
readBufferIdx = 0;
}
const nextChar = vpro.read();
if (nextChar && nextChar.constructor === Buffer) {
nextChar.copy(readBuffer, readBufferIdx);
readBufferIdx++;
return true;
}
const buf = trimBufferEnd(readBuffer);
readBuffer = undefined;
return buf;
}
/**
* Wakes up the weather station per the Davis specs
*/
async function wakeUpStation(): Promise<void> {
return new Promise((resolve, reject) => {
vpro.write('\r', (err) => {
if (err) {
if (verbose) {
logError(`Could not wake up weather station`);
}
reject();
}
if (verbose) {
logSuccess(`Woke up weather station`);
}
vpro.drain();
setTimeout(() => {
resolve();
}, 200);
});
});
}
/**
* Turn display backlight on/off
*
* @param turnOn desired backlight state
*/
function switchBacklight(turnOn: boolean) {
if (verbose) {
log(`Turning backlight ${turnOn ? 'on' : 'off'}...`);
}
const buf = Buffer.from(`LAMPS ${turnOn ? '1' : '0'}\n`, 'ascii');
vpro.write(buf, (err) => {
if (err) {
logError(`Failed to turn backlight ${turnOn ? 'on' : 'off'}`);
return;
}
vpro.drain();
if (verbose) {
logSuccess(`Turned backlight ${turnOn ? 'on' : 'off'}`);
}
})
}
/**
* Get display firmware version
*/
function getFirmwareVersion() {
if (verbose) {
log(`Getting firmware version...`);
const buf = Buffer.from(`VER\n`, 'ascii');
vpro.write(buf, (err) => {
if (err) {
logError(`Failed to get firmware version`);
return;
}
vpro.drain();
vpro.on('readable', () => {
setTimeout(() => {
let res: Buffer | boolean = true;
while (res === true) {
res = readAllIncomingBytes();
}
if (res.constructor === Buffer) {
console.log(`
Data: ${res.toString('hex')}
Length: ${res.length}
`);
}
process.exit(0);
}, 1000);
});
});
}
}
/**
* Get display model
*/
function getModel() {
if (verbose) {
log(`Getting model...`);
const buf = Buffer.from(`WRD\x12\x4d\n`, 'ascii');
vpro.write(buf, (err) => {
if (err) {
logError(`Failed to get firmware version`);
return;
}
vpro.drain();
vpro.on('readable', () => {
setTimeout(() => {
const readBuf = vpro.read(4);
if (readBuf && readBuf.constructor === Buffer) {
const modelCode = readBuf.readUInt8(3);
let model: string;
switch (modelCode) {
case 0: model = 'Wizard III'; break;
case 1: model = 'Wizard II'; break;
case 2: model = 'Monitor'; break;
case 3: model = 'Perception'; break;
case 4: model = 'GroWeather'; break;
case 5: model = 'Energy Environmonitor'; break;
case 6: model = 'Health Environmonitor'; break;
case 16: model = 'Vantage Pro'; break;
default: model = 'Unknown model'; break;
}
logSuccess(`Display model: ${model}`);
}
process.exit(0);
}, 2000);
});
});
}
}
const argv = yargs(process.argv.slice(2))
.scriptName('vproweather')
.usage('$0 <cmd> [args]')
.option('p', {
alias: 'port',
describe: 'Port the Vantage Pro Weather Station is connected to',
demandOption: 'The port is required',
type: 'string',
nargs: 1,
})
.option('verbose', {
describe: 'Show verbose output',
type: 'boolean'
})
.option('b', {
alias: 'set-backlight',
describe: 'turn backlight on/off',
type: 'number',
nargs: 1,
})
.option('f', {
alias: 'firmware-version',
describe: 'Query for Davis firmware version string',
})
.option('m', {
alias: 'model',
describe: 'Query for weather station model',
})
.describe('version', "Show version number")
.help().argv;
const { p: port, verbose, f: firmware, bk: backlight, m: model } = argv;
const vpro = new SerialPort(port, {
baudRate: 19200,
});
vpro.on('open', async () => {
if (verbose) {
logSuccess(`Serial port opened`);
}
// wake up station
await wakeUpStation();
if (firmware) {
getFirmwareVersion();
}
else if (backlight !== undefined) {
if (backlight === 0) {
switchBacklight(false);
}
else {
switchBacklight(true);
}
}
else if (model) {
getModel();
}
});