Merge branch 'main' of https://github.com/Newtbot/MP
This commit is contained in:
@ -1,3 +1,31 @@
|
||||
'use strict';
|
||||
const router = require('express').Router();
|
||||
const { auth } = require("../middleware/authChecker")
|
||||
const { APIlogger } = require('../middleware/apiLogger.js');
|
||||
const { apikeyCheck } = require('../middleware/apiKey.js');
|
||||
|
||||
router.use('/auth', require('./auth'));
|
||||
|
||||
router.use('/apikey', require('./apikey'));
|
||||
|
||||
router.use('/user', [auth, APIlogger], require('./user'));
|
||||
|
||||
//TO REFACTOR INTO ONE MIDDLWARE
|
||||
|
||||
//location route
|
||||
router.use('/location', [apikeyCheck , APIlogger], require('./location.js'));
|
||||
|
||||
//location route
|
||||
router.use('/sensor', [apikeyCheck , APIlogger], require('./sensor.js'));
|
||||
|
||||
//location route
|
||||
router.use('/sensor-data', [apikeyCheck, APIlogger], require('./sensorData.js'));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
'use strict';
|
||||
const router = require('express').Router();
|
||||
const { auth } = require("../middleware/authChecker")
|
||||
@ -12,3 +40,4 @@ router.use('/user', auth ,require('./user'));
|
||||
module.exports = router;
|
||||
|
||||
|
||||
*/
|
@ -1,19 +1,9 @@
|
||||
const { getAPIKey , addAPIKey } = require("../functions/apiDatabase.js");
|
||||
const { addAPIKey } = require("../functions/api");
|
||||
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", async (req, res, next) => {
|
||||
try {
|
||||
const location = await getAPIKey();
|
||||
res.status(200).json(location);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
1) ensure user is logged in (frontend session validation blah or wtv)
|
||||
2) when user click on generate api key button, it will generate a random api key. how to get userid can be done by session or wtv
|
||||
@ -34,9 +24,6 @@ router.post("/new", async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
//update
|
||||
//delete
|
||||
//getbyid
|
||||
|
||||
module.exports = router;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
const { addUser, loginUser } = require("../functions/apiDatabase.js");
|
||||
const { addUser, loginUser } = require("../functions/user");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
77
consumerWebsite/routes/location.js
Normal file
77
consumerWebsite/routes/location.js
Normal file
@ -0,0 +1,77 @@
|
||||
const {
|
||||
addLocation,
|
||||
getLocation,
|
||||
getLocationById,
|
||||
updateLocation,
|
||||
deleteLocation,
|
||||
} = require("../functions/location");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
//get location
|
||||
router.get("/", async (req, res, next) => {
|
||||
try {
|
||||
const location = await getLocation();
|
||||
//res send json and status code
|
||||
res.status(200).json(location);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//add location
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
console.log(req.body);
|
||||
const { name, added_by, description } = req.body;
|
||||
await addLocation(name, added_by, description);
|
||||
res.sendStatus(200)
|
||||
} 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);
|
||||
res.status(200).json({ message: "Location " + id + " updated" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
//delete location
|
||||
router.delete("/delete", async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.body;
|
||||
await deleteLocation(id);
|
||||
res.status(200).json({ message: "Location " + id + " deleted" });
|
||||
} 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.status(200).json(location);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
@ -92,5 +92,9 @@ router.get("/api", function (req, res, next) {
|
||||
res.render("api");
|
||||
});
|
||||
|
||||
// sensor data
|
||||
router.get("/sensor-data", function (req, res, next) {
|
||||
res.render("sensor-data");
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router;
|
41
consumerWebsite/routes/seedLocationAndSensor.js
Normal file
41
consumerWebsite/routes/seedLocationAndSensor.js
Normal file
@ -0,0 +1,41 @@
|
||||
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 {
|
||||
console.log(mockLocation)
|
||||
|
||||
for(let locationName of req.body.mockLocation){
|
||||
//create location and create sensor
|
||||
let location = await locationModel.create({
|
||||
name: locationName,
|
||||
added_by: "system",
|
||||
description: "system generated location",
|
||||
});
|
||||
await sensorModel.create({
|
||||
name: `AQI-${Math.floor(Math.random()*898)+101}`,
|
||||
added_by: "system",
|
||||
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",
|
||||
locationid: location.id
|
||||
|
||||
});
|
||||
}
|
||||
res.sendStatus(200)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = router;
|
14
consumerWebsite/routes/seed_route.js
Normal file
14
consumerWebsite/routes/seed_route.js
Normal file
@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
const router = require('express').Router();
|
||||
|
||||
//location route
|
||||
router.use('/seedSensorData', require('./seedsensorData.js'));
|
||||
|
||||
router.use('/seed', require('./seedLocationAndSensor.js'));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = router;
|
122
consumerWebsite/routes/seedsensorData.js
Normal file
122
consumerWebsite/routes/seedsensorData.js
Normal file
@ -0,0 +1,122 @@
|
||||
const { sequelize } = require("../database/mySQL.js");
|
||||
const { locationModel } = require("../database/model/locationModel.js");
|
||||
const { sensorModel } = require("../database/model/sensorModel.js");
|
||||
const { sensorDataModel } = require("../database/model/sensorDataModel.js");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
var moment = require("moment");
|
||||
|
||||
async function seedSensorData(seedOptions) {
|
||||
seedOptions.endDate = new Date(seedOptions.endDate) || Date.now();
|
||||
seedOptions.interval = seedOptions.interval || 15; //15 minutes
|
||||
seedOptions.sensorid = seedOptions.sensorid || (await sensorModel.findAll()).map((i) => i.id);
|
||||
seedOptions.seedData = seedOptions.seedData || {};
|
||||
|
||||
let rows = [];
|
||||
|
||||
for (let sensorId of seedOptions.sensorid) {
|
||||
let sensor = await sensorModel.findByPk(sensorId);
|
||||
let locationId = sensor.locationid;
|
||||
|
||||
let currentRow = firstDataRow(seedOptions.startDate, sensorId, locationId);
|
||||
rows.push(currentRow);
|
||||
while (currentRow.createdAt <= seedOptions.endDate) {
|
||||
currentRow = nextDataRow(currentRow, seedOptions.interval);
|
||||
rows.push(currentRow);
|
||||
}
|
||||
}
|
||||
|
||||
await sensorDataModel.bulkCreate(rows)
|
||||
}
|
||||
|
||||
function convertDateToUTC(startDate) {
|
||||
let date = new Date(startDate);
|
||||
date = moment(date).utc().toDate();
|
||||
//return as object
|
||||
return date;
|
||||
}
|
||||
|
||||
//populate first row of sensordata model with random data from seedData
|
||||
function firstDataRow(startDate, sensorId, locationId) {
|
||||
return {
|
||||
sensorid: sensorId,
|
||||
locationid: locationId,
|
||||
measurement: {
|
||||
psi: Math.floor(Math.random() * 30) + 5,
|
||||
humidity: Math.floor(Math.random() * (90 - 80 + 1) + 80),
|
||||
o3: Math.floor(Math.random() * (100 - 20 + 1) + 30),
|
||||
no2: Math.floor(Math.random() * 30) + 5,
|
||||
so2: Math.floor(Math.random() * 30) + 5,
|
||||
co: Math.floor(Math.random() * 25 - 0.5),
|
||||
temperature: Math.floor(Math.random() * (30 - 23 + 1) + 25),
|
||||
windspeed: Math.floor(Math.random() * (10 - 1 + 1) + 1),
|
||||
},
|
||||
createdAt: convertDateToUTC(startDate),
|
||||
};
|
||||
}
|
||||
|
||||
function nextDataRow(currentRow, interval) {
|
||||
return {
|
||||
sensorid: currentRow.sensorid,
|
||||
locationid: currentRow.locationid,
|
||||
measurement: {
|
||||
psi: numberWithinPercent(currentRow.measurement.psi),
|
||||
humidity: Math.floor(Math.random() * (90 - 80 + 1) + 80),
|
||||
o3: numberWithinPercent(currentRow.measurement.o3),
|
||||
no2: numberWithinPercent(currentRow.measurement.no2),
|
||||
so2: numberWithinPercent(currentRow.measurement.so2),
|
||||
co: numberWithinPercent(currentRow.measurement.co),
|
||||
temperature: Math.floor(Math.random() * (30 - 23 + 1) + 25),
|
||||
windspeed: Math.floor(Math.random() * (10 - 1 + 1) + 1),
|
||||
},
|
||||
//add 15 minutes to current row time to get next row time in UTC
|
||||
createdAt: moment(currentRow.createdAt).add(interval, "m").toDate(),
|
||||
};
|
||||
}
|
||||
|
||||
function numberWithinPercent(inputNumber) {
|
||||
// Define a reasonable range for the random offset
|
||||
const maxOffset = 5;
|
||||
const minOffset = -5;
|
||||
|
||||
// Add a random offset within the defined range
|
||||
const randomOffset = Math.random() * (maxOffset - minOffset) + minOffset;
|
||||
|
||||
// Calculate the new number with the offset
|
||||
const newNumber = inputNumber + randomOffset;
|
||||
|
||||
// Ensure the new number is within a reasonable range
|
||||
return Math.max(5, Math.min(100, Math.floor(newNumber)));
|
||||
}
|
||||
|
||||
//add seed
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
const seedOptions = req.body;
|
||||
console.log(seedOptions);
|
||||
seedSensorData(seedOptions);
|
||||
res.status(200)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
/*
|
||||
POST /api/v0/seed/sensordata
|
||||
{
|
||||
"startDate" : "10-10-2010", // Date to start faking data REQUIRED
|
||||
"endDate": "10-10-2011", // Date to stop fake data, optional defaults today
|
||||
"interval": 150000, // Time in seconds between sensor polling
|
||||
"sensors": [0,1,2], // ID of sensors to fake, optional defaults to all
|
||||
"seedData": {"object of sensor data"} // The first sensor data row to start with, optional, will use random function as default
|
||||
}
|
||||
|
||||
1) firstDataRow(startDate)
|
||||
2) nextDataRow(lastRow, interval)
|
||||
3) seedSensorData({post object from abovr})
|
||||
|
||||
*/
|
66
consumerWebsite/routes/sensor.js
Normal file
66
consumerWebsite/routes/sensor.js
Normal file
@ -0,0 +1,66 @@
|
||||
const {
|
||||
getSensor,
|
||||
addSensor,
|
||||
updateSensor,
|
||||
deleteSensor,
|
||||
getSensorById
|
||||
} = require("../functions/sensor.js");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", async (req, res, next) => {
|
||||
try {
|
||||
const sensor = await getSensor();
|
||||
res.status(200).json(sensor);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
router.get("/:id", async (req, res, next) => {
|
||||
try {
|
||||
const sensor = await getSensorById(req.params.id);
|
||||
res.status(200).json(sensor);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
92
consumerWebsite/routes/sensorData.js
Normal file
92
consumerWebsite/routes/sensorData.js
Normal file
@ -0,0 +1,92 @@
|
||||
const {
|
||||
getSensorData,
|
||||
addSensorData,
|
||||
updateSensorData,
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
getData,
|
||||
getDatabyRange,
|
||||
} = require("../functions/sensorData");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", async (req, res, next) => {
|
||||
try {
|
||||
const sensor = await getSensorData();
|
||||
res.status(200).json(sensor);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
const { id_sensor, id_location, sensordata } = req.body;
|
||||
let data = await addSensorData(id_sensor, id_location, sensordata);
|
||||
res.json({ message: "SensorData " + data.id + " added", ...data });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
router.get("/data", async (req, res, next) => {
|
||||
try {
|
||||
console.log(req.query);
|
||||
const data = await getData(req.query);
|
||||
res.status(200).json(data);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
//date range
|
||||
router.get("/range", async (req, res, next) => {
|
||||
try {
|
||||
console.log(req.query);
|
||||
const data = await getDatabyRange(req.query);
|
||||
res.status(200).json(data);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
router.get("/:id", async (req, res, next) => {
|
||||
try {
|
||||
const sensor = await getSensorDataById(req.params.id);
|
||||
res.status(200).json(sensor);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
@ -1,4 +1,4 @@
|
||||
const { getUserID, updateProfile } = require("../functions/apiDatabase.js");
|
||||
const { getUserID, updateProfile } = require("../functions/user");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
Reference in New Issue
Block a user