Skip to content

Commit

Permalink
New emulate api for infrared (#731)
Browse files Browse the repository at this point in the history
**Background**

Now we can true emulate infrared with same name

**Changes**

* Update protobuf
* Check support new api
* New field in emulate config

**Test plan**

Try emulate with flipper on new api
  • Loading branch information
Programistich authored Nov 13, 2023
1 parent 04007a7 commit d5e4f82
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# 1.6.6

- [Feature] Bump deps
- [Feature] New Infrared Emulate Api
- [FIX] Fix faphub minor version supported suggest to update
- [FIX] Fix faphub manifest minor api version
- [FIX] Fix faphub manifest image base64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ object Constants {
majorVersion = 0,
minorVersion = 20
)
val API_SUPPORTED_INFRARED_EMULATE = SemVer(
majorVersion = 0,
minorVersion = 21
)
val API_SUPPORTED_GET_REQUEST = API_SUPPORTED_FLIPPER_ERROR
val API_SUPPORTED_VERSION = SemVer(majorVersion = 0, minorVersion = 3)
const val LAGS_FLIPPER_DETECT_TIMEOUT_MS = 10 * 1000L // 10 seconds
Expand Down
2 changes: 1 addition & 1 deletion components/bridge/pbutils/src/main/proto
Submodule proto updated 2 files
+4 −0 Changelog
+1 −0 application.proto
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ class InfraredEditorViewModel @VMInject constructor(
val errorRemotes = mutableListOf<Int>()

remotes.forEachIndexed { index, remote ->
val countRepeat = remotes.count { it.name == remote.name }
if (remote.name.isEmpty() || countRepeat > 1) {
if (remote.name.isEmpty()) {
errorRemotes.add(index)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ internal fun ComposableInfraredRemotes(
private fun KeyScreenState.Ready.toEmulateConfigs(): ImmutableList<EmulateConfig> {
val infraredParsed = this.parsedKey as? FlipperKeyParsed.Infrared ?: return persistentListOf()

return infraredParsed.remotes.map { name ->
return infraredParsed.remotes.mapIndexed { index, name ->
EmulateConfig(
keyType = FlipperKeyType.INFRARED,
keyPath = this.flipperKey.path,
args = name
args = name,
index = index
)
}.toImmutableList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.flipperdevices.infrared.impl.viewmodel
import androidx.lifecycle.viewModelScope
import com.flipperdevices.bridge.api.manager.ktx.state.ConnectionState
import com.flipperdevices.bridge.api.manager.ktx.state.FlipperSupportedState
import com.flipperdevices.bridge.api.utils.Constants.API_SUPPORTED_REMOTE_EMULATE
import com.flipperdevices.bridge.api.utils.Constants.API_SUPPORTED_INFRARED_EMULATE
import com.flipperdevices.bridge.dao.api.model.FlipperKeyPath
import com.flipperdevices.bridge.service.api.FlipperServiceApi
import com.flipperdevices.bridge.service.api.provider.FlipperBleServiceConsumer
Expand Down Expand Up @@ -62,7 +62,7 @@ class InfraredViewModel @VMInject constructor(
} else if (synchronizationState is SynchronizationState.InProgress) {
InfraredEmulateState.SYNCING
} else if (versionInformation == null ||
versionInformation < API_SUPPORTED_REMOTE_EMULATE
versionInformation < API_SUPPORTED_INFRARED_EMULATE
) {
InfraredEmulateState.UPDATE_FLIPPER
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ data class EmulateConfig(
val keyType: FlipperKeyType,
val keyPath: FlipperFilePath,
val minEmulateTime: Long? = null,
val args: String? = null
val args: String? = null,
val index: Int? = null
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -21,6 +22,7 @@ data class EmulateConfig(
if (keyType != other.keyType) return false
if (keyPath != other.keyPath) return false
if (args != other.args) return false
if (index != other.index) return false

return true
}
Expand All @@ -29,6 +31,7 @@ data class EmulateConfig(
var result = keyType.hashCode()
result = 31 * result + keyPath.hashCode()
result = 31 * result + (args?.hashCode() ?: 0)
result = 31 * result + (index?.hashCode() ?: 0)
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.flipperdevices.keyemulate.helpers
import com.flipperdevices.bridge.api.manager.FlipperRequestApi
import com.flipperdevices.bridge.api.model.FlipperRequestPriority
import com.flipperdevices.bridge.api.model.wrapToRequest
import com.flipperdevices.bridge.api.utils.Constants
import com.flipperdevices.bridge.dao.api.model.FlipperKeyType
import com.flipperdevices.bridge.service.api.FlipperServiceApi
import com.flipperdevices.core.di.AppGraph
Expand Down Expand Up @@ -93,10 +94,16 @@ class StartEmulateHelperImpl @Inject constructor(
return true
}

val indexEmulateSupported =
serviceApi.flipperVersionApi.isSupported(Constants.API_SUPPORTED_INFRARED_EMULATE)

info { "Support emulate by index: $indexEmulateSupported" }

return processButtonPress(
config = config,
onResultTime = onResultTime,
serviceApi = serviceApi
serviceApi = serviceApi,
isIndexEmulateSupport = indexEmulateSupported
)
}

Expand All @@ -107,6 +114,7 @@ class StartEmulateHelperImpl @Inject constructor(

private suspend fun processButtonPress(
config: EmulateConfig,
isIndexEmulateSupport: Boolean,
onResultTime: (Long) -> Unit,
serviceApi: FlipperServiceApi
): Boolean {
Expand All @@ -117,7 +125,7 @@ class StartEmulateHelperImpl @Inject constructor(
val appButtonPressResponse = serviceApi.requestApi.request(
flowOf(
main {
appButtonPressRequest = getAppButtonPressRequest(config)
appButtonPressRequest = getAppButtonPressRequest(config, isIndexEmulateSupport)
}.wrapToRequest(FlipperRequestPriority.FOREGROUND)
)
)
Expand Down Expand Up @@ -156,14 +164,24 @@ class StartEmulateHelperImpl @Inject constructor(
}
}

private fun getAppButtonPressRequest(config: EmulateConfig): Application.AppButtonPressRequest {
val configArgs = config.args
val keyType = config.keyType

return when {
keyType == FlipperKeyType.SUB_GHZ -> appButtonPressRequest {}
configArgs != null && keyType == FlipperKeyType.INFRARED -> appButtonPressRequest {
args = configArgs
private fun getAppButtonPressRequest(
config: EmulateConfig,
isIndexEmulateSupport: Boolean,
): Application.AppButtonPressRequest {
return when (config.keyType) {
FlipperKeyType.SUB_GHZ -> appButtonPressRequest {}
FlipperKeyType.INFRARED -> if (isIndexEmulateSupport) {
val indexArgs = config.index ?: error("Index args is null")
info { "#getAppButtonPressRequest by index with $config" }
appButtonPressRequest {
index = indexArgs
}
} else {
val configArgs = config.args ?: error("Config args is null")
info { "#getAppButtonPressRequest by args with $config" }
appButtonPressRequest {
args = configArgs
}
}
else -> error("Unknown button press request with config $config")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.flipperdevices.keyscreen.emulate.viewmodel.helpers

import com.flipperdevices.bridge.api.manager.FlipperRequestApi
import com.flipperdevices.bridge.api.manager.service.FlipperVersionApi
import com.flipperdevices.bridge.api.model.FlipperRequest
import com.flipperdevices.bridge.api.model.FlipperRequestPriority
import com.flipperdevices.bridge.dao.api.model.FlipperFilePath
import com.flipperdevices.bridge.dao.api.model.FlipperKeyType
import com.flipperdevices.bridge.service.api.FlipperServiceApi
import com.flipperdevices.core.data.SemVer
import com.flipperdevices.core.test.TimberRule
import com.flipperdevices.keyemulate.api.EmulateHelper
import com.flipperdevices.keyemulate.helpers.AppEmulateHelper
Expand All @@ -27,6 +29,8 @@ import io.mockk.mockk
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -57,6 +61,7 @@ class EmulateHelperTest {
private lateinit var appEmulateHelper: AppEmulateHelper
private lateinit var serviceApi: FlipperServiceApi
private lateinit var requestTestApi: FlipperRequestApi
private lateinit var versionTestApi: FlipperVersionApi
private lateinit var underTest: EmulateHelper

private fun mockAfterStart(scope: CoroutineScope) {
Expand All @@ -73,6 +78,7 @@ class EmulateHelperTest {
fun setUp() {
serviceApi = mockk {
every { requestApi } answers { requestTestApi }
every { flipperVersionApi } answers { versionTestApi }
}
flipperAppErrorHandler = mockk {
coEvery { requestError(serviceApi, FlipperRequestPriority.FOREGROUND) } answers {
Expand All @@ -89,6 +95,12 @@ class EmulateHelperTest {
flowOf(appStateResponseOk)
}
}
versionTestApi = mockk {
every { getVersionInformationFlow() } coAnswers {
MutableStateFlow(SemVer(0, 20)).asStateFlow()
}
coEvery { isSupported(any(), any()) } coAnswers { true }
}
mockFlipperRequest()
}

Expand Down

0 comments on commit d5e4f82

Please sign in to comment.