3e96f3b8e054ef993d6ab20d46a3bdabc11ce300
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_10;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 class AltosIdler {
26         String  prefix;
27         int[]   idlers;
28
29         static final int        idle_gps = 0;
30         static final int        idle_imu = 1;
31         static final int        idle_mag = 2;
32         static final int        idle_ms5607 = 3;
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_tmini = 14;
41         static final int        idle_sensor_tgps = 15;
42
43         public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {
44                 for (int idler : idlers) {
45                         AltosIdle idle = null;
46                         switch (idler) {
47                         case idle_gps:
48                                 AltosGPS.update_state(state, link, config_data);
49                                 break;
50                         case idle_imu:
51                                 AltosIMU.update_state(state, link, config_data);
52                                 break;
53                         case idle_mag:
54                                 AltosMag.update_state(state, link, config_data);
55                                 break;
56                         case idle_ms5607:
57                                 AltosMs5607.update_state(state, link, config_data);
58                                 break;
59                         case idle_mma655x:
60                                 AltosMma655x.update_state(state, link, config_data);
61                                 break;
62                         case idle_sensor_tm:
63                                 AltosSensorTM.update_state(state, link, config_data);
64                                 break;
65                         case idle_sensor_metrum:
66                                 AltosSensorMetrum.update_state(state, link, config_data);
67                                 break;
68                         case idle_sensor_mega:
69                                 AltosSensorMega.update_state(state, link, config_data);
70                                 break;
71                         case idle_sensor_emini:
72                                 AltosSensorEMini.update_state(state, link, config_data);
73                                 break;
74                         case idle_sensor_tmini:
75                                 AltosSensorTMini.update_state(state, link, config_data);
76                                 break;
77                         case idle_sensor_tgps:
78                                 AltosSensorTGPS.update_state(state, link, config_data);
79                                 break;
80                         }
81                         if (idle != null)
82                                 idle.update_state(state);
83                 }
84         }
85
86         public boolean matches(AltosConfigData config_data) {
87                 return config_data.product.startsWith(prefix);
88         }
89
90         public AltosIdler(String prefix, int ... idlers) {
91                 this.prefix = prefix;
92                 this.idlers = idlers;
93         }
94 }
95
96
97 public class AltosIdleFetch implements AltosStateUpdate {
98
99         static final AltosIdler[] idlers = {
100
101                 new AltosIdler("EasyMini",
102                                AltosIdler.idle_ms5607,
103                                AltosIdler.idle_sensor_emini),
104
105                 new AltosIdler("TeleMini-v1",
106                                AltosIdler.idle_sensor_tm),
107
108                 new AltosIdler("TeleMini-v2",
109                                AltosIdler.idle_ms5607,
110                                AltosIdler.idle_sensor_tmini),
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_ms5607, AltosIdler.idle_mma655x,
119                                AltosIdler.idle_sensor_metrum),
120
121                 new AltosIdler("TeleMega",
122                                AltosIdler.idle_gps,
123                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
124                                AltosIdler.idle_imu, AltosIdler.idle_mag,
125                                AltosIdler.idle_sensor_mega),
126                 new AltosIdler("EasyMega",
127                                AltosIdler.idle_ms5607, 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         double                  frequency;
138         String                  callsign;
139
140         public void update_state(AltosState state) throws InterruptedException {
141                 try {
142                         /* Fetch config data from remote */
143                         AltosConfigData config_data = new AltosConfigData(link);
144                         state.set_state(AltosLib.ao_flight_stateless);
145                         state.set_serial(config_data.serial);
146                         state.set_callsign(config_data.callsign);
147                         state.set_ground_accel(config_data.accel_cal_plus);
148                         state.set_accel_g(config_data.accel_cal_plus, config_data.accel_cal_minus);
149                         state.set_product(config_data.product);
150                         for (AltosIdler idler : idlers) {
151                                 if (idler.matches(config_data)) {
152                                         idler.update_state(state, link, config_data);
153                                         break;
154                                 }
155                         }
156                         state.set_received_time(System.currentTimeMillis());
157                 } catch (TimeoutException te) {
158                 }
159
160         }
161
162         public AltosIdleFetch(AltosLink link) {
163                 this.link = link;
164         }
165 }