-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.js
94 lines (85 loc) · 3.35 KB
/
alarm.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
'use strict';
const axios = require('axios');
const User = require('./models/User');
const spotify = require('./spotify');
function powerUpSB(user, playlist_uri, volume) {
axios.get(`http://${user.yamaha_ip}/YamahaExtendedControl/v1/main/setPower?power=on`)
.then(function(response) {
console.log(response.data);
setInputSB(user, playlist_uri, volume);
})
.catch(function(error) {
console.log(error);
});
}
function setInputSB(user, playlist_uri, volume) {
axios.get(`http://${user.yamaha_ip}/YamahaExtendedControl/v1/main/setInput?input=spotify`)
.then(function(response) {
console.log(response.data);
setTimeout(() => {setVolumeSB(user, 0, () => {setVolumeSpotify(user, playlist_uri, volume)})}, 2000);
})
.catch(function(error) {
console.log(error);
});
}
function setVolumeSB(user, volume, next) {
axios.get(`http://${user.yamaha_ip}/YamahaExtendedControl/v1/main/setVolume?volume=${volume}`)
.then(function(response) {
console.log('volume '+response.data.response_code);
next();
})
.catch(function(error) {
console.log(error);
});
}
function setVolumeSpotify(user, playlist_uri, volume) {
// axios.put("https://api.spotify.com/v1/me/player/volume", { 'volume_percent': '50' }, {headers: { 'Authorization': 'Bearer ' + user.access_token }})
// .then(function(response) {
// console.log('ok');
playPlaylist(user, playlist_uri, volume);
// })
// .catch(function(error) {
// console.log(error);
// });
}
function playPlaylist(user, playlist_uri, volume) {
spotify.play(user, playlist_uri, volume, () => {updateVolumeSB(user, volume)});
}
function updateVolumeSB(user, volume) {
var id = setInterval(increaseVolumeSB, 5000);
function increaseVolumeSB() {
axios.get(`http://${user.yamaha_ip}/YamahaExtendedControl/v1/main/getStatus`)
.then(function(response) {
let cur_vol = response.data.volume;
console.log(`current volume ${cur_vol}`);
if( cur_vol < volume )
setVolumeSB(user, cur_vol+1, () => {});
else
clearInterval(id);
})
.catch(function(error) {
console.log(error);
clearInterval(id);
});
};
}
module.exports = async function(username, playlist_uri, volume) {
const user = await User.findOne({username: username });
// axios.put(`https://api.spotify.com/v1/me/player/play?device_id=${user.yamaha_id}`, { 'context_uri': playlist_uri }, {headers: { 'Authorization': `Bearer ${user.access_token}` }})
// .then(function(response) {
// console.log('ok');
// })
// .catch(function(error) {
// console.log(error);
// console.log(error.response.data.error);
// });
// axios.get("https://api.spotify.com/v1/me/player/devices", {headers: { 'Authorization': 'Bearer ' + user.access_token }})
// .then(function(response) {
// console.log(response.data);
// console.log('ok');
// })
// .catch(function(error) {
// console.log(error);
// });
powerUpSB(user, playlist_uri, volume);
}