김윤지

Merge branch 'master' into 'master'

db통합, 배경적용



See merge request !7

71.6 KB

1 module.exports = { 1 module.exports = {
2 host: 'localhost', 2 host: 'localhost',
3 user: 'root', 3 user: 'root',
4 - password: 'abcd', 4 + password: 'Serrmica0!',
5 - database: 'db_option' 5 + database: 'db_option_total'
6 } 6 }
...\ No newline at end of file ...\ No newline at end of file
......
1 -module.exports = {
2 - host: 'localhost',
3 - user: 'root',
4 - password: 'abcd',
5 - database: 'db_option_traffic'
6 -}
...\ No newline at end of file ...\ No newline at end of file
1 const mysql = require('mysql'); 1 const mysql = require('mysql');
2 -const DB_option = require('../keys/db_option'); 2 +const DB_option = require('../keys/db_option_total');
3 3
4 module.exports = mysql.createConnection(DB_option); 4 module.exports = mysql.createConnection(DB_option);
......
1 -const mysql = require('mysql');
2 -const DB_option_traffic = require('../keys/db_option_traffic');
3 -
4 -module.exports = mysql.createConnection(DB_option_traffic);
1 -const db = require('./db.js'); 1 +const db_total = require('./db_total.js'); //DB 합침
2 -const db_traffic = require('./db_traffic.js');
3 const secret_key = require('../keys/api_option').key; 2 const secret_key = require('../keys/api_option').key;
4 const secret_key_traffic = require('../keys/api_option_traffic').key; 3 const secret_key_traffic = require('../keys/api_option_traffic').key;
5 4
...@@ -26,20 +25,13 @@ module.exports = (server, app) => { ...@@ -26,20 +25,13 @@ module.exports = (server, app) => {
26 let sending_to_client_info = {}; 25 let sending_to_client_info = {};
27 let client_send = {}; 26 let client_send = {};
28 let client_name = ""; 27 let client_name = "";
29 - let client_birth ; 28 + let client_birth;
30 let Destiny; 29 let Destiny;
31 let sql; 30 let sql;
32 let info = {} //;? 31 let info = {} //;?
33 32
34 - let Current_TrafficAcc = {}; 33 + let Current_TrafficAcc = {};
35 - let sql_traffic; 34 +
36 - //let DeathNum_index = {}; //사상자
37 - //let M_HurtNum_index = {}; //중상자
38 - //let L_HurtNum_index = {}; //경상자
39 - //let LocalName_index = {}; //지역 이름
40 - //let OccurNum_index = {}; //발생 건수
41 - //afos_fid: 다발지역 FID, bjd_cd: 법정동 코드, spot_cd:지점코드
42 -
43 //API 호출_Weather 35 //API 호출_Weather
44 const req_API = (when, what) => { 36 const req_API = (when, what) => {
45 //async await 사용하기 위하여 promise 사용 37 //async await 사용하기 위하여 promise 사용
...@@ -62,6 +54,8 @@ module.exports = (server, app) => { ...@@ -62,6 +54,8 @@ module.exports = (server, app) => {
62 }); 54 });
63 }) 55 })
64 } 56 }
57 +
58 +
65 //API 호출_Traffic 59 //API 호출_Traffic
66 const req_API_traffic = (cate1, cate2) => { 60 const req_API_traffic = (cate1, cate2) => {
67 //async await 사용하기 위하여 promise 사용 61 //async await 사용하기 위하여 promise 사용
...@@ -100,12 +94,7 @@ module.exports = (server, app) => { ...@@ -100,12 +94,7 @@ module.exports = (server, app) => {
100 Discomport_index = await req_API("index", "th"); //불쾌지수 94 Discomport_index = await req_API("index", "th"); //불쾌지수
101 Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 95 Ultra_Violet_index = await req_API("index", "uv"); //자외선지수
102 96
103 - Current_TrafficAcc = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); //cate1, cate2 97 + Current_TrafficAcc = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); //cate1, cate2
104 - /*DeathNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
105 - M_HurtNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
106 - L_HurtNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
107 - LocalName_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
108 - OccurNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");*/
109 98
110 info = { 99 info = {
111 heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 100 heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수
...@@ -121,7 +110,7 @@ module.exports = (server, app) => { ...@@ -121,7 +110,7 @@ module.exports = (server, app) => {
121 typhoon: Current_Weather.common.stormYn, //현재 태풍 110 typhoon: Current_Weather.common.stormYn, //현재 태풍
122 time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 111 time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각
123 death_prob: 0, //확률 112 death_prob: 0, //확률
124 - death_number: Current_TrafficAcc.items.item[0].caslt_cnt, //사상자 수 113 + death_number: Current_TrafficAcc.items.item[0].dth_dnv_cnt, //사망자 수
125 midhurt_number: Current_TrafficAcc.items.item[0].se_dnv_cnt, //중상자 수 114 midhurt_number: Current_TrafficAcc.items.item[0].se_dnv_cnt, //중상자 수
126 lighthurt_number: Current_TrafficAcc.items.item[0].sl_dnv_cnt, //경상자 수 115 lighthurt_number: Current_TrafficAcc.items.item[0].sl_dnv_cnt, //경상자 수
127 occur_number: Current_TrafficAcc.items.item[0].occrrnc_cnt //발생건수 116 occur_number: Current_TrafficAcc.items.item[0].occrrnc_cnt //발생건수
...@@ -143,7 +132,8 @@ module.exports = (server, app) => { ...@@ -143,7 +132,8 @@ module.exports = (server, app) => {
143 //죽을 확률 계산(내맘대로 커스텀) 132 //죽을 확률 계산(내맘대로 커스텀)
144 info.death_prob = ( 133 info.death_prob = (
145 (info.heat / 50) + (Math.abs(info.sensible_temperature - 15) / 10) + (info.discomport / 10) + (info.UV / 10) 134 (info.heat / 50) + (Math.abs(info.sensible_temperature - 15) / 10) + (info.discomport / 10) + (info.UV / 10)
146 - + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) 135 + + info.windspd * 1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10)
136 + + (info.death_number / 60) + (info.midhurt_number / 80) + (info.lighthurt_number / 90) + (info.occur_number / 90)
147 ); 137 );
148 138
149 //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보, 홈페이지 그래프에 나타날 정보 139 //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보, 홈페이지 그래프에 나타날 정보
...@@ -160,19 +150,19 @@ module.exports = (server, app) => { ...@@ -160,19 +150,19 @@ module.exports = (server, app) => {
160 }; 150 };
161 function getRandom_add_prob(min, max) { 151 function getRandom_add_prob(min, max) {
162 return Math.random() * (max - min) + min; 152 return Math.random() * (max - min) + min;
163 - } 153 + }
164 - 154 +
155 +
156 + // 심장이 크게 뛰며 확률이 증가하거나 감소 할 수 있음
157 + Math.random() * 2 >= 1 ? client_send.death += getRandom_add_prob(0, 5) : client_send.death -= getRandom_add_prob(0, 5);
158 +
165 159
166 - // 심장이 크게 뛰며 확률이 증가하거나 감소 할 수 있음 160 + //운명의 장난으로 죽을 확률이 증가하거나 감소함
167 - Math.random() * 2 >= 1 ? client_send.death += getRandom_add_prob(0,5) : client_send.death -= getRandom_add_prob(0,5) ; 161 + const rand = Math.floor(Math.random() * 6) //생년월일 중 한자리 뽑음
168 - 162 +
169 - 163 + Destiny = client_birth.charAt(rand) / 3; //명시적 형 변환
170 - //운명의 장난으로 죽을 확률이 증가하거나 감소함 164 + if (Destiny == 0) Destiny = 1; //사용자 잘못 입력했을때 예외처리
171 - const rand = Math.floor(Math.random() * 6) //생년월일 중 한자리 뽑음 165 + Math.random() * 2 >= 1 ? client_send.death += Destiny : client_send.death -= Destiny;
172 -
173 - Destiny=client_birth.charAt(rand)/3; //명시적 형 변환
174 - if(Destiny==0)Destiny=1; //사용자 잘못 입력했을때 예외처리
175 - Math.random() * 2 >= 1 ? client_send.death += Destiny : client_send.death -= Destiny ;
176 166
177 167
178 //만약 날이 너무 안좋아서 확률이 100을 넘긴다면 100으로 예외처리 168 //만약 날이 너무 안좋아서 확률이 100을 넘긴다면 100으로 예외처리
...@@ -180,21 +170,17 @@ module.exports = (server, app) => { ...@@ -180,21 +170,17 @@ module.exports = (server, app) => {
180 client_send.death = 100; 170 client_send.death = 100;
181 } 171 }
182 172
183 - console.log("client send data \n",client_send) 173 + console.log("client send data \n", client_send)
184 - 174 +
185 app.get("socket").emit("weather_and_traffic_Info_minutely_send_to_client", client_send); // 클라이언트에게 정보 담아서 이벤트 발산 175 app.get("socket").emit("weather_and_traffic_Info_minutely_send_to_client", client_send); // 클라이언트에게 정보 담아서 이벤트 발산
186 console.log("emit"); 176 console.log("emit");
187 177
188 //db에 저장 178 //db에 저장
189 - sql = "INSERT INTO weatherInfo (time,wind,temperature,rain,prob) VALUES (?,?,?,?,?)"; 179 + sql = "INSERT INTO apisInfo (time,wind,temperature,rain,prob,tdeath,mhurt,lhurt,occurence) VALUES (?,?,?,?,?,?,?,?,?)";
190 - db.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death], (err, result) => { 180 + 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) => {
191 - if (err) console.log(err);
192 - })
193 -
194 - sql_traffic = "INSERT INTO trafficInfo (tdeath,mhurt,lhurt,occurence,prob) VALUES (?,?,?,?,?)";
195 - db_traffic.query(sql_traffic, [client_send.trafficdeath, client_send.mhurt, client_send.lhurt, client_send.occurence, client_send.death], (err, result) => {
196 if (err) console.log(err); 181 if (err) console.log(err);
197 }) 182 })
183 +
198 } catch (err) { //promise err or try err catch 184 } catch (err) { //promise err or try err catch
199 console.log("================Error Occured !!================\n", err); 185 console.log("================Error Occured !!================\n", err);
200 } 186 }
...@@ -205,7 +191,7 @@ module.exports = (server, app) => { ...@@ -205,7 +191,7 @@ module.exports = (server, app) => {
205 const Start_Interval = (second, CALL) => { 191 const Start_Interval = (second, CALL) => {
206 CALL(); //처음 불러올때 한번 호출하고 192 CALL(); //처음 불러올때 한번 호출하고
207 call_interval = setInterval(CALL, second * 1000); //그 후에 1분마다 호출 193 call_interval = setInterval(CALL, second * 1000); //그 후에 1분마다 호출
208 - } 194 + }
209 195
210 io.on('connection', (socket) => { //프론트와 소켓 연결시 이벤트 루프 동작 196 io.on('connection', (socket) => { //프론트와 소켓 연결시 이벤트 루프 동작
211 197
......
1 var express = require('express'); 1 var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 -var db = require('../lib/db'); 3 +var db = require('../lib/db_total');
4 4
5 /* GET home page. */ 5 /* GET home page. */
6 6
7 router.post('/starting' , (req,res) => { 7 router.post('/starting' , (req,res) => {
8 - res.redirect(`/name/${req.body.name}/birth/${req.body.birth}`); 8 + res.redirect(`/name/${req.body.name}/birth/${req.body.birth}`);
9 }) 9 })
10 -router.get('/name/:name/birth/:birth', (req,res) => { 10 +router.get('/name/:name/birth/:birth', (req, res) => {
11 -
12 - // 렌더링 변수
13 - var time = new Array(); // 타임스탬프
14 - var ptArr = new Array(); // 현재 온도
15 - var wsArr = new Array(); // 풍속
16 - var rainArr = new Array(); // 강우량
17 - var probArr = new Array(); // 사망 확률
18 - var dataLen = 0; // 데이터 개수
19 - var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음
20 - var sql = ""; // 쿼리
21 - var count = 0;
22 - const name = req.params.name;
23 - const birth = req.params.birth;
24 11
25 - // 이전 10분간 데이터 찾기 12 + // 렌더링 변수
26 - sql = "SELECT * FROM weatherInfo WHERE time >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -20 MINUTE), '%Y-%m-%d %H:%i:%s')"; 13 + var time = new Array(); // 타임스탬프
27 - db.query(sql, function(err, rows, fields){ 14 + var ptArr = new Array(); // 현재 온도
28 - if(err) 15 + var wsArr = new Array(); // 풍속
29 - { 16 + var rainArr = new Array(); // 강우량
30 - console.log(err); 17 + var probArr = new Array(); // 사망 확률
31 - } 18 + var dataLen = 0; // 데이터 개수
32 - else 19 + var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음
33 - { 20 + var sql = ""; // 쿼리
34 - if (rows.length == 0) 21 + var count = 0;
35 - { 22 + const name = req.params.name;
36 - empty = 1; 23 + const birth = req.params.birth;
37 - } 24 + //const address = req.params.address;
38 - else 25 + ////////////////////////////////////////
39 - { 26 + var deathArr = new Array();
40 - for(var i = rows.length - 1; i >= 0; i--) 27 + var mhurtArr = new Array();
41 - { 28 + var lhurtArr = new Array();
42 - probArr.unshift(rows[i].prob); 29 + var occurArr = new Array();
43 - time.unshift(rows[i].time); 30 + ////////////////////////////////////////
44 - ptArr.unshift(rows[i].temperature);
45 - wsArr.unshift(rows[i].wind);
46 - rainArr.unshift(rows[i].rain);
47 - count = count + 1;
48 31
49 - if (count == 10){ 32 +
50 - break; 33 + // 이전 10분간 데이터 찾기
51 - } 34 + sql = "SELECT * FROM apisInfo WHERE time >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -20 MINUTE), '%Y-%m-%d %H:%i:%s')";
35 + db.query(sql, function (err, rows, fields) {
36 + if (err) {
37 + console.log(err);
52 } 38 }
53 - } 39 + else {
40 + if (rows.length == 0) {
41 + empty = 1;
42 + }
43 + else {
44 + for (var i = rows.length - 1; i >= 0; i--) {
45 + probArr.unshift(rows[i].prob);
46 + time.unshift(rows[i].time);
47 + ptArr.unshift(rows[i].temperature);
48 + wsArr.unshift(rows[i].wind);
49 + rainArr.unshift(rows[i].rain);
50 + ////
51 + deathArr.unshift(rows[i].tdeath);
52 + mhurtArr.unshift(rows[i].mhurt);
53 + lhurtArr.unshift(rows[i].lhurt);
54 + occurArr.unshift(rows[i].occurence);
55 + count = count + 1;
56 +
57 + if (count == 10) {
58 + break;
59 + }
60 + }
61 + }
54 62
55 - dataLen = probArr.length; 63 + dataLen = probArr.length;
56 - res.render('index', { 64 + res.render('index', {
57 - empty, 65 + empty,
58 - time, 66 + time,
59 - ptArr, 67 + ptArr,
60 - wsArr, 68 + wsArr,
61 - rainArr, 69 + rainArr,
62 - probArr, 70 + probArr,
63 - dataLen, 71 + dataLen,
64 - name, 72 + name,
65 - birth 73 + birth,
66 - }); 74 + deathArr,
67 - } 75 + mhurtArr,
68 - }); 76 + lhurtArr,
69 -} ) 77 + occurArr
70 -router.get('/', function(req, res, next) { 78 + });
71 - res.render( 'main' ); 79 + }
80 + });
81 +})
82 +router.get('/', function (req, res, next) {
83 + res.render('main');
72 }); 84 });
73 85
74 module.exports = router; 86 module.exports = router;
......
This diff is collapsed. Click to expand it.
1 <!-- <!DOCTYPE html> 1 <!-- <!DOCTYPE html>
2 <html> 2 <html>
3 -
4 <head> 3 <head>
5 <!-- font --> 4 <!-- font -->
6 <link href="https://fonts.googleapis.com/css?family=Nanum+Brush+Script&amp;subset=korean" rel="stylesheet" /> 5 <link href="https://fonts.googleapis.com/css?family=Nanum+Brush+Script&amp;subset=korean" rel="stylesheet" />
...@@ -39,7 +38,7 @@ ...@@ -39,7 +38,7 @@
39 <script src="modules/exporting.js"></script> 38 <script src="modules/exporting.js"></script>
40 <script src="modules/export-data.js"></script> 39 <script src="modules/export-data.js"></script>
41 40
42 - <img src="images/background.jpg" alt="" style="z-index:-1; min-width: 100%; min-height: 100%"> 41 + <img src="images/newbackground.png" alt="" style="z-index:-1; min-width: 100%; min-height: 100%">
43 42
44 <div id="banner"> 43 <div id="banner">
45 <div id="container1" style="width:1260px; height: 400px; margin: 0 auto;"></div> 44 <div id="container1" style="width:1260px; height: 400px; margin: 0 auto;"></div>
......
...@@ -24,48 +24,48 @@ ...@@ -24,48 +24,48 @@
24 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> 24 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
25 25
26 <style type="text/css"> 26 <style type="text/css">
27 - html, 27 + html,
28 - { 28 + {
29 - margin: 0; 29 + margin: 0;
30 - padding: 0; 30 + padding: 0;
31 - } 31 + }
32 - 32 +
33 - body { 33 + body {
34 - margin: 0; 34 + margin: 0;
35 - padding: 0; 35 + padding: 0;
36 - 36 +
37 - /* background-image: url('background.jpg'); 37 + /* background-image: url('newbackground.png');
38 - background-repeat: no-repeat; 38 + background-repeat: no-repeat;
39 - background-size: cover; */ 39 + background-size: cover; */
40 - /* background: linear-gradient( to bottom, #fbc2eb, #a6c1ee ); */ 40 + /* background: linear-gradient( to bottom, #fbc2eb, #a6c1ee ); */
41 - } 41 + }
42 - 42 +
43 - h1 { 43 + h1 {
44 - color: white; 44 + color: white;
45 - font-family: 'Nanum Brush Script', cursive; 45 + font-family: 'Nanum Brush Script', cursive;
46 - } 46 + }
47 - 47 +
48 - label { 48 + label {
49 - color: white; 49 + color: white;
50 - font-size: 20px; 50 + font-size: 20px;
51 - font-family: 'Yeon Sung', cursive; 51 + font-family: 'Yeon Sung', cursive;
52 - } 52 + }
53 - 53 +
54 - #banner { 54 + #banner {
55 - position: absolute; 55 + position: absolute;
56 - top: 0; 56 + top: 0;
57 - width: 100%; 57 + width: 100%;
58 - } 58 + }
59 - 59 +
60 - #footer { 60 + #footer {
61 - position: absolute; 61 + position: absolute;
62 - bottom: 0; 62 + bottom: 0;
63 - width: 100%; 63 + width: 100%;
64 - height: 50px; 64 + height: 50px;
65 - text-align: center; 65 + text-align: center;
66 - font-family: 'Yeon Sung', cursive; 66 + font-family: 'Yeon Sung', cursive;
67 - color: white; 67 + color: white;
68 - } 68 + }
69 </style> 69 </style>
70 70
71 </head> 71 </head>
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
21 21
22 /* background: linear-gradient( to bottom, #65799B, rgb(38, 14, 41) ); */ 22 /* background: linear-gradient( to bottom, #65799B, rgb(38, 14, 41) ); */
23 /* background-color:#65799B; */ 23 /* background-color:#65799B; */
24 - /* background-image: url("2.jpg"); 24 + /* background-image: url("newbackground.png");
25 background-repeat: no-repeat; 25 background-repeat: no-repeat;
26 background-size: cover; */ 26 background-size: cover; */
27 } 27 }
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
38 <script src="highcharts.js"></script> 38 <script src="highcharts.js"></script>
39 <script src="modules/exporting.js"></script> 39 <script src="modules/exporting.js"></script>
40 <script src="modules/export-data.js"></script> 40 <script src="modules/export-data.js"></script>
41 - <img src="images/background.jpg" alt="" style="z-index:-1; min-width: 100%; min-height: 100%"> 41 + <img src="images/newbackground.png" alt="" style="z-index:-1; min-width: 100%; min-height: 100%">
42 42
43 <div id="banner"> 43 <div id="banner">
44 <div id="container1" style="width:1260px; height: 400px; margin: 0 auto"></div> 44 <div id="container1" style="width:1260px; height: 400px; margin: 0 auto"></div>
......