iot sensor finished

1)with validation on front and backend
2)fixed seed route generating value 0 for data
This commit is contained in:
newtbot
2024-01-14 02:43:27 +08:00
parent a7e1a0028e
commit c00a57d5f6
12 changed files with 417 additions and 148 deletions

View File

@ -1,57 +1,71 @@
/*
1) PSI metric data
2) Humidity
3) Gases (O3,NO2,SO2)
4) temperature
5) Air pressure?
6) windspeed?
8) time when data was collected / generated
*/
const { getLocation, getSensor } = require("../functions/dbFunctions");
/*
1) generate random data for each sensor
2) pass to mqtt broker
*/
//class to generate random data
var dataAray = [];
class IoTdataGenerator {
constructor() {
}
async getLocationAndSensorId() {
try {
const loc = await getLocation();
const sen = await getSensor();
return { loc, sen };
} catch (err) {
console.error(err);
}
}
let region = ["central", "north-east", "north", "east", "west"];
async generateData() {
try {
const { loc, sen } = await this.getLocationAndSensorId();
for (let i = 0; i < sen.length; i++) {
//console.log(sen[i].id);
//console.log(loc[i].id);
//console.log("you should appear 6 times only")
dataAray.push(firstDataRow(sen[i].id, loc[i].id));
}
} catch (err) {
console.error(err);
}
return dataAray;
function generateRandomData() {
const psiData = getRandomValue(0, 500);
const humidityData = getRandomValue(0, 100);
const o3Data = getRandomValue(0, 600); //max 600
const no2Data = getRandomValue(0, 1000); //max 1000
const so2Data = getRandomValue(0, 1000); //max 1000
const coData = getRandomValue(0 , 100);
const temperatureData = getRandomValue(24, 40);
const windspeedData = getRandomValue(0, 35);
const currentTime = new Date(Date.now() + 28800000)
.toISOString()
.slice(0, 19)
.replace("T", " ");
const regionData = region[Math.floor(Math.random() * region.length)];
}
}
var json = {
psi: psiData.toFixed(0),
humidity: humidityData.toFixed(0) + "%",
o3: o3Data.toFixed(0) + "ppm",
no2: no2Data.toFixed(0) + "ppm",
so2: so2Data.toFixed(0) + "ppm",
co: coData.toFixed(0) + "ppm",
temperature: temperatureData.toFixed(0) + "°C",
windspeed: windspeedData.toFixed(0) + "km/h",
time: currentTime,
region: regionData,
//helper function to generate random data
function firstDataRow(sensorId, locationId) {
return {
sensorid: sensorId,
locationid: locationId,
measurement: {
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),
},
//time stamp are auto generated by sequelize
//createdAt: convertDateToUTC(startDate),
};
return json;
}
function getRandomValue(min, max) {
return Math.random() * (max - min) + min;
/*
1) get location and sensor id from db
2) loop through each sensor id and location id and generate random data and pass to mqtt
*/
async function run() {
let iotData = new IoTdataGenerator();
const result = await iotData.generateData();
console.log(result);
return result;
}
module.exports = { run };
function iot_sensor_data() {
return generateRandomData();
}
module.exports = { iot_sensor_data };