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();
			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;

	}
}

//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
*/

async function run() {
    let iotData = new IoTdataGenerator();
    const result = await iotData.generateData();
	console.log(result);
	return result;
}
  

module.exports = { run };



