forked from heretse/humix-spotify-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
humix-spotify.js
90 lines (64 loc) · 2.08 KB
/
humix-spotify.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
var HumixSense = require('humix-sense');
var SpotifyPlayer = require('./index');
var fs = require('fs');
//var moduleConfig = require('./config.js');
var config = {
"moduleName" : "humix-spotify-module",
"commands" : ["play-spotify", "pause-spotify", "resume-spotify", "stop-spotify"],
"events" : [],
"log" : {
file : 'humix-spotify-module.log',
fileLevel : 'info',
consoleLevel : 'debug'
}
};
var humix = new HumixSense(config);
var spotify;
var hsm;
var logger;
console.log('========= starting ===========');
humix.on('connection', function(humixSensorModule){
hsm = humixSensorModule;
logger = hsm.getLogger();
logger.info('access config');
var conf = hsm.getDefaultConfig();
if(!conf){
if(fs.existsSync('./config.js')){
logger.info('using local config file')
conf = require('./config.js');
}else{
logger.error('fail to load conversation config. Exit');
process.exit(1);
}
}
logger.info('loading config: ' + JSON.stringify(conf));
spotify = new SpotifyPlayer(conf, logger)
spotify.init();
logger.info('Communication with humix-sense is now ready.');
hsm.on("play-spotify", function (data) {
logger.info('received play-spotify data: ' + data);
// TODO : Check the type of data.
if (data.songName) {
spotify.playSong(data.songName, data.artistName);
} else {
var re = /\{.*\}/;
if (data.match(re)) {
var obj = JSON.parse(data);
logger.info("obj.songName = " + obj.songName);
logger.info("obj.artistName = " + obj.artistName);
spotify.playSong(obj.songName, obj.artistName);
} else {
spotify.playSong(data);
}
}
});
hsm.on("pause-spotify", () => {
spotify.pausePlaying();
});
hsm.on("resume-spotify", () => {
spotify.resumePlaying();
});
hsm.on("stop-spotify", () => {
spotify.stopPlaying();
});
});