fgj
This commit is contained in:
parent
1819956bd0
commit
b978f07867
@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
|
const { APIlogger } = require('../middleware/apiLogger.js');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
@ -15,9 +16,9 @@ app.disable("x-powered-by");
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.set("json spaces", 2);
|
app.set("json spaces", 2);
|
||||||
|
|
||||||
const { APIlogger } = require('../middleware/apiLogger.js');
|
|
||||||
|
|
||||||
//middleware logic ( called by next() )
|
//middleware logic ( called by next() )
|
||||||
|
|
||||||
//app.use('/api/v0', require('../middleware/ApiKey.js'));
|
//app.use('/api/v0', require('../middleware/ApiKey.js'));
|
||||||
app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
||||||
|
|
||||||
|
@ -1,15 +1,112 @@
|
|||||||
const { sequelize } = require("../../Database/mySql.js");
|
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 express = require("express");
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
let mockLocation = []
|
async function seedSensorData(seedOptions) {
|
||||||
|
//2024-01-01 18:48:04
|
||||||
|
seedOptions.endDate = seedOptions.endDate || Date.now();
|
||||||
|
seedOptions.interval = seedOptions.interval || 150000; //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.location_id;
|
||||||
|
|
||||||
|
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)
|
||||||
|
console.log(rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
//populate first row of sensordata model with random data from seedData
|
||||||
|
function firstDataRow(startDate , sensorId, locationID) {
|
||||||
|
console.log(startDate);
|
||||||
|
console.log(locationID);
|
||||||
|
return {
|
||||||
|
sensorid: sensorId,
|
||||||
|
locationid: locationID,
|
||||||
|
sensordata: {
|
||||||
|
//console.log(Math.floor(Math.random() * 30) + 5)
|
||||||
|
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: startDate,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function numberWithinTenPercent(inputNumber) {
|
||||||
|
// Calculate the range for 10% of the input number
|
||||||
|
const range = 0.1 * inputNumber;
|
||||||
|
|
||||||
|
// Generate a random number within the range (-10% to +10%)
|
||||||
|
const randomOffset = Math.random() * range * 2 - range;
|
||||||
|
|
||||||
|
// Calculate the new number within the +/- 10% range
|
||||||
|
const newNumber = inputNumber + randomOffset;
|
||||||
|
|
||||||
|
return newNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
function nextDataRow(currentRow, interval) {
|
||||||
|
return {
|
||||||
|
sensorid: currentRow.sensorid,
|
||||||
|
locationid: currentRow.locationid,
|
||||||
|
sensordata: {
|
||||||
|
psi: numberWithinTenPercent(currentRow.sensordata.psi),
|
||||||
|
humidity: numberWithinTenPercent(currentRow.sensordata.humidity),
|
||||||
|
o3: numberWithinTenPercent(currentRow.sensordata.o3),
|
||||||
|
no2: numberWithinTenPercent(currentRow.sensordata.no2),
|
||||||
|
so2: numberWithinTenPercent(currentRow.sensordata.so2),
|
||||||
|
co: numberWithinTenPercent(currentRow.sensordata.co),
|
||||||
|
temperature: numberWithinTenPercent(currentRow.sensordata.temperatue),
|
||||||
|
windspeed: numberWithinTenPercent(currentRow.sensordata.windspeed),
|
||||||
|
},
|
||||||
|
//convert 10-10-2020 to utc time
|
||||||
|
/*
|
||||||
|
const createdAtString = 'Sat Oct 10 2020 00:00:00 GMT+0800 (Singapore Standard Time)';
|
||||||
|
const createdAtDateObject = new Date(createdAtString);
|
||||||
|
|
||||||
|
// Add 150000 milliseconds
|
||||||
|
const updatedTimestamp = createdAtDateObject.getTime() + 150000;
|
||||||
|
|
||||||
|
// Create a new Date object with the updated timestamp
|
||||||
|
const updatedDateObject = new Date(updatedTimestamp);
|
||||||
|
|
||||||
|
console.log(updatedDateObject);
|
||||||
|
*/
|
||||||
|
createdAt: new Date (currentRow.createdAt).getTime() + interval,
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//add seed
|
//add seed
|
||||||
router.post("/new", async (req, res, next) => {
|
router.post("/new", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
|
const seedOptions = req.body;
|
||||||
|
console.log(seedOptions);
|
||||||
|
seedSensorData(seedOptions);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@ -17,18 +114,20 @@ router.post("/new", async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
1) get id of sensor and location
|
POST /api/v0/seed/sensordata
|
||||||
2) mock data
|
{
|
||||||
3) take post req for start date and end date
|
"startDate" : "10-10-2010", // Date to start faking data REQUIRED
|
||||||
4) post to db
|
"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
|
||||||
|
}
|
||||||
|
|
||||||
that takes startDate and an option endDate (the defaults to day) and an option interval to declare the polling interval
|
1) firstDataRow(startDate)
|
||||||
i would have more field TBH, that i would start with that
|
2) nextDataRow(lastRow, interval)
|
||||||
|
3) seedSensorData({post object from abovr})
|
||||||
|
|
||||||
*/
|
*/
|
@ -2,9 +2,10 @@
|
|||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
|
|
||||||
//location route
|
//location route
|
||||||
|
router.use('/seedSensorData', require('./seedSensorData.js'));
|
||||||
|
|
||||||
router.use('/seed', require('./seedLocationAndSensor'));
|
router.use('/seed', require('./seedLocationAndSensor'));
|
||||||
|
|
||||||
router.use('/seedSensorData ', require('./seedsensorData'));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
23
api.MD
23
api.MD
@ -85,8 +85,6 @@ curl localhost/api/v0/sensor-data/update -H "Content-Type: application/json" -X
|
|||||||
"co": "16ppm",
|
"co": "16ppm",
|
||||||
"temperature": "25C",
|
"temperature": "25C",
|
||||||
"windspeed": "2km/h",
|
"windspeed": "2km/h",
|
||||||
"time": "2023-12-21 14:24:44",
|
|
||||||
"region": "east"
|
|
||||||
}}'
|
}}'
|
||||||
|
|
||||||
//delete
|
//delete
|
||||||
@ -94,3 +92,24 @@ curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application
|
|||||||
|
|
||||||
//seed
|
//seed
|
||||||
curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["test", "test123"]}'
|
curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["test", "test123"]}'
|
||||||
|
|
||||||
|
//router.use('/seedSensorData ', require('./seedSensorData.js'));
|
||||||
|
|
||||||
|
//seed
|
||||||
|
curl localhost/api/seed/v0/seedSensorData/new -H "Content-Type: application/json" -X POST -d '{
|
||||||
|
"startDate": "10-10-2020",
|
||||||
|
"endDate": "10-12-2020",
|
||||||
|
"interval": "150000",
|
||||||
|
"sensorid": ["1", "4"],
|
||||||
|
"locationid": ["11", "12"],
|
||||||
|
"seedData": {
|
||||||
|
"psi": "30",
|
||||||
|
"humidity": "15%",
|
||||||
|
"o3": "50ppm",
|
||||||
|
"no2": "45ppm",
|
||||||
|
"so2": "30ppm",
|
||||||
|
"co": "16ppm",
|
||||||
|
"temperature": "30C",
|
||||||
|
"windspeed": "4km/h"
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user