Showing
3 changed files
with
54 additions
and
24 deletions
| ... | @@ -21,7 +21,6 @@ module.exports = (server, app) => { | ... | @@ -21,7 +21,6 @@ module.exports = (server, app) => { |
| 21 | let Ultra_Violet_index = {}; | 21 | let Ultra_Violet_index = {}; |
| 22 | let Traffic_Accident = {}; | 22 | let Traffic_Accident = {}; |
| 23 | let Death_Factors = []; | 23 | let Death_Factors = []; |
| 24 | - let Death_Factor = ""; | ||
| 25 | let sending_to_client_info = {}; | 24 | let sending_to_client_info = {}; |
| 26 | let client_send = {}; | 25 | let client_send = {}; |
| 27 | let client_name = ""; | 26 | let client_name = ""; |
| ... | @@ -104,6 +103,8 @@ module.exports = (server, app) => { | ... | @@ -104,6 +103,8 @@ module.exports = (server, app) => { |
| 104 | typhoon: Current_Weather.common.stormYn, //현재 태풍 | 103 | typhoon: Current_Weather.common.stormYn, //현재 태풍 |
| 105 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 | 104 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 |
| 106 | traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수 | 105 | traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수 |
| 106 | + death_factor: "", //사망요인 | ||
| 107 | + death_countdown: 0, //죽기까지 남은 시간(단위: 초) | ||
| 107 | death_prob: 0 //확률 | 108 | death_prob: 0 //확률 |
| 108 | } | 109 | } |
| 109 | console.log("API INFO \n", info); | 110 | console.log("API INFO \n", info); |
| ... | @@ -126,29 +127,50 @@ module.exports = (server, app) => { | ... | @@ -126,29 +127,50 @@ module.exports = (server, app) => { |
| 126 | + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) + (info.traffic / 5) | 127 | + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) + (info.traffic / 5) |
| 127 | ); | 128 | ); |
| 128 | 129 | ||
| 130 | + // ------------------------------ death_factor 정의 ------------------------------ | ||
| 131 | + | ||
| 132 | + if (info.typhoon == "Y") | ||
| 133 | + Death_Factors.push("태풍에 휩쓸려 사망"); | ||
| 134 | + if (info.lightning == 1) | ||
| 135 | + Death_Factors.push("번개에 맞아 사망"); | ||
| 136 | + if (info.warning == "Y") | ||
| 137 | + Death_Factors.push("우박에 머리를 맞아 사망"); | ||
| 138 | + if (info.windspd*1 >= 2) | ||
| 139 | + Death_Factors.push("애인에게 바람맞아 그 충격으로 인해 사망"); | ||
| 140 | + if (info.heat*1 >= 100) | ||
| 141 | + Death_Factors.push("학점이 너무 낮아 화병으로 사망"); | ||
| 142 | + if (info.discomport*1 >= 40) | ||
| 143 | + Death_Factors.push("날씨가 너무 찝찝해서 사망"); | ||
| 144 | + | ||
| 145 | + Death_Factors.push("심장마비로 사망"); //반드시 하나의 요인은 추가되어야 함 | ||
| 146 | + | ||
| 147 | + info.death_factor = Death_Factors[Math.round(Math.random()*Death_Factors.length)]; //사망요인 하나를 렌덤으로 고른다. | ||
| 148 | + | ||
| 149 | + | ||
| 129 | //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 | 150 | //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 |
| 130 | client_send = { | 151 | client_send = { |
| 131 | time: info.time, | 152 | time: info.time, |
| 132 | wind: info.windspd, | 153 | wind: info.windspd, |
| 133 | temperature: info.current_temperature, | 154 | temperature: info.current_temperature, |
| 134 | rain: info.rain, | 155 | rain: info.rain, |
| 135 | - death: info.death_prob | 156 | + death: info.death_prob, |
| 157 | + factor: info.death_factor, | ||
| 136 | }; | 158 | }; |
| 137 | function getRandom_add_prob(min, max) { | 159 | function getRandom_add_prob(min, max) { |
| 138 | return Math.random() * (max - min) + min; | 160 | return Math.random() * (max - min) + min; |
| 139 | - } | 161 | + } |
| 140 | 162 | ||
| 141 | 163 | ||
| 142 | - // 심장이 크게 뛰며 확률이 증가하거나 감소 할 수 있음 | 164 | + //심장이 크게 뛰며 확률이 증가하거나 감소 할 수 있음 |
| 143 | - Math.random() * 2 >= 1 ? client_send.death += getRandom_add_prob(0,5) : client_send.death -= getRandom_add_prob(0,5) ; | 165 | + Math.random() * 2 >= 1 ? client_send.death += getRandom_add_prob(0,5) : client_send.death -= getRandom_add_prob(0,5) ; |
| 144 | 166 | ||
| 145 | 167 | ||
| 146 | - //운명의 장난으로 죽을 확률이 증가하거나 감소함 | 168 | + //운명의 장난으로 죽을 확률이 증가하거나 감소함 |
| 147 | - const rand = Math.floor(Math.random() * 6) * 10//생년월일 중 한자리 뽑음 | 169 | + const rand = Math.floor(Math.random() * 6) * 10//생년월일 중 한자리 뽑음 |
| 148 | 170 | ||
| 149 | - Destiny=client_birth.charAt(rand)/3; //명시적 형 변환 | 171 | + Destiny=client_birth.charAt(rand)/3; //명시적 형 변환 |
| 150 | - if(Destiny==0)Destiny=1; //사용자 잘못 입력했을때 예외처리 | 172 | + if(Destiny==0)Destiny=1; //사용자 잘못 입력했을때 예외처리 |
| 151 | - Math.random() * 2 >= 1 ? client_send.death += Destiny : client_send.death -= Destiny ; | 173 | + Math.random() * 2 >= 1 ? client_send.death += Destiny : client_send.death -= Destiny ; |
| 152 | 174 | ||
| 153 | 175 | ||
| 154 | //만약 날이 너무 안좋아서 확률이 100을 넘긴다면 100으로 예외처리 | 176 | //만약 날이 너무 안좋아서 확률이 100을 넘긴다면 100으로 예외처리 |
| ... | @@ -162,8 +184,8 @@ module.exports = (server, app) => { | ... | @@ -162,8 +184,8 @@ module.exports = (server, app) => { |
| 162 | console.log("emit"); | 184 | console.log("emit"); |
| 163 | 185 | ||
| 164 | //db에 저장 | 186 | //db에 저장 |
| 165 | - sql = "INSERT INTO weather_info (time,wind,temperature,rain,prob) VALUES (?,?,?,?,?)"; | 187 | + sql = "INSERT INTO weather_info (time,wind,temperature,rain,prob,factor) VALUES (?,?,?,?,?,?)"; |
| 166 | - db.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death], (err, result) => { | 188 | + db.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death, client_send.factor], (err, result) => { |
| 167 | if (err) console.log(err); | 189 | if (err) console.log(err); |
| 168 | }) | 190 | }) |
| 169 | } catch (err) { //promise err or try err catch | 191 | } catch (err) { //promise err or try err catch | ... | ... |
| ... | @@ -15,6 +15,7 @@ router.get('/name/:name/birth/:birth', (req,res) => { | ... | @@ -15,6 +15,7 @@ router.get('/name/:name/birth/:birth', (req,res) => { |
| 15 | var wsArr = new Array(); // 풍속 | 15 | var wsArr = new Array(); // 풍속 |
| 16 | var rainArr = new Array(); // 강우량 | 16 | var rainArr = new Array(); // 강우량 |
| 17 | var probArr = new Array(); // 사망 확률 | 17 | var probArr = new Array(); // 사망 확률 |
| 18 | + var factorArr = new Array(); // 사망요인 | ||
| 18 | var dataLen = 0; // 데이터 개수 | 19 | var dataLen = 0; // 데이터 개수 |
| 19 | var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음 | 20 | var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음 |
| 20 | var sql = ""; // 쿼리 | 21 | var sql = ""; // 쿼리 |
| ... | @@ -44,6 +45,7 @@ router.get('/name/:name/birth/:birth', (req,res) => { | ... | @@ -44,6 +45,7 @@ router.get('/name/:name/birth/:birth', (req,res) => { |
| 44 | ptArr.unshift(rows[i].temperature); | 45 | ptArr.unshift(rows[i].temperature); |
| 45 | wsArr.unshift(rows[i].wind); | 46 | wsArr.unshift(rows[i].wind); |
| 46 | rainArr.unshift(rows[i].rain); | 47 | rainArr.unshift(rows[i].rain); |
| 48 | + factorArr.unshift(rows[i].factor); | ||
| 47 | count = count + 1; | 49 | count = count + 1; |
| 48 | 50 | ||
| 49 | if (count == 10){ | 51 | if (count == 10){ |
| ... | @@ -62,7 +64,8 @@ router.get('/name/:name/birth/:birth', (req,res) => { | ... | @@ -62,7 +64,8 @@ router.get('/name/:name/birth/:birth', (req,res) => { |
| 62 | probArr, | 64 | probArr, |
| 63 | dataLen, | 65 | dataLen, |
| 64 | name, | 66 | name, |
| 65 | - birth | 67 | + birth, |
| 68 | + factorArr, | ||
| 66 | }); | 69 | }); |
| 67 | } | 70 | } |
| 68 | }); | 71 | }); | ... | ... |
| ... | @@ -59,7 +59,7 @@ | ... | @@ -59,7 +59,7 @@ |
| 59 | 59 | ||
| 60 | <script src="/socket.io/socket.io.js"></script> | 60 | <script src="/socket.io/socket.io.js"></script> |
| 61 | <script type="text/javascript"> | 61 | <script type="text/javascript"> |
| 62 | - let client_data = { | 62 | + let client_data = { |
| 63 | birth: "<%=birth%>", | 63 | birth: "<%=birth%>", |
| 64 | name: "<%=name%>" | 64 | name: "<%=name%>" |
| 65 | } | 65 | } |
| ... | @@ -131,7 +131,7 @@ | ... | @@ -131,7 +131,7 @@ |
| 131 | }, | 131 | }, |
| 132 | tooltip: { | 132 | tooltip: { |
| 133 | headerFormat: "<b>{series.name}</b><br/>", | 133 | headerFormat: "<b>{series.name}</b><br/>", |
| 134 | - pointFormat: "{point.x:%Y년%m월%d일 %H시%M분}<br/>의 사망률 : {point.y:.2f}%" | 134 | + pointFormat: "{point.x:%Y년%m월%d일 %H시%M분}<br/>의 사망률 : {point.y:.2f}%<br/>사망요인 : {point.factor}" |
| 135 | }, | 135 | }, |
| 136 | legend: { | 136 | legend: { |
| 137 | //enabled: false | 137 | //enabled: false |
| ... | @@ -150,7 +150,7 @@ | ... | @@ -150,7 +150,7 @@ |
| 150 | enabled: false | 150 | enabled: false |
| 151 | }, | 151 | }, |
| 152 | 152 | ||
| 153 | -plotOptions: { | 153 | + plotOptions: { |
| 154 | series: { | 154 | series: { |
| 155 | marker: { | 155 | marker: { |
| 156 | radius: 6 | 156 | radius: 6 |
| ... | @@ -170,14 +170,17 @@ plotOptions: { | ... | @@ -170,14 +170,17 @@ plotOptions: { |
| 170 | for (; j < 10 - length; j++) { | 170 | for (; j < 10 - length; j++) { |
| 171 | deathArr.push({ | 171 | deathArr.push({ |
| 172 | x: time + i * 60000, | 172 | x: time + i * 60000, |
| 173 | - y: 0 | 173 | + y: 0, |
| 174 | + factor: "없음" | ||
| 174 | }) | 175 | }) |
| 175 | i++; | 176 | i++; |
| 176 | } | 177 | } |
| 177 | 178 | ||
| 178 | - <% probArr.forEach((probArr) => {%> | 179 | + //<% probArr.forEach((probArr) => {%> |
| 179 | - var temp; | 180 | + <% for(var n in probArr) {%> |
| 180 | - temp = <%=probArr %>; | 181 | + var temp, temp2; |
| 182 | + temp = <%=probArr[n] %>; | ||
| 183 | + temp2 = <%=factorArr[n] %>; | ||
| 181 | 184 | ||
| 182 | // for(;j<10;j++) | 185 | // for(;j<10;j++) |
| 183 | // { | 186 | // { |
| ... | @@ -190,11 +193,12 @@ plotOptions: { | ... | @@ -190,11 +193,12 @@ plotOptions: { |
| 190 | 193 | ||
| 191 | deathArr.push({ | 194 | deathArr.push({ |
| 192 | x: time + i * 60000, | 195 | x: time + i * 60000, |
| 193 | - y: temp | 196 | + y: temp, |
| 197 | + factor: temp2 | ||
| 194 | }) | 198 | }) |
| 195 | i++; | 199 | i++; |
| 196 | 200 | ||
| 197 | - <%}) %> | 201 | + <%} %> |
| 198 | 202 | ||
| 199 | return deathArr; | 203 | return deathArr; |
| 200 | })(), | 204 | })(), |
| ... | @@ -205,7 +209,6 @@ plotOptions: { | ... | @@ -205,7 +209,6 @@ plotOptions: { |
| 205 | 209 | ||
| 206 | }); | 210 | }); |
| 207 | 211 | ||
| 208 | - | ||
| 209 | var chart2 = Highcharts.chart("container2", { | 212 | var chart2 = Highcharts.chart("container2", { |
| 210 | chart: { | 213 | chart: { |
| 211 | type: "spline", | 214 | type: "spline", |
| ... | @@ -593,16 +596,18 @@ plotOptions: { | ... | @@ -593,16 +596,18 @@ plotOptions: { |
| 593 | 596 | ||
| 594 | 597 | ||
| 595 | }); | 598 | }); |
| 599 | + | ||
| 596 | var socket = io.connect('/', { transports: ['websocket'], upgrade: false }); | 600 | var socket = io.connect('/', { transports: ['websocket'], upgrade: false }); |
| 597 | socket.emit("connection", client_data); | 601 | socket.emit("connection", client_data); |
| 598 | socket.on("weatherInfo_minutely_send_to_client", (info) => { //서버에서 client에게 메세지 전송 | 602 | socket.on("weatherInfo_minutely_send_to_client", (info) => { //서버에서 client에게 메세지 전송 |
| 599 | console.log(info); | 603 | console.log(info); |
| 600 | 604 | ||
| 601 | - var date = new Date().getTime(); | 605 | + var date = new Date().getTime(); |
| 602 | 606 | ||
| 603 | chart1.series[0].addPoint({ | 607 | chart1.series[0].addPoint({ |
| 604 | x: date, | 608 | x: date, |
| 605 | y: info.death, | 609 | y: info.death, |
| 610 | + factor: info.factor, | ||
| 606 | color:"#FFE08C" | 611 | color:"#FFE08C" |
| 607 | }); | 612 | }); |
| 608 | chart2.series[0].addPoint({ | 613 | chart2.series[0].addPoint({ | ... | ... |
-
Please register or login to post a comment