-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
156 lines (150 loc) · 4.24 KB
/
backend.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
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var io = require('socket.io');
var exec = require('child_process').exec
var redis = require('redis');
var client = redis.createClient(6379, 'localhost', {no_ready_check: true});
client.auth('password', function (err) {
if (err) throw err;
});
var Group = function(on,color,white,whiteBrightness,colorBrightness){
this.on = on;
this.color = color;
this.white = white;
this.whiteBrightness = whiteBrightness;
this.colorBrightness = colorBrightness;
}
var State = function(){
this.groups = [
new Group(1,0,1,25,25),
new Group(1,0,1,25,25),
new Group(1,0,1,25,25),
new Group(1,0,1,25,25),
new Group(1,0,1,25,25)
]
this.currentGroup = 0;
this.getColor = function(){
if(this.groups[this.currentGroup].white){
return -1;
}else{
return this.groups[this.currentGroup].color;
}
}
this.getBrightness = function(){
var group = this.groups[this.currentGroup]
if(group.white){
return group.whiteBrightness;
}else{
return group.colorBrightness;
}
}
this.isOn = function(){
return this.groups[this.currentGroup].on;
}
this.isWhite = function(){
return this.groups[this.currentGroup].white;
}
}
var state = new State();
client.on('connect', function() {
console.log('Connected to Redis');
client.get("state",function(err,data){
if(!data){
console.log("Could not get data from Redis")
}else{
state = JSON.parse( data.toString() );
}
});
});
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname+'/bin/'));
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");
next();
});
var server = http.createServer(app);
var serv_io = io.listen(server);
server.listen(8080);
app.post('/:group',function(req,res){
var p = req.body;
for(key in p){
state.groups[req.params.group][key] = p[key];
}
updateState(state);
if(p.on=='true'){
exec("python /home/pi/Milight/wrapper.py " + req.params.group + " ON")
if(p.white=='true'){
exec("python /home/pi/Milight/wrapper.py " + req.params.group + " ON -w");
}else{
exec("python /home/pi/Milight/wrapper.py " + req.params.group + " ON -c " + p.color);
}
exec("python /home/pi/Milight/wrapper.py " + req.params.group + " ON -b " + p.brightness);
}else{
exec("python /home/pi/Milight/wrapper.py " + req.params.group + " OFF")
}
res.json(state);
});
app.get('/:group',function(req,res){
var g = state.groups[req.params.group];
var obj = {data:g};
res.json(obj)
})
function updateState(data){
state = data;
state.getColor = function(){
if(this.groups[this.currentGroup].white){
return -1;
}else{
return this.groups[this.currentGroup].color;
}
}
state.getBrightness = function(){
var group = this.groups[this.currentGroup]
if(group.white){
return group.whiteBrightness;
}else{
return group.colorBrightness;
}
}
state.isOn = function(){
return this.groups[this.currentGroup].on;
}
state.isWhite = function(){
return this.groups[this.currentGroup].white;
}
client.set("state",JSON.stringify(state));
}
serv_io.sockets.on('connection',function(socket){
socket.emit('update',JSON.stringify(state));
socket.on('group',function(data){
updateState(data);
exec("python /home/pi/Milight/wrapper.py " + state.currentGroup + " ON");
});
socket.on('white',function(data){
updateState(data);
if(state.isWhite()){
exec("python /home/pi/Milight/wrapper.py " + state.currentGroup + " ON -w");
}else{
exec("python /home/pi/Milight/wrapper.py " + state.currentGroup + " ON -c " + state.getColor());
}
});
socket.on('brightness',function(data){
updateState(data);
exec("python /home/pi/Milight/wrapper.py " + state.currentGroup + " ON -b " + state.getBrightness());
});
socket.on('color',function(data){
updateState(data);
exec("python /home/pi/Milight/wrapper.py " + state.currentGroup + " ON -c " + state.getColor());
});
socket.on('power',function(data){
updateState(data);
if(state.isOn()){
exec("python /home/pi/Milight/wrapper.py " + state.currentGroup + " ON");
}else{
exec("python /home/pi/Milight/wrapper.py " + state.currentGroup + " OFF");
}
});
});