Skip to content

Commit

Permalink
refactor(logs): add optional portNum parameter to getMeshPacketsFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Oct 23, 2024
1 parent fa24dd4 commit eb80d41
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,25 @@ class MeshLogRepository @Inject constructor(private val meshLogDaoLazy: dagger.L
private fun parseTelemetryLog(log: MeshLog): Telemetry? =
runCatching { Telemetry.parseFrom(log.fromRadio.packet.decoded.payload) }.getOrNull()

private fun parseMeshPacket(log: MeshLog): MeshPacket? =
runCatching { log.meshPacket }.getOrNull()

@OptIn(ExperimentalCoroutinesApi::class)
fun getTelemetryFrom(nodeNum: Int): Flow<List<Telemetry>> =
meshLogDao.getLogsFrom(nodeNum, Portnums.PortNum.TELEMETRY_APP_VALUE, MAX_MESH_PACKETS)
.distinctUntilChanged()
.mapLatest { list -> list.mapNotNull(::parseTelemetryLog) }
.flowOn(Dispatchers.IO)

/*
* Retrieves MeshPackets matching 'nodeNum' and 'portNum'.
* If 'portNum' is not specified, returns all MeshPackets. Otherwise, filters by 'portNum'.
*/
@OptIn(ExperimentalCoroutinesApi::class)
fun getMeshPacketsFrom(nodeNum: Int): Flow<List<MeshPacket>> =
meshLogDao.getLogsFrom(nodeNum, Portnums.PortNum.TELEMETRY_APP_VALUE, MAX_MESH_PACKETS)
.distinctUntilChanged()
.mapLatest { list -> list.mapNotNull(::parseMeshPacket) }
.flowOn(Dispatchers.IO)
fun getMeshPacketsFrom(
nodeNum: Int,
portNum: Int = Portnums.PortNum.UNKNOWN_APP_VALUE,
): Flow<List<MeshPacket>> = meshLogDao.getLogsFrom(nodeNum, portNum, MAX_MESH_PACKETS)
.distinctUntilChanged()
.mapLatest { list -> list.map { it.fromRadio.packet } }
.flowOn(Dispatchers.IO)

suspend fun insert(log: MeshLog) = withContext(Dispatchers.IO) {
meshLogDao.insert(log)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ interface MeshLogDao {
@Query("SELECT * FROM log ORDER BY received_date ASC LIMIT 0,:maxItem")
fun getAllLogsInReceiveOrder(maxItem: Int): Flow<List<MeshLog>>

/*
* Retrieves MeshPackets matching 'from_num' (nodeNum) and 'port_num' (PortNum).
* If 'portNum' is 0, returns all MeshPackets. Otherwise, filters by 'port_num'.
*/
@Query(
"""
SELECT * FROM log
WHERE from_num = :fromNum AND port_num = :portNum
WHERE from_num = :fromNum AND (:portNum = 0 AND port_num != 0 OR port_num = :portNum)
ORDER BY received_date DESC LIMIT 0,:maxItem
"""
)
Expand Down

0 comments on commit eb80d41

Please sign in to comment.