altoslib: Add debugging to AltosPreferences.state() etc
[fw/altos] / altoslib / AltosPreferences.java
1 /*
2  * Copyright © 2010 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_9;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosPreferences {
25         public static AltosPreferencesBackend backend = null;
26
27         /* logdir preference name */
28         public final static String logdirPreference = "LOGDIR";
29
30         /* channel preference name */
31         public final static String channelPreferenceFormat = "CHANNEL-%d";
32
33         /* frequency preference name */
34         public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
35
36         /* telemetry format preference name */
37         public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
38
39         /* telemetry rate format preference name */
40         public final static String telemetryRatePreferenceFormat = "RATE-%d";
41
42         /* log file format preference name */
43         public final static String logfilePreferenceFormat = "LOGFILE-%d";
44
45         /* state preference name */
46         public final static String statePreferenceHead = "STATE-";
47         public final static String statePreferenceFormat = "STATE-%d";
48         public final static String statePreferenceLatest = "STATE-LATEST";
49
50         /* voice preference name */
51         public final static String voicePreference = "VOICE";
52
53         /* callsign preference name */
54         public final static String callsignPreference = "CALLSIGN";
55
56         /* firmware directory preference name */
57         public final static String firmwaredirPreference = "FIRMWARE";
58
59         /* serial debug preference name */
60         public final static String serialDebugPreference = "SERIAL-DEBUG";
61
62         /* scanning telemetry preferences name */
63         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
64
65         /* scanning telemetry rate preferences name */
66         public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
67
68         /* Launcher serial preference name */
69         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
70
71         /* Launcher channel preference name */
72         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
73
74         /* Default logdir is ~/TeleMetrum */
75         public final static String logdirName = "TeleMetrum";
76
77         /* Log directory */
78         public static File logdir;
79
80         /* Last log directory - use this next time we open or save something */
81         public static File last_logdir;
82
83         /* Map directory -- hangs of logdir */
84         public static File mapdir;
85
86         /* Frequency (map serial to frequency) */
87         public static Hashtable<Integer, Double> frequencies;
88
89         /* Telemetry (map serial to telemetry format) */
90         public static Hashtable<Integer, Integer> telemetries;
91
92         /* Telemetry rate (map serial to telemetry format) */
93         public static Hashtable<Integer, Integer> telemetry_rates;
94
95         /* Log file (map serial to logfile name) */
96         public static Hashtable<Integer, File> logfiles;
97
98         /* Voice preference */
99         public static boolean voice;
100
101         /* Callsign preference */
102         public static String callsign;
103
104         /* Firmware directory */
105         public static File firmwaredir;
106
107         /* Scanning telemetry */
108         public static int scanning_telemetry;
109
110         public static int scanning_telemetry_rate;
111
112         /* List of frequencies */
113         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
114         public static AltosFrequency[] common_frequencies;
115
116         public final static String      frequency_count = "COUNT";
117         public final static String      frequency_format = "FREQUENCY-%d";
118         public final static String      description_format = "DESCRIPTION-%d";
119
120         /* Units preference */
121
122         public final static String      unitsPreference = "IMPERIAL-UNITS";
123
124         /* Maps cache size preference name */
125         final static String mapCachePreference = "MAP-CACHE";
126
127         static LinkedList<AltosMapCacheListener> map_cache_listeners;
128
129         public static int map_cache = 9;
130
131         public static AltosFrequency[] load_common_frequencies() {
132                 AltosFrequency[] frequencies = null;
133                 boolean existing = false;
134                 existing = backend.nodeExists(common_frequencies_node_name);
135
136                 if (existing) {
137                         AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
138                         int             count = node.getInt(frequency_count, 0);
139
140                         frequencies = new AltosFrequency[count];
141                         for (int i = 0; i < count; i++) {
142                                 double  frequency;
143                                 String  description;
144
145                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
146                                 description = node.getString(String.format(description_format, i), null);
147                                 frequencies[i] = new AltosFrequency(frequency, description);
148                         }
149                 } else {
150                         frequencies = new AltosFrequency[10];
151                         for (int i = 0; i < 10; i++) {
152                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
153                                                                            String.format("Channel %d", i));
154                         }
155                 }
156                 return frequencies;
157         }
158
159         public static void save_common_frequencies(AltosFrequency[] frequencies) {
160                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
161
162                 node.putInt(frequency_count, frequencies.length);
163                 for (int i = 0; i < frequencies.length; i++) {
164                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
165                         node.putString(String.format(description_format, i), frequencies[i].description);
166                 }
167         }
168         public static int launcher_serial;
169
170         public static int launcher_channel;
171
172         public static void init(AltosPreferencesBackend in_backend) {
173
174                 if (backend != null)
175                         return;
176
177                 backend = in_backend;
178
179                 /* Initialize logdir from preferences */
180                 String logdir_string = backend.getString(logdirPreference, null);
181                 if (logdir_string != null)
182                         logdir = new File(logdir_string);
183                 else {
184                         logdir = new File(backend.homeDirectory(), logdirName);
185                         if (!logdir.exists())
186                                 logdir.mkdirs();
187                 }
188                 mapdir = new File(logdir, "maps");
189                 if (!mapdir.exists())
190                         mapdir.mkdirs();
191
192                 frequencies = new Hashtable<Integer, Double>();
193
194                 telemetries = new Hashtable<Integer,Integer>();
195
196                 telemetry_rates = new Hashtable<Integer,Integer>();
197
198                 logfiles = new Hashtable<Integer,File>();
199
200                 voice = backend.getBoolean(voicePreference, true);
201
202                 callsign = backend.getString(callsignPreference,"N0CALL");
203
204                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
205
206                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
207
208                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
209
210                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
211
212                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
213                 if (firmwaredir_string != null)
214                         firmwaredir = new File(firmwaredir_string);
215                 else
216                         firmwaredir = null;
217
218                 common_frequencies = load_common_frequencies();
219
220                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
221
222                 map_cache = backend.getInt(mapCachePreference, 9);
223                 map_cache_listeners = new LinkedList<AltosMapCacheListener>();
224         }
225
226         public static void flush_preferences() {
227                 backend.flush();
228         }
229
230         public static void set_logdir(File new_logdir) {
231                 synchronized (backend) {
232                         logdir = new_logdir;
233                         mapdir = new File(logdir, "maps");
234                         if (!mapdir.exists())
235                                 mapdir.mkdirs();
236                         backend.putString(logdirPreference, logdir.getPath());
237                         flush_preferences();
238                 }
239         }
240
241         public static File logdir() {
242                 synchronized (backend) {
243                         return logdir;
244                 }
245         }
246
247         public static File last_logdir() {
248                 synchronized (backend) {
249                         if (last_logdir == null)
250                                 last_logdir = logdir;
251                         return last_logdir;
252                 }
253         }
254
255         public static void set_last_logdir(File file) {
256                 synchronized(backend) {
257                         if (file != null && !file.isDirectory())
258                                 file = file.getParentFile();
259                         if (file == null)
260                                 file = new File(".");
261                         last_logdir = file;
262                 }
263         }
264
265         public static File mapdir() {
266                 synchronized (backend) {
267                         return mapdir;
268                 }
269         }
270
271         public static void set_frequency(int serial, double new_frequency) {
272                 synchronized (backend) {
273                         frequencies.put(serial, new_frequency);
274                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
275                         flush_preferences();
276                 }
277         }
278
279         public static double frequency(int serial) {
280                 synchronized (backend) {
281                         if (frequencies.containsKey(serial))
282                                 return frequencies.get(serial);
283                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
284                         if (frequency == 0.0) {
285                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
286                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
287                         }
288                         frequencies.put(serial, frequency);
289                         return frequency;
290                 }
291         }
292
293         public static void set_telemetry(int serial, int new_telemetry) {
294                 synchronized (backend) {
295                         telemetries.put(serial, new_telemetry);
296                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
297                         flush_preferences();
298                 }
299         }
300
301         public static int telemetry(int serial) {
302                 synchronized (backend) {
303                         if (telemetries.containsKey(serial))
304                                 return telemetries.get(serial);
305                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
306                                                    AltosLib.ao_telemetry_standard);
307                         telemetries.put(serial, telemetry);
308                         return telemetry;
309                 }
310         }
311
312         public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
313                 synchronized (backend) {
314                         telemetry_rates.put(serial, new_telemetry_rate);
315                         backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
316                         flush_preferences();
317                 }
318         }
319
320         public static int telemetry_rate(int serial) {
321                 synchronized (backend) {
322                         if (telemetry_rates.containsKey(serial))
323                                 return telemetry_rates.get(serial);
324                         int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
325                                                             AltosLib.ao_telemetry_rate_38400);
326                         telemetry_rates.put(serial, telemetry_rate);
327                         return telemetry_rate;
328                 }
329         }
330
331         public static void set_logfile(int serial, File new_logfile) {
332                 synchronized(backend) {
333                         logfiles.put(serial, new_logfile);
334                         backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
335                         flush_preferences();
336                 }
337         }
338
339         public static File logfile(int serial) {
340                 synchronized(backend) {
341                         if (logfiles.containsKey(serial))
342                                 return logfiles.get(serial);
343                         String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
344                         if (logfile_string == null)
345                                 return null;
346                         File logfile = new File(logfile_string);
347                         logfiles.put(serial, logfile);
348                         return logfile;
349                 }
350         }
351
352         public static void set_state(int serial, AltosState state, AltosListenerState listener_state) {
353
354                 backend.debug("set_state for %d pos %g,%g\n",
355                               serial,
356                               state.gps != null ? state.gps.lat : 0.0,
357                               state.gps != null ? state.gps.lon : 0.0);
358
359                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
360
361                 try {
362                         ObjectOutputStream oos = new ObjectOutputStream(baos);
363
364                         AltosSavedState saved_state = new AltosSavedState(state, listener_state);
365                         oos.writeObject(saved_state);
366
367                         byte[] bytes = baos.toByteArray();
368
369                         synchronized(backend) {
370                                 backend.putBytes(String.format(statePreferenceFormat, serial), bytes);
371                                 backend.putInt(statePreferenceLatest, serial);
372                                 flush_preferences();
373                         }
374                 } catch (IOException ie) {
375                         backend.debug("set_state failed %s\n", ie.toString());
376                 }
377         }
378
379         public static ArrayList<Integer> list_states() {
380                 String[]                keys = backend.keys();
381                 ArrayList<Integer>      states = new ArrayList<Integer>();
382
383                 for (String key : keys) {
384                         if (key.startsWith(statePreferenceHead)) {
385                                 backend.debug("list_states %s\n", key);
386                                 try {
387                                         int serial = AltosParse.parse_int(key.substring(statePreferenceHead.length()));
388                                         states.add(serial);
389                                 } catch (ParseException pe) {
390                                 }
391                         }
392                 }
393                 return states;
394         }
395
396         public static void remove_state(int serial) {
397                 synchronized(backend) {
398                         backend.remove(String.format(statePreferenceFormat, serial));
399                 }
400         }
401
402         public static int latest_state() {
403                 int     latest = 0;
404                 synchronized (backend) {
405                         latest = backend.getInt(statePreferenceLatest, 0);
406                 }
407                 return latest;
408         }
409
410         public static AltosSavedState state(int serial) {
411                 byte[] bytes = null;
412
413                 backend.debug("get state %d\n", serial);
414
415                 synchronized(backend) {
416                         bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null);
417                 }
418
419                 if (bytes == null) {
420                         backend.debug("no state for %d\n", serial);
421                         return null;
422                 }
423
424                 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
425
426                 try {
427                         ObjectInputStream ois = new ObjectInputStream(bais);
428                         AltosSavedState saved_state = (AltosSavedState) ois.readObject();
429                         backend.debug("got saved state for %d: %g,%g\n",
430                                       serial,
431                                       saved_state.state.gps != null ? saved_state.state.gps.lat : 0.0,
432                                       saved_state.state.gps != null ? saved_state.state.gps.lon : 0.0);
433                         return saved_state;
434                 } catch (IOException ie) {
435                         backend.debug("IO exception %s\n", ie.toString());
436                 } catch (ClassNotFoundException ce) {
437                         backend.debug("ClassNotFoundException %s\n", ce.toString());
438                 }
439                 return null;
440         }
441
442         public static void set_scanning_telemetry(int new_scanning_telemetry) {
443                 synchronized (backend) {
444                         scanning_telemetry = new_scanning_telemetry;
445                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
446                         flush_preferences();
447                 }
448         }
449
450         public static int scanning_telemetry() {
451                 synchronized (backend) {
452                         return scanning_telemetry;
453                 }
454         }
455
456         public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
457                 synchronized (backend) {
458                         scanning_telemetry_rate = new_scanning_telemetry_rate;
459                         backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
460                         flush_preferences();
461                 }
462         }
463
464         public static int scanning_telemetry_rate() {
465                 synchronized(backend) {
466                         return scanning_telemetry_rate;
467                 }
468         }
469
470         public static void set_voice(boolean new_voice) {
471                 synchronized (backend) {
472                         voice = new_voice;
473                         backend.putBoolean(voicePreference, voice);
474                         flush_preferences();
475                 }
476         }
477
478         public static boolean voice() {
479                 synchronized (backend) {
480                         return voice;
481                 }
482         }
483
484         public static void set_callsign(String new_callsign) {
485                 synchronized(backend) {
486                         callsign = new_callsign;
487                         backend.putString(callsignPreference, callsign);
488                         flush_preferences();
489                 }
490         }
491
492         public static String callsign() {
493                 synchronized(backend) {
494                         return callsign;
495                 }
496         }
497
498         public static void set_firmwaredir(File new_firmwaredir) {
499                 synchronized (backend) {
500                         firmwaredir = new_firmwaredir;
501                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
502                         flush_preferences();
503                 }
504         }
505
506         public static File firmwaredir() {
507                 synchronized (backend) {
508                         return firmwaredir;
509                 }
510         }
511
512         public static void set_launcher_serial(int new_launcher_serial) {
513                 synchronized (backend) {
514                         launcher_serial = new_launcher_serial;
515                         backend.putInt(launcherSerialPreference, launcher_serial);
516                         flush_preferences();
517                 }
518         }
519
520         public static int launcher_serial() {
521                 synchronized (backend) {
522                         return launcher_serial;
523                 }
524         }
525
526         public static void set_launcher_channel(int new_launcher_channel) {
527                 synchronized (backend) {
528                         launcher_channel = new_launcher_channel;
529                         backend.putInt(launcherChannelPreference, launcher_channel);
530                         flush_preferences();
531                 }
532         }
533
534         public static int launcher_channel() {
535                 synchronized (backend) {
536                         return launcher_channel;
537                 }
538         }
539
540         public static AltosPreferencesBackend bt_devices() {
541                 synchronized (backend) {
542                         return backend.node("bt_devices");
543                 }
544         }
545
546         public static AltosFrequency[] common_frequencies() {
547                 synchronized (backend) {
548                         return common_frequencies;
549                 }
550         }
551
552         public static void set_common_frequencies(AltosFrequency[] frequencies) {
553                 synchronized(backend) {
554                         common_frequencies = frequencies;
555                         save_common_frequencies(frequencies);
556                         flush_preferences();
557                 }
558         }
559
560         public static void add_common_frequency(AltosFrequency frequency) {
561                 AltosFrequency[]        old_frequencies = common_frequencies();
562                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
563                 int                     i;
564
565                 for (i = 0; i < old_frequencies.length; i++) {
566                         if (frequency.frequency == old_frequencies[i].frequency)
567                                 return;
568                         if (frequency.frequency < old_frequencies[i].frequency)
569                                 break;
570                         new_frequencies[i] = old_frequencies[i];
571                 }
572                 new_frequencies[i] = frequency;
573                 for (; i < old_frequencies.length; i++)
574                         new_frequencies[i+1] = old_frequencies[i];
575                 set_common_frequencies(new_frequencies);
576         }
577
578         static LinkedList<AltosUnitsListener> units_listeners;
579
580         public static boolean imperial_units() {
581                 synchronized(backend) {
582                         return AltosConvert.imperial_units;
583                 }
584         }
585
586         public static void set_imperial_units(boolean imperial_units) {
587                 synchronized (backend) {
588                         AltosConvert.imperial_units = imperial_units;
589                         backend.putBoolean(unitsPreference, imperial_units);
590                         flush_preferences();
591                 }
592                 if (units_listeners != null) {
593                         for (AltosUnitsListener l : units_listeners) {
594                                 l.units_changed(imperial_units);
595                         }
596                 }
597         }
598
599         public static void register_units_listener(AltosUnitsListener l) {
600                 synchronized(backend) {
601                         if (units_listeners == null)
602                                 units_listeners = new LinkedList<AltosUnitsListener>();
603                         units_listeners.add(l);
604                 }
605         }
606
607         public static void unregister_units_listener(AltosUnitsListener l) {
608                 synchronized(backend) {
609                         units_listeners.remove(l);
610                 }
611         }
612
613
614         public static void register_map_cache_listener(AltosMapCacheListener l) {
615                 synchronized(backend) {
616                         map_cache_listeners.add(l);
617                 }
618         }
619
620         public static void unregister_map_cache_listener(AltosMapCacheListener l) {
621                 synchronized (backend) {
622                         map_cache_listeners.remove(l);
623                 }
624         }
625
626         public static void set_map_cache(int new_map_cache) {
627                 synchronized(backend) {
628                         map_cache = new_map_cache;
629                         backend.putInt(mapCachePreference, map_cache);
630                         flush_preferences();
631                         for (AltosMapCacheListener l: map_cache_listeners)
632                                 l.map_cache_changed(map_cache);
633                 }
634         }
635
636         public static int map_cache() {
637                 synchronized(backend) {
638                         return map_cache;
639                 }
640         }
641 }