Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

activate a temporary profile for a specific amount of milliseconds #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion MMM-ProfileSwitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
},

Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br><br> **Possible values:** `Object with profiles` or `false` <br> **Default value:** `{}`
| `includeEveryoneMessages` | Determines if the messages for everyone should also be added to the possible messages for profiles that have custome messages. <br><br> **Possible values:** `true` or `false` <br> **Default value:** `false`
| `useLockStrings` | Determines whether or not to use [*lockStrings*](https://github.com/MichMich/MagicMirror/tree/master/modules#thishidespeed-callback-options). <br><br> **Possible values:** `true` or `false` <br> **Default value:** `true`.
| `defaultTime` | The default time (in microseconds) when none is set for a profile in `timers`. <br><br> **Possible values:** `number` <br> **Default value:** `60000` (`60` seconds)
| `defaultTime` | The default time (in milliseconds) when none is set for a profile in `timers`. <br><br> **Possible values:** `number` <br> **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. <br><br> **Possible values:** `number` <br> **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. <br><br> **Possible values:** `Object with timers` or `undefined` <br> **Default value:** `undefined`


Expand Down Expand Up @@ -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.

Expand Down