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