Skip to content

Commit

Permalink
Merge pull request #11 from RehanShakir/WebApp
Browse files Browse the repository at this point in the history
Web app
  • Loading branch information
Nauman3S authored Dec 22, 2021
2 parents 3732722 + c2bd9bd commit 9d5c02b
Show file tree
Hide file tree
Showing 14 changed files with 689 additions and 73 deletions.
Binary file added Web App/Backend/Backend.tar
Binary file not shown.
4 changes: 4 additions & 0 deletions Web App/Backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const app = express();
const userRoute = require("./routes/api/users");
const mqttRoute = require("./routes/api/mqtt");
const fileUploadRoute = require("./routes/api/fileUpload");
const alarmRoute = require("./routes/api/alarms");

//Database Connection
require("./db/connection");
Expand All @@ -28,6 +29,9 @@ app.use(fileUpload());
app.use("/api/users", userRoute);
app.use("/api/mqtt", mqttRoute);
app.use("/api/fileUpload", fileUploadRoute);

app.use("/api/alarm", alarmRoute);

app.use('/api/ota', express.static(path.join(__dirname, 'routes/api/files')));


Expand Down
46 changes: 23 additions & 23 deletions Web App/Backend/db/connection.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
//FOR LIVE SERVER CAPROVER Uncomment it
const mongoose = require("mongoose");

mongoose
.connect(
"mongodb://srv-captain--smart-agri-database/mydatabase?authSource=admin",
{
user: "smartAgriDatabase",
pass: "sma$r$t$Agr$iD$a$%tabase",
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => {
console.log("DB Connected");
})
.catch((err) => {
console.log("Error Connecting to Database");
console.log(err);
});

// mongoose
// .connect(
// "mongodb+srv://rehan:rehan@cluster0.qhfay.mongodb.net/sensors?retryWrites=true&w=majority",
// "mongodb://srv-captain--smart-agri-database/mydatabase?authSource=admin",
// {
// user: "smartAgriDatabase",
// pass: "sma$r$t$Agr$iD$a$%tabase",
// useNewUrlParser: true,
// useUnifiedTopology: true,
// }
// )
// .then(() => {
// console.log("Connected To MongoDB");
// console.log("DB Connected");
// })
// .catch((error) => {
// console.log("Error while connecting to Database");
// console.log(error.message);
// .catch((err) => {
// console.log("Error Connecting to Database");
// console.log(err);
// });

mongoose
.connect(
"mongodb+srv://rehan:rehan@cluster0.qhfay.mongodb.net/sensors?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => {
console.log("Connected To MongoDB");
})
.catch((error) => {
console.log("Error while connecting to Database");
console.log(error.message);
});
49 changes: 49 additions & 0 deletions Web App/Backend/models/alarm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const mongoose = require("mongoose");

// let date = new Date();

let alarmSchema = mongoose.Schema({
macAddress: {
type: String,
required: true,
},
temperature: {
max: Number,
min: Number,
},
humidity: {
max: Number,
min: Number,
},
atmosphereicPressure: {
max: Number,
min: Number,
},
soilMoisture: {
max: Number,
min: Number,
},
ec: {
max: Number,
min: Number,
},
ph: {
max: Number,
min: Number,
},
nitrogen: {
max: Number,
min: Number,
},
phosphorus: {
max: Number,
min: Number,
},
potassium: {
max: Number,
min: Number,
},
});
let alarmModel = new mongoose.model("Alarm", alarmSchema);

module.exports = alarmModel;
13 changes: 13 additions & 0 deletions Web App/Backend/models/mqttmessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ let mqttMessageSchema = mongoose.Schema(
type: String,
required: true,
},
relay1: {
type: String,
},
relay2: {
type: String,
},
relay3: {
type: String,
},
msg: {
type: String,
},

