-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
134 lines (111 loc) · 3.09 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require('dotenv').config()
var express = require('express');
var async = require('async');
var insteon = require('./api/insteon');
var harmony = require('./api/harmony');
var lifx = require('./api/lifx');
var lib = require("./lib");
var cams = require('./api/cams');
var router = require('./router');
var dispatcher = require('./lib/dispatch');
var cors = require('cors');
var fs = require('fs');
var app = express();
var server = require('http').createServer(app);
var io;
if (process.env.NODE_ENV == "production") {
var secureServer = require('https').createServer({
key: fs.readFileSync(process.env.CERT_KEY),
cert: fs.readFileSync(process.env.CERT)
}, app);
io = require('socket.io').listen(secureServer);
io.set('transports', ['websocket']);
}
require("console-stamp")(console, {
pattern: ".",
colors: {
stamp: 'blue',
label: 'green',
metadata: 'red'
}
});
var bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cors({
origin: 'http://localhost:3000'
}));
app.use(express.static(__dirname + '/public'));
var prefix = "home";
app.use("/", router); //for legacy calls
app.use("/" + prefix, router);
app.use("/" + prefix, express.static(__dirname + '/public'));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
res.status(404);
// default to plain-text. send()
res.type('txt').send('Not found');
});
// error handlers
app.use(function (err, req, res, next) {
console.error(err);
res.status(err.status || 500);
res.type('txt').send(JSON.stringify(err));
});
async.auto({
insteonHub: function (next) {
insteon.register(function (err) {
console.log("connected to insteon hub");
next(err);
});
},
harmonyActivities: function (next) {
console.log("Starting harmony");
harmony.init(function (err) {
console.log("retrieved harmony commands");
next(err);
});
},
killStreams: function(next) {
if (process.env.NODE_ENV != "production") return next();
var spawn = require('child_process').spawn;
spawn("taskkill", ["/IM", "ffmpeg.exe", "/F"], { windowsHide: true});
next();
},
cams: function(next) {
if (process.env.NODE_ENV != "production") return next();
cams.init(next);
},
deviceList: ['killStreams', 'cams', 'insteonHub', 'harmonyActivities', function (next) {
dispatcher.devices(function (err, devices) {
console.log("got device list");
next(err);
});
}],
routines: ['deviceList', function(next) {
lib.routines.init();
next();
}],
sockets: [ 'routines', function (next) {
server.listen(3000, function () {
console.log("API Listening from 3000");
});
if (process.env.NODE_ENV == "production") {
secureServer.listen(3333, function () {
console.log("Socket server running from 3333");
});
dispatcher.server(io);
io.on('connection', function (socket) {
dispatcher.broadcastDevices();
});
}
next();
}]
}, function (err, results) {
if (err) {
process.exit(1);
}
});
module.exports = app;