This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_helper.js
108 lines (92 loc) · 3.6 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
const fs = require('fs');
const path = require('path');
const NodeHelper = require('node_helper');
const Player = require('node-aplay');
const moment = require('moment');
module.exports = NodeHelper.create({
isLoaded: false,
config: null,
/**
* @param {String} notification
* @param {*} payload
*/
socketNotificationReceived: function (notification, payload) {
if (notification === 'CONFIG') {
if (!this.isLoaded) {
this.config = payload;
this.isLoaded = true;
if (this.config.startupSound) {
this.playFile(this.config.startupSound);
}
}
} else if (notification === 'PLAY_SOUND') {
if (typeof payload === 'string') {
this.playFile(payload);
} else if (typeof payload === 'object') {
if (typeof payload.sound === 'undefined' || !payload.sound) {
this.log('Could not play sound, notification payload `sound` was not supplied');
} else {
this.playFile(payload.sound, payload.delay);
}
}
}
},
/**
* @param {String} filename
* @param {Number} [delay] in ms
*/
playFile: function (filename, delay) {
// Only play if outside of quiet hours
let play = true;
if (this.config.quietTimeStart && this.config.quietTimeEnd) {
this.log('Quiet Time Start is: ' + this.config.quietTimeStart, true);
this.log('Quiet Time End is: ' + this.config.quietTimeEnd, true);
let start_moment = moment(this.config.quietTimeStart, 'HH:mm');
let end_moment = moment(this.config.quietTimeEnd, 'HH:mm');
this.log('Start Moment: ' + start_moment.format('YYYY-MM-DD HH:mm'));
this.log('End Moment: ' + end_moment.format('YYYY-MM-DD HH:mm'));
let time = moment();
if (start_moment.isBefore(end_moment)) {
if (moment().isBetween(start_moment, end_moment)) {
play = false;
}
} else {
let day_start = moment('00:00:00', 'HH:mm:ss');
let day_end = moment('23:59:59', 'HH:mm:ss');
if (time.isBetween(day_start, end_moment) || time.isBetween(start_moment, day_end)) {
play = false;
}
}
}
if (play) {
delay = delay || this.config.defaultDelay;
let soundfile = __dirname + '/sounds/' + filename;
// Make sure file exists before playing
try {
fs.accessSync(soundfile, fs.F_OK);
} catch (e) {
// Custom sequence doesn't exist
this.log('Sound does not exist: ' + soundfile);
return;
}
this.log('Playing ' + filename + ' with ' + delay + 'ms delay', true);
setTimeout(() => {
new Player(path.normalize(__dirname + '/sounds/' + filename)).play();
}, delay);
} else {
this.log('Not playing sound as quiet hours are in effect');
}
},
/**
* Outputs log messages
*
* @param {String} message
* @param {Boolean} [debug_only]
*/
log: function (message, debug_only) {
if (!debug_only || (debug_only && typeof this.config.debug !== 'undefined' && this.config.debug)) {
console.log('[' + moment().format('YYYY-MM-DD HH:mm:ss') + '] [MMM-Sounds] ' + message);
}
}
});