altos: Support staging by going back to boost as needed
[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     boolean has_gps;
18     boolean has_accel;
19     boolean has_ignite;
20
21     final static int MISSING = AltosRecord.MISSING;
22
23     public AltosDataPointReader(AltosRecordIterable reader) {
24         this.iter = reader.iterator();
25         this.state = null;
26         has_accel = reader.has_accel();
27         has_gps = reader.has_gps();
28         has_ignite = reader.has_ignite();
29     }
30
31     private void read_next_record() 
32         throws NoSuchElementException
33     {
34         record = iter.next();
35         state = new AltosState(record, state);
36     }
37
38     private AltosDataPoint current_dp() {
39         assert this.record != null;
40         
41         return new AltosDataPoint() {
42             public int version() { return record.version; }
43             public int serial() { return record.serial; }
44             public int flight() { return record.flight; }
45             public String callsign() { return record.callsign; }
46             public double time() { return record.time; }
47             public double rssi() { return record.rssi; }
48
49             public int state() { return record.state; }
50             public String state_name() { return record.state(); }
51
52             public double acceleration() { return record.acceleration(); }
53             public double pressure() { return record.raw_pressure(); }
54             public double altitude() { return record.raw_altitude(); }
55             public double height() { return record.raw_height(); }
56             public double accel_speed() { return record.accel_speed(); }
57             public double baro_speed() { return state.baro_speed; }
58             public double temperature() { return record.temperature(); }
59             public double battery_voltage() { return record.battery_voltage(); }
60             public double drogue_voltage() { return record.drogue_voltage(); }
61             public double main_voltage() { return record.main_voltage(); }
62             public boolean has_accel() { return has_accel; }
63         };
64     }
65
66     public Iterator<AltosDataPoint> iterator() {
67         return new Iterator<AltosDataPoint>() {
68             public void remove() { 
69                 throw new UnsupportedOperationException(); 
70             }
71             public boolean hasNext() {
72                 if (record != null && record.state == Altos.ao_flight_landed)
73                     return false;
74                 return iter.hasNext();
75             }
76             public AltosDataPoint next() {
77                 do {
78                     read_next_record();
79                 } while (record.time < -1.0 && hasNext());
80                 return current_dp();
81             }
82         };
83     }
84 }
85