김주희

대기오염 api 추가

const db_total = require('./db_total.js'); //DB 합침
const secret_key = require('../keys/api_option').key;
const secret_key_traffic = require('../keys/api_option_traffic').key;
const secret_key_air = require('../keys/api_option_air').key;
const requesting = require('request');
......@@ -31,6 +32,7 @@ module.exports = (server, app) => {
let info = {} //;?
let Current_TrafficAcc = {};
let Current_AirPollution = {};
//API 호출_Weather
const req_API = (when, what) => {
......@@ -80,6 +82,29 @@ module.exports = (server, app) => {
}) //Promise
}
//API 호출_Traffic
const req_API_air = () => {
//async await 사용하기 위하여 promise 사용
return new Promise((resolve, reject) => {
requesting.get({
// api를 요청할 주소 -- 시크릿키,위도,경도 입력
url: `http://api.airvisual.com/v2/nearest_city?lat=${lat}&lon=${lon}&key=${secret_key_air}`,
json: true
},
//api에게 응답 받았을때 실행되는 callback function
function (err, api_res, api_body) {
//err 존재시 promise reject 호출
if (err) reject(err);
// api의 response이 있을경우 promise resolve 호출
if (api_res) {
console.log("calling air pollution api");
resolve(api_body);
}
});
}) //Promise
}
const API_bundle = async () => {
try {
var order = 0;
......@@ -93,6 +118,7 @@ module.exports = (server, app) => {
Discomport_index = await req_API("index", "th"); //불쾌지수
Ultra_Violet_index = await req_API("index", "uv"); //자외선지수
Current_TrafficAcc = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); //cate1, cate2
Current_AirPollution = await req_API_air();
order = 1;
break;
case 1:
......@@ -111,11 +137,15 @@ module.exports = (server, app) => {
typhoon: Current_Weather.common.stormYn, //현재 태풍
time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각
death_prob: 0, //확률
death_number: Current_TrafficAcc.items.item[0].dth_dnv_cnt, //사망자 수
midhurt_number: Current_TrafficAcc.items.item[0].se_dnv_cnt, //중상자 수
lighthurt_number: Current_TrafficAcc.items.item[0].sl_dnv_cnt, //경상자 수
occur_number: Current_TrafficAcc.items.item[0].occrrnc_cnt //발생건수
//"spot_cd": "11440001"
occur_number: Current_TrafficAcc.items.item[0].occrrnc_cnt, //발생건수
airpollution: Current_AirPollution.data.current.pollution.aqius //대기질 지수
//aqi = 대기질 지수, aqius = us기준으로 산정, mainus:p2 미세먼지 기준 pm2.5
//0-50 좋음 //51-100 보통 // 101-150 나쁨
}
console.log("API INFO \n", info);
......@@ -135,6 +165,7 @@ module.exports = (server, app) => {
(info.heat / 50) + (Math.abs(info.sensible_temperature - 15) / 10) + (info.discomport / 10) + (info.UV / 10)
+ info.windspd * 1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10)
+ (info.death_number / 60) + (info.midhurt_number / 80) + (info.lighthurt_number / 90) + (info.occur_number / 90)
+ (info.airpollution / 100)
);
//이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보, 홈페이지 그래프에 나타날 정보
......@@ -147,7 +178,8 @@ module.exports = (server, app) => {
trafficdeath: info.death_number,
mhurt: info.midhurt_number,
lhurt: info.lighthurt_number,
occurence: info.occur_number
occurence: info.occur_number,
pollution: info.airpollution
};
function getRandom_add_prob(min, max) {
return Math.random() * (max - min) + min;
......@@ -173,12 +205,12 @@ module.exports = (server, app) => {
console.log("client send data \n", client_send)
app.get("socket").emit("weather_and_traffic_Info_minutely_send_to_client", client_send); // 클라이언트에게 정보 담아서 이벤트 발산
app.get("socket").emit("weather_traffic_air_Info_minutely_send_to_client", client_send); // 클라이언트에게 정보 담아서 이벤트 발산
console.log("emit");
//db에 저장
sql = "INSERT INTO apisInfo (time,wind,temperature,rain,prob,tdeath,mhurt,lhurt,occurence) VALUES (?,?,?,?,?,?,?,?,?)";
db_total.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death, client_send.trafficdeath, client_send.mhurt, client_send.lhurt, client_send.occurence], (err, result) => {
sql = "INSERT INTO apisInfo (time,wind,temperature,rain,prob,tdeath,mhurt,lhurt,occurence,pollution) VALUES (?,?,?,?,?,?,?,?,?,?)";
db_total.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death, client_send.trafficdeath, client_send.mhurt, client_send.lhurt, client_send.occurence, client_send.pollution], (err, result) => {
if (err) console.log(err);
})
order = 2;
......