altoslib: Switch preserved state format to JSON
authorKeith Packard <keithp@keithp.com>
Thu, 16 Jun 2016 05:40:27 +0000 (22:40 -0700)
committerKeith Packard <keithp@keithp.com>
Thu, 16 Jun 2016 05:40:27 +0000 (22:40 -0700)
This is much easier to debug than the icky strings with backslashes everywhere.

Signed-off-by: Keith Packard <keithp@keithp.com>
15 files changed:
altoslib/AltosCompanion.java
altoslib/AltosFrequency.java
altoslib/AltosGPS.java
altoslib/AltosGPSSat.java
altoslib/AltosGreatCircle.java
altoslib/AltosIMU.java
altoslib/AltosMag.java
altoslib/AltosMs5607.java
altoslib/AltosParse.java
altoslib/AltosPreferences.java
altoslib/AltosPreferencesBackend.java
altoslib/AltosQuaternion.java
altoslib/AltosRotation.java
altoslib/AltosState.java
altoslib/Makefile.am

index 6f18d93eba01acda5e7e55224eeadc38f97e24da..abb988304e3d4c7e17f668125ffc93b1cce3ebb8 100644 (file)
@@ -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);
+       }
 }
index f9aa6de6ccb3eeea88baee2eb751a6ae3b9cd44d..99828d5362ff57668709d00e374a3e62914bc4b6 100644 (file)
@@ -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);
+       }
 }
index 371fd7bf1418f638f9d0494c393df46e30e1692e..d3710e4e60f46b5642ce1f7dd2c988d1e235a910 100644 (file)
@@ -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);
+       }
 }
index ad7a864702323efa1c5a109b230c682df08d8c48..319fe7f17f93e4cb98f64b06afcbc798083b0845 100644 (file)
@@ -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)
index 9ec808a5ca74e0c3b9e93d6a4a489d224d550448..8fd380a100ada00c645698460426dbd56223adc4 100644 (file)
@@ -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);
+       }
 }
index df6c4ed36fbc08c7d2d77cdbe56f06886cdc7eeb..ecc02f15b5d2dda6e7b8c62be7027eff2984e7dd 100644 (file)
@@ -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;
+       }
 }
index c350ae46392f4ddd637b58fe8f34c0c825a9faf1..ec98882f2e0513629fa3e25395d814b120a9b757 100644 (file)
@@ -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);
+       }
 }
index 88a978280af9e0004c89127c0c8019c78e34f035..6d2f22032c8715414461375b410b7a255e66fe1a 100644 (file)
@@ -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);
+       }
 }
index fbd049ae7a3da093c89de7ea5345546a4d3b8bf5..45ef7dac843cf265e1fb7309136d8a757e1c36e2 100644 (file)
@@ -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 {
index 3f8e7a070a98715ab0749a80a35a4c1b32250e85..8e625674840c964ecf6335b1918d58a2e3347807 100644 (file)
@@ -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<AltosFrequency>       freqs = new ArrayList<AltosFrequency>();
+               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<AltosFrequency>       freqs = new ArrayList<AltosFrequency>();
+
+                               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;
                        }
index 9131ad39b5d0c72190c5e7967bfa6a2814675a0f..d4c3d7e54e304143fbe3ffcdc04fa4db5e7d322b 100644 (file)
@@ -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);
 
index af9eb47558c701ad273753934826a4f6a5638854..6a09bb8e490d4ef3b36e9b4f9508c7465f1e1bda 100644 (file)
@@ -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);
+       }
 }
index e9c447ad1e4d69e355a96b25ab630809878a6fdf..4b7ab407514331e6b3bb62ee117a0a78f62d62c7 100644 (file)
@@ -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);
+       }
 }
index d359d67aa1ca16cbeaa94e69ed57485fd17ffe19..a3c6cc34d1f930cb63ab423db1e851dca2f81206 100644 (file)
@@ -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);
+       }
 }
index d4554df3bc0d8f62b85a6522d81131da54ca2a06..534d2047789b2c64d00f73ea8311fbf45e3ffc82 100644 (file)
@@ -163,6 +163,8 @@ altoslib_JAVA = \
        AltosMapTypeListener.java \
        AltosHashSet.java \
        AltosHashable.java \
+       AltosJson.java \
+       AltosJsonable.java \
        AltosVersion.java
 
 JAR=altoslib_$(ALTOSLIB_VERSION).jar