Ady Wicaksono Daily Activities

Archive for February 26th, 2008

When will your SBS Transit Bus arrive, check on your mobile phone?

with 3 comments

Previously I create an Iris next bus application using perl, now I create a J2ME application so you can now access Iris next bus using your mobile phone, everywhere, everytime, no matter what operator you use as long as you have access to GPRS network.

Why? Because I need it and I want to make it cheap service. How it works? Simply see the application on my Nokia N73.

1. Step 1, input your bus number and bus stop number
fescr000.jpg

2. Get the information

fescr001.jpg

fescr002.jpg

So, here is the J2ME source code, it’s very simple, dummy code, no such optimization, the I/O network is blocking, but it’s all that I need.


// Simple IRIS Next Bus Application
// version 0.1 2/26/2008
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class irisnextbus extends MIDlet  implements CommandListener{

     private Display display;
     private Command openCmd =
	   new Command("Check Sched.",Command.OK,1);
     private Command exitCmd =
	   new Command("Exit",Command.EXIT,2);
     private Command backCmd =
	   new Command("Back",Command.EXIT,2);

     private Form f, f2;
     private TextField t1, t2;
     private Alert a1;

     public  irisnextbus(){}

     public void startApp(){
        display    = Display.getDisplay(this);
		f          = new Form("IRIS next bus");
        t1         = new TextField("Bus Number","",4,TextField.NUMERIC);
        t2         = new TextField("Bus Stop Number","",5,TextField.NUMERIC);
		f.append(t1);
		f.append(t2);

		f.addCommand(openCmd);
		f.addCommand(exitCmd);
		f.setCommandListener(this);
		display.setCurrent(f);
     }

     public void checkSched(){
	     openSched();
     }

     public void openSched(){
     	 HttpConnection c = null;
         InputStream is   = null;
         String url2open;
		 int responsecode;
		 int ch;

		 if(t1.getString().trim().equals("") || t2.getString().trim().equals("")){

		 }else{
			 StringBuffer info = new StringBuffer("");
	     	try{
	           url2open = "http://202.147.193.229/iris/next.cgi?b1=" +
				         t1.getString().trim() + "&b2=" + t2.getString().trim();
	     	   c  = (HttpConnection)Connector.open(url2open.trim());
			   c.setRequestMethod(HttpConnection.GET);
			   is = c.openInputStream();
		       responsecode = c.getResponseCode();
			   String str   = null;
		   	   if(responsecode == 200){
					int length = (int) c.getLength();
					if (length != -1){
						byte incomingData[] = new byte[length];
						is.read(incomingData);
						str = new String(incomingData);
					}else{
						ByteArrayOutputStream bytestream =
							  new ByteArrayOutputStream();

						while ((ch = is.read()) != -1)
						{
						  bytestream.write(ch);
						}
						str = new String(bytestream.toByteArray());
						bytestream.close();
					}
					display.setCurrent(new Alert("Info",
					str,null,AlertType.INFO),f);
		       }
	     	}catch(IOException e){

	     	}finally{
				try{
				   if (is != null){
						 is.close();
				   }
				   if (c != null){
						 c.close();
				   }	

				}catch(Exception e){}
			}
	     }
     }  	 

	 public void exitApp(){
        destroyApp(true);
     }

     public void commandAction(Command c, Displayable d){
	    String lbl = c.getLabel();
		if(lbl.equals("Exit")){
		   exitApp();
		}else if(lbl.equals("Check Sched.")){
		   checkSched();
		}else if(lbl.equals("Back")){
			display.setCurrent(f);
		}
     }

	 public void pauseApp() {
	 }

	 public void destroyApp(boolean unconditional){
	    notifyDestroyed();
     }
}
Advertisements

Written by adywicaksono

February 26, 2008 at 6:13 pm

Posted in j2me

When will your SBS Transit Bus arrive?

leave a comment »

Iris next bus is provided by SBS Transit (www.sbstransit.com.sg) for us here at Singapore to check the estimated time of SBS bus arrival. It’s very important for us, especially if we don’t want to wait for the bus too long 😦

This service is accessible by internet freely, simply put bus number and bus stop we can get the
information.

Just now I create a simple perl script to access this service
Here an example session of my script in my linux box

$ perl sbs.pl
Enter Bus Number           : 91
Enter Bus Stop Number      : 18069
Bus Stop Name              : AYER RAJAH CRES - OPP BLK 71
Next Bus Arriving in       : 15 minutes
Subsequent Bus Arriving in : 32 minutes

Here the similar screen capture from web


iris-next-bus1.jpg

The HTTP transaction to get the next bus information from Iris Next Bus system is like this:

Step 1. From Browser (Firefox) we send this HTTP request
========================================================
POST http://www.sbstransit.com.sg/index.aspx HTTP/1.1
Host: www.sbstransit.com.sg
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Cookie: rl-sticky-key=c0a8089c; ASP.NET_SessionId=1qtfoortt4l2id45wgafpbzz
Content-Type: application/x-www-form-urlencoded
Content-Length: 197

__VIEWSTATE=dDwyNjcxMTQzMTA7O2w8aGVhZGVyMDppbWdidG5TZWFyY2g7Sm91cm5leXBsYW46aW1nR287aVJJUzpidG5nbzs%2BPg%3D%3D
&keyword=&iRIS%3Atxtsvcno=91&iRIS%3Atxtbusstop=18069&iRIS%3Abtngo.x=28&iRIS%3Abtngo.y=8

Step 2. SBSTransit.com.sg respond with this HTTP response
=========================================================
HTTP/1.0 302 Moved Temporarily
Date: Tue, 26 Feb 2008 02:46:52 GMT
Location: /iris3/myirisnextbus.aspx?svcno=91&stopcode=18069
Server: Concealed by Juniper Networks DX
Via: 1.1 dx2 (Juniper Networks Application Acceleration Platform - DX 5.2.6 0)
Set-Cookie: rl-sticky-key=c0a8089c; path=/;

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href='/iris3/myirisnextbus.aspx?svcno=91&amp;stopcode=18069'>here</a>.</h2>
</body></html>

Step 3. From Browser (Firefox) we send this HTTP request
=========================================================
GET http://www.sbstransit.com.sg/iris3/myirisnextbus.aspx?svcno=91&stopcode=18069 HTTP/1.1
Host: www.sbstransit.com.sg
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Referer: http://www.sbstransit.com.sg/
Cookie: rl-sticky-key=c0a8089c; ASP.NET_SessionId=1qtfoortt4l2id45wgafpbzz

Step 4. SBSTransit.com.sg respond with this HTTP response
=========================================================
HTTP/1.0 200 OK
Date: Tue, 26 Feb 2008 02:46:53 GMT
Server: Concealed by Juniper Networks DX
Content-Encoding: deflate
Warning: 214  "Juniper Networks DX Active"
Vary: Accept-Encoding, User-Agent
Via: 1.1 dx2 (Juniper Networks Application Acceleration Platform - DX 5.2.6 0)
Set-Cookie: rl-sticky-key=c0a8089c; path=/;

[Web content in gzip-deflate format]

base on the HTTP transaction above, we can create a perl code like this

#!/usr/bin/perl
# Iris Next Bus
# Usage:
#  perl sbs.pl

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Status;

do{
	print "Enter Bus Number: ";
	chop($bus_number = <STDIN>);
	if($bus_number !~ /^[0-9]+[A-Z}*[a-z]*$/){
		$bus_number = '';
	}
}while($bus_number == '');
do{
	print "Enter Bus Stop Number: ";
	chop($bus_stop_number = <STDIN>);
	if($bus_stop_number !~ /^[0-9]+$/){
		$bus_stop_number = '';
	}
}while($bus_stop_number == '');

$DEBUG  = 0;
$cookie = "rl-sticky-key=c0a8089c; ASP.NET_SessionId=1qtfoortt4l2id45wgafpbzz";
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows) Gecko/20080201 Firefox/2.0.0.12");

# Create Request
$req = HTTP::Request->new(POST => "http://www.sbstransit.com.sg/index.aspx");
$req->content_type('application/x-www-form-urlencoded');
$req->header("Cookie" => "$cookie");
$cnt  = '__VIEWSTATE=dDwyNjcxMTQzMTA7O2w8aGVhZGVyMDppbWdidG5TZWFyY2g7Sm91';
$cnt .= 'cm5leXBsYW46aW1nR287aVJJUzpidG5nbzs%2BPg%3D%3D&keyword=&iRIS%3Atxtsvcno=';
$cnt .= $bus_number;
$cnt .= '&iRIS%3Atxtbusstop=';
$cnt .= $bus_stop_number;
$cnt .= '&iRIS%3Abtngo.x=28&iRIS%3Abtngo.y=8';
$req->content($cnt);
if($DEBUG){
	print $req->as_string."n";
}

# Send Request
my $res = $ua->request($req);
my $rc  = $res->code;
if($DEBUG){
	print $res->content;
}
if (is_redirect($rc)){
	$referral_uri = $res->header('Location');
	# Create Request
	$req          = HTTP::Request->new(GET => 
	                                 "http://www.sbstransit.com.sg/".$referral_uri);
	$req->header("Cookie" => "$cookie");
	$req->header("Referer" => "http://www.sbstransit.com.sg/");
	if($DEBUG){
		print $req->as_string."n";
	}
	# Send Request
	$res = $ua->request($req);
	$cnt = $res->content;
	# Find Bus Stop Info
	$cnt   =~ s/^.*id="lblroadesc"[^>]*>//gs;
	$binfo = $cnt;
	$binfo =~ s/</span.*$//gs;
	$cnt   =~ s/^.*id="bus1"[^>]*>//gs;
	$arrive= $cnt;
	$arrive=~ s/</span.*$//gs;
	$cnt   =~ s/^.*id="bus2"[^>]*>//gs;
	$next  = $cnt;
	$next  =~ s/</span.*$//gs;
	print "Bus Stop Name              : $binfon";
	print "Next Bus Arriving in       : $arriven";
	print "Subsequent Bus Arriving in : $nextn";
	if($DEBUG){
		#print $cnt;
	}
}

Written by adywicaksono

February 26, 2008 at 8:33 am

Posted in life, Linux, perl