diff --git a/backend/src/index.js b/backend/src/index.js index 1f7703b..0b654bf 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -22,8 +22,8 @@ const startAgenda = async () => { // Local // await agenda.every(CronTime.everyDayAt(23), "retrieveDailyAqi"); - await agenda.every(CronTime.everyDayAt(16, 5), "retrieveDailyAqi"); - await agenda.every(CronTime.everyHour(), "getHourlyAqi"); + await agenda.every(CronTime.everyDayAt(16, 55), "retrieveDailyAqi"); + // await agenda.every(CronTime.everyHour(), "getHourlyAqi"); }; mongoose diff --git a/backend/src/jobs/definitions/retrieveDailyAqi.js b/backend/src/jobs/definitions/retrieveDailyAqi.js index a9290eb..7d0ba70 100644 --- a/backend/src/jobs/definitions/retrieveDailyAqi.js +++ b/backend/src/jobs/definitions/retrieveDailyAqi.js @@ -1,7 +1,9 @@ const { aqiApiToken, predictionServiceUrl } = require("../../config/config"); const axios = require("axios"); const logger = require("../../config/logger"); -const HourlyAQIModel = require("../../models/HourlyAQI"); + +const AirQualityModel = require("../../models/AirQuality"); +const { mapServerTimeToVnTime } = require("../../helper/serverDate"); const retrieveDailyAqi = async (agenda) => { try { @@ -10,10 +12,63 @@ const retrieveDailyAqi = async (agenda) => { "retrieveDailyAqi", async function (job, done) { try { - const hourlyAqi = await HourlyAQIModel.find(); + const { offsetToday } = mapServerTimeToVnTime(); + const gmt7Offset = 7 * 60 * 60 * 1000; + var offsetNextDay = new Date(offsetToday.getTime() + 24 * 3600 * 1000) + .toISOString() + .replace(/T(.+)/g, "T00:00:00.000Z"); + + let hourlyAqi = await AirQualityModel.aggregate([ + { + $addFields: { + // Convert the createdAt field to GMT+7 by adding the offset + createdAtGMT7: { + $add: ["$createdAt", gmt7Offset], + }, + }, + }, + { + $match: { + createdAtGMT7: { + $gte: new Date( + offsetToday + .toISOString() + .replace(/T(.+)/g, "T00:00:00.000Z") + ), + $lt: new Date(offsetNextDay), + }, + }, + }, + { + $project: { + _id: 1, + aqi: 1, + humidity: 1, + temperature: 1, + co: 1, + co2: 1, + tvoc: 1, + o3: 1, + pm25: 1, + calc_aqi: 1, + createdAt: "$createdAtGMT7", + }, + }, + ]).exec(); + + console.log(hourlyAqi); + + hourlyAqi = hourlyAqi.map((x) => { + return { + aqi: x.calc_aqi ?? x.aqi, + dateTime: `${x.createdAt.toISOString()}`, + }; + }); + + console.log(hourlyAqi); const compositeAqiObject = hourlyAqi.reduce((prev, curr) => - prev > curr ? prev : curr + prev.aqi > curr.aqi ? prev : curr ); var requestBody = { @@ -29,8 +84,6 @@ const retrieveDailyAqi = async (agenda) => { data: requestBody, }); - await HourlyAQIModel.deleteMany(); - done(); } catch (error) { console.error(error); diff --git a/client/index.html b/client/index.html index 8e17729..d7e7a4d 100644 --- a/client/index.html +++ b/client/index.html @@ -457,7 +457,7 @@

Solutions:

-

Forecasting

+

Forecast

diff --git a/iot/arduino/arduino.ino b/iot/arduino/arduino.ino index fa664a6..b81eef4 100644 --- a/iot/arduino/arduino.ino +++ b/iot/arduino/arduino.ino @@ -65,7 +65,7 @@ void measureOpticalDustSensor() { Serial.print("PM2.5 voltage: "); Serial.println(voltage); - pm25 = 170 * voltage - 0.1; // unit: ug/m3 + pm25 = (170 * voltage - 0.1) / 2; // unit: ug/m3 Serial.print("PM2.5: "); Serial.print(pm25); diff --git a/prediction-service/app.py b/prediction-service/app.py index 8fa3210..4999f59 100644 --- a/prediction-service/app.py +++ b/prediction-service/app.py @@ -24,7 +24,7 @@ best_model = load_model(best_model_filename) best_scaler = joblib.load(best_scaler_filename) -MONGODB_URL="mongodb+srv://admin:Pa$$word!@aircheckercluster.yymawyw.mongodb.net/aircheckerdb?retryWrites=true&w=majority" +MONGODB_URL="mongodb://localhost:27017/aircheckerdb" # MONGODB_URL="mongodb://localhost:27017/aircheckerdb" # Connect to MongoDB