-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIFunctions.js
More file actions
138 lines (125 loc) · 4.79 KB
/
APIFunctions.js
File metadata and controls
138 lines (125 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const config = require("./config");
const requestify = require("requestify");
const mongoHelpers = require('./mongoFunctions.js');
const { MongoClient } = require('mongodb');
const { ObjectID } = require('bson');
const accountSid = config.cred.accountSid;
const authToken = config.cred.authToken;
const client = require("twilio")(accountSid, authToken);
function isAuthorized(req, res, next) {
const auth = req.headers.authorization;
if (auth === config.cred.secretpassword) {
next();
} else {
res.status(401);
res.send('Not permitted');
}
}
function uploadNewUser(body) {
var user = {
"_id": new ObjectID(),
"name": body.name,
"risk": body.risk,
"lat": body.lat,
"lon": body.lon,
"phone": body.phone,
"email": body.email
};
mongoHelpers.mongoUpload("main", user);
}
async function getAQIData(body) {
let regularResponse = await getRegularResponse(body);
let forecastResponse = await getForecastResponse(body);
return { regularResponse, forecastResponse }
}
async function getRegularResponse(body) {
let url =
"https://api.breezometer.com/air-quality/v2/current-conditions?lang=en&key=496bfcfb6ddd40ef831e29858c8ba7a9&metadata=extended_aqi&features=breezometer_aqi,local_aqi,health_recommendations,sources_and_effects,dominant_pollutant_concentrations,pollutants_concentrations,all_pollutants_concentrations,pollutants_aqi_information&lat=" +
body.lat +
"&lon=" +
body.lon +
"&all_aqi=true";
return await requestify.get(url).then(async function (response) {
let res = await response.getBody();
return res.data;
});
}
async function getForecastResponse(body) {
let url =
"https://api.breezometer.com/air-quality/v2/forecast/hourly?lang=en&key=496bfcfb6ddd40ef831e29858c8ba7a9&features=breezometer_aqi,local_aqi&hours=12&lat=" +
body.lat +
"&lon=" +
body.lon;
try {
return await requestify.get(url).then(async function (response) {
let res = await response.getBody();
return res.data;
});
}
catch {
console.log("Bad Location", body.lat, body.lon, url)
return "Bad Location"
}
}
//driver function for notifying users regarding data. Called each morning
async function notifyUsers() {
const users = await getUsers();
console.log(users, users.length)
for (var i = 0; i < users.length; i++) {
let user = users[i];
let userLocationInfo = await getForecastResponse(user);
if (userLocationInfo != "Bad Location") {
let parsedUserDangerInfo = parseUserDangerInfo(userLocationInfo, user.risk); //reformat data to usable state
textUser(parsedUserDangerInfo, user.phone);
}
}
console.log("done")
}
//return json array of objects, each element representing a user in the format uploaded
async function getUsers() {
return await mongoHelpers.mongoRead("main", "data");
}
//parse breezeometer response for relevant data
function parseUserDangerInfo(userLocationInfo, risk) {
let prediction = new Array();
let max = 0;
let avg = 0;
for (let i = 0; i < userLocationInfo.length; i++) {
prediction.push(userLocationInfo[i].indexes.usa_epa_nowcast.aqi)
avg += prediction[i];
if (prediction[i] > max)
max = prediction[i];
}
avg = avg / userLocationInfo.length;
let s = "The AIR quality index is currently " + prediction[0] + ", rising to " + max + " at its max and finishing the day at " + prediction[prediction.length - 1] + ". ";
let strEnd = "\n Given your risk demographics, there is nothing to worry about. Go enjoy the outdoors!";
if (avg > 50 && avg <= 100 && risk > 2)
strEnd = "\n Given your risk demographics, you might experience discomfort as a result of the air quality today. Try limiting outdoor activity if you start to feel affected.";
else if (avg > 100 && avg <= 150 && risk == 1)
strEnd = "\n Given your risk demographics, strenuous outdoor activity may cause health effects. If you start to feel sick, limit outdoor activity."
else if (avg > 100 && avg <= 150 && risk > 1)
strEnd = "\n Given your risk demographics, outdoor activity could cause negative health effects. Try to limit outdoor activity."
else if (avg > 150)
strEnd = "\nWARNING: Very poor air quality. Stay inside as much as possible to limit negative health effects."
return s + strEnd;
}
function textUser(content, phone) {
console.log("texting: ", phone, content);
client.messages
.create({
body: content,
from: "+12184132230",
to: phone,
})
.then((message) => console.log(message));
return;
}
exports.isAuthorized = isAuthorized
exports.uploadNewUser = uploadNewUser
exports.getAQIData = getAQIData
exports.getRegularResponse = getRegularResponse
exports.getForecastResponse = getForecastResponse
exports.notifyUsers = notifyUsers
exports.getUsers = getUsers
exports.parseUserDangerInfo = parseUserDangerInfo
exports.textUser = textUser