mp/IoT-sensor/modules/IoT-sensor.js
2024-01-25 03:26:56 +08:00

64 lines
1.5 KiB
JavaScript

const { getLocation, getSensor } = require("../functions/dbFunctions");
//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);
}
}
async generateData() {
try {
const { loc, sen } = await this.getLocationAndSensorId();
const dataAray = []; // create a new array for each call
for (let i = 0; i < sen.length; i++) {
dataAray.push(firstDataRow(sen[i].id, loc[i].id));
}
return dataAray;
} catch (err) {
console.error(err);
}
}
}
//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),
};
}
/*
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
*/
module.exports = { IoTdataGenerator };