diff --git a/axios-get-trip/index.js b/axios-get-trip/index.js index 503645c..4fe1b7b 100644 --- a/axios-get-trip/index.js +++ b/axios-get-trip/index.js @@ -6,92 +6,120 @@ const debug=require('debug')('trips'); const URL=process.env.URL; debug('URL: '+URL) -run().catch(err => { +let dataGet=''; +let dataServices=''; + +run(dataGet,dataServices).catch(err => { debug('run: error') console.log(err) }); -async function run() { +async function handleTrip(element){ + const mapTime=new Map(); + mapTime.clear(); + + const tripId=element.trip_id; + //debug('tripId: %s',tripId); + const tripShortName=element.trip_short_name; + //debug('tripShortName: %s',tripShortName); + const serviceId=element.service_id; + //debug('serviceId: %s',serviceId); + + const queryService=`http://localhost:65534/service?serviceid=${serviceId}`; + //debug('queryService: '+queryService); + dataServices = await axios.get(queryService); + + const monday=dataServices.data[0].monday; + const tuesday=dataServices.data[0].tuesday; + //debug('tuesday: '+tuesday); + const wednesday=dataServices.data[0].wednesday; + //debug('wednesday: '+wednesday); + const thursday=dataServices.data[0].thursday; + const friday=dataServices.data[0].friday; + const saturday=dataServices.data[0].saturday; + const sunday=dataServices.data[0].sunday; + const start_date=dataServices.data[0].start_date; + const dateStart=utils.gtfsDate2NodeDate(start_date); + //debug('dateStart: '+dateStart); + const end_date=dataServices.data[0].end_date; + const dateEnd=utils.gtfsDate2NodeDate(end_date); + //debug('dateEnd: '+dateEnd); + let dateNext=new Date(dateStart); + //debug('dateNext: '+dateNext); + + while(dateNext.getTime()<=dateEnd.getTime()){ + let weekday=dateNext.getDay(); + //debug('weekday: '+weekday); + + if((weekday===utils.dateWeekday.monday && monday) || + (weekday===utils.dateWeekday.tuesday && tuesday) || + (weekday===utils.dateWeekday.wednesday && wednesday) || + (weekday===utils.dateWeekday.thursday && thursday) || + (weekday===utils.dateWeekday.friday && friday) || + (weekday===utils.dateWeekday.saturday && saturday) || + (weekday===utils.dateWeekday.sunday && sunday)){ + //update map + updateMap(dateNext.getTime(),tripId,tripShortName,mapTime); + } + dateNext=new Date(dateNext.setDate(dateNext.getDate()+1)); + //debug('dateNext: '+dateNext.getDate()); + //debug('dateNext.getTime(): '+dateNext.getTime()); + } + //debug('dateEnd: '+dateEnd); + //debug('dateNext: '+dateNext); + return mapTime; +} + +async function run(dataTrips, dataServices) { debug('run:...') //HTTP GET - let dataGet = await axios.get( - URL -//async example to process responce -// ).then(res => { -// debug('res.data: %s',res.data) -// }); - ); + dataTrips = await axios.get(URL); - debug('data received via GET'); - debug('dataGet: %s',dataGet); - debug('dataGet.data.length: %s',dataGet.data.length); - debug('dataGet.data[0].ageny_name: %s',dataGet.data[0].agency_name); - debug('dataGet.data[0].route_short_name: %s',dataGet.data[0].route_short_name); - debug('dataGet.data[0].agency_id: %s',dataGet.data[0].agency_id); - debug('dataGet.data[0].route_id: %s',dataGet.data[0].route_id); -/* - const service_id=dataGet.data[0].service_id; - debug('service_id: %s',service_id); + //TODO refactor callback function + const map=new Map(dataTrips.data.forEach(handleTrip)); + debug('map.size: '+map.size); +} - debug('dataGet.data[0].trip_id: %s',dataGet.data[0].trip_id); - - const trip_id=dataGet.data[0].trip_id; - debug('trip_id: %s',trip_id); -*/ - for(var i = 0; i < 2; i++) - { - const tripId=dataGet.data[i].trip_id; - debug('tripId: %s',tripId); - const serviceId=dataGet.data[0].service_id; - debug('serviceId: %s',serviceId); - - const queryService=`http://localhost:65534/service?serviceid=${serviceId}`; - debug('queryService: '+queryService); - let dataService = await axios.get(queryService); - - const monday=dataService.data[0].monday; - const tuesday=dataService.data[0].tuesday; - const wednesday=dataService.data[0].wednesday; - const thursday=dataService.data[0].thursday; - const friday=dataService.data[0].friday; - const saturday=dataService.data[0].saturday; - const sunday=dataService.data[0].sunday; - const start_date=dataService.data[0].start_date; - const end_date=dataService.data[0].end_date; - const mapTime=new Map(); - const timeStart=utils.gtfsDate2NodeDate(start_date).getTime(); - debug('timeStart: '+timeStart); - debug('mapTime size: '+mapTime.size); - debug('mapTime has timeStart: '+mapTime.has(timeStart)); - mapTime.set(timeStart,tripId); - debug('mapTime size: '+mapTime.size); - debug('mapTime has timeStart: '+mapTime.has(timeStart)); +function updateMap(time,tripId,tripShortName,map){ + //debug('time: '+time); + //time map empty + if(map.size===0){ + //debug('map.size: '+map.size); + //create trip map + let mapTrips=new Map(); + //add trip to trip map + addTrip(tripId,tripShortName,mapTrips); + //add time to time map + map.set(time,mapTrips); + //debug('map.size: '+map.size); + }//time map NOT empty + else{ + //debug('map.size: '+map.size); + //time not present yet + if(!map.has(time)){ + //debug('map has not: '+time); + //create trips map + let mapTrips=new Map(); + //add trip to trip map + addTrip(tripId,tripShortName,mapTrips); + //add time to time map + map.set(time,mapTrips); + //debug('map.size: '+map.size); + } + //time already present + else{ + //debug('map has: '+time); + let mapTrips=map.get(time); + //debug('mapTrips.size: '+mapTrips.size); + //TODO Does it matter to override existing trips? + //add trip to trip map + addTrip(tripId,tripShortName,mapTrips); + } } -/* - const map=new Map(); +} - debug('map size: '+map.size); - debug('map has start_date: '+map.has(start_date)); - map.set(start_date,trip_id); - debug('map size: '+map.size); - debug('map has start_date: '+map.has(start_date)); - - debug('map has end_date: '+map.has(end_date)); - map.set(end_date,trip_id); - debug('map size: '+map.size); - debug('map has end_date: '+map.has(end_date)); - - const mapDate=new Map(); - const nodeDateStart=utils.gtfsDate2NodeDate(start_date); - debug('nodeDateStart: '+nodeDateStart); - - debug('mapDate size: '+mapDate.size); - debug('mapDate has nodeDateStart: '+mapDate.has(nodeDateStart)); - mapDate.set(nodeDateStart,trip_id); - debug('mapDate size: '+mapDate.size); - debug('mapDate has nodeDateStart: '+mapDate.has(nodeDateStart)); - const nodeDateStartCopy=new Date(nodeDateStart); - debug('mapDate has nodeDateStartCopy: '+mapDate.has(nodeDateStartCopy)); -*/ -} +function addTrip(tripId, tripShortName, map){ + map.set(tripId,tripShortName); + //debug('map.size: '+map.size); +} diff --git a/axios-get-trip/utils.js b/axios-get-trip/utils.js index 222b239..341196e 100644 --- a/axios-get-trip/utils.js +++ b/axios-get-trip/utils.js @@ -2,7 +2,7 @@ const debug=require('debug')('utils'); function findSubStr(str, start, end) { var index = str.slice(start, end); - debug('index: '+index); + //debug('index: '+index); return index; } @@ -10,20 +10,39 @@ function gtfsDate2NodeDate(date){ let start = 0; let end = 4; let year=findSubStr(date, start, end); - debug('year: '+year); + //debug('year: '+year); start=4; end=6; let month=findSubStr(date, start, end); - debug('month: '+month); + //debug('month: '+month); start=6; end=8; let day=findSubStr(date, start, end); - debug('day: '+day); + //debug('day: '+day); let nodeDate = new Date(year, month - 1, day); - debug('nodeDate: '+nodeDate.toDateString()); + //debug('nodeDate: '+nodeDate); return nodeDate; } +const dateWeekday={ + sunday:0, + monday:1, + tuesday:2, + wednesday:3, + thursday:4, + friday:5, + saturday:6 +} + +function nextDay(epoch){ + debug('epoch: '+epoch); + let epochNext=epoch + (24 * 60 * 60 * 1000) // 1 day in millisecond + debug('epochNext: '+epochNext); + return epochNext; +} + module.exports={ - gtfsDate2NodeDate + gtfsDate2NodeDate, + nextDay, + dateWeekday };