add: added mosquitto and front alerts
This commit is contained in:
parent
78a6d26984
commit
cb9a0c27b4
11 changed files with 948 additions and 10 deletions
|
@ -1,6 +1,22 @@
|
|||
const express = require("express");
|
||||
const app = express();
|
||||
const http = require("http");
|
||||
const mqtt = require("mqtt");
|
||||
const mysql = require("mysql");
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
const client = mqtt.connect("mqtt://localhost/", {
|
||||
port: 1883,
|
||||
protocol: "mqtt",
|
||||
clientId: "unidite_traitement",
|
||||
username: "student",
|
||||
password: "ensibs"
|
||||
});
|
||||
|
||||
client.on("error", (err) => {
|
||||
console.error("MQTT connection error:", err);
|
||||
});
|
||||
|
||||
http.get("http://192.168.4.1/", (cameraRes) => {
|
||||
app.get("/api/video", (req, res) => {
|
||||
|
@ -9,11 +25,55 @@ http.get("http://192.168.4.1/", (cameraRes) => {
|
|||
});
|
||||
|
||||
cameraRes.on("data", (chunk) => {
|
||||
res.write(chunk)
|
||||
var string = chunk.toString();
|
||||
res.write(chunk);
|
||||
});
|
||||
|
||||
cameraRes.on("end", () => res.end());
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/api/alert", (req, res) => {
|
||||
const { sensor_id, level, text } = req.body;
|
||||
|
||||
const connection = mysql.createConnection({
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "IloveSachAwAmama69",
|
||||
database: "sachamama"
|
||||
});
|
||||
|
||||
connection.connect((err) => {
|
||||
if (err) {
|
||||
console.error("Error connecting to MySQL:", err);
|
||||
return res.status(500).json({ error: "Database connection failed" });
|
||||
}
|
||||
});
|
||||
|
||||
const query = "INSERT INTO alerts (sensor_id, time, level, text) VALUES (?, ?, ?, ?)";
|
||||
|
||||
const values = [
|
||||
sensor_id,
|
||||
new Date().toISOString().slice(0, 19).replace("T", " "),
|
||||
level,
|
||||
text
|
||||
];
|
||||
|
||||
connection.query(query, values, (err, results) => {
|
||||
if (err) {
|
||||
console.error("Error inserting alert into database:", err);
|
||||
return res.status(500).json({ error: "Database insertion failed" });
|
||||
}
|
||||
});
|
||||
|
||||
client.publish("alert", "alert", { qos: 1 }, (err) => {
|
||||
if (err) {
|
||||
console.error("Error publishing alert:", err);
|
||||
return res.status(500).json({ error: "Failed to send alert" });
|
||||
}
|
||||
});
|
||||
|
||||
res.status(200).json({ message: "Alert sent successfully" });
|
||||
});
|
||||
|
||||
app.listen(3000, () => console.log("Server running on http://localhost:3000"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue