-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
70 lines (62 loc) · 2.45 KB
/
index.js
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
let fs = require('fs');
let path = require('path');
let instance = null;
let targetBreakpointIndex = undefined;
let breakpoints = undefined;
let generalMessages = undefined;
let specificMessages = undefined;
/**
* Import modules
*/
let pollutantBreakpointFinder = require('./utils/PollutantBreakpointFinder');
let messageService = require('./utils/MessageService');
let calculator = require('./utils/Calculator');
let constants = require('./utils/Constants');
class AQICalculator {
constructor() {
breakpoints = JSON.parse(fs.readFileSync(path.join(__dirname, 'resources/aqi-breakpoint.json'), 'utf8'));
generalMessages = JSON.parse(fs.readFileSync(path.join(__dirname, 'resources/aqi-general-messages.json'), 'utf8'));
specificMessages = JSON.parse(fs.readFileSync(path.join(__dirname, 'resources/aqi-specific-messages.json'), 'utf8'));
}
getAQIResult(pollutantCode, concentration) {
/* Grab concentration object */
return new Promise((resolve, reject) => {
pollutantBreakpointFinder.getConcentrationRangeWithAvgConcentration(pollutantCode, concentration, breakpoints).then((breakpointIndex) => {
targetBreakpointIndex = breakpointIndex;
let aqi = calculator.calculateAQI(concentration, targetBreakpointIndex);
return aqi;
}, (err) => {
reject(err);
}).then((aqi) => {
let generalMessage = messageService.getGeneralMessage(aqi, generalMessages);
let specificMessage = messageService.getSpecificMessage(pollutantCode, aqi, specificMessages);
let result = {
pollutant: pollutantCode,
concentration: concentration,
aqi: aqi,
category: generalMessage.category,
generalMessage: generalMessage.message,
healthEffectsStatements: specificMessage.healthEffectsStatements,
guidanceStatement: specificMessage.guidanceStatement
}
resolve(result);
});
});
}
getNowcastAQIResult(pollutantCode, concentrations) {
return new Promise((resolve, reject) => {
calculator.calculateNowcastConcentration(pollutantCode, concentrations).then((nowcastConcentration) => {
this.getAQIResult(pollutantCode, nowcastConcentration).then((result) => {
resolve(result);
});
}).catch((err) => {
reject(err);
});
});
}
}
const AQICalculatorInstance = new AQICalculator();
module.exports = {
AQICalculator: AQICalculatorInstance,
PollutantType: constants.POLLUTANT_TYPE
};