Add delay option to dirWatch type=default

This commit is contained in:
Chrystian Huot 2020-05-26 13:41:08 -04:00
parent f2ed09d183
commit 9833900751
2 changed files with 78 additions and 22 deletions

View file

@ -11,6 +11,11 @@ The *rdioScanner.dirWatch* property an *array* of *dirWatch objects* which descr
"rdioScanner": { "rdioScanner": {
"dirWatch": [ "dirWatch": [
{ {
// [optional]
// Works only for type="default"
// Delay the import until this number of milliseconds without any modification of the file
"delay": 3000
// [optional] Whether the audio is deleted after importation (defaults to false) // [optional] Whether the audio is deleted after importation (defaults to false)
"deleteAfter": true, "deleteAfter": true,

View file

@ -28,8 +28,6 @@ class DirWatch {
constructor(app = {}) { constructor(app = {}) {
this.app = app; this.app = app;
this.watchers = [];
this.app.config.dirWatch.forEach((dirWatch = {}) => { this.app.config.dirWatch.forEach((dirWatch = {}) => {
if (typeof dirWatch.directory !== 'string' || !dirWatch.directory.length) { if (typeof dirWatch.directory !== 'string' || !dirWatch.directory.length) {
console.error(`Invalid dirWatch.directory: ${dirWatch.directory}`); console.error(`Invalid dirWatch.directory: ${dirWatch.directory}`);
@ -58,22 +56,46 @@ class DirWatch {
usePolling: typeof dirWatch.usePolling === 'boolean' ? dirWatch.usePolling : false, usePolling: typeof dirWatch.usePolling === 'boolean' ? dirWatch.usePolling : false,
}); });
watcher.on('add', (filename) => {
switch (dirWatch.type) { switch (dirWatch.type) {
case 'trunk-recorder': case 'trunk-recorder':
this.importTrunkRecorderType(dirWatch, filename); watcher.on('add', (filename) => this.importTrunkRecorderType(dirWatch, filename));
break; break;
case 'sdrtrunk': case 'sdrtrunk':
this.importSdrTrunkType(dirWatch, filename); watcher.on('add', (filename) => this.importSdrTrunkType(dirWatch, filename));
break; break;
default: default:
if (typeof dirWatch.delay === 'number' && dirWatch.delay >= 100) {
const debounces = {};
const debounceFn = (filename) => setTimeout(() => {
if (this.exists(filename)) {
if (this.statFile(filename).size > 44) {
delete debounces[filename];
this.importDefaultType(dirWatch, filename); this.importDefaultType(dirWatch, filename);
} else {
debounces[filename] = debounceFn(filename);
} }
}
}, dirWatch.delay);
watcher.on('raw', (_, filename) => {
filename = path.resolve(dir, filename);
if (debounces[filename]) {
clearTimeout(debounces[filename]);
}
debounces[filename] = debounceFn(filename);
}); });
this.watchers.push(watcher); } else {
watcher.on('add', (filename) => this.importDefaultType(dirWatch, filename));
}
}
}); });
} }
@ -81,13 +103,13 @@ class DirWatch {
const file = path.parse(filename); const file = path.parse(filename);
if (file.ext === `.${dirWatch.extension}`) { if (file.ext === `.${dirWatch.extension}`) {
const audio = fs.readFileSync(filename); const audio = this.readFile(filename);
const audioName = file.base; const audioName = file.base;
const audioType = mime.lookup(file.base); const audioType = mime.lookup(file.base);
const dateTime = new Date(fs.statSync(filename).ctime); const dateTime = new Date(this.statFile(filename).ctime);
const frequency = parseInt(dirWatch.frequency, 10) || null; const frequency = parseInt(dirWatch.frequency, 10) || null;
@ -118,7 +140,7 @@ class DirWatch {
const audioFile = path.resolve(file.dir, `${file.name}.${dirWatch.extension}`); const audioFile = path.resolve(file.dir, `${file.name}.${dirWatch.extension}`);
if (fs.existsSync(audioFile)) { if (fs.existsSync(audioFile)) {
const audio = fs.readFileSync(audioFile); const audio = this.readFile(audioFile);
const audioName = file.base; const audioName = file.base;
@ -127,7 +149,7 @@ class DirWatch {
const system = this.parseId(dirWatch.system, filename); const system = this.parseId(dirWatch.system, filename);
try { try {
const meta = JSON.parse(fs.readFileSync(filename, 'utf8')); const meta = JSON.parse(this.readFile(filename, 'utf8'));
await this.app.controller.importSdrtrunk(audio, audioName, audioType, system, meta); await this.app.controller.importSdrtrunk(audio, audioName, audioType, system, meta);
@ -153,7 +175,7 @@ class DirWatch {
const audioFile = path.resolve(file.dir, `${file.name}.${dirWatch.extension}`); const audioFile = path.resolve(file.dir, `${file.name}.${dirWatch.extension}`);
if (fs.existsSync(audioFile)) { if (fs.existsSync(audioFile)) {
const audio = fs.readFileSync(audioFile); const audio = this.readFile(audioFile);
const audioName = file.base; const audioName = file.base;
@ -162,7 +184,7 @@ class DirWatch {
const system = this.parseId(dirWatch.system, filename); const system = this.parseId(dirWatch.system, filename);
try { try {
const meta = JSON.parse(fs.readFileSync(filename, 'utf8')); const meta = JSON.parse(this.readFile(filename, 'utf8'));
await this.app.controller.importTrunkRecorder(audio, audioName, audioType, system, meta); await this.app.controller.importTrunkRecorder(audio, audioName, audioType, system, meta);
@ -202,9 +224,38 @@ class DirWatch {
} }
} }
exists(filename) {
try {
return fs.existsSync(filename);
} catch (error) {
return false;
}
}
readFile(filename, mode) {
try {
return fs.readFileSync(filename, mode);
} catch (error) {
console.error(`Unable to read ${filename}`);
}
}
statFile(filename) {
try {
return fs.statSync(filename);
} catch (error) {
console.error(`Unable to stat ${filename}`);
return {};
}
}
unlink(filename) { unlink(filename) {
try { try {
setTimeout(() => fs.unlinkSync(filename), 1000); fs.unlinkSync(filename);
} catch (error) { } catch (error) {
console.error(`Unable to delete ${filename}`); console.error(`Unable to delete ${filename}`);