Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add fixed position admin messages #946

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class RadioConfigViewModel @Inject constructor(
// A map from nodeNum to NodeInfo
val nodes: StateFlow<Map<Int, NodeInfo>> get() = radioConfigRepository.nodeDBbyNum

private val _myNodeInfo = MutableStateFlow<MyNodeInfo?>(null)
val myNodeInfo get() = _myNodeInfo
val myNodeInfo: StateFlow<MyNodeInfo?> get() = radioConfigRepository.myNodeInfo
val ourNodeInfo: StateFlow<NodeInfo?> get() = radioConfigRepository.ourNodeInfo

private val requestIds = MutableStateFlow<HashMap<Int, Boolean>>(hashMapOf())
private val _radioConfigState = MutableStateFlow(RadioConfigState())
Expand All @@ -81,10 +81,6 @@ class RadioConfigViewModel @Inject constructor(
val currentDeviceProfile get() = _currentDeviceProfile.value

init {
radioConfigRepository.myNodeInfoFlow().onEach {
_myNodeInfo.value = it
}.launchIn(viewModelScope)

radioConfigRepository.deviceProfileFlow.onEach {
_currentDeviceProfile.value = it
}.launchIn(viewModelScope)
Expand All @@ -100,7 +96,6 @@ class RadioConfigViewModel @Inject constructor(

val myNodeNum get() = myNodeInfo.value?.myNodeNum
val maxChannels get() = myNodeInfo.value?.maxChannels ?: 8
private val ourNodeInfo: NodeInfo? get() = nodes.value[myNodeNum]

override fun onCleared() {
super.onCleared()
Expand Down Expand Up @@ -263,14 +258,16 @@ class RadioConfigViewModel @Inject constructor(
"Request NodeDB reset error"
)

fun requestPosition(destNum: Int, position: Position = Position(0.0, 0.0, 0)) {
fun setFixedPosition(position: Position) {
try {
meshService?.requestPosition(destNum, position)
meshService?.requestPosition(myNodeNum ?: return, position)
} catch (ex: RemoteException) {
errormsg("Request position error: ${ex.message}")
}
}

fun removeFixedPosition() = setFixedPosition(Position(0.0, 0.0, 0))

// Set the radio config (also updates our saved copy in preferences)
fun setConfig(config: ConfigProtos.Config) {
setRemoteConfig(myNodeNum ?: return, config)
Expand Down Expand Up @@ -323,7 +320,7 @@ class RadioConfigViewModel @Inject constructor(
fun installProfile(protobuf: DeviceProfile) = with(protobuf) {
_deviceProfile.value = null
// meshService?.beginEditSettings()
if (hasLongName() || hasShortName()) ourNodeInfo?.user?.let {
if (hasLongName() || hasShortName()) ourNodeInfo.value?.user?.let {
val user = it.copy(
longName = if (hasLongName()) longName else it.longName,
shortName = if (hasShortName()) shortName else it.shortName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class RadioConfigRepository @Inject constructor(
fun myNodeInfoFlow(): Flow<MyNodeInfo?> = nodeDB.myNodeInfoFlow()
suspend fun getMyNodeInfo(): MyNodeInfo? = myNodeInfoFlow().firstOrNull()
val myNodeInfo: StateFlow<MyNodeInfo?> get() = nodeDB.myNodeInfo
val ourNodeInfo: StateFlow<NodeInfo?> get() = nodeDB.ourNodeInfo

val nodeDBbyNum: StateFlow<Map<Int, NodeInfo>> get() = nodeDB.nodeDBbyNum
val nodeDBbyID: StateFlow<Map<String, NodeInfo>> get() = nodeDB.nodeDBbyID
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/geeksville/mesh/service/MeshService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1864,13 +1864,24 @@ class MeshService : Service(), Logging {
}

override fun requestPosition(destNum: Int, position: Position) = toRemoteExceptions {
if (position == Position(0.0, 0.0, 0)) {
if (destNum != myNodeNum) {
// request position
sendPosition(destNum = destNum, wantResponse = true)
} else {
// send fixed position (local only/no remote method, so we force destNum to null)
val (lat, lon, alt) = position
sendPosition(destNum = null, lat = lat, lon = lon, alt = alt)
sendToRadio(newMeshPacketTo(destNum).buildAdminPacket {
if (position != Position(0.0, 0.0, 0)) {
setFixedPosition = position {
longitudeI = Position.degI(lon)
latitudeI = Position.degI(lat)
altitude = alt
}
} else {
removeFixedPosition = true
}
})
}
}

Expand Down
17 changes: 13 additions & 4 deletions app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ sealed class ResponseState<out T> {
data class Loading(var total: Int = 1, var completed: Int = 0) : ResponseState<Nothing>()
data class Success<T>(val result: T) : ResponseState<T>()
data class Error(val error: String) : ResponseState<Nothing>()

fun isWaiting() = this !is Empty
}

@Composable
Expand Down Expand Up @@ -237,7 +239,7 @@ fun RadioConfigNavHost(
var location by remember(node) { mutableStateOf(node?.position) } // FIXME

val deviceProfile by viewModel.deviceProfile.collectAsStateWithLifecycle()
val isWaiting = radioConfigState.responseState !is ResponseState.Empty
val isWaiting = radioConfigState.responseState.isWaiting()
var showEditDeviceProfileDialog by remember { mutableStateOf(false) }

val importConfigLauncher = rememberLauncherForActivityResult(
Expand Down Expand Up @@ -401,9 +403,16 @@ fun RadioConfigNavHost(
positionConfig = radioConfigState.radioConfig.position,
enabled = connected,
onSaveClicked = { locationInput, positionInput ->
if (locationInput != location && positionInput.fixedPosition) {
locationInput?.let { viewModel.requestPosition(destNum, it) }
location = locationInput
if (positionInput.fixedPosition) {
if (locationInput != null && locationInput != location) {
viewModel.setFixedPosition(locationInput)
location = locationInput
}
} else {
if (radioConfigState.radioConfig.position.fixedPosition) {
// fixed position changed from enabled to disabled
viewModel.removeFixedPosition()
}
}
val config = config { position = positionInput }
viewModel.setRemoteConfig(destNum, config)
Expand Down