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

129 lines
3.6 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
// 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)
}
}
}
data class Position(val latitude: Double, val longitude: Double, val altitude: Int) :
Parcelable {
constructor(parcel: Parcel) : this(
parcel.readDouble(),
parcel.readDouble(),
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)
}
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)
}
}
}
data class NodeInfo(
val num: Int, // This is immutable, and used as a key
var user: MeshUser? = null,
var position: Position? = null,
2020-02-12 14:15:59 -08:00
var lastSeen: Int? = null
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readParcelable(MeshUser::class.java.classLoader),
parcel.readParcelable(Position::class.java.classLoader),
2020-02-12 14:15:59 -08:00
parcel.readValue(Int::class.java.classLoader) as? Int
) {
}
2020-02-17 15:03:34 -08:00
/// @return distance in meters to some other node (or null if unknown)
fun distance(o: NodeInfo?): Double? {
val p = position
val op = o?.position
return if (p != null && op != null)
p.distance(op)
else
null
}
/// @return a nice human readable string for the distance, or null for unknown
fun distanceStr(o: NodeInfo?) = distance(o)?.let { dist ->
if (dist < 1000)
"%.0f m".format(dist)
else
"%.1f km".format(dist / 1000)
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(num)
parcel.writeParcelable(user, flags)
parcel.writeParcelable(position, flags)
parcel.writeValue(lastSeen)
}
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)
}
}
}