Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions MMM-MotionDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Module.register("MMM-MotionDetector", {
scoreThreshold: 20,
timeout: 120000, // 2 minutes,
deviceId: null,
controlDisplay: true, // switch the monitor on/off
additionalNotification: false, // set to some other value to specify an additional notification sent to all modules
},

lastScoreDetected: null,
Expand Down Expand Up @@ -41,13 +43,14 @@ Module.register("MMM-MotionDetector", {

// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
Log.info("Notification received: " + notification);
if (notification === "USER_PRESENCE") {
this.sendNotification(notification, payload);
}
},

start: function () {
Log.info("MMM-MotionDetector: starting up");
Log.info(this.name + ": starting up");

this.data.header = "MMM-MotionDetector";
this.lastScoreDetected = 0;
Expand All @@ -70,11 +73,18 @@ Module.register("MMM-MotionDetector", {
deviceId: this.config.deviceId,
captureIntervalTime: this.config.captureIntervalTime,
motionCanvas: canvas,
scoreThreshold: this.config.scoreThreshold,

initSuccessCallback: () => {
Log.info("MMM-MotionDetector: DiffCamEngine init successful.");
Log.info("Timeout set to " + this.config.timeout + " milliseconds.");
Log.info("Motion threshold set to " + this.config.scoreThreshold);
Log.info("Will MotionDetector control the monitor? " + this.config.controlDisplay);
if (this.config.additionalNotification) {
Log.info("Will send additional notification: " + this.config.additionalNotification);
}
DiffCamEngine.start();
},

initErrorCallback: (error) => {
Log.error("MMM-MotionDetector: DiffCamEngine init failed: " + error);
this.error = error;
Expand All @@ -85,20 +95,31 @@ Module.register("MMM-MotionDetector", {
this.percentagePoweredOff = ((100 * this.poweredOffTime) / (currentDate.getTime() - this.timeStarted)).toFixed(
2
);
const time = currentDate.getTime() - this.lastTimeMotionDetected.getTime();
if (hasMotion) {
Log.info("MMM-MotionDetector: Motion detected, score " + score);
this.sendSocketNotification("MOTION_DETECTED", { score: score });
this.sendNotification("MOTION_DETECTED", { score: score });

if (this.poweredOff) {
this.sendSocketNotification("ACTIVATE_MONITOR");
this.poweredOffTime = this.poweredOffTime + (currentDate.getTime() - this.lastTimePoweredOff.getTime());
this.sendNotification("MOTION_DETECTED", { score: score });

// if configured for additional notification, send it out
if (this.config.additionalNotification) {
Log.debug("Additional notification outbound: " + this.config.additionalNotification);
this.sendNotification(this.config.additionalNotification, "");
}

this.sendSocketNotification("ACTIVATE_MONITOR");
this.poweredOff = false;
}
this.lastTimeMotionDetected = currentDate;
} else {
const time = currentDate.getTime() - this.lastTimeMotionDetected.getTime();
if (this.config.timeout >= 0 && time > this.config.timeout && !this.poweredOff) {
this.sendSocketNotification("DEACTIVATE_MONITOR");
if (this.config.controlDisplay) {
Log.debug("Deactivating monitor.");
this.sendSocketNotification("DEACTIVATE_MONITOR");
}
this.lastTimePoweredOff = currentDate;
this.poweredOff = true;
}
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Assuming you are in your MagicMirror directory execute these commands:
```
cd modules
git clone https://github.com/rejas/MMM-MotionDetector.git
cd MMM-MotionDetector
npm install
```

## Requirements
Expand Down Expand Up @@ -93,6 +95,7 @@ The following properties can be configured:
| `scoreThreshold` | Threshold minimum for an image to be considered significant<br><br>**Default value:** `20` |
| `timeout` | Time in ms after which monitor is turned off when no motion is detected<br><br>Set to -1 to never turn off the monitor<br><br>**Default value:** `120000` |
| `deviceId` | (optional) specify which camera to use in case multiple exist in the system. |
| `additionalNotification` | An additional notification to send to other modules. Any value other than false will be the notification.<br><br>**Default value:** `false` |

#### How to get the deviceId

Expand Down Expand Up @@ -157,13 +160,19 @@ As you are bypassing browser security with this workaround you may want to add s

## Notifications send

| Notification | Payload | Description |
| -------------------- | ------------- | ----------------------------------------------------------------------- |
| `MOTION_DETECTED` | score | score calculated by the diff-cam-engine, 0 or greater |
| `DEACTIVATE_MONITOR` | percentageOff | percentage of time the monitor was deactivated since the module started |
| Notification | Payload | Description |
| -------------------- | ------------- | ----------------------------------------------------------------------------------------- |
| `MOTION_DETECTED` | score | score calculated by the diff-cam-engine, 0 or greater |
| `DEACTIVATE_MONITOR` | percentageOff | percentage of time the monitor was deactivated since the module started |
| [User-specified] | empty string | A user-defined notification. (ex: `TAKE_SELFIE`, `SHOW_FRAME_1`, `LAUNCH_WARHEADS`, etc.) |

## Changelog

### [1.6.1] - 2020-07-17

- Separate variables for monitor control, timeout
- User-defined notification

### [1.6.0] - 2020-07-05

- Allow disabling the monitor-functionality and just get the motion detection
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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