From 08fc05761047f6219c85d23160b138dddb852da6 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Sat, 17 May 2014 01:58:00 +0300 Subject: [PATCH] Current tracker --- .../src/net/osmand/plus/osmo/OsMoService.java | 6 +- .../src/net/osmand/plus/osmo/OsMoTracker.java | 108 ++++++++++++++++-- 2 files changed, 105 insertions(+), 9 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java index 1850e37bc1..b16dbc08de 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java @@ -165,8 +165,10 @@ public class OsMoService { throw new RuntimeException("Token not specified"); } SessionInfo si = new SessionInfo(); - si.hostName = obj.getString("address"); - si.port = obj.getString("port"); + String a = obj.getString("address"); + int i = a.indexOf(':'); + si.hostName = a.substring(0, i); + si.port = a.substring(i + 1); si.token = obj.getString("token"); return si; } catch (ClientProtocolException e) { diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java index 25078a0d27..f55790e980 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java @@ -1,23 +1,60 @@ package net.osmand.plus.osmo; -import java.util.LinkedList; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; import net.osmand.Location; +import net.osmand.plus.osmo.OsMoService.OsMoReactor; import net.osmand.plus.osmo.OsMoService.OsMoSender; +import net.osmand.plus.views.OsmandMapTileView; -public class OsMoTracker implements OsMoSender { - private LinkedList bufferOfLocations = new LinkedList(); - private boolean start; +import org.json.JSONObject; + +public class OsMoTracker implements OsMoSender, OsMoReactor { + private ConcurrentLinkedQueue bufferOfLocations = new ConcurrentLinkedQueue(); + private Map otherLocations = new ConcurrentHashMap(); + private ConcurrentLinkedQueue listenIds = new ConcurrentLinkedQueue(); + private boolean trackerStarted; + private boolean startSendingLocations; + private OsmandMapTileView view; public OsMoTracker(OsMoService service) { service.registerSender(this); + service.registerReactor(this); + } + + public void enableTracker() { + startSendingLocations = true; + } + + public void disableTracker() { + startSendingLocations = false; + } + + public void startTrackingId(String id) { + listenIds.add("LISTEN|"+id); + otherLocations.put(id, null); + } + + public void stopTrackingId(String id) { + listenIds.add("UNLISTEN|"+id); + otherLocations.remove(id); } @Override public String nextSendCommand(OsMoThread thread) { - if(!start) { - start = true; - return "TRACKER_SESSION_OPEN"; + if (trackerStarted != startSendingLocations) { + if (!trackerStarted) { + trackerStarted = true; + return "TRACKER_SESSION_OPEN"; + } else { + trackerStarted = false; + return "TRACKER_SESSION_CLOSE"; + } + } + if(!listenIds.isEmpty()) { + return listenIds.poll(); } if(!bufferOfLocations.isEmpty()){ Location loc = bufferOfLocations.poll(); @@ -32,6 +69,12 @@ public class OsMoTracker implements OsMoSender { if(loc.hasSpeed()) { cmd.append("S").append((float)loc.getSpeed()); } + if(loc.hasBearing()) { + cmd.append("C").append((float)loc.getBearing()); + } + if((System.currentTimeMillis() - loc.getTime()) > 30000) { + cmd.append("T").append(loc.getTime()); + } return cmd.toString(); } return null; @@ -48,5 +91,56 @@ public class OsMoTracker implements OsMoSender { bufferOfLocations.add(l); } + public void setView(OsmandMapTileView view) { + this.view = view; + } + + public OsmandMapTileView getView() { + return view; + } + + @Override + public boolean acceptCommand(String command, String data, JSONObject obj, OsMoThread thread) { + if(command.startsWith("LT:")) { + String tid = command.substring(3); + float lat = 0; + float lon = 0; + float speed = 0; + int k = 0; + for (int i = 0; i <= data.length(); i++) { + boolean separator = i == data.length() || Character.isDigit(data.charAt(i)) || data.charAt(i) == ':' + || data.charAt(i) == '.'; + if (separator) { + char ch = data.charAt(k); + String vl = data.substring(k + 1, i); + if (ch == 'L') { + int l = vl.indexOf(":"); + lat = Float.parseFloat(vl.substring(0, l)); + lon = Float.parseFloat(vl.substring(l + 1)); + } else if (ch == 'S') { + speed = Float.parseFloat(vl); + } + k = i; + } + } + if(lat != 0 || lon != 0) { + Location loc = new Location("osmo"); + loc.setTime(System.currentTimeMillis()); + loc.setLatitude(lat); + loc.setLongitude(lon); + if(speed > 0) { + loc.setSpeed(speed); + } + otherLocations.put(tid, loc); + OsmandMapTileView v = view; + if(v != null){ + v.refreshMap(); + } + } + return true; + } + return false; + } + }