From 12f7e9a8ceec4c82dc2ebcdd299cdc4687891a01 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 4 May 2020 08:05:59 -0700 Subject: [PATCH] support int based lat/long for https://github.com/meshtastic/Meshtastic-device/issues/124 --- .../main/java/com/geeksville/mesh/NodeInfo.kt | 20 +++++++++++++- .../geeksville/mesh/service/MeshService.kt | 26 ++++++------------- app/src/main/proto | 2 +- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/NodeInfo.kt b/app/src/main/java/com/geeksville/mesh/NodeInfo.kt index 2632ac8f..d67e9f04 100644 --- a/app/src/main/java/com/geeksville/mesh/NodeInfo.kt +++ b/app/src/main/java/com/geeksville/mesh/NodeInfo.kt @@ -79,8 +79,26 @@ data class Position( val latitude: Double, val longitude: Double, val altitude: Int, - val time: Int = (System.currentTimeMillis() / 1000).toInt() // default to current time in secs + val time: Int = currentTime() // default to current time in secs ) : Parcelable { + companion object { + /// Convert to a double representation of degrees + fun degD(i: Int) = i * 1e-7 + fun degI(d: Double) = (d * 1e7).toInt() + + fun currentTime() = (System.currentTimeMillis() / 1000).toInt() + } + + /** Create our model object from a protobuf. If time is unspecified in the protobuf, the provided default time will be used. + */ + constructor(p: MeshProtos.Position, defaultTime: Int = currentTime()) : this( + // We prefer the int version of lat/lon but if not available use the depreciated legacy version + if (p.latitudeI == 0) p.latitudeD else degD(p.latitudeI), + if (p.longitudeI == 0) p.longitudeD else degD(p.longitudeI), + p.altitude, + if (p.time != 0) p.time else defaultTime + ) + /// @return distance in meters to some other node (or null if unknown) fun distance(o: Position) = latLongToMeter(latitude, longitude, o.latitude, o.longitude) diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index bf6443d5..a054454c 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -746,13 +746,7 @@ class MeshService : Service(), Logging { /// Update our DB of users based on someone sending out a Position subpacket private fun handleReceivedPosition(fromNum: Int, p: MeshProtos.Position) { updateNodeInfo(fromNum) { - it.position = Position( - p.latitude, - p.longitude, - p.altitude, - if (p.time != 0) p.time else it.position?.time - ?: 0 // if this position didn't include time, just keep our old one - ) + it.position = Position(p, it.position?.time ?: 0) } } @@ -1117,15 +1111,7 @@ class MeshService : Service(), Logging { if (info.hasPosition()) { // For the local node, it might not be able to update its times because it doesn't have a valid GPS reading yet // so if the info is for _our_ node we always assume time is current - val time = - if (it.num == mi.myNodeNum) currentSecond() else info.position.time - - it.position = Position( - info.position.latitude, - info.position.longitude, - info.position.altitude, - time - ) + it.position = Position(info.position) } } } @@ -1214,8 +1200,12 @@ class MeshService : Service(), Logging { debug("Sending our position to=$destNum lat=$lat, lon=$lon, alt=$alt") val position = MeshProtos.Position.newBuilder().also { - it.latitude = lat - it.longitude = lon + it.latitudeD = lat // Only old radios will use this variant, others will just ignore it + it.longitudeD = lon + + it.longitudeI = Position.degI(lon) + it.latitudeI = Position.degI(lat) + it.altitude = alt it.time = currentSecond() // Include our current timestamp }.build() diff --git a/app/src/main/proto b/app/src/main/proto index bd002e5a..cabbdf51 160000 --- a/app/src/main/proto +++ b/app/src/main/proto @@ -1 +1 @@ -Subproject commit bd002e5a144f209e42c97b64fea9a05a2e513b28 +Subproject commit cabbdf51ed365b72ab995ad24b075269627f58ad