From dccde82a912db72f0a3ba8e29c817941c02a581c Mon Sep 17 00:00:00 2001 From: Thomas Hirschberger Date: Wed, 15 Mar 2023 19:23:40 +0100 Subject: [PATCH] added option to activate a temporary profile for a specific amount of milliseconds. After the timeout the module changes back to the regular profile. --- MMM-ProfileSwitcher.js | 31 ++++++++++++++++++++++++++++++- README.md | 14 +++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/MMM-ProfileSwitcher.js b/MMM-ProfileSwitcher.js index 430c6bb..5f2efcb 100644 --- a/MMM-ProfileSwitcher.js +++ b/MMM-ProfileSwitcher.js @@ -42,9 +42,11 @@ Module.register("MMM-ProfileSwitcher", { // The default time when none is set for a profile in timers defaultTime: 60000, + // The default time a temporary profile is active before the module changes back to the regular profile + defaultTimeTemporary: 5000, // Timers for different profiles. A timer lets you automatically swap to a different profile after a certain amount of time. // Check README.md for configuration - timers: undefined + timers: undefined, }, // Override the default getTranslations function @@ -159,18 +161,45 @@ Module.register("MMM-ProfileSwitcher", { // Override the default NotificationRecieved function notificationReceived: function (notification, payload, sender) { + const self = this if (notification === "DOM_OBJECTS_CREATED") { Log.log("Hiding all non default modules."); this.set_profile(this.config.includeEveryoneToDefault); this.sendNotification("CHANGED_PROFILE", {to: this.config.defaultClass}); } else if (notification === "CURRENT_PROFILE") { this.change_profile(payload); + clearTimeout(self.temporaryTimer); + self.inTemporaryProfile = false; } else if (notification === "DISABLE_PROFILE_TIMERS"){ clearTimeout(this.timer); } else if (notification === "ENABLE_PROFILE_TIMERS"){ if (this.config.timers && this.config.timers[this.current_profile]){ this.set_timer(this.config.timers[this.current_profile]); } + } else if (notification === "ACTIVATE_TEMPORARY_PROFILE"){ + clearTimeout(this.timer); + clearTimeout(self.temporaryTimer); + if (!self.inTemporaryProfile){ + self.oldProfile = self.current_profile; + } + self.inTemporaryProfile = true; + let newProfile = payload.profile || false; + let time = payload.time || self.config.defaultTimeTemporary; + if (newProfile !== false){ + self.change_profile(newProfile); + self.temporaryTimer = setTimeout(()=>{ + self.change_profile(self.oldProfile) + self.inTemporaryProfile = false; + }, time); + + } + + if (this.config.timers && this.config.timers[this.current_profile]){ + this.set_timer(this.config.timers[this.current_profile]); + } + } else if (notification === "ABORT_TEMPORARY_PROFILE"){ + clearTimeout(self.temporaryTimer); + self.change_profile(self.oldProfile); } }, diff --git a/README.md b/README.md index ac3134f..f40b422 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ The following properties can be configured: | `leaveMessages` | The notification message that will be shown when we switch to the `defaultClass`. See [Configuring Profile Messages](#configuring-profile-messages) for more information.

**Possible values:** `Object with profiles` or `false`
**Default value:** `{}` | `includeEveryoneMessages` | Determines if the messages for everyone should also be added to the possible messages for profiles that have custome messages.

**Possible values:** `true` or `false`
**Default value:** `false` | `useLockStrings` | Determines whether or not to use [*lockStrings*](https://github.com/MichMich/MagicMirror/tree/master/modules#thishidespeed-callback-options).

**Possible values:** `true` or `false`
**Default value:** `true`. -| `defaultTime` | The default time (in microseconds) when none is set for a profile in `timers`.

**Possible values:** `number`
**Default value:** `60000` (`60` seconds) +| `defaultTime` | The default time (in milliseconds) when none is set for a profile in `timers`.

**Possible values:** `number`
**Default value:** `60000` (`60` seconds) +| `defaultTimeTemporary` | The default time (in milliseconds) a temporary profile is active before the module changes back to the regular profile.

**Possible values:** `number`
**Default value:** `5000` (`5` seconds) | `timers` | Timers for different profiles. A timer lets you automatically swap to a different profile after a certain amount of time. See [Configuring Timers](#configuring-timers) for more information.

**Possible values:** `Object with timers` or `undefined`
**Default value:** `undefined` @@ -187,6 +188,17 @@ Like so (replace `'DESIRED_PROFILE_NAME_HERE'` with your profile name): this.sendNotification('CURRENT_PROFILE', 'DESIRED_PROFILE_NAME_HERE'); ```` +### Temporary Profiles +The module can switch to a temporary profile for a specific amount of time (i.e. to display a video stream module after the bell of the front door has rang). +To do this send a `ACTIVATE_TEMPORARY_PROFILE` notification with the a payload object containing the options `profile` and `time` (replace `DESIRED_PROFILE_NAME_HERE` with your profile name and `DESIRED_TIME` with the time in milliseconds the profile should be active). +i.e. +````javascript +this.sendNotification('ACTIVATE_TEMPORARY_PROFILE', {'profile': 'DESIRED_PROFILE_NAME_HERE', 'time': DESIRED_TIME} ); +```` +The `time` configuration is optional and the value of `defaultTimeTemporary` is used if it is missing. + +If you do not want to wait for the timeout to change back to the regular profile you can abort the temporary profile by sending a `ABORT_TEMPORARY_PROFILE` notification. + ## Using With Other Modules Since this module uses notifications, as described in [Switching profiles](#switching-profiles), it can easily be used in conjunction with other modules.