From b978f0786791f7e9e8a0d2cd47e54473d2653181 Mon Sep 17 00:00:00 2001 From: newtbot Date: Fri, 5 Jan 2024 03:47:13 +0800 Subject: [PATCH] fgj --- Web-Server/functions/SeedCRUD.js | 0 Web-Server/modules/express.js | 3 +- Web-Server/routes/SeedsensorData.js | 123 +++++++++++++++++++++++++--- Web-Server/routes/seed_route.js | 3 +- api.MD | 23 +++++- 5 files changed, 136 insertions(+), 16 deletions(-) delete mode 100644 Web-Server/functions/SeedCRUD.js diff --git a/Web-Server/functions/SeedCRUD.js b/Web-Server/functions/SeedCRUD.js deleted file mode 100644 index e69de29..0000000 diff --git a/Web-Server/modules/express.js b/Web-Server/modules/express.js index bad741f..62d2f51 100644 --- a/Web-Server/modules/express.js +++ b/Web-Server/modules/express.js @@ -4,6 +4,7 @@ */ const express = require("express"); const helmet = require("helmet"); +const { APIlogger } = require('../middleware/apiLogger.js'); const app = express(); app.use(helmet()); @@ -15,9 +16,9 @@ app.disable("x-powered-by"); app.use(express.json()); app.set("json spaces", 2); -const { APIlogger } = require('../middleware/apiLogger.js'); //middleware logic ( called by next() ) + //app.use('/api/v0', require('../middleware/ApiKey.js')); app.use('/api/v0', APIlogger, require('../routes/api_route.js')); diff --git a/Web-Server/routes/SeedsensorData.js b/Web-Server/routes/SeedsensorData.js index 44563c0..1baf8c1 100644 --- a/Web-Server/routes/SeedsensorData.js +++ b/Web-Server/routes/SeedsensorData.js @@ -1,15 +1,112 @@ 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"); const express = require("express"); 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 router.post("/new", async (req, res, next) => { - try { + try { + const seedOptions = req.body; + console.log(seedOptions); + seedSensorData(seedOptions); } catch (error) { console.error(error); @@ -17,18 +114,20 @@ router.post("/new", async (req, res, next) => { } }); - - - module.exports = router; /* -1) get id of sensor and location -2) mock data -3) take post req for start date and end date -4) post to db +POST /api/v0/seed/sensordata +{ +"startDate" : "10-10-2010", // Date to start faking data REQUIRED +"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 -i would have more field TBH, that i would start with that +1) firstDataRow(startDate) +2) nextDataRow(lastRow, interval) +3) seedSensorData({post object from abovr}) -*/ \ No newline at end of file +*/ diff --git a/Web-Server/routes/seed_route.js b/Web-Server/routes/seed_route.js index 17c14be..e89f904 100644 --- a/Web-Server/routes/seed_route.js +++ b/Web-Server/routes/seed_route.js @@ -2,9 +2,10 @@ const router = require('express').Router(); //location route +router.use('/seedSensorData', require('./seedSensorData.js')); + router.use('/seed', require('./seedLocationAndSensor')); -router.use('/seedSensorData ', require('./seedsensorData')); diff --git a/api.MD b/api.MD index 23985f6..df7ff93 100644 --- a/api.MD +++ b/api.MD @@ -85,8 +85,6 @@ curl localhost/api/v0/sensor-data/update -H "Content-Type: application/json" -X "co": "16ppm", "temperature": "25C", "windspeed": "2km/h", - "time": "2023-12-21 14:24:44", - "region": "east" }}' //delete @@ -94,3 +92,24 @@ curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application //seed 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" + } +}'