-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·102 lines (83 loc) · 2.46 KB
/
server.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
'use strict';
var http = require('http');
var SonosDiscovery = require('sonos-discovery');
var SonosHttpAPI = require('./lib/sonos-http-api.js');
var nodeStatic = require('node-static');
var fs = require('fs');
var path = require('path');
var webroot = path.resolve(__dirname, 'static');
var settings = {
port: 5005,
cacheDir: './cache',
webroot: webroot
};
// Create webroot + tts if not exist
if (!fs.existsSync(webroot)) {
fs.mkdirSync(webroot);
}
if (!fs.existsSync(webroot + '/tts/')) {
fs.mkdirSync(webroot + '/tts/');
}
// load user settings
try {
var userSettings = require(path.resolve(__dirname, 'settings.json'));
} catch (e) {
console.log('no settings file found, will only use default settings');
}
if (userSettings) {
for (var i in userSettings) {
settings[i] = userSettings[i];
}
}
var fileServer = new nodeStatic.Server(webroot);
var discovery = new SonosDiscovery(settings);
var api = new SonosHttpAPI(discovery, settings);
var server = http.createServer(function (req, res) {
req.addListener('end', function () {
fileServer.serve(req, res, function (err) {
// If error, route it.
if (!err) {
return;
}
if (req.method === 'GET') {
api.requestHandler(req, res);
}
});
}).resume();
});
var bridgeDiscovery = require("./hue-api/bridge-discovery")
, Hue = require("./hue-api")
, lightState = require("./hue-api/lightstate")
, scheduledEvent = require("./hue-api/scheduledEvent.js")
, ApiError = require("./hue-api/errors.js").ApiError
;
function deprecated(fn, message) {
return function () {
var args = Array.prototype.slice.call(arguments);
console.error(message);
return fn.apply(this, args);
};
}
module.exports = {
HueApi: Hue,
BridgeApi: Hue,
api: Hue,
//TODO document this
connect: function(config) {
return new Hue(config);
},
lightState: lightState,
scheduledEvent: scheduledEvent,
upnpSearch: bridgeDiscovery.networkSearch,
nupnpSearch: bridgeDiscovery.locateBridges,
locateBridges: deprecated(bridgeDiscovery.locateBridges
, "'locateBridges' is deprecated, please use 'nupnpSearch' instead"),
searchForBridges: deprecated(
bridgeDiscovery.networkSearch
, "'searchForBridges' is deprecated, please use 'upnpSearch' instead"
),
ApiError: ApiError
};
server.listen(settings.port, function () {
console.log('http server listening on port', settings.port);
});