Allow specifying polling delay on dirWatch

This commit is contained in:
Chrystian Huot 2020-08-23 10:14:55 -04:00
parent fff40747d2
commit d4f25eee3c
3 changed files with 74 additions and 60 deletions

View file

@ -6,6 +6,7 @@
- Fix potential error in handling keypadBeeps styles.
- Change handling of skip delay on client.
- Fix beep method not completing promise in some situation.
- Allow specifying polling delay on dirWatch
# Version 4.6

View file

@ -333,7 +333,7 @@ dirWatch: {
system?: number | string; // optional
talkgroup?: number | string; // optional
type?: "default" | "trunk-recorder"; // optional, default is default
usePolling?: boolean; // optional, default is false
usePolling?: boolean | number; // optional, value is in ms, true = 1000ms, default is false
}[]
```
@ -356,6 +356,7 @@ dirWatch: {
* **system** - A valid system id defined in **rdioScanner.systems**.
* **talkgroup** - A valid talkgroup id defined in **rdioScanner.systems**.
* **type** - In case of *Trunk Recorder*, the metadata of the *JSON file* will be used.
* **usePolling** - When monitoring a network folder, you must use Polling for dirWatch to work. However, this is very CPU intensive and should be used with care. You can also try values greater than 1000 to decrease CPU consumption.
> Note that [Rdio Scanner](https://github.com/chuot/rdio-scanner) must know the **system id** and the **talkgroup id** for the call to be ingested. These two values must be specified either by **dirWatch.system**, **dirWatch.talkgroup**, **dirWatch.mask** or a mix of them. **dirWatch.system** and **dirWatch.talkgroup** have priority over **dirWatch.mask**.
@ -382,7 +383,7 @@ Default value is an empty array `[]` .
"extension": "wav",
"system": 21,
"type": "trunk-recorder"
},
}
]
```

View file

@ -36,69 +36,81 @@ class DirWatch {
this.registredDirectories = [];
this.config.forEach((dirWatch = {}) => {
if (typeof dirWatch.directory !== 'string' || !dirWatch.directory.length) {
console.error(`Config: No dirWatch.direction defined`);
return;
}
if (typeof dirWatch.extension !== 'string' || !dirWatch.extension.length) {
console.error(`Config: Mo dirWatch.extension defined`);
return;
}
if (typeof dirWatch.delay !== 'number' || dirWatch.delay < 100) {
delete dirWatch.delay;
}
if (typeof dirWatch.deleteAfter !== 'boolean') {
dirWatch.deleteAfter = false;
}
if (dirWatch.frequency === null || typeof dirWatch.frequency !== 'number') {
delete dirWatch.frequency;
}
if (dirWatch.mask === null || typeof dirWatch.mask !== 'string' || !dirWatch.mask.length) {
delete dirWatch.mask;
}
dirWatch.usePolling = dirWatch.usePolling === true ? true
: typeof dirWatch.usePolling === 'number' && dirWatch.usePolling >= 1000 ? dirWatch.usePolling
: false;
if (!dirWatch.usePolling) {
delete dirWatch.usePolling;
}
if (dirWatch.system === null && typeof dirWatch.system !== 'number' && dirWatch.system !== 'string') {
delete dirWatch.system;
}
if (dirWatch.talkgroup === null && typeof dirWatch.talkgroup !== 'number' && dirWatch.talkgroup !== 'string') {
delete dirWatch.talkgroup;
}
if (!['trunk-recorder'].includes(dirWatch.type)) {
delete dirWatch.type;
}
const dir = path.resolve(__dirname, '../../..', dirWatch.directory);
if (this.registredDirectories.includes(dir)) {
console.error(`Config: Duplicate directory definitions ${dir}`);
return;
} else {
this.registredDirectories.push(dir);
}
});
ctx.once('ready', () => {
this.config.forEach((dirWatch = {}) => {
if (typeof dirWatch.directory !== 'string' || !dirWatch.directory.length) {
console.error(`Config: No dirWatch.direction defined`);
return;
}
if (typeof dirWatch.extension !== 'string' || !dirWatch.extension.length) {
console.error(`Config: Mo dirWatch.extension defined`);
return;
}
if (typeof dirWatch.delay !== 'number' || dirWatch.delay < 100) {
delete dirWatch.delay;
}
if (typeof dirWatch.deleteAfter !== 'boolean') {
dirWatch.deleteAfter = false;
}
if (dirWatch.frequency === null || typeof dirWatch.frequency !== 'number') {
delete dirWatch.frequency;
}
if (dirWatch.mask === null || typeof dirWatch.mask !== 'string' || !dirWatch.mask.length) {
delete dirWatch.mask;
}
if (typeof dirWatch.usePolling !== 'boolean' || !dirWatch.usePolling) {
delete dirWatch.usePolling;
}
if (dirWatch.system === null && typeof dirWatch.system !== 'number' && dirWatch.system !== 'string') {
delete dirWatch.system;
}
if (dirWatch.talkgroup === null && typeof dirWatch.talkgroup !== 'number' && dirWatch.talkgroup !== 'string') {
delete dirWatch.talkgroup;
}
if (!['trunk-recorder'].includes(dirWatch.type)) {
delete dirWatch.type;
}
const dir = path.resolve(__dirname, '../../..', dirWatch.directory);
if (this.registredDirectories.includes(dir)) {
console.error(`Config: Duplicate directory definitions ${dir}`);
return;
} else {
this.registredDirectories.push(dir);
}
const watcher = chokidar.watch(dir, {
const options = {
awaitWriteFinish: true,
ignoreInitial: !dirWatch.deleteAfter,
recursive: true,
usePolling: dirWatch.usePolling,
});
usePolling: !!dirWatch.usePolling || false,
};
if (options.usePolling) {
options.binaryInterval = options.interval = dirWatch.usePolling === true ? 1000 : dirWatch.usePolling;
}
const watcher = chokidar.watch(path.resolve(__dirname, '../../..', dirWatch.directory), options);
switch (dirWatch.type) {
case 'trunk-recorder':