commit
2641b7ad24
@ -2,8 +2,17 @@ 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");
|
||||
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() {
|
||||
const location = await locationModel.findAll();
|
||||
return location;
|
||||
@ -15,7 +24,6 @@ async function addLocation(name, added_by, description) {
|
||||
added_by: added_by,
|
||||
description: description,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async function updateLocation(id, name, added_by, description) {
|
||||
@ -52,16 +60,18 @@ async function getLocationById(id) {
|
||||
}
|
||||
|
||||
async function getSensor() {
|
||||
try {
|
||||
const sensor = await sensorModel.findAll();
|
||||
return sensor;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function addSensor(sensorname, added_by, mac_address , description, location) {
|
||||
try {
|
||||
async function addSensor(
|
||||
sensorname,
|
||||
added_by,
|
||||
mac_address,
|
||||
description,
|
||||
location
|
||||
) {
|
||||
const sensor = await sensorModel.create({
|
||||
name: sensorname,
|
||||
added_by: added_by,
|
||||
@ -69,14 +79,16 @@ async function addSensor(sensorname, added_by, mac_address , description, locati
|
||||
description: description,
|
||||
location: location,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSensor(id, sensorname, added_by, mac_address ,description, location) {
|
||||
try {
|
||||
//update by id
|
||||
async function updateSensor(
|
||||
id,
|
||||
sensorname,
|
||||
added_by,
|
||||
mac_address,
|
||||
description,
|
||||
location
|
||||
) {
|
||||
const sensor = await sensorModel.update(
|
||||
{
|
||||
name: sensorname,
|
||||
@ -84,7 +96,6 @@ async function updateSensor(id, sensorname, added_by, mac_address ,description,
|
||||
mac_address: mac_address,
|
||||
description: description,
|
||||
location: location,
|
||||
|
||||
},
|
||||
{
|
||||
where: {
|
||||
@ -92,68 +103,43 @@ async function updateSensor(id, sensorname, added_by, mac_address ,description,
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function deleteSensor(id) {
|
||||
try {
|
||||
//delete by id
|
||||
const sensor = await sensorModel.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getSensorById(id) {
|
||||
try {
|
||||
const sensor = await sensorModel.findAll({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
return sensor;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getSensorData() {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.findAll();
|
||||
return sensorData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function addSensorData(id, id_sensor, id_location, sensordata) {
|
||||
try{
|
||||
console.log(typeof sensordata);
|
||||
console.log(sensordata);
|
||||
if (!sensordata){
|
||||
console.log("Sensor Data is null");
|
||||
}
|
||||
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) {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.update(
|
||||
{
|
||||
ensorid: id_sensor,
|
||||
@ -166,61 +152,131 @@ async function updateSensorData(id, id_sensor, id_location, sensordata) {
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function deleteSensorData(id) {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getSensorDataById(id) {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.findAll({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
return sensorData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getallData() {
|
||||
try {
|
||||
const allData = await IoTModel.findAll();
|
||||
return allData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
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: {
|
||||
sensorid: query.sensorid,
|
||||
},
|
||||
...ormQuery,
|
||||
};
|
||||
} else if (query.locationid !== undefined) {
|
||||
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 getLatestData() {
|
||||
try {
|
||||
const latestData = await IoTModel.findAll({
|
||||
limit: 1,
|
||||
order: [["createdAt", "DESC"]],
|
||||
});
|
||||
return latestData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
//daily data
|
||||
|
||||
//hourly data
|
||||
|
||||
//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 like psi or wtv
|
||||
|
||||
return await sensorDataModel.findAll(ormQuery);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getallData,
|
||||
getLatestData,
|
||||
getLocation,
|
||||
addLocation,
|
||||
updateLocation,
|
||||
@ -236,4 +292,5 @@ module.exports = {
|
||||
updateSensorData,
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
getData,
|
||||
};
|
||||
|
@ -6,6 +6,7 @@ const {
|
||||
updateSensorData,
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
getData,
|
||||
|
||||
} = 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) => {
|
||||
try {
|
||||
const sensor = await getSensorDataById(req.params.id);
|
||||
|
54
api.MD
54
api.MD
@ -53,14 +53,12 @@ curl localhost/api/v0/sensor/delete -X DELETE -H "Content-Type: application/json
|
||||
"message": "sensor 13 deleted"
|
||||
}
|
||||
|
||||
|
||||
//sensor data
|
||||
curl http://localhost/api/v0/sensor-data/
|
||||
|
||||
//sensor data by id
|
||||
curl http://localhost/api/v0/sensor-data/1
|
||||
|
||||
|
||||
//post
|
||||
curl localhost/api/v0/sensor-data/new -H "Content-Type: application/json" -X POST -d '{"id_sensor": "1" , "id_location": "11" , "sensordata": {
|
||||
"psi": "34",
|
||||
@ -113,3 +111,55 @@ curl localhost/api/seed/v0/seedSensorData/new -H "Content-Type: application/json
|
||||
"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
|
Loading…
x
Reference in New Issue
Block a user