blah
This commit is contained in:
@ -23,7 +23,7 @@ function insertData(data) {
|
||||
}
|
||||
}
|
||||
|
||||
function insertLogData(log){
|
||||
async function insertLogData(log){
|
||||
try{
|
||||
api_log_Model.create({
|
||||
ip: log.ip,
|
||||
|
@ -1,3 +1,5 @@
|
||||
var validator = require("validator");
|
||||
|
||||
// Regular expressions for data validation
|
||||
const psiPattern = /^\d+$/;
|
||||
const humidityPattern = /^\d+%$/;
|
||||
@ -8,25 +10,66 @@ const timePattern = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
||||
const regionPattern = /^[a-zA-Z-]+$/;
|
||||
|
||||
function validateData(data) {
|
||||
return (
|
||||
psiPattern.test(data.psi) &&
|
||||
humidityPattern.test(data.humidity) &&
|
||||
concentrationPattern.test(data.o3) &&
|
||||
concentrationPattern.test(data.no2) &&
|
||||
concentrationPattern.test(data.so2) &&
|
||||
concentrationPattern.test(data.co) &&
|
||||
temperaturePattern.test(data.temperature) &&
|
||||
windspeedPattern.test(data.windspeed) &&
|
||||
timePattern.test(data.time) &&
|
||||
regionPattern.test(data.region)
|
||||
);
|
||||
return (
|
||||
psiPattern.test(data.psi) &&
|
||||
humidityPattern.test(data.humidity) &&
|
||||
concentrationPattern.test(data.o3) &&
|
||||
concentrationPattern.test(data.no2) &&
|
||||
concentrationPattern.test(data.so2) &&
|
||||
concentrationPattern.test(data.co) &&
|
||||
temperaturePattern.test(data.temperature) &&
|
||||
windspeedPattern.test(data.windspeed) &&
|
||||
timePattern.test(data.time) &&
|
||||
regionPattern.test(data.region)
|
||||
);
|
||||
}
|
||||
|
||||
const dateRegex = /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
||||
const dateRegex =
|
||||
/^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
||||
|
||||
function isValidDateString(){
|
||||
return dateRegex.test(value);
|
||||
function isValidDateString(value) {
|
||||
return dateRegex.test(value);
|
||||
}
|
||||
|
||||
function isAlphaNumericwithSpaces(value) {
|
||||
return validator.isAlphanumeric(value, ["en-US"], { ignore: " " });
|
||||
}
|
||||
|
||||
module.exports = { validateData , isValidDateString };
|
||||
//allow alphanumeric and spaces and -
|
||||
function isAlphaNumericWithSpacesAndDash(value) {
|
||||
const alphanumeric = /^[a-zA-Z0-9]+$/;
|
||||
const valid = value
|
||||
.split("")
|
||||
.every((char) => alphanumeric.test(char) || char === " " || char === "-");
|
||||
return valid;
|
||||
}
|
||||
|
||||
function isMacAddress(value) {
|
||||
// Joi.string().regex(/^([0-9a-f]{2}-){5}([0-9a-f]{2})$/i).lowercase()
|
||||
//return validator.isMACAddress(value, { no_separators: true, eui: 48 });
|
||||
const macAddress = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
|
||||
const valid = macAddress.test(value);
|
||||
return valid;
|
||||
}
|
||||
|
||||
function isJson(value) {
|
||||
//check if its object
|
||||
if(typeof value === "object"){
|
||||
console.log("its an object")
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validateData,
|
||||
isValidDateString,
|
||||
isAlphaNumericwithSpaces,
|
||||
isAlphaNumericWithSpacesAndDash,
|
||||
isMacAddress,
|
||||
isJson,
|
||||
};
|
||||
/*
|
||||
isMACAddress(str [, options])
|
||||
|
||||
*/
|
||||
|
@ -1,7 +1,7 @@
|
||||
const { app } = require("./modules/express.js");
|
||||
const client = require("./modules/mqtt");
|
||||
const { validateData } = require("./functions/validateData.js");
|
||||
const { insertData } = require("./functions/Database.js");
|
||||
const { insertData } = require("./functions/database.js");
|
||||
/*
|
||||
1) validate data from IoT sensor
|
||||
2) upload data to database
|
||||
|
@ -1,4 +1,3 @@
|
||||
const { getAPIKey } = require('../db/ApiKeys');
|
||||
|
||||
function apiKeyMiddleware(req, res, next) {
|
||||
const apiKey = req.headers['x-api-key'];
|
||||
|
@ -1,20 +1,24 @@
|
||||
const { insertLogData } = require("../functions/Database.js");
|
||||
const { insertLogData } = require("../functions/database.js");
|
||||
const APIlogger = (req, res, next) => {
|
||||
|
||||
const log = {
|
||||
ip: req.ip,
|
||||
time: new Date().toUTCString(),
|
||||
method: req.method,
|
||||
//https://stackoverflow.com/questions/10183291/how-to-get-the-full-url-in-express
|
||||
host: `${req.protocol}://${req.get("host")}${req.originalUrl}`,
|
||||
statusCode: res.statusCode,
|
||||
Responsesize: res.get('Content-Length') ? res.get('Content-Length') : 0,
|
||||
referrer: res.get('content-type') ? res.get('content-type') : "none",
|
||||
userAgent: req.headers["user-agent"],
|
||||
};
|
||||
//upload to db logic here for api logs
|
||||
insertLogData(log);
|
||||
next();
|
||||
try {
|
||||
const log = {
|
||||
ip: req.ip,
|
||||
time: new Date().toUTCString(),
|
||||
method: req.method,
|
||||
//https://stackoverflow.com/questions/10183291/how-to-get-the-full-url-in-express
|
||||
host: `${req.protocol}://${req.get("host")}${req.originalUrl}`,
|
||||
statusCode: res.statusCode,
|
||||
Responsesize: res.get('Content-Length') ? res.get('Content-Length') : 0,
|
||||
referrer: res.get('content-type') ? res.get('content-type') : "none",
|
||||
userAgent: req.headers["user-agent"],
|
||||
};
|
||||
//upload to db logic here for api logs
|
||||
insertLogData(log);
|
||||
next();
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { APIlogger };
|
||||
|
@ -15,11 +15,11 @@ app.disable("x-powered-by");
|
||||
app.use(express.json());
|
||||
app.set("json spaces", 2);
|
||||
|
||||
//const { APIlogger } = require('../middleware/ApiLogger.js');
|
||||
const { APIlogger } = require('../middleware/apiLogger.js');
|
||||
|
||||
//middleware logic ( called by next() )
|
||||
//app.use('/api/v0', require('../middleware/ApiKey.js'));
|
||||
//app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
||||
app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
||||
|
||||
//route logic
|
||||
app.use("/api/v0", require("../routes/api_route.js"));
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
||||
*/
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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'));
|
||||
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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'));
|
||||
|
||||
|
||||
|
||||
|
@ -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;
|
Reference in New Issue
Block a user