package com.geeksville.mesh import android.os.Parcelable import com.geeksville.mesh.ui.bearing import com.geeksville.mesh.ui.latLongToMeter import kotlinx.android.parcel.Parcelize import kotlinx.serialization.Serializable /** * 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 * of the string. Which should effectively hide real usernames and locations, * but still let us see if values were zero, empty or different. */ val Any?.anonymized: String get() = if (this != null) ("..." + this.toString().takeLast(3)) else "null" // // model objects that directly map to the corresponding protobufs // /** * A parcelable version of the protobuf MeshPacket + Data subpacket. */ @Serializable @Parcelize data class DataPacket( val from: String, // a nodeID string val to: String, // a nodeID string val rxTime: Long, // msecs since 1970 val id: Int, val dataType: Int, val bytes: ByteArray ) : Parcelable { // Autogenerated comparision, because we have a byte array override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false other as DataPacket if (from != other.from) return false if (to != other.to) return false if (rxTime != other.rxTime) return false if (id != other.id) return false if (dataType != other.dataType) return false if (!bytes.contentEquals(other.bytes)) return false return true } override fun hashCode(): Int { var result = from.hashCode() result = 31 * result + to.hashCode() result = 31 * result + rxTime.hashCode() result = 31 * result + id result = 31 * result + dataType result = 31 * result + bytes.contentHashCode() return result } } @Serializable @Parcelize data class MeshUser(val id: String, val longName: String, val shortName: String) : Parcelable { override fun toString(): String { return "MeshUser(id=${id.anonymized}, longName=${longName.anonymized}, shortName=${shortName.anonymized})" } } @Serializable @Parcelize 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 { /// @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 toString(): String { return "Position(lat=${latitude.anonymized}, lon=${longitude.anonymized}, alt=${altitude.anonymized}, time=${time})" } } @Serializable @Parcelize data class NodeInfo( val num: Int, // This is immutable, and used as a key var user: MeshUser? = null, var position: Position? = null ) : Parcelable { /// 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 val timeout = 15 * 60 // Don't set this timeout too tight, or otherwise we will stop sending GPS helper positions to our device 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 } } /// @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() 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) } } }