a
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { locationModel } = require("../../Database/model/locationModel.js");
|
||||
const { sensorModel } = require("../../Database/model/sensorModel.js");
|
||||
const { sensorDataModel } = require("../../Database/model/sensorDataModel.js");
|
||||
@ -64,7 +63,7 @@ async function getSensor() {
|
||||
async function addSensor(sensorname, added_by, mac_address , description, location) {
|
||||
try {
|
||||
const sensor = await sensorModel.create({
|
||||
sensorname: sensorname,
|
||||
name: sensorname,
|
||||
added_by: added_by,
|
||||
mac_address: mac_address,
|
||||
description: description,
|
||||
@ -80,7 +79,7 @@ async function updateSensor(id, sensorname, added_by, mac_address ,description,
|
||||
//update by id
|
||||
const sensor = await sensorModel.update(
|
||||
{
|
||||
sensorname: sensorname,
|
||||
name: sensorname,
|
||||
added_by: added_by,
|
||||
mac_address: mac_address,
|
||||
description: description,
|
||||
@ -143,9 +142,9 @@ async function addSensorData(id , id_sensor , id_location , sensordata){
|
||||
}
|
||||
const sensorData = await sensorDataModel.create({
|
||||
id: id,
|
||||
id_sensor: id_sensor,
|
||||
id_location: id_location,
|
||||
sensordata: sensordata,
|
||||
sensorid: id_sensor,
|
||||
locationid: id_location,
|
||||
measurement: sensordata,
|
||||
});
|
||||
}catch(error){
|
||||
console.error(error);
|
||||
@ -157,9 +156,9 @@ async function updateSensorData(id, id_sensor, id_location, sensordata) {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.update(
|
||||
{
|
||||
id_sensor: id_sensor,
|
||||
id_location: id_location,
|
||||
sensordata: sensordata,
|
||||
ensorid: id_sensor,
|
||||
locationid: id_location,
|
||||
measurement: sensordata,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
|
@ -1,27 +1,5 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { api_log_Model } = require("../../Database/model/apiLog.js");
|
||||
|
||||
function insertData(data) {
|
||||
try {
|
||||
//const latestData = await IoTModel.create({
|
||||
IoTModel.create({
|
||||
psiData: data.psi,
|
||||
humidityData: data.humidity,
|
||||
o3Data: data.o3,
|
||||
no2Data: data.no2,
|
||||
so2Data: data.so2,
|
||||
coData: data.co,
|
||||
temperatureData: data.temperature,
|
||||
windspeedData: data.windspeed,
|
||||
currentTime: data.time,
|
||||
regionData: data.region,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
const { api_log_Model } = require("../../Database/model/apiLogModel.js");
|
||||
|
||||
async function insertLogData(log){
|
||||
try{
|
||||
@ -43,7 +21,7 @@ async function insertLogData(log){
|
||||
|
||||
}
|
||||
|
||||
module.exports = { insertData , insertLogData };
|
||||
module.exports = { insertLogData };
|
||||
|
||||
|
||||
|
||||
|
@ -20,11 +20,11 @@ router.post("/new", async (req, res, next) => {
|
||||
description: "system generated location",
|
||||
});
|
||||
await sensorModel.create({
|
||||
sensorname: `AQI-${Math.floor(Math.random()*898)+101}`,
|
||||
name: `AQI-${Math.floor(Math.random()*898)+101}`,
|
||||
added_by: "system",
|
||||
mac_address: `${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}`,
|
||||
description: "system generated sensor",
|
||||
location: location.id
|
||||
locationid: location.id
|
||||
|
||||
});
|
||||
}
|
||||
|
@ -5,40 +5,45 @@ const { sensorDataModel } = require("../../Database/model/sensorDataModel.js");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
var moment = require("moment");
|
||||
|
||||
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 = []
|
||||
seedOptions.endDate = new Date(seedOptions.endDate) || Date.now();
|
||||
seedOptions.interval = seedOptions.interval || 15; //15 minutes
|
||||
seedOptions.sensorid = seedOptions.sensorid || (await sensorModel.findAll()).map((i) => i.id);
|
||||
seedOptions.seedData = seedOptions.seedData || {};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
let rows = [];
|
||||
|
||||
//await sensorDataModel.bulkCreate(rows)
|
||||
console.log(rows);
|
||||
for (let sensorId of seedOptions.sensorid) {
|
||||
let sensor = await sensorModel.findByPk(sensorId);
|
||||
let locationId = sensor.locationid;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function convertDateToUTC(startDate) {
|
||||
let date = new Date(startDate);
|
||||
date = moment(date).utc().toDate();
|
||||
//return as object
|
||||
return date;
|
||||
}
|
||||
|
||||
//populate first row of sensordata model with random data from seedData
|
||||
function firstDataRow(startDate , sensorId, locationID) {
|
||||
console.log(startDate);
|
||||
console.log(locationID);
|
||||
function firstDataRow(startDate, sensorId, locationId) {
|
||||
return {
|
||||
sensorid: sensorId,
|
||||
locationid: locationID,
|
||||
sensordata: {
|
||||
locationid: locationId,
|
||||
measurement: {
|
||||
//console.log(Math.floor(Math.random() * 30) + 5)
|
||||
psi: Math.floor(Math.random() * 30) + 5,
|
||||
humidity: Math.floor(Math.random() * (90 - 80 + 1) + 80),
|
||||
@ -49,57 +54,37 @@ function firstDataRow(startDate , sensorId, locationID) {
|
||||
temperature: Math.floor(Math.random() * (30 - 23 + 1) + 25),
|
||||
windspeed: Math.floor(Math.random() * (10 - 1 + 1) + 1),
|
||||
},
|
||||
createdAt: startDate,
|
||||
createdAt: convertDateToUTC(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,
|
||||
};
|
||||
|
||||
return {
|
||||
sensorid: currentRow.sensorid,
|
||||
locationid: currentRow.locationid,
|
||||
measurement: {
|
||||
psi: numberWithinPercent(currentRow.measurement.psi),
|
||||
humidity: Math.floor(Math.random() * (90 - 80 + 1) + 80),
|
||||
o3: Math.floor(Math.random() * (100 - 20 + 1) + 30),
|
||||
no2: numberWithinPercent(currentRow.measurement.no2),
|
||||
so2: numberWithinPercent(currentRow.measurement.so2),
|
||||
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: moment(currentRow.createdAt).add(interval, "m").toDate(),
|
||||
};
|
||||
}
|
||||
|
||||
function numberWithinPercent(inputNumber) {
|
||||
const range = inputNumber * 0.003;
|
||||
|
||||
const randomOffset = Math.random() * range;
|
||||
|
||||
const newNumber = inputNumber + randomOffset;
|
||||
|
||||
return Math.floor(newNumber);
|
||||
}
|
||||
|
||||
//add seed
|
||||
router.post("/new", async (req, res, next) => {
|
||||
@ -107,7 +92,7 @@ router.post("/new", async (req, res, next) => {
|
||||
const seedOptions = req.body;
|
||||
console.log(seedOptions);
|
||||
seedSensorData(seedOptions);
|
||||
|
||||
res.status(200)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
|
@ -26,7 +26,7 @@ router.use('/sensor', require('./sensor'))
|
||||
//sensor data route
|
||||
router.use('/sensor-data', require('./sensorData'));
|
||||
|
||||
router.use('/latest-data', require('./latest-data'));
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,23 +0,0 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { getLatestData } = require("../functions/apiDatabase.js");
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const data = await getLatestData();
|
||||
|
||||
if (data === null) {
|
||||
res.status(404).send("No data found");
|
||||
} else {
|
||||
res.status(200).send(data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send('Internal Server Error');
|
||||
}
|
||||
});
|
||||
|
||||
// Export the router
|
||||
module.exports = router;
|
Reference in New Issue
Block a user