Skip to content

Commit

Permalink
Make watchedObjects case insensitive (#138)
Browse files Browse the repository at this point in the history
* Make watchedObjects case insensitive
Fixes #134

* Update changelog

* 1.6.0

* Bump version in changelog
  • Loading branch information
neilenns authored Jun 6, 2020
1 parent 7a4ed35 commit 737f83d
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

## Version 1.6.0

- watchObjects is now case insensitive when comparing against the matched objects ([issue 134](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/134))
- Address a warning during config file validation ([issue 123](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/123)).
- Document the available Docker image tags ([issue 128](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/128)).
- Addresses code cleanup [issue 136](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/136).
Expand Down
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-deepstackai-trigger",
"version": "1.5.0",
"version": "1.6.0",
"description": "Detects motion using DeepStack AI and calls registered triggers based on trigger rules.",
"main": "dist/src/main.js",
"files": [
Expand Down Expand Up @@ -32,6 +32,7 @@
"homepage": "https://github.com/danecreekphotography/node-deepstackai-trigger#readme",
"dependencies": {
"ajv": "^6.12.2",
"ajv-keywords": "^3.4.1",
"async-mqtt": "^2.6.0",
"bufferutil": "^4.0.1",
"chalk": "^3.0.0",
Expand All @@ -47,6 +48,7 @@
"utf-8-validate": "^5.0.2"
},
"devDependencies": {
"@types/ajv-keywords": "^3.4.0",
"@types/jest": "^25.2.3",
"@types/node": "^13.1.2",
"@types/node-telegram-bot-api": "^0.40.3",
Expand Down
5 changes: 4 additions & 1 deletion src/Trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ export default class Trigger {
* @returns True if the trigger is activated by the label
*/
public isRegisteredForObject(fileName: string, label: string): boolean {
const isRegistered = this.watchObjects?.includes(label);
const isRegistered = this.watchObjects?.some(watchLabel => {
return watchLabel.toLowerCase() === label?.toLowerCase();
});

if (!isRegistered) {
log.info(
`Trigger ${this.name}`,
Expand Down
3 changes: 3 additions & 0 deletions src/schemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import telegramHandlerConfiguration from "./schemas/telegramHandlerConfiguration
import triggerSchema from "./schemas/triggerConfiguration.schema.json";
import webRequestHandlerConfig from "./schemas/webRequestHandlerConfig.schema.json";
import maskConfiguration from "./schemas/maskConfiguration.schema.json";
import ajvkeywords from "ajv-keywords";

/**
* Validates an object against a schema file
Expand All @@ -23,6 +24,8 @@ export default async function validateJsonAgainstSchema(
): Promise<boolean> {
const validator = new Ajv();

ajvkeywords(validator, "transform");

// Register all the schemas that get used with this app. It doesn't matter
// if they are for different schema files/uses, ajv only loads them when
// actually required by the file being processed.
Expand Down
1 change: 1 addition & 0 deletions src/schemas/triggerConfiguration.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"type": "array",
"items": {
"type": "string",
"transform": ["toLowerCase"],
"enum": [
"person",
"bicycle",
Expand Down
47 changes: 37 additions & 10 deletions tests/Trigger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,45 @@
* Copyright (c) Neil Enns. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import trigger from "../src/Trigger";
import Trigger from "../src/Trigger";
import validateJsonAgainstSchema from "../src/schemaValidator";
import triggerConfigJson from "../src/schemas/triggerConfiguration.schema.json";
import triggerJson from "./triggers.json";

test("Verify watchObjects", () => {
const testTrigger = new trigger();
testTrigger.watchObjects = ["cat", "elephant", "bike"];
test("Verify isRegisteredForObject()", () => {
// Empty constructor should default to enabled true
const trigger = new Trigger();
trigger.name = "Trigger.test.ts";

expect(testTrigger.isRegisteredForObject("test", "dog")).toBe(false);
expect(testTrigger.isRegisteredForObject("test", "cat")).toBe(true);
trigger.watchObjects = ["dog"];
expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(true);

// testTrigger.watchObjects = undefined;
// expect(testTrigger.isRegisteredForObject("test", "cat")).toBe(false);
trigger.watchObjects = [];
expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false);

testTrigger.watchObjects = [];
expect(testTrigger.isRegisteredForObject("test", "cat")).toBe(false);
trigger.watchObjects = undefined;
expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false);

trigger.watchObjects = null;
expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false);

trigger.watchObjects = ["DoG"];
expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(true);

trigger.watchObjects = ["dog"];
expect(trigger.isRegisteredForObject("unit test", "doG")).toBe(true);

trigger.watchObjects = ["cat", "elephant"];
expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false);

trigger.watchObjects = ["cat", "elephant"];
expect(trigger.isRegisteredForObject("unit test", undefined)).toBe(false);
});

test("Verify isRegisteredForObject()", async () => {
await expect(validateJsonAgainstSchema(triggerJson, triggerConfigJson)).resolves.toEqual(true);

// Check that case doesn't matter for string arrays that take an enum
triggerJson.triggers[0].watchObjects = ["dOg"];
await expect(validateJsonAgainstSchema(triggerJson, triggerConfigJson)).resolves.toEqual(true);
});
32 changes: 0 additions & 32 deletions tests/handlers/Trigger.test.ts

This file was deleted.

27 changes: 27 additions & 0 deletions tests/triggers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "https://raw.githubusercontent.com/danecreekphotography/node-deepstackai-trigger/master/src/schemas/triggerConfiguration.schema.json",
"triggers": [
{
"name": "Dog detector",
"watchPattern": "/aiinput/Dog*.jpg",
"enabled": true,
"threshold": {
"minimum": 50,
"maximum": 100
},
"handlers": {
"webRequest": {
"triggerUris": ["http://localhost:81/admin?trigger&camera=Dog"]
},
"mqtt": {
"topic": "aimotion/triggers/dog"
},
"telegram": {
"chatIds": [1],
"cooldownTime": 60
}
},
"watchObjects": ["dog"]
}
]
}

0 comments on commit 737f83d

Please sign in to comment.