FW_Version: {
type: String,
},
Expand Down
2 changes: 1 addition & 1 deletion Web App/Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
"start": "nodemon ./bin/www"
},
"dependencies": {
"axios": "^0.22.0",
Expand Down
109 changes: 109 additions & 0 deletions Web App/Backend/routes/api/alarms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const express = require("express");
const router = express.Router();
const _ = require("lodash");
const alarmModel = require("../../models/alarm");

router.get("/:macAddress", async (req, res) => {
let macAddress = req.params.macAddress;
let alarm = await alarmModel.findOne({ macAddress });
return res.send(alarm);
});

router.post("/set/:macAddress", async (req, res) => {
try {
let { max, min } = req.body;
console.log(max);
console.log(min);
console.log(req.params.col);

let alarmSet = new alarmModel();
alarmSet.macAddress = req.params.macAddress;
alarmSet.temperature.max = max;
alarmSet.temperature.min = min;
alarmSet.humidity.max = max;
alarmSet.humidity.min = min;
alarmSet.atmosphereicPressure.max = max;
alarmSet.atmosphereicPressure.min = min;
alarmSet.soilMoisture.max = max;
alarmSet.soilMoisture.min = min;
alarmSet.ec.max = max;
alarmSet.ec.min = min;
alarmSet.ph.max = max;
alarmSet.ph.min = min;
alarmSet.nitrogen.max = max;
alarmSet.nitrogen.min = min;
alarmSet.phosphorus.max = max;
alarmSet.phosphorus.min = min;
alarmSet.potassium.max = max;
alarmSet.potassium.min = min;

await alarmSet.save();
return res.send("added");
} catch (err) {
console.error(err);
}
});
router.put("/set/:col/:macAddress", async (req, res) => {
try {
let { max, min } = req.body;

let macAddress = req.params.macAddress;

// let col = req.params.col;
console.log(max);
console.log(min);
console.log(req.params.col);
let alarm = await alarmModel.findOneAndUpdate({ macAddress });

if (req.params.col === "temperature") {
alarm.temperature.max = max;
alarm.temperature.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "humidity") {
alarm.humidity.max = max;
alarm.humidity.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "atmosphereicPressure") {
alarm.atmosphereicPressure.max = max;
alarm.atmosphereicPressure.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "soilMoisture") {
alarm.soilMoisture.max = max;
alarm.soilMoisture.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "ec") {
alarm.ec.max = max;
alarm.ec.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "ph") {
alarm.ph.max = max;
alarm.ph.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "nitrogen") {
alarm.nitrogen.max = max;
alarm.nitrogen.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "phosphorus") {
alarm.phosphorus.max = max;
alarm.phosphorus.min = min;
await alarm.save();
return res.send("Updated");
} else if (req.params.col === "potassium") {
alarm.potassium.max = max;
alarm.potassium.min = min;
await alarm.save();
return res.send("Updated");
}
} catch (err) {
console.error(err);
}
});

module.exports = router;
28 changes: 17 additions & 11 deletions Web App/Backend/routes/api/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ const host = "34.214.65.82";
const port = "1883";
const clientId = `mqtt_${Math.random().toString(16).slice(3)}`;
const connectUrl = `mqtt://${host}:${port}`;
const client = mqtt.connect(connectUrl, {
clientId,
clean: true,
connectTimeout: 4000000,
username: "hello",
password: "hello",
reconnectPeriod: 1000000,
});
let client;

//Get all Devices Data
router.get("/", async (req, res) => {
try {
Expand All @@ -41,6 +35,15 @@ router.post("/getOne", async (req, res) => {
});
router.post("/", async (req, res) => {
try {
client = mqtt.connect(connectUrl, {
clientId,
clean: true,
connectTimeout: 4000000,
username: "hello",
password: "hello",
reconnectPeriod: 1000000,
});

client.on("connect", () => {
console.log("Connected");
client.subscribe([topic], () => {
Expand All @@ -56,7 +59,12 @@ router.post("/", async (req, res) => {

let MqttMessgae = new mqttMessgae({
macAddress: macAdrs[1],
relay1: message.relay1,
relay2: message.relay2,
relay3: message.relay3,
msg: message.msg,
FW_Version: message.FW_Version,

Environment: message.Environment[0],
Soil_Parameters: message.SoilParameters[0],
});
Expand All @@ -70,11 +78,9 @@ router.post("/", async (req, res) => {

router.post("/publish/:macAddress/:button", async (req, res) => {
try {
let { message } = req.body;

// console.log(message);
console.log(req.params.macAddress);
console.log(req.params.button);
let { message } = req.body;

client.publish(
`${req.params.macAddress}/${req.params.button}`,
Expand Down
Binary file added Web App/Frontend/Frontend.tar
Binary file not shown.
14 changes: 14 additions & 0 deletions Web App/Frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Web App/Frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"antd": "^4.16.6",
"apexcharts": "^3.27.1",
"axios": "^0.24.0",
"beepbeep": "^1.3.0",
"install": "^0.13.0",
"react": "^17.0.2",
"react-apexcharts": "^1.3.9",
Expand Down
18 changes: 9 additions & 9 deletions Web App/Frontend/src/api/smartAgri.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import axios from "axios";

// export default axios.create({
// baseURL: "http://localhost:3001",
// // withCredentials: false,
// // headers: {
// // "Access-Control-Allow-Origin": "*",
// // "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS",
// // },
// });
export default axios.create({
baseURL: "https://smart-agri-backend.iot.intelligadgets.me",
baseURL: "http://localhost:3001",
// withCredentials: false,
// headers: {
// "Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS",
// },
});
// export default axios.create({
// baseURL: "https://smart-agri-backend.iot.intelligadgets.me",
// });
Loading

0 comments on commit 9d5c02b

Please sign in to comment.