altosdroid: Save AltosState and restore at startup
authorKeith Packard <keithp@keithp.com>
Sun, 16 Nov 2014 06:52:42 +0000 (22:52 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 16 Nov 2014 06:52:42 +0000 (22:52 -0800)
Instead of re-parsing the old logfile, save the current state in the
preferences database and restore at restart of the
TelemetryService. This makes the state get restored even before the BT
connection is recovered.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java

index 971c3e80ddab085af28e729d9e34e3197c25641c..03abeec6d8bb3fe4e5888902b9d20c34be396f70 100644 (file)
@@ -40,8 +40,6 @@ public class TelemetryReader extends Thread {
        AltosLink   link;
        AltosState  state = null;
 
-       AltosFlightReader       stacked;
-
        LinkedBlockingQueue<AltosLine> telemQueue;
 
        public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
@@ -59,10 +57,6 @@ public class TelemetryReader extends Thread {
 
        public void close() {
                state = null;
-               if (stacked != null) {
-                       stacked.close(false);
-                       stacked = null;
-               }
                link.remove_monitor(telemQueue);
                link = null;
                telemQueue.clear();
@@ -73,25 +67,6 @@ public class TelemetryReader extends Thread {
                AltosState  state = null;
 
                try {
-                       if (D) Log.d(TAG, "starting reader");
-                       while (stacked != null) {
-                               AltosState      stacked_state = null;
-                               try {
-                                       stacked_state = stacked.read();
-                               } catch (ParseException pe) {
-                                       continue;
-                               } catch (AltosCRCException ce) {
-                                       continue;
-                               }
-                               if (stacked_state != null)
-                                       state = stacked_state;
-                               else
-                                       stacked = null;
-                       }
-                       if (state != null) {
-                               if (D) Log.d(TAG, "Send initial state");
-                               handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
-                       }
                        if (D) Log.d(TAG, "starting loop");
                        while (telemQueue != null) {
                                try {
@@ -111,34 +86,16 @@ public class TelemetryReader extends Thread {
                }
        }
 
-       public TelemetryReader (AltosLink in_link, Handler in_handler, AltosFlightReader in_stacked) {
+       public TelemetryReader (AltosLink in_link, Handler in_handler, AltosState in_state) {
                if (D) Log.d(TAG, "connected TelemetryReader create started");
                link    = in_link;
                handler = in_handler;
-               stacked = in_stacked;
 
-               state = null;
+               state = in_state;
                telemQueue = new LinkedBlockingQueue<AltosLine>();
                link.add_monitor(telemQueue);
                link.set_telemetry(AltosLib.ao_telemetry_standard);
 
                if (D) Log.d(TAG, "connected TelemetryReader created");
        }
-
-       private static AltosFlightReader existing_data(AltosLink link) {
-               if (link == null)
-                       return null;
-
-               File    file = AltosPreferences.logfile(link.serial);
-               if (file != null) {
-                       AltosStateIterable      iterable = AltosStateIterable.iterable(file);
-                       if (iterable != null)
-                               return new AltosReplayReader(iterable.iterator(), file, false);
-               }
-               return null;
-       }
-
-       public TelemetryReader(AltosLink link, Handler handler) {
-               this(link, handler, existing_data(link));
-       }
 }
index 30d94409a917a339aeeed88b4de31ca560a843b5..e029c75c9cabfcc11d782cc39ea8a000c4d1c3f4 100644 (file)
@@ -139,6 +139,10 @@ public class TelemetryService extends Service implements LocationListener {
                        case MSG_TELEMETRY:
                                // forward telemetry messages
                                s.telemetry_state.state = (AltosState) msg.obj;
+                               if (s.telemetry_state.state != null) {
+                                       if (D) Log.d(TAG, "Save state");
+                                       AltosPreferences.set_state(0, s.telemetry_state.state, null);
+                               }
                                if (D) Log.d(TAG, "MSG_TELEMETRY");
                                s.sendMessageToClients();
                                break;
@@ -179,6 +183,8 @@ public class TelemetryService extends Service implements LocationListener {
        private Message message() {
                if (telemetry_state == null)
                        Log.d(TAG, "telemetry_state null!");
+               if (telemetry_state.state == null)
+                       Log.d(TAG, "telemetry_state.state null!");
                return Message.obtain(null, AltosDroid.MSG_STATE, telemetry_state);
        }
 
@@ -262,7 +268,7 @@ public class TelemetryService extends Service implements LocationListener {
                if (D) Log.d(TAG, "connected bluetooth configured");
                telemetry_state.connect = TelemetryState.CONNECT_CONNECTED;
 
-               mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler);
+               mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler, telemetry_state.state);
                mTelemetryReader.start();
 
                if (D) Log.d(TAG, "connected TelemetryReader started");
@@ -306,6 +312,13 @@ public class TelemetryService extends Service implements LocationListener {
 
                telemetry_state.connect = TelemetryState.CONNECT_READY;
 
+               AltosSavedState saved_state = AltosPreferences.state(0);
+
+               if (saved_state != null) {
+                       if (D) Log.d(TAG, String.format("recovered old state flight %d\n", saved_state.state.flight));
+                       telemetry_state.state = saved_state.state;
+               }
+
                // Start our timer - first event in 10 seconds, then every 10 seconds after that.
                timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 10000L, 10000L);