-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
170 lines (156 loc) · 4.49 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
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
const express = require("express");
const app = express();
const http = require("http");
var cors = require('cors');
const server = http.createServer(app);
const path = require("path");
var compression = require('compression');
var expressStaticGzip = require('express-static-gzip');
var port = process.env.PORT || 3000;
//Import the insert,read and createTable from database.js
const { insert, read, db} = require("./database.js");
//To Create the table if it does not already exist
db.run("CREATE TABLE IF NOT EXISTS weather (date TEXT, temperature REAL, humidity REAL)");
//Create a table fan state if it does not already exist
db.run("CREATE TABLE IF NOT EXISTS fan (state TEXT)");
//Insert the initial fan state
db.run("INSERT INTO fan VALUES ('true')");
//MiddleWare
app.use(compression()); //Compress all routes
app.use('/', expressStaticGzip(path.join(__dirname, "dist"), {
enableBrotli: true,
orderPreference: ['br', 'gz'],
}));
app.use(cors());
app.use(express.json());
app.get("/", (req, res) => {
res.sendFile(__dirname + "/dist/index.html");
});
app.get("/project", (req, res) => {
res.sendFile(__dirname + "/dist/project.html");
});
app.get("/home",(req,res)=>{
res.sendFile(__dirname + "/dist/home.html");
});
app.get("/slides", (req, res) => {
res.sendFile(__dirname + "/dist/slides.html");
});
//Create an API endpoint to get all the data from the database
app.get("/api/getReading", (req, res) => {
read().then(data => {
res.json(data);
}).catch(err => {
console.log(err);
});
});
//Create an API endpoint to read the last row of the fan state table
app.get("/api/getFanState", (req, res) => {
db.all("SELECT * FROM fan LIMIT 1", (err, rows) => {
if (err) {
console.log(err);
} else {
res.json(rows);
}
});
});
//Create a Map to store the socket.id connection related to the ESP8266
var connected = new Map();
//Error In Handling MiddleWare
app.use(function (err, req, res, next) {
res.status(422).send({ error: err.message });
});
//Socket.IO
const io = require("socket.io")(server, {
cors: {
credentials: true
}
});
io.on("connection", (socket) => {
//Show the socket its id
console.log("Socket id: " + socket.id);
console.log("a user connected");
socket.on("disconnect", () => {
console.log("Socket id: " + socket.id);
if(connected.get("ESP8266") == socket.id){
console.log(connected);
connected.delete("ESP8266");
io.emit("ESP8266", false);
console.log("ESP8266 Disconnected");
}
else
console.log("user disconnected");
});
socket.on("Fan State", (msg) => {
//Insert the fan state into the database
db.run("DELETE FROM fan;");
db.run("INSERT INTO fan VALUES (?)", msg);
io.emit("Fan State", msg);
console.log("Fan State " + msg);
});
socket.on("Sensor", (msg) => {
//Set the connected state of the ESP8266
if(!connected.has("ESP8266")){
connected.set("ESP8266",socket.id);
console.log("ESP8266 Connected");
console.log(connected);
}
io.emit("Sensor", msg);
//Parse the JSON msg into temperature and humidity
var temp = msg.temperature;
var hum = msg.humidity;
//Insert the data into the database
insert(temp, hum);
console.log("Temperature: " + temp + " Humidity: " + hum);
});
socket.on("Manual",(msg) => {
//convert the percentage to a pwm value
io.emit("Manual", msg);
console.log("Manual:");
var pwm = Math.round(msg * 255 / 100);
if(pwm < 0){
pwm = 0;
}
else if(pwm > 255){
pwm = 255;
}
console.log("Speed: "+msg);
console.log("PWM: " + pwm);
//Send the pwm value to the arduino
io.emit("PWM", pwm);
});
socket.on("PWM",(msg) => {
console.log("PWM: " + msg);
});
socket.on("Auto",(msg) => {
io.emit("Auto", msg);
console.log("Auto: " + msg);
if(msg){
read().then(data => {
var temp = data.temperature;
var hum = data.humidity;
var speed = (temp-20)*(hum-50)/100;
if(speed < 0){
speed = 0;
}
else if(speed > 100){
speed = 100;
}
var pwm = Math.round(speed * 255);
//Check if the pwm value is within the range of 0-255
if(pwm < 0){
pwm = 0;
}
else if(pwm > 255){
pwm = 255;
}
//Send the pwm value to the arduino
io.emit("PWM", pwm);
console.log("Speed: "+speed);
console.log("PWM: " + pwm);
});
}
});
});
server.listen(port, () => {
console.log("listening on *:"+port);
});