Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
15 arune 1
/*
2
 * File created by arune
3
 * at 2006-feb
4
 */
5
package Macbeth.Modules.modVagnen;
6
import java.io.BufferedReader;
7
import java.io.InputStreamReader;
8
import java.net.URL;
9
import java.net.URLConnection;
10
import java.util.*;
11
 
12
 
13
/**
14
 *
15
 *
16
 *
17
 * @author arune
18
 */
19
public class StopObject {
20
 
21
    public String stop;
22
    public String displayName;
23
    public String[] incLines;
24
    public String[] excLines;
25
    public String[] excDests;
26
 
27
    /**
28
     * Creates a new instance of StopObject.
29
     * @param .
30
     * @param .
31
     * @param .
32
     * @param .
33
     */
34
    public StopObject(String stop, String displayName, String[] incLines, String[] excLines, String[] incDests, String[] excDests) {
35
        this.stop = stop;
36
        this.displayName = displayName;
37
        this.incLines = incLines;
38
        this.excLines = excLines;
39
        this.excDests = excDests;
40
    }
41
 
42
    public ArrayList getStopData() {
43
        String url = "http://wap.trafikkontoret.goteborg.se/QuestionPage.aspx?__redir=1&hpl=" + stop + "&pg=1";
44
        ArrayList traficData = getWebPage(url);
45
 
46
        //filtrera och lagra som Buss-objekt
47
 
48
        //StopObject.Buss test = test1.getStopData();
49
        ArrayList stopTimeList = new ArrayList();
50
        String[] numberStopTime = {"", "", "", ""};
51
        int cnt = 0;
52
 
53
        for (int i = 0; i < traficData.size(); i++) {
54
            //_debug.println(((String)traficData.get(i)));
55
            //System.out.println((String)traficData.get(i));
56
 
57
            String row = (String)traficData.get(i);
58
            if (row.indexOf("<td><div nowrap>")==0) {
59
                if (row.indexOf("<font") == -1) {
60
 
61
                    int endTag = row.indexOf("</div>");
62
                    if (endTag > 16) {
63
                        String contents = row.substring(16, endTag);
64
                        //System.out.println(contents);
65
 
66
                        numberStopTime[cnt] = contents;
67
 
68
                        cnt++;
69
                        if (cnt == 4) {
70
                            cnt = 0;
71
                            //specialfall:
72
                            /*
73
                             * <td><div nowrap>--</div></td> bussen går inte alls den tiden (därefter-tiden)
74
                             * <td><div nowrap>19,L</div></td> något med komma
75
                             * <td><div nowrap>ca 47</div></td> ca tid (fixas i modVagnen.java)
76
                             */
77
                            int delimIndex = numberStopTime[2].indexOf(",");
78
                            if (delimIndex != -1) {
79
                                numberStopTime[2] = numberStopTime[2].substring(0, delimIndex);
80
                            }
81
                            delimIndex = numberStopTime[3].indexOf(",");
82
                            if (delimIndex != -1) {
83
                                numberStopTime[3] = numberStopTime[3].substring(0, delimIndex);
84
                            }
85
 
86
                            boolean addFirst;
87
                            boolean addSec;
88
                            if (incLines.length > 0) {
89
                                addFirst = false;
90
                                addSec = false;
91
                                for (int j = 0; j < incLines.length; j++) {
92
                                    if (incLines[j].equals(numberStopTime[0])) {
93
                                        addFirst = true;
94
                                        addSec = true;
95
                                    }
96
                                }
97
                            } else if (excLines.length > 0) {
98
                                addFirst = true;
99
                                addSec = true;
100
                                for (int j = 0; j < excLines.length; j++) {
101
                                    if (excLines[j].equals(numberStopTime[0])) {
102
                                        addFirst = false;
103
                                        addSec = false;
104
                                    }
105
                                }
106
 
107
                            } else {
108
                                addFirst = true;
109
                                addSec = true;
110
                            }
111
 
112
                            if (numberStopTime[2].indexOf("--") > -1) {
113
                                addFirst = false;
114
                            }
115
                            if (numberStopTime[3].indexOf("--") > -1) {
116
                                addSec = false;
117
                            }
118
 
119
                            for (int j = 0; j < excDests.length; j++) {
120
                                if (numberStopTime[1].toLowerCase().contains(excDests[j].toLowerCase())) {
121
                                    addFirst = false;
122
                                    addSec = false;
123
                                }
124
                            }
125
                            //System.out.println(displayName + " " + numberStopTime[0] + " " + numberStopTime[1] + " " + numberStopTime[2] + "/" + numberStopTime[3]);
126
                            //System.out.println(addFirst + "/" + addSec);
127
 
128
                            //filtrera obs gemener versaler
129
                            //kolla numberStopTime[0] mot inc/excLines
130
                            //kolla numberStopTime[1] mot inc/excDests
131
                            if (addFirst) {
132
                                stopTimeList.add(new Buss(displayName, numberStopTime[0], numberStopTime[2], numberStopTime[1]));
133
                            }
134
                            if (addSec) {
135
                                stopTimeList.add(new Buss(displayName, numberStopTime[0], numberStopTime[3], numberStopTime[1]));
136
                            }
137
                        }
138
                        //då cnt = 3:
139
                        //stopTimeList.add()
140
                        //kolla om det är en tid/stop som ska vara med
141
                    }
142
                }
143
 
144
 
145
            }
146
        }
147
 
148
        //return new ArrayList();
149
        return stopTimeList;
150
    }
151
 
152
    private ArrayList getWebPage(String url) {
153
        ArrayList rowStrings = new ArrayList();
154
 
155
        try {
156
            URL connurl = new URL(url);
157
            URLConnection connection = connurl.openConnection();
158
            //detta för att man inte ska få en fattig wap-version av sidan
159
            connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)");
160
            connection.connect();
161
 
162
            //BufferedReader page = new BufferedReader(new InputStreamReader(connurl.openStream()));
163
            BufferedReader page = new BufferedReader(new InputStreamReader(connection.getInputStream()));
164
 
165
            String curLine = page.readLine();
166
            while ( curLine != null ) {
167
                rowStrings.add(curLine);
168
                // get the next line
169
                curLine = page.readLine();
170
            }
171
        }
172
        catch ( Exception ex ){
173
            //TODO: borde skriva ut felmeddelande, kanske kasta på anropande funtion?
174
            //_errors.println("Comunication problem with server (" + url + "), exception: " + ex.getMessage());
175
        }
176
 
177
        return rowStrings;
178
    }
179
 
180
    public class Buss {
181
 
182
        public String stop;
183
        public String number;
184
        public String time;
185
        public String dest;
186
 
187
        /**
188
         * Creates a new instance of RemoteButton.
189
         */
190
        public Buss(String stop, String number, String time, String dest) {
191
            this.stop = stop;
192
            this.number = number;
193
            this.time = time;
194
            this.dest = dest;
195
        }
196
 
197
    }
198
}