-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
165 lines (141 loc) · 3.49 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
/*
Hydroponic IoT Backend
Control of several
*/
var devices = []; // list of connected iot devices
// shared configs
var liveConfig = {
track: {
ec: true,
temperature: true,
lightTime: true
},
power: {
waterPumps: false,
airPumps: false,
lamp1: false,
lamp2: false,
lamp3: false
}
};
/*
Simple HTTP Server
*/
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.json(liveConfig);
});
/*
All Components control (for shutdown, e.q)
*/
app.get('/all/off', (req, res) => {
for (var component in liveConfig.power) {
if (Object.prototype.hasOwnProperty.call(liveConfig.power, component)) {
liveConfig.power[component] = false;
}
}
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
app.get('/all/on', (req, res) => {
for (var component in liveConfig.power) {
if (Object.prototype.hasOwnProperty.call(liveConfig.power, component)) {
liveConfig.power[component] = true;
}
}
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
/*
Water Pump control
*/
app.get('/water/off', (req, res) => {
liveConfig.power.waterPumps = false;
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
app.get('/water/on', (req, res) => {
liveConfig.power.waterPumps = true;
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
/*
Air Pump control
*/
app.get('/air/off', (req, res) => {
liveConfig.power.airPumps = false;
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
app.get('/air/on', (req, res) => {
liveConfig.power.airPumps = true;
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
/*
Lamp control
*/
for(let i = 1; i <= 3; i++){
app.get('/lamp'+i+'/off', (req, res) => {
liveConfig.power['lamp'+i] = false;
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
app.get('/lamp'+i+'/on', (req, res) => {
liveConfig.power['lamp'+i] = true;
devices.forEach(function(d) {
// turn pumps off and send to client devices
d.send(JSON.stringify(liveConfig));
});
res.json(liveConfig);
});
}
app.listen(8080, () => {
console.log('Backend API listening on port 8080.');
});
/*
WebSocket
On Connection
*/
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 3000 });
wss.on('connection', (ws) => {
console.log("New IoT Device Connection");
// push to known devices
devices.push(ws);
// send current config
ws.send(JSON.stringify(liveConfig));
/* receive data */
ws.on('message', function incoming(datastring) {
let data = JSON.parse(datastring);
if(data.method == "collect"){
console.log("Received Data:",datastring);
}
});
});
console.log('Backend Websocket listening on port 3000.');