more routes!

This commit is contained in:
newtbot 2024-01-07 04:15:22 +08:00
parent 5ff05a7a9e
commit 64e022e2ea
3 changed files with 357 additions and 213 deletions

View File

@ -2,225 +2,281 @@ const { sequelize } = require("../../Database/mySql.js");
const { locationModel } = require("../../Database/model/locationModel.js"); const { locationModel } = require("../../Database/model/locationModel.js");
const { sensorModel } = require("../../Database/model/sensorModel.js"); const { sensorModel } = require("../../Database/model/sensorModel.js");
const { sensorDataModel } = require("../../Database/model/sensorDataModel.js"); const { sensorDataModel } = require("../../Database/model/sensorDataModel.js");
var moment = require("moment");
//helper function to convert month name to month number
//https://stackoverflow.com/questions/13566552/easiest-way-to-convert-month-name-to-month-number-in-js-jan-01
function getMonthFromString(mon) {
var d = Date.parse(mon + "1, 2012");
if (!isNaN(d)) {
return new Date(d).getMonth() + 1;
}
return -1;
}
async function getLocation() { async function getLocation() {
const location = await locationModel.findAll(); const location = await locationModel.findAll();
return location; return location;
} }
async function addLocation(name, added_by, description) { async function addLocation(name, added_by, description) {
const location = await locationModel.create({ const location = await locationModel.create({
name: name, name: name,
added_by: added_by, added_by: added_by,
description: description, description: description,
}); });
} }
async function updateLocation(id, name, added_by, description) { async function updateLocation(id, name, added_by, description) {
const location = await locationModel.update( const location = await locationModel.update(
{ {
name: name, name: name,
added_by: added_by, added_by: added_by,
description: description, description: description,
},
{
where: {
id: id,
}, },
{ }
where: { );
id: id,
},
}
);
} }
async function deleteLocation(id) { async function deleteLocation(id) {
//delete by id //delete by id
const location = await locationModel.destroy({ const location = await locationModel.destroy({
where: { where: {
id: id, id: id,
}, },
}); });
} }
async function getLocationById(id) { async function getLocationById(id) {
const location = await locationModel.findAll({ const location = await locationModel.findAll({
where: { where: {
id: id, id: id,
}, },
}); });
return location; return location;
} }
async function getSensor() { async function getSensor() {
try { const sensor = await sensorModel.findAll();
const sensor = await sensorModel.findAll(); return sensor;
return sensor; console.error(error);
} catch (error) {
console.error(error);
}
} }
async function addSensor(sensorname, added_by, mac_address , description, location) { async function addSensor(
try { sensorname,
const sensor = await sensorModel.create({ added_by,
mac_address,
description,
location
) {
const sensor = await sensorModel.create({
name: sensorname,
added_by: added_by,
mac_address: mac_address,
description: description,
location: location,
});
}
async function updateSensor(
id,
sensorname,
added_by,
mac_address,
description,
location
) {
const sensor = await sensorModel.update(
{
name: sensorname, name: sensorname,
added_by: added_by, added_by: added_by,
mac_address: mac_address, mac_address: mac_address,
description: description, description: description,
location: location, location: location,
}); },
} catch (error) { {
console.error(error); where: {
} id: id,
}
async function updateSensor(id, sensorname, added_by, mac_address ,description, location) {
try {
//update by id
const sensor = await sensorModel.update(
{
name: sensorname,
added_by: added_by,
mac_address: mac_address,
description: description,
location: location,
}, },
{ }
where: { );
id: id,
},
}
);
} catch (error) {
console.error(error);
}
} }
async function deleteSensor(id) { async function deleteSensor(id) {
try { //delete by id
//delete by id const sensor = await sensorModel.destroy({
const sensor = await sensorModel.destroy({ where: {
where: { id: id,
id: id, },
}, });
});
} catch (error) { console.error(error);
console.error(error);
}
} }
async function getSensorById(id) { async function getSensorById(id) {
try { const sensor = await sensorModel.findAll({
const sensor = await sensorModel.findAll({ where: {
where: { id: id,
id: id, },
}, });
}); return sensor;
return sensor;
} catch (error) {
console.error(error);
}
} }
async function getSensorData() { async function getSensorData() {
try { const sensorData = await sensorDataModel.findAll();
const sensorData = await sensorDataModel.findAll(); return sensorData;
return sensorData;
} catch (error) {
console.error(error);
}
} }
async function addSensorData(id , id_sensor , id_location , sensordata){ async function addSensorData(id, id_sensor, id_location, sensordata) {
try{ const sensorData = await sensorDataModel.create({
console.log(typeof sensordata); id: id,
console.log(sensordata); sensorid: id_sensor,
if (!sensordata){ locationid: id_location,
console.log("Sensor Data is null"); measurement: sensordata,
} });
const sensorData = await sensorDataModel.create({
id: id,
sensorid: id_sensor,
locationid: id_location,
measurement: sensordata,
});
}catch(error){
console.error(error);
}
} }
async function updateSensorData(id, id_sensor, id_location, sensordata) { async function updateSensorData(id, id_sensor, id_location, sensordata) {
try { const sensorData = await sensorDataModel.update(
const sensorData = await sensorDataModel.update( {
{ ensorid: id_sensor,
ensorid: id_sensor, locationid: id_location,
locationid: id_location, measurement: sensordata,
measurement: sensordata, },
{
where: {
id: id,
}, },
{ }
where: { );
id: id,
},
}
);
} catch (error) {
console.error(error);
}
} }
async function deleteSensorData(id) { async function deleteSensorData(id) {
try { const sensorData = await sensorDataModel.destroy({
const sensorData = await sensorDataModel.destroy({ where: {
where: { id: id,
id: id, },
}, });
});
} catch (error) {
console.error(error);
}
} }
async function getSensorDataById(id) { async function getSensorDataById(id) {
try { const sensorData = await sensorDataModel.findAll({
const sensorData = await sensorDataModel.findAll({ where: {
id: id,
},
});
return sensorData;
}
async function getData(query) {
let ormQuery = {};
if (query.limit !== undefined && query.order !== undefined)
ormQuery = {
limit: parseInt(query.limit),
//console.log(sentence.toUpperCase());
order: [["createdAt", query.order.toUpperCase()]],
...ormQuery,
};
else if (query.sensorid !== undefined) {
ormQuery = {
where: { where: {
id: id, sensorid: query.sensorid,
}, },
}); ...ormQuery,
return sensorData; };
} catch (error) { } else if (query.locationid !== undefined) {
console.error(error); ormQuery = {
where: {
locationid: query.locationid,
},
...ormQuery,
};
} else if (query.year !== undefined) {
ormQuery = {
where: sequelize.where(
sequelize.fn("YEAR", sequelize.col("createdAt")),
query.year
),
...ormQuery,
};
} else if (query.month !== undefined) {
console.log(typeof query.month);
const validMonths = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
];
if (validMonths.includes(query.month)) {
ormQuery = {
where: sequelize.where(
sequelize.fn("MONTH", sequelize.col("createdAt")),
query.month
),
...ormQuery,
};
} else {
query.month = getMonthFromString(query.month);
ormQuery = {
where: sequelize.where(
sequelize.fn("MONTH", sequelize.col("createdAt")),
query.month
),
...ormQuery,
};
}
//weekly
} else if (query.week !== undefined) {
ormQuery = {
where: sequelize.where(
sequelize.fn("WEEK", sequelize.col("createdAt")),
query.week
),
...ormQuery,
};
} }
}
async function getallData() { //daily data
try {
const allData = await IoTModel.findAll();
return allData;
} catch (error) {
console.error(error);
}
}
async function getLatestData() { //hourly data
try {
const latestData = await IoTModel.findAll({ //get data by year by specific sensor
limit: 1,
order: [["createdAt", "DESC"]], //get by month by specific sensor
});
return latestData; //get by week by specific sensor
} catch (error) {
console.error(error); //get by daily by specific sensor
}
//get by hourly by specific sensor
//get data by year by specific location
//get by month by specific location
//get by week by specific location
//get by daily by specific location
//get by hourly by specific location
//get specific data like psi or wtv
return await sensorDataModel.findAll(ormQuery);
} }
module.exports = { module.exports = {
getallData,
getLatestData,
getLocation, getLocation,
addLocation, addLocation,
updateLocation, updateLocation,
@ -236,4 +292,5 @@ module.exports = {
updateSensorData, updateSensorData,
deleteSensorData, deleteSensorData,
getSensorDataById, getSensorDataById,
getData,
}; };

View File

@ -6,6 +6,7 @@ const {
updateSensorData, updateSensorData,
deleteSensorData, deleteSensorData,
getSensorDataById, getSensorDataById,
getData,
} = require("../functions/apiDatabase.js"); } = require("../functions/apiDatabase.js");
@ -56,6 +57,42 @@ router.delete("/delete", async (req, res, next) => {
} }
}); });
router.get("/data", async (req, res, next) => {
try {
let query = {
//can be desc or asc
order: req.query.order,
// number of data to be returned
limit: req.query.limit,
//can be sensorid, locationid
sensorid: req.query.sensorid,
//can be sensorid, locationid
locationid: req.query.locationid,
//yearly
year: req.query.year,
//monthly
month: req.query.month,
//weekly
week: req.query.week,
};
const data = await getData(query);
res.status(200).json(data);
} catch (error) {
console.error(error);
next(error);
}
});
router.get("/:id", async (req, res, next) => { router.get("/:id", async (req, res, next) => {
try { try {
const sensor = await getSensorDataById(req.params.id); const sensor = await getSensorDataById(req.params.id);

148
api.MD
View File

@ -8,21 +8,21 @@ curl http://localhost/api/v0/location/3
//post //post
curl localhost/api/v0/location/new -H "Content-Type: application/json" -X POST -d '{"name": "SAMPLE", "added_by": "system" , "description": "test"}' curl localhost/api/v0/location/new -H "Content-Type: application/json" -X POST -d '{"name": "SAMPLE", "added_by": "system" , "description": "test"}'
status: 200 status: 200
added_name allowed: system , admin added_name allowed: system , admin
name allowed: shld contain alphanumeric only name allowed: shld contain alphanumeric only
//put //put
curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}' curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}'
status: 200 status: 200
"message": "Location 13 updated" "message": "Location 13 updated"
//delete //delete
curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }' curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }'
status: 200 status: 200
{ {
"message": "Location 13 deleted" "message": "Location 13 deleted"
} }
//sensor //sensor
@ -35,14 +35,14 @@ curl http://localhost/api/v0/sensor/3
//POST //POST
curl localhost/api/v0/sensor/new -H "Content-Type: application/json" -X POST -d '{"sensorname": "test", "added_by": "system" , "mac_address": "99-6A-F8-7D-B4-94", "description": "test" , "location": "11"}' curl localhost/api/v0/sensor/new -H "Content-Type: application/json" -X POST -d '{"sensorname": "test", "added_by": "system" , "mac_address": "99-6A-F8-7D-B4-94", "description": "test" , "location": "11"}'
status: 200 status: 200
added_name allowed: system , admin added_name allowed: system , admin
sensor name : shld contain alphanumeric only and be unique sensor name : shld contain alphanumeric only and be unique
//put //put
curl localhost/api/v0/sensor/update -H "Content-Type: application/json" -X PUT -d '{"id": "2" ,"sensorname": "test", "added_by": "system" , "mac_address": "99-6A-F8-7D-B4-94" , "description": "test123" , "location": "11" }' curl localhost/api/v0/sensor/update -H "Content-Type: application/json" -X PUT -d '{"id": "2" ,"sensorname": "test", "added_by": "system" , "mac_address": "99-6A-F8-7D-B4-94" , "description": "test123" , "location": "11" }'
status: 200 status: 200
added_name allowed: system , admin added_name allowed: system , admin
sensor name : shld contain alphanumeric only and be unique sensor name : shld contain alphanumeric only and be unique
"message": "Sensor 13 updated" "message": "Sensor 13 updated"
@ -50,66 +50,116 @@ sensor name : shld contain alphanumeric only and be unique
//delete //delete
curl localhost/api/v0/sensor/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "2" }' curl localhost/api/v0/sensor/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "2" }'
{ {
"message": "sensor 13 deleted" "message": "sensor 13 deleted"
} }
//sensor data //sensor data
curl http://localhost/api/v0/sensor-data/ curl http://localhost/api/v0/sensor-data/
//sensor data by id //sensor data by id
curl http://localhost/api/v0/sensor-data/1 curl http://localhost/api/v0/sensor-data/1
//post
//post curl localhost/api/v0/sensor-data/new -H "Content-Type: application/json" -X POST -d '{"id_sensor": "1" , "id_location": "11" , "sensordata": {
curl localhost/api/v0/sensor-data/new -H "Content-Type: application/json" -X POST -d '{"id_sensor": "1" , "id_location": "11" , "sensordata": { "psi": "34",
"psi": "34", "humidity": "11%",
"humidity": "11%", "o3": "326ppm",
"o3": "326ppm", "no2": "445ppm",
"no2": "445ppm", "so2": "511ppm",
"so2": "511ppm", "co": "16ppm",
"co": "16ppm", "temperature": "25C",
"temperature": "25C", "windspeed": "2km/h",
"windspeed": "2km/h", "time": "2023-12-21 14:24:44",
"time": "2023-12-21 14:24:44", "region": "east"
"region": "east"
}}' }}'
//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": "326ppm",
"no2": "445ppm", "no2": "445ppm",
"so2": "511ppm", "so2": "511ppm",
"co": "16ppm", "co": "16ppm",
"temperature": "25C", "temperature": "25C",
"windspeed": "2km/h", "windspeed": "2km/h",
}}' }}'
//delete //delete
curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "3" }' curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "3" }'
//seed //seed
curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["Ang Mo Kio", "Bishan" , "Tampines" , "Jurong" , "Marine Parade" , "Woodlands "]}' curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["Ang Mo Kio", "Bishan" , "Tampines" , "Jurong" , "Marine Parade" , "Woodlands "]}'
//router.use('/seedSensorData ', require('./seedSensorData.js')); //router.use('/seedSensorData ', require('./seedSensorData.js'));
//seed //seed
curl localhost/api/seed/v0/seedSensorData/new -H "Content-Type: application/json" -X POST -d '{ curl localhost/api/seed/v0/seedSensorData/new -H "Content-Type: application/json" -X POST -d '{
"startDate": "2023-01-01T16:00:00.000Z", "startDate": "2023-01-01T16:00:00.000Z",
"endDate": "2023-02-01T16:00:00.000Z", "endDate": "2023-02-01T16:00:00.000Z",
"interval": "15", "interval": "15",
"sensorid": ["1", "2" , "3" , "4" , "5" , "6"], "sensorid": ["1", "2" , "3" , "4" , "5" , "6"],
"locationid": ["1", "2" , "3" , "4" , "5" , "6"], "locationid": ["1", "2" , "3" , "4" , "5" , "6"],
"seedData": { "seedData": {
"psi": "30", "psi": "30",
"humidity": "15%", "humidity": "15%",
"o3": "50ppm", "o3": "50ppm",
"no2": "45ppm", "no2": "45ppm",
"so2": "30ppm", "so2": "30ppm",
"co": "16ppm", "co": "16ppm",
"temperature": "30C", "temperature": "30C",
"windspeed": "4km/h" "windspeed": "4km/h"
} }
}' }'
//latest or last data
curl http://localhost/api/v0/sensor-data/data?order=DESC&limit=2
order= ASC OR DESC
limit = 1 or whatever
//sort by sensor id
curl http://localhost/api/v0/sensor-data/data?sensorid=1
//sort by location id
curl http://localhost/api/v0/sensor-data/data?locationid=1
//get data by year
curl http://localhost/api/v0/sensor-data/data?year=2023
year = 2023 or wtv
//get by month
curl http://localhost/api/v0/sensor-data/data?month=1
month = 1 or jan
//get by week
curl http://localhost/api/v0/sensor-data/data?week=1
//get by daily
//get by hourly
//get data by year by specific sensor
//get by month by specific sensor
//get by week by specific sensor
//get by daily by specific sensor
//get by hourly by specific sensor
//get data by year by specific location
//get by month by specific location
//get by week by specific location
//get by daily by specific location
//get by hourly by specific location
//get specific data