-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
207 lines (154 loc) · 4.95 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//Default imports
'use strict';
const express = require('express');
const path = require('path');
const fs = require('fs');
const spdy = require('spdy');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const config = require('config');
const router = express.Router();
//effects
const rawColor = require('./effects/raw');
const color = require('./effects/color');
const app = express();
const networkConfig = config.get('serverProperties.networkConfiguration');
//include socket stuff
const server = require('http').Server(app);
// const io = require('socket.io')(server);
//UDP IMPORT
const UDP_PORT = networkConfig.udp_port;
const HOST = server.address.address;
const dgram = require('dgram');
const server_udp = dgram.createSocket('udp4');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
//res.io = io;
next();
});
server_udp.bind(UDP_PORT, HOST);
//set up and intialize the pixels
//when support comes for multiple outs they will be saved in a db
const stripConfig = config.get('serverProperties.stripProperties');
console.log(stripConfig);
const StripState = require('./helpers/stripState');
const stripState = new StripState.StripState(false, stripConfig.num_leds, 255, 'off' , 'off', 'stopped' , stripConfig.location, stripConfig.strandType);
let previousState= {};
let previousStateArray = new Uint32Array(stripConfig.num_leds);
previousState.previousStateArray = previousStateArray;
//pass the strip and previous state as global array
app.set('stripState', stripState);
app.set('previousStateArray', previousStateArray);
//ws281x.init(stripState.numLEDS);
process.on('SIGINT', function() {
stripState.reset();
//ws281x.reset();
process.nextTick(function() {
process.exit(0);
});
});
//UDP listener
server_udp.on('listening', function() {
const address = server.address();
console.log(address);
});
server_udp.on('message', function(message, remote) {
console.log(remote);
rawColor.raw(message, stripState);
});
//Force the array to emppty on start
let initalColor = 0xFFFFFF;
color.setColor(initalColor, stripState);
if(initalColor === 0x000000){
stripState.power = false;
stripState.setMode('off', 'off', 'stopped');
}
// io.on('connection', function(socket) {
// socket.emit('stripProperties', {
// stripState: stripState,
// });
// socket.on('changeColor', function(data){
// clearInterval(stripState.interval);
// color.setColor( data.color, stripState);
// socket.emit('state', {
// stripState: stripState,
// });
// });
// socket.on('changeBrightness', function(data){
// stripState.brightness = data.brightness;
// socket.emit('state', {
// stripState: stripState,
// });
// });
// socket.on('getStripProperties', function(data) {
// console.log(data);
// socket.emit('stripState', {
// stripState: stripState,
// });
// });
// socket.on('colorStatus', function(data) {
// console.log(data);
// socket.emit('color', {
// });
// });
// socket.on('rawColor', function(data) {
// rawColor.raw( data.message, stripState);
// socket.emit('color', {
// stripState: stripState
// });
// });
// });
//include routes
const powerRoute = require('./routes/power');
const effects = require('./routes/effects');
const routes = require('./routes/index');
const brightness = require('./routes/brightness');
const colorRoute = require('./routes/color');
const state = require('./routes/state');
const scene = require('./routes/scene');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
//add routes to apps
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public/dist')));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/mode', router);
app.use('/', routes);
app.use('/api/power', powerRoute);
app.use('/api/effects', effects);
app.use('/api/brightness', brightness);
app.use('/api/color', colorRoute);
app.use('/api/state', state);
app.use('/api/scene', scene);
//app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(500).json({message: err.message, error: err });
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(500).json({message: err.message, error: err });
});
module.exports = {
app: app,
server: server
};