Meshtastic-Android/app/src/main/java/com/geeksville/mesh/NodeInfo.kt

176 lines
5.5 KiB
Kotlin
Raw Normal View History

package com.geeksville.mesh
import android.os.Parcel
import android.os.Parcelable
2020-02-17 15:03:34 -08:00
import com.geeksville.mesh.ui.bearing
import com.geeksville.mesh.ui.latLongToMeter
/**
* When printing strings to logs sometimes we want to print useful debugging information about users
* or positions. But we don't want to leak things like usernames or locations. So this function
* if given a string, will return a string which is a maximum of three characters long, taken from the tail
2020-04-12 10:56:45 -07:00
* of the string. Which should effectively hide real usernames and locations,
* but still let us see if values were zero, empty or different.
*/
2020-04-12 10:56:45 -07:00
val Any?.anonymized: String
get() = if (this != null) ("..." + this.toString().takeLast(3)) else "null"
// model objects that directly map to the corresponding protobufs
data class MeshUser(val id: String, val longName: String, val shortName: String) :
Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString()!!,
parcel.readString()!!,
parcel.readString()!!
) {
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(id)
parcel.writeString(longName)
parcel.writeString(shortName)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<MeshUser> {
override fun createFromParcel(parcel: Parcel): MeshUser {
return MeshUser(parcel)
}
override fun newArray(size: Int): Array<MeshUser?> {
return arrayOfNulls(size)
}
}
override fun toString(): String {
return "MeshUser(id=${id.anonymized}, longName=${longName.anonymized}, shortName=${shortName.anonymized})"
}
}
2020-02-19 10:53:36 -08:00
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
) :
Parcelable {
constructor(parcel: Parcel) : this(
parcel.readDouble(),
parcel.readDouble(),
2020-02-19 10:53:36 -08:00
parcel.readInt(),
parcel.readInt()
) {
}
2020-02-17 15:03:34 -08:00
/// @return distance in meters to some other node (or null if unknown)
fun distance(o: Position) = latLongToMeter(latitude, longitude, o.latitude, o.longitude)
/// @return bearing to the other position in degrees
fun bearing(o: Position) = bearing(latitude, longitude, o.latitude, o.longitude)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeDouble(latitude)
parcel.writeDouble(longitude)
parcel.writeInt(altitude)
2020-02-19 10:53:36 -08:00
parcel.writeInt(time)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Position> {
override fun createFromParcel(parcel: Parcel): Position {
return Position(parcel)
}
override fun newArray(size: Int): Array<Position?> {
return arrayOfNulls(size)
}
}
override fun toString(): String {
return "Position(lat=${latitude.anonymized}, lon=${longitude.anonymized}, alt=${altitude.anonymized}, time=${time})"
}
}
data class NodeInfo(
val num: Int, // This is immutable, and used as a key
var user: MeshUser? = null,
2020-02-19 10:53:36 -08:00
var position: Position? = null
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readParcelable(MeshUser::class.java.classLoader),
2020-02-19 10:53:36 -08:00
parcel.readParcelable(Position::class.java.classLoader)
) {
}
2020-02-19 10:53:36 -08:00
/// Return the last time we've seen this node in secs since 1970
val lastSeen get() = position?.time ?: 0
/**
* true if the device was heard from recently
*
* Note: if a node has never had its time set, it will have a time of zero. In that
* case assume it is online - so that we will start sending GPS updates
*/
val isOnline: Boolean
get() {
val now = System.currentTimeMillis() / 1000
// FIXME - use correct timeout from the device settings
2020-02-19 15:28:15 -08:00
val timeout =
15 * 60 // Don't set this timeout too tight, or otherwise we will stop sending GPS helper positions to our device
2020-02-19 10:53:36 -08:00
return (now - lastSeen <= timeout) || lastSeen == 0
}
/// return the position if it is valid, else null
val validPosition: Position?
get() {
return position?.takeIf { it.latitude != 0.0 || it.longitude != 0.0 }
}
2020-02-17 15:03:34 -08:00
/// @return distance in meters to some other node (or null if unknown)
fun distance(o: NodeInfo?): Int? {
val p = validPosition
val op = o?.validPosition
return if (p != null && op != null)
p.distance(op).toInt()
2020-02-17 15:03:34 -08:00
else
null
}
/// @return a nice human readable string for the distance, or null for unknown
fun distanceStr(o: NodeInfo?) = distance(o)?.let { dist ->
when {
dist == 0 -> null // same point
dist < 1000 -> "%.0f m".format(dist.toDouble())
else -> "%.1f km".format(dist / 1000.0)
}
2020-02-17 15:03:34 -08:00
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(num)
parcel.writeParcelable(user, flags)
parcel.writeParcelable(position, flags)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<NodeInfo> {
override fun createFromParcel(parcel: Parcel): NodeInfo {
return NodeInfo(parcel)
}
override fun newArray(size: Int): Array<NodeInfo?> {
return arrayOfNulls(size)
}
}
}