홍용민

버스별 변수 분리

......@@ -8,6 +8,7 @@ require('dotenv').config();
//console.log(BusArrivalUrl);
var routeID = ['200000112', '200000115', '234000016', '200000103'];
var Bus = [];
var BusNum = [];
// request(BusArrivalUrl, (err, res, body) => {
// var $ = cheerio.load(body, {decodeEntities: false});
// $('busArrivalList').each(function(idx){
......@@ -41,6 +42,7 @@ function getBusNum(){
newBus.MaxTime = maxtime;
//console.log(newBus);
Bus.push(newBus);
BusNum.push(num);
//console.log(Bus);
})
})
......@@ -51,9 +53,19 @@ for(var i=0; i<routeID.length; i++){
const GateBusUrl = bus_url + '?servicekey=' + process.env.key + '&stationId=' + gateStationID; //국제캠 정문 정류장
let date = new Date();
let predictTime = ['-1', '-1', '-1', '-1'];
let body = new Object();
module.exports.data = [];
function predict(){
console.log(GateBusUrl);
//console.log(GateBusUrl);
if(module.exports.data.length == 0){
for(var i=0; i<BusNum.length; i++){
var info = new Object();
info.BusNum = BusNum[i];
module.exports.data.push(info);
}
}
request(GateBusUrl, (err, res, body) => {
var $ = cheerio.load(body, {decodeEntities: false});
$('busArrivalList').each(function(idx){
......@@ -61,6 +73,7 @@ function predict(){
var index = Bus.findIndex(function(e, idx){ return e.routeId == route;});
if(index > -1){
//var num = $(this).find('plateNo1').text(); // 버스 차량번호 ex) 70사 1290
console.log(Bus[index].BusNum);
var time = $(this).find('predictTime1').text(); // 버스 예상 도착시간
var predictHour1 = date.getHours();
var predictMinute1 = date.getMinutes() + parseInt(Bus[index].MinTime) - (3 - parseInt(time)); // 사색의 광장 -> 경희대학교 3분정도 소요
......@@ -89,14 +102,15 @@ function predict(){
//controller에 데이터 전송
module.exports.data = {};
module.exports.data.remainTime = gapHour + "시간" + gapMin + "분 이상";
module.exports.data.ETD_min_H = predictTime[0];
module.exports.data.ETD_min_M = predictTime[1];
module.exports.data.ETD_max_H = predictTime[2];
module.exports.data.ETD_max_M = predictTime[3];
var busIndex = module.exports.data.findIndex(function(e, idx){ return e.BusNum == Bus[index].BusNum;});
console.log(busIndex);
module.exports.data[busIndex]["remainTime"] = gapHour + "시간" + gapMin + "분 이상";
module.exports.data[busIndex]["ETD_min_H"] = predictTime[0];
module.exports.data[busIndex]["ETD_min_M"] = predictTime[1];
module.exports.data[busIndex]["ETD_max_H"] = predictTime[2];
module.exports.data[busIndex]["ETD_max_M"] = predictTime[3];
}
})
})
......@@ -107,4 +121,4 @@ function predict(){
function start(){
setInterval(predict, 60000);
}
setTimeout(start, 20000);
setTimeout(start, 10000);
......
......@@ -13,8 +13,9 @@ exports.mainView = function(req, res) {
}//main 화면
exports.timeTable = function(req, res) {
res.render("ejstest.ejs",{busNum:req.query.busNum, remainTime: Businfo.data.remainTime, ETD_min_H: Businfo.data.ETD_min_H,
ETD_min_m: Businfo.data.ETD_max_m, ETD_max_H: Businfo.data.ETD_max_H, ETD_max_m: Businfo.data.ETD_max_m });
var index = Businfo.data.findIndex(function(e, idx){ return e.BusNum == req.query.busNum;});
res.render("timetable.ejs",{busNum:req.query.busNum, remainTime: Businfo.data[index].remainTime, ETD_min_H: Businfo.data[index].ETD_min_H,
ETD_min_m: Businfo.data[index].ETD_min_M, ETD_max_H: Businfo.data[index].ETD_max_H, ETD_max_m: Businfo.data[index].ETD_max_M });
} //timeTable 화면 ejs 변수 렌더링
......