-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (50 loc) · 1.57 KB
/
index.js
File metadata and controls
56 lines (50 loc) · 1.57 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
const notifier = require('node-notifier');
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const name_of_device = "CH340";
const baudRate = 115200;
let selected_port;
async function find_port() {
const ports = await SerialPort.list();
let found_port;
ports.forEach(port => {
if (port.friendlyName.toLowerCase().includes(name_of_device.toLowerCase())) {
found_port = port.path;
}
});
if (!found_port) {
console.log('No port selected.\nListing ports:');
ports.forEach(port => { console.log(port.path + ' - ' + port.friendlyName) });
} else {
return found_port;
}
}
function notify(serial_data) {
if (serial_data.startsWith('$s$')) {
let text = serial_data.split('$*$');
const title = text[1];
const message = text[2];
notifier.notify({
title,
message,
icon: 'No Image',
sound: true,
timeout: 4,
appID: '🔔📱 ' + text[0].substring(3),
urgency: 'critical',
actions: ['OK', 'Cancel']
});
notifier.on('click', function () {
console.log('Clicked');
});
}
}
async function main() {
selected_port = await find_port();
if (!selected_port) return;
console.log('Selected port: ' + selected_port);
const port = new SerialPort({ path: selected_port, baudRate: baudRate });
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }));
parser.on('data', notify)
}
main()