Update API routes and sensor data functionality And API.ejs
This commit is contained in:
parent
8836a3cfd4
commit
129ca202c0
25
api.MD
25
api.MD
@ -84,13 +84,13 @@ curl localhost/api/v0/sensor-data/new -H "Content-Type: application/json" -X POS
|
|||||||
//put
|
//put
|
||||||
curl localhost/api/v0/sensor-data/update -H "Content-Type: application/json" -X PUT -d '{"id": "1", "id_sensor": "1" , "id_location": "3" , "sensordata": {
|
curl localhost/api/v0/sensor-data/update -H "Content-Type: application/json" -X PUT -d '{"id": "1", "id_sensor": "1" , "id_location": "3" , "sensordata": {
|
||||||
"psi": "500",
|
"psi": "500",
|
||||||
"humidity": "11%",
|
"humidity": "11",
|
||||||
"o3": "326ppm",
|
"o3": "326",
|
||||||
"no2": "445ppm",
|
"no2": "445",
|
||||||
"so2": "511ppm",
|
"so2": "511",
|
||||||
"co": "16ppm",
|
"co": "16",
|
||||||
"temperature": "25C",
|
"temperature": "25",
|
||||||
"windspeed": "2km/h",
|
"windspeed": "2",
|
||||||
}}'
|
}}'
|
||||||
|
|
||||||
//delete
|
//delete
|
||||||
@ -160,10 +160,15 @@ Hour = 1 or wtv
|
|||||||
curl 'http://localhost/api/v0/sensor-data/data?year=2023&month=1&week=1&day=1&sensorid=1&locationid=1'
|
curl 'http://localhost/api/v0/sensor-data/data?year=2023&month=1&week=1&day=1&sensorid=1&locationid=1'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//get specific data
|
//get specific data
|
||||||
http://localhost/api/v0/sensor-data/filter?windspeed=highest&limit=1
|
http://localhost/api/v0/sensor-data/data?psi=highest
|
||||||
|
|
||||||
|
//avg
|
||||||
|
http://localhost/api/v0/sensor-data/data?avg=temperature
|
||||||
|
|
||||||
|
//sum
|
||||||
|
http://localhost/api/v0/sensor-data/data?sum=temperature
|
||||||
|
|
||||||
|
|
||||||
//pagination
|
//pagination
|
||||||
http://localhost/api/v0/sensor-data/data?week=1&sensorid=1&locationid=1&page=2&pagesize=10
|
http://localhost/api/v0/sensor-data/data?week=1&sensorid=1&locationid=1&page=2&pagesize=10
|
||||||
|
@ -6,7 +6,7 @@ const ejs = require("ejs");
|
|||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|
||||||
//process.nextTick(() => require("./mqttApp"));
|
process.nextTick(() => require("./mqttApp"));
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.set("json spaces", 2);
|
app.set("json spaces", 2);
|
||||||
@ -68,7 +68,6 @@ app.use(function (err, req, res, next) {
|
|||||||
keyErrors[item.path] = item.message;
|
keyErrors[item.path] = item.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.status = 422;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (![404, 401, 422].includes(err.status || res.status)) {
|
if (![404, 401, 422].includes(err.status || res.status)) {
|
||||||
@ -77,7 +76,6 @@ app.use(function (err, req, res, next) {
|
|||||||
console.error("=========================================");
|
console.error("=========================================");
|
||||||
}
|
}
|
||||||
res.status(err.status || 500);
|
res.status(err.status || 500);
|
||||||
// res.status(err.status || 500);
|
|
||||||
|
|
||||||
if (req.get('Content-Type') && req.get('Content-Type').includes("json")) {
|
if (req.get('Content-Type') && req.get('Content-Type').includes("json")) {
|
||||||
res.json({
|
res.json({
|
||||||
|
@ -8,6 +8,7 @@ async function getTokenByToken(token) {
|
|||||||
const splitAuthToken = token.split("-");
|
const splitAuthToken = token.split("-");
|
||||||
const rowid = splitAuthToken[0];
|
const rowid = splitAuthToken[0];
|
||||||
const suppliedToken = splitAuthToken.slice(1).join("-");
|
const suppliedToken = splitAuthToken.slice(1).join("-");
|
||||||
|
if (!suppliedToken) return false;
|
||||||
|
|
||||||
token = await tokenModel.findByPk(rowid, { include: userModel });
|
token = await tokenModel.findByPk(rowid, { include: userModel });
|
||||||
|
|
||||||
|
@ -18,17 +18,17 @@ async function getSensorData() {
|
|||||||
const sensorData = await sensorDataModel.findAll();
|
const sensorData = await sensorDataModel.findAll();
|
||||||
return sensorData;
|
return sensorData;
|
||||||
}
|
}
|
||||||
async function addSensorData(id_sensor, id_location, sensordata) {
|
async function addSensorData(sensorid , locationid , measurement) {
|
||||||
const sensorData = await sensorDataModel.create({
|
const sensorData = await sensorDataModel.create({
|
||||||
sensorid: id_sensor,
|
sensorid: sensorid,
|
||||||
locationid: id_location ,
|
locationid: locationid,
|
||||||
measurement: sensordata.measurement,
|
measurement: measurement
|
||||||
});
|
});
|
||||||
//console.log("sensorData", sensorData);
|
//console.log("sensorData", sensorData);
|
||||||
//console.log("sensorData", sensordata.measurement);
|
//console.log("sensorData", sensordata.measurement);
|
||||||
|
//console.log("sensorData", sensorData.measurement);
|
||||||
|
|
||||||
io().emit('sensorData:new', sensordata)
|
io().emit('sensorData:new', sensorData.measurement);
|
||||||
return sensorData;
|
return sensorData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -674,6 +674,7 @@ async function getDatabyRange(queryString) {
|
|||||||
queryString.limit = queryString.pagesize;
|
queryString.limit = queryString.pagesize;
|
||||||
|
|
||||||
whereDate = {};
|
whereDate = {};
|
||||||
|
ormQuery = {};
|
||||||
for (let query in queryString) {
|
for (let query in queryString) {
|
||||||
if (buildFunc[query]) {
|
if (buildFunc[query]) {
|
||||||
await buildFunc[query](queryString);
|
await buildFunc[query](queryString);
|
||||||
@ -700,6 +701,8 @@ async function getDatabyRange(queryString) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
whereDate = {};
|
whereDate = {};
|
||||||
|
ormQuery = {};
|
||||||
|
|
||||||
for (let query in queryString) {
|
for (let query in queryString) {
|
||||||
if (buildFunc[query]) {
|
if (buildFunc[query]) {
|
||||||
await buildFunc[query](queryString);
|
await buildFunc[query](queryString);
|
||||||
|
@ -15,12 +15,11 @@ body {
|
|||||||
}
|
}
|
||||||
.wrapper {
|
.wrapper {
|
||||||
position: relative;
|
position: relative;
|
||||||
max-width: 470px;
|
max-width: 600px; /* Increase the maximum width */
|
||||||
width: 100%;
|
width: 100%;
|
||||||
border-radius: 12px;
|
border-radius: 12px;
|
||||||
padding: 20px
|
padding: 40px 50px 150px; /* Adjust the padding */
|
||||||
30px
|
|
||||||
120px;
|
|
||||||
background: #4070f4;
|
background: #4070f4;
|
||||||
box-shadow: 0
|
box-shadow: 0
|
||||||
5px
|
5px
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
//getting button from DOM id
|
//getting button from DOM id
|
||||||
const buttons = document.querySelectorAll(".button-container button");
|
const buttons = document.querySelectorAll(".button-container button");
|
||||||
|
const weeklybuttons = document.querySelectorAll(".weeklybutton-container button");
|
||||||
const queryButton = document.getElementById("querybutton-container");
|
const queryButton = document.getElementById("querybutton-container");
|
||||||
|
|
||||||
$(document).ready(async function () {
|
$(document).ready(async function () {
|
||||||
@ -21,7 +22,7 @@ $(document).ready(async function () {
|
|||||||
labels: [], // Array to store timestamps
|
labels: [], // Array to store timestamps
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
label: "Average MeasurementData",
|
label: "Average Measurement Data",
|
||||||
data: [], // Array to store measurements objects
|
data: [], // Array to store measurements objects
|
||||||
backgroundColor: "green",
|
backgroundColor: "green",
|
||||||
borderColor: "green",
|
borderColor: "green",
|
||||||
@ -87,7 +88,6 @@ $(document).ready(async function () {
|
|||||||
chart.update();
|
chart.update();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event listeners for buttons
|
// Event listeners for buttons
|
||||||
document.getElementById("psiButton").addEventListener("click", function () {
|
document.getElementById("psiButton").addEventListener("click", function () {
|
||||||
updateChart("psi");
|
updateChart("psi");
|
||||||
@ -116,4 +116,6 @@ $(document).ready(async function () {
|
|||||||
document.getElementById("coButton").addEventListener("click", function () {
|
document.getElementById("coButton").addEventListener("click", function () {
|
||||||
updateChart("co");
|
updateChart("co");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -19,7 +19,7 @@ router.use('/sensor', [auth, APIlogger], require('./sensor.js'));
|
|||||||
router.use('/sensor-data', [auth, APIlogger], require('./sensorData.js'));
|
router.use('/sensor-data', [auth, APIlogger], require('./sensorData.js'));
|
||||||
|
|
||||||
//apilog route
|
//apilog route
|
||||||
router.use('/apilog', [APIlogger], require('./apilog.js'));
|
router.use('/apilog', [auth, APIlogger], require('./apilog.js'));
|
||||||
|
|
||||||
//latest sensor data to display on dashboard
|
//latest sensor data to display on dashboard
|
||||||
router.use('/latest-sensor-data', [APIlogger], require('./latestsensorData.js'));
|
router.use('/latest-sensor-data', [APIlogger], require('./latestsensorData.js'));
|
||||||
|
@ -12,6 +12,7 @@ router.get("/", async (req, res, next) => {
|
|||||||
res.json(Res);
|
res.json(Res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
//get by route name?
|
//get by route name?
|
||||||
router.get("/route/:name", async (req, res, next) => {
|
router.get("/route/:name", async (req, res, next) => {
|
||||||
});
|
});
|
||||||
@ -27,6 +28,8 @@ router.get("/method/:method", async (req, res, next) => {
|
|||||||
//by ip
|
//by ip
|
||||||
router.get("/ip/:ip", async (req, res, next) => {
|
router.get("/ip/:ip", async (req, res, next) => {
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,8 +23,9 @@ router.get("/", async (req, res, next) => {
|
|||||||
|
|
||||||
router.post("/new", async (req, res, next) => {
|
router.post("/new", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { id_sensor, id_location, sensordata } = req.body;
|
//locationid
|
||||||
let data = await addSensorData(id_sensor, id_location, sensordata);
|
const { sensorid , locationid , measurement } = req.body;
|
||||||
|
let data = await addSensorData(sensorid , locationid , measurement);
|
||||||
res.json({ message: "SensorData " + data.id + " added", ...data });
|
res.json({ message: "SensorData " + data.id + " added", ...data });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@ -34,8 +35,8 @@ router.post("/new", async (req, res, next) => {
|
|||||||
|
|
||||||
router.put("/update", async (req, res, next) => {
|
router.put("/update", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { id, id_sensor, id_location, sensordata } = req.body;
|
const { id , sensorid , locationid , measurement } = req.body;
|
||||||
await updateSensorData(id, id_sensor, id_location, sensordata);
|
await updateSensorData(id, sensorid , locationid , measurement);
|
||||||
res.status(200).json({ message: "SensorData " + id + " updated" });
|
res.status(200).json({ message: "SensorData " + id + " updated" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@ -55,7 +56,6 @@ router.delete("/delete", async (req, res, next) => {
|
|||||||
});
|
});
|
||||||
router.get("/data", async (req, res, next) => {
|
router.get("/data", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
console.log(req.query);
|
|
||||||
const data = await getData(req.query);
|
const data = await getData(req.query);
|
||||||
res.status(200).json(data);
|
res.status(200).json(data);
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user