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;
|
||||
|
@ -78,44 +79,87 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer
|
|||
}
|
||||
}
|
||||
|
||||
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());
|
||||
final double lat = view.getLatitude();
|
||||
final double lon = view.getLongitude();
|
||||
sendCoordinate(lat, lon);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}).reg();
|
||||
}
|
||||
|
||||
|
|
|
@ -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