Update osmo service
This commit is contained in:
parent
4868fb7eb5
commit
3753d8a7a9
3 changed files with 133 additions and 45 deletions
|
@ -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
|
||||
-->
|
||||
<string name="osmo_io_error">OSMo connection problem : </string>
|
||||
<string name="osmo_mode_on">Stop OSMo</string>
|
||||
<string name="osmo_mode_off">Start OSMo</string>
|
||||
<string name="osmo_settings_uuid">Unique device id</string>
|
||||
<string name="osmo_settings_descr">View unique device registration key and other monitoring specific settings </string>
|
||||
<string name="osmo_settings">OSMo (OpenStreetMap-Monitoring)</string>
|
||||
|
|
|
@ -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<Void, Void, String>() {
|
||||
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<Void, Void, String>() {
|
||||
|
||||
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));
|
||||
|
|
|
@ -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<String> buffer = new LinkedList<String>();
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue