From 3753d8a7a95c07a32cfe81491e6c1e742a5732c8 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Tue, 6 May 2014 02:07:09 +0200 Subject: [PATCH] Update osmo service --- OsmAnd/res/values/strings.xml | 3 + .../src/net/osmand/plus/osmo/OsMoPlugin.java | 122 ++++++++++++------ .../src/net/osmand/plus/osmo/OsMoService.java | 53 +++++++- 3 files changed, 133 insertions(+), 45 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 7fe73e8181..347b324f54 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,9 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + OSMo connection problem : + Stop OSMo + Start OSMo Unique device id View unique device registration key and other monitoring specific settings OSMo (OpenStreetMap-Monitoring) diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java index 72d32e74fc..e2b5d9bee2 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java @@ -20,6 +20,7 @@ import org.apache.commons.logging.Log; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.os.AsyncTask; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceScreen; @@ -31,33 +32,33 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer public static final String ID = "osmand.osmo"; private static final Log log = PlatformUtil.getLog(OsMoPlugin.class); private OsMoService service; - - public OsMoPlugin(final OsmandApplication app){ + + public OsMoPlugin(final OsmandApplication app) { service = new OsMoService(); this.app = app; } - + @Override public boolean init(final OsmandApplication app) { return true; } - + @Override public void updateLocation(Location location) { - if(service.isActive()) { + if (service.isActive()) { try { - service.sendCoordinate(location.getLatitude(), location.getLongitude(), location.getAccuracy(), + service.sendCoordinate(location.getLatitude(), location.getLongitude(), location.getAccuracy(), (float) location.getAltitude(), location.getSpeed(), location.getBearing()); } catch (IOException e) { log.error(e.getMessage(), e); } } } - + public static String getUUID(Context ctx) { return Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID); } - + @Override public String getDescription() { return app.getString(R.string.osmo_plugin_description); @@ -65,65 +66,108 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer @Override public String getName() { - return app.getString(R.string.osmo_plugin_name) ; + return app.getString(R.string.osmo_plugin_name); } - + @Override public void updateLayers(OsmandMapTileView mapView, MapActivity activity) { // registerLayers(activity); super.updateLayers(mapView, activity); MonitoringInfoControl lock = activity.getMapLayers().getMapInfoLayer().getMonitoringInfoControl(); - if(lock != null && !lock.getMonitorActions().contains(this)) { + if (lock != null && !lock.getMonitorActions().contains(this)) { lock.addMonitorActions(this); } } + private void connect(final boolean enable) { + new AsyncTask() { + private Exception e; + @Override + protected String doInBackground(Void... params) { + try { + String response; + if (enable) { + response = service.activate(getUUID(app)); + } else { + response = service.deactivate(); + } + return response; + } catch (Exception e) { + this.e = e; + return null; + } + } + protected void onPostExecute(String result) { + if(e != null) { + app.showToastMessage(app.getString(R.string.osmo_io_error) + ": " + e.getMessage()); + log.error(e.getMessage(), e); + } else { + app.showToastMessage(result + ""); + } + }; + + }.execute(); + } + + private void sendCoordinate(final double lat, final double lon) { + new AsyncTask() { + + private Exception e; + + @Override + protected String doInBackground(Void... params) { + try { + return service.sendCoordinate(lat, lon, 0, 0, 0, + 0); + } catch (Exception e) { + this.e = e; + return null; + } + } + protected void onPostExecute(String result) { + if(e != null) { + app.showToastMessage(app.getString(R.string.osmo_io_error) + ": " + e.getMessage()); + log.error(e.getMessage(), e); + } else { + app.showToastMessage(result + ""); + } + }; + + }.execute(); + } + @Override public void addMonitorActions(ContextMenuAdapter qa, MonitoringInfoControl li, final OsmandMapTileView view) { final boolean off = !service.isActive(); - qa.item(off ? R.string.osmodroid_mode_off : R.string.osmodroid_mode_on) + qa.item(off ? R.string.osmo_mode_off : R.string.osmo_mode_on) .icon(off ? R.drawable.monitoring_rec_inactive : R.drawable.monitoring_rec_big) .listen(new OnContextMenuClick() { @Override public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) { - try { - String response; - if (off) { - response = service.activate(getUUID(app)); - } else { - response = service.deactivate(); - } - app.showToastMessage(response); - } catch (Exception e) { - app.showToastMessage(app.getString(R.string.error_io_error) + ": " + e.getMessage()); - } + connect(off); } + }).reg(); - qa.item("Test (send)"). - icons(R.drawable.ic_action_grefresh_dark, R.drawable.ic_action_grefresh_light). listen(new OnContextMenuClick() { + qa.item("Test (send)").icons(R.drawable.ic_action_grefresh_dark, R.drawable.ic_action_grefresh_light) + .listen(new OnContextMenuClick() { - @Override - public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) { - try { - String response = service.sendCoordinate(view.getLatitude(), view.getLongitude(), 0, 0, 0, 0); - app.showToastMessage(response); - } catch (Exception e) { - app.showToastMessage(app.getString(R.string.error_io_error) + ": " + e.getMessage()); - } - } - - - }).reg(); + @Override + public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) { + final double lat = view.getLatitude(); + final double lon = view.getLongitude(); + sendCoordinate(lat, lon); + } + }).reg(); } - + @Override public void settingsActivityCreate(final SettingsActivity activity, PreferenceScreen screen) { Preference grp = new Preference(activity); grp.setOnPreferenceClickListener(new OnPreferenceClickListener() { - + @Override public boolean onPreferenceClick(Preference preference) { activity.startActivity(new Intent(activity, SettingsOsMoActivity.class)); diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java index f0c3a1b94e..d4fe249156 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java @@ -5,38 +5,79 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; +import java.util.LinkedList; +import java.util.Queue; + +import net.osmand.PlatformUtil; + +import org.apache.commons.logging.Log; public class OsMoService { private static String TRACKER_SERVER = "srv.osmo.mobi"; private static int TRACKER_PORT = 4242; + public static int NUMBER_OF_TRIES_TO_RECONNECT = 20; + private Log log = PlatformUtil.getLog(OsMoService.class); private OutputStreamWriter out; private BufferedReader in; private Socket socket; + private Queue buffer = new LinkedList(); + private int numberOfFailures = 0; + private String loginHash; public boolean isActive() { return socket != null; } - public String activate(String hash) throws IOException { + public String activate(String loginHash) throws IOException { + this.loginHash = loginHash; socket = new Socket(TRACKER_SERVER, TRACKER_PORT); in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8"); - String t = sendCommand("auth|"+hash); + String t = sendCommand("auth|"+loginHash); t += sendCommand("session_open"); return t; } private String sendCommand(String s) throws IOException { - if(s.endsWith("\n")) { - s += "\n"; - } + s = prepareCommand(s); + log.info("OSMo command : " + s); out.write(s); return in.readLine(); } + + private String prepareCommand(String s) { + if(s.endsWith("\n")) { + s += "\n"; + } + return s; + } public String sendCoordinate(double lat, double lon, float hdop, float alt, float speed, float bearing) throws IOException { - return sendCommand("p|"+lat+":"+lon+":"+hdop+":"+alt+":"+speed+":"+bearing); + return sendCommandWithBuffer("p|"+lat+":"+lon+":"+hdop+":"+alt+":"+speed+":"+bearing); + } + + private synchronized String sendCommandWithBuffer(String command) throws IOException { + buffer.add(command); + String lastResponse = null; + while (!buffer.isEmpty()) { + reconnectIfNeeded(); + try { + lastResponse = sendCommand(command); + } catch (IOException e) { + numberOfFailures++; + e.printStackTrace(); + } + } + return lastResponse; + } + + private void reconnectIfNeeded() throws IOException { + if(numberOfFailures >= NUMBER_OF_TRIES_TO_RECONNECT) { + deactivate(); + activate(this.loginHash); + numberOfFailures = 0; + } } public String deactivate() throws IOException {