From 97adfff4cfb67c17a96f3ff46606b4e439422b01 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 12 May 2016 12:14:03 -0700 Subject: Bump java library versions Prepare for 1.6.4 release Signed-off-by: Keith Packard --- altoslib/AltosFrequency.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'altoslib/AltosFrequency.java') diff --git a/altoslib/AltosFrequency.java b/altoslib/AltosFrequency.java index 9542fe33..ef46bd67 100644 --- a/altoslib/AltosFrequency.java +++ b/altoslib/AltosFrequency.java @@ -15,7 +15,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_10; +package org.altusmetrum.altoslib_11; import java.io.*; import java.util.*; -- cgit v1.2.3 From b13037fad0905c5933d1ff579122ba1357b02eea Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 12 May 2016 19:13:05 -0700 Subject: altoslib: Store common frequencies in library version-independent form Serializable Objects in java are very specific to the class being serialized. As we bump the name of the library on a regular basis to note API/ABI issues, this mean a saved a Serializable object in the preferences database will fail to load across library version upgrades. The saved tracker state and saved common frequencies were the only objects saved in this form; this patch adds infrastructure for writing objects in a version-independent form, and then adds support for saving frequencies in that form. Signed-off-by: Keith Packard --- altoslib/AltosFrequency.java | 13 ++++ altoslib/AltosHashSet.java | 173 +++++++++++++++++++++++++++++++++++++++++ altoslib/AltosParse.java | 8 ++ altoslib/AltosPreferences.java | 29 ++++++- altoslib/Makefile.am | 1 + 5 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 altoslib/AltosHashSet.java (limited to 'altoslib/AltosFrequency.java') diff --git a/altoslib/AltosFrequency.java b/altoslib/AltosFrequency.java index ef46bd67..88997152 100644 --- a/altoslib/AltosFrequency.java +++ b/altoslib/AltosFrequency.java @@ -58,8 +58,21 @@ public class AltosFrequency implements Serializable { return diff < 0.010; } + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putDouble("frequency", frequency); + h.putString("description", description); + return h; + } + public AltosFrequency(double f, String d) { frequency = f; description = d; } + + public AltosFrequency(AltosHashSet h) { + frequency = h.getDouble("frequency", 0.0); + description = h.getString("description", ""); + } } diff --git a/altoslib/AltosHashSet.java b/altoslib/AltosHashSet.java new file mode 100644 index 00000000..488d52e8 --- /dev/null +++ b/altoslib/AltosHashSet.java @@ -0,0 +1,173 @@ +/* + * Copyright © 2016 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altoslib_11; + +import java.io.*; +import java.util.*; +import java.text.*; + +public class AltosHashSet extends Hashtable { + private StringWriter writer; + + static private int get(StringReader reader) throws IOException { + return reader.read(); + } + + static private String get_token(StringReader reader) throws IOException { + int c = get(reader); + + if (c == -1) + return null; + + ArrayList chars = new ArrayList(); + + for (;;) { + chars.add(c); + c = get(reader); + if (c == -1 || c == ';') + break; + if (c == '\\') + c = get(reader); + } + int[] ch = new int[chars.size()]; + for (int i = 0; i < ch.length; i++) + ch[i] = chars.get(i); + return new String(ch, 0, ch.length); + } + + static private void put(StringWriter writer, int c) throws IOException { + writer.write(c); + } + + static private void put_token(StringWriter writer, String token) throws IOException { + for (int i = 0; i < token.length(); i++) { + int c = token.codePointAt(i); + + switch (c) { + case ';': + case '\\': + put(writer, '\\'); + } + put(writer, c); + } + put(writer, ';'); + } + + public String toString() { + try { + StringWriter writer = new StringWriter(); + + for (String key : keySet()) { + String value = get(key); + put_token(writer, key); + put_token(writer, value); + } + return writer.toString(); + } catch (IOException ie) { + return null; + } + } + + public void putInt(String key, int value) { + put(key, Integer.toString(value)); + } + + public int getInt(String key, int def) { + String value = get(key); + + if (value == null) + return def; + try { + return AltosParse.parse_int(value); + } catch (ParseException pe) { + return def; + } + } + + public void putDouble(String key, double value) { + put(key, AltosParse.format_double_net(value)); + } + + public double getDouble(String key, double def) { + String value = get(key); + + if (value == null) + return def; + try { + return AltosParse.parse_double_net(value); + } catch (ParseException pe) { + return def; + } + } + + public String getString(String key, String def) { + String value = get(key); + + if (value == null) + return def; + return value; + } + + public void putString(String key, String value) { + put(key, value); + } + + public AltosHashSet (String string) throws IOException { + StringReader reader = new StringReader(string); + String key, value; + + for (;;) { + key = get_token(reader); + value = get_token(reader); + if (key == null || value == null) + break; + put(key, value); + } + } + + public AltosHashSet() { + } + + static public AltosHashSet[] array(String string) throws IOException { + + if (string == null) + return null; + + StringReader reader = new StringReader(string); + ArrayList array = new ArrayList(); + String element; + + while ((element = get_token(reader)) != null) + array.add(new AltosHashSet(element)); + return array.toArray(new AltosHashSet[0]); + } + + static public String toString(AltosHashSet[] sets) throws IOException { + + if (sets == null) + return null; + + StringWriter writer = new StringWriter(); + + for (AltosHashSet h : sets) { + String element = h.toString(); + put_token(writer, element); + } + return writer.toString(); + } +} diff --git a/altoslib/AltosParse.java b/altoslib/AltosParse.java index ae88182d..12499b7b 100644 --- a/altoslib/AltosParse.java +++ b/altoslib/AltosParse.java @@ -53,6 +53,10 @@ public class AltosParse { } } + public static String format_double_locale(double number) { + return nf_locale.format(number); + } + public static double parse_double_net(String str) throws ParseException { try { return nf_net.parse(str.trim()).doubleValue(); @@ -61,6 +65,10 @@ public class AltosParse { } } + public static String format_double_net(double number) { + return nf_net.format(number); + } + public static double parse_coord(String coord) throws ParseException { String[] dsf = coord.split("\\D+"); diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index b853e944..f8101ce6 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -116,7 +116,7 @@ public class AltosPreferences { public final static String frequency_count = "COUNT"; public final static String frequency_format = "FREQUENCY-%d"; public final static String description_format = "DESCRIPTION-%d"; - public final static String frequenciesPreference = "FREQUENCIES"; + public final static String frequenciesPreference = "FREQUENCIES-1"; /* Units preference */ @@ -136,7 +136,17 @@ public class AltosPreferences { AltosFrequency[] frequencies = null; - frequencies = (AltosFrequency[]) backend.getSerializable(frequenciesPreference, null); + try { + AltosHashSet[] sets = AltosHashSet.array(backend.getString(frequenciesPreference,null)); + if (sets != null) { + frequencies = new AltosFrequency[sets.length]; + for (int i = 0; i < frequencies.length; i++) + frequencies[i] = new AltosFrequency(sets[i]); + } + + } catch (IOException ie) { + frequencies = null; + } if (frequencies == null) { if (backend.nodeExists(common_frequencies_node_name)) { @@ -165,6 +175,17 @@ public class AltosPreferences { return frequencies; } + public static void save_common_frequencies() { + try { + AltosHashSet[] sets = new AltosHashSet[common_frequencies.length]; + for (int i = 0; i < sets.length; i++) + sets[i] = common_frequencies[i].hashSet(); + backend.putString(frequenciesPreference, AltosHashSet.toString(sets)); + } catch (IOException ie) { + } + flush_preferences(); + } + public static int launcher_serial; public static int launcher_channel; @@ -512,8 +533,8 @@ public class AltosPreferences { public static void set_common_frequencies(AltosFrequency[] frequencies) { synchronized(backend) { common_frequencies = frequencies; - backend.putSerializable(frequenciesPreference, frequencies); - flush_preferences(); + + save_common_frequencies(); } } diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index f3219839..512e1cca 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -160,6 +160,7 @@ altoslib_JAVA = \ AltosMapLoaderListener.java \ AltosMapLoader.java \ AltosMapTypeListener.java \ + AltosHashSet.java \ AltosVersion.java JAR=altoslib_$(ALTOSLIB_VERSION).jar -- cgit v1.2.3 From b1a90adac9f6e2a609ce1ccd6749462bb5c9adbe Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 12 May 2016 23:33:53 -0700 Subject: altoslib: Store saved state in version-independent format Use AltosHashSet for AltosState so that AltosDroid doesn't lose tracker information when the application is upgraded. Signed-off-by: Keith Packard --- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 8 +- .../altusmetrum/AltosDroid/TelemetryService.java | 6 +- altoslib/AltosAccel.java | 2 +- altoslib/AltosCompanion.java | 28 +- altoslib/AltosConfigData.java | 2 +- altoslib/AltosFrequency.java | 10 +- altoslib/AltosGPS.java | 63 +++- altoslib/AltosGPSSat.java | 62 +++- altoslib/AltosGreatCircle.java | 29 +- altoslib/AltosHashSet.java | 183 ++++++++-- altoslib/AltosHashable.java | 25 ++ altoslib/AltosIMU.java | 37 +- altoslib/AltosLib.java | 7 +- altoslib/AltosListenerState.java | 2 +- altoslib/AltosMag.java | 28 +- altoslib/AltosMs5607.java | 44 ++- altoslib/AltosParse.java | 8 + altoslib/AltosPreferences.java | 50 +-- altoslib/AltosPreferencesBackend.java | 40 +-- altoslib/AltosPyro.java | 2 +- altoslib/AltosQuaternion.java | 22 +- altoslib/AltosRotation.java | 20 +- altoslib/AltosSavedState.java | 2 +- altoslib/AltosState.java | 398 ++++++++++++++++++++- altoslib/Makefile.am | 1 + altosui/AltosLaunch.java | 4 +- 26 files changed, 965 insertions(+), 118 deletions(-) create mode 100644 altoslib/AltosHashable.java (limited to 'altoslib/AltosFrequency.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 385348ea..a62bf7fe 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -825,9 +825,9 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, } } - private void disconnectDevice() { + private void disconnectDevice(boolean remember) { try { - mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null)); + mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, (Boolean) remember)); } catch (RemoteException e) { } } @@ -978,11 +978,11 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, case R.id.disconnect: /* Disconnect the device */ - disconnectDevice(); + disconnectDevice(false); return true; case R.id.quit: AltosDebug.debug("R.id.quit"); - disconnectDevice(); + disconnectDevice(true); finish(); return true; case R.id.setup: diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 6519a114..dc39c899 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -129,7 +129,8 @@ public class TelemetryService extends Service implements AltosIdleMonitorListene case MSG_DISCONNECT: AltosDebug.debug("Disconnect command received"); s.address = null; - AltosDroidPreferences.set_active_device(null); + if (!(Boolean) msg.obj) + AltosDroidPreferences.set_active_device(null); s.disconnect(true); break; case MSG_DELETE_SERIAL: @@ -613,6 +614,8 @@ public class TelemetryService extends Service implements AltosIdleMonitorListene telemetry_state.latest_serial = AltosPreferences.latest_state(); + AltosDebug.debug("latest serial %d\n", telemetry_state.latest_serial); + for (int serial : serials) { AltosState saved_state = AltosPreferences.state(serial); if (saved_state != null) { @@ -629,6 +632,7 @@ public class TelemetryService extends Service implements AltosIdleMonitorListene telemetry_state.states.put(serial, saved_state); } else { AltosDebug.debug("Failed to recover state for %d", serial); + AltosPreferences.remove_state(serial); } } } diff --git a/altoslib/AltosAccel.java b/altoslib/AltosAccel.java index 00f3aefc..c6a2da11 100644 --- a/altoslib/AltosAccel.java +++ b/altoslib/AltosAccel.java @@ -19,7 +19,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosAccel extends AltosUnits implements Serializable { +public class AltosAccel extends AltosUnits { public double value(double v, boolean imperial_units) { if (imperial_units) diff --git a/altoslib/AltosCompanion.java b/altoslib/AltosCompanion.java index 381b0a25..6f18d93e 100644 --- a/altoslib/AltosCompanion.java +++ b/altoslib/AltosCompanion.java @@ -19,7 +19,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosCompanion implements Serializable { +public class AltosCompanion implements AltosHashable { public final static int board_id_telescience = 0x0a; public final static int MAX_CHANNELS = 12; @@ -37,4 +37,30 @@ public class AltosCompanion implements Serializable { channels = MAX_CHANNELS; companion_data = new int[channels]; } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putInt("tick", tick); + h.putInt("board_id", board_id); + h.putInt("update_period", update_period); + h.putInt("channels", channels); + h.putIntArray("companion_data", companion_data); + return h; + } + + public AltosCompanion(AltosHashSet h) { + tick = h.getInt("tick", tick); + board_id = h.getInt("board_id", board_id); + update_period = h.getInt("update_period", update_period); + channels = h.getInt("channels", channels); + companion_data = h.getIntArray("companion_data", new int[channels]); + } + + public static AltosCompanion fromHashSet(AltosHashSet h, AltosCompanion def) { + if (h == null) + return def; + + return new AltosCompanion(h); + } } diff --git a/altoslib/AltosConfigData.java b/altoslib/AltosConfigData.java index aa46f118..ce430d7a 100644 --- a/altoslib/AltosConfigData.java +++ b/altoslib/AltosConfigData.java @@ -204,7 +204,7 @@ public class AltosConfigData implements Iterable { for (int i = 0; i < parts.length; i++) { try { - r[i] = AltosLib.fromdec(parts[i]); + r[i] = (int) AltosLib.fromdec(parts[i]); } catch (NumberFormatException n) { r[i] = 0; } diff --git a/altoslib/AltosFrequency.java b/altoslib/AltosFrequency.java index 88997152..f9aa6de6 100644 --- a/altoslib/AltosFrequency.java +++ b/altoslib/AltosFrequency.java @@ -21,7 +21,7 @@ import java.io.*; import java.util.*; import java.text.*; -public class AltosFrequency implements Serializable { +public class AltosFrequency { public double frequency; public String description; @@ -71,8 +71,14 @@ public class AltosFrequency implements Serializable { description = d; } - public AltosFrequency(AltosHashSet h) { + private AltosFrequency(AltosHashSet h) { frequency = h.getDouble("frequency", 0.0); description = h.getString("description", ""); } + + public static AltosFrequency fromHashSet(AltosHashSet h, AltosFrequency def) { + if (h == null) + return def; + return new AltosFrequency(h); + } } diff --git a/altoslib/AltosGPS.java b/altoslib/AltosGPS.java index 6f7c40b7..371fd7bf 100644 --- a/altoslib/AltosGPS.java +++ b/altoslib/AltosGPS.java @@ -21,7 +21,7 @@ import java.text.*; import java.util.concurrent.*; import java.io.*; -public class AltosGPS implements Cloneable, Serializable { +public class AltosGPS implements Cloneable, AltosHashable { public final static int MISSING = AltosLib.MISSING; @@ -388,4 +388,65 @@ public class AltosGPS implements Cloneable, Serializable { break; } } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putInt("nsat", nsat); + h.putBoolean("locked", locked); + h.putBoolean("connected", connected); + h.putDouble("lat", lat); + h.putDouble("lon", lon); + h.putDouble("alt", alt); + h.putInt("year", year); + h.putInt("month", month); + h.putInt("day", day); + h.putInt("hour", hour); + h.putInt("minute", minute); + h.putInt("second", second); + + h.putDouble("ground_speed", ground_speed); + h.putInt("course", course); + h.putDouble("climb_rate", climb_rate); + h.putDouble("pdop", pdop); + h.putDouble("hdop", hdop); + h.putDouble("vdop", vdop); + h.putDouble("h_error", h_error); + h.putDouble("v_error", v_error); + h.putString("cc_gps_sat", AltosGPSSat.toString(cc_gps_sat)); + return h; + } + + public AltosGPS(AltosHashSet h) { + init(); + nsat = h.getInt("nsat", nsat); + locked = h.getBoolean("locked", locked); + connected = h.getBoolean("connected", connected); + lat = h.getDouble("lat", lat); + lon = h.getDouble("lon", lon); + alt = h.getDouble("alt", alt); + year = h.getInt("year", year); + month = h.getInt("month", month); + day = h.getInt("day", day); + hour = h.getInt("hour", hour); + minute = h.getInt("minute", minute); + second = h.getInt("second", second); + + ground_speed = h.getDouble("ground_speed", ground_speed); + course = h.getInt("course", course); + climb_rate = h.getDouble("climb_rate", climb_rate); + pdop = h.getDouble("pdop", pdop); + hdop = h.getDouble("hdop", hdop); + vdop = h.getDouble("vdop", vdop); + h_error = h.getDouble("h_error", h_error); + v_error = h.getDouble("v_error", v_error); + cc_gps_sat = AltosGPSSat.array(h.getString("cc_gps_sat", null)); + } + + public static AltosGPS fromHashSet(AltosHashSet h, AltosGPS def) { + if (h == null) + return def; + + return new AltosGPS(h); + } } diff --git a/altoslib/AltosGPSSat.java b/altoslib/AltosGPSSat.java index abde1c0c..ad7a8647 100644 --- a/altoslib/AltosGPSSat.java +++ b/altoslib/AltosGPSSat.java @@ -16,11 +16,13 @@ */ package org.altusmetrum.altoslib_11; + +import java.io.*; import java.text.*; +import java.util.*; import java.util.concurrent.*; -import java.io.*; -public class AltosGPSSat implements Serializable { +public class AltosGPSSat { public int svid; public int c_n0; @@ -31,5 +33,61 @@ public class AltosGPSSat implements Serializable { public AltosGPSSat() { } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + h.putInt("svid", svid); + h.putInt("c_n0", c_n0); + return h; + } + + private AltosGPSSat(AltosHashSet h) { + svid = h.getInt("svid", 0); + c_n0 = h.getInt("c_n0", 0); + } + + static public AltosGPSSat fromHashSet(AltosHashSet h, AltosGPSSat def) { + if (h == null) + return def; + return new AltosGPSSat(h); + } + + static public AltosGPSSat[] array(String string) { + + if (string == null) + return null; + + try { + StringReader reader = new StringReader(string); + ArrayList array = new ArrayList(); + String element; + + while ((element = AltosHashSet.get_token(reader)) != null) { + AltosGPSSat sat = AltosGPSSat.fromHashSet(AltosHashSet.fromString(element), null); + if (sat != null) + array.add(sat); + } + return array.toArray(new AltosGPSSat[0]); + } catch (IOException ie) { + return null; + } + } + + public static String toString(AltosGPSSat[] sats) { + if (sats == null) + return null; + + try { + StringWriter writer = new StringWriter(); + + for (AltosGPSSat g : sats) { + String element = g.hashSet().toString(); + AltosHashSet.put_token(writer, element); + } + return writer.toString(); + } catch (IOException ie) { + return null; + } + } } diff --git a/altoslib/AltosGreatCircle.java b/altoslib/AltosGreatCircle.java index 03e05678..9ec808a5 100644 --- a/altoslib/AltosGreatCircle.java +++ b/altoslib/AltosGreatCircle.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.lang.Math; import java.io.*; -public class AltosGreatCircle implements Cloneable, Serializable { +public class AltosGreatCircle implements Cloneable, AltosHashable { public double distance; public double bearing; public double range; @@ -103,4 +103,31 @@ public class AltosGreatCircle implements Cloneable, Serializable { range = 0; elevation = 0; } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putDouble("distance", distance); + h.putDouble("bearing", bearing); + h.putDouble("range", range); + h.putDouble("elevation", elevation); + + return h; + } + + public AltosGreatCircle(AltosHashSet h) { + this(); + + distance = h.getDouble("distance", distance); + bearing = h.getDouble("bearing", bearing); + range = h.getDouble("range", range); + elevation = h.getDouble("elevation", elevation); + } + + public static AltosGreatCircle fromHashSet(AltosHashSet h, AltosGreatCircle def) { + if (h == null) + return def; + + return new AltosGreatCircle(h); + } } diff --git a/altoslib/AltosHashSet.java b/altoslib/AltosHashSet.java index 488d52e8..4b89f8cc 100644 --- a/altoslib/AltosHashSet.java +++ b/altoslib/AltosHashSet.java @@ -22,13 +22,11 @@ import java.util.*; import java.text.*; public class AltosHashSet extends Hashtable { - private StringWriter writer; - static private int get(StringReader reader) throws IOException { return reader.read(); } - static private String get_token(StringReader reader) throws IOException { + static public String get_token(StringReader reader) throws IOException { int c = get(reader); if (c == -1) @@ -54,7 +52,7 @@ public class AltosHashSet extends Hashtable { writer.write(c); } - static private void put_token(StringWriter writer, String token) throws IOException { + static public void put_token(StringWriter writer, String token) throws IOException { for (int i = 0; i < token.length(); i++) { int c = token.codePointAt(i); @@ -83,6 +81,22 @@ public class AltosHashSet extends Hashtable { } } + public void putBoolean(String key, boolean value) { + put(key, value ? "t" : "f"); + } + + public boolean getBoolean(String key, boolean def) { + String value = get(key); + + if (value == null) + return def; + if (value.equals("t")) + return true; + if (value.equals("f")) + return false; + return def; + } + public void putInt(String key, int value) { put(key, Integer.toString(value)); } @@ -99,6 +113,59 @@ public class AltosHashSet extends Hashtable { } } + public void putIntArray(String key, int value[]) { + if (value == null) + return; + + StringWriter writer = new StringWriter(); + + try { + for (int i = 0; i < value.length; i++) + put_token(writer, Integer.toString(value[i])); + put(key, writer.toString()); + } catch (IOException ie) { + } + } + + public int[] getIntArray(String key, int[] def) { + String value = get(key); + + if (value == null) + return def; + try { + StringReader reader = new StringReader(value); + ArrayList array = new ArrayList(); + String elt; + + while ((elt = get_token(reader)) != null) + array.add(AltosParse.parse_int(elt)); + int[] ret = new int[array.size()]; + for (int i = 0; i < ret.length; i++) + ret[i] = array.get(i); + return ret; + } catch (ParseException pe) { + return def; + } catch (IOException ie) { + return def; + } + } + + public void putLong(String key, long value) { + put(key, Long.toString(value)); + } + + public long getLong(String key, long def) { + String value = get(key); + + if (value == null) + return def; + try { + return AltosParse.parse_long(value); + } catch (ParseException pe) { + return def; + } + } + public void putDouble(String key, double value) { put(key, AltosParse.format_double_net(value)); } @@ -115,6 +182,43 @@ public class AltosHashSet extends Hashtable { } } + public void putDoubleArray(String key, double value[]) { + if (value == null) + return; + + StringWriter writer = new StringWriter(); + + try { + for (int i = 0; i < value.length; i++) + put_token(writer, AltosParse.format_double_net(value[i])); + put(key, writer.toString()); + } catch (IOException ie) { + } + } + + public double[] getDoubleArray(String key, double[] def) { + String value = get(key); + + if (value == null) + return def; + try { + StringReader reader = new StringReader(value); + ArrayList array = new ArrayList(); + String elt; + + while ((elt = get_token(reader)) != null) + array.add(AltosParse.parse_double_net(elt)); + double[] ret = new double[array.size()]; + for (int i = 0; i < ret.length; i++) + ret[i] = array.get(i); + return ret; + } catch (ParseException pe) { + return def; + } catch (IOException ie) { + return def; + } + } + public String getString(String key, String def) { String value = get(key); @@ -124,10 +228,34 @@ public class AltosHashSet extends Hashtable { } public void putString(String key, String value) { - put(key, value); + if (value != null) + put(key, value); } - public AltosHashSet (String string) throws IOException { + public AltosHashSet getHash(String key) { + String value = get(key); + + if (value == null) + return null; + try { + return new AltosHashSet(value); + } catch (IOException ie) { + return null; + } + } + + public void putHash(String key, AltosHashSet h) { + put(key, h.toString()); + } + + public void putHashable(String key, AltosHashable h) { + if (h == null) + return; + + put(key, h.hashSet().toString()); + } + + private AltosHashSet (String string) throws IOException { StringReader reader = new StringReader(string); String key, value; @@ -143,31 +271,46 @@ public class AltosHashSet extends Hashtable { public AltosHashSet() { } - static public AltosHashSet[] array(String string) throws IOException { + static public AltosHashSet fromString(String string) { + try { + return new AltosHashSet(string); + } catch (IOException ie) { + return null; + } + } + + static public AltosHashSet[] array(String string) { if (string == null) return null; - StringReader reader = new StringReader(string); - ArrayList array = new ArrayList(); - String element; + try { + StringReader reader = new StringReader(string); + ArrayList array = new ArrayList(); + String element; - while ((element = get_token(reader)) != null) - array.add(new AltosHashSet(element)); - return array.toArray(new AltosHashSet[0]); + while ((element = get_token(reader)) != null) + array.add(new AltosHashSet(element)); + return array.toArray(new AltosHashSet[0]); + } catch (IOException ie) { + return null; + } } - static public String toString(AltosHashSet[] sets) throws IOException { - + static public String toString(AltosHashSet[] sets) { if (sets == null) return null; - StringWriter writer = new StringWriter(); + try { + StringWriter writer = new StringWriter(); - for (AltosHashSet h : sets) { - String element = h.toString(); - put_token(writer, element); + for (AltosHashSet h : sets) { + String element = h.toString(); + put_token(writer, element); + } + return writer.toString(); + } catch (IOException ie) { + return null; } - return writer.toString(); } } diff --git a/altoslib/AltosHashable.java b/altoslib/AltosHashable.java new file mode 100644 index 00000000..e228543d --- /dev/null +++ b/altoslib/AltosHashable.java @@ -0,0 +1,25 @@ +/* + * Copyright © 2016 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altoslib_11; + +import java.io.*; + +public interface AltosHashable { + + public AltosHashSet hashSet(); +} diff --git a/altoslib/AltosIMU.java b/altoslib/AltosIMU.java index 26d11591..df6c4ed3 100644 --- a/altoslib/AltosIMU.java +++ b/altoslib/AltosIMU.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosIMU implements Cloneable, Serializable { +public class AltosIMU implements Cloneable, AltosHashable { public int accel_along; public int accel_across; public int accel_through; @@ -29,13 +29,13 @@ public class AltosIMU implements Cloneable, Serializable { public int gyro_pitch; public int gyro_yaw; - public static double counts_per_g = 2048.0; + public static final double counts_per_g = 2048.0; public static double convert_accel(double counts) { return counts / counts_per_g * (-AltosConvert.GRAVITATIONAL_ACCELERATION); } - public static double counts_per_degsec = 16.4; + public static final double counts_per_degsec = 16.4; public static double convert_gyro(double counts) { return counts / counts_per_degsec; @@ -115,4 +115,35 @@ public class AltosIMU implements Cloneable, Serializable { break; } } + + public AltosIMU (AltosHashSet h) { + this(); + + accel_along = h.getInt("accel_along", accel_along); + accel_across = h.getInt("accel_across", accel_across); + accel_through = h.getInt("accel_through", accel_through); + + gyro_roll = h.getInt("gyro_roll", gyro_roll); + gyro_pitch = h.getInt("gyro_pitch", gyro_pitch); + gyro_yaw = h.getInt("gyro_yaw", gyro_yaw); + } + + static public AltosIMU fromHashSet(AltosHashSet h, AltosIMU def) { + if (h == null) + return def; + return new AltosIMU(h); + } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putInt("accel_along", accel_along); + h.putInt("accel_across", accel_across); + h.putInt("accel_through", accel_through); + + h.putInt("gyro_roll", gyro_roll); + h.putInt("gyro_pitch", gyro_pitch); + h.putInt("gyro_yaw", gyro_yaw); + return h; + } } diff --git a/altoslib/AltosLib.java b/altoslib/AltosLib.java index 103052cb..044caf8d 100644 --- a/altoslib/AltosLib.java +++ b/altoslib/AltosLib.java @@ -493,9 +493,10 @@ public class AltosLib { return r; } - public static int fromdec(String s) throws NumberFormatException { - int c, v = 0; - int sign = 1; + public static long fromdec(String s) throws NumberFormatException { + int c; + long v = 0; + long sign = 1; for (int i = 0; i < s.length(); i++) { c = s.charAt(i); if (i == 0 && c == '-') { diff --git a/altoslib/AltosListenerState.java b/altoslib/AltosListenerState.java index f5d1c0cb..7d9ec2a4 100644 --- a/altoslib/AltosListenerState.java +++ b/altoslib/AltosListenerState.java @@ -19,7 +19,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosListenerState implements Serializable { +public class AltosListenerState { public int crc_errors; public double battery; public boolean running; diff --git a/altoslib/AltosMag.java b/altoslib/AltosMag.java index 3e82f499..c350ae46 100644 --- a/altoslib/AltosMag.java +++ b/altoslib/AltosMag.java @@ -20,12 +20,12 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMag implements Cloneable, Serializable { +public class AltosMag implements Cloneable, AltosHashable { public int along; public int across; public int through; - public static double counts_per_gauss = 1090; + public static final double counts_per_gauss = 1090; public static double convert_gauss(double counts) { return counts / counts_per_gauss; @@ -93,4 +93,28 @@ public class AltosMag implements Cloneable, Serializable { break; } } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putInt("along", along); + h.putInt("across", across); + h.putInt("through", through); + return h; + } + + public AltosMag(AltosHashSet h) { + this(); + + along = h.getInt("along", along); + across = h.getInt("across", across); + through = h.getInt("through", through); + } + + public static AltosMag fromHashSet(AltosHashSet h, AltosMag def) { + if (h == null) + return def; + + return new AltosMag(h); + } } diff --git a/altoslib/AltosMs5607.java b/altoslib/AltosMs5607.java index 4f5549a7..88a97828 100644 --- a/altoslib/AltosMs5607.java +++ b/altoslib/AltosMs5607.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMs5607 implements Serializable { +public class AltosMs5607 implements AltosHashable { public int reserved; public int sens; public int off; @@ -166,4 +166,46 @@ public class AltosMs5607 implements Serializable { } convert(); } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putInt("reserved", reserved); + h.putInt("sens", sens); + h.putInt("off", off); + h.putInt("tcs", tcs); + h.putInt("tco", tco); + h.putInt("tref", tref); + h.putInt("tempsens", tempsens); + h.putInt("crc", crc); + h.putInt("raw_pres", raw_pres); + h.putInt("raw_temp", raw_temp); + h.putInt("pa", pa); + h.putInt("cc", cc); + return h; + } + + public AltosMs5607(AltosHashSet h) { + this(); + + reserved = h.getInt("reserved", reserved); + sens = h.getInt("sens", sens); + off = h.getInt("off", off); + tcs = h.getInt("tcs", tcs); + tco = h.getInt("tco", tco); + tref = h.getInt("tref", tref); + tempsens = h.getInt("tempsens", tempsens); + crc = h.getInt("crc", crc); + raw_pres = h.getInt("raw_pres", raw_pres); + raw_temp = h.getInt("raw_temp", raw_temp); + pa = h.getInt("pa", pa); + cc = h.getInt("cc", cc); + } + + public static AltosMs5607 fromHashSet(AltosHashSet h, AltosMs5607 def) { + if (h == null) + return def; + + return new AltosMs5607(h); + } } diff --git a/altoslib/AltosParse.java b/altoslib/AltosParse.java index 12499b7b..fbd049ae 100644 --- a/altoslib/AltosParse.java +++ b/altoslib/AltosParse.java @@ -26,6 +26,14 @@ public class AltosParse { } public static int parse_int(String v) throws ParseException { + try { + return (int) AltosLib.fromdec(v); + } catch (NumberFormatException e) { + throw new ParseException("error parsing int " + v, 0); + } + } + + public static long parse_long(String v) throws ParseException { try { return AltosLib.fromdec(v); } catch (NumberFormatException e) { diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index f8101ce6..3f8e7a07 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -136,16 +136,16 @@ public class AltosPreferences { AltosFrequency[] frequencies = null; - try { - AltosHashSet[] sets = AltosHashSet.array(backend.getString(frequenciesPreference,null)); - if (sets != null) { - frequencies = new AltosFrequency[sets.length]; - for (int i = 0; i < frequencies.length; i++) - frequencies[i] = new AltosFrequency(sets[i]); + AltosHashSet[] sets = AltosHashSet.array(backend.getString(frequenciesPreference,null)); + if (sets != null) { + ArrayList freqs = new ArrayList(); + + for (int i = 0; i < sets.length; i++) { + AltosFrequency f = AltosFrequency.fromHashSet(sets[i], null); + if (f != null) + freqs.add(f); } - - } catch (IOException ie) { - frequencies = null; + frequencies = freqs.toArray(new AltosFrequency[0]); } if (frequencies == null) { @@ -153,14 +153,16 @@ public class AltosPreferences { AltosPreferencesBackend node = backend.node(common_frequencies_node_name); int count = node.getInt(frequency_count, 0); - frequencies = new AltosFrequency[count]; - for (int i = 0; i < count; i++) { - double frequency; - String description; + if (count > 0) { + frequencies = new AltosFrequency[count]; + for (int i = 0; i < count; i++) { + double frequency; + String description; - frequency = node.getDouble(String.format(frequency_format, i), 0.0); - description = node.getString(String.format(description_format, i), null); - frequencies[i] = new AltosFrequency(frequency, description); + frequency = node.getDouble(String.format(frequency_format, i), 0.0); + description = node.getString(String.format(description_format, i), null); + frequencies[i] = new AltosFrequency(frequency, description); + } } } } @@ -176,13 +178,10 @@ public class AltosPreferences { } public static void save_common_frequencies() { - try { - AltosHashSet[] sets = new AltosHashSet[common_frequencies.length]; - for (int i = 0; i < sets.length; i++) - sets[i] = common_frequencies[i].hashSet(); - backend.putString(frequenciesPreference, AltosHashSet.toString(sets)); - } catch (IOException ie) { - } + AltosHashSet[] sets = new AltosHashSet[common_frequencies.length]; + for (int i = 0; i < sets.length; i++) + sets[i] = common_frequencies[i].hashSet(); + backend.putString(frequenciesPreference, AltosHashSet.toString(sets)); flush_preferences(); } @@ -374,7 +373,7 @@ public class AltosPreferences { public static void set_state(AltosState state) { synchronized(backend) { - backend.putSerializable(String.format(statePreferenceFormat, state.serial), state); + backend.putHashSet(String.format(statePreferenceFormat, state.serial), state.hashSet()); backend.putInt(statePreferenceLatest, state.serial); flush_preferences(); } @@ -399,6 +398,7 @@ public class AltosPreferences { public static void remove_state(int serial) { synchronized(backend) { backend.remove(String.format(statePreferenceFormat, serial)); + flush_preferences(); } } @@ -413,7 +413,7 @@ public class AltosPreferences { public static AltosState state(int serial) { synchronized(backend) { try { - return (AltosState) backend.getSerializable(String.format(statePreferenceFormat, serial), null); + return AltosState.fromHashSet(backend.getHashSet(String.format(statePreferenceFormat, serial))); } catch (Exception e) { return null; } diff --git a/altoslib/AltosPreferencesBackend.java b/altoslib/AltosPreferencesBackend.java index 1f925914..9131ad39 100644 --- a/altoslib/AltosPreferencesBackend.java +++ b/altoslib/AltosPreferencesBackend.java @@ -38,40 +38,16 @@ public abstract class AltosPreferencesBackend { public abstract byte[] getBytes(String key, byte[] def); public abstract void putBytes(String key, byte[] value); - public Serializable getSerializable(String key, Serializable def) { - byte[] bytes = null; - - bytes = getBytes(key, null); - if (bytes == null) - return def; - - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - - try { - ObjectInputStream ois = new ObjectInputStream(bais); - Serializable object = (Serializable) ois.readObject(); - return object; - } catch (IOException ie) { - debug("IO exception %s\n", ie.toString()); - } catch (ClassNotFoundException ce) { - debug("ClassNotFoundException %s\n", ce.toString()); - } - return def; - } - - public void putSerializable(String key, Serializable object) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + public AltosHashSet getHashSet(String key) { + String value = getString(key, null); - try { - ObjectOutputStream oos = new ObjectOutputStream(baos); - - oos.writeObject(object); - byte[] bytes = baos.toByteArray(); + if (value == null) + return null; + return AltosHashSet.fromString(value); + } - putBytes(key, bytes); - } catch (IOException ie) { - debug("set_state failed %s\n", ie.toString()); - } + public void putHashSet(String key, AltosHashSet h) { + putString(key, h.toString()); } public abstract boolean nodeExists(String key); diff --git a/altoslib/AltosPyro.java b/altoslib/AltosPyro.java index a1a903fd..c948ce21 100644 --- a/altoslib/AltosPyro.java +++ b/altoslib/AltosPyro.java @@ -277,7 +277,7 @@ public class AltosPyro { int value = 0; ++i; try { - value = AltosLib.fromdec(tokens[i]); + value = (int) AltosLib.fromdec(tokens[i]); } catch (NumberFormatException n) { throw new ParseException(String.format("Invalid pyro value \"%s\"", tokens[i]), i); diff --git a/altoslib/AltosQuaternion.java b/altoslib/AltosQuaternion.java index 351685f8..af9eb475 100644 --- a/altoslib/AltosQuaternion.java +++ b/altoslib/AltosQuaternion.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosQuaternion { +public class AltosQuaternion implements AltosHashable { double r; /* real bit */ double x, y, z; /* imaginary bits */ @@ -147,4 +147,24 @@ public class AltosQuaternion { c_x * s_y * c_z + s_x * c_y * s_z, c_x * c_y * s_z - s_x * s_y * c_z); } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putDouble("r", r); + h.putDouble("x", x); + h.putDouble("y", y); + h.putDouble("z", z); + return h; + } + + public AltosQuaternion(AltosHashSet h) { + if (h == null) + return; + + r = h.getDouble("r", 1); + x = h.getDouble("x", 0); + y = h.getDouble("y", 0); + z = h.getDouble("z", 0); + } } diff --git a/altoslib/AltosRotation.java b/altoslib/AltosRotation.java index 411ecbdf..e9c447ad 100644 --- a/altoslib/AltosRotation.java +++ b/altoslib/AltosRotation.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosRotation { +public class AltosRotation implements AltosHashable { private AltosQuaternion rotation; public double tilt() { @@ -47,4 +47,22 @@ public class AltosRotation { AltosQuaternion up = new AltosQuaternion(0, 0, 0, sky); rotation = up.vectors_to_rotation(orient); } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putHashable("rotation", rotation); + return h; + } + + public AltosRotation(AltosHashSet h) { + rotation = new AltosQuaternion(h.getHash("rotation")); + } + + public static AltosRotation fromHashSet(AltosHashSet h, AltosRotation def) { + if (h == null) + return def; + + return new AltosRotation(h); + } } diff --git a/altoslib/AltosSavedState.java b/altoslib/AltosSavedState.java index a7954043..f1d3e993 100644 --- a/altoslib/AltosSavedState.java +++ b/altoslib/AltosSavedState.java @@ -19,7 +19,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosSavedState implements Serializable { +public class AltosSavedState { public AltosState state; public AltosListenerState listener_state; diff --git a/altoslib/AltosState.java b/altoslib/AltosState.java index ca28a16d..0970a88e 100644 --- a/altoslib/AltosState.java +++ b/altoslib/AltosState.java @@ -23,7 +23,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosState implements Cloneable, Serializable { +public class AltosState implements Cloneable, AltosHashable { public static final int set_position = 1; public static final int set_gps = 2; @@ -46,7 +46,7 @@ public class AltosState implements Cloneable, Serializable { private int prev_tick; public int boost_tick; - class AltosValue implements Serializable{ + class AltosValue implements AltosHashable { double value; double prev_value; private double max_value; @@ -177,19 +177,56 @@ public class AltosState implements Cloneable, Serializable { prev_set_time = set_time; } + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putDouble("value", value); + h.putDouble("prev_value", prev_value); + h.putDouble("max_value", max_value); + h.putDouble("set_time", set_time); + h.putDouble("prev_set_time", prev_set_time); + return h; + } + + AltosValue(AltosHashSet h) { + this(); + if (h != null) { + value = h.getDouble("value", value); + prev_value = h.getDouble("prev_value", prev_value); + max_value = h.getDouble("max_value", max_value); + set_time = h.getDouble("set_time", 0); + prev_set_time = h.getDouble("prev_set_time", 0); + } + } + AltosValue() { value = AltosLib.MISSING; prev_value = AltosLib.MISSING; max_value = AltosLib.MISSING; } + + } + + AltosValue AltosValue_fromHashSet(AltosHashSet h, AltosValue def) { + if (h == null) + return def; + return new AltosValue(h); } - class AltosCValue implements Serializable { + class AltosCValue implements AltosHashable { - class AltosIValue extends AltosValue implements Serializable { + class AltosIValue extends AltosValue implements AltosHashable { boolean can_max() { return c_can_max(); } + + AltosIValue() { + super(); + } + + AltosIValue(AltosHashSet h) { + super(h); + } }; public AltosIValue measured; @@ -282,6 +319,26 @@ public class AltosState implements Cloneable, Serializable { measured = new AltosIValue(); computed = new AltosIValue(); } + + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putHashable("measured", measured); + h.putHashable("computed", computed); + return h; + } + + AltosCValue(AltosHashSet h) { + measured = new AltosIValue(h.getHash("measured")); + computed = new AltosIValue(h.getHash("computed")); + } + } + + AltosCValue AltosCValue_fromHashSet(AltosHashSet h, AltosCValue def) { + if (h == null) + return def; + return new AltosCValue(h); } private int state; @@ -317,7 +374,7 @@ public class AltosState implements Cloneable, Serializable { ground_altitude.set_measured(a, time); } - class AltosGpsGroundAltitude extends AltosValue implements Serializable { + class AltosGpsGroundAltitude extends AltosValue { void set(double a, double t) { super.set(a, t); pad_alt = value(); @@ -329,6 +386,19 @@ public class AltosState implements Cloneable, Serializable { pad_alt = value(); gps_altitude.set_gps_height(); } + + AltosGpsGroundAltitude() { + super(); + } + + AltosGpsGroundAltitude (AltosHashSet h) { + super(h); + } + } + + AltosGpsGroundAltitude AltosGpsGroundAltitude_fromHashSet(AltosHashSet h, AltosGpsGroundAltitude def) { + if (h == null) return def; + return new AltosGpsGroundAltitude(h); } private AltosGpsGroundAltitude gps_ground_altitude; @@ -341,7 +411,7 @@ public class AltosState implements Cloneable, Serializable { gps_ground_altitude.set(a, time); } - class AltosGroundPressure extends AltosCValue implements Serializable { + class AltosGroundPressure extends AltosCValue { void set_filtered(double p, double time) { computed.set_filtered(p, time); if (!is_measured()) @@ -352,6 +422,19 @@ public class AltosState implements Cloneable, Serializable { super.set_measured(p, time); ground_altitude.set_computed(pressure_to_altitude(p), time); } + + AltosGroundPressure () { + super(); + } + + AltosGroundPressure (AltosHashSet h) { + super(h); + } + } + + AltosGroundPressure AltosGroundPressure_fromHashSet(AltosHashSet h, AltosGroundPressure def) { + if (h == null) return def; + return new AltosGroundPressure(h); } private AltosGroundPressure ground_pressure; @@ -364,7 +447,7 @@ public class AltosState implements Cloneable, Serializable { ground_pressure.set_measured(pressure, time); } - class AltosAltitude extends AltosCValue implements Serializable { + class AltosAltitude extends AltosCValue implements AltosHashable { private void set_speed(AltosValue v) { if (!acceleration.is_measured() || !ascent) @@ -382,11 +465,24 @@ public class AltosState implements Cloneable, Serializable { set_speed(measured); set |= set_position; } + + AltosAltitude() { + super(); + } + + AltosAltitude (AltosHashSet h) { + super(h); + } + } + + AltosAltitude AltosAltitude_fromHashSet(AltosHashSet h, AltosAltitude def) { + if (h == null) return def; + return new AltosAltitude(h); } private AltosAltitude altitude; - class AltosGpsAltitude extends AltosValue implements Serializable { + class AltosGpsAltitude extends AltosValue implements AltosHashable { private void set_gps_height() { double a = value(); @@ -402,6 +498,19 @@ public class AltosState implements Cloneable, Serializable { super.set(a, t); set_gps_height(); } + + AltosGpsAltitude() { + super(); + } + + AltosGpsAltitude (AltosHashSet h) { + super(h); + } + } + + AltosGpsAltitude AltosGpsAltitude_fromHashSet(AltosHashSet h, AltosGpsAltitude def) { + if (h == null) return def; + return new AltosGpsAltitude(h); } private AltosGpsAltitude gps_altitude; @@ -469,7 +578,7 @@ public class AltosState implements Cloneable, Serializable { return gps_speed.max(); } - class AltosPressure extends AltosValue implements Serializable { + class AltosPressure extends AltosValue { void set(double p, double time) { super.set(p, time); if (state == AltosLib.ao_flight_pad) @@ -477,6 +586,19 @@ public class AltosState implements Cloneable, Serializable { double a = pressure_to_altitude(p); altitude.set_computed(a, time); } + + AltosPressure() { + super(); + } + + AltosPressure (AltosHashSet h) { + super(h); + } + } + + AltosPressure AltosPressure_fromHashSet(AltosHashSet h, AltosPressure def) { + if (h == null) return def; + return new AltosPressure(h); } private AltosPressure pressure; @@ -539,7 +661,7 @@ public class AltosState implements Cloneable, Serializable { return AltosLib.MISSING; } - class AltosSpeed extends AltosCValue implements Serializable { + class AltosSpeed extends AltosCValue implements AltosHashable { boolean can_max() { return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless; @@ -563,6 +685,19 @@ public class AltosState implements Cloneable, Serializable { super.set_measured(new_value, time); set_accel(); } + + AltosSpeed() { + super(); + } + + AltosSpeed (AltosHashSet h) { + super(h); + } + } + + AltosSpeed AltosSpeed_fromHashSet(AltosHashSet h, AltosSpeed def) { + if (h == null) return def; + return new AltosSpeed(h); } private AltosSpeed speed; @@ -593,7 +728,7 @@ public class AltosState implements Cloneable, Serializable { return AltosLib.MISSING; } - class AltosAccel extends AltosCValue implements Serializable { + class AltosAccel extends AltosCValue implements AltosHashable { boolean can_max() { return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless; @@ -604,6 +739,19 @@ public class AltosState implements Cloneable, Serializable { if (ascent) speed.set_integral(this.measured); } + + AltosAccel() { + super(); + } + + AltosAccel (AltosHashSet h) { + super(h); + } + } + + AltosAccel AltosAccel_fromHashSet(AltosHashSet h, AltosAccel def) { + if (h == null) return def; + return new AltosAccel(h); } AltosAccel acceleration; @@ -1483,10 +1631,238 @@ public class AltosState implements Cloneable, Serializable { public AltosState clone() { AltosState s = new AltosState(); s.copy(this); + + AltosHashSet hash = hashSet(); + String onetrip = hash.toString(); + AltosHashSet back = AltosHashSet.fromString(onetrip); + AltosState tripstate = AltosState.fromHashSet(back); + AltosHashSet triphash = tripstate.hashSet(); + String twotrip = triphash.toString(); + + if (!onetrip.equals(twotrip)) { + System.out.printf("%s\n%s\n", onetrip, twotrip); + System.exit(1); + } return s; } public AltosState () { init(); } + + public AltosHashSet hashSet() { + AltosHashSet h = new AltosHashSet(); + + h.putBoolean("valid", true); + h.putInt("set", set); + h.putLong("received_time", received_time); + h.putDouble("time", time); + h.putDouble("prev_time", prev_time); + h.putDouble("time_change", time_change); + h.putInt("tick", tick); + h.putInt("prev_tick", prev_tick); + h.putInt("boost_tick", boost_tick); + h.putInt("state", state); + h.putInt("flight", flight); + h.putInt("serial", serial); + h.putInt("altitude_32", altitude_32); + h.putInt("receiver_serial", receiver_serial); + h.putBoolean("landed", landed); + h.putBoolean("ascent", ascent); + h.putBoolean("boost", boost); + h.putInt("rssi", rssi); + h.putInt("status", status); + h.putInt("device_type", device_type); + h.putInt("config_major", config_major); + h.putInt("config_minor", config_minor); + h.putInt("apogee_delay", apogee_delay); + h.putInt("main_deploy", main_deploy); + h.putInt("flight_log_max", flight_log_max); + h.putHashable("ground_altitude", ground_altitude); + h.putHashable("gps_ground_altitude", gps_ground_altitude); + h.putHashable("ground_pressure", ground_pressure); + h.putHashable("altitude", altitude); + h.putHashable("gps_altitude", gps_altitude); + h.putHashable("gps_ground_speed", gps_ground_speed); + h.putHashable("gps_ascent_rate", gps_ascent_rate); + h.putHashable("gps_course", gps_course); + h.putHashable("gps_speed", gps_speed); + h.putHashable("pressure", pressure); + h.putHashable("speed", speed); + h.putHashable("acceleration", acceleration); + h.putHashable("orient", orient); + h.putHashable("kalman_height", kalman_height); + h.putHashable("kalman_speed", kalman_speed); + h.putHashable("kalman_acceleration", kalman_acceleration); + + h.putDouble("battery_voltage",battery_voltage); + h.putDouble("pyro_voltage",pyro_voltage); + h.putDouble("temperature",temperature); + h.putDouble("apogee_voltage",apogee_voltage); + h.putDouble("main_voltage",main_voltage); + h.putDoubleArray("ignitor_voltage",ignitor_voltage); + h.putHashable("gps", gps); + h.putHashable("temp_gps", temp_gps); + h.putInt("temp_gps_sat_tick", temp_gps_sat_tick); + h.putBoolean("gps_pending", gps_pending); + h.putInt("gps_sequence", gps_sequence); + h.putHashable("imu", imu); + h.putHashable("mag", mag); + + h.putInt("npad", npad); + h.putInt("gps_waiting", gps_waiting); + h.putBoolean("gps_ready", gps_ready); + h.putInt("ngps", ngps); + h.putHashable("from_pad", from_pad); + h.putDouble("elevation", elevation); + h.putDouble("range", range); + h.putDouble("gps_height", gps_height); + h.putDouble("pad_lat", pad_lat); + h.putDouble("pad_lon", pad_lon); + h.putDouble("pad_alt", pad_alt); + h.putInt("speak_tick", speak_tick); + h.putDouble("speak_altitude", speak_altitude); + h.putString("callsign", callsign); + h.putString("firmware_version", firmware_version); + h.putDouble("accel_plus_g", accel_plus_g); + h.putDouble("accel_minus_g", accel_minus_g); + h.putDouble("accel", accel); + h.putDouble("ground_accel", ground_accel); + h.putDouble("ground_accel_avg", ground_accel_avg); + h.putInt("log_format", log_format); + h.putString("product", product); + h.putHashable("baro", baro); + h.putHashable("companion", companion); + h.putInt("pyro_fired", pyro_fired); + h.putDouble("accel_zero_along", accel_zero_along); + h.putDouble("accel_zero_across", accel_zero_across); + h.putDouble("accel_zero_through", accel_zero_through); + + h.putHashable("rotation", rotation); + h.putHashable("ground_rotation", ground_rotation); + + h.putInt("pad_orientation", pad_orientation); + + h.putDouble("accel_ground_along", accel_ground_along); + h.putDouble("accel_ground_across", accel_ground_across); + h.putDouble("accel_ground_through", accel_ground_through); + + h.putDouble("gyro_zero_roll", gyro_zero_roll); + h.putDouble("gyro_zero_pitch", gyro_zero_pitch); + h.putDouble("gyro_zero_yaw", gyro_zero_yaw); + + h.putDouble("last_imu_time", last_imu_time); + return h; + } + + public AltosState(AltosHashSet h) { + this(); + + set = h.getInt("set", set); + received_time = h.getLong("received_time", received_time); + time = h.getDouble("time", time); + prev_time = h.getDouble("prev_time", prev_time); + time_change = h.getDouble("time_change", time_change); + tick = h.getInt("tick", tick); + prev_tick = h.getInt("prev_tick", prev_tick); + boost_tick = h.getInt("boost_tick", boost_tick); + state = h.getInt("state", state); + flight = h.getInt("flight", flight); + serial = h.getInt("serial", serial); + altitude_32 = h.getInt("altitude_32", altitude_32); + receiver_serial = h.getInt("receiver_serial", receiver_serial); + landed = h.getBoolean("landed", landed); + ascent = h.getBoolean("ascent", ascent); + boost = h.getBoolean("boost", boost); + rssi = h.getInt("rssi", rssi); + status = h.getInt("status", status); + device_type = h.getInt("device_type", device_type); + config_major = h.getInt("config_major", config_major); + config_minor = h.getInt("config_minor", config_minor); + apogee_delay = h.getInt("apogee_delay", apogee_delay); + main_deploy = h.getInt("main_deploy", main_deploy); + flight_log_max = h.getInt("flight_log_max", flight_log_max); + ground_altitude = AltosCValue_fromHashSet(h.getHash("ground_altitude"), ground_altitude); + gps_ground_altitude = AltosGpsGroundAltitude_fromHashSet(h.getHash("gps_ground_altitude"), gps_ground_altitude); + ground_pressure = AltosGroundPressure_fromHashSet(h.getHash("ground_pressure"), ground_pressure); + altitude = AltosAltitude_fromHashSet(h.getHash("altitude"), altitude); + gps_altitude = AltosGpsAltitude_fromHashSet(h.getHash("gps_altitude"), gps_altitude); + gps_ground_speed = AltosValue_fromHashSet(h.getHash("gps_ground_speed"), gps_ground_speed); + gps_ascent_rate = AltosValue_fromHashSet(h.getHash("gps_ascent_rate"), gps_ascent_rate); + gps_course = AltosValue_fromHashSet(h.getHash("gps_course"), gps_course); + gps_speed = AltosValue_fromHashSet(h.getHash("gps_speed"), gps_speed); + pressure = AltosPressure_fromHashSet(h.getHash("pressure"), pressure); + speed = AltosSpeed_fromHashSet(h.getHash("speed"), speed); + acceleration = AltosAccel_fromHashSet(h.getHash("acceleration"), acceleration); + orient = AltosCValue_fromHashSet(h.getHash("orient"), orient); + kalman_height = AltosValue_fromHashSet(h.getHash("kalman_height"), kalman_height); + kalman_speed = AltosValue_fromHashSet(h.getHash("kalman_speed"), kalman_speed); + kalman_acceleration = AltosValue_fromHashSet(h.getHash("kalman_acceleration"), kalman_acceleration); + + battery_voltage = h.getDouble("battery_voltage", battery_voltage); + pyro_voltage = h.getDouble("pyro_voltage", pyro_voltage); + temperature = h.getDouble("temperature", temperature); + apogee_voltage = h.getDouble("apogee_voltage", apogee_voltage); + main_voltage= h.getDouble("main_voltage", main_voltage); + ignitor_voltage = h.getDoubleArray("ignitor_voltage", ignitor_voltage); + gps = AltosGPS.fromHashSet(h.getHash("gps"), gps); + temp_gps = AltosGPS.fromHashSet(h.getHash("temp_gps"), temp_gps); + temp_gps_sat_tick = h.getInt("temp_gps_sat_tick", temp_gps_sat_tick); + gps_pending = h.getBoolean("gps_pending", gps_pending); + gps_sequence = h.getInt("gps_sequence", gps_sequence); + imu = AltosIMU.fromHashSet(h.getHash("imu"), imu); + mag = AltosMag.fromHashSet(h.getHash("mag"), mag); + + npad = h.getInt("npad", npad); + gps_waiting = h.getInt("gps_waiting", gps_waiting); + gps_ready = h.getBoolean("gps_ready", gps_ready); + ngps = h.getInt("ngps", ngps); + from_pad = AltosGreatCircle.fromHashSet(h.getHash("from_pad"), from_pad); + elevation = h.getDouble("elevation", elevation); + range = h.getDouble("range", range); + gps_height = h.getDouble("gps_height", gps_height); + pad_lat = h.getDouble("pad_lat", pad_lat); + pad_lon = h.getDouble("pad_lon", pad_lon); + pad_alt = h.getDouble("pad_alt", pad_alt); + speak_tick = h.getInt("speak_tick", speak_tick); + speak_altitude = h.getDouble("speak_altitude", speak_altitude); + callsign = h.getString("callsign", callsign); + firmware_version = h.getString("firmware_version", firmware_version); + accel_plus_g = h.getDouble("accel_plus_g", accel_plus_g); + accel_minus_g = h.getDouble("accel_minus_g", accel_minus_g); + accel = h.getDouble("accel", accel); + ground_accel = h.getDouble("ground_accel", ground_accel); + ground_accel_avg = h.getDouble("ground_accel_avg", ground_accel_avg); + log_format = h.getInt("log_format", log_format); + product = h.getString("product", product); + baro = AltosMs5607.fromHashSet(h.getHash("baro"), baro); + companion = AltosCompanion.fromHashSet(h.getHash("companion"), companion); + pyro_fired = h.getInt("pyro_fired", pyro_fired); + accel_zero_along = h.getDouble("accel_zero_along", accel_zero_along); + accel_zero_across = h.getDouble("accel_zero_across", accel_zero_across); + accel_zero_through = h.getDouble("accel_zero_through", accel_zero_through); + + rotation = AltosRotation.fromHashSet(h.getHash("rotation"), rotation); + ground_rotation = AltosRotation.fromHashSet(h.getHash("ground_rotation"), ground_rotation); + + pad_orientation = h.getInt("pad_orientation", pad_orientation); + + accel_ground_along = h.getDouble("accel_ground_along", accel_ground_along); + accel_ground_across = h.getDouble("accel_ground_across", accel_ground_across); + accel_ground_through = h.getDouble("accel_ground_through", accel_ground_through); + + gyro_zero_roll = h.getDouble("gyro_zero_roll", gyro_zero_roll); + gyro_zero_pitch = h.getDouble("gyro_zero_pitch", gyro_zero_pitch); + gyro_zero_yaw = h.getDouble("gyro_zero_yaw", gyro_zero_yaw); + + last_imu_time = h.getDouble("last_imu_time", last_imu_time); + } + + public static AltosState fromHashSet(AltosHashSet h) { + if (h == null) + return null; + if (!h.getBoolean("valid", false)) + return null; + return new AltosState(h); + } } diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index 512e1cca..edc443b0 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -161,6 +161,7 @@ altoslib_JAVA = \ AltosMapLoader.java \ AltosMapTypeListener.java \ AltosHashSet.java \ + AltosHashable.java \ AltosVersion.java JAR=altoslib_$(ALTOSLIB_VERSION).jar diff --git a/altosui/AltosLaunch.java b/altosui/AltosLaunch.java index 46a29d45..fb2cd883 100644 --- a/altosui/AltosLaunch.java +++ b/altosui/AltosLaunch.java @@ -91,7 +91,7 @@ public class AltosLaunch { throw new TimeoutException(); if (get_string(line, "Rssi: ", status_name)) { try { - rssi = Altos.fromdec(status_name.get()); + rssi = (int) Altos.fromdec(status_name.get()); } catch (NumberFormatException ne) { } break; @@ -194,4 +194,4 @@ public class AltosLaunch { device = in_device; serial = new AltosSerial(device); } -} \ No newline at end of file +} -- cgit v1.2.3 From 1b5ea911049a8afae6af475a4a2bf62a6e3aa57b Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 15 Jun 2016 22:40:27 -0700 Subject: altoslib: Switch preserved state format to JSON This is much easier to debug than the icky strings with backslashes everywhere. Signed-off-by: Keith Packard --- altoslib/AltosCompanion.java | 28 ++- altoslib/AltosFrequency.java | 21 +- altoslib/AltosGPS.java | 63 +++++- altoslib/AltosGPSSat.java | 25 ++- altoslib/AltosGreatCircle.java | 29 ++- altoslib/AltosIMU.java | 33 ++- altoslib/AltosMag.java | 26 ++- altoslib/AltosMs5607.java | 44 +++- altoslib/AltosParse.java | 26 ++- altoslib/AltosPreferences.java | 34 ++-- altoslib/AltosPreferencesBackend.java | 16 ++ altoslib/AltosQuaternion.java | 22 +- altoslib/AltosRotation.java | 17 +- altoslib/AltosState.java | 373 ++++++++++++++++++++++++++++++++-- altoslib/Makefile.am | 2 + 15 files changed, 716 insertions(+), 43 deletions(-) (limited to 'altoslib/AltosFrequency.java') diff --git a/altoslib/AltosCompanion.java b/altoslib/AltosCompanion.java index 6f18d93e..abb98830 100644 --- a/altoslib/AltosCompanion.java +++ b/altoslib/AltosCompanion.java @@ -19,7 +19,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosCompanion implements AltosHashable { +public class AltosCompanion implements AltosHashable, AltosJsonable { public final static int board_id_telescience = 0x0a; public final static int MAX_CHANNELS = 12; @@ -49,6 +49,17 @@ public class AltosCompanion implements AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("tick", tick); + j.put("board_id", board_id); + j.put("update_period", update_period); + j.put("channels", channels); + j.put("companion_data", companion_data); + return j; + } + public AltosCompanion(AltosHashSet h) { tick = h.getInt("tick", tick); board_id = h.getInt("board_id", board_id); @@ -63,4 +74,19 @@ public class AltosCompanion implements AltosHashable { return new AltosCompanion(h); } + + public AltosCompanion(AltosJson j) { + tick = j.get_int("tick", tick); + board_id = j.get_int("board_id", board_id); + update_period = j.get_int("update_period", update_period); + channels = j.get_int("channels", channels); + companion_data = j.get_int_array("companion_data", new int[channels]); + } + + public static AltosCompanion fromJson(AltosJson j, AltosCompanion def) { + if (j == null) + return def; + + return new AltosCompanion(j); + } } diff --git a/altoslib/AltosFrequency.java b/altoslib/AltosFrequency.java index f9aa6de6..99828d53 100644 --- a/altoslib/AltosFrequency.java +++ b/altoslib/AltosFrequency.java @@ -21,7 +21,7 @@ import java.io.*; import java.util.*; import java.text.*; -public class AltosFrequency { +public class AltosFrequency implements AltosJsonable { public double frequency; public String description; @@ -66,6 +66,14 @@ public class AltosFrequency { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("frequency", frequency); + j.put("description", description); + return j; + } + public AltosFrequency(double f, String d) { frequency = f; description = d; @@ -81,4 +89,15 @@ public class AltosFrequency { return def; return new AltosFrequency(h); } + + private AltosFrequency(AltosJson j) { + frequency = j.get_double("frequency", 0.0); + description = j.get_string("description", ""); + } + + public static AltosFrequency fromJson(AltosJson j, AltosFrequency def) { + if (j == null) + return def; + return new AltosFrequency(j); + } } diff --git a/altoslib/AltosGPS.java b/altoslib/AltosGPS.java index 371fd7bf..d3710e4e 100644 --- a/altoslib/AltosGPS.java +++ b/altoslib/AltosGPS.java @@ -21,7 +21,7 @@ import java.text.*; import java.util.concurrent.*; import java.io.*; -public class AltosGPS implements Cloneable, AltosHashable { +public class AltosGPS implements Cloneable, AltosHashable, AltosJsonable { public final static int MISSING = AltosLib.MISSING; @@ -417,6 +417,34 @@ public class AltosGPS implements Cloneable, AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("nsat", nsat); + j.put("locked", locked); + j.put("connected", connected); + j.put("lat", lat); + j.put("lon", lon); + j.put("alt", alt); + j.put("year", year); + j.put("month", month); + j.put("day", day); + j.put("hour", hour); + j.put("minute", minute); + j.put("second", second); + + j.put("ground_speed", ground_speed); + j.put("course", course); + j.put("climb_rate", climb_rate); + j.put("pdop", pdop); + j.put("hdop", hdop); + j.put("vdop", vdop); + j.put("h_error", h_error); + j.put("v_error", v_error); + j.put("cc_gps_sat", cc_gps_sat); + return j; + } + public AltosGPS(AltosHashSet h) { init(); nsat = h.getInt("nsat", nsat); @@ -443,10 +471,43 @@ public class AltosGPS implements Cloneable, AltosHashable { cc_gps_sat = AltosGPSSat.array(h.getString("cc_gps_sat", null)); } + public AltosGPS(AltosJson j) { + init(); + nsat = j.get_int("nsat", nsat); + locked = j.get_boolean("locked", locked); + connected = j.get_boolean("connected", connected); + lat = j.get_double("lat", lat); + lon = j.get_double("lon", lon); + alt = j.get_double("alt", alt); + year = j.get_int("year", year); + month = j.get_int("month", month); + day = j.get_int("day", day); + hour = j.get_int("hour", hour); + minute = j.get_int("minute", minute); + second = j.get_int("second", second); + + ground_speed = j.get_double("ground_speed", ground_speed); + course = j.get_int("course", course); + climb_rate = j.get_double("climb_rate", climb_rate); + pdop = j.get_double("pdop", pdop); + hdop = j.get_double("hdop", hdop); + vdop = j.get_double("vdop", vdop); + h_error = j.get_double("h_error", h_error); + v_error = j.get_double("v_error", v_error); + cc_gps_sat = AltosGPSSat.json_array(j.get("cc_gps_sat")); + } + public static AltosGPS fromHashSet(AltosHashSet h, AltosGPS def) { if (h == null) return def; return new AltosGPS(h); } + + public static AltosGPS fromJson(AltosJson j, AltosGPS def) { + if (j == null) + return def; + + return new AltosGPS(j); + } } diff --git a/altoslib/AltosGPSSat.java b/altoslib/AltosGPSSat.java index ad7a8647..319fe7f1 100644 --- a/altoslib/AltosGPSSat.java +++ b/altoslib/AltosGPSSat.java @@ -22,7 +22,7 @@ import java.text.*; import java.util.*; import java.util.concurrent.*; -public class AltosGPSSat { +public class AltosGPSSat implements AltosJsonable { public int svid; public int c_n0; @@ -41,17 +41,40 @@ public class AltosGPSSat { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + j.put("svid", svid); + j.put("c_n0", c_n0); + return j; + } + private AltosGPSSat(AltosHashSet h) { svid = h.getInt("svid", 0); c_n0 = h.getInt("c_n0", 0); } + private AltosGPSSat(AltosJson j) { + svid = j.get_int("svid", 0); + c_n0 = j.get_int("c_n0", 0); + } + static public AltosGPSSat fromHashSet(AltosHashSet h, AltosGPSSat def) { if (h == null) return def; return new AltosGPSSat(h); } + static public AltosGPSSat[] json_array(AltosJson j) { + if (j == null) + return null; + + int size = j.size(); + AltosGPSSat[] sats = new AltosGPSSat[size]; + for (int i = 0; i < size; i++) + sats[i] = new AltosGPSSat(j.get(i)); + return sats; + } + static public AltosGPSSat[] array(String string) { if (string == null) diff --git a/altoslib/AltosGreatCircle.java b/altoslib/AltosGreatCircle.java index 9ec808a5..8fd380a1 100644 --- a/altoslib/AltosGreatCircle.java +++ b/altoslib/AltosGreatCircle.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.lang.Math; import java.io.*; -public class AltosGreatCircle implements Cloneable, AltosHashable { +public class AltosGreatCircle implements Cloneable, AltosHashable, AltosJsonable { public double distance; public double bearing; public double range; @@ -115,6 +115,17 @@ public class AltosGreatCircle implements Cloneable, AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("distance", distance); + j.put("bearing", bearing); + j.put("range", range); + j.put("elevation", elevation); + + return j; + } + public AltosGreatCircle(AltosHashSet h) { this(); @@ -130,4 +141,20 @@ public class AltosGreatCircle implements Cloneable, AltosHashable { return new AltosGreatCircle(h); } + + public AltosGreatCircle(AltosJson j) { + this(); + + distance = j.get_double("distance", distance); + bearing = j.get_double("bearing", bearing); + range = j.get_double("range", range); + elevation = j.get_double("elevation", elevation); + } + + public static AltosGreatCircle fromJson(AltosJson j, AltosGreatCircle def) { + if (j == null) + return def; + + return new AltosGreatCircle(j); + } } diff --git a/altoslib/AltosIMU.java b/altoslib/AltosIMU.java index df6c4ed3..ecc02f15 100644 --- a/altoslib/AltosIMU.java +++ b/altoslib/AltosIMU.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosIMU implements Cloneable, AltosHashable { +public class AltosIMU implements Cloneable, AltosHashable, AltosJsonable { public int accel_along; public int accel_across; public int accel_through; @@ -128,12 +128,30 @@ public class AltosIMU implements Cloneable, AltosHashable { gyro_yaw = h.getInt("gyro_yaw", gyro_yaw); } + public AltosIMU (AltosJson j) { + this(); + + accel_along = j.get_int("accel_along", accel_along); + accel_across = j.get_int("accel_across", accel_across); + accel_through = j.get_int("accel_through", accel_through); + + gyro_roll = j.get_int("gyro_roll", gyro_roll); + gyro_pitch = j.get_int("gyro_pitch", gyro_pitch); + gyro_yaw = j.get_int("gyro_yaw", gyro_yaw); + } + static public AltosIMU fromHashSet(AltosHashSet h, AltosIMU def) { if (h == null) return def; return new AltosIMU(h); } + static public AltosIMU fromJson(AltosJson j, AltosIMU def) { + if (j == null) + return def; + return new AltosIMU(j); + } + public AltosHashSet hashSet() { AltosHashSet h = new AltosHashSet(); @@ -146,4 +164,17 @@ public class AltosIMU implements Cloneable, AltosHashable { h.putInt("gyro_yaw", gyro_yaw); return h; } + + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("accel_along", accel_along); + j.put("accel_across", accel_across); + j.put("accel_through", accel_through); + + j.put("gyro_roll", gyro_roll); + j.put("gyro_pitch", gyro_pitch); + j.put("gyro_yaw", gyro_yaw); + return j; + } } diff --git a/altoslib/AltosMag.java b/altoslib/AltosMag.java index c350ae46..ec98882f 100644 --- a/altoslib/AltosMag.java +++ b/altoslib/AltosMag.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMag implements Cloneable, AltosHashable { +public class AltosMag implements Cloneable, AltosHashable, AltosJsonable { public int along; public int across; public int through; @@ -103,6 +103,15 @@ public class AltosMag implements Cloneable, AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("along", along); + j.put("across", across); + j.put("through", through); + return j; + } + public AltosMag(AltosHashSet h) { this(); @@ -117,4 +126,19 @@ public class AltosMag implements Cloneable, AltosHashable { return new AltosMag(h); } + + public AltosMag(AltosJson j) { + this(); + + along = j.get_int("along", along); + across = j.get_int("across", across); + through = j.get_int("through", through); + } + + public static AltosMag fromJson(AltosJson j, AltosMag def) { + if (j == null) + return def; + + return new AltosMag(j); + } } diff --git a/altoslib/AltosMs5607.java b/altoslib/AltosMs5607.java index 88a97828..6d2f2203 100644 --- a/altoslib/AltosMs5607.java +++ b/altoslib/AltosMs5607.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMs5607 implements AltosHashable { +public class AltosMs5607 implements AltosHashable, AltosJsonable { public int reserved; public int sens; public int off; @@ -185,6 +185,24 @@ public class AltosMs5607 implements AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("reserved", reserved); + j.put("sens", sens); + j.put("off", off); + j.put("tcs", tcs); + j.put("tco", tco); + j.put("tref", tref); + j.put("tempsens", tempsens); + j.put("crc", crc); + j.put("raw_pres", raw_pres); + j.put("raw_temp", raw_temp); + j.put("pa", pa); + j.put("cc", cc); + return j; + } + public AltosMs5607(AltosHashSet h) { this(); @@ -208,4 +226,28 @@ public class AltosMs5607 implements AltosHashable { return new AltosMs5607(h); } + + public AltosMs5607(AltosJson j) { + this(); + + reserved = j.get_int("reserved", reserved); + sens = j.get_int("sens", sens); + off = j.get_int("off", off); + tcs = j.get_int("tcs", tcs); + tco = j.get_int("tco", tco); + tref = j.get_int("tref", tref); + tempsens = j.get_int("tempsens", tempsens); + crc = j.get_int("crc", crc); + raw_pres = j.get_int("raw_pres", raw_pres); + raw_temp = j.get_int("raw_temp", raw_temp); + pa = j.get_int("pa", pa); + cc = j.get_int("cc", cc); + } + + public static AltosMs5607 fromJson(AltosJson j, AltosMs5607 def) { + if (j == null) + return def; + + return new AltosMs5607(j); + } } diff --git a/altoslib/AltosParse.java b/altoslib/AltosParse.java index fbd049ae..45ef7dac 100644 --- a/altoslib/AltosParse.java +++ b/altoslib/AltosParse.java @@ -49,9 +49,23 @@ public class AltosParse { } } - static NumberFormat nf_locale = NumberFormat.getInstance(); + static NumberFormat get_nf_locale() { + NumberFormat nf = NumberFormat.getInstance(); + nf.setParseIntegerOnly(false); + nf.setGroupingUsed(false); + return nf; + } + + static NumberFormat nf_locale = get_nf_locale(); + + static NumberFormat get_nf_net() { + NumberFormat nf = NumberFormat.getInstance(Locale.ROOT); + nf.setParseIntegerOnly(false); + nf.setGroupingUsed(false); + return nf; + } - static NumberFormat nf_net = NumberFormat.getInstance(Locale.ROOT); + static NumberFormat nf_net = get_nf_net(); public static double parse_double_locale(String str) throws ParseException { try { @@ -67,14 +81,18 @@ public class AltosParse { public static double parse_double_net(String str) throws ParseException { try { - return nf_net.parse(str.trim()).doubleValue(); + String t = str.trim(); +// System.out.printf("Parse string \"%s\" trim \"%s\"\n", str, t); + return nf_net.parse(t).doubleValue(); } catch (ParseException pe) { throw new ParseException("error parsing double " + str, 0); } } public static String format_double_net(double number) { - return nf_net.format(number); + String ret = nf_net.format(number); +// System.out.printf("format double %f \"%s\"\n", number, ret); + return ret; } public static double parse_coord(String coord) throws ParseException { diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index 3f8e7a07..8e625674 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -133,19 +133,25 @@ public class AltosPreferences { static int map_type; public static AltosFrequency[] load_common_frequencies() { - AltosFrequency[] frequencies = null; - AltosHashSet[] sets = AltosHashSet.array(backend.getString(frequenciesPreference,null)); - if (sets != null) { - ArrayList freqs = new ArrayList(); + AltosJson sets; + + try { + sets = AltosJson.fromString(backend.getString(frequenciesPreference,null)); - for (int i = 0; i < sets.length; i++) { - AltosFrequency f = AltosFrequency.fromHashSet(sets[i], null); - if (f != null) - freqs.add(f); + if (sets != null) { + ArrayList freqs = new ArrayList(); + + for (int i = 0; i < sets.size(); i++) { + AltosFrequency f = AltosFrequency.fromJson(sets.get(i), null); + if (f != null) + freqs.add(f); + } + frequencies = freqs.toArray(new AltosFrequency[0]); } - frequencies = freqs.toArray(new AltosFrequency[0]); + } catch (Exception e) { + sets = null; } if (frequencies == null) { @@ -178,10 +184,8 @@ public class AltosPreferences { } public static void save_common_frequencies() { - AltosHashSet[] sets = new AltosHashSet[common_frequencies.length]; - for (int i = 0; i < sets.length; i++) - sets[i] = common_frequencies[i].hashSet(); - backend.putString(frequenciesPreference, AltosHashSet.toString(sets)); + AltosJson json = new AltosJson(common_frequencies); + backend.putString(frequenciesPreference, json.toString()); flush_preferences(); } @@ -373,7 +377,7 @@ public class AltosPreferences { public static void set_state(AltosState state) { synchronized(backend) { - backend.putHashSet(String.format(statePreferenceFormat, state.serial), state.hashSet()); + backend.putJson(String.format(statePreferenceFormat, state.serial), state.json()); backend.putInt(statePreferenceLatest, state.serial); flush_preferences(); } @@ -413,7 +417,7 @@ public class AltosPreferences { public static AltosState state(int serial) { synchronized(backend) { try { - return AltosState.fromHashSet(backend.getHashSet(String.format(statePreferenceFormat, serial))); + return AltosState.fromJson(backend.getJson(String.format(statePreferenceFormat, serial))); } catch (Exception e) { return null; } diff --git a/altoslib/AltosPreferencesBackend.java b/altoslib/AltosPreferencesBackend.java index 9131ad39..d4c3d7e5 100644 --- a/altoslib/AltosPreferencesBackend.java +++ b/altoslib/AltosPreferencesBackend.java @@ -50,6 +50,22 @@ public abstract class AltosPreferencesBackend { putString(key, h.toString()); } + public AltosJson getJson(String key) { + String value = getString(key, null); + + if (value == null) + return null; + try { + return AltosJson.fromString(value); + } catch (IllegalArgumentException ie) { + return null; + } + } + + public void putJson(String key, AltosJson j) { + putString(key, j.toString()); + } + public abstract boolean nodeExists(String key); public abstract AltosPreferencesBackend node(String key); diff --git a/altoslib/AltosQuaternion.java b/altoslib/AltosQuaternion.java index af9eb475..6a09bb8e 100644 --- a/altoslib/AltosQuaternion.java +++ b/altoslib/AltosQuaternion.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosQuaternion implements AltosHashable { +public class AltosQuaternion implements AltosHashable, AltosJsonable { double r; /* real bit */ double x, y, z; /* imaginary bits */ @@ -158,6 +158,16 @@ public class AltosQuaternion implements AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("r", r); + j.put("x", x); + j.put("y", y); + j.put("z", z); + return j; + } + public AltosQuaternion(AltosHashSet h) { if (h == null) return; @@ -167,4 +177,14 @@ public class AltosQuaternion implements AltosHashable { y = h.getDouble("y", 0); z = h.getDouble("z", 0); } + + public AltosQuaternion(AltosJson j) { + if (j == null) + return; + + r = j.get_double("r", 1); + x = j.get_double("x", 0); + y = j.get_double("y", 0); + z = j.get_double("z", 0); + } } diff --git a/altoslib/AltosRotation.java b/altoslib/AltosRotation.java index e9c447ad..4b7ab407 100644 --- a/altoslib/AltosRotation.java +++ b/altoslib/AltosRotation.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosRotation implements AltosHashable { +public class AltosRotation implements AltosHashable, AltosJsonable { private AltosQuaternion rotation; public double tilt() { @@ -55,6 +55,10 @@ public class AltosRotation implements AltosHashable { return h; } + public AltosJson json() { + return rotation.json(); + } + public AltosRotation(AltosHashSet h) { rotation = new AltosQuaternion(h.getHash("rotation")); } @@ -65,4 +69,15 @@ public class AltosRotation implements AltosHashable { return new AltosRotation(h); } + + public AltosRotation(AltosJson j) { + rotation = new AltosQuaternion(j); + } + + public static AltosRotation fromJson(AltosJson j, AltosRotation def) { + if (j == null) + return def; + + return new AltosRotation(j); + } } diff --git a/altoslib/AltosState.java b/altoslib/AltosState.java index d359d67a..a3c6cc34 100644 --- a/altoslib/AltosState.java +++ b/altoslib/AltosState.java @@ -46,7 +46,7 @@ public class AltosState implements Cloneable, AltosHashable { private int prev_tick; public int boost_tick; - class AltosValue implements AltosHashable { + class AltosValue implements AltosHashable, AltosJsonable { double value; double prev_value; private double max_value; @@ -188,6 +188,17 @@ public class AltosState implements Cloneable, AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("value", value); + j.put("prev_value", prev_value); + j.put("max_value", max_value); + j.put("set_time", set_time); + j.put("prev_set_time", prev_set_time); + return j; + } + AltosValue(AltosHashSet h) { this(); if (h != null) { @@ -199,6 +210,17 @@ public class AltosState implements Cloneable, AltosHashable { } } + AltosValue(AltosJson j) { + this(); + if (j != null) { + value = j.get_double("value", value); + prev_value = j.get_double("prev_value", prev_value); + max_value = j.get_double("max_value", max_value); + set_time = j.get_double("set_time", 0); + prev_set_time = j.get_double("prev_set_time", 0); + } + } + AltosValue() { value = AltosLib.MISSING; prev_value = AltosLib.MISSING; @@ -213,9 +235,15 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosValue(h); } - class AltosCValue implements AltosHashable { + AltosValue AltosValue_fromJson(AltosJson j, AltosValue def) { + if (j == null) + return def; + return new AltosValue(j); + } + + class AltosCValue implements AltosHashable, AltosJsonable { - class AltosIValue extends AltosValue implements AltosHashable { + class AltosIValue extends AltosValue implements AltosHashable, AltosJsonable { boolean can_max() { return c_can_max(); } @@ -227,6 +255,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosIValue(AltosHashSet h) { super(h); } + + AltosIValue(AltosJson j) { + super(j); + } }; public AltosIValue measured; @@ -320,7 +352,6 @@ public class AltosState implements Cloneable, AltosHashable { computed = new AltosIValue(); } - public AltosHashSet hashSet() { AltosHashSet h = new AltosHashSet(); @@ -329,10 +360,23 @@ public class AltosState implements Cloneable, AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("measured", measured.json()); + j.put("computed", computed.json()); + return j; + } + AltosCValue(AltosHashSet h) { measured = new AltosIValue(h.getHash("measured")); computed = new AltosIValue(h.getHash("computed")); } + + AltosCValue(AltosJson j) { + measured = new AltosIValue(j.get("measured")); + computed = new AltosIValue(j.get("computed")); + } } AltosCValue AltosCValue_fromHashSet(AltosHashSet h, AltosCValue def) { @@ -341,6 +385,12 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosCValue(h); } + AltosCValue AltosCValue_fromJson(AltosJson j, AltosCValue def) { + if (j == null) + return def; + return new AltosCValue(j); + } + private int state; public int flight; public int serial; @@ -394,6 +444,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosGpsGroundAltitude (AltosHashSet h) { super(h); } + + AltosGpsGroundAltitude (AltosJson j) { + super(j); + } } AltosGpsGroundAltitude AltosGpsGroundAltitude_fromHashSet(AltosHashSet h, AltosGpsGroundAltitude def) { @@ -401,6 +455,11 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosGpsGroundAltitude(h); } + AltosGpsGroundAltitude AltosGpsGroundAltitude_fromJson(AltosJson j, AltosGpsGroundAltitude def) { + if (j == null) return def; + return new AltosGpsGroundAltitude(j); + } + private AltosGpsGroundAltitude gps_ground_altitude; public double gps_ground_altitude() { @@ -430,6 +489,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosGroundPressure (AltosHashSet h) { super(h); } + + AltosGroundPressure (AltosJson j) { + super(j); + } } AltosGroundPressure AltosGroundPressure_fromHashSet(AltosHashSet h, AltosGroundPressure def) { @@ -437,6 +500,11 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosGroundPressure(h); } + AltosGroundPressure AltosGroundPressure_fromJson(AltosJson j, AltosGroundPressure def) { + if (j == null) return def; + return new AltosGroundPressure(j); + } + private AltosGroundPressure ground_pressure; public double ground_pressure() { @@ -473,6 +541,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosAltitude (AltosHashSet h) { super(h); } + + AltosAltitude (AltosJson j) { + super(j); + } } AltosAltitude AltosAltitude_fromHashSet(AltosHashSet h, AltosAltitude def) { @@ -480,6 +552,11 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosAltitude(h); } + AltosAltitude AltosAltitude_fromJson(AltosJson j, AltosAltitude def) { + if (j == null) return def; + return new AltosAltitude(j); + } + private AltosAltitude altitude; class AltosGpsAltitude extends AltosValue implements AltosHashable { @@ -506,6 +583,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosGpsAltitude (AltosHashSet h) { super(h); } + + AltosGpsAltitude (AltosJson j) { + super(j); + } } AltosGpsAltitude AltosGpsAltitude_fromHashSet(AltosHashSet h, AltosGpsAltitude def) { @@ -513,6 +594,11 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosGpsAltitude(h); } + AltosGpsAltitude AltosGpsAltitude_fromJson(AltosJson j, AltosGpsAltitude def) { + if (j == null) return def; + return new AltosGpsAltitude(j); + } + private AltosGpsAltitude gps_altitude; private AltosValue gps_ground_speed; @@ -594,6 +680,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosPressure (AltosHashSet h) { super(h); } + + AltosPressure (AltosJson j) { + super(j); + } } AltosPressure AltosPressure_fromHashSet(AltosHashSet h, AltosPressure def) { @@ -601,6 +691,11 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosPressure(h); } + AltosPressure AltosPressure_fromJson(AltosJson j, AltosPressure def) { + if (j == null) return def; + return new AltosPressure(j); + } + private AltosPressure pressure; public double pressure() { @@ -693,6 +788,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosSpeed (AltosHashSet h) { super(h); } + + AltosSpeed (AltosJson j) { + super(j); + } } AltosSpeed AltosSpeed_fromHashSet(AltosHashSet h, AltosSpeed def) { @@ -700,6 +799,11 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosSpeed(h); } + AltosSpeed AltosSpeed_fromJson(AltosJson j, AltosSpeed def) { + if (j == null) return def; + return new AltosSpeed(j); + } + private AltosSpeed speed; public double speed() { @@ -747,6 +851,10 @@ public class AltosState implements Cloneable, AltosHashable { AltosAccel (AltosHashSet h) { super(h); } + + AltosAccel (AltosJson j) { + super(j); + } } AltosAccel AltosAccel_fromHashSet(AltosHashSet h, AltosAccel def) { @@ -754,6 +862,11 @@ public class AltosState implements Cloneable, AltosHashable { return new AltosAccel(h); } + AltosAccel AltosAccel_fromJson(AltosJson j, AltosAccel def) { + if (j == null) return def; + return new AltosAccel(j); + } + AltosAccel acceleration; public double acceleration() { @@ -1639,16 +1752,31 @@ public class AltosState implements Cloneable, AltosHashable { AltosState s = new AltosState(); s.copy(this); - AltosHashSet hash = hashSet(); - String onetrip = hash.toString(); - AltosHashSet back = AltosHashSet.fromString(onetrip); - AltosState tripstate = AltosState.fromHashSet(back); - AltosHashSet triphash = tripstate.hashSet(); - String twotrip = triphash.toString(); - - if (!onetrip.equals(twotrip)) { - System.out.printf("%s\n%s\n", onetrip, twotrip); - System.exit(1); + if (false) { + AltosJson json = json(); + String onetrip = json.toPrettyString(); + AltosJson back = AltosJson.fromString(onetrip); + AltosState tripstate = AltosState.fromJson(back); + AltosJson tripjson = tripstate.json(); + String twotrip = tripjson.toPrettyString(); + + if (!onetrip.equals(twotrip)) { + System.out.printf("one:\n%s\ntwo:\n%s\n", onetrip, twotrip); + System.exit(1); + } + } + if (false) { + AltosHashSet hash = hashSet(); + String onetrip = hash.toString(); + AltosHashSet back = AltosHashSet.fromString(onetrip); + AltosState tripstate = AltosState.fromHashSet(back); + AltosHashSet triphash = tripstate.hashSet(); + String twotrip = triphash.toString(); + + if (!onetrip.equals(twotrip)) { + System.out.printf("%s\n%s\n", onetrip, twotrip); + System.exit(1); + } } return s; } @@ -1763,6 +1891,112 @@ public class AltosState implements Cloneable, AltosHashable { return h; } + public AltosJson json() { + AltosJson j = new AltosJson(); + + j.put("valid", true); + j.put("set", set); + j.put("received_time", received_time); + j.put("time", time); + j.put("prev_time", prev_time); + j.put("time_change", time_change); + j.put("tick", tick); + j.put("prev_tick", prev_tick); + j.put("boost_tick", boost_tick); + j.put("state", state); + j.put("flight", flight); + j.put("serial", serial); + j.put("altitude_32", altitude_32); + j.put("receiver_serial", receiver_serial); + j.put("landed", landed); + j.put("ascent", ascent); + j.put("boost", boost); + j.put("rssi", rssi); + j.put("status", status); + j.put("device_type", device_type); + j.put("config_major", config_major); + j.put("config_minor", config_minor); + j.put("apogee_delay", apogee_delay); + j.put("main_deploy", main_deploy); + j.put("flight_log_max", flight_log_max); + j.put("ground_altitude", ground_altitude); + j.put("gps_ground_altitude", gps_ground_altitude); + j.put("ground_pressure", ground_pressure); + j.put("altitude", altitude); + j.put("gps_altitude", gps_altitude); + j.put("gps_ground_speed", gps_ground_speed); + j.put("gps_ascent_rate", gps_ascent_rate); + j.put("gps_course", gps_course); + j.put("gps_speed", gps_speed); + j.put("pressure", pressure); + j.put("speed", speed); + j.put("acceleration", acceleration); + j.put("orient", orient); + j.put("kalman_height", kalman_height); + j.put("kalman_speed", kalman_speed); + j.put("kalman_acceleration", kalman_acceleration); + + j.put("battery_voltage",battery_voltage); + j.put("pyro_voltage",pyro_voltage); + j.put("temperature",temperature); + j.put("apogee_voltage",apogee_voltage); + j.put("main_voltage",main_voltage); + j.put("ignitor_voltage",ignitor_voltage); + j.put("gps", gps); + j.put("temp_gps", temp_gps); + j.put("temp_gps_sat_tick", temp_gps_sat_tick); + j.put("gps_pending", gps_pending); + j.put("gps_sequence", gps_sequence); + j.put("imu", imu); + j.put("mag", mag); + + j.put("npad", npad); + j.put("gps_waiting", gps_waiting); + j.put("gps_ready", gps_ready); + j.put("ngps", ngps); + j.put("from_pad", from_pad); + j.put("elevation", elevation); + j.put("range", range); + j.put("gps_height", gps_height); + j.put("pad_lat", pad_lat); + j.put("pad_lon", pad_lon); + j.put("pad_alt", pad_alt); + j.put("speak_tick", speak_tick); + j.put("speak_altitude", speak_altitude); + j.put("callsign", callsign); + j.put("firmware_version", firmware_version); + j.put("accel_plus_g", accel_plus_g); + j.put("accel_minus_g", accel_minus_g); + j.put("accel", accel); + j.put("ground_accel", ground_accel); + j.put("ground_accel_avg", ground_accel_avg); + j.put("log_format", log_format); + j.put("log_space", log_space); + j.put("product", product); + j.put("baro", baro); + j.put("companion", companion); + j.put("pyro_fired", pyro_fired); + j.put("accel_zero_along", accel_zero_along); + j.put("accel_zero_across", accel_zero_across); + j.put("accel_zero_through", accel_zero_through); + + j.put("rotation", rotation); + j.put("ground_rotation", ground_rotation); + + j.put("pad_orientation", pad_orientation); + + j.put("accel_ground_along", accel_ground_along); + j.put("accel_ground_across", accel_ground_across); + j.put("accel_ground_through", accel_ground_through); + + j.put("gyro_zero_roll", gyro_zero_roll); + j.put("gyro_zero_pitch", gyro_zero_pitch); + j.put("gyro_zero_yaw", gyro_zero_yaw); + + j.put("last_imu_time", last_imu_time); + return j; + } + public AltosState(AltosHashSet h) { this(); @@ -1867,6 +2101,109 @@ public class AltosState implements Cloneable, AltosHashable { last_imu_time = h.getDouble("last_imu_time", last_imu_time); } + public AltosState(AltosJson j) { + this(); + + set = j.get_int("set", set); + received_time = j.get_long("received_time", received_time); + time = j.get_double("time", time); + prev_time = j.get_double("prev_time", prev_time); + time_change = j.get_double("time_change", time_change); + tick = j.get_int("tick", tick); + prev_tick = j.get_int("prev_tick", prev_tick); + boost_tick = j.get_int("boost_tick", boost_tick); + state = j.get_int("state", state); + flight = j.get_int("flight", flight); + serial = j.get_int("serial", serial); + altitude_32 = j.get_int("altitude_32", altitude_32); + receiver_serial = j.get_int("receiver_serial", receiver_serial); + landed = j.get_boolean("landed", landed); + ascent = j.get_boolean("ascent", ascent); + boost = j.get_boolean("boost", boost); + rssi = j.get_int("rssi", rssi); + status = j.get_int("status", status); + device_type = j.get_int("device_type", device_type); + config_major = j.get_int("config_major", config_major); + config_minor = j.get_int("config_minor", config_minor); + apogee_delay = j.get_int("apogee_delay", apogee_delay); + main_deploy = j.get_int("main_deploy", main_deploy); + flight_log_max = j.get_int("flight_log_max", flight_log_max); + ground_altitude = AltosCValue_fromJson(j.get("ground_altitude"), ground_altitude); + gps_ground_altitude = AltosGpsGroundAltitude_fromJson(j.get("gps_ground_altitude"), gps_ground_altitude); + ground_pressure = AltosGroundPressure_fromJson(j.get("ground_pressure"), ground_pressure); + altitude = AltosAltitude_fromJson(j.get("altitude"), altitude); + gps_altitude = AltosGpsAltitude_fromJson(j.get("gps_altitude"), gps_altitude); + gps_ground_speed = AltosValue_fromJson(j.get("gps_ground_speed"), gps_ground_speed); + gps_ascent_rate = AltosValue_fromJson(j.get("gps_ascent_rate"), gps_ascent_rate); + gps_course = AltosValue_fromJson(j.get("gps_course"), gps_course); + gps_speed = AltosValue_fromJson(j.get("gps_speed"), gps_speed); + pressure = AltosPressure_fromJson(j.get("pressure"), pressure); + speed = AltosSpeed_fromJson(j.get("speed"), speed); + acceleration = AltosAccel_fromJson(j.get("acceleration"), acceleration); + orient = AltosCValue_fromJson(j.get("orient"), orient); + kalman_height = AltosValue_fromJson(j.get("kalman_height"), kalman_height); + kalman_speed = AltosValue_fromJson(j.get("kalman_speed"), kalman_speed); + kalman_acceleration = AltosValue_fromJson(j.get("kalman_acceleration"), kalman_acceleration); + + battery_voltage = j.get_double("battery_voltage", battery_voltage); + pyro_voltage = j.get_double("pyro_voltage", pyro_voltage); + temperature = j.get_double("temperature", temperature); + apogee_voltage = j.get_double("apogee_voltage", apogee_voltage); + main_voltage= j.get_double("main_voltage", main_voltage); + ignitor_voltage = j.get_double_array("ignitor_voltage", ignitor_voltage); + gps = AltosGPS.fromJson(j.get("gps"), gps); + temp_gps = AltosGPS.fromJson(j.get("temp_gps"), temp_gps); + temp_gps_sat_tick = j.get_int("temp_gps_sat_tick", temp_gps_sat_tick); + gps_pending = j.get_boolean("gps_pending", gps_pending); + gps_sequence = j.get_int("gps_sequence", gps_sequence); + imu = AltosIMU.fromJson(j.get("imu"), imu); + mag = AltosMag.fromJson(j.get("mag"), mag); + + npad = j.get_int("npad", npad); + gps_waiting = j.get_int("gps_waiting", gps_waiting); + gps_ready = j.get_boolean("gps_ready", gps_ready); + ngps = j.get_int("ngps", ngps); + from_pad = AltosGreatCircle.fromJson(j.get("from_pad"), from_pad); + elevation = j.get_double("elevation", elevation); + range = j.get_double("range", range); + gps_height = j.get_double("gps_height", gps_height); + pad_lat = j.get_double("pad_lat", pad_lat); + pad_lon = j.get_double("pad_lon", pad_lon); + pad_alt = j.get_double("pad_alt", pad_alt); + speak_tick = j.get_int("speak_tick", speak_tick); + speak_altitude = j.get_double("speak_altitude", speak_altitude); + callsign = j.get_string("callsign", callsign); + firmware_version = j.get_string("firmware_version", firmware_version); + accel_plus_g = j.get_double("accel_plus_g", accel_plus_g); + accel_minus_g = j.get_double("accel_minus_g", accel_minus_g); + accel = j.get_double("accel", accel); + ground_accel = j.get_double("ground_accel", ground_accel); + ground_accel_avg = j.get_double("ground_accel_avg", ground_accel_avg); + log_format = j.get_int("log_format", log_format); + log_space = j.get_int("log_space", log_space); + product = j.get_string("product", product); + baro = AltosMs5607.fromJson(j.get("baro"), baro); + companion = AltosCompanion.fromJson(j.get("companion"), companion); + pyro_fired = j.get_int("pyro_fired", pyro_fired); + accel_zero_along = j.get_double("accel_zero_along", accel_zero_along); + accel_zero_across = j.get_double("accel_zero_across", accel_zero_across); + accel_zero_through = j.get_double("accel_zero_through", accel_zero_through); + + rotation = AltosRotation.fromJson(j.get("rotation"), rotation); + ground_rotation = AltosRotation.fromJson(j.get("ground_rotation"), ground_rotation); + + pad_orientation = j.get_int("pad_orientation", pad_orientation); + + accel_ground_along = j.get_double("accel_ground_along", accel_ground_along); + accel_ground_across = j.get_double("accel_ground_across", accel_ground_across); + accel_ground_through = j.get_double("accel_ground_through", accel_ground_through); + + gyro_zero_roll = j.get_double("gyro_zero_roll", gyro_zero_roll); + gyro_zero_pitch = j.get_double("gyro_zero_pitch", gyro_zero_pitch); + gyro_zero_yaw = j.get_double("gyro_zero_yaw", gyro_zero_yaw); + + last_imu_time = j.get_double("last_imu_time", last_imu_time); + } public static AltosState fromHashSet(AltosHashSet h) { if (h == null) return null; @@ -1874,4 +2211,12 @@ public class AltosState implements Cloneable, AltosHashable { return null; return new AltosState(h); } + + public static AltosState fromJson(AltosJson j) { + if (j == null) + return null; + if (!j.get_boolean("valid", false)) + return null; + return new AltosState(j); + } } diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index d4554df3..534d2047 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -163,6 +163,8 @@ altoslib_JAVA = \ AltosMapTypeListener.java \ AltosHashSet.java \ AltosHashable.java \ + AltosJson.java \ + AltosJsonable.java \ AltosVersion.java JAR=altoslib_$(ALTOSLIB_VERSION).jar -- cgit v1.2.3 From f7e2f7f430e612c682bf55478860054ce94b995f Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 15 Jun 2016 22:52:08 -0700 Subject: altoslib: Remove AltosHashSet code Everything has switched to JSON now. Signed-off-by: Keith Packard --- altoslib/AltosCompanion.java | 28 +-- altoslib/AltosFrequency.java | 19 -- altoslib/AltosGPS.java | 63 +----- altoslib/AltosGPSSat.java | 56 ------ altoslib/AltosGreatCircle.java | 29 +-- altoslib/AltosHashSet.java | 316 ----------------------------- altoslib/AltosHashable.java | 25 --- altoslib/AltosIMU.java | 33 +--- altoslib/AltosMag.java | 26 +-- altoslib/AltosMs5607.java | 44 +---- altoslib/AltosPreferencesBackend.java | 12 -- altoslib/AltosQuaternion.java | 22 +-- altoslib/AltosRotation.java | 20 +- altoslib/AltosState.java | 360 +--------------------------------- altoslib/Makefile.am | 2 - 15 files changed, 16 insertions(+), 1039 deletions(-) delete mode 100644 altoslib/AltosHashSet.java delete mode 100644 altoslib/AltosHashable.java (limited to 'altoslib/AltosFrequency.java') diff --git a/altoslib/AltosCompanion.java b/altoslib/AltosCompanion.java index abb98830..2db8ea1b 100644 --- a/altoslib/AltosCompanion.java +++ b/altoslib/AltosCompanion.java @@ -19,7 +19,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosCompanion implements AltosHashable, AltosJsonable { +public class AltosCompanion implements AltosJsonable { public final static int board_id_telescience = 0x0a; public final static int MAX_CHANNELS = 12; @@ -38,17 +38,6 @@ public class AltosCompanion implements AltosHashable, AltosJsonable { companion_data = new int[channels]; } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putInt("tick", tick); - h.putInt("board_id", board_id); - h.putInt("update_period", update_period); - h.putInt("channels", channels); - h.putIntArray("companion_data", companion_data); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -60,21 +49,6 @@ public class AltosCompanion implements AltosHashable, AltosJsonable { return j; } - public AltosCompanion(AltosHashSet h) { - tick = h.getInt("tick", tick); - board_id = h.getInt("board_id", board_id); - update_period = h.getInt("update_period", update_period); - channels = h.getInt("channels", channels); - companion_data = h.getIntArray("companion_data", new int[channels]); - } - - public static AltosCompanion fromHashSet(AltosHashSet h, AltosCompanion def) { - if (h == null) - return def; - - return new AltosCompanion(h); - } - public AltosCompanion(AltosJson j) { tick = j.get_int("tick", tick); board_id = j.get_int("board_id", board_id); diff --git a/altoslib/AltosFrequency.java b/altoslib/AltosFrequency.java index 99828d53..3c1631a8 100644 --- a/altoslib/AltosFrequency.java +++ b/altoslib/AltosFrequency.java @@ -58,14 +58,6 @@ public class AltosFrequency implements AltosJsonable { return diff < 0.010; } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putDouble("frequency", frequency); - h.putString("description", description); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -79,17 +71,6 @@ public class AltosFrequency implements AltosJsonable { description = d; } - private AltosFrequency(AltosHashSet h) { - frequency = h.getDouble("frequency", 0.0); - description = h.getString("description", ""); - } - - public static AltosFrequency fromHashSet(AltosHashSet h, AltosFrequency def) { - if (h == null) - return def; - return new AltosFrequency(h); - } - private AltosFrequency(AltosJson j) { frequency = j.get_double("frequency", 0.0); description = j.get_string("description", ""); diff --git a/altoslib/AltosGPS.java b/altoslib/AltosGPS.java index d3710e4e..ba2eda1b 100644 --- a/altoslib/AltosGPS.java +++ b/altoslib/AltosGPS.java @@ -21,7 +21,7 @@ import java.text.*; import java.util.concurrent.*; import java.io.*; -public class AltosGPS implements Cloneable, AltosHashable, AltosJsonable { +public class AltosGPS implements Cloneable, AltosJsonable { public final static int MISSING = AltosLib.MISSING; @@ -389,34 +389,6 @@ public class AltosGPS implements Cloneable, AltosHashable, AltosJsonable { } } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putInt("nsat", nsat); - h.putBoolean("locked", locked); - h.putBoolean("connected", connected); - h.putDouble("lat", lat); - h.putDouble("lon", lon); - h.putDouble("alt", alt); - h.putInt("year", year); - h.putInt("month", month); - h.putInt("day", day); - h.putInt("hour", hour); - h.putInt("minute", minute); - h.putInt("second", second); - - h.putDouble("ground_speed", ground_speed); - h.putInt("course", course); - h.putDouble("climb_rate", climb_rate); - h.putDouble("pdop", pdop); - h.putDouble("hdop", hdop); - h.putDouble("vdop", vdop); - h.putDouble("h_error", h_error); - h.putDouble("v_error", v_error); - h.putString("cc_gps_sat", AltosGPSSat.toString(cc_gps_sat)); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -445,32 +417,6 @@ public class AltosGPS implements Cloneable, AltosHashable, AltosJsonable { return j; } - public AltosGPS(AltosHashSet h) { - init(); - nsat = h.getInt("nsat", nsat); - locked = h.getBoolean("locked", locked); - connected = h.getBoolean("connected", connected); - lat = h.getDouble("lat", lat); - lon = h.getDouble("lon", lon); - alt = h.getDouble("alt", alt); - year = h.getInt("year", year); - month = h.getInt("month", month); - day = h.getInt("day", day); - hour = h.getInt("hour", hour); - minute = h.getInt("minute", minute); - second = h.getInt("second", second); - - ground_speed = h.getDouble("ground_speed", ground_speed); - course = h.getInt("course", course); - climb_rate = h.getDouble("climb_rate", climb_rate); - pdop = h.getDouble("pdop", pdop); - hdop = h.getDouble("hdop", hdop); - vdop = h.getDouble("vdop", vdop); - h_error = h.getDouble("h_error", h_error); - v_error = h.getDouble("v_error", v_error); - cc_gps_sat = AltosGPSSat.array(h.getString("cc_gps_sat", null)); - } - public AltosGPS(AltosJson j) { init(); nsat = j.get_int("nsat", nsat); @@ -497,13 +443,6 @@ public class AltosGPS implements Cloneable, AltosHashable, AltosJsonable { cc_gps_sat = AltosGPSSat.json_array(j.get("cc_gps_sat")); } - public static AltosGPS fromHashSet(AltosHashSet h, AltosGPS def) { - if (h == null) - return def; - - return new AltosGPS(h); - } - public static AltosGPS fromJson(AltosJson j, AltosGPS def) { if (j == null) return def; diff --git a/altoslib/AltosGPSSat.java b/altoslib/AltosGPSSat.java index 319fe7f1..8cdeed0e 100644 --- a/altoslib/AltosGPSSat.java +++ b/altoslib/AltosGPSSat.java @@ -34,13 +34,6 @@ public class AltosGPSSat implements AltosJsonable { public AltosGPSSat() { } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - h.putInt("svid", svid); - h.putInt("c_n0", c_n0); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); j.put("svid", svid); @@ -48,22 +41,11 @@ public class AltosGPSSat implements AltosJsonable { return j; } - private AltosGPSSat(AltosHashSet h) { - svid = h.getInt("svid", 0); - c_n0 = h.getInt("c_n0", 0); - } - private AltosGPSSat(AltosJson j) { svid = j.get_int("svid", 0); c_n0 = j.get_int("c_n0", 0); } - static public AltosGPSSat fromHashSet(AltosHashSet h, AltosGPSSat def) { - if (h == null) - return def; - return new AltosGPSSat(h); - } - static public AltosGPSSat[] json_array(AltosJson j) { if (j == null) return null; @@ -74,43 +56,5 @@ public class AltosGPSSat implements AltosJsonable { sats[i] = new AltosGPSSat(j.get(i)); return sats; } - - static public AltosGPSSat[] array(String string) { - - if (string == null) - return null; - - try { - StringReader reader = new StringReader(string); - ArrayList array = new ArrayList(); - String element; - - while ((element = AltosHashSet.get_token(reader)) != null) { - AltosGPSSat sat = AltosGPSSat.fromHashSet(AltosHashSet.fromString(element), null); - if (sat != null) - array.add(sat); - } - return array.toArray(new AltosGPSSat[0]); - } catch (IOException ie) { - return null; - } - } - - public static String toString(AltosGPSSat[] sats) { - if (sats == null) - return null; - - try { - StringWriter writer = new StringWriter(); - - for (AltosGPSSat g : sats) { - String element = g.hashSet().toString(); - AltosHashSet.put_token(writer, element); - } - return writer.toString(); - } catch (IOException ie) { - return null; - } - } } diff --git a/altoslib/AltosGreatCircle.java b/altoslib/AltosGreatCircle.java index 8fd380a1..f2c1783d 100644 --- a/altoslib/AltosGreatCircle.java +++ b/altoslib/AltosGreatCircle.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.lang.Math; import java.io.*; -public class AltosGreatCircle implements Cloneable, AltosHashable, AltosJsonable { +public class AltosGreatCircle implements Cloneable, AltosJsonable { public double distance; public double bearing; public double range; @@ -104,17 +104,6 @@ public class AltosGreatCircle implements Cloneable, AltosHashable, AltosJsonable elevation = 0; } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putDouble("distance", distance); - h.putDouble("bearing", bearing); - h.putDouble("range", range); - h.putDouble("elevation", elevation); - - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -126,22 +115,6 @@ public class AltosGreatCircle implements Cloneable, AltosHashable, AltosJsonable return j; } - public AltosGreatCircle(AltosHashSet h) { - this(); - - distance = h.getDouble("distance", distance); - bearing = h.getDouble("bearing", bearing); - range = h.getDouble("range", range); - elevation = h.getDouble("elevation", elevation); - } - - public static AltosGreatCircle fromHashSet(AltosHashSet h, AltosGreatCircle def) { - if (h == null) - return def; - - return new AltosGreatCircle(h); - } - public AltosGreatCircle(AltosJson j) { this(); diff --git a/altoslib/AltosHashSet.java b/altoslib/AltosHashSet.java deleted file mode 100644 index 77bd48c2..00000000 --- a/altoslib/AltosHashSet.java +++ /dev/null @@ -1,316 +0,0 @@ -/* - * Copyright © 2016 Keith Packard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ - -package org.altusmetrum.altoslib_11; - -import java.io.*; -import java.util.*; -import java.text.*; - -public class AltosHashSet extends Hashtable { - static private int get(StringReader reader) throws IOException { - return reader.read(); - } - - static public String get_token(StringReader reader) throws IOException { - int c = get(reader); - - if (c == -1) - return null; - - ArrayList chars = new ArrayList(); - - for (;;) { - if (c == -1 || c == ';') - break; - if (c == '\\') - c = get(reader); - chars.add(c); - c = get(reader); - } - int[] ch = new int[chars.size()]; - for (int i = 0; i < ch.length; i++) - ch[i] = chars.get(i); - return new String(ch, 0, ch.length); - } - - static private void put(StringWriter writer, int c) throws IOException { - writer.write(c); - } - - static public void put_token(StringWriter writer, String token) throws IOException { - for (int i = 0; i < token.length(); i++) { - int c = token.codePointAt(i); - - switch (c) { - case ';': - case '\\': - put(writer, '\\'); - } - put(writer, c); - } - put(writer, ';'); - } - - public String toString() { - try { - StringWriter writer = new StringWriter(); - - for (String key : keySet()) { - String value = get(key); - put_token(writer, key); - put_token(writer, value); - } - return writer.toString(); - } catch (IOException ie) { - return null; - } - } - - public void putBoolean(String key, boolean value) { - put(key, value ? "t" : "f"); - } - - public boolean getBoolean(String key, boolean def) { - String value = get(key); - - if (value == null) - return def; - if (value.equals("t")) - return true; - if (value.equals("f")) - return false; - return def; - } - - public void putInt(String key, int value) { - put(key, Integer.toString(value)); - } - - public int getInt(String key, int def) { - String value = get(key); - - if (value == null) - return def; - try { - return AltosParse.parse_int(value); - } catch (ParseException pe) { - return def; - } - } - - public void putIntArray(String key, int value[]) { - if (value == null) - return; - - StringWriter writer = new StringWriter(); - - try { - for (int i = 0; i < value.length; i++) - put_token(writer, Integer.toString(value[i])); - put(key, writer.toString()); - } catch (IOException ie) { - } - } - - public int[] getIntArray(String key, int[] def) { - String value = get(key); - - if (value == null) - return def; - try { - StringReader reader = new StringReader(value); - ArrayList array = new ArrayList(); - String elt; - - while ((elt = get_token(reader)) != null) - array.add(AltosParse.parse_int(elt)); - int[] ret = new int[array.size()]; - for (int i = 0; i < ret.length; i++) - ret[i] = array.get(i); - return ret; - } catch (ParseException pe) { - return def; - } catch (IOException ie) { - return def; - } - } - - public void putLong(String key, long value) { - put(key, Long.toString(value)); - } - - public long getLong(String key, long def) { - String value = get(key); - - if (value == null) - return def; - try { - return AltosParse.parse_long(value); - } catch (ParseException pe) { - return def; - } - } - - public void putDouble(String key, double value) { - put(key, AltosParse.format_double_net(value)); - } - - public double getDouble(String key, double def) { - String value = get(key); - - if (value == null) - return def; - try { - return AltosParse.parse_double_net(value); - } catch (ParseException pe) { - return def; - } - } - - public void putDoubleArray(String key, double value[]) { - if (value == null) - return; - - StringWriter writer = new StringWriter(); - - try { - for (int i = 0; i < value.length; i++) - put_token(writer, AltosParse.format_double_net(value[i])); - put(key, writer.toString()); - } catch (IOException ie) { - } - } - - public double[] getDoubleArray(String key, double[] def) { - String value = get(key); - - if (value == null) - return def; - try { - StringReader reader = new StringReader(value); - ArrayList array = new ArrayList(); - String elt; - - while ((elt = get_token(reader)) != null) - array.add(AltosParse.parse_double_net(elt)); - double[] ret = new double[array.size()]; - for (int i = 0; i < ret.length; i++) - ret[i] = array.get(i); - return ret; - } catch (ParseException pe) { - return def; - } catch (IOException ie) { - return def; - } - } - - public String getString(String key, String def) { - String value = get(key); - - if (value == null) - return def; - return value; - } - - public void putString(String key, String value) { - if (value != null) - put(key, value); - } - - public AltosHashSet getHash(String key) { - String value = get(key); - - if (value == null) - return null; - try { - return new AltosHashSet(value); - } catch (IOException ie) { - return null; - } - } - - public void putHash(String key, AltosHashSet h) { - put(key, h.toString()); - } - - public void putHashable(String key, AltosHashable h) { - if (h == null) - return; - - put(key, h.hashSet().toString()); - } - - private AltosHashSet (String string) throws IOException { - StringReader reader = new StringReader(string); - String key, value; - - for (;;) { - key = get_token(reader); - value = get_token(reader); - if (key == null || value == null) - break; - put(key, value); - } - } - - public AltosHashSet() { - } - - static public AltosHashSet fromString(String string) { - try { - return new AltosHashSet(string); - } catch (IOException ie) { - return null; - } - } - - static public AltosHashSet[] array(String string) { - - if (string == null) - return null; - - try { - StringReader reader = new StringReader(string); - ArrayList array = new ArrayList(); - String element; - - while ((element = get_token(reader)) != null) - array.add(new AltosHashSet(element)); - return array.toArray(new AltosHashSet[0]); - } catch (IOException ie) { - return null; - } - } - - static public String toString(AltosHashSet[] sets) { - if (sets == null) - return null; - - try { - StringWriter writer = new StringWriter(); - - for (AltosHashSet h : sets) { - String element = h.toString(); - put_token(writer, element); - } - return writer.toString(); - } catch (IOException ie) { - return null; - } - } -} diff --git a/altoslib/AltosHashable.java b/altoslib/AltosHashable.java deleted file mode 100644 index e228543d..00000000 --- a/altoslib/AltosHashable.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright © 2016 Keith Packard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ - -package org.altusmetrum.altoslib_11; - -import java.io.*; - -public interface AltosHashable { - - public AltosHashSet hashSet(); -} diff --git a/altoslib/AltosIMU.java b/altoslib/AltosIMU.java index ecc02f15..672c6111 100644 --- a/altoslib/AltosIMU.java +++ b/altoslib/AltosIMU.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosIMU implements Cloneable, AltosHashable, AltosJsonable { +public class AltosIMU implements Cloneable, AltosJsonable { public int accel_along; public int accel_across; public int accel_through; @@ -116,18 +116,6 @@ public class AltosIMU implements Cloneable, AltosHashable, AltosJsonable { } } - public AltosIMU (AltosHashSet h) { - this(); - - accel_along = h.getInt("accel_along", accel_along); - accel_across = h.getInt("accel_across", accel_across); - accel_through = h.getInt("accel_through", accel_through); - - gyro_roll = h.getInt("gyro_roll", gyro_roll); - gyro_pitch = h.getInt("gyro_pitch", gyro_pitch); - gyro_yaw = h.getInt("gyro_yaw", gyro_yaw); - } - public AltosIMU (AltosJson j) { this(); @@ -140,31 +128,12 @@ public class AltosIMU implements Cloneable, AltosHashable, AltosJsonable { gyro_yaw = j.get_int("gyro_yaw", gyro_yaw); } - static public AltosIMU fromHashSet(AltosHashSet h, AltosIMU def) { - if (h == null) - return def; - return new AltosIMU(h); - } - static public AltosIMU fromJson(AltosJson j, AltosIMU def) { if (j == null) return def; return new AltosIMU(j); } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putInt("accel_along", accel_along); - h.putInt("accel_across", accel_across); - h.putInt("accel_through", accel_through); - - h.putInt("gyro_roll", gyro_roll); - h.putInt("gyro_pitch", gyro_pitch); - h.putInt("gyro_yaw", gyro_yaw); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); diff --git a/altoslib/AltosMag.java b/altoslib/AltosMag.java index ec98882f..8d40bc60 100644 --- a/altoslib/AltosMag.java +++ b/altoslib/AltosMag.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMag implements Cloneable, AltosHashable, AltosJsonable { +public class AltosMag implements Cloneable, AltosJsonable { public int along; public int across; public int through; @@ -94,15 +94,6 @@ public class AltosMag implements Cloneable, AltosHashable, AltosJsonable { } } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putInt("along", along); - h.putInt("across", across); - h.putInt("through", through); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -112,21 +103,6 @@ public class AltosMag implements Cloneable, AltosHashable, AltosJsonable { return j; } - public AltosMag(AltosHashSet h) { - this(); - - along = h.getInt("along", along); - across = h.getInt("across", across); - through = h.getInt("through", through); - } - - public static AltosMag fromHashSet(AltosHashSet h, AltosMag def) { - if (h == null) - return def; - - return new AltosMag(h); - } - public AltosMag(AltosJson j) { this(); diff --git a/altoslib/AltosMs5607.java b/altoslib/AltosMs5607.java index 6d2f2203..a769223e 100644 --- a/altoslib/AltosMs5607.java +++ b/altoslib/AltosMs5607.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMs5607 implements AltosHashable, AltosJsonable { +public class AltosMs5607 implements AltosJsonable { public int reserved; public int sens; public int off; @@ -167,24 +167,6 @@ public class AltosMs5607 implements AltosHashable, AltosJsonable { convert(); } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putInt("reserved", reserved); - h.putInt("sens", sens); - h.putInt("off", off); - h.putInt("tcs", tcs); - h.putInt("tco", tco); - h.putInt("tref", tref); - h.putInt("tempsens", tempsens); - h.putInt("crc", crc); - h.putInt("raw_pres", raw_pres); - h.putInt("raw_temp", raw_temp); - h.putInt("pa", pa); - h.putInt("cc", cc); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -203,30 +185,6 @@ public class AltosMs5607 implements AltosHashable, AltosJsonable { return j; } - public AltosMs5607(AltosHashSet h) { - this(); - - reserved = h.getInt("reserved", reserved); - sens = h.getInt("sens", sens); - off = h.getInt("off", off); - tcs = h.getInt("tcs", tcs); - tco = h.getInt("tco", tco); - tref = h.getInt("tref", tref); - tempsens = h.getInt("tempsens", tempsens); - crc = h.getInt("crc", crc); - raw_pres = h.getInt("raw_pres", raw_pres); - raw_temp = h.getInt("raw_temp", raw_temp); - pa = h.getInt("pa", pa); - cc = h.getInt("cc", cc); - } - - public static AltosMs5607 fromHashSet(AltosHashSet h, AltosMs5607 def) { - if (h == null) - return def; - - return new AltosMs5607(h); - } - public AltosMs5607(AltosJson j) { this(); diff --git a/altoslib/AltosPreferencesBackend.java b/altoslib/AltosPreferencesBackend.java index d4c3d7e5..0580652e 100644 --- a/altoslib/AltosPreferencesBackend.java +++ b/altoslib/AltosPreferencesBackend.java @@ -38,18 +38,6 @@ public abstract class AltosPreferencesBackend { public abstract byte[] getBytes(String key, byte[] def); public abstract void putBytes(String key, byte[] value); - public AltosHashSet getHashSet(String key) { - String value = getString(key, null); - - if (value == null) - return null; - return AltosHashSet.fromString(value); - } - - public void putHashSet(String key, AltosHashSet h) { - putString(key, h.toString()); - } - public AltosJson getJson(String key) { String value = getString(key, null); diff --git a/altoslib/AltosQuaternion.java b/altoslib/AltosQuaternion.java index 6a09bb8e..1b4a9419 100644 --- a/altoslib/AltosQuaternion.java +++ b/altoslib/AltosQuaternion.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosQuaternion implements AltosHashable, AltosJsonable { +public class AltosQuaternion implements AltosJsonable { double r; /* real bit */ double x, y, z; /* imaginary bits */ @@ -148,16 +148,6 @@ public class AltosQuaternion implements AltosHashable, AltosJsonable { c_x * c_y * s_z - s_x * s_y * c_z); } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putDouble("r", r); - h.putDouble("x", x); - h.putDouble("y", y); - h.putDouble("z", z); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -168,16 +158,6 @@ public class AltosQuaternion implements AltosHashable, AltosJsonable { return j; } - public AltosQuaternion(AltosHashSet h) { - if (h == null) - return; - - r = h.getDouble("r", 1); - x = h.getDouble("x", 0); - y = h.getDouble("y", 0); - z = h.getDouble("z", 0); - } - public AltosQuaternion(AltosJson j) { if (j == null) return; diff --git a/altoslib/AltosRotation.java b/altoslib/AltosRotation.java index 4b7ab407..321a0f30 100644 --- a/altoslib/AltosRotation.java +++ b/altoslib/AltosRotation.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosRotation implements AltosHashable, AltosJsonable { +public class AltosRotation implements AltosJsonable { private AltosQuaternion rotation; public double tilt() { @@ -48,28 +48,10 @@ public class AltosRotation implements AltosHashable, AltosJsonable { rotation = up.vectors_to_rotation(orient); } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putHashable("rotation", rotation); - return h; - } - public AltosJson json() { return rotation.json(); } - public AltosRotation(AltosHashSet h) { - rotation = new AltosQuaternion(h.getHash("rotation")); - } - - public static AltosRotation fromHashSet(AltosHashSet h, AltosRotation def) { - if (h == null) - return def; - - return new AltosRotation(h); - } - public AltosRotation(AltosJson j) { rotation = new AltosQuaternion(j); } diff --git a/altoslib/AltosState.java b/altoslib/AltosState.java index a3c6cc34..26cedce9 100644 --- a/altoslib/AltosState.java +++ b/altoslib/AltosState.java @@ -23,7 +23,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosState implements Cloneable, AltosHashable { +public class AltosState implements Cloneable, AltosJsonable { public static final int set_position = 1; public static final int set_gps = 2; @@ -46,7 +46,7 @@ public class AltosState implements Cloneable, AltosHashable { private int prev_tick; public int boost_tick; - class AltosValue implements AltosHashable, AltosJsonable { + class AltosValue implements AltosJsonable { double value; double prev_value; private double max_value; @@ -177,17 +177,6 @@ public class AltosState implements Cloneable, AltosHashable { prev_set_time = set_time; } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putDouble("value", value); - h.putDouble("prev_value", prev_value); - h.putDouble("max_value", max_value); - h.putDouble("set_time", set_time); - h.putDouble("prev_set_time", prev_set_time); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -199,17 +188,6 @@ public class AltosState implements Cloneable, AltosHashable { return j; } - AltosValue(AltosHashSet h) { - this(); - if (h != null) { - value = h.getDouble("value", value); - prev_value = h.getDouble("prev_value", prev_value); - max_value = h.getDouble("max_value", max_value); - set_time = h.getDouble("set_time", 0); - prev_set_time = h.getDouble("prev_set_time", 0); - } - } - AltosValue(AltosJson j) { this(); if (j != null) { @@ -229,21 +207,15 @@ public class AltosState implements Cloneable, AltosHashable { } - AltosValue AltosValue_fromHashSet(AltosHashSet h, AltosValue def) { - if (h == null) - return def; - return new AltosValue(h); - } - AltosValue AltosValue_fromJson(AltosJson j, AltosValue def) { if (j == null) return def; return new AltosValue(j); } - class AltosCValue implements AltosHashable, AltosJsonable { + class AltosCValue implements AltosJsonable { - class AltosIValue extends AltosValue implements AltosHashable, AltosJsonable { + class AltosIValue extends AltosValue implements AltosJsonable { boolean can_max() { return c_can_max(); } @@ -252,10 +224,6 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosIValue(AltosHashSet h) { - super(h); - } - AltosIValue(AltosJson j) { super(j); } @@ -352,14 +320,6 @@ public class AltosState implements Cloneable, AltosHashable { computed = new AltosIValue(); } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putHashable("measured", measured); - h.putHashable("computed", computed); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -368,23 +328,12 @@ public class AltosState implements Cloneable, AltosHashable { return j; } - AltosCValue(AltosHashSet h) { - measured = new AltosIValue(h.getHash("measured")); - computed = new AltosIValue(h.getHash("computed")); - } - AltosCValue(AltosJson j) { measured = new AltosIValue(j.get("measured")); computed = new AltosIValue(j.get("computed")); } } - AltosCValue AltosCValue_fromHashSet(AltosHashSet h, AltosCValue def) { - if (h == null) - return def; - return new AltosCValue(h); - } - AltosCValue AltosCValue_fromJson(AltosJson j, AltosCValue def) { if (j == null) return def; @@ -441,20 +390,11 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosGpsGroundAltitude (AltosHashSet h) { - super(h); - } - AltosGpsGroundAltitude (AltosJson j) { super(j); } } - AltosGpsGroundAltitude AltosGpsGroundAltitude_fromHashSet(AltosHashSet h, AltosGpsGroundAltitude def) { - if (h == null) return def; - return new AltosGpsGroundAltitude(h); - } - AltosGpsGroundAltitude AltosGpsGroundAltitude_fromJson(AltosJson j, AltosGpsGroundAltitude def) { if (j == null) return def; return new AltosGpsGroundAltitude(j); @@ -486,20 +426,11 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosGroundPressure (AltosHashSet h) { - super(h); - } - AltosGroundPressure (AltosJson j) { super(j); } } - AltosGroundPressure AltosGroundPressure_fromHashSet(AltosHashSet h, AltosGroundPressure def) { - if (h == null) return def; - return new AltosGroundPressure(h); - } - AltosGroundPressure AltosGroundPressure_fromJson(AltosJson j, AltosGroundPressure def) { if (j == null) return def; return new AltosGroundPressure(j); @@ -515,7 +446,7 @@ public class AltosState implements Cloneable, AltosHashable { ground_pressure.set_measured(pressure, time); } - class AltosAltitude extends AltosCValue implements AltosHashable { + class AltosAltitude extends AltosCValue { private void set_speed(AltosValue v) { if (!acceleration.is_measured() || !ascent) @@ -538,20 +469,11 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosAltitude (AltosHashSet h) { - super(h); - } - AltosAltitude (AltosJson j) { super(j); } } - AltosAltitude AltosAltitude_fromHashSet(AltosHashSet h, AltosAltitude def) { - if (h == null) return def; - return new AltosAltitude(h); - } - AltosAltitude AltosAltitude_fromJson(AltosJson j, AltosAltitude def) { if (j == null) return def; return new AltosAltitude(j); @@ -559,7 +481,7 @@ public class AltosState implements Cloneable, AltosHashable { private AltosAltitude altitude; - class AltosGpsAltitude extends AltosValue implements AltosHashable { + class AltosGpsAltitude extends AltosValue { private void set_gps_height() { double a = value(); @@ -580,20 +502,11 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosGpsAltitude (AltosHashSet h) { - super(h); - } - AltosGpsAltitude (AltosJson j) { super(j); } } - AltosGpsAltitude AltosGpsAltitude_fromHashSet(AltosHashSet h, AltosGpsAltitude def) { - if (h == null) return def; - return new AltosGpsAltitude(h); - } - AltosGpsAltitude AltosGpsAltitude_fromJson(AltosJson j, AltosGpsAltitude def) { if (j == null) return def; return new AltosGpsAltitude(j); @@ -677,20 +590,11 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosPressure (AltosHashSet h) { - super(h); - } - AltosPressure (AltosJson j) { super(j); } } - AltosPressure AltosPressure_fromHashSet(AltosHashSet h, AltosPressure def) { - if (h == null) return def; - return new AltosPressure(h); - } - AltosPressure AltosPressure_fromJson(AltosJson j, AltosPressure def) { if (j == null) return def; return new AltosPressure(j); @@ -756,7 +660,7 @@ public class AltosState implements Cloneable, AltosHashable { return AltosLib.MISSING; } - class AltosSpeed extends AltosCValue implements AltosHashable { + class AltosSpeed extends AltosCValue { boolean can_max() { return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless; @@ -785,20 +689,11 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosSpeed (AltosHashSet h) { - super(h); - } - AltosSpeed (AltosJson j) { super(j); } } - AltosSpeed AltosSpeed_fromHashSet(AltosHashSet h, AltosSpeed def) { - if (h == null) return def; - return new AltosSpeed(h); - } - AltosSpeed AltosSpeed_fromJson(AltosJson j, AltosSpeed def) { if (j == null) return def; return new AltosSpeed(j); @@ -832,7 +727,7 @@ public class AltosState implements Cloneable, AltosHashable { return AltosLib.MISSING; } - class AltosAccel extends AltosCValue implements AltosHashable { + class AltosAccel extends AltosCValue { boolean can_max() { return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless; @@ -848,20 +743,11 @@ public class AltosState implements Cloneable, AltosHashable { super(); } - AltosAccel (AltosHashSet h) { - super(h); - } - AltosAccel (AltosJson j) { super(j); } } - AltosAccel AltosAccel_fromHashSet(AltosHashSet h, AltosAccel def) { - if (h == null) return def; - return new AltosAccel(h); - } - AltosAccel AltosAccel_fromJson(AltosJson j, AltosAccel def) { if (j == null) return def; return new AltosAccel(j); @@ -1765,19 +1651,6 @@ public class AltosState implements Cloneable, AltosHashable { System.exit(1); } } - if (false) { - AltosHashSet hash = hashSet(); - String onetrip = hash.toString(); - AltosHashSet back = AltosHashSet.fromString(onetrip); - AltosState tripstate = AltosState.fromHashSet(back); - AltosHashSet triphash = tripstate.hashSet(); - String twotrip = triphash.toString(); - - if (!onetrip.equals(twotrip)) { - System.out.printf("%s\n%s\n", onetrip, twotrip); - System.exit(1); - } - } return s; } @@ -1785,112 +1658,6 @@ public class AltosState implements Cloneable, AltosHashable { init(); } - public AltosHashSet hashSet() { - AltosHashSet h = new AltosHashSet(); - - h.putBoolean("valid", true); - h.putInt("set", set); - h.putLong("received_time", received_time); - h.putDouble("time", time); - h.putDouble("prev_time", prev_time); - h.putDouble("time_change", time_change); - h.putInt("tick", tick); - h.putInt("prev_tick", prev_tick); - h.putInt("boost_tick", boost_tick); - h.putInt("state", state); - h.putInt("flight", flight); - h.putInt("serial", serial); - h.putInt("altitude_32", altitude_32); - h.putInt("receiver_serial", receiver_serial); - h.putBoolean("landed", landed); - h.putBoolean("ascent", ascent); - h.putBoolean("boost", boost); - h.putInt("rssi", rssi); - h.putInt("status", status); - h.putInt("device_type", device_type); - h.putInt("config_major", config_major); - h.putInt("config_minor", config_minor); - h.putInt("apogee_delay", apogee_delay); - h.putInt("main_deploy", main_deploy); - h.putInt("flight_log_max", flight_log_max); - h.putHashable("ground_altitude", ground_altitude); - h.putHashable("gps_ground_altitude", gps_ground_altitude); - h.putHashable("ground_pressure", ground_pressure); - h.putHashable("altitude", altitude); - h.putHashable("gps_altitude", gps_altitude); - h.putHashable("gps_ground_speed", gps_ground_speed); - h.putHashable("gps_ascent_rate", gps_ascent_rate); - h.putHashable("gps_course", gps_course); - h.putHashable("gps_speed", gps_speed); - h.putHashable("pressure", pressure); - h.putHashable("speed", speed); - h.putHashable("acceleration", acceleration); - h.putHashable("orient", orient); - h.putHashable("kalman_height", kalman_height); - h.putHashable("kalman_speed", kalman_speed); - h.putHashable("kalman_acceleration", kalman_acceleration); - - h.putDouble("battery_voltage",battery_voltage); - h.putDouble("pyro_voltage",pyro_voltage); - h.putDouble("temperature",temperature); - h.putDouble("apogee_voltage",apogee_voltage); - h.putDouble("main_voltage",main_voltage); - h.putDoubleArray("ignitor_voltage",ignitor_voltage); - h.putHashable("gps", gps); - h.putHashable("temp_gps", temp_gps); - h.putInt("temp_gps_sat_tick", temp_gps_sat_tick); - h.putBoolean("gps_pending", gps_pending); - h.putInt("gps_sequence", gps_sequence); - h.putHashable("imu", imu); - h.putHashable("mag", mag); - - h.putInt("npad", npad); - h.putInt("gps_waiting", gps_waiting); - h.putBoolean("gps_ready", gps_ready); - h.putInt("ngps", ngps); - h.putHashable("from_pad", from_pad); - h.putDouble("elevation", elevation); - h.putDouble("range", range); - h.putDouble("gps_height", gps_height); - h.putDouble("pad_lat", pad_lat); - h.putDouble("pad_lon", pad_lon); - h.putDouble("pad_alt", pad_alt); - h.putInt("speak_tick", speak_tick); - h.putDouble("speak_altitude", speak_altitude); - h.putString("callsign", callsign); - h.putString("firmware_version", firmware_version); - h.putDouble("accel_plus_g", accel_plus_g); - h.putDouble("accel_minus_g", accel_minus_g); - h.putDouble("accel", accel); - h.putDouble("ground_accel", ground_accel); - h.putDouble("ground_accel_avg", ground_accel_avg); - h.putInt("log_format", log_format); - h.putInt("log_space", log_space); - h.putString("product", product); - h.putHashable("baro", baro); - h.putHashable("companion", companion); - h.putInt("pyro_fired", pyro_fired); - h.putDouble("accel_zero_along", accel_zero_along); - h.putDouble("accel_zero_across", accel_zero_across); - h.putDouble("accel_zero_through", accel_zero_through); - - h.putHashable("rotation", rotation); - h.putHashable("ground_rotation", ground_rotation); - - h.putInt("pad_orientation", pad_orientation); - - h.putDouble("accel_ground_along", accel_ground_along); - h.putDouble("accel_ground_across", accel_ground_across); - h.putDouble("accel_ground_through", accel_ground_through); - - h.putDouble("gyro_zero_roll", gyro_zero_roll); - h.putDouble("gyro_zero_pitch", gyro_zero_pitch); - h.putDouble("gyro_zero_yaw", gyro_zero_yaw); - - h.putDouble("last_imu_time", last_imu_time); - return h; - } - public AltosJson json() { AltosJson j = new AltosJson(); @@ -1997,110 +1764,6 @@ public class AltosState implements Cloneable, AltosHashable { return j; } - public AltosState(AltosHashSet h) { - this(); - - set = h.getInt("set", set); - received_time = h.getLong("received_time", received_time); - time = h.getDouble("time", time); - prev_time = h.getDouble("prev_time", prev_time); - time_change = h.getDouble("time_change", time_change); - tick = h.getInt("tick", tick); - prev_tick = h.getInt("prev_tick", prev_tick); - boost_tick = h.getInt("boost_tick", boost_tick); - state = h.getInt("state", state); - flight = h.getInt("flight", flight); - serial = h.getInt("serial", serial); - altitude_32 = h.getInt("altitude_32", altitude_32); - receiver_serial = h.getInt("receiver_serial", receiver_serial); - landed = h.getBoolean("landed", landed); - ascent = h.getBoolean("ascent", ascent); - boost = h.getBoolean("boost", boost); - rssi = h.getInt("rssi", rssi); - status = h.getInt("status", status); - device_type = h.getInt("device_type", device_type); - config_major = h.getInt("config_major", config_major); - config_minor = h.getInt("config_minor", config_minor); - apogee_delay = h.getInt("apogee_delay", apogee_delay); - main_deploy = h.getInt("main_deploy", main_deploy); - flight_log_max = h.getInt("flight_log_max", flight_log_max); - ground_altitude = AltosCValue_fromHashSet(h.getHash("ground_altitude"), ground_altitude); - gps_ground_altitude = AltosGpsGroundAltitude_fromHashSet(h.getHash("gps_ground_altitude"), gps_ground_altitude); - ground_pressure = AltosGroundPressure_fromHashSet(h.getHash("ground_pressure"), ground_pressure); - altitude = AltosAltitude_fromHashSet(h.getHash("altitude"), altitude); - gps_altitude = AltosGpsAltitude_fromHashSet(h.getHash("gps_altitude"), gps_altitude); - gps_ground_speed = AltosValue_fromHashSet(h.getHash("gps_ground_speed"), gps_ground_speed); - gps_ascent_rate = AltosValue_fromHashSet(h.getHash("gps_ascent_rate"), gps_ascent_rate); - gps_course = AltosValue_fromHashSet(h.getHash("gps_course"), gps_course); - gps_speed = AltosValue_fromHashSet(h.getHash("gps_speed"), gps_speed); - pressure = AltosPressure_fromHashSet(h.getHash("pressure"), pressure); - speed = AltosSpeed_fromHashSet(h.getHash("speed"), speed); - acceleration = AltosAccel_fromHashSet(h.getHash("acceleration"), acceleration); - orient = AltosCValue_fromHashSet(h.getHash("orient"), orient); - kalman_height = AltosValue_fromHashSet(h.getHash("kalman_height"), kalman_height); - kalman_speed = AltosValue_fromHashSet(h.getHash("kalman_speed"), kalman_speed); - kalman_acceleration = AltosValue_fromHashSet(h.getHash("kalman_acceleration"), kalman_acceleration); - - battery_voltage = h.getDouble("battery_voltage", battery_voltage); - pyro_voltage = h.getDouble("pyro_voltage", pyro_voltage); - temperature = h.getDouble("temperature", temperature); - apogee_voltage = h.getDouble("apogee_voltage", apogee_voltage); - main_voltage= h.getDouble("main_voltage", main_voltage); - ignitor_voltage = h.getDoubleArray("ignitor_voltage", ignitor_voltage); - gps = AltosGPS.fromHashSet(h.getHash("gps"), gps); - temp_gps = AltosGPS.fromHashSet(h.getHash("temp_gps"), temp_gps); - temp_gps_sat_tick = h.getInt("temp_gps_sat_tick", temp_gps_sat_tick); - gps_pending = h.getBoolean("gps_pending", gps_pending); - gps_sequence = h.getInt("gps_sequence", gps_sequence); - imu = AltosIMU.fromHashSet(h.getHash("imu"), imu); - mag = AltosMag.fromHashSet(h.getHash("mag"), mag); - - npad = h.getInt("npad", npad); - gps_waiting = h.getInt("gps_waiting", gps_waiting); - gps_ready = h.getBoolean("gps_ready", gps_ready); - ngps = h.getInt("ngps", ngps); - from_pad = AltosGreatCircle.fromHashSet(h.getHash("from_pad"), from_pad); - elevation = h.getDouble("elevation", elevation); - range = h.getDouble("range", range); - gps_height = h.getDouble("gps_height", gps_height); - pad_lat = h.getDouble("pad_lat", pad_lat); - pad_lon = h.getDouble("pad_lon", pad_lon); - pad_alt = h.getDouble("pad_alt", pad_alt); - speak_tick = h.getInt("speak_tick", speak_tick); - speak_altitude = h.getDouble("speak_altitude", speak_altitude); - callsign = h.getString("callsign", callsign); - firmware_version = h.getString("firmware_version", firmware_version); - accel_plus_g = h.getDouble("accel_plus_g", accel_plus_g); - accel_minus_g = h.getDouble("accel_minus_g", accel_minus_g); - accel = h.getDouble("accel", accel); - ground_accel = h.getDouble("ground_accel", ground_accel); - ground_accel_avg = h.getDouble("ground_accel_avg", ground_accel_avg); - log_format = h.getInt("log_format", log_format); - log_space = h.getInt("log_space", log_space); - product = h.getString("product", product); - baro = AltosMs5607.fromHashSet(h.getHash("baro"), baro); - companion = AltosCompanion.fromHashSet(h.getHash("companion"), companion); - pyro_fired = h.getInt("pyro_fired", pyro_fired); - accel_zero_along = h.getDouble("accel_zero_along", accel_zero_along); - accel_zero_across = h.getDouble("accel_zero_across", accel_zero_across); - accel_zero_through = h.getDouble("accel_zero_through", accel_zero_through); - - rotation = AltosRotation.fromHashSet(h.getHash("rotation"), rotation); - ground_rotation = AltosRotation.fromHashSet(h.getHash("ground_rotation"), ground_rotation); - - pad_orientation = h.getInt("pad_orientation", pad_orientation); - - accel_ground_along = h.getDouble("accel_ground_along", accel_ground_along); - accel_ground_across = h.getDouble("accel_ground_across", accel_ground_across); - accel_ground_through = h.getDouble("accel_ground_through", accel_ground_through); - - gyro_zero_roll = h.getDouble("gyro_zero_roll", gyro_zero_roll); - gyro_zero_pitch = h.getDouble("gyro_zero_pitch", gyro_zero_pitch); - gyro_zero_yaw = h.getDouble("gyro_zero_yaw", gyro_zero_yaw); - - last_imu_time = h.getDouble("last_imu_time", last_imu_time); - } - public AltosState(AltosJson j) { this(); @@ -2204,13 +1867,6 @@ public class AltosState implements Cloneable, AltosHashable { last_imu_time = j.get_double("last_imu_time", last_imu_time); } - public static AltosState fromHashSet(AltosHashSet h) { - if (h == null) - return null; - if (!h.getBoolean("valid", false)) - return null; - return new AltosState(h); - } public static AltosState fromJson(AltosJson j) { if (j == null) diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index 534d2047..912976f9 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -161,8 +161,6 @@ altoslib_JAVA = \ AltosMapLoaderListener.java \ AltosMapLoader.java \ AltosMapTypeListener.java \ - AltosHashSet.java \ - AltosHashable.java \ AltosJson.java \ AltosJsonable.java \ AltosVersion.java -- cgit v1.2.3 From 1dce20f7eee56166ac61798ca26eeb323dc8f012 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 17 Jun 2016 00:52:38 -0700 Subject: altoslib: Get rid of manual JSON encoding stuff Now that the reflective JSON stuff is working, we can delete all of the manual code. Signed-off-by: Keith Packard --- altoslib/AltosCompanion.java | 28 +--- altoslib/AltosFrequency.java | 22 +-- altoslib/AltosGPS.java | 63 +------- altoslib/AltosGPSSat.java | 25 +-- altoslib/AltosGreatCircle.java | 34 +---- altoslib/AltosIMU.java | 33 +--- altoslib/AltosJson.java | 23 ++- altoslib/AltosMag.java | 26 +--- altoslib/AltosMs5607.java | 44 +----- altoslib/AltosPreferences.java | 8 +- altoslib/AltosQuaternion.java | 22 +-- altoslib/AltosRotation.java | 17 +-- altoslib/AltosState.java | 340 +---------------------------------------- altoslib/Makefile.am | 1 - 14 files changed, 39 insertions(+), 647 deletions(-) (limited to 'altoslib/AltosFrequency.java') diff --git a/altoslib/AltosCompanion.java b/altoslib/AltosCompanion.java index 2db8ea1b..d517fd4f 100644 --- a/altoslib/AltosCompanion.java +++ b/altoslib/AltosCompanion.java @@ -19,7 +19,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosCompanion implements AltosJsonable { +public class AltosCompanion { public final static int board_id_telescience = 0x0a; public final static int MAX_CHANNELS = 12; @@ -37,30 +37,4 @@ public class AltosCompanion implements AltosJsonable { channels = MAX_CHANNELS; companion_data = new int[channels]; } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("tick", tick); - j.put("board_id", board_id); - j.put("update_period", update_period); - j.put("channels", channels); - j.put("companion_data", companion_data); - return j; - } - - public AltosCompanion(AltosJson j) { - tick = j.get_int("tick", tick); - board_id = j.get_int("board_id", board_id); - update_period = j.get_int("update_period", update_period); - channels = j.get_int("channels", channels); - companion_data = j.get_int_array("companion_data", new int[channels]); - } - - public static AltosCompanion fromJson(AltosJson j, AltosCompanion def) { - if (j == null) - return def; - - return new AltosCompanion(j); - } } diff --git a/altoslib/AltosFrequency.java b/altoslib/AltosFrequency.java index 3c1631a8..874a9bcc 100644 --- a/altoslib/AltosFrequency.java +++ b/altoslib/AltosFrequency.java @@ -21,7 +21,7 @@ import java.io.*; import java.util.*; import java.text.*; -public class AltosFrequency implements AltosJsonable { +public class AltosFrequency { public double frequency; public String description; @@ -57,28 +57,8 @@ public class AltosFrequency implements AltosJsonable { return diff < 0.010; } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("frequency", frequency); - j.put("description", description); - return j; - } - public AltosFrequency(double f, String d) { frequency = f; description = d; } - - private AltosFrequency(AltosJson j) { - frequency = j.get_double("frequency", 0.0); - description = j.get_string("description", ""); - } - - public static AltosFrequency fromJson(AltosJson j, AltosFrequency def) { - if (j == null) - return def; - return new AltosFrequency(j); - } } diff --git a/altoslib/AltosGPS.java b/altoslib/AltosGPS.java index ba2eda1b..d29ccdd1 100644 --- a/altoslib/AltosGPS.java +++ b/altoslib/AltosGPS.java @@ -21,7 +21,7 @@ import java.text.*; import java.util.concurrent.*; import java.io.*; -public class AltosGPS implements Cloneable, AltosJsonable { +public class AltosGPS implements Cloneable { public final static int MISSING = AltosLib.MISSING; @@ -388,65 +388,4 @@ public class AltosGPS implements Cloneable, AltosJsonable { break; } } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("nsat", nsat); - j.put("locked", locked); - j.put("connected", connected); - j.put("lat", lat); - j.put("lon", lon); - j.put("alt", alt); - j.put("year", year); - j.put("month", month); - j.put("day", day); - j.put("hour", hour); - j.put("minute", minute); - j.put("second", second); - - j.put("ground_speed", ground_speed); - j.put("course", course); - j.put("climb_rate", climb_rate); - j.put("pdop", pdop); - j.put("hdop", hdop); - j.put("vdop", vdop); - j.put("h_error", h_error); - j.put("v_error", v_error); - j.put("cc_gps_sat", cc_gps_sat); - return j; - } - - public AltosGPS(AltosJson j) { - init(); - nsat = j.get_int("nsat", nsat); - locked = j.get_boolean("locked", locked); - connected = j.get_boolean("connected", connected); - lat = j.get_double("lat", lat); - lon = j.get_double("lon", lon); - alt = j.get_double("alt", alt); - year = j.get_int("year", year); - month = j.get_int("month", month); - day = j.get_int("day", day); - hour = j.get_int("hour", hour); - minute = j.get_int("minute", minute); - second = j.get_int("second", second); - - ground_speed = j.get_double("ground_speed", ground_speed); - course = j.get_int("course", course); - climb_rate = j.get_double("climb_rate", climb_rate); - pdop = j.get_double("pdop", pdop); - hdop = j.get_double("hdop", hdop); - vdop = j.get_double("vdop", vdop); - h_error = j.get_double("h_error", h_error); - v_error = j.get_double("v_error", v_error); - cc_gps_sat = AltosGPSSat.json_array(j.get("cc_gps_sat")); - } - - public static AltosGPS fromJson(AltosJson j, AltosGPS def) { - if (j == null) - return def; - - return new AltosGPS(j); - } } diff --git a/altoslib/AltosGPSSat.java b/altoslib/AltosGPSSat.java index 8cdeed0e..8b95c150 100644 --- a/altoslib/AltosGPSSat.java +++ b/altoslib/AltosGPSSat.java @@ -22,7 +22,7 @@ import java.text.*; import java.util.*; import java.util.concurrent.*; -public class AltosGPSSat implements AltosJsonable { +public class AltosGPSSat { public int svid; public int c_n0; @@ -33,28 +33,5 @@ public class AltosGPSSat implements AltosJsonable { public AltosGPSSat() { } - - public AltosJson json() { - AltosJson j = new AltosJson(); - j.put("svid", svid); - j.put("c_n0", c_n0); - return j; - } - - private AltosGPSSat(AltosJson j) { - svid = j.get_int("svid", 0); - c_n0 = j.get_int("c_n0", 0); - } - - static public AltosGPSSat[] json_array(AltosJson j) { - if (j == null) - return null; - - int size = j.size(); - AltosGPSSat[] sats = new AltosGPSSat[size]; - for (int i = 0; i < size; i++) - sats[i] = new AltosGPSSat(j.get(i)); - return sats; - } } diff --git a/altoslib/AltosGreatCircle.java b/altoslib/AltosGreatCircle.java index f2c1783d..a2f12807 100644 --- a/altoslib/AltosGreatCircle.java +++ b/altoslib/AltosGreatCircle.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.lang.Math; import java.io.*; -public class AltosGreatCircle implements Cloneable, AltosJsonable { +public class AltosGreatCircle implements Cloneable { public double distance; public double bearing; public double range; @@ -71,7 +71,10 @@ public class AltosGreatCircle implements Cloneable, AltosJsonable { course = 2 * Math.PI-course; } distance = d * earth_radius; - bearing = course * 180/Math.PI; + if (Double.isNaN(course) || Double.isInfinite(course)) + bearing = 0; + else + bearing = course * 180/Math.PI; double height_diff = end_alt - start_alt; range = Math.sqrt(distance * distance + height_diff * height_diff); @@ -103,31 +106,4 @@ public class AltosGreatCircle implements Cloneable, AltosJsonable { range = 0; elevation = 0; } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("distance", distance); - j.put("bearing", bearing); - j.put("range", range); - j.put("elevation", elevation); - - return j; - } - - public AltosGreatCircle(AltosJson j) { - this(); - - distance = j.get_double("distance", distance); - bearing = j.get_double("bearing", bearing); - range = j.get_double("range", range); - elevation = j.get_double("elevation", elevation); - } - - public static AltosGreatCircle fromJson(AltosJson j, AltosGreatCircle def) { - if (j == null) - return def; - - return new AltosGreatCircle(j); - } } diff --git a/altoslib/AltosIMU.java b/altoslib/AltosIMU.java index 672c6111..dbadcf89 100644 --- a/altoslib/AltosIMU.java +++ b/altoslib/AltosIMU.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosIMU implements Cloneable, AltosJsonable { +public class AltosIMU implements Cloneable { public int accel_along; public int accel_across; public int accel_through; @@ -115,35 +115,4 @@ public class AltosIMU implements Cloneable, AltosJsonable { break; } } - - public AltosIMU (AltosJson j) { - this(); - - accel_along = j.get_int("accel_along", accel_along); - accel_across = j.get_int("accel_across", accel_across); - accel_through = j.get_int("accel_through", accel_through); - - gyro_roll = j.get_int("gyro_roll", gyro_roll); - gyro_pitch = j.get_int("gyro_pitch", gyro_pitch); - gyro_yaw = j.get_int("gyro_yaw", gyro_yaw); - } - - static public AltosIMU fromJson(AltosJson j, AltosIMU def) { - if (j == null) - return def; - return new AltosIMU(j); - } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("accel_along", accel_along); - j.put("accel_across", accel_across); - j.put("accel_through", accel_through); - - j.put("gyro_roll", gyro_roll); - j.put("gyro_pitch", gyro_pitch); - j.put("gyro_yaw", gyro_yaw); - return j; - } } diff --git a/altoslib/AltosJson.java b/altoslib/AltosJson.java index 6ae7e7dc..e979a459 100644 --- a/altoslib/AltosJson.java +++ b/altoslib/AltosJson.java @@ -280,6 +280,9 @@ class JsonLexer extends JsonUtil { static keyword[] keywords = { new keyword("true", new JsonToken(JsonToken._boolean, true)), new keyword("false", new JsonToken(JsonToken._boolean, false)), + new keyword("NegInfinity", new JsonToken(JsonToken._double, Double.NEGATIVE_INFINITY)), + new keyword("Infinity", new JsonToken(JsonToken._double, Double.POSITIVE_INFINITY)), + new keyword("NaN", new JsonToken(JsonToken._double, Double.NaN)) }; static JsonToken keyword(String word) { @@ -583,10 +586,19 @@ public class AltosJson extends JsonUtil { array.append_array(result, indent, pretty); break; case type_double: - String dval = nf_json.format(d_number); - if (dval.equals("-0")) - dval = "0"; - result.append(dval); + if (Double.isInfinite(d_number)) { + if (d_number < 0) + result.append("NegInfinity"); + else + result.append("Infinity"); + } else if (Double.isNaN(d_number)) { + result.append("NaN"); + } else { + String dval = nf_json.format(d_number); + if (dval.equals("-0")) + dval = "0"; + result.append(dval); + } break; case type_long: result.append(new Long(l_number).toString()); @@ -1030,7 +1042,6 @@ public class AltosJson extends JsonUtil { for (Field field : c.getDeclaredFields()) { String fieldName = field.getName(); Class fieldClass = field.getType(); - String className = fieldClass.getName(); if (Modifier.isStatic(field.getModifiers())) continue; @@ -1165,8 +1176,6 @@ public class AltosJson extends JsonUtil { for (Class c = object.getClass(); c != null; c = c.getSuperclass()) { for (Field field : c.getDeclaredFields()) { String fieldName = field.getName(); - Class fieldClass = field.getType(); - String className = fieldClass.getName(); /* Skip static fields */ if (Modifier.isStatic(field.getModifiers())) diff --git a/altoslib/AltosMag.java b/altoslib/AltosMag.java index 8d40bc60..5864529f 100644 --- a/altoslib/AltosMag.java +++ b/altoslib/AltosMag.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMag implements Cloneable, AltosJsonable { +public class AltosMag implements Cloneable { public int along; public int across; public int through; @@ -93,28 +93,4 @@ public class AltosMag implements Cloneable, AltosJsonable { break; } } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("along", along); - j.put("across", across); - j.put("through", through); - return j; - } - - public AltosMag(AltosJson j) { - this(); - - along = j.get_int("along", along); - across = j.get_int("across", across); - through = j.get_int("through", through); - } - - public static AltosMag fromJson(AltosJson j, AltosMag def) { - if (j == null) - return def; - - return new AltosMag(j); - } } diff --git a/altoslib/AltosMs5607.java b/altoslib/AltosMs5607.java index a769223e..e40479b1 100644 --- a/altoslib/AltosMs5607.java +++ b/altoslib/AltosMs5607.java @@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11; import java.util.concurrent.*; import java.io.*; -public class AltosMs5607 implements AltosJsonable { +public class AltosMs5607 { public int reserved; public int sens; public int off; @@ -166,46 +166,4 @@ public class AltosMs5607 implements AltosJsonable { } convert(); } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("reserved", reserved); - j.put("sens", sens); - j.put("off", off); - j.put("tcs", tcs); - j.put("tco", tco); - j.put("tref", tref); - j.put("tempsens", tempsens); - j.put("crc", crc); - j.put("raw_pres", raw_pres); - j.put("raw_temp", raw_temp); - j.put("pa", pa); - j.put("cc", cc); - return j; - } - - public AltosMs5607(AltosJson j) { - this(); - - reserved = j.get_int("reserved", reserved); - sens = j.get_int("sens", sens); - off = j.get_int("off", off); - tcs = j.get_int("tcs", tcs); - tco = j.get_int("tco", tco); - tref = j.get_int("tref", tref); - tempsens = j.get_int("tempsens", tempsens); - crc = j.get_int("crc", crc); - raw_pres = j.get_int("raw_pres", raw_pres); - raw_temp = j.get_int("raw_temp", raw_temp); - pa = j.get_int("pa", pa); - cc = j.get_int("cc", cc); - } - - public static AltosMs5607 fromJson(AltosJson j, AltosMs5607 def) { - if (j == null) - return def; - - return new AltosMs5607(j); - } } diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index 51fc4205..569aaa54 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -365,7 +365,7 @@ public class AltosPreferences { public static void set_state(AltosState state) { synchronized(backend) { - backend.putJson(String.format(statePreferenceFormat, state.serial), state.json()); + backend.putJson(String.format(statePreferenceFormat, state.serial), new AltosJson(state)); backend.putInt(statePreferenceLatest, state.serial); flush_preferences(); } @@ -405,10 +405,12 @@ public class AltosPreferences { public static AltosState state(int serial) { synchronized(backend) { try { - return AltosState.fromJson(backend.getJson(String.format(statePreferenceFormat, serial))); + AltosJson json = backend.getJson(String.format(statePreferenceFormat, serial)); + if (json != null) + return (AltosState) (json.make(AltosState.class)); } catch (Exception e) { - return null; } + return null; } } diff --git a/altoslib/AltosQuaternion.java b/altoslib/AltosQuaternion.java index 98c2fe51..79559429 100644 --- a/altoslib/AltosQuaternion.java +++ b/altoslib/AltosQuaternion.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosQuaternion implements AltosJsonable { +public class AltosQuaternion { double r; /* real bit */ double x, y, z; /* imaginary bits */ @@ -154,24 +154,4 @@ public class AltosQuaternion implements AltosJsonable { c_x * s_y * c_z + s_x * c_y * s_z, c_x * c_y * s_z - s_x * s_y * c_z); } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("r", r); - j.put("x", x); - j.put("y", y); - j.put("z", z); - return j; - } - - public AltosQuaternion(AltosJson j) { - if (j == null) - return; - - r = j.get_double("r", 1); - x = j.get_double("x", 0); - y = j.get_double("y", 0); - z = j.get_double("z", 0); - } } diff --git a/altoslib/AltosRotation.java b/altoslib/AltosRotation.java index 6db0b541..97cf7896 100644 --- a/altoslib/AltosRotation.java +++ b/altoslib/AltosRotation.java @@ -17,7 +17,7 @@ package org.altusmetrum.altoslib_11; -public class AltosRotation implements AltosJsonable { +public class AltosRotation extends AltosQuaternion { private AltosQuaternion rotation; public double tilt() { @@ -48,22 +48,7 @@ public class AltosRotation implements AltosJsonable { rotation = up.vectors_to_rotation(orient); } - public AltosRotation(AltosJson j) { - rotation = new AltosQuaternion(j); - } - public AltosRotation() { rotation = new AltosQuaternion(); } - - public AltosJson json() { - return rotation.json(); - } - - public static AltosRotation fromJson(AltosJson j, AltosRotation def) { - if (j == null) - return def; - - return new AltosRotation(j); - } } diff --git a/altoslib/AltosState.java b/altoslib/AltosState.java index 93586e8c..15cf7d64 100644 --- a/altoslib/AltosState.java +++ b/altoslib/AltosState.java @@ -23,7 +23,7 @@ package org.altusmetrum.altoslib_11; import java.io.*; -public class AltosState implements Cloneable, AltosJsonable { +public class AltosState implements Cloneable { public static final int set_position = 1; public static final int set_gps = 2; @@ -46,7 +46,7 @@ public class AltosState implements Cloneable, AltosJsonable { private int prev_tick; public int boost_tick; - class AltosValue implements AltosJsonable { + class AltosValue { double value; double prev_value; private double max_value; @@ -177,28 +177,6 @@ public class AltosState implements Cloneable, AltosJsonable { prev_set_time = set_time; } - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("value", value); - j.put("prev_value", prev_value); - j.put("max_value", max_value); - j.put("set_time", set_time); - j.put("prev_set_time", prev_set_time); - return j; - } - - AltosValue(AltosJson j) { - this(); - if (j != null) { - value = j.get_double("value", value); - prev_value = j.get_double("prev_value", prev_value); - max_value = j.get_double("max_value", max_value); - set_time = j.get_double("set_time", 0); - prev_set_time = j.get_double("prev_set_time", 0); - } - } - AltosValue() { value = AltosLib.MISSING; prev_value = AltosLib.MISSING; @@ -207,15 +185,9 @@ public class AltosState implements Cloneable, AltosJsonable { } - AltosValue AltosValue_fromJson(AltosJson j, AltosValue def) { - if (j == null) - return def; - return new AltosValue(j); - } - - class AltosCValue implements AltosJsonable { + class AltosCValue { - class AltosIValue extends AltosValue implements AltosJsonable { + class AltosIValue extends AltosValue { boolean can_max() { return c_can_max(); } @@ -223,10 +195,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosIValue() { super(); } - - AltosIValue(AltosJson j) { - super(j); - } }; public AltosIValue measured; @@ -319,25 +287,6 @@ public class AltosState implements Cloneable, AltosJsonable { measured = new AltosIValue(); computed = new AltosIValue(); } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("measured", measured.json()); - j.put("computed", computed.json()); - return j; - } - - AltosCValue(AltosJson j) { - measured = new AltosIValue(j.get("measured")); - computed = new AltosIValue(j.get("computed")); - } - } - - AltosCValue AltosCValue_fromJson(AltosJson j, AltosCValue def) { - if (j == null) - return def; - return new AltosCValue(j); } private int state; @@ -389,15 +338,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosGpsGroundAltitude() { super(); } - - AltosGpsGroundAltitude (AltosJson j) { - super(j); - } - } - - AltosGpsGroundAltitude AltosGpsGroundAltitude_fromJson(AltosJson j, AltosGpsGroundAltitude def) { - if (j == null) return def; - return new AltosGpsGroundAltitude(j); } private AltosGpsGroundAltitude gps_ground_altitude; @@ -425,15 +365,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosGroundPressure () { super(); } - - AltosGroundPressure (AltosJson j) { - super(j); - } - } - - AltosGroundPressure AltosGroundPressure_fromJson(AltosJson j, AltosGroundPressure def) { - if (j == null) return def; - return new AltosGroundPressure(j); } private AltosGroundPressure ground_pressure; @@ -468,15 +399,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosAltitude() { super(); } - - AltosAltitude (AltosJson j) { - super(j); - } - } - - AltosAltitude AltosAltitude_fromJson(AltosJson j, AltosAltitude def) { - if (j == null) return def; - return new AltosAltitude(j); } private AltosAltitude altitude; @@ -501,15 +423,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosGpsAltitude() { super(); } - - AltosGpsAltitude (AltosJson j) { - super(j); - } - } - - AltosGpsAltitude AltosGpsAltitude_fromJson(AltosJson j, AltosGpsAltitude def) { - if (j == null) return def; - return new AltosGpsAltitude(j); } private AltosGpsAltitude gps_altitude; @@ -589,15 +502,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosPressure() { super(); } - - AltosPressure (AltosJson j) { - super(j); - } - } - - AltosPressure AltosPressure_fromJson(AltosJson j, AltosPressure def) { - if (j == null) return def; - return new AltosPressure(j); } private AltosPressure pressure; @@ -688,15 +592,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosSpeed() { super(); } - - AltosSpeed (AltosJson j) { - super(j); - } - } - - AltosSpeed AltosSpeed_fromJson(AltosJson j, AltosSpeed def) { - if (j == null) return def; - return new AltosSpeed(j); } private AltosSpeed speed; @@ -742,15 +637,6 @@ public class AltosState implements Cloneable, AltosJsonable { AltosAccel() { super(); } - - AltosAccel (AltosJson j) { - super(j); - } - } - - AltosAccel AltosAccel_fromJson(AltosJson j, AltosAccel def) { - if (j == null) return def; - return new AltosAccel(j); } AltosAccel acceleration; @@ -1668,222 +1554,4 @@ public class AltosState implements Cloneable, AltosJsonable { public AltosState () { init(); } - - public AltosJson json() { - AltosJson j = new AltosJson(); - - j.put("valid", true); - j.put("set", set); - j.put("received_time", received_time); - j.put("time", time); - j.put("prev_time", prev_time); - j.put("time_change", time_change); - j.put("tick", tick); - j.put("prev_tick", prev_tick); - j.put("boost_tick", boost_tick); - j.put("state", state); - j.put("flight", flight); - j.put("serial", serial); - j.put("altitude_32", altitude_32); - j.put("receiver_serial", receiver_serial); - j.put("landed", landed); - j.put("ascent", ascent); - j.put("boost", boost); - j.put("rssi", rssi); - j.put("status", status); - j.put("device_type", device_type); - j.put("config_major", config_major); - j.put("config_minor", config_minor); - j.put("apogee_delay", apogee_delay); - j.put("main_deploy", main_deploy); - j.put("flight_log_max", flight_log_max); - j.put("ground_altitude", ground_altitude); - j.put("gps_ground_altitude", gps_ground_altitude); - j.put("ground_pressure", ground_pressure); - j.put("altitude", altitude); - j.put("gps_altitude", gps_altitude); - j.put("gps_ground_speed", gps_ground_speed); - j.put("gps_ascent_rate", gps_ascent_rate); - j.put("gps_course", gps_course); - j.put("gps_speed", gps_speed); - j.put("pressure", pressure); - j.put("speed", speed); - j.put("acceleration", acceleration); - j.put("orient", orient); - j.put("kalman_height", kalman_height); - j.put("kalman_speed", kalman_speed); - j.put("kalman_acceleration", kalman_acceleration); - - j.put("battery_voltage",battery_voltage); - j.put("pyro_voltage",pyro_voltage); - j.put("temperature",temperature); - j.put("apogee_voltage",apogee_voltage); - j.put("main_voltage",main_voltage); - j.put("ignitor_voltage",ignitor_voltage); - j.put("gps", gps); - j.put("temp_gps", temp_gps); - j.put("temp_gps_sat_tick", temp_gps_sat_tick); - j.put("gps_pending", gps_pending); - j.put("gps_sequence", gps_sequence); - j.put("imu", imu); - j.put("mag", mag); - - j.put("npad", npad); - j.put("gps_waiting", gps_waiting); - j.put("gps_ready", gps_ready); - j.put("ngps", ngps); - j.put("from_pad", from_pad); - j.put("elevation", elevation); - j.put("range", range); - j.put("gps_height", gps_height); - j.put("pad_lat", pad_lat); - j.put("pad_lon", pad_lon); - j.put("pad_alt", pad_alt); - j.put("speak_tick", speak_tick); - j.put("speak_altitude", speak_altitude); - j.put("callsign", callsign); - j.put("firmware_version", firmware_version); - j.put("accel_plus_g", accel_plus_g); - j.put("accel_minus_g", accel_minus_g); - j.put("accel", accel); - j.put("ground_accel", ground_accel); - j.put("ground_accel_avg", ground_accel_avg); - j.put("log_format", log_format); - j.put("log_space", log_space); - j.put("product", product); - j.put("baro", baro); - j.put("companion", companion); - j.put("pyro_fired", pyro_fired); - j.put("accel_zero_along", accel_zero_along); - j.put("accel_zero_across", accel_zero_across); - j.put("accel_zero_through", accel_zero_through); - - j.put("rotation", rotation); - j.put("ground_rotation", ground_rotation); - - j.put("pad_orientation", pad_orientation); - - j.put("accel_ground_along", accel_ground_along); - j.put("accel_ground_across", accel_ground_across); - j.put("accel_ground_through", accel_ground_through); - - j.put("gyro_zero_roll", gyro_zero_roll); - j.put("gyro_zero_pitch", gyro_zero_pitch); - j.put("gyro_zero_yaw", gyro_zero_yaw); - - j.put("last_imu_time", last_imu_time); - return j; - } - - public AltosState(AltosJson j) { - this(); - - set = j.get_int("set", set); - received_time = j.get_long("received_time", received_time); - time = j.get_double("time", time); - prev_time = j.get_double("prev_time", prev_time); - time_change = j.get_double("time_change", time_change); - tick = j.get_int("tick", tick); - prev_tick = j.get_int("prev_tick", prev_tick); - boost_tick = j.get_int("boost_tick", boost_tick); - state = j.get_int("state", state); - flight = j.get_int("flight", flight); - serial = j.get_int("serial", serial); - altitude_32 = j.get_int("altitude_32", altitude_32); - receiver_serial = j.get_int("receiver_serial", receiver_serial); - landed = j.get_boolean("landed", landed); - ascent = j.get_boolean("ascent", ascent); - boost = j.get_boolean("boost", boost); - rssi = j.get_int("rssi", rssi); - status = j.get_int("status", status); - device_type = j.get_int("device_type", device_type); - config_major = j.get_int("config_major", config_major); - config_minor = j.get_int("config_minor", config_minor); - apogee_delay = j.get_int("apogee_delay", apogee_delay); - main_deploy = j.get_int("main_deploy", main_deploy); - flight_log_max = j.get_int("flight_log_max", flight_log_max); - ground_altitude = AltosCValue_fromJson(j.get("ground_altitude"), ground_altitude); - gps_ground_altitude = AltosGpsGroundAltitude_fromJson(j.get("gps_ground_altitude"), gps_ground_altitude); - ground_pressure = AltosGroundPressure_fromJson(j.get("ground_pressure"), ground_pressure); - altitude = AltosAltitude_fromJson(j.get("altitude"), altitude); - gps_altitude = AltosGpsAltitude_fromJson(j.get("gps_altitude"), gps_altitude); - gps_ground_speed = AltosValue_fromJson(j.get("gps_ground_speed"), gps_ground_speed); - gps_ascent_rate = AltosValue_fromJson(j.get("gps_ascent_rate"), gps_ascent_rate); - gps_course = AltosValue_fromJson(j.get("gps_course"), gps_course); - gps_speed = AltosValue_fromJson(j.get("gps_speed"), gps_speed); - pressure = AltosPressure_fromJson(j.get("pressure"), pressure); - speed = AltosSpeed_fromJson(j.get("speed"), speed); - acceleration = AltosAccel_fromJson(j.get("acceleration"), acceleration); - orient = AltosCValue_fromJson(j.get("orient"), orient); - kalman_height = AltosValue_fromJson(j.get("kalman_height"), kalman_height); - kalman_speed = AltosValue_fromJson(j.get("kalman_speed"), kalman_speed); - kalman_acceleration = AltosValue_fromJson(j.get("kalman_acceleration"), kalman_acceleration); - - battery_voltage = j.get_double("battery_voltage", battery_voltage); - pyro_voltage = j.get_double("pyro_voltage", pyro_voltage); - temperature = j.get_double("temperature", temperature); - apogee_voltage = j.get_double("apogee_voltage", apogee_voltage); - main_voltage= j.get_double("main_voltage", main_voltage); - ignitor_voltage = j.get_double_array("ignitor_voltage", ignitor_voltage); - gps = AltosGPS.fromJson(j.get("gps"), gps); - temp_gps = AltosGPS.fromJson(j.get("temp_gps"), temp_gps); - temp_gps_sat_tick = j.get_int("temp_gps_sat_tick", temp_gps_sat_tick); - gps_pending = j.get_boolean("gps_pending", gps_pending); - gps_sequence = j.get_int("gps_sequence", gps_sequence); - imu = AltosIMU.fromJson(j.get("imu"), imu); - mag = AltosMag.fromJson(j.get("mag"), mag); - - npad = j.get_int("npad", npad); - gps_waiting = j.get_int("gps_waiting", gps_waiting); - gps_ready = j.get_boolean("gps_ready", gps_ready); - ngps = j.get_int("ngps", ngps); - from_pad = AltosGreatCircle.fromJson(j.get("from_pad"), from_pad); - elevation = j.get_double("elevation", elevation); - range = j.get_double("range", range); - gps_height = j.get_double("gps_height", gps_height); - pad_lat = j.get_double("pad_lat", pad_lat); - pad_lon = j.get_double("pad_lon", pad_lon); - pad_alt = j.get_double("pad_alt", pad_alt); - speak_tick = j.get_int("speak_tick", speak_tick); - speak_altitude = j.get_double("speak_altitude", speak_altitude); - callsign = j.get_string("callsign", callsign); - firmware_version = j.get_string("firmware_version", firmware_version); - accel_plus_g = j.get_double("accel_plus_g", accel_plus_g); - accel_minus_g = j.get_double("accel_minus_g", accel_minus_g); - accel = j.get_double("accel", accel); - ground_accel = j.get_double("ground_accel", ground_accel); - ground_accel_avg = j.get_double("ground_accel_avg", ground_accel_avg); - log_format = j.get_int("log_format", log_format); - log_space = j.get_int("log_space", log_space); - product = j.get_string("product", product); - baro = AltosMs5607.fromJson(j.get("baro"), baro); - companion = AltosCompanion.fromJson(j.get("companion"), companion); - pyro_fired = j.get_int("pyro_fired", pyro_fired); - accel_zero_along = j.get_double("accel_zero_along", accel_zero_along); - accel_zero_across = j.get_double("accel_zero_across", accel_zero_across); - accel_zero_through = j.get_double("accel_zero_through", accel_zero_through); - - rotation = AltosRotation.fromJson(j.get("rotation"), rotation); - ground_rotation = AltosRotation.fromJson(j.get("ground_rotation"), ground_rotation); - - pad_orientation = j.get_int("pad_orientation", pad_orientation); - - accel_ground_along = j.get_double("accel_ground_along", accel_ground_along); - accel_ground_across = j.get_double("accel_ground_across", accel_ground_across); - accel_ground_through = j.get_double("accel_ground_through", accel_ground_through); - - gyro_zero_roll = j.get_double("gyro_zero_roll", gyro_zero_roll); - gyro_zero_pitch = j.get_double("gyro_zero_pitch", gyro_zero_pitch); - gyro_zero_yaw = j.get_double("gyro_zero_yaw", gyro_zero_yaw); - - last_imu_time = j.get_double("last_imu_time", last_imu_time); - } - - public static AltosState fromJson(AltosJson j) { - if (j == null) - return null; - if (!j.get_boolean("valid", false)) - return null; - return new AltosState(j); - } } diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index 912976f9..2a9eb9c9 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -162,7 +162,6 @@ altoslib_JAVA = \ AltosMapLoader.java \ AltosMapTypeListener.java \ AltosJson.java \ - AltosJsonable.java \ AltosVersion.java JAR=altoslib_$(ALTOSLIB_VERSION).jar -- cgit v1.2.3