Skip to content

Commit

Permalink
Merge pull request #22 from fewieden/develop
Browse files Browse the repository at this point in the history
Refactoring and popup config option
  • Loading branch information
fewieden committed Aug 6, 2019
2 parents c5a4cda + 9f4dbbb commit 9e906ad
Show file tree
Hide file tree
Showing 13 changed files with 3,755 additions and 121 deletions.
14 changes: 7 additions & 7 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ engines:
enabled: true
config:
languages:
- javascript
- javascript
eslint:
enabled: true
channel: "eslint-3"
Expand All @@ -15,9 +15,9 @@ engines:
enabled: true
ratings:
paths:
- "**.css"
- "**.js"
- "**.md"
exclude_paths: [
"node_modules/**/*"
]
- "**.css"
- "**.js"
- "**.md"
exclude_paths:
- "node_modules/**/*"
- "docs/**/*"
9 changes: 0 additions & 9 deletions .doclets.yml

This file was deleted.

32 changes: 21 additions & 11 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
{
"extends": "airbnb-base",
"rules": {
"comma-dangle": 0,
"indent": [2, 4],
"max-len": [2, 120, { "ignoreStrings": true }],
"radix": [2, "as-needed"]
},
"env": {
"browser": true,
"es6": true
}
"extends": ["esnext", "esnext/style-guide"],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"env": {
"browser": true,
"es6": true
},
"rules": {
"import/no-commonjs": 0,
"import/no-nodejs-modules": 0,
"semi": 0,
"comma-dangle": 0,
"indent": ["error", 4],
"template-curly-spacing": 0,
"no-console": 0,
"curly": ["error", "all"],
"array-bracket-spacing": 0,
"space-before-function-paren": 0
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
npm-debug.log*

node_modules/
docs/

.idea/

docs/
sounds/*
!sounds/blackforest.mp3
!sounds/alarm.mp3
2 changes: 1 addition & 1 deletion .mdlrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
all
rules "~MD013", "~MD026", "~MD033"
rules "~MD013", "~MD024", "~MD026", "~MD033"
2 changes: 1 addition & 1 deletion .stylelintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"rules": {
"indentation": 4
}
}
}
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: node_js
node_js:
- "stable"
- "7"
- "6"
- "5"
- "8"
- "10"
- "12"
script:
- npm run lint
cache:
Expand Down
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# MMM-AlarmClock Changelog

## [2.0.0]

### Added

* Config option `popup` to avoid alert popup.
* Nunjuck templates
* Notification handler `STOP_ALARM` to stop an alarm from another module.

### Changed

* Config files
* Removed documentation on doclets.io (unmaintained).
* eslint recommended instead of airbnb ruleset.

## [1.1.1]

### Added

* Add a new alarm option : timer
* Add a .gitignore in sounds folder.

## [1.1.0]

### Added
Expand Down
174 changes: 98 additions & 76 deletions MMM-AlarmClock.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ Module.register('MMM-AlarmClock', {
* @property {boolean} fade - Flag to fade in alarm sound volume.
* @property {int} fadeTimer - Fade in duration.
* @property {number} fadeStep - Fade in volume steps.
* @property {boolean} popup - Flag to show alert popup or not.
*/
defaults: {
sound: 'alarm.mp3',
touch: false,
volume: 1.0,
format: 'ddd, h:mmA',
timer: 60 * 1000, // one minute
timer: 60 * 1000,
fade: false,
fadeTimer: 60 * 1000, // 60 seconds
fadeStep: 0.005 // 0.5%
fadeTimer: 60 * 1000,
fadeStep: 0.005,
popup: true
},

/**
Expand Down Expand Up @@ -116,10 +118,49 @@ Module.register('MMM-AlarmClock', {
};
},

/**
* @function getTemplate
* @description Nunjuck template.
* @override
*
* @returns {string} Path to nunjuck template.
*/
getTemplate() {
return 'templates/MMM-AlarmClock.njk';
},

/**
* @function getTemplateData
* @description Data that gets rendered in the nunjuck template.
* @override
*
* @returns {string} Data for the nunjuck template.
*/
getTemplateData() {
let src = this.config.sound;

if (this.alarmFired && this.next.sound) {
src = this.next.sound;
}

if (!src.match(/^https?:\/\//)) {
src = this.file(`sounds/${src}`);
}

return {
config: this.config,
next: this.next,
alarmFired: this.alarmFired,
src
};
},

/**
* @function start
* @description Sets first alarm and creates interval to check the alarm event.
* @override
*
* @returns {void}
*/
start() {
Log.info(`Starting module: ${this.name}`);
Expand All @@ -130,9 +171,26 @@ Module.register('MMM-AlarmClock', {
moment.locale(config.language);
},

/**
* @function notificationReceived
* @description Handles incoming broadcasts from other modules or the MagicMirror core.
* @override
*
* @param {string} notification - Notification name
*
* @returns {void}
*/
notificationReceived(notification) {
if (notification === 'STOP_ALARM') {
this.resetAlarmClock()
}
},

/**
* @function checkAlarm
* @description Checks the alarm event and triggers the alert.
*
* @returns {void}
*/
checkAlarm() {
if (!this.alarmFired && this.next && moment().diff(this.next.moment) >= 0) {
Expand All @@ -141,32 +199,44 @@ Module.register('MMM-AlarmClock', {
title: this.next.sender || this.next.title,
message: this.next.message
};
let timer = this.config.timer;
// If the alarm has specific timer and if MM is not touch, we use the alarm timer.
if (typeof this.next.timer !== 'undefined' && !this.config.touch) {
timer = this.next.timer;
}
if (!this.config.touch) {
alert.timer = this.config.timer;
alert.timer = timer;
}
if (this.config.popup) {
this.sendNotification('SHOW_ALERT', alert);
}
this.sendNotification('SHOW_ALERT', alert);
this.alarmFired = true;
this.updateDom(300);
this.updateDom();
this.timer = setTimeout(() => {
this.resetAlarmClock();
}, this.config.timer);
if (this.config.touch) {
MM.getModules().enumerate((module) => {
}, timer);
if (this.config.touch && this.config.popup) {
MM.getModules().enumerate(module => {
if (module.name === 'alert') {
module.alerts['MMM-AlarmClock'].ntf.addEventListener('click', () => {
clearTimeout(this.timer);
clearTimeout(this.fadeInterval);
this.resetAlarmClock();
});
}
});
}
setTimeout(() => {
const player = document.getElementById('MMM-AlarmClock-Player');
player.volume = this.config.fade ? 0 : this.config.volume;
this.fadeAlarm();
}, 100);
}
},

/**
* @function fadeAlarm
* @description Fades in the alarm sound step by step.
*
* @returns {void}
*/
fadeAlarm() {
let volume = 0;
Expand All @@ -186,6 +256,8 @@ Module.register('MMM-AlarmClock', {
/**
* @function setNextAlarm
* @description Sets the next occurring alarm event.
*
* @returns {void}
*/
setNextAlarm() {
this.next = null;
Expand All @@ -201,10 +273,14 @@ Module.register('MMM-AlarmClock', {
/**
* @function resetAlarmClock
* @description Resets the alarm clock.
*
* @returns {void}
*/
resetAlarmClock() {
clearTimeout(this.timer);
clearTimeout(this.fadeInterval);
this.alarmFired = false;
if (this.config.touch) {
if (this.config.touch && this.config.popup) {
this.sendNotification('HIDE_ALERT');
}
this.setNextAlarm();
Expand Down Expand Up @@ -238,76 +314,22 @@ Module.register('MMM-AlarmClock', {
for (let i = 0; i < alarm.days.length; i += 1) {
if (now.day() < alarm.days[i]) {
difference = Math.min(alarm.days[i] - now.day(), difference);
} else if (now.day() === alarm.days[i] && (parseInt(now.hour()) < hour ||
(parseInt(now.hour()) === hour && parseInt(now.minute()) < minute))) {
} else if (now.day() === alarm.days[i] && (parseInt(now.hour()) < hour
|| parseInt(now.hour()) === hour && parseInt(now.minute()) < minute)) {
difference = Math.min(0, difference);
} else if (now.day() === alarm.days[i]) {
difference = Math.min(7, difference);
} else {
difference = Math.min((7 - now.day()) + alarm.days[i], difference);
}
}

return moment().add(difference, 'days').set({
hour,
minute,
second: 0,
millisecond: 0
});
},

/**
* @function getDom
* @description Creates the UI as DOM for displaying in MagicMirror application.
* @override
*
* @returns {Element}
*/
getDom() {
const wrapper = document.createElement('div');
const header = document.createElement('header');
header.classList.add('align-left');

const logo = document.createElement('i');
logo.classList.add('fa', 'fa-bell-o', 'logo');
header.appendChild(logo);

const name = document.createElement('span');
name.innerHTML = this.translate('ALARM_CLOCK');
header.appendChild(name);
wrapper.appendChild(header);

if (!this.next) {
const text = document.createElement('div');
text.innerHTML = this.translate('LOADING');
text.classList.add('dimmed', 'light');
wrapper.appendChild(text);
} else if (this.alarmFired) {
const sound = document.createElement('audio');
let srcSound = this.config.sound;
if (this.next.sound) {
srcSound = this.next.sound;
}
if (!srcSound.match(/^https?:\/\//)) {
srcSound = this.file(`sounds/${srcSound}`);
}
sound.src = srcSound;
sound.volume = this.config.volume;
sound.setAttribute('id', 'MMM-AlarmClock-Player');
sound.volume = this.config.fade ? 0 : this.config.volume;
sound.setAttribute('autoplay', true);
sound.setAttribute('loop', true);
if (this.config.fade === true) {
this.fadeAlarm();
difference = Math.min(7 - now.day() + alarm.days[i], difference);
}
wrapper.appendChild(sound);
} else {
const alarm = document.createElement('div');
alarm.classList.add('small');
alarm.innerHTML = `${this.next.title}: ${this.next.moment.format(this.config.format)}`;
wrapper.appendChild(alarm);
}

return wrapper;
return moment().add(difference, 'days')
.set({
hour,
minute,
second: 0,
millisecond: 0
});
}
});
Loading

0 comments on commit 9e906ad

Please sign in to comment.