-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
113 lines (90 loc) · 3 KB
/
index.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
109
110
111
112
113
const loudness = require('loudness');
const osascript = require('node-osascript');
const path = require('path');
let Service, Characteristic;
module.exports = (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-spotify', 'Spotify', Spotify);
};
class Spotify {
constructor (log, config) {
this.log = log;
this.service = 'Switch';
this.config = config;
this.retrieveSwitchState = this.retrieveSwitchState.bind(this);
this.on = false;
}
setState (on, callback) {
if (this.on === on) return callback();
const state = on ? 'on' : 'off';
const script = on ? path.join(__dirname, 'scripts', 'spotify-on.applescript') : path.join(__dirname, 'scripts', 'spotify-off.applescript');
const fadeConfig = on ? 'fadeInTime' : 'fadeOutTime';
const fade = this.config[fadeConfig] * 1000 || 0;
const done = (err) => {
if (err) {
this.log('Error: ' + err);
callback(err || new Error('Error setting ' + this.config.name + ' to ' + state));
} else {
this.on = on;
this.log('Set ' + this.config.name + ' to ' + state);
callback(null);
}
};
const logVolErr = (err) => {
if (err) this.log('node-loudness error:', err);
};
const adjustVol = (target, curVol, interval, cb) => {
setTimeout(() => {
const newVol = curVol > target ? curVol - 1 : curVol + 1;
loudness.setVolume(newVol, err => {
logVolErr(err);
if (newVol !== target) adjustVol(target, newVol, interval, cb);
else cb(err);
});
}, interval);
};
if (fade) {
loudness.getVolume((err, initVol) => {
const interval = fade / initVol;
if (on) {
loudness.setVolume(0, (err) => {
logVolErr(err);
setTimeout(() => {
osascript.executeFile(script);
}, 100);
adjustVol(initVol, 0, interval, done);
});
} else {
adjustVol(0, initVol, interval, () => {
osascript.executeFile(script, done);
setTimeout(() => {
loudness.setVolume(initVol, logVolErr);
}, 100);
});
}
});
} else {
osascript.executeFile(script, done);
}
}
retrieveSwitchState () {
const script = path.join(__dirname, 'scripts', 'spotify-status.applescript');
const done = (err, on) => {
setTimeout(this.retrieveSwitchState, (this.config.statusCheckInterval * 1000) || 1000);
if (err) return;
this.on = !!on;
this.switchService.setCharacteristic(Characteristic.On, !!on);
};
osascript.executeFile(script, done);
}
getServices () {
const switchService = new Service.Switch(this.config.name);
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this));
this.switchService = switchService;
this.retrieveSwitchState();
return [switchService];
}
}