-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
96 lines (85 loc) · 3.36 KB
/
config.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
'use strict'; // eslint-disable-line strict
let kodiConfig = [];
let globalConfig = {};
try {
// Try to import the kodi hosts. If not found, we'll asume that env varialbes are available.
let config = require('./kodi-hosts.config.js'); // eslint-disable-line global-require
kodiConfig = config.kodiConfig;
globalConfig = config.globalConfig;
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
const Kodi = require('./kodi-connection/node.js');
const Init = function() {
this.kodiHosts = [];
this.globalConf = {};
require('dotenv').load(); // eslint-disable-line global-require
if (kodiConfig.length !== 0) {
// We've found one or more kodi configurations.
this.kodiHosts = kodiConfig.map((config) => {
return {
id: config.id,
host: new Kodi(
config.kodiProtocol || 'http',
config.kodiIp,
config.kodiPort,
config.kodiUser,
config.kodiPassword)
};
});
console.log(`Loaded ${this.kodiHosts.length} hosts from the config.js`);
} else {
if (!process.env.AUTH_TOKEN || !process.env.KODI_IP || !process.env.KODI_PORT || !process.env.KODI_USER || !process.env.KODI_PASSWORD) {
console.log('Missing kodi host config. Please configure one using the .env (when using Glitch) or the config.js file.');
process.exit();
}
this.kodiHosts[0] = {
id: 'kodi',
host: new Kodi(
process.env.KODI_PROTOCOL || 'http',
process.env.KODI_IP,
process.env.KODI_PORT,
process.env.KODI_USER,
process.env.KODI_PASSWORD)
};
}
if (Object.keys(globalConfig).length > 0) {
console.log(`Starting using kodi-hosts.config.js, ${JSON.stringify(globalConfig, null, 2)}`);
this.globalConf = globalConfig;
} else {
if (!process.env.AUTH_TOKEN || !process.env.PORT) {
console.log('Missing AuthToken. Please configure one using the .env (when using Glitch) or the config.js file.');
process.exit();
}
this.globalConf.authToken = process.env.AUTH_TOKEN;
this.globalConf.listenerPort = process.env.PORT;
this.globalConf.youtubeKey = process.env.YOUTUBE_KEY || 'AIzaSyBYKxhPJHYUnzYcdOAv14Gmq-43_W9_79w';
console.log('Loaded config from .env');
}
this.getHost = (kodiId) => {
let returnHost;
if (kodiId) {
returnHost = this.kodiHosts.filter((kodiHost) => {
if (kodiHost.id === kodiId) {
return kodiHost.host;
}
})[0].host;
} else {
returnHost = this.kodiHosts[0].host;
}
return returnHost;
};
/*
* Check the request to determine to which kodi instance we want to route the actions to.
* The request object is anylyzed. And the Kodi object is attached to the request, for further use.
*/
this.routeKodiInstance = (request) => {
// For now we are only attaching the first host in the list.
// Next will be to determin a way of passing a host, through IFTTT.
request.kodi = this.getHost(request.body.kodiid);
request.config = this.globalConf;
};
};
module.exports = Init;