Dashticz - Module - Public Transport

Dashticz, alternative dashboard based on HTML, CSS, jQuery

Moderators: htilburgs, robgeerts

User avatar
robgeerts
Posts: 1140
Joined: Saturday 24 January 2015 23:12
Target OS: NAS (Synology & others)
Domoticz version: 3.7067
Location: Son, NL
Contact:

Re: Dashticz - Module - Public Transport

Post by robgeerts » Friday 11 August 2017 14:52

Could you send me your config (the transport part) so I can test with it later?
Creator of Dashticz, Do you appreciate my work and want to buy me a beer? You can send Bitcoins to 17Qy4bsLM9J238fCZt5kaRc2bD5S1Aw6og or donate via PayPal: https://www.paypal.me/robgeerts

User avatar
Luxtux
Posts: 13
Joined: Monday 14 August 2017 15:16
Target OS: Linux
Domoticz version: 3.8153
Location: Luxembourg
Contact:

Re: Dashticz - Module - Public Transport

Post by Luxtux » Tuesday 15 August 2017 15:25

I've added the real-time public transport of Luxembourg to the public transport script. It should show the time a bus or train arrives at a public transport stop in the entire country. just use the name of your stop you normally use on mobiliteit.lu

Code: Select all

var publictransport = {}
publictransport.busstop = { show_via: false, station: 'Luxembourg/Centre, Royal', provider: 'mobiliteit',title:'Royal Stop',show_lastupdate:true, icon: 'bus', width:6,  results: 4}

Code: Select all

function loadPublicTransport(random,transportobject,key){
        var width = 12;
        if(typeof(transportobject.width)!=='undefined') width=transportobject.width;
        var html='<div data-id="publictransport.'+key+'" class="col-xs-'+width+'" style="padding-left:0px !important;padding-right:0px !important;">';
        if(typeof(transportobject.title)!=='undefined') html+='<div class="col-xs-12 mh titlegroups transbg"><h3>'+transportobject.title+'</h3></div>';

        html+='<div class="publictransport publictransport'+random+' col-xs-12 transbg">';
                if(typeof(transportobject.icon)!=='undefined' && transportobject.icon!==''){
                        if(transportobject.icon.substr(0,2)!=='fa') transportobject.icon = 'fa-'+transportobject.icon;
                        html+='<div class="col-xs-2 col-icon">';
                                html+='<em class="fa '+transportobject.icon+'"></em>';
                        html+='</div>';
                        html+='<div class="col-xs-10 col-data">';
                                html+='<span class="state"></span>';
                        html+='</div>';
                }
                else {
                                html+='<div class="col-xs-12 col-data">';
                                        html+='<span class="state"></span>';
                                html+='</div>';
                }
                html+='</div>';

        html+='</div>';

        //Get data every interval and call function to create block
        var interval = 60;
        if(typeof(transportobject.interval)!=='undefined') interval = transportobject.interval;
        getData(random,transportobject);
        setTimeout(function(){$('.publictransport'+random+' .state').html(language.misc.loading);},100);

        if(transportobject.provider.toLowerCase() == 'ns'){
                if(parseFloat(interval)<60) interval=60; // limit request because of limitations in NS api for my private key ;)
        }

        setInterval(function(){
                getData(random,transportobject)
        },(interval * 1000));
        return html;
}


function getData(random,transportobject){
        var provider = transportobject.provider.toLowerCase();
        if(provider == 'vvs'){
                dataURL = 'https://efa-api.asw.io/api/v1/station/'+transportobject.station+'/departures/';
        }
        else if(provider == 'mobiliteit'){
                dataURL = 'https://cors-anywhere.herokuapp.com/http://travelplanner.mobiliteit.lu/restproxy/departureBoard?accessId=cdt&format=json&[email protected]$
        }
        else if(provider == '9292' || provider == '9292-train' || provider == '9292-bus' || provider == '9292-metro' || provider == '9292-tram-bus'){
                dataURL = 'https://cors-anywhere.herokuapp.com/http://api.9292.nl/0.1/locations/'+transportobject.station+'/departure-times?lang=nl-NL&time=$
        }

        $.getJSON(dataURL,function(data){
                dataPublicTransport(random,data,transportobject);
        });

}

function dataPublicTransport(random,data,transportobject){
        var provider = transportobject.provider.toLowerCase();
        var dataPart = {}
        var i = 0;
        for(d in data){
                if(provider == '9292' || provider == '9292-train' || provider == '9292-bus' || provider =='9292-metro' || provider == '9292-tram-bus'){
                        for(t in data[d]){
                                if(provider == '9292' ||
                                   (data[d][t]['id']=='bus' && provider == '9292-bus') ||
                                   (data[d][t]['id']=='metro' && provider == '9292-metro') ||
                                   (data[d][t]['id']=='tram-bus' && provider == '9292-tram-bus') ||
                                   (data[d][t]['id']=='trein' && provider == '9292-train')
                                ){
                                        deps = data[d][t]['departures'];
                                        for(de in deps){
                                                key = deps[de]['time'];
                                                if(typeof(dataPart[key])=='undefined') dataPart[key]=[];
                                                dataPart[key][i]='';
                                                dataPart[key][i]+='<div><b>'+deps[de]['time']+'</b> ';
                                                if(deps[de]['realtimeText']!=null) dataPart[key][i]+='<span id="latetrain">'+deps[de]['realtimeText'].replac$
                                                if(deps[de]['platform']!=null) dataPart[key][i]+='- Spoor '+deps[de]['platform'];
                                                else dataPart[key][i]+='- Lijn '+deps[de]['service'];

                                                dest = deps[de]['destinationName'].split(' via ');
                                                dataPart[key][i]+=' - '+dest[0];
                                                if(typeof(transportobject.show_via)=='undefined' || transportobject.show_via==true){
                                                        if(typeof(dest[1])!=='undefined') dataPart[key][i]+=' via '+dest[1];
                                                        else if(typeof(deps[de]['RouteTekst'])!=='undefined') dataPart[key][i]+=' via '+deps[de]['viaNames'];
                                                }
                                                dataPart[key][i]+=' </div>';
                                                i++;
                                        }
                                }
                        }
                }
				                else if(provider == 'mobiliteit') {
                        for(t in data[d]) {
                                if(data[d][t]['time']==null){
                                        continue;
                                }
                                key = data[d][t]['time'];
                                if(typeof(dataPart[key])=='undefined') dataPart[key]=[];
                                BusTime = data[d][t]['time'].slice(0,-3);
                                dataPart[key][i]='';
                                dataPart[key][i]+='<div><b>'+ BusTime +'</b> ';
                                dataPart[key][i]+=' - '+data[d][t]['name']+' - ';
                                //console.log(data[d][t]['time']+ ' - ' +data[d][t]['name']+ ' - ' +data[d][t]['direction'] );
                                dest = data[d][t]['direction'].split(' via ');
                                dataPart[key][i]+=dest[0];
                                if(typeof(transportobject.show_via)=='undefined' || transportobject.show_via==true){
                                        if(typeof(dest[1])!=='undefined') dataPart[key][i]+=' via '+dest[1];
                                }

                                dataPart[key][i]+=' </div>';
                        }
                }

                else if(provider == 'vvs'){
                        arrivalTime = data[d]['departureTime']['year']+':'+data[d]['departureTime']['month']+':'+data[d]['departureTime']['day']+':'+addZero$
                        arrivalTimeShow = addZero(data[d]['departureTime']['hour'])+':'+addZero(data[d]['departureTime']['minute']);
                        if(typeof(dataPart[arrivalTime])=='undefined') dataPart[arrivalTime]=[];
                        dataPart[arrivalTime][i] = '';
                        arrivalTimeScheduled = addMinutes(arrivalTimeShow, data[d]['delay']*-1);
                        dataPart[arrivalTime][i]+='<div><b>'+arrivalTimeShow+'</b> ';
                        if(data[d]['delay'] == 0) latecolor='notlatetrain';
                        if(data[d]['delay'] > 0) latecolor='latetrain';
                        dataPart[arrivalTime][i]+='<span id="'+latecolor+'">+'+data[d]['delay']+' Min.</span> ';
                        dataPart[arrivalTime][i]+='<span id="departureScheduled">('+language.misc.scheduled+': '+arrivalTimeScheduled+')</span> ';
                        dataPart[arrivalTime][i]+='- '+data[d]['number']+' ';

                        dest = data[d]['direction'].split(' via ');
                        dataPart[arrivalTime][i]+=dest[0];
                        if(typeof(transportobject.show_via)=='undefined' || transportobject.show_via==true){
                                if(typeof(dest[1])!=='undefined') dataPart[arrivalTime][i]+=' via '+dest[1];
                        }

                        dataPart[arrivalTime][i]+='</div>';
                        i++;
                }
        }

        $('.publictransport'+random+' .state').html('');
        var c = 1;
        Object.keys(dataPart).forEach(function(d) {
        //Object.keys(dataPart).sort().forEach(function(d) {
                for(p in dataPart[d]){
                        if(c<=transportobject.results) $('.publictransport'+random+' .state').append(dataPart[d][p]);
                        c++;
                }
        });

        if(typeof(transportobject.show_lastupdate)!=='undefined' && transportobject.show_lastupdate==true){
                var dt = new Date();
                $('.publictransport'+random+' .state').append('<em>'+language.misc.last_update+': '+addZero(dt.getHours()) + ":"+addZero(dt.getMinutes())+":$
        }
}

function addZero(input){
        if(input < 10){
                return '0' + input;
        }
        else{
                return input;
        }
}
function addMinutes(time/*"hh:mm"*/, minsToAdd/*"N"*/) {
  function z(n){
    return (n<10? '0':'') + n;
  }
  var bits = time.split(':');
  var mins = bits[0]*60 + (+bits[1]) + (+minsToAdd);

  return z(mins%(24*60)/60 | 0) + ':' + z(mins%60);
}


User avatar
robgeerts
Posts: 1140
Joined: Saturday 24 January 2015 23:12
Target OS: NAS (Synology & others)
Domoticz version: 3.7067
Location: Son, NL
Contact:

Re: Dashticz - Module - Public Transport

Post by robgeerts » Tuesday 15 August 2017 17:42

Although your code didnt work immediately (forgot an ' and forgot to add a variable to te script) I fixed this and added mobiliteit.lu to the latest beta!
Thanks!
Creator of Dashticz, Do you appreciate my work and want to buy me a beer? You can send Bitcoins to 17Qy4bsLM9J238fCZt5kaRc2bD5S1Aw6og or donate via PayPal: https://www.paypal.me/robgeerts

User avatar
Luxtux
Posts: 13
Joined: Monday 14 August 2017 15:16
Target OS: Linux
Domoticz version: 3.8153
Location: Luxembourg
Contact:

Re: Dashticz - Module - Public Transport

Post by Luxtux » Tuesday 29 August 2017 19:21

little update for mobiliteit. it now shows the delay if bus/train or tram is equipped with live tracking.

Code: Select all

		else if(provider == 'mobiliteit') {
			for(t in data[d]) {
				if(data[d][t]['time']==null){
					continue;
				}
				key = data[d][t]['time'];
				if(typeof(dataPart[key])=='undefined') dataPart[key]=[];
				var fullArrivalDate = data[d][t]['date'] + ' ' + data[d][t]['time'];
				var arrivalTime =  moment(fullArrivalDate);
				var delay = 'Null';
				if (data[d][t]['rtTime']) {
					var fullRealArrivalDate = data[d][t]['rtDate'] + ' ' + data[d][t]['rtTime'];
					var realArrivalTime = moment(fullRealArrivalDate);
					var delay = '+' + realArrivalTime.diff(arrivalTime, 'minutes');
				}
				dataPart[key][i]='';
				dataPart[key][i]+='<div><b>'+ arrivalTime.format('HH:mm') +'</b> ';
			
				if (delay <= 0) {
					dataPart[key][i]+='<span id="notlatetrain">'+delay+'</span> ';
				} 
				else if (delay > 0) {
					dataPart[key][i]+='<span id="latetrain">'+delay+'</span> ';
				}
				
				dataPart[key][i]+=' - '+data[d][t]['name']+' - ';
				

				dest = data[d][t]['direction'].split(' via ');
				dataPart[key][i]+=dest[0];
				if(typeof(transportobject.show_via)=='undefined' || transportobject.show_via==true){
					if(typeof(dest[1])!=='undefined') dataPart[key][i]+=' via '+dest[1];
				}

				dataPart[key][i]+=' </div>';
			}
		}

User avatar
robgeerts
Posts: 1140
Joined: Saturday 24 January 2015 23:12
Target OS: NAS (Synology & others)
Domoticz version: 3.7067
Location: Son, NL
Contact:

Re: Dashticz - Module - Public Transport

Post by robgeerts » Tuesday 29 August 2017 20:05

Added to latest beta! Thanks!
Creator of Dashticz, Do you appreciate my work and want to buy me a beer? You can send Bitcoins to 17Qy4bsLM9J238fCZt5kaRc2bD5S1Aw6og or donate via PayPal: https://www.paypal.me/robgeerts

Zoekm
Posts: 19
Joined: Friday 18 November 2016 12:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Dashticz - Module - Public Transport

Post by Zoekm » Sunday 03 December 2017 23:23

freakshock wrote:
Tuesday 16 May 2017 21:06
Idea for a configurable feature for this module:
How about making it possible to exclude results between the current time and the first departure time listed?

For instance: I have added my local trainstation near my house. It takes at least 15 minutes from my house to get there. It would be nice to be able to exclude results that show the trains leaving from the current time +15 minutes.

If I don't have this feature I have to make my list really long :lol:
Is this an option already? I would love to have the option to select an offset of for example 30 min. So I will only see trains departing 30 min or later from now. At the moment I only see trains that I will never be able to make, as I need 30 min to get to the station and the list of departures in the next 30 min is too long for my screen....

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests