diff --git a/.devcontainer/triggers.json b/.devcontainer/triggers.json index 165f062..9f16ab8 100644 --- a/.devcontainer/triggers.json +++ b/.devcontainer/triggers.json @@ -24,7 +24,7 @@ }, { "name": "Dog detector", - "watchPattern": "/aiinput/Dog*.jpg", + "watchPattern": "/aiinput/**/Dog*.jpg", "enabled": true, "threshold": { "minimum": 50, diff --git a/CHANGELOG.md b/CHANGELOG.md index 988e571..67ff0f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Add support for `customEndpoint` on a trigger to support custom Deepstack models. Resolves [issue 416](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/416). +- Support for glob patterns in trigger watchPatterns. Resolves [issue 412](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/412). ## 5.6.1 diff --git a/package-lock.json b/package-lock.json index cc95752..c17c714 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1062,6 +1062,16 @@ "@types/range-parser": "*" } }, + "@types/glob": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz", + "integrity": "sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==", + "dev": true, + "requires": { + "@types/minimatch": "*", + "@types/node": "*" + } + }, "@types/graceful-fs": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.3.tgz", @@ -1127,6 +1137,12 @@ "integrity": "sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q==", "dev": true }, + "@types/minimatch": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz", + "integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==", + "dev": true + }, "@types/mkdirp": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@types/mkdirp/-/mkdirp-1.0.1.tgz", diff --git a/package.json b/package.json index 0626513..2469813 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "devDependencies": { "@types/ajv-keywords": "^3.4.0", "@types/express": "^4.17.6", + "@types/glob": "^7.1.3", "@types/http-terminator": "^2.0.1", "@types/jest": "^25.2.3", "@types/mkdirp": "^1.0.1", diff --git a/sampleConfiguration/aiinput/dogcamera/Dog_20200523-075000.jpg b/sampleConfiguration/aiinput/dogcamera/Dog_20200523-075000.jpg new file mode 100644 index 0000000..0208e82 Binary files /dev/null and b/sampleConfiguration/aiinput/dogcamera/Dog_20200523-075000.jpg differ diff --git a/sampleConfiguration/triggers.json b/sampleConfiguration/triggers.json index 9d892a3..fd4ba07 100644 --- a/sampleConfiguration/triggers.json +++ b/sampleConfiguration/triggers.json @@ -24,7 +24,7 @@ }, { "name": "Dog detector", - "watchPattern": "/aiinput/Dog*.jpg", + "watchPattern": "/aiinput/**/Dog*.jpg", "enabled": true, "threshold": { "minimum": 50, diff --git a/src/TriggerManager.ts b/src/TriggerManager.ts index 720f7ce..08941b3 100644 --- a/src/TriggerManager.ts +++ b/src/TriggerManager.ts @@ -7,9 +7,8 @@ import * as log from "./Log"; import * as MQTTManager from "./handlers/mqttManager/MqttManager"; import ITriggerConfigJson from "./types/ITriggerConfigJson"; -import * as fs from "fs"; +import glob from "glob" import MqttHandlerConfig from "./handlers/mqttManager/MqttHandlerConfig"; -import path from "path"; import PushbulletConfig from "./handlers/pushbulletManager/PushbulletConfig"; import PushoverConfig from "./handlers/pushoverManager/PushoverConfig"; import Rect from "./Rect"; @@ -69,7 +68,7 @@ export function loadConfiguration(configurations: IConfiguration[]): IConfigurat if (!triggerConfigJson) { throw Error( "Unable to find a trigger configuration file. Verify the trigger secret points to a file " + - "called triggers.json or that the /config mount point contains a file called triggers.json.", + "called triggers.json or that the /config mount point contains a file called triggers.json.", ); } @@ -272,21 +271,19 @@ export function resetOverallStatistics(): ITriggerStatistics { */ export function verifyTriggerWatchLocations(): boolean { const invalidWatchLocations = triggers?.filter(trigger => { - const watchFolder = path.dirname(trigger.watchPattern); - let files: string[]; try { - files = fs.readdirSync(watchFolder); + files = glob.sync(trigger.watchPattern); } catch (e) { log.warn( "Trigger manager", - `Unable to read contents of watch folder ${watchFolder} for trigger ${trigger.name}. Check and make sure the image folder is mounted properly. ${e}`, + `Unable to read contents of watch folder ${trigger.watchPattern} for trigger ${trigger.name}. Check and make sure the image folder is mounted properly. ${e}`, ); return true; } - log.verbose("Trigger manager", `There are ${files.length} images waiting in ${watchFolder} for ${trigger.name}.`); + log.verbose("Trigger manager", `There are ${files.length} images waiting in ${trigger.watchPattern} for ${trigger.name}.`); return false; });