altoslib: Make sure AltosFlightSeries is filled in before use
[fw/altos] / altoslib / AltosIdleFetch.java
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import java.util.concurrent.*;
25
26 class AltosIdler {
27         String  prefix;
28         int[]   idlers;
29
30         static final int        idle_gps = 0;
31         static final int        idle_imu = 1;
32         static final int        idle_mag = 2;
33         static final int        idle_mma655x = 4;
34
35
36         static final int        idle_sensor_tm = 10;
37         static final int        idle_sensor_metrum = 11;
38         static final int        idle_sensor_mega = 12;
39         static final int        idle_sensor_emini = 13;
40         static final int        idle_sensor_tmini2 = 14;
41         static final int        idle_sensor_tgps = 15;
42         static final int        idle_sensor_tmini3 = 16;
43
44         public void provide_data(AltosDataListener listener, AltosLink link, AltosCalData cal_data) throws InterruptedException, TimeoutException, AltosUnknownProduct {
45                 for (int idler : idlers) {
46                         switch (idler) {
47                         case idle_gps:
48                                 AltosGPS.provide_data(listener, link, cal_data);
49                                 break;
50                         case idle_imu:
51                                 AltosIMU.provide_data(listener, link, cal_data);
52                                 break;
53                         case idle_mag:
54                                 AltosMag.provide_data(listener, link, cal_data);
55                                 break;
56                         case idle_mma655x:
57                                 AltosMma655x.provide_data(listener, link, cal_data);
58                                 break;
59 /*                      case idle_sensor_tm:
60                                 AltosSensorTM.provide_data(listener, link, cal_data);
61                                 break;
62                         case idle_sensor_metrum:
63                                 AltosSensorMetrum.provide_data(listener, link, cal_data);
64                                 break;
65                         case idle_sensor_mega:
66                                 AltosSensorMega.provide_data(listener, link, cal_data);
67                                 break;
68                         case idle_sensor_emini:
69                                 AltosSensorEMini.provide_data(listener, link, cal_data);
70                                 break;
71                         case idle_sensor_tmini2:
72                                 AltosSensorTMini2.provide_data(listener, link, cal_data);
73                                 break;
74                         case idle_sensor_tgps:
75                                 AltosSensorTGPS.provide_data(listener, link, cal_data);
76                                 break;
77                         case idle_sensor_tmini3:
78                                 AltosSensorTMini3.provide_data(listener, link, cal_data);
79                                 break;
80 */
81                         }
82                 }
83         }
84
85         public boolean matches(AltosConfigData config_data) {
86                 return config_data.product.startsWith(prefix);
87         }
88
89         public AltosIdler(String prefix, int ... idlers) {
90                 this.prefix = prefix;
91                 this.idlers = idlers;
92         }
93 }
94
95
96 public class AltosIdleFetch implements AltosDataProvider {
97
98         static final AltosIdler[] idlers = {
99
100                 new AltosIdler("EasyMini",
101                                AltosIdler.idle_sensor_emini),
102
103                 new AltosIdler("TeleMini-v1",
104                                AltosIdler.idle_sensor_tm),
105
106                 new AltosIdler("TeleMini-v2",
107                                AltosIdler.idle_sensor_tmini2),
108
109                 new AltosIdler("TeleMini-v3",
110                                AltosIdler.idle_sensor_tmini3),
111
112                 new AltosIdler("TeleMetrum-v1",
113                                AltosIdler.idle_gps,
114                                AltosIdler.idle_sensor_tm),
115
116                 new AltosIdler("TeleMetrum-v2",
117                                AltosIdler.idle_gps,
118                                AltosIdler.idle_mma655x,
119                                AltosIdler.idle_sensor_metrum),
120
121                 new AltosIdler("TeleMega",
122                                AltosIdler.idle_gps,
123                                AltosIdler.idle_mma655x,
124                                AltosIdler.idle_imu, AltosIdler.idle_mag,
125                                AltosIdler.idle_sensor_mega),
126                 new AltosIdler("EasyMega",
127                                AltosIdler.idle_mma655x,
128                                AltosIdler.idle_imu, AltosIdler.idle_mag,
129                                AltosIdler.idle_sensor_mega),
130                 new AltosIdler("TeleGPS",
131                                AltosIdler.idle_gps,
132                                AltosIdler.idle_sensor_tgps),
133         };
134
135         AltosLink               link;
136
137         public void provide_data(AltosDataListener listener, AltosCalData cal_data) throws InterruptedException, AltosUnknownProduct {
138                 try {
139                         boolean matched = false;
140                         /* Fetch config data from remote */
141                         AltosConfigData config_data = new AltosConfigData(link);
142                         listener.set_state(AltosLib.ao_flight_stateless);
143                         for (AltosIdler idler : idlers) {
144                                 if (idler.matches(config_data)) {
145                                         idler.provide_data(listener, link, cal_data);
146                                         matched = true;
147                                         break;
148                                 }
149                         }
150                         if (!matched)
151                                 throw new AltosUnknownProduct(config_data.product);
152                         listener.set_received_time(System.currentTimeMillis());
153                 } catch (TimeoutException te) {
154                 }
155
156         }
157
158         public AltosIdleFetch(AltosLink link) {
159                 this.link = link;
160         }
161 }