2020-02-17 13:34:52 -08:00
|
|
|
package com.geeksville.mesh.model
|
|
|
|
|
|
2020-04-09 16:33:42 -07:00
|
|
|
import android.app.Application
|
2020-02-18 10:40:02 -08:00
|
|
|
import android.content.Context
|
2020-03-02 08:41:16 -08:00
|
|
|
import android.content.SharedPreferences
|
2020-03-17 11:35:19 -07:00
|
|
|
import android.net.Uri
|
2020-02-18 12:22:45 -08:00
|
|
|
import android.os.RemoteException
|
2020-07-17 17:06:29 -04:00
|
|
|
import android.view.Menu
|
2020-02-18 10:40:02 -08:00
|
|
|
import androidx.core.content.edit
|
2022-04-22 17:22:06 -03:00
|
|
|
import androidx.lifecycle.LiveData
|
2020-04-07 17:42:31 -07:00
|
|
|
import androidx.lifecycle.MutableLiveData
|
2022-02-08 13:50:21 -08:00
|
|
|
import androidx.lifecycle.ViewModel
|
2022-09-15 22:24:04 -03:00
|
|
|
import androidx.lifecycle.asLiveData
|
2020-09-23 22:47:45 -04:00
|
|
|
import androidx.lifecycle.viewModelScope
|
2022-09-04 22:52:40 -03:00
|
|
|
import com.geeksville.mesh.android.Logging
|
2022-02-03 18:15:06 -08:00
|
|
|
import com.geeksville.mesh.*
|
2022-09-12 00:26:12 -03:00
|
|
|
import com.geeksville.mesh.ConfigProtos.Config
|
2022-09-13 22:49:38 -03:00
|
|
|
import com.geeksville.mesh.database.MeshLogRepository
|
2022-08-11 16:43:26 +01:00
|
|
|
import com.geeksville.mesh.database.QuickChatActionRepository
|
2022-09-14 01:54:13 -03:00
|
|
|
import com.geeksville.mesh.database.entity.Packet
|
2022-09-13 22:49:38 -03:00
|
|
|
import com.geeksville.mesh.database.entity.MeshLog
|
2022-08-11 16:43:26 +01:00
|
|
|
import com.geeksville.mesh.database.entity.QuickChatAction
|
2022-09-12 00:26:12 -03:00
|
|
|
import com.geeksville.mesh.LocalOnlyProtos.LocalConfig
|
2022-09-14 01:54:13 -03:00
|
|
|
import com.geeksville.mesh.database.PacketRepository
|
2022-09-12 19:07:30 -03:00
|
|
|
import com.geeksville.mesh.repository.datastore.ChannelSetRepository
|
2022-06-11 18:36:57 -03:00
|
|
|
import com.geeksville.mesh.repository.datastore.LocalConfigRepository
|
2020-04-04 15:29:16 -07:00
|
|
|
import com.geeksville.mesh.service.MeshService
|
2022-08-30 17:25:11 -03:00
|
|
|
import com.geeksville.mesh.util.GPSFormat
|
2022-05-20 11:20:13 -03:00
|
|
|
import com.geeksville.mesh.util.positionToMeter
|
2022-02-08 13:50:21 -08:00
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
2020-09-23 22:47:45 -04:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2022-09-15 22:24:04 -03:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2022-02-08 13:50:21 -08:00
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
2022-02-03 18:15:06 -08:00
|
|
|
import kotlinx.coroutines.flow.first
|
2022-09-15 22:24:04 -03:00
|
|
|
import kotlinx.coroutines.flow.flatMapLatest
|
|
|
|
|
import kotlinx.coroutines.flow.mapLatest
|
2020-09-23 22:47:45 -04:00
|
|
|
import kotlinx.coroutines.launch
|
2022-02-03 18:15:06 -08:00
|
|
|
import kotlinx.coroutines.withContext
|
2022-09-22 08:35:33 -04:00
|
|
|
import org.osmdroid.bonuspack.kml.KmlDocument
|
|
|
|
|
import org.osmdroid.views.MapView
|
|
|
|
|
import org.osmdroid.views.overlay.FolderOverlay
|
2022-02-03 18:15:06 -08:00
|
|
|
import java.io.BufferedWriter
|
2022-02-13 08:09:26 -03:00
|
|
|
import java.io.FileNotFoundException
|
2022-02-03 18:15:06 -08:00
|
|
|
import java.io.FileWriter
|
|
|
|
|
import java.text.SimpleDateFormat
|
2022-10-16 12:40:05 -03:00
|
|
|
import java.util.Locale
|
2022-02-08 13:50:21 -08:00
|
|
|
import javax.inject.Inject
|
2022-10-16 12:40:05 -03:00
|
|
|
import kotlin.math.max
|
2022-02-03 18:15:06 -08:00
|
|
|
import kotlin.math.roundToInt
|
2020-04-08 09:53:04 -07:00
|
|
|
|
|
|
|
|
/// Given a human name, strip out the first letter of the first three words and return that as the initials for
|
2020-09-17 00:37:51 -07:00
|
|
|
/// that user. If the original name is only one word, strip vowels from the original name and if the result is
|
|
|
|
|
/// 3 or more characters, use the first three characters. If not, just take the first 3 characters of the
|
|
|
|
|
/// original name.
|
2020-09-17 12:03:27 -07:00
|
|
|
fun getInitials(nameIn: String): String {
|
2020-09-17 00:37:51 -07:00
|
|
|
val nchars = 3
|
|
|
|
|
val minchars = 2
|
2020-09-17 12:03:27 -07:00
|
|
|
val name = nameIn.trim()
|
2020-09-17 00:37:51 -07:00
|
|
|
val words = name.split(Regex("\\s+")).filter { it.isNotEmpty() }
|
2020-04-08 09:53:04 -07:00
|
|
|
|
2020-09-17 00:37:51 -07:00
|
|
|
val initials = when (words.size) {
|
2022-05-26 16:23:47 -03:00
|
|
|
in 0 until minchars -> {
|
|
|
|
|
val nm = if (name.isNotEmpty())
|
2022-01-24 18:23:09 -03:00
|
|
|
name.first() + name.drop(1).filterNot { c -> c.lowercase() in "aeiou" }
|
2020-09-27 14:05:10 -07:00
|
|
|
else
|
|
|
|
|
""
|
2020-09-17 00:37:51 -07:00
|
|
|
if (nm.length >= nchars) nm else name
|
|
|
|
|
}
|
2020-09-17 12:03:27 -07:00
|
|
|
else -> words.map { it.first() }.joinToString("")
|
2020-09-17 00:37:51 -07:00
|
|
|
}
|
|
|
|
|
return initials.take(nchars)
|
2020-04-08 09:53:04 -07:00
|
|
|
}
|
2020-03-15 16:30:12 -07:00
|
|
|
|
2022-02-08 13:50:21 -08:00
|
|
|
@HiltViewModel
|
|
|
|
|
class UIViewModel @Inject constructor(
|
|
|
|
|
private val app: Application,
|
2022-09-13 22:49:38 -03:00
|
|
|
private val meshLogRepository: MeshLogRepository,
|
2022-09-12 19:07:30 -03:00
|
|
|
private val channelSetRepository: ChannelSetRepository,
|
2022-09-14 01:54:13 -03:00
|
|
|
private val packetRepository: PacketRepository,
|
2022-06-11 18:36:57 -03:00
|
|
|
private val localConfigRepository: LocalConfigRepository,
|
2022-08-11 16:43:26 +01:00
|
|
|
private val quickChatActionRepository: QuickChatActionRepository,
|
2022-02-08 13:50:21 -08:00
|
|
|
private val preferences: SharedPreferences
|
|
|
|
|
) : ViewModel(), Logging {
|
2020-09-23 22:47:45 -04:00
|
|
|
|
2022-09-13 22:49:38 -03:00
|
|
|
private val _meshLog = MutableStateFlow<List<MeshLog>>(emptyList())
|
|
|
|
|
val meshLog: StateFlow<List<MeshLog>> = _meshLog
|
2020-09-23 22:47:45 -04:00
|
|
|
|
2022-09-14 01:54:13 -03:00
|
|
|
private val _packets = MutableStateFlow<List<Packet>>(emptyList())
|
|
|
|
|
val packets: StateFlow<List<Packet>> = _packets
|
|
|
|
|
|
2022-09-12 00:26:12 -03:00
|
|
|
private val _localConfig = MutableStateFlow<LocalConfig>(LocalConfig.getDefaultInstance())
|
|
|
|
|
val localConfig: StateFlow<LocalConfig> = _localConfig
|
|
|
|
|
val config get() = _localConfig.value
|
2022-06-20 22:46:45 -03:00
|
|
|
|
2022-09-12 19:07:30 -03:00
|
|
|
private val _channels = MutableStateFlow(ChannelSet())
|
|
|
|
|
val channels: StateFlow<ChannelSet> = _channels
|
|
|
|
|
|
2022-08-23 21:39:08 -03:00
|
|
|
private val _quickChatActions = MutableStateFlow<List<QuickChatAction>>(emptyList())
|
2022-08-11 16:43:26 +01:00
|
|
|
val quickChatActions: StateFlow<List<QuickChatAction>> = _quickChatActions
|
|
|
|
|
|
2020-04-07 17:42:31 -07:00
|
|
|
init {
|
2022-02-08 13:50:21 -08:00
|
|
|
viewModelScope.launch {
|
2022-09-13 22:49:38 -03:00
|
|
|
meshLogRepository.getAllLogs().collect { logs ->
|
|
|
|
|
_meshLog.value = logs
|
2022-06-11 18:36:57 -03:00
|
|
|
}
|
2022-06-17 02:00:18 -03:00
|
|
|
}
|
2022-09-14 01:54:13 -03:00
|
|
|
viewModelScope.launch {
|
2022-09-15 22:24:04 -03:00
|
|
|
packetRepository.getAllPackets().collect { packets ->
|
|
|
|
|
_packets.value = packets
|
2022-09-14 01:54:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-20 22:46:45 -03:00
|
|
|
viewModelScope.launch {
|
2022-06-17 02:00:18 -03:00
|
|
|
localConfigRepository.localConfigFlow.collect { config ->
|
2022-06-20 22:46:45 -03:00
|
|
|
_localConfig.value = config
|
2022-02-08 13:50:21 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-11 16:43:26 +01:00
|
|
|
viewModelScope.launch {
|
|
|
|
|
quickChatActionRepository.getAllActions().collect { actions ->
|
|
|
|
|
_quickChatActions.value = actions
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-12 19:07:30 -03:00
|
|
|
viewModelScope.launch {
|
|
|
|
|
channelSetRepository.channelSetFlow.collect { channelSet ->
|
|
|
|
|
_channels.value = ChannelSet(channelSet)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-07 17:42:31 -07:00
|
|
|
debug("ViewModel created")
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 22:24:04 -03:00
|
|
|
private val contactKey: MutableStateFlow<String> = MutableStateFlow(DataPacket.ID_BROADCAST)
|
|
|
|
|
fun setContactKey(contact: String) {
|
|
|
|
|
contactKey.value = contact
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@OptIn(ExperimentalCoroutinesApi::class)
|
|
|
|
|
val messages: LiveData<List<Packet>> = contactKey.flatMapLatest { contactKey ->
|
|
|
|
|
packetRepository.getMessagesFrom(contactKey)
|
|
|
|
|
}.asLiveData()
|
|
|
|
|
|
|
|
|
|
@OptIn(ExperimentalCoroutinesApi::class)
|
|
|
|
|
val contacts: LiveData<Map<String, Packet>> = _packets.mapLatest { list ->
|
|
|
|
|
list.associateBy { packet -> packet.contact_key }
|
|
|
|
|
.filter { it.value.port_num == Portnums.PortNum.TEXT_MESSAGE_APP_VALUE }
|
|
|
|
|
}.asLiveData()
|
|
|
|
|
|
|
|
|
|
@OptIn(ExperimentalCoroutinesApi::class)
|
|
|
|
|
val waypoints: LiveData<Map<Int?, Packet>> = _packets.mapLatest { list ->
|
|
|
|
|
list.associateBy { packet -> packet.data.waypoint?.id }
|
|
|
|
|
.filter { it.value.port_num == Portnums.PortNum.WAYPOINT_APP_VALUE }
|
|
|
|
|
}.asLiveData()
|
|
|
|
|
|
|
|
|
|
fun sendMessage(str: String, channel: Int = 0, dest: String = DataPacket.ID_BROADCAST) {
|
|
|
|
|
val p = DataPacket(dest, channel, str)
|
|
|
|
|
sendDataPacket(p)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun sendDataPacket(p: DataPacket) {
|
|
|
|
|
try {
|
|
|
|
|
meshService?.send(p)
|
|
|
|
|
} catch (ex: RemoteException) {
|
|
|
|
|
errormsg("Send DataPacket error: ${ex.message}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 22:49:38 -03:00
|
|
|
fun deleteAllLogs() = viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
|
meshLogRepository.deleteAll()
|
2020-09-23 22:47:45 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-15 22:24:04 -03:00
|
|
|
fun deleteAllMessages() = viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
|
packetRepository.deleteAllMessages()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun deleteMessages(uuidList: List<Long>) = viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
|
packetRepository.deleteMessages(uuidList)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 17:42:31 -07:00
|
|
|
companion object {
|
|
|
|
|
fun getPreferences(context: Context): SharedPreferences =
|
|
|
|
|
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 17:06:29 -04:00
|
|
|
var actionBarMenu: Menu? = null
|
|
|
|
|
|
2020-04-07 17:42:31 -07:00
|
|
|
var meshService: IMeshService? = null
|
|
|
|
|
|
2020-04-08 09:53:04 -07:00
|
|
|
val nodeDB = NodeDB(this)
|
|
|
|
|
|
2022-04-22 17:22:06 -03:00
|
|
|
/// Connection state to our radio device
|
|
|
|
|
private val _connectionState = MutableLiveData(MeshService.ConnectionState.DISCONNECTED)
|
2022-04-24 12:12:13 -03:00
|
|
|
val connectionState: LiveData<MeshService.ConnectionState> get() = _connectionState
|
2020-04-07 17:42:31 -07:00
|
|
|
|
2022-04-24 12:12:13 -03:00
|
|
|
fun isConnected() = _connectionState.value == MeshService.ConnectionState.CONNECTED
|
2020-04-07 17:42:31 -07:00
|
|
|
|
2022-04-22 17:22:06 -03:00
|
|
|
fun setConnectionState(connectionState: MeshService.ConnectionState) {
|
|
|
|
|
_connectionState.value = connectionState
|
2021-02-27 11:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-17 17:29:21 -03:00
|
|
|
private val _requestChannelUrl = MutableLiveData<Uri?>(null)
|
|
|
|
|
val requestChannelUrl: LiveData<Uri?> get() = _requestChannelUrl
|
|
|
|
|
|
|
|
|
|
fun setRequestChannelUrl(channelUrl: Uri) {
|
|
|
|
|
_requestChannelUrl.value = channelUrl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called immediately after activity observes requestChannelUrl
|
|
|
|
|
*/
|
|
|
|
|
fun clearRequestChannelUrl() {
|
|
|
|
|
_requestChannelUrl.value = null
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 18:35:13 -03:00
|
|
|
var txEnabled: Boolean
|
|
|
|
|
get() = config.lora.txEnabled
|
|
|
|
|
set(value) {
|
|
|
|
|
updateLoraConfig { it.copy { txEnabled = value } }
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 00:26:12 -03:00
|
|
|
var region: Config.LoRaConfig.RegionCode
|
2022-09-18 18:35:13 -03:00
|
|
|
get() = config.lora.region
|
2021-03-04 10:25:24 +08:00
|
|
|
set(value) {
|
2022-09-12 00:26:12 -03:00
|
|
|
updateLoraConfig { it.copy { region = value } }
|
2021-02-05 21:29:28 -08:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 18:35:13 -03:00
|
|
|
fun gpsString(p: Position): String {
|
|
|
|
|
return when (config.display.gpsFormat) {
|
|
|
|
|
Config.DisplayConfig.GpsCoordinateFormat.DEC -> GPSFormat.DEC(p)
|
|
|
|
|
Config.DisplayConfig.GpsCoordinateFormat.DMS -> GPSFormat.DMS(p)
|
|
|
|
|
Config.DisplayConfig.GpsCoordinateFormat.UTM -> GPSFormat.UTM(p)
|
|
|
|
|
Config.DisplayConfig.GpsCoordinateFormat.MGRS -> GPSFormat.MGRS(p)
|
|
|
|
|
else -> GPSFormat.DEC(p)
|
2022-08-30 17:25:11 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 23:01:28 -03:00
|
|
|
@Suppress("MemberVisibilityCanBePrivate")
|
2022-09-18 18:35:13 -03:00
|
|
|
val isRouter: Boolean = config.device.role == Config.DeviceConfig.Role.ROUTER
|
2022-06-17 02:00:18 -03:00
|
|
|
|
2022-08-28 07:54:47 -03:00
|
|
|
// We consider hasWifi = ESP32
|
|
|
|
|
fun isESP32() = myNodeInfo.value?.hasWifi == true
|
2022-06-10 19:22:34 -03:00
|
|
|
|
2021-03-04 09:08:29 +08:00
|
|
|
/// hardware info about our local device (can be null)
|
2022-04-22 17:22:06 -03:00
|
|
|
private val _myNodeInfo = MutableLiveData<MyNodeInfo?>()
|
|
|
|
|
val myNodeInfo: LiveData<MyNodeInfo?> get() = _myNodeInfo
|
|
|
|
|
|
|
|
|
|
fun setMyNodeInfo(info: MyNodeInfo?) {
|
|
|
|
|
_myNodeInfo.value = info
|
|
|
|
|
}
|
2020-05-13 17:00:23 -07:00
|
|
|
|
2020-04-08 09:53:04 -07:00
|
|
|
override fun onCleared() {
|
|
|
|
|
super.onCleared()
|
|
|
|
|
debug("ViewModel cleared")
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 17:22:06 -03:00
|
|
|
/// Pull our latest node db from the device
|
|
|
|
|
fun updateNodesFromDevice() {
|
|
|
|
|
meshService?.let { service ->
|
|
|
|
|
// Update our nodeinfos based on data from the device
|
|
|
|
|
val nodes = service.nodes.associateBy { it.user?.id!! }
|
|
|
|
|
nodeDB.setNodes(nodes)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Pull down our real node ID - This must be done AFTER reading the nodedb because we need the DB to find our nodeinof object
|
|
|
|
|
nodeDB.setMyId(service.myId)
|
|
|
|
|
val ownerName = nodeDB.ourNodeInfo?.user?.longName
|
|
|
|
|
_ownerName.value = ownerName
|
|
|
|
|
} catch (ex: Exception) {
|
|
|
|
|
warn("Ignoring failure to get myId, service is probably just uninited... ${ex.message}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 00:26:12 -03:00
|
|
|
inline fun updateDeviceConfig(crossinline body: (Config.DeviceConfig) -> Config.DeviceConfig) {
|
|
|
|
|
val data = body(config.device)
|
2022-10-11 16:27:36 -03:00
|
|
|
setConfig(config { device = data })
|
2022-09-12 00:26:12 -03:00
|
|
|
}
|
2021-02-27 13:43:55 +08:00
|
|
|
|
2022-09-12 00:26:12 -03:00
|
|
|
inline fun updatePositionConfig(crossinline body: (Config.PositionConfig) -> Config.PositionConfig) {
|
|
|
|
|
val data = body(config.position)
|
2022-10-11 16:27:36 -03:00
|
|
|
setConfig(config { position = data })
|
2022-06-10 21:55:26 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-12 00:26:12 -03:00
|
|
|
inline fun updatePowerConfig(crossinline body: (Config.PowerConfig) -> Config.PowerConfig) {
|
|
|
|
|
val data = body(config.power)
|
2022-10-11 16:27:36 -03:00
|
|
|
setConfig(config { power = data })
|
2022-09-12 00:26:12 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 18:35:13 -03:00
|
|
|
inline fun updateNetworkConfig(crossinline body: (Config.NetworkConfig) -> Config.NetworkConfig) {
|
|
|
|
|
val data = body(config.network)
|
2022-10-11 16:27:36 -03:00
|
|
|
setConfig(config { network = data })
|
2022-09-12 00:26:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline fun updateDisplayConfig(crossinline body: (Config.DisplayConfig) -> Config.DisplayConfig) {
|
|
|
|
|
val data = body(config.display)
|
2022-10-11 16:27:36 -03:00
|
|
|
setConfig(config { display = data })
|
2022-09-12 00:26:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline fun updateLoraConfig(crossinline body: (Config.LoRaConfig) -> Config.LoRaConfig) {
|
|
|
|
|
val data = body(config.lora)
|
2022-10-11 16:27:36 -03:00
|
|
|
setConfig(config { lora = data })
|
2022-09-12 00:26:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline fun updateBluetoothConfig(crossinline body: (Config.BluetoothConfig) -> Config.BluetoothConfig) {
|
|
|
|
|
val data = body(config.bluetooth)
|
2022-10-11 16:27:36 -03:00
|
|
|
setConfig(config { bluetooth = data })
|
2022-09-12 00:26:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the radio config (also updates our saved copy in preferences)
|
2022-10-11 16:27:36 -03:00
|
|
|
fun setConfig(config: Config) {
|
|
|
|
|
meshService?.setConfig(config.toByteArray())
|
2021-02-27 11:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-11 16:27:36 -03:00
|
|
|
/// Convert the channels array to and from [AppOnlyProtos.ChannelSet]
|
|
|
|
|
private var _channelSet: AppOnlyProtos.ChannelSet
|
|
|
|
|
get() = channels.value.protobuf
|
|
|
|
|
set(value) {
|
2022-10-16 12:40:05 -03:00
|
|
|
(0 until max(_channelSet.settingsCount, value.settingsCount)).map { i ->
|
2022-10-11 16:27:36 -03:00
|
|
|
channel {
|
2022-10-16 12:40:05 -03:00
|
|
|
role = when (i) {
|
|
|
|
|
0 -> ChannelProtos.Channel.Role.PRIMARY
|
|
|
|
|
in 1 until value.settingsCount -> ChannelProtos.Channel.Role.SECONDARY
|
|
|
|
|
else -> ChannelProtos.Channel.Role.DISABLED
|
|
|
|
|
}
|
2022-10-11 16:27:36 -03:00
|
|
|
index = i
|
2022-10-16 12:40:05 -03:00
|
|
|
settings = value.settingsList.getOrNull(i) ?: channelSettings { }
|
2022-10-11 16:27:36 -03:00
|
|
|
}
|
2022-10-16 12:40:05 -03:00
|
|
|
}.forEach {
|
2022-10-11 16:27:36 -03:00
|
|
|
meshService?.setChannel(it.toByteArray())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
viewModelScope.launch {
|
|
|
|
|
channelSetRepository.clearSettings()
|
|
|
|
|
channelSetRepository.addAllSettings(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val newConfig = config { lora = value.loraConfig }
|
|
|
|
|
if (config.lora != newConfig.lora) setConfig(newConfig)
|
|
|
|
|
}
|
|
|
|
|
val channelSet get() = _channelSet
|
|
|
|
|
|
2021-02-27 11:44:05 +08:00
|
|
|
/// Set the radio config (also updates our saved copy in preferences)
|
2022-10-11 16:27:36 -03:00
|
|
|
fun setChannels(channelSet: ChannelSet) {
|
2021-02-27 11:44:05 +08:00
|
|
|
debug("Setting new channels!")
|
2022-10-11 16:27:36 -03:00
|
|
|
this._channelSet = channelSet.protobuf
|
2020-04-09 16:33:42 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-17 13:34:52 -08:00
|
|
|
/// our name in hte radio
|
|
|
|
|
/// Note, we generate owner initials automatically for now
|
2020-02-18 12:22:45 -08:00
|
|
|
/// our activity will read this from prefs or set it to the empty string
|
2022-04-22 17:22:06 -03:00
|
|
|
private val _ownerName = MutableLiveData<String?>()
|
|
|
|
|
val ownerName: LiveData<String?> get() = _ownerName
|
2020-03-15 16:30:12 -07:00
|
|
|
|
2022-06-22 22:07:55 -03:00
|
|
|
val provideLocation = object : MutableLiveData<Boolean>(preferences.getBoolean("provide-location", false)) {
|
2021-06-23 12:41:44 -07:00
|
|
|
override fun setValue(value: Boolean) {
|
|
|
|
|
super.setValue(value)
|
|
|
|
|
|
2022-04-22 17:22:06 -03:00
|
|
|
preferences.edit {
|
2022-06-22 22:07:55 -03:00
|
|
|
this.putBoolean("provide-location", value)
|
2021-06-23 12:41:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-20 09:56:38 -07:00
|
|
|
|
2020-02-18 10:40:02 -08:00
|
|
|
// clean up all this nasty owner state management FIXME
|
2020-04-09 16:33:42 -07:00
|
|
|
fun setOwner(s: String? = null) {
|
2020-02-18 12:22:45 -08:00
|
|
|
|
2020-02-18 10:40:02 -08:00
|
|
|
if (s != null) {
|
2022-04-22 17:22:06 -03:00
|
|
|
_ownerName.value = s
|
2020-02-18 12:22:45 -08:00
|
|
|
|
|
|
|
|
// note: we allow an empty userstring to be written to prefs
|
2022-04-22 17:22:06 -03:00
|
|
|
preferences.edit {
|
2020-02-18 10:40:02 -08:00
|
|
|
putString("owner", s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note: we are careful to not set a new unique ID
|
2022-04-22 17:22:06 -03:00
|
|
|
if (_ownerName.value!!.isNotEmpty())
|
2020-02-18 12:22:45 -08:00
|
|
|
try {
|
|
|
|
|
meshService?.setOwner(
|
|
|
|
|
null,
|
2022-04-22 17:22:06 -03:00
|
|
|
_ownerName.value,
|
|
|
|
|
getInitials(_ownerName.value!!)
|
2020-02-18 12:22:45 -08:00
|
|
|
) // Note: we use ?. here because we might be running in the emulator
|
|
|
|
|
} catch (ex: RemoteException) {
|
2020-02-29 13:21:05 -08:00
|
|
|
errormsg("Can't set username on device, is device offline? ${ex.message}")
|
2020-02-18 12:22:45 -08:00
|
|
|
}
|
2020-02-18 10:40:02 -08:00
|
|
|
}
|
2022-02-03 18:15:06 -08:00
|
|
|
|
2022-10-16 12:36:21 -03:00
|
|
|
val adminChannelIndex: Int
|
|
|
|
|
get() = channelSet.settingsList.map { it.name.lowercase() }.indexOf("admin")
|
2022-10-10 18:09:20 -03:00
|
|
|
|
2022-09-30 15:57:04 -03:00
|
|
|
fun requestShutdown(idNum: Int) {
|
|
|
|
|
try {
|
|
|
|
|
meshService?.requestShutdown(idNum)
|
|
|
|
|
} catch (ex: RemoteException) {
|
|
|
|
|
errormsg("RemoteException: ${ex.message}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun requestReboot(idNum: Int) {
|
|
|
|
|
try {
|
|
|
|
|
meshService?.requestReboot(idNum)
|
|
|
|
|
} catch (ex: RemoteException) {
|
|
|
|
|
errormsg("RemoteException: ${ex.message}")
|
|
|
|
|
}
|
2022-06-06 17:29:09 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-30 15:57:04 -03:00
|
|
|
fun requestFactoryReset(idNum: Int) {
|
|
|
|
|
try {
|
|
|
|
|
meshService?.requestFactoryReset(idNum)
|
|
|
|
|
} catch (ex: RemoteException) {
|
|
|
|
|
errormsg("RemoteException: ${ex.message}")
|
|
|
|
|
}
|
2022-06-06 17:29:09 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-30 15:57:04 -03:00
|
|
|
fun requestNodedbReset(idNum: Int) {
|
|
|
|
|
try {
|
|
|
|
|
meshService?.requestNodedbReset(idNum)
|
|
|
|
|
} catch (ex: RemoteException) {
|
|
|
|
|
errormsg("RemoteException: ${ex.message}")
|
|
|
|
|
}
|
2022-08-23 11:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-03 18:15:06 -08:00
|
|
|
/**
|
|
|
|
|
* Write the persisted packet data out to a CSV file in the specified location.
|
|
|
|
|
*/
|
|
|
|
|
fun saveMessagesCSV(file_uri: Uri) {
|
|
|
|
|
viewModelScope.launch(Dispatchers.Main) {
|
|
|
|
|
// Extract distances to this device from position messages and put (node,SNR,distance) in
|
|
|
|
|
// the file_uri
|
|
|
|
|
val myNodeNum = myNodeInfo.value?.myNodeNum ?: return@launch
|
|
|
|
|
|
|
|
|
|
// Capture the current node value while we're still on main thread
|
|
|
|
|
val nodes = nodeDB.nodes.value ?: emptyMap()
|
|
|
|
|
|
2022-02-25 14:14:50 -08:00
|
|
|
val positionToPos: (MeshProtos.Position?) -> Position? = { meshPosition ->
|
|
|
|
|
meshPosition?.let { Position(it) }.takeIf {
|
|
|
|
|
it?.isValid() == true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 18:15:06 -08:00
|
|
|
writeToUri(file_uri) { writer ->
|
|
|
|
|
// Create a map of nodes keyed by their ID
|
2022-02-25 14:14:50 -08:00
|
|
|
val nodesById = nodes.values.associateBy { it.num }.toMutableMap()
|
|
|
|
|
val nodePositions = mutableMapOf<Int, MeshProtos.Position?>()
|
2022-02-03 18:15:06 -08:00
|
|
|
|
|
|
|
|
writer.appendLine("date,time,from,sender name,sender lat,sender long,rx lat,rx long,rx elevation,rx snr,distance,hop limit,payload")
|
|
|
|
|
|
|
|
|
|
// Packets are ordered by time, we keep most recent position of
|
|
|
|
|
// our device in localNodePosition.
|
|
|
|
|
val dateFormat = SimpleDateFormat("yyyy-MM-dd,HH:mm:ss", Locale.getDefault())
|
2022-09-13 22:49:38 -03:00
|
|
|
meshLogRepository.getAllLogsInReceiveOrder(Int.MAX_VALUE).first().forEach { packet ->
|
2022-02-25 14:14:50 -08:00
|
|
|
// If we get a NodeInfo packet, use it to update our position data (if valid)
|
|
|
|
|
packet.nodeInfo?.let { nodeInfo ->
|
|
|
|
|
positionToPos.invoke(nodeInfo.position)?.let { _ ->
|
|
|
|
|
nodePositions[nodeInfo.num] = nodeInfo.position
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packet.meshPacket?.let { proto ->
|
|
|
|
|
// If the packet contains position data then use it to update, if valid
|
2022-02-03 18:15:06 -08:00
|
|
|
packet.position?.let { position ->
|
2022-02-25 14:14:50 -08:00
|
|
|
positionToPos.invoke(position)?.let { _ ->
|
|
|
|
|
nodePositions[proto.from] = position
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter out of our results any packet that doesn't report SNR. This
|
|
|
|
|
// is primarily ADMIN_APP.
|
2022-10-26 17:03:51 -03:00
|
|
|
if (proto.rxSnr != 0.0f) {
|
2022-02-25 14:14:50 -08:00
|
|
|
val rxDateTime = dateFormat.format(packet.received_date)
|
|
|
|
|
val rxFrom = proto.from.toUInt()
|
|
|
|
|
val senderName = nodesById[proto.from]?.user?.longName ?: ""
|
|
|
|
|
|
|
|
|
|
// sender lat & long
|
|
|
|
|
val senderPosition = nodePositions[proto.from]
|
|
|
|
|
val senderPos = positionToPos.invoke(senderPosition)
|
|
|
|
|
val senderLat = senderPos?.latitude ?: ""
|
|
|
|
|
val senderLong = senderPos?.longitude ?: ""
|
|
|
|
|
|
|
|
|
|
// rx lat, long, and elevation
|
|
|
|
|
val rxPosition = nodePositions[myNodeNum]
|
|
|
|
|
val rxPos = positionToPos.invoke(rxPosition)
|
|
|
|
|
val rxLat = rxPos?.latitude ?: ""
|
|
|
|
|
val rxLong = rxPos?.longitude ?: ""
|
|
|
|
|
val rxAlt = rxPos?.altitude ?: ""
|
|
|
|
|
val rxSnr = "%f".format(proto.rxSnr)
|
|
|
|
|
|
|
|
|
|
// Calculate the distance if both positions are valid
|
|
|
|
|
|
|
|
|
|
val dist = if (senderPos == null || rxPos == null) {
|
|
|
|
|
""
|
2022-02-03 18:15:06 -08:00
|
|
|
} else {
|
2022-02-25 14:14:50 -08:00
|
|
|
positionToMeter(
|
|
|
|
|
rxPosition!!, // Use rxPosition but only if rxPos was valid
|
|
|
|
|
senderPosition!! // Use senderPosition but only if senderPos was valid
|
|
|
|
|
).roundToInt().toString()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val hopLimit = proto.hopLimit
|
|
|
|
|
|
|
|
|
|
val payload = when {
|
|
|
|
|
proto.decoded.portnumValue != Portnums.PortNum.TEXT_MESSAGE_APP_VALUE -> "<${proto.decoded.portnum}>"
|
|
|
|
|
proto.hasDecoded() -> "\"" + proto.decoded.payload.toStringUtf8()
|
|
|
|
|
.replace("\"", "\\\"") + "\""
|
|
|
|
|
proto.hasEncrypted() -> "${proto.encrypted.size()} encrypted bytes"
|
|
|
|
|
else -> ""
|
2022-02-03 18:15:06 -08:00
|
|
|
}
|
2022-02-25 14:14:50 -08:00
|
|
|
|
|
|
|
|
// date,time,from,sender name,sender lat,sender long,rx lat,rx long,rx elevation,rx snr,distance,hop limit,payload
|
|
|
|
|
writer.appendLine("$rxDateTime,$rxFrom,$senderName,$senderLat,$senderLong,$rxLat,$rxLong,$rxAlt,$rxSnr,$dist,$hopLimit,$payload")
|
2022-02-03 18:15:06 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private suspend inline fun writeToUri(uri: Uri, crossinline block: suspend (BufferedWriter) -> Unit) {
|
|
|
|
|
withContext(Dispatchers.IO) {
|
2022-02-13 08:09:26 -03:00
|
|
|
try {
|
|
|
|
|
app.contentResolver.openFileDescriptor(uri, "wt")?.use { parcelFileDescriptor ->
|
|
|
|
|
FileWriter(parcelFileDescriptor.fileDescriptor).use { fileWriter ->
|
|
|
|
|
BufferedWriter(fileWriter).use { writer ->
|
|
|
|
|
block.invoke(writer)
|
|
|
|
|
}
|
2022-02-03 18:15:06 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-02-13 08:09:26 -03:00
|
|
|
} catch (ex: FileNotFoundException) {
|
|
|
|
|
errormsg("Can't write file error: ${ex.message}")
|
2022-02-03 18:15:06 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:35:33 -04:00
|
|
|
|
|
|
|
|
fun parseUrl(url: String, map: MapView) {
|
|
|
|
|
viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
|
parseIt(url, map)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For Future Use
|
|
|
|
|
// model.parseUrl(
|
|
|
|
|
// "https://www.google.com/maps/d/kml?forcekml=1&mid=1FmqWhZG3PG3dY92x9yf2RlREcK7kMZs&lid=-ivSjBCePsM",
|
|
|
|
|
// map
|
|
|
|
|
// )
|
|
|
|
|
private fun parseIt(url: String, map: MapView) {
|
|
|
|
|
val kmlDoc = KmlDocument()
|
|
|
|
|
try {
|
|
|
|
|
kmlDoc.parseKMLUrl(url)
|
|
|
|
|
val kmlOverlay = kmlDoc.mKmlRoot.buildOverlay(map, null, null, kmlDoc) as FolderOverlay
|
|
|
|
|
kmlDoc.mKmlRoot.mItems
|
|
|
|
|
kmlDoc.mKmlRoot.mName
|
|
|
|
|
map.overlayManager.overlays().add(kmlOverlay)
|
|
|
|
|
} catch (ex: Exception) {
|
|
|
|
|
debug("Failed to download .kml $ex")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 17:29:17 +01:00
|
|
|
fun addQuickChatAction(name: String, value: String, mode: QuickChatAction.Mode) {
|
2022-08-11 16:43:26 +01:00
|
|
|
viewModelScope.launch(Dispatchers.Main) {
|
2022-08-12 15:35:27 +01:00
|
|
|
val action = QuickChatAction(0, name, value, mode, _quickChatActions.value.size)
|
2022-08-11 16:43:26 +01:00
|
|
|
quickChatActionRepository.insert(action)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun deleteQuickChatAction(action: QuickChatAction) {
|
|
|
|
|
viewModelScope.launch(Dispatchers.Main) {
|
2022-08-12 15:35:27 +01:00
|
|
|
quickChatActionRepository.delete(action)
|
2022-08-11 16:43:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun updateQuickChatAction(
|
|
|
|
|
action: QuickChatAction,
|
|
|
|
|
name: String?,
|
|
|
|
|
message: String?,
|
|
|
|
|
mode: QuickChatAction.Mode?
|
|
|
|
|
) {
|
|
|
|
|
viewModelScope.launch(Dispatchers.Main) {
|
|
|
|
|
val newAction = QuickChatAction(
|
|
|
|
|
action.uuid,
|
|
|
|
|
name ?: action.name,
|
|
|
|
|
message ?: action.message,
|
2022-08-12 15:35:27 +01:00
|
|
|
mode ?: action.mode,
|
|
|
|
|
action.position
|
2022-08-11 16:43:26 +01:00
|
|
|
)
|
|
|
|
|
quickChatActionRepository.update(newAction)
|
|
|
|
|
}
|
2022-08-10 17:29:17 +01:00
|
|
|
}
|
2022-08-12 15:35:27 +01:00
|
|
|
|
2022-08-16 11:46:57 +01:00
|
|
|
fun updateActionPositions(actions: List<QuickChatAction>) {
|
2022-08-12 15:35:27 +01:00
|
|
|
viewModelScope.launch(Dispatchers.Main) {
|
2022-08-16 12:25:40 +01:00
|
|
|
for (position in actions.indices) {
|
2022-08-16 11:46:57 +01:00
|
|
|
quickChatActionRepository.setItemPosition(actions[position].uuid, position)
|
|
|
|
|
}
|
2022-08-12 15:35:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-17 13:34:52 -08:00
|
|
|
}
|
2020-04-08 09:53:04 -07:00
|
|
|
|