altosui: Make flight log downloading handle 'Connecting...' dialog
[fw/altos] / altosui / AltosDataPointReader.java
1
2 // Copyright (c) 2010 Anthony Towns
3 // GPL v2 or later
4
5 package altosui;
6
7 import java.io.IOException;
8 import java.text.ParseException;
9 import java.lang.UnsupportedOperationException;
10 import java.util.NoSuchElementException;
11 import java.util.Iterator;
12
13 class AltosDataPointReader implements Iterable<AltosDataPoint> {
14     Iterator<AltosRecord> iter;
15     AltosState state;
16     AltosRecord record;
17
18     final static int MISSING = AltosRecord.MISSING;
19
20     public AltosDataPointReader(Iterable<AltosRecord> reader) {
21         this.iter = reader.iterator();
22         this.state = null;
23     }
24
25     private void read_next_record() 
26         throws NoSuchElementException
27     {
28         record = iter.next();
29         state = new AltosState(record, state);
30     }
31
32     private AltosDataPoint current_dp() {
33         assert this.record != null;
34         
35         return new AltosDataPoint() {
36             public int version() { return record.version; }
37             public int serial() { return record.serial; }
38             public int flight() { return record.flight; }
39             public String callsign() { return record.callsign; }
40             public double time() { return record.time; }
41             public double rssi() { return record.rssi; }
42
43             public int state() { return record.state; }
44             public String state_name() { return record.state(); }
45
46             public double acceleration() { return record.acceleration(); }
47             public double pressure() { return record.raw_pressure(); }
48             public double altitude() { return record.raw_altitude(); }
49             public double height() { return record.raw_height(); }
50             public double accel_speed() { return record.accel_speed(); }
51             public double baro_speed() { return state.baro_speed; }
52             public double temperature() { return record.temperature(); }
53             public double battery_voltage() { return record.battery_voltage(); }
54             public double drogue_voltage() { return record.drogue_voltage(); }
55             public double main_voltage() { return record.main_voltage(); }
56         };
57     }
58
59     public Iterator<AltosDataPoint> iterator() {
60         return new Iterator<AltosDataPoint>() {
61             public void remove() { 
62                 throw new UnsupportedOperationException(); 
63             }
64             public boolean hasNext() {
65                 return iter.hasNext();
66             }
67             public AltosDataPoint next() {
68                 read_next_record();
69                 return current_dp();
70             }
71         };
72     }
73 }
74