From 234906ada53337c5aadab190cbc41356fdf50c47 Mon Sep 17 00:00:00 2001 From: newtbot Date: Sun, 31 Dec 2023 02:30:49 +0800 Subject: [PATCH] more work! --- Database/model/adminUserModel.js | 45 ------------ Database/model/apiLog.js | 2 +- Database/model/locationModel.js | 2 +- Database/model/sensorDataModel.js | 54 +++++++++++++++ Database/model/sensorModel.js | 54 +++++++++++++++ Database/model/userModel.js | 40 ----------- Web-Server/functions/APIDatabase.js | 102 +++++++++++++++++++++++----- Web-Server/modules/express.js | 47 +++++++++---- Web-Server/routes/Location.js | 71 +++++++++++++++++++ Web-Server/routes/Sensor.js | 64 +++++++++++++++++ Web-Server/routes/addLocation.js | 19 ------ Web-Server/routes/api_route.js | 12 ++-- Web-Server/routes/deleteLocation.js | 19 ------ Web-Server/routes/getLocation.js | 18 ----- Web-Server/routes/getLocationId.js | 20 ------ Web-Server/routes/test.js | 17 ----- Web-Server/routes/updateLocation.js | 19 ------ api.MD | 27 ++++++-- 18 files changed, 394 insertions(+), 238 deletions(-) delete mode 100644 Database/model/adminUserModel.js create mode 100644 Database/model/sensorDataModel.js create mode 100644 Database/model/sensorModel.js delete mode 100644 Database/model/userModel.js create mode 100644 Web-Server/routes/Location.js create mode 100644 Web-Server/routes/Sensor.js delete mode 100644 Web-Server/routes/addLocation.js delete mode 100644 Web-Server/routes/deleteLocation.js delete mode 100644 Web-Server/routes/getLocation.js delete mode 100644 Web-Server/routes/getLocationId.js delete mode 100644 Web-Server/routes/updateLocation.js diff --git a/Database/model/adminUserModel.js b/Database/model/adminUserModel.js deleted file mode 100644 index ab37e99..0000000 --- a/Database/model/adminUserModel.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; -const { Sequelize, DataTypes } = require('sequelize'); -const { sequelize } = require("../nySql.js") - -const adminUserModel = sequelize.define('adminusers', { - // Model attributes are defined here - id: { - type: DataTypes.INTEGER, - allowNull: false, - primaryKey: true - }, - name: { - type: DataTypes.STRING, - allowNull: true, - length: 255 - }, - username: { - type: DataTypes.STRING, - allowNull: true, - length: 50 - }, - email: { - type: DataTypes.STRING, - allowNull: true, - length: 255 - }, - password: { - type: DataTypes.STRING, - allowNull: true, - length: 255 - }, - lastLogin: { - type: DataTypes.timestamps, - allowNull: true, - }, - jobTitle: { - type: DataTypes.STRING, - allowNull: true, - length: 255 - } - },{ - timestamps: false, // Disable automatic timestamps - }); - -module.exports = { adminUserModel } \ No newline at end of file diff --git a/Database/model/apiLog.js b/Database/model/apiLog.js index b7c9b92..91cb51c 100644 --- a/Database/model/apiLog.js +++ b/Database/model/apiLog.js @@ -3,7 +3,7 @@ const { Sequelize, DataTypes } = require("sequelize"); const { sequelize } = require("../mySQL"); -sequelize.sync(); +//sequelize.sync(); const api_log_Model = sequelize.define("api-logs",{ // Model attributes are defined here id: { diff --git a/Database/model/locationModel.js b/Database/model/locationModel.js index 4997602..d0381c7 100644 --- a/Database/model/locationModel.js +++ b/Database/model/locationModel.js @@ -2,7 +2,7 @@ const { Sequelize, DataTypes } = require("sequelize"); const { sequelize } = require("../mySQL"); -sequelize.sync(); +//sequelize.sync(); const locationModel = sequelize.define( "location", { diff --git a/Database/model/sensorDataModel.js b/Database/model/sensorDataModel.js new file mode 100644 index 0000000..ef1862d --- /dev/null +++ b/Database/model/sensorDataModel.js @@ -0,0 +1,54 @@ +"use strict"; +const { Sequelize, DataTypes } = require("sequelize"); +const { sequelize } = require("../mySQL"); +const { locationModel } = require("./locationModel"); +const { sensorModel } = require("./sensorModel"); + +//sequelize.sync(); +const sensorDataModel = sequelize.define("sensorData", + { + id: { + type: DataTypes.INTEGER, + allowNull: true, + primaryKey: true, + autoIncrement: true, + }, + id_sensor: { + type: DataTypes.INTEGER, + allowNull: false, + length: 100, + //FK + references: { + model: sensorModel, + key: 'id' + } + }, + id_location: { + type: DataTypes.INTEGER, + allowNull: false, + length: 100, + //FK + references: { + model: locationModel, + key: 'id' + } + }, + Sensordata: { + type: DataTypes.JSON, + allowNull: false, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: true, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: true, + }, + }, + { + timestamps: true, + } +); + +module.exports = { sensorDataModel }; diff --git a/Database/model/sensorModel.js b/Database/model/sensorModel.js new file mode 100644 index 0000000..fdbd4c9 --- /dev/null +++ b/Database/model/sensorModel.js @@ -0,0 +1,54 @@ +"use strict"; +const { Sequelize, DataTypes } = require("sequelize"); +const { sequelize } = require("../mySQL"); +const { locationModel } = require("./locationModel"); + +//sequelize.sync(); +const sensorModel = sequelize.define("sensors", + { + id: { + type: DataTypes.INTEGER, + allowNull: true, + primaryKey: true, + autoIncrement: true, + }, + sensortype: { + type: DataTypes.STRING, + allowNull: false, + length: 10, + }, + added_by: { + type: DataTypes.STRING, + allowNull: false, + length: 10, + }, + description: { + type: DataTypes.STRING, + allowNull: true, + length: 100, + }, + location: { + type: DataTypes.INTEGER, + allowNull: true, + length: 100, + //one to many relationship + references: { + model: locationModel, + key: 'id' + } + }, + createdAt: { + type: DataTypes.DATE, + allowNull: true, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: true, + }, + }, + { + timestamps: true, + } +); + +module.exports = { sensorModel }; diff --git a/Database/model/userModel.js b/Database/model/userModel.js deleted file mode 100644 index d555fae..0000000 --- a/Database/model/userModel.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; -const { Sequelize, DataTypes } = require('sequelize'); -const { sequelize } = require("../mySql.js"); - -const userModel = sequelize.define('users', { - // Model attributes are defined here - id: { - type: DataTypes.INTEGER, - allowNull: false, - primaryKey: true - }, - name: { - type: DataTypes.STRING, - allowNull: true, - length: 255 - }, - username: { - type: DataTypes.STRING, - allowNull: true, - length: 50 - }, - email: { - type: DataTypes.STRING, - allowNull: true, - length: 255 - }, - password: { - type: DataTypes.STRING, - allowNull: true, - length: 255 - }, - lastLogin: { - type: DataTypes.timestamps, - allowNull: true, - } - },{ - timestamps: false, // Disable automatic timestamps - }); - -module.exports = { userModel } \ No newline at end of file diff --git a/Web-Server/functions/APIDatabase.js b/Web-Server/functions/APIDatabase.js index f5f5242..b20f2f1 100644 --- a/Web-Server/functions/APIDatabase.js +++ b/Web-Server/functions/APIDatabase.js @@ -1,20 +1,11 @@ const { sequelize } = require("../../Database/mySql.js"); const { IoTModel } = require("../../Database/model/IoTModel.js"); const { locationModel } = require("../../Database/model/locationModel.js"); +const { sensorModel } = require("../../Database/model/sensorModel.js"); async function getLocation() { try { - sequelize.sync(); - const location = await locationModel.findAll({ - attributes: [ - "id", - "name", - "added_by", - "description", - "createdAt", - "updatedAt", - ], - }); + const location = await locationModel.findAll(); return location; } catch (error) { console.error(error); @@ -24,7 +15,6 @@ async function getLocation() { async function addLocation(name, added_by, description) { try { - sequelize.sync(); const location = await locationModel.create({ name: name, added_by: added_by, @@ -38,7 +28,6 @@ async function addLocation(name, added_by, description) { async function updateLocation(id, name, added_by, description) { try { - sequelize.sync(); //update by id const location = await locationModel.update( { @@ -60,7 +49,6 @@ async function updateLocation(id, name, added_by, description) { async function deleteLocation(id) { try { - sequelize.sync(); //delete by id const location = await locationModel.destroy({ where: { @@ -75,8 +63,6 @@ async function deleteLocation(id) { async function getLocationById(id) { try { - sequelize.sync(); - //delete by id const location = await locationModel.findAll({ where: { id: id, @@ -89,9 +75,85 @@ async function getLocationById(id) { } } +async function getSensor() { + try { + const sensor = await sensorModel.findAll(); + return sensor; + } catch (error) { + console.error(error); + return null; + } +} + +async function addSensor(sensortype, added_by, description, location) { + try { + const sensor = await sensorModel.create({ + sensortype: sensortype, + added_by: added_by, + description: description, + location: location, + }); + } catch (error) { + console.error(error); + return null; + } +} + +async function updateSensor(id, sensortype, added_by, description, location) { + try { + //update by id + const sensor = await sensorModel.update( + { + sensortype: sensortype, + added_by: added_by, + description: description, + location: location, + + }, + { + where: { + id: id, + }, + } + ); + } catch (error) { + console.error(error); + return null; + } + +} + +async function deleteSensor(id) { + try { + //delete by id + const sensor = await sensorModel.destroy({ + where: { + id: id, + }, + }); + } catch (error) { + console.error(error); + return null; + } +} + +async function getSensorById(id) { + try { + const sensor = await sensorModel.findAll({ + where: { + id: id, + }, + }); + return sensor; + } catch (error) { + console.error(error); + return null; + } +} + + async function getallData() { try { - sequelize.sync(); const allData = await IoTModel.findAll({ attributes: [ "id", @@ -118,7 +180,6 @@ async function getallData() { async function getLatestData() { try { - sequelize.sync(); const latestData = await IoTModel.findAll({ limit: 1, order: [["createdAt", "DESC"]], @@ -138,4 +199,9 @@ module.exports = { updateLocation, deleteLocation, getLocationById, + getSensor, + addSensor, + updateSensor, + deleteSensor, + getSensorById, }; diff --git a/Web-Server/modules/express.js b/Web-Server/modules/express.js index bb91c2c..0f4c8ab 100644 --- a/Web-Server/modules/express.js +++ b/Web-Server/modules/express.js @@ -3,32 +3,55 @@ 2) enforce best practice for api routes */ const express = require("express"); -const helmet = require('helmet') +const helmet = require("helmet"); const app = express(); -app.use(helmet()) +app.use(helmet()); const port = 80; //disable x-powered-by header for security reasons -app.disable('x-powered-by') +app.disable("x-powered-by"); app.use(express.json()); -app.set('json spaces', 2); +app.set("json spaces", 2); -const { APIlogger } = require('../middleware/ApiLogger.js'); -const { json } = require("body-parser"); +//const { APIlogger } = require('../middleware/ApiLogger.js'); //middleware logic -//app.use('/api/v1', require('../middleware/ApiKey.js')); -app.use('/api/v0', APIlogger ); +//app.use('/api/v0', require('../middleware/ApiKey.js')); +//app.use('/api/v0', APIlogger, require('../routes/api_route.js')); -//route logic -app.use('/api/v0', require('../routes/api_route.js')); +//route logic +app.use("/api/v0", require("../routes/api_route.js")); +// Catch 404 and forward to error handler. If none of the above routes are +// used, this is what will be called. +app.use(function (req, res, next) { + var err = new Error("Not Found"); + err.message = "Page not found"; + err.status = 404; + next(err); +}); + +// Error handler. This is where `next()` will go on error +app.use(function (err, req, res, next) { + console.error(err.status || res.status, err.name, req.method, req.url); + if (![404].includes(err.status || res.status)) { + console.error(err.message); + console.error(err.stack); + console.error("========================================="); + } + + res.status(err.status || 500); + res.json({ + name: err.name, + message: err.message, + runner: err.runner && err.runner.name, + duration: err.duration, + }); +}); app.listen(port, () => { console.log(`app listening on port ${port}`); }); module.exports = { app }; - - diff --git a/Web-Server/routes/Location.js b/Web-Server/routes/Location.js new file mode 100644 index 0000000..9c8218a --- /dev/null +++ b/Web-Server/routes/Location.js @@ -0,0 +1,71 @@ +const { sequelize } = require("../../Database/mySql.js"); +const { locationModel } = require("../../Database/model/locationModel.js"); +const { + addLocation, + getLocation, + getLocationById, + updateLocation, + deleteLocation, +} = require("../functions/APIDatabase.js"); + +const express = require("express"); +const router = express.Router(); + +//get location +router.get("/", async (req, res, next) => { + try { + const location = await getLocation(); + res.json(location); + } catch (error) { + console.error(error); + next(error); + } +}); + +//add location +router.post("/new", async (req, res, next) => { + try { + const { name, added_by, description } = req.body; + await addLocation(name, added_by, description); + } catch (error) { + console.error(error); + next(error); + } +}); + +//update location +router.put("/update", async (req, res, next) => { + try { + const { id, name, added_by, description } = req.body; + await updateLocation(id, name, added_by, description); + } catch (error) { + console.error(error); + next(error); + } +}); + +//delete location +router.delete("/delete", async (req, res, next) => { + try { + const { id } = req.body; + await deleteLocation(id); + } catch (error) { + console.error(error); + next(error); + } +}); + +//get location by id +router.get("/:id", async (req, res, next) => { + try { + //get params + const { id } = req.params; + const location = await getLocationById(id); + res.json(location); + } catch (error) { + console.error(error); + next(error); + } +}); + +module.exports = router; diff --git a/Web-Server/routes/Sensor.js b/Web-Server/routes/Sensor.js new file mode 100644 index 0000000..2b034ab --- /dev/null +++ b/Web-Server/routes/Sensor.js @@ -0,0 +1,64 @@ +const { sequelize } = require("../../Database/mySql.js"); +const { sensorModel } = require("../../Database/model/sensorModel.js"); +const { + getSensor, + addSensor, + updateSensor, + deleteSensor, + getSensorById +} = require("../functions/APIDatabase.js"); + +const express = require("express"); +const router = express.Router(); + +router.get("/", async (req, res, next) => { + try { + const sensor = await getSensor(); + res.json(sensor); + } catch (error) { + console.error(error); + next(error); + } +}); + +router.post("/new", async (req, res, next) => { + try { + const { sensortype, added_by, description, location } = req.body; + await addSensor(sensortype, added_by, description, location); + } catch (error) { + console.error(error); + next(error); + } +}); + +router.put("/update", async (req, res, next) => { + try { + const { id, sensortype, added_by, description, location } = req.body; + await updateSensor(id, sensortype, added_by, description, location); + } catch (error) { + console.error(error); + next(error); + } +}); + +router.delete("/delete", async (req, res, next) => { + try { + const { id } = req.body; + await deleteSensor(id); + } catch (error) { + console.error(error); + next(error); + } +}); + +router.get("/:id", async (req, res, next) => { + try { + const sensor = await getSensorById(req.params.id); + res.json(sensor); + } catch (error) { + console.error(error); + next(error); + } +}); + +module.exports = router; diff --git a/Web-Server/routes/addLocation.js b/Web-Server/routes/addLocation.js deleted file mode 100644 index 12b1170..0000000 --- a/Web-Server/routes/addLocation.js +++ /dev/null @@ -1,19 +0,0 @@ -const { sequelize } = require("../../Database/mySql.js"); -const { locatioModel } = require("../../Database/model/locationModel.js"); -const { addLocation } = require("../functions/APIDatabase.js"); - -const express = require('express'); -const router = express.Router(); - -router.post('/', async (req, res) => { - try { - const {name , added_by , description } = req.body; - await addLocation(name, added_by, description); - - } catch (error) { - console.error(error); - } - }); - - // Export the router - module.exports = router; \ No newline at end of file diff --git a/Web-Server/routes/api_route.js b/Web-Server/routes/api_route.js index 2289204..898d228 100644 --- a/Web-Server/routes/api_route.js +++ b/Web-Server/routes/api_route.js @@ -18,11 +18,10 @@ module.exports = router; const router = require('express').Router(); //location route -router.use('/location', require('./getLocation')); -router.use('/add-location', require('./addLocation')); -router.use('/update-location', require('./updateLocation')); -router.use('/delete-location', require('./deleteLocation')); -router.use('/', require('./getLocationId')); +router.use('/location', require('./Location')); + +//sensor route +router.use('/sensor', require('./Sensor')) @@ -31,4 +30,7 @@ router.use('/test' , require('./test')); router.use('/latest-data', require('./latest-data')); router.use('/:month', require('./monthlyData')); + + + module.exports = router; \ No newline at end of file diff --git a/Web-Server/routes/deleteLocation.js b/Web-Server/routes/deleteLocation.js deleted file mode 100644 index b68a3af..0000000 --- a/Web-Server/routes/deleteLocation.js +++ /dev/null @@ -1,19 +0,0 @@ -const { sequelize } = require("../../Database/mySql.js"); -const { locatioModel } = require("../../Database/model/locationModel.js"); -const { deleteLocation } = require("../functions/APIDatabase.js"); - -const express = require('express'); -const router = express.Router(); - -router.delete('/', async (req, res) => { - try { - const {id} = req.body; - await deleteLocation(id); - - } catch (error) { - console.error(error); - } - }); - - // Export the router - module.exports = router; \ No newline at end of file diff --git a/Web-Server/routes/getLocation.js b/Web-Server/routes/getLocation.js deleted file mode 100644 index 38ae348..0000000 --- a/Web-Server/routes/getLocation.js +++ /dev/null @@ -1,18 +0,0 @@ -const { sequelize } = require("../../Database/mySql.js"); -const { locatioModel } = require("../../Database/model/locationModel.js"); -const { getLocation } = require("../functions/APIDatabase.js"); - -const express = require('express'); -const router = express.Router(); - -router.get('/', async (req, res) => { - try { - const location = await getLocation(); - res.json(location); - } catch (error) { - console.error(error); - } - }); - - // Export the router - module.exports = router; \ No newline at end of file diff --git a/Web-Server/routes/getLocationId.js b/Web-Server/routes/getLocationId.js deleted file mode 100644 index 88d6730..0000000 --- a/Web-Server/routes/getLocationId.js +++ /dev/null @@ -1,20 +0,0 @@ -const { sequelize } = require("../../Database/mySql.js"); -const { locatioModel } = require("../../Database/model/locationModel.js"); -const { getLocationById } = require("../functions/APIDatabase.js"); - -const express = require('express'); -const router = express.Router(); - -router.get('/:id', async (req, res) => { - try { - //get params - const { id } = req.params; - const location = await getLocationById(id); - res.json(location); - } catch (error) { - console.error(error); - } - }); - - // Export the router - module.exports = router; \ No newline at end of file diff --git a/Web-Server/routes/test.js b/Web-Server/routes/test.js index 25f2101..efc202e 100644 --- a/Web-Server/routes/test.js +++ b/Web-Server/routes/test.js @@ -5,23 +5,6 @@ const { getallData } = require("../functions/APIDatabase.js"); const express = require('express'); const router = express.Router(); -/* -async function getallData() { - try { - sequelize.sync(); - const allData = await IoTModel.findAll({ - attributes: ['id', 'psiData', 'humidityData', 'o3Data', 'no2Data', 'so2Data', 'coData', 'temperatureData', 'windspeedData', 'currentTime', 'regionData' , 'createdAt' , 'updatedAt'], - }); - return allData; - } - catch(error) { - console.error(error); - return null; - - } -} -*/ - router.get('/', async (req, res) => { try { const data = await getallData(); diff --git a/Web-Server/routes/updateLocation.js b/Web-Server/routes/updateLocation.js deleted file mode 100644 index ff3829a..0000000 --- a/Web-Server/routes/updateLocation.js +++ /dev/null @@ -1,19 +0,0 @@ -const { sequelize } = require("../../Database/mySql.js"); -const { locatioModel } = require("../../Database/model/locationModel.js"); -const { updateLocation } = require("../functions/APIDatabase.js"); - -const express = require('express'); -const router = express.Router(); - -router.put('/', async (req, res) => { - try { - const {id , name , added_by , description } = req.body; - await updateLocation(id , name, added_by , description); - - } catch (error) { - console.error(error); - } - }); - - // Export the router - module.exports = router; \ No newline at end of file diff --git a/api.MD b/api.MD index 3bd6f87..b1ce947 100644 --- a/api.MD +++ b/api.MD @@ -1,14 +1,33 @@ +//location //get all curl localhost/api/v0/location //get id -curl localhost/api/v0/1 +http://localhost/api/v0/location/3 //post -curl localhost/api/v0/add-location -H "Content-Type: application/json" -X POST -d '{"name": "test", "added_by": "noot" , "description": "test"}' +curl localhost/api/v0/location/new -H "Content-Type: application/json" -X POST -d '{"name": "test", "added_by": "noot" , "description": "test"}' //put - curl localhost/api/v0/update-location -X PUT -H "Content-Type: application/json" -d '{"id": "1" , "name": "test", "added_by": "noot", "description": "test123"}' + curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}' //delete -curl localhost/api/v0/delete-location -X DELETE -H "Content-Type: application/json" -d '{"id": "1" }' +curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }' + +//sensor +//GET all +curl localhost/api/v0/sensor + +//get id +curl http://localhost/api/v0/sensor/3 + +//POST + curl localhost/api/v0/sensor/new -H "Content-Type: application/json" -X POST -d '{"sensortype": "test", "added_by": "noot" , "description": "test" , "location": "2"}' + + //put + curl localhost/api/v0/sensor/update -H "Content-Type: application/json" -X PUT -d '{"id": "2" ,"sensortype": "test", "added_by": "noot" , "description": "test123333333" , "location": "3" }' + +//delete +curl localhost/api/v0/sensor/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "2" }' + +//sensor data