-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (93 loc) · 3 KB
/
server.js
File metadata and controls
112 lines (93 loc) · 3 KB
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
/* EXPRESS | REST API */
// CONST VARS
const express = require("express");
const socket = require("socket.io");
const MQTT_client = require("mqtt").connect("mqtt://broker.hivemq.com");
const app = express();
const PORT = process.env.PORT || 5000;
// Route Handler
const databaseRouter = require("./routes/api");
// Simple authentication
const AUTH_KEY = "SECRETBOJELLEBENJIGIP6TEA";
let AUTH = true;
// State handler
var State = {
motor: false,
autoRefresh: false,
iTemp: "20",
fetchAmount: "15",
};
// Initialize our express app with the correct port!
const server = app.listen(PORT, () =>
console.log(`Server is listening on port ${PORT}...`)
);
// Enable JSON to be used in our backend api!
app.use(express.json());
// Define the about route.
app.get("/about", (req, res) => {
res.send(
"<h1>About our webpage!</h1>\n" +
`<a href="/api"><h4>Go to /api : to see our home api page</h4></a>\n`
);
});
// Info on how the login works! (simple security)
app.get("/login", (req, res) => {
res.send("<h1>Login/[SECRET KEY HERE TO ENABLE THE API]</h1>");
});
// The login route (u can provide a key to login), this will enable the home page!
app.get("/login/:key", (req, res) => {
if (AUTH === true) return res.send("<h1>You'r already logged in!</h1>");
if (req.params.key === AUTH_KEY) {
AUTH = true;
res.send("<h1>Succesfull login!</h1>");
} else {
AUTH = false;
res.send("<h1>incorrect secret!</h1>");
}
});
// Disable the authentication (aka. the home page)!
app.get("/logout", (req, res) => {
if (AUTH === false) return res.send("<h1>You'r already logged out!</h1>");
AUTH = false;
res.send("<h1>Succesfully logged out!</h1>");
});
// Check if you have been authorized!
app.use((req, res, next) => {
if (AUTH === true) {
next();
}
});
// Enable the api Router!
app.use("/api", databaseRouter);
// Enable our static website so they can load when a user makes a request to '/'!
app.use(express.static("public"));
/* -- SOCKET.IO SETUP -- */
const io = socket(server);
/* -- SOCKET FUNCTIONALITY -- */
// Runs when a new connection is made!
io.on("connection", socket => {
// Logs the id from the client that made a connection.
console.log("connection: ", socket.id);
// Sends immediatly the Controls state back to the client.
io.sockets.emit("StateToClient", State);
// Listens if there are changes happening to the control state from our website.
socket.on("StateToServer", state => {
State = state;
io.sockets.emit("StateToClient", State);
});
});
/* -- CUSTOM FUNCTIONS -- */
// This will send a set_temp to the MQTT broker!
function msgToLoraC() {
MQTT_client.publish("dragino-1e9d94/cmd", `C${State.iTemp}`);
}
// This will send a command to start and stop the motor to the MQTT broker!
function msgToLora() {
State.motor
? MQTT_client.publish("dragino-1e9d94/cmd", "A")
: MQTT_client.publish("dragino-1e9d94/cmd", "B");
// this will msgToLoraC run after a second
setTimeout(msgToLoraC, 1000);
}
// Run every 2 seconds
setInterval(msgToLora, 2000);