初始化提交
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
const chokidar = require('chokidar');
|
||||
|
||||
let watchedPath = {};
|
||||
|
||||
const watch = function(inPath) {
|
||||
if (watchedPath[watchedPath]) {
|
||||
return;
|
||||
}
|
||||
watchedPath[inPath] = chokidar.watch(inPath, {
|
||||
persistent: true,
|
||||
depth: 0,
|
||||
ignoreInitial: true
|
||||
});
|
||||
|
||||
watchedPath[inPath].on('add', (actionPath, stats) => {
|
||||
self.postMessage({
|
||||
watcher: inPath,
|
||||
event: 'add',
|
||||
path: actionPath,
|
||||
stats
|
||||
});
|
||||
});
|
||||
|
||||
watchedPath[inPath].on('addDir', (actionPath, stats) => {
|
||||
self.postMessage({
|
||||
watcher: inPath,
|
||||
event: 'addDir',
|
||||
path: actionPath,
|
||||
stats
|
||||
});
|
||||
});
|
||||
|
||||
watchedPath[inPath].on('unlink', (actionPath, stats) => {
|
||||
self.postMessage({
|
||||
watcher: inPath,
|
||||
event: 'unlink',
|
||||
path: actionPath,
|
||||
stats
|
||||
});
|
||||
});
|
||||
|
||||
watchedPath[inPath].on('unlinkDir', (actionPath, stats) => {
|
||||
self.postMessage({
|
||||
watcher: inPath,
|
||||
event: 'unlinkDir',
|
||||
path: actionPath,
|
||||
stats
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const unwatch = function(inPath) {
|
||||
if (!watchedPath[inPath]) {
|
||||
return;
|
||||
}
|
||||
watchedPath[inPath].close();
|
||||
delete watchedPath[inPath];
|
||||
}
|
||||
|
||||
self.addEventListener('message', function(event) {
|
||||
if (event.data.func === 'watch') {
|
||||
watch(...event.data.args);
|
||||
} else if (event.data.func === 'unwatch') {
|
||||
unwatch(...event.data.args);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
importScripts('../common/serial-worker.js');
|
||||
const lodash_fp = require('lodash/fp');
|
||||
const child_process = require('node:child_process');
|
||||
const serialport = require('serialport');
|
||||
|
||||
const {
|
||||
SerialPort,
|
||||
ReadlineParser,
|
||||
ByteLengthParser
|
||||
} = serialport;
|
||||
|
||||
const portsOperator = {};
|
||||
|
||||
class NodeSerialWorker extends SerialWorker {
|
||||
#serialport_ = null;
|
||||
#parserBytes_ = null;
|
||||
constructor(port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
#addEventsListener_() {
|
||||
this.#parserBytes_.on('data', (buffer) => {
|
||||
this.onBuffer(buffer);
|
||||
});
|
||||
|
||||
this.#serialport_.on('error', (error) => {
|
||||
this.onError(error);
|
||||
this.onClose(1);
|
||||
});
|
||||
|
||||
this.#serialport_.on('open', () => {
|
||||
this.onOpen();
|
||||
});
|
||||
|
||||
this.#serialport_.on('close', () => {
|
||||
this.onClose(1);
|
||||
});
|
||||
}
|
||||
|
||||
open() {
|
||||
super.open();
|
||||
this.#serialport_ = new SerialPort({
|
||||
path: this.getPortName(),
|
||||
baudRate: this.getBaudRate() - 0, // 波特率
|
||||
dataBits: 8, // 数据位
|
||||
parity: 'none', // 奇偶校验
|
||||
stopBits: 1, // 停止位
|
||||
flowControl: false,
|
||||
autoOpen: false // 不自动打开
|
||||
}, false);
|
||||
this.#parserBytes_ = this.#serialport_.pipe(new ByteLengthParser({ length: 1 }));
|
||||
this.#serialport_.open((error) => {
|
||||
if (error) {
|
||||
this.onError(error);
|
||||
// this.onClose(1);
|
||||
}
|
||||
});
|
||||
this.#addEventsListener_();
|
||||
}
|
||||
|
||||
close() {
|
||||
super.close();
|
||||
if (this.isOpened()) {
|
||||
try {
|
||||
this.#serialport_.close();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onBuffer(buffer) {
|
||||
super.onBuffer(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
const create = (port) => {
|
||||
if (!portsOperator[port]) {
|
||||
portsOperator[port] = new NodeSerialWorker(port);
|
||||
}
|
||||
portsOperator[port].open();
|
||||
}
|
||||
|
||||
self.addEventListener('message', function(event) {
|
||||
console.log(event.data);
|
||||
const { port, type } = event.data;
|
||||
if (type === 'open') {
|
||||
create(port);
|
||||
} else if (type === 'close') {
|
||||
portsOperator[port] && portsOperator[port].close();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user