This commit is contained in:
newtbot
2024-01-04 15:32:46 +08:00
parent db1b513ad5
commit 1819956bd0
21 changed files with 1541 additions and 642 deletions

View File

@ -6,7 +6,7 @@ const {
getLocationById,
updateLocation,
deleteLocation,
} = require("../functions/APIDatabase.js");
} = require("../functions/apiDatabase.js");
const express = require("express");
const router = express.Router();
@ -15,7 +15,8 @@ const router = express.Router();
router.get("/", async (req, res, next) => {
try {
const location = await getLocation();
res.json(location);
//res send json and status code
res.status(200).json(location);
} catch (error) {
console.error(error);
next(error);
@ -64,7 +65,7 @@ router.get("/:id", async (req, res, next) => {
//get params
const { id } = req.params;
const location = await getLocationById(id);
res.json(location);
res.status(200).json(location);
} catch (error) {
console.error(error);
next(error);

View File

@ -22,14 +22,13 @@ router.post("/new", async (req, res, next) => {
await sensorModel.create({
sensorname: `AQI-${Math.floor(Math.random()*898)+101}`,
added_by: "system",
//random mac address
mac_address: `${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}`,
description: "system generated sensor",
location: location.id
});
}
res.sendStatus(200).json({message: "seeded"})
res.sendStatus(200)
} catch (error) {
console.error(error);
next(error);

View File

@ -0,0 +1,34 @@
const { sequelize } = require("../../Database/mySql.js");
const { locationModel } = require("../../Database/model/locationModel.js");
const { sensorModel } = require("../../Database/model/sensorModel.js");
const express = require("express");
const router = express.Router();
let mockLocation = []
//add seed
router.post("/new", async (req, res, next) => {
try {
} catch (error) {
console.error(error);
next(error);
}
});
module.exports = router;
/*
1) get id of sensor and location
2) mock data
3) take post req for start date and end date
4) post to db
that takes startDate and an option endDate (the defaults to day) and an option interval to declare the polling interval
i would have more field TBH, that i would start with that
*/

View File

@ -6,7 +6,7 @@ const {
updateSensor,
deleteSensor,
getSensorById
} = require("../functions/APIDatabase.js");
} = require("../functions/apiDatabase.js");
const express = require("express");
const router = express.Router();
@ -14,7 +14,7 @@ const router = express.Router();
router.get("/", async (req, res, next) => {
try {
const sensor = await getSensor();
res.json(sensor);
res.status(200).json(sensor);
} catch (error) {
console.error(error);
next(error);
@ -25,6 +25,7 @@ router.post("/new", async (req, res, next) => {
try {
const { sensorname, added_by, mac_address , description, location } = req.body;
await addSensor(sensorname, added_by, mac_address ,description, location);
res.sendStatus(200)
} catch (error) {
console.error(error);
next(error);
@ -35,6 +36,7 @@ router.put("/update", async (req, res, next) => {
try {
const { id, sensorname, added_by, mac_address ,description, location } = req.body;
await updateSensor(id, sensorname, added_by, mac_address , description, location);
res.status(200).json({ message: "Sensor " + id + " updated" });
} catch (error) {
console.error(error);
next(error);
@ -45,6 +47,7 @@ router.delete("/delete", async (req, res, next) => {
try {
const { id } = req.body;
await deleteSensor(id);
res.status(200).json({ message: "Sensor " + id + " deleted" });
} catch (error) {
console.error(error);
next(error);
@ -54,7 +57,8 @@ router.delete("/delete", async (req, res, next) => {
router.get("/:id", async (req, res, next) => {
try {
const sensor = await getSensorById(req.params.id);
res.json(sensor);
res.status(200).json(sensor);
} catch (error) {
console.error(error);
next(error);

View File

@ -7,7 +7,7 @@ const {
deleteSensorData,
getSensorDataById,
} = require("../functions/APIDatabase.js");
} = require("../functions/apiDatabase.js");
const express = require("express");
const { json } = require("body-parser");
@ -16,7 +16,7 @@ const router = express.Router();
router.get("/", async (req, res, next) => {
try {
const sensor = await getSensorData();
res.json(sensor);
res.status(200).json(sensor);
} catch (error) {
console.error(error);
next(error);
@ -25,10 +25,9 @@ router.get("/", async (req, res, next) => {
router.post("/new", async (req, res, next) => {
try {
//JSON.parse(d) /* d is the parameter of the method 'add()' */
const { id, id_sensor, id_location, sensordata } = req.body;
await addSensorData(id , id_sensor , id_location , sensordata);
res.sendStatus(200).json({message: "SensorData " + id + " added" });
} catch (error) {
console.error(error);
next(error);
@ -39,6 +38,7 @@ router.put("/update", async (req, res, next) => {
try {
const { id , id_sensor , id_location , sensordata } = req.body;
await updateSensorData( id , id_sensor , id_location , sensordata );
res.status(200).json({ message: "SensorData " + id + " updated" });
} catch (error) {
console.error(error);
next(error);
@ -49,6 +49,7 @@ router.delete("/delete", async (req, res, next) => {
try {
const { id } = req.body;
await deleteSensorData(id);
res.status(200).json({ message: "SensorData " + id + " deleted" });
} catch (error) {
console.error(error);
next(error);
@ -58,7 +59,7 @@ router.delete("/delete", async (req, res, next) => {
router.get("/:id", async (req, res, next) => {
try {
const sensor = await getSensorDataById(req.params.id);
res.json(sensor);
res.status(200).json(sensor);
} catch (error) {
console.error(error);
next(error);

View File

@ -18,18 +18,14 @@ module.exports = router;
const router = require('express').Router();
//location route
router.use('/location', require('./Location'));
router.use('/location', require('./location'));
//sensor route
router.use('/sensor', require('./Sensor'))
router.use('/sensor', require('./sensor'))
//sensor data route
router.use('/sensor-data', require('./SensorData'));
router.use('/sensor-data', require('./sensorData'));
router.use('/test' , require('./test'));
router.use('/latest-data', require('./latest-data'));

View File

@ -1,28 +1,9 @@
const { sequelize } = require("../../Database/mySql.js");
const { IoTModel } = require("../../Database/model/IoTModel.js");
const { getLatestData } = require("../functions/APIDatabase.js");
const { getLatestData } = require("../functions/apiDatabase.js");
const express = require('express');
const router = express.Router();
// Logic for model and API by 1
/*
async function getLatestData() {
try {
sequelize.sync();
const latestData = await IoTModel.findAll({
limit: 1,
order: [['createdAt', 'DESC']]
});
return latestData;
}
catch (error) {
console.error(error);
return null;
}
}
*/
router.get('/', async (req, res) => {
try {
const data = await getLatestData();

View File

@ -2,7 +2,9 @@
const router = require('express').Router();
//location route
router.use('/seed', require('./SeedLocationAndSensor'));
router.use('/seed', require('./seedLocationAndSensor'));
router.use('/seedSensorData ', require('./seedsensorData'));

View File

@ -1,25 +0,0 @@
const { sequelize } = require("../../Database/mySql.js");
const { IoTModel } = require("../../Database/model/IoTModel.js");
const { getallData } = require("../functions/APIDatabase.js");
const express = require('express');
const router = express.Router();
router.get('/', async (req, res) => {
try {
const data = await getallData();
if (data === null) {
res.status(404).send("No data found");
} else {
res.send(data);
}
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
// Export the router
module.exports = router;