홍용민

다음 버스 시간 예측

Showing 1 changed file with 46 additions and 31 deletions
...@@ -10,8 +10,7 @@ const BusArrivalUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + stati ...@@ -10,8 +10,7 @@ const BusArrivalUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + stati
10 //console.log(BusArrivalUrl); 10 //console.log(BusArrivalUrl);
11 11
12 var routeID = []; 12 var routeID = [];
13 -var Bus = []; // 버스 차 번호( ex) 경기 70바 3713 ) / 도착 여부 확인용 13 +var Bus = [];
14 -var BusNum = []; // 버스 번호 ( ex) 9)
15 request(BusArrivalUrl, (err, res, body) => { 14 request(BusArrivalUrl, (err, res, body) => {
16 var $ = cheerio.load(body, {decodeEntities: false}); 15 var $ = cheerio.load(body, {decodeEntities: false});
17 16
...@@ -65,32 +64,48 @@ function useFor(){ ...@@ -65,32 +64,48 @@ function useFor(){
65 } 64 }
66 setTimeout(useFor, 500); 65 setTimeout(useFor, 500);
67 66
68 -// const GateBusUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + gateStationID;
69 -
70 -// request(GateBusUrl, (err, res, body) => {
71 -// var $ = cheerio.load(body, {decodeEntities: false});
72 -
73 -// $('busArrivalList').each(function(idx){
74 -// let route = $(this).find('routeId').text();
75 -// let num = $(this).find('plateNo1').text();
76 -
77 -// var index = routeID.indexOf(route);
78 -// if(index > -1){
79 -// for(var i=0; i<Bus.length; i++){
80 -// if(Bus[i]['route'] == route){
81 -// if(Bus[i]['num'] != num){
82 -// Bus[i]['pass'] = true;
83 -// Bus[i]['num'] == num;
84 -// }
85 -// }
86 -// }
87 -// var info = new Object();
88 -// info.route = route;
89 -// info.num = num;
90 -// info.pass = false; // json 형태로 저장
91 -// Bus.push(info);
92 -// }
93 -// })
94 -// //console.log(routeID);
95 -// //console.log(Bus);
96 -// })
...\ No newline at end of file ...\ No newline at end of file
67 +const GateBusUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + gateStationID; //국제캠 정문 정류장
68 +let date = new Date();
69 +let curHour = date.getHours(); // 현재 시간
70 +let curMinute = date.getMinutes(); //현재 분
71 +
72 +function predict(){
73 + request(GateBusUrl, (err, res, body) => {
74 + var $ = cheerio.load(body, {decodeEntities: false});
75 +
76 + $('busArrivalList').each(function(idx){
77 + let route = $(this).find('routeId').text();
78 +
79 + var index = Bus.findIndex(function(e, idx){ return e.routeId == route;});
80 + if(index > -1){
81 + //var num = $(this).find('plateNo1').text(); // 버스 차량번호 ex) 70사 1290
82 + var time = $(this).find('predictTime1').text(); // 버스 예상 도착시간
83 + var predictHour1 = date.getHours();
84 + var predictMinute1 = date.getMinutes() + Bus[index].mintime - (3 - (time*1)); // 사색의 광장 -> 경희대학교 3분정도 소요
85 + var predictHour2 = date.getHours();
86 + var predictMinute2 = date.getMinutes() + Bus[index].maxtime - (3 - (time*1)); // time*1 : string to integer 형변환
87 +
88 + if(predictMinute1 >= 60){
89 + predictHour1 += 1;
90 + predictMinute1 -= 60;
91 + }
92 + if(predictMinute2 >= 60){
93 + predictHour2 += 1;
94 + predictMinute2 -= 60;
95 + }
96 +
97 + Bus[index]["MinPredictTime"] = predictHour1 + ":" + predictMinute1;
98 + Bus[index]["MaxPredictTime"] = predictHour2 + ":" + predictMinute2;
99 + }
100 + })
101 + console.log(Bus);
102 + })
103 +}
104 +
105 +function useWhile(){
106 + while(true){
107 + setTimeout(predict, 6000);
108 + }
109 +}
110 +
111 +setTimeout(useWhile, 1000);
...\ No newline at end of file ...\ No newline at end of file
......