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 2004-dec
4
 */
5
package Macbeth.Modules.modVagnen;
6
 
7
import java.util.*;
8
import java.net.URL;
9
import java.net.URLConnection;
10
//import java.net.HttpURLConnection;
11
import java.io.InputStreamReader;
12
import java.io.BufferedReader;
13
import javax.swing.Timer;
14
 
15
import java.awt.event.*;
16
 
17
import Macbeth.System.*;
18
import Macbeth.Utilities.*;
19
import Macbeth.XML.XMLDataHandler;
20
 
21
/**
22
 *
23
 *
24
 * @author arune
25
 */
26
public class modVagnen extends MbModule {
27
 
28
    //list of stops and their variables
29
    private ArrayList stops = new ArrayList();           //a map of StopObjects
30
 
31
    private Timer requestDataTimer = null;
32
    private int timerInterval = 0;
33
 
34
    private String menuMiddleId;
35
 
36
    //menusystem specific bool, if this submenu is activated
37
    private boolean activated;
38
 
39
    /**
40
     * Creates a new instance of modTVchart.
41
     */
42
    public modVagnen() {
43
        //construct MbModule
44
        super();
45
        //initial values
46
 
47
        //remotebuttons = new HashMap(32);
48
        stops = new ArrayList();
49
 
50
        menuMiddleId = "";
51
        activated = false;
52
        //TODO: remove next line, for debug
53
        //activated = true;
54
 
55
        addDependency("MenuSystem");
56
 
57
        //register our packet data handler
58
        setPacketDataHandler(new PacketDataHandler());
59
    }
60
 
61
    public String name() {
62
        return "Vagnen";
63
    }
64
 
65
    public String description() {
66
        return "Checks tram and bus stops";
67
    }
68
 
69
    /**
70
     * This method sets default values on all _required_ data
71
     * fields in the data repository.
72
     */
73
    public void initDataFields() {
74
        //set default values on all REQUIRED option fields before starting up subsystems
75
//        options.putField("host", "localhost");
76
//        options.putField("port", "2050");
77
        options.putField("checkinterval", "1");
78
    }
79
 
80
    public void startup() throws MbStartupException {
81
        //startup MbModule
82
        super.startup();
83
 
84
        //get all remotebuttons from the data repository
85
        DataRepository.DataList list = dataRepository.getList("stops");
86
        if (list!=null) {
87
            Iterator it = list.items();
88
            while (it.hasNext()) {
89
                DataRepository.DataListItem item = (DataRepository.DataListItem)it.next();
90
                String stop = item.getField("stop");
91
                String displayName = item.getField("displayname");
92
                String includeLines = item.getField("includelines");
93
                String excludeLines = item.getField("excludelines");
94
                String includeDests = item.getField("includedest");
95
                String excludeDests = item.getField("excludedest");
96
                //parsa och verifiera includelines, (excludelines), excludedest, (includedest)
97
                String[] incLines = null;
98
                String[] excLines = null;
99
                String[] incDests = null;
100
                String[] excDests = null;
101
                if (includeLines != null) {
102
                    incLines = includeLines.split(",");
103
                }
104
                if (excludeLines != null) {
105
                    excLines = excludeLines.split(",");
106
                }
107
                if (includeDests != null) {
108
                    incDests = includeDests.split(",");
109
                }
110
                if (excludeDests != null) {
111
                    excDests = excludeDests.split(",");
112
                }
113
 
114
                stops.add(new StopObject(stop, displayName, incLines, excLines, incDests, excDests));
115
            }
116
        }
117
 
118
        sendToMenuSys("<registersubmod displayname=\"Vagnen\" displaydesc=\"When does the bus arrive?\" visible=\"true\" />");
119
 
120
        timerInterval = Integer.parseInt(options.getField("checkinterval"));
121
        if (timerInterval > 0) {
122
            TimerListener lyssnare = new TimerListener();
123
            requestDataTimer = new Timer(1000*60 * timerInterval, lyssnare);
124
            requestDataTimer.start();
125
        }
126
 
127
    }
128
 
129
    public void shutdown() {
130
        sendToMenuSys("<unregistersubmod />");
131
        super.shutdown();
132
    }
133
 
134
    private void sendToMenuSys(String dataToSend) {
135
        MbPacket p = new MbPacket();
136
        p.setDestination(new MbLocation("self", "MenuSystem"));
137
        p.appendContents(dataToSend);
138
        sendPacket(p);
139
    }
140
 
141
    public ArrayList appendList(ArrayList appendThis, ArrayList toThis) {
142
        ArrayList copyToThis = toThis;
143
 
144
        for (int i = 0; i< appendThis.size(); i++) {
145
            copyToThis.add(appendThis.get(i));
146
        }
147
 
148
        return copyToThis;
149
    }
150
 
151
    public ArrayList sortList(ArrayList toSort) {
152
        ArrayList sorted = new ArrayList();
153
        ArrayList copyToSort = toSort;
154
        StopObject.Buss trafikData;
155
 
156
        int i = 0;
157
        while (i < copyToSort.size()) {
158
            trafikData = (StopObject.Buss)copyToSort.get(i);
159
            if (trafikData.time.equals("Nu")) {
160
                sorted.add(copyToSort.get(i));
161
                copyToSort.remove(i);
162
 
163
            } else {
164
                i++;
165
            }
166
        }
167
 
168
        i = 0;
169
        while (copyToSort.size() > 0) {
170
            int smallest = 100;
171
            int smallestInd = 0;
172
            for (int j = 0; j < copyToSort.size(); j++) {
173
                trafikData = (StopObject.Buss)copyToSort.get(j);
174
                int currentValue = 100;
175
                int spaceInd = trafikData.time.indexOf(" ");
176
                //_debug.println("|" + trafikData.time+"|");
177
                if (spaceInd == -1) {
178
                    try {
179
                        currentValue = Integer.parseInt(trafikData.time);
180
                    } catch (NumberFormatException ex) {
181
                    }
182
                } else {
183
                    if (trafikData.time.indexOf(" ", spaceInd+1) > -1) {
184
                        spaceInd = trafikData.time.indexOf(" ", spaceInd+1);
185
                    }
186
                    try {
187
                        //_debug.println("|" + trafikData.time.substring(spaceInd, trafikData.time.length())+"|");
188
                        currentValue = Integer.parseInt(trafikData.time.substring(spaceInd+1, trafikData.time.length()));
189
                    } catch (NumberFormatException ex) {
190
                    }
191
                }
192
 
193
                if (currentValue < smallest) {
194
                    smallestInd = j;
195
                    smallest = currentValue;
196
                }
197
            }
198
            //trafikData = (StopObject.Buss)copyToSort.get(smallestInd);
199
            //_debug.println("|" + trafikData.time+"|");
200
            sorted.add(copyToSort.get(smallestInd));
201
            copyToSort.remove(smallestInd);
202
        }
203
        return sorted;
204
    }
205
 
206
    private void sendMain() {
207
        ArrayList stopData = new ArrayList();
208
 
209
        for (int i = 0; i < stops.size(); i++) {
210
            StopObject stop = (StopObject)stops.get(i);
211
 
212
            ArrayList newStopData = stop.getStopData();
213
 
214
            stopData = appendList(newStopData, stopData);
215
 
216
        }
217
        stopData = sortList(stopData);
218
        //TODO: sortering!
219
        //specialfall: .equals("Nu") om contains("ca")
220
 
221
        //TODO: hållplats är inte med, det har jag glömt, det finns ju med i config
222
 
223
        String dataToSend = "<packet menuid=\"MainMenu\"><data>";
224
        dataToSend = dataToSend + "<middleid>" + menuMiddleId + "</middleid>";
225
        dataToSend = dataToSend + "<list id=\"menudata\">";
226
        for (int i = 0; i < stopData.size(); i++) {
227
            StopObject.Buss trafikData = (StopObject.Buss)stopData.get(i);
228
            String line = "Linje " + trafikData.number;
229
            String time = "";
230
            if (trafikData.time.equals("Nu")) {
231
                time = " avg&#229;r NU";
232
            } else if (trafikData.time.equals("1")) {
233
                time = " avg&#229;r om 1 minut";
234
            } else if (trafikData.time.equals("ca 1")) {
235
                time = " avg&#229;r om ca 1 minut";
236
            } else {
237
                time = " avg&#229;r om " + trafikData.time + " minuter";
238
            }
239
            String stop = " (" + trafikData.stop + ")";
240
            //String stop = " fr&#229;n " + trafikData.stop;
241
            String dest = " mot " + trafikData.dest;
242
            dataToSend = dataToSend + "<item id=\"" + i + "\" title=\"" + line + stop + time + dest + "\" desc=\"\" />";
243
 
244
            //_debug.println(test.stop + " " + test.number + " " + test.time);
245
            //System.out.println((String)traficData.get(i));
246
 
247
        }
248
        dataToSend = dataToSend + "</list>";
249
        dataToSend = dataToSend + "</data></packet>";
250
        sendToMenuSys(dataToSend);
251
        //_debug.println(dataToSend);
252
 
253
        //sortera och skapa en lista att skicka flinsh från stopData
254
 
255
 
256
 
257
    }
258
 
259
 
260
 
261
 
262
    /**
263
     * Takes care of XML-data found in incoming packets.
264
     */
265
    private class PacketDataHandler implements XMLDataHandler {
266
        public void XMLstartElement(String element, HashMap attributes) {
267
            if (element.equals("activate")) {
268
                //menusystem activates this submenu
269
                //we should send a menu to display
270
                activated = true;
271
 
272
                sendMain();
273
 
274
            } else if (element.equals("refresh")) {
275
                //menusystem wants a refreshed menu
276
                sendMain();
277
 
278
            } else if (element.equals("padLeft") && attributes.containsKey("state") && attributes.containsKey("level")) {
279
                //user pressed padleft on his remote
280
                //we should traverse back one step or deactivate
281
                //if we are in submenu root
282
                String state = (String)attributes.get("state");
283
                int level = Integer.parseInt((String)attributes.get("level"));
284
                if (level >= 1) {
285
                    if (state.equals("DOWN")) {
286
                        activated = false;
287
                        sendToMenuSys("<deactivates />");
288
                    }
289
                }
290
            } else if (element.equals("padRight") && attributes.containsKey("state") && attributes.containsKey("level")) {
291
                String state = (String)attributes.get("state");
292
                int level = Integer.parseInt((String)attributes.get("level"));
293
                //user pressed padright on his remote
294
                //we should traverse forward one step 
295
                if (level >= 1) {
296
                    if (state.equals("DOWN")) {
297
                        //do nothing
298
                    }
299
                }
300
 
301
            } else if (element.equals("padUp") && attributes.containsKey("state") && attributes.containsKey("level")) {
302
                //user pressed padright on his remote
303
                //we should traverse forward one step 
304
                String state = (String)attributes.get("state");
305
                int level = Integer.parseInt((String)attributes.get("level"));
306
 
307
                if (level >= 2) {
308
                    if (state.equals("DOWN")) {
309
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>1</scroll></command></packet>");
310
                    } else {
311
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>0</scroll></command></packet>");
312
                    }
313
                }
314
 
315
            } else if (element.equals("padDown") && attributes.containsKey("state") && attributes.containsKey("level")) {
316
                //user pressed padright on his remote
317
                //we should traverse forward one step 
318
                String state = (String)attributes.get("state");
319
                int level = Integer.parseInt((String)attributes.get("level"));
320
 
321
                if (level >= 2) {
322
                    if (state.equals("DOWN")) {
323
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>-1</scroll></command></packet>");
324
                    } else {
325
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>0</scroll></command></packet>");
326
                    }
327
                }
328
 
329
            } else if (element.equals("padOk") && attributes.containsKey("state") && attributes.containsKey("level")) {
330
                //user pressed padOk on his remote 
331
                String state = (String)attributes.get("state");
332
                int level = Integer.parseInt((String)attributes.get("level"));
333
                    //state=\"UP\"
334
                if (level >= 3) {
335
                    if (state.equals("DOWN")) {
336
 
337
                    } else if (state.equals("UP")) {
338
 
339
                    }
340
                }
341
            } else if (element.equals("middleid") && attributes.containsKey("value") && attributes.containsKey("menuid") && attributes.containsKey("userlevel")) {
342
                //menuMiddleId = (String)attributes.get("value");
343
                String middleId = (String)attributes.get("value");
344
                String menuId = (String)attributes.get("menuid");
345
                int userlevel = 0;
346
                try {
347
                    userlevel = Integer.parseInt((String)attributes.get("userlevel"));
348
                } catch (NumberFormatException ex) {
349
                    _errors.println("There was a faulty userlevel sent to me: " + (String)attributes.get("userlevel"));
350
                }
351
 
352
                if (userlevel >= 2 && menuId.equals("MainMenu")) {
353
                    menuMiddleId = middleId;
354
                }              
355
 
356
 
357
            }
358
        }
359
 
360
        public void XMLendElement(String element) {}
361
        public void XMLelementData(String data) {}
362
        public void XMLdocumentStart() {}
363
        public void XMLdocumentEnd() {}
364
    }
365
 
366
    //En lyssnare för timern
367
    /**
368
     * Timer handles datafetch from tv-server and also
369
     * sends data to display periodically
370
     */
371
    private class TimerListener implements ActionListener {
372
        public void actionPerformed(ActionEvent e) {
373
            //fetchData();
374
 
375
            //om aktiv: skicka ny data till flinsh (just nu / härnäst kan ha ändrats)
376
            if (activated) {
377
                sendMain();
378
            }
379
        }
380
    }
381
 
382
 
383
}