|
| 1 | +/* |
| 2 | + * Copyright (c) 2025-2026 Meshtastic LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package com.geeksville.mesh.domain.usecase |
| 18 | + |
| 19 | +import android.hardware.usb.UsbManager |
| 20 | +import android.net.nsd.NsdServiceInfo |
| 21 | +import com.geeksville.mesh.model.DeviceListEntry |
| 22 | +import com.geeksville.mesh.model.getMeshtasticShortName |
| 23 | +import com.geeksville.mesh.repository.network.NetworkRepository |
| 24 | +import com.geeksville.mesh.repository.network.NetworkRepository.Companion.toAddressString |
| 25 | +import com.geeksville.mesh.repository.radio.RadioInterfaceService |
| 26 | +import com.geeksville.mesh.repository.usb.UsbRepository |
| 27 | +import kotlinx.coroutines.flow.Flow |
| 28 | +import kotlinx.coroutines.flow.combine |
| 29 | +import kotlinx.coroutines.flow.map |
| 30 | +import org.jetbrains.compose.resources.getString |
| 31 | +import org.meshtastic.core.ble.BluetoothRepository |
| 32 | +import org.meshtastic.core.data.repository.NodeRepository |
| 33 | +import org.meshtastic.core.database.DatabaseManager |
| 34 | +import org.meshtastic.core.database.model.Node |
| 35 | +import org.meshtastic.core.datastore.RecentAddressesDataSource |
| 36 | +import org.meshtastic.core.datastore.model.RecentAddress |
| 37 | +import org.meshtastic.core.resources.Res |
| 38 | +import org.meshtastic.core.resources.meshtastic |
| 39 | +import java.util.Locale |
| 40 | +import javax.inject.Inject |
| 41 | + |
| 42 | +data class DiscoveredDevices( |
| 43 | + val bleDevices: List<DeviceListEntry>, |
| 44 | + val usbDevices: List<DeviceListEntry>, |
| 45 | + val discoveredTcpDevices: List<DeviceListEntry>, |
| 46 | + val recentTcpDevices: List<DeviceListEntry>, |
| 47 | +) |
| 48 | + |
| 49 | +@Suppress("LongParameterList") |
| 50 | +class GetDiscoveredDevicesUseCase |
| 51 | +@Inject |
| 52 | +constructor( |
| 53 | + private val bluetoothRepository: BluetoothRepository, |
| 54 | + private val networkRepository: NetworkRepository, |
| 55 | + private val recentAddressesDataSource: RecentAddressesDataSource, |
| 56 | + private val nodeRepository: NodeRepository, |
| 57 | + private val databaseManager: DatabaseManager, |
| 58 | + private val usbRepository: UsbRepository, |
| 59 | + private val radioInterfaceService: RadioInterfaceService, |
| 60 | + private val usbManagerLazy: dagger.Lazy<UsbManager>, |
| 61 | +) { |
| 62 | + private val suffixLength = 4 |
| 63 | + |
| 64 | + @Suppress("LongMethod", "CyclomaticComplexMethod") |
| 65 | + fun invoke(showMock: Boolean): Flow<DiscoveredDevices> { |
| 66 | + val nodeDb = nodeRepository.nodeDBbyNum |
| 67 | + |
| 68 | + val bondedBleFlow = bluetoothRepository.state.map { ble -> ble.bondedDevices.map { DeviceListEntry.Ble(it) } } |
| 69 | + |
| 70 | + val processedTcpFlow = |
| 71 | + combine(networkRepository.resolvedList, recentAddressesDataSource.recentAddresses) { |
| 72 | + tcpServices, |
| 73 | + recentList, |
| 74 | + -> |
| 75 | + val recentMap = recentList.associateBy({ it.address }) { it.name } |
| 76 | + tcpServices |
| 77 | + .map { service -> |
| 78 | + val address = "t${service.toAddressString()}" |
| 79 | + val txtRecords = service.attributes |
| 80 | + val shortNameBytes = txtRecords["shortname"] |
| 81 | + val idBytes = txtRecords["id"] |
| 82 | + |
| 83 | + val shortName = |
| 84 | + shortNameBytes?.let { String(it, Charsets.UTF_8) } ?: getString(Res.string.meshtastic) |
| 85 | + val deviceId = idBytes?.let { String(it, Charsets.UTF_8) }?.replace("!", "") |
| 86 | + var displayName = recentMap[address] ?: shortName |
| 87 | + if (deviceId != null && (displayName.split("_").none { it == deviceId })) { |
| 88 | + displayName += "_$deviceId" |
| 89 | + } |
| 90 | + DeviceListEntry.Tcp(displayName, address) |
| 91 | + } |
| 92 | + .sortedBy { it.name } |
| 93 | + } |
| 94 | + |
| 95 | + val usbDevicesFlow = |
| 96 | + usbRepository.serialDevices.map { usb -> |
| 97 | + usb.map { (_, d) -> DeviceListEntry.Usb(radioInterfaceService, usbManagerLazy.get(), d) } |
| 98 | + } |
| 99 | + |
| 100 | + return combine( |
| 101 | + nodeDb, |
| 102 | + bondedBleFlow, |
| 103 | + processedTcpFlow, |
| 104 | + usbDevicesFlow, |
| 105 | + networkRepository.resolvedList, |
| 106 | + recentAddressesDataSource.recentAddresses, |
| 107 | + ) { args: Array<Any> -> |
| 108 | + @Suppress("UNCHECKED_CAST", "MagicNumber") |
| 109 | + val db = args[0] as Map<Int, Node> |
| 110 | + |
| 111 | + @Suppress("UNCHECKED_CAST", "MagicNumber") |
| 112 | + val bondedBle = args[1] as List<DeviceListEntry.Ble> |
| 113 | + |
| 114 | + @Suppress("UNCHECKED_CAST", "MagicNumber") |
| 115 | + val processedTcp = args[2] as List<DeviceListEntry.Tcp> |
| 116 | + |
| 117 | + @Suppress("UNCHECKED_CAST", "MagicNumber") |
| 118 | + val usbDevices = args[3] as List<DeviceListEntry.Usb> |
| 119 | + |
| 120 | + @Suppress("UNCHECKED_CAST", "MagicNumber") |
| 121 | + val resolved = args[4] as List<NsdServiceInfo> |
| 122 | + |
| 123 | + @Suppress("UNCHECKED_CAST", "MagicNumber") |
| 124 | + val recentList = args[5] as List<RecentAddress> |
| 125 | + |
| 126 | + val bleForUi = |
| 127 | + bondedBle |
| 128 | + .map { entry -> |
| 129 | + val matchingNode = |
| 130 | + if (databaseManager.hasDatabaseFor(entry.fullAddress)) { |
| 131 | + db.values.find { node -> |
| 132 | + val suffix = entry.peripheral.getMeshtasticShortName()?.lowercase(Locale.ROOT) |
| 133 | + suffix != null && node.user.id.lowercase(Locale.ROOT).endsWith(suffix) |
| 134 | + } |
| 135 | + } else { |
| 136 | + null |
| 137 | + } |
| 138 | + entry.copy(node = matchingNode) |
| 139 | + } |
| 140 | + .sortedBy { it.name } |
| 141 | + |
| 142 | + val usbForUi = |
| 143 | + (usbDevices + if (showMock) listOf(DeviceListEntry.Mock("Demo Mode")) else emptyList()).map { entry -> |
| 144 | + val matchingNode = |
| 145 | + if (databaseManager.hasDatabaseFor(entry.fullAddress)) { |
| 146 | + db.values.find { node -> |
| 147 | + val suffix = entry.name.split("_").lastOrNull()?.lowercase(Locale.ROOT) |
| 148 | + suffix != null && |
| 149 | + suffix.length >= suffixLength && |
| 150 | + node.user.id.lowercase(Locale.ROOT).endsWith(suffix) |
| 151 | + } |
| 152 | + } else { |
| 153 | + null |
| 154 | + } |
| 155 | + entry.copy(node = matchingNode) |
| 156 | + } |
| 157 | + |
| 158 | + val discoveredTcpForUi = |
| 159 | + processedTcp.map { entry -> |
| 160 | + val matchingNode = |
| 161 | + if (databaseManager.hasDatabaseFor(entry.fullAddress)) { |
| 162 | + val resolvedService = resolved.find { "t${it.toAddressString()}" == entry.fullAddress } |
| 163 | + val deviceId = resolvedService?.attributes?.get("id")?.let { String(it, Charsets.UTF_8) } |
| 164 | + db.values.find { node -> |
| 165 | + node.user.id == deviceId || (deviceId != null && node.user.id == "!$deviceId") |
| 166 | + } |
| 167 | + } else { |
| 168 | + null |
| 169 | + } |
| 170 | + entry.copy(node = matchingNode) |
| 171 | + } |
| 172 | + |
| 173 | + val discoveredTcpAddresses = processedTcp.map { it.fullAddress }.toSet() |
| 174 | + val recentTcpForUi = |
| 175 | + recentList |
| 176 | + .filterNot { discoveredTcpAddresses.contains(it.address) } |
| 177 | + .map { DeviceListEntry.Tcp(it.name, it.address) } |
| 178 | + .map { entry -> |
| 179 | + val matchingNode = |
| 180 | + if (databaseManager.hasDatabaseFor(entry.fullAddress)) { |
| 181 | + val suffix = entry.name.split("_").lastOrNull()?.lowercase(Locale.ROOT) |
| 182 | + db.values.find { node -> |
| 183 | + suffix != null && |
| 184 | + suffix.length >= suffixLength && |
| 185 | + node.user.id.lowercase(Locale.ROOT).endsWith(suffix) |
| 186 | + } |
| 187 | + } else { |
| 188 | + null |
| 189 | + } |
| 190 | + entry.copy(node = matchingNode) |
| 191 | + } |
| 192 | + .sortedBy { it.name } |
| 193 | + |
| 194 | + DiscoveredDevices( |
| 195 | + bleDevices = bleForUi, |
| 196 | + usbDevices = usbForUi, |
| 197 | + discoveredTcpDevices = discoveredTcpForUi, |
| 198 | + recentTcpDevices = recentTcpForUi, |
| 199 | + ) |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments