2020-12-07 20:33:29 +08:00
|
|
|
package com.geeksville.mesh.ui
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
2024-07-01 23:04:15 -03:00
|
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
|
|
import androidx.compose.foundation.layout.Row
|
|
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
|
|
|
import androidx.compose.foundation.lazy.items
|
|
|
|
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
|
|
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
2024-07-13 08:00:42 -03:00
|
|
|
import androidx.compose.foundation.text.selection.SelectionContainer
|
2024-07-01 23:04:15 -03:00
|
|
|
import androidx.compose.material.Card
|
|
|
|
|
import androidx.compose.material.Icon
|
|
|
|
|
import androidx.compose.material.Surface
|
|
|
|
|
import androidx.compose.material.Text
|
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
2024-07-13 08:00:17 -03:00
|
|
|
import androidx.compose.runtime.derivedStateOf
|
2024-07-01 23:04:15 -03:00
|
|
|
import androidx.compose.runtime.getValue
|
2024-07-13 08:00:17 -03:00
|
|
|
import androidx.compose.runtime.remember
|
2024-07-01 23:04:15 -03:00
|
|
|
import androidx.compose.ui.Modifier
|
|
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
|
|
import androidx.compose.ui.res.colorResource
|
|
|
|
|
import androidx.compose.ui.res.painterResource
|
|
|
|
|
import androidx.compose.ui.text.SpanStyle
|
|
|
|
|
import androidx.compose.ui.text.TextStyle
|
|
|
|
|
import androidx.compose.ui.text.buildAnnotatedString
|
|
|
|
|
import androidx.compose.ui.text.font.FontFamily
|
|
|
|
|
import androidx.compose.ui.text.font.FontStyle
|
|
|
|
|
import androidx.compose.ui.text.font.FontWeight
|
2024-07-13 07:26:45 -03:00
|
|
|
import androidx.compose.ui.tooling.preview.PreviewLightDark
|
2024-07-01 23:04:15 -03:00
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
|
import androidx.compose.ui.unit.sp
|
2020-12-07 20:33:29 +08:00
|
|
|
import androidx.fragment.app.Fragment
|
2024-04-07 16:56:24 -03:00
|
|
|
import androidx.fragment.app.viewModels
|
2024-07-01 23:04:15 -03:00
|
|
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
2020-12-07 20:33:29 +08:00
|
|
|
import com.geeksville.mesh.R
|
2024-02-02 18:55:41 -08:00
|
|
|
import com.geeksville.mesh.database.entity.MeshLog
|
2022-09-13 22:49:38 -03:00
|
|
|
import com.geeksville.mesh.databinding.FragmentDebugBinding
|
2024-04-07 16:56:24 -03:00
|
|
|
import com.geeksville.mesh.model.DebugViewModel
|
2024-07-01 23:04:15 -03:00
|
|
|
import com.geeksville.mesh.ui.theme.AppTheme
|
2022-02-08 13:50:21 -08:00
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
2024-07-01 23:04:15 -03:00
|
|
|
import java.text.DateFormat
|
2024-02-02 18:55:41 -08:00
|
|
|
import java.util.Locale
|
2020-12-07 20:33:29 +08:00
|
|
|
|
2022-02-08 13:50:21 -08:00
|
|
|
@AndroidEntryPoint
|
2020-12-07 20:33:29 +08:00
|
|
|
class DebugFragment : Fragment() {
|
|
|
|
|
|
2022-09-13 22:49:38 -03:00
|
|
|
private var _binding: FragmentDebugBinding? = null
|
2021-03-29 20:33:06 +08:00
|
|
|
|
2020-12-07 20:33:29 +08:00
|
|
|
// This property is only valid between onCreateView and onDestroyView.
|
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
|
2024-04-07 16:56:24 -03:00
|
|
|
private val model: DebugViewModel by viewModels()
|
2020-12-07 20:33:29 +08:00
|
|
|
|
|
|
|
|
override fun onCreateView(
|
2024-10-13 23:02:05 -03:00
|
|
|
inflater: LayoutInflater,
|
|
|
|
|
container: ViewGroup?,
|
2020-12-07 20:33:29 +08:00
|
|
|
savedInstanceState: Bundle?
|
2022-07-29 21:28:09 -03:00
|
|
|
): View {
|
2022-09-13 22:49:38 -03:00
|
|
|
_binding = FragmentDebugBinding.inflate(inflater, container, false)
|
2020-12-07 20:33:29 +08:00
|
|
|
return binding.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
|
|
|
|
binding.clearButton.setOnClickListener {
|
2022-09-13 22:49:38 -03:00
|
|
|
model.deleteAllLogs()
|
2020-12-07 20:33:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 20:33:06 +08:00
|
|
|
binding.closeButton.setOnClickListener {
|
|
|
|
|
parentFragmentManager.popBackStack()
|
2020-12-07 20:33:29 +08:00
|
|
|
}
|
2024-07-01 23:04:15 -03:00
|
|
|
|
|
|
|
|
binding.debugListView.setContent {
|
|
|
|
|
val listState = rememberLazyListState()
|
|
|
|
|
val logs by model.meshLog.collectAsStateWithLifecycle()
|
|
|
|
|
|
2024-07-13 08:00:17 -03:00
|
|
|
val shouldAutoScroll by remember { derivedStateOf { listState.firstVisibleItemIndex < 3 } }
|
|
|
|
|
if (shouldAutoScroll) {
|
|
|
|
|
LaunchedEffect(logs) {
|
|
|
|
|
if (!listState.isScrollInProgress) {
|
|
|
|
|
listState.scrollToItem(0)
|
|
|
|
|
}
|
2024-07-01 23:04:15 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppTheme {
|
2024-07-13 08:00:42 -03:00
|
|
|
SelectionContainer {
|
|
|
|
|
LazyColumn(
|
|
|
|
|
modifier = Modifier.fillMaxSize(),
|
|
|
|
|
state = listState,
|
|
|
|
|
) {
|
|
|
|
|
items(logs, key = { it.uuid }) { log -> DebugItem(annotateMeshLog(log)) }
|
|
|
|
|
}
|
2024-07-01 23:04:15 -03:00
|
|
|
}
|
|
|
|
|
}
|
2022-02-08 13:50:21 -08:00
|
|
|
}
|
2020-12-07 20:33:29 +08:00
|
|
|
}
|
2023-04-03 18:03:55 -03:00
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
|
super.onDestroyView()
|
|
|
|
|
_binding = null
|
|
|
|
|
}
|
2024-02-02 18:55:41 -08:00
|
|
|
|
|
|
|
|
/**
|
2024-07-01 23:04:15 -03:00
|
|
|
* Transform the input [MeshLog] by enhancing the raw message with annotations.
|
2024-02-02 18:55:41 -08:00
|
|
|
*/
|
2024-07-01 23:04:15 -03:00
|
|
|
private fun annotateMeshLog(meshLog: MeshLog): MeshLog {
|
|
|
|
|
val annotated = when (meshLog.message_type) {
|
|
|
|
|
"Packet" -> {
|
|
|
|
|
meshLog.meshPacket?.let { packet ->
|
|
|
|
|
annotateRawMessage(meshLog.raw_message, packet.from, packet.to)
|
2024-02-02 18:55:41 -08:00
|
|
|
}
|
2024-07-01 23:04:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"NodeInfo" -> {
|
|
|
|
|
meshLog.nodeInfo?.let { nodeInfo ->
|
|
|
|
|
annotateRawMessage(meshLog.raw_message, nodeInfo.num)
|
2024-02-02 18:55:41 -08:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-01 23:04:15 -03:00
|
|
|
|
|
|
|
|
"MyNodeInfo" -> {
|
|
|
|
|
meshLog.myNodeInfo?.let { nodeInfo ->
|
|
|
|
|
annotateRawMessage(meshLog.raw_message, nodeInfo.myNodeNum)
|
|
|
|
|
}
|
2024-02-02 18:55:41 -08:00
|
|
|
}
|
2024-07-01 23:04:15 -03:00
|
|
|
|
|
|
|
|
else -> null
|
|
|
|
|
}
|
|
|
|
|
return if (annotated == null) {
|
|
|
|
|
meshLog
|
|
|
|
|
} else {
|
|
|
|
|
meshLog.copy(raw_message = annotated)
|
2024-02-02 18:55:41 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Annotate the raw message string with the node IDs provided, in hex, if they are present.
|
|
|
|
|
*/
|
|
|
|
|
private fun annotateRawMessage(rawMessage: String, vararg nodeIds: Int): String {
|
|
|
|
|
val msg = StringBuilder(rawMessage)
|
|
|
|
|
var mutated = false
|
|
|
|
|
nodeIds.forEach { nodeId ->
|
|
|
|
|
mutated = mutated or msg.annotateNodeId(nodeId)
|
|
|
|
|
}
|
|
|
|
|
return if (mutated) {
|
|
|
|
|
return msg.toString()
|
|
|
|
|
} else {
|
|
|
|
|
rawMessage
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Look for a single node ID integer in the string and annotate it with the hex equivalent
|
|
|
|
|
* if found.
|
|
|
|
|
*/
|
|
|
|
|
private fun StringBuilder.annotateNodeId(nodeId: Int): Boolean {
|
|
|
|
|
val nodeIdStr = nodeId.toUInt().toString()
|
|
|
|
|
indexOf(nodeIdStr).takeIf { it >= 0 }?.let { idx ->
|
|
|
|
|
insert(idx + nodeIdStr.length, " (${nodeId.asNodeId()})")
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun Int.asNodeId(): String {
|
|
|
|
|
return "!%08x".format(Locale.getDefault(), this)
|
|
|
|
|
}
|
2024-07-01 23:04:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val REGEX_ANNOTATED_NODE_ID = Regex("\\(![0-9a-fA-F]{8}\\)$", RegexOption.MULTILINE)
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
internal fun DebugItem(log: MeshLog) {
|
|
|
|
|
val timeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM)
|
|
|
|
|
|
|
|
|
|
Card(
|
|
|
|
|
modifier = Modifier
|
|
|
|
|
.fillMaxWidth()
|
|
|
|
|
.padding(4.dp),
|
|
|
|
|
elevation = 4.dp,
|
|
|
|
|
shape = RoundedCornerShape(12.dp),
|
|
|
|
|
) {
|
|
|
|
|
Surface {
|
|
|
|
|
Column(
|
|
|
|
|
modifier = Modifier.padding(8.dp)
|
|
|
|
|
) {
|
|
|
|
|
Row(
|
|
|
|
|
modifier = Modifier
|
|
|
|
|
.fillMaxWidth()
|
|
|
|
|
.padding(bottom = 8.dp),
|
|
|
|
|
horizontalArrangement = Arrangement.SpaceBetween,
|
|
|
|
|
) {
|
|
|
|
|
Text(
|
|
|
|
|
text = log.message_type,
|
|
|
|
|
modifier = Modifier.weight(1f),
|
|
|
|
|
style = TextStyle(fontWeight = FontWeight.Bold),
|
|
|
|
|
)
|
|
|
|
|
Icon(
|
|
|
|
|
painterResource(R.drawable.cloud_download_outline_24),
|
|
|
|
|
contentDescription = null,
|
|
|
|
|
tint = Color.Gray.copy(alpha = 0.6f),
|
|
|
|
|
modifier = Modifier.padding(end = 8.dp),
|
|
|
|
|
)
|
|
|
|
|
Text(
|
|
|
|
|
text = timeFormat.format(log.received_date),
|
|
|
|
|
style = TextStyle(fontWeight = FontWeight.Bold),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val style = SpanStyle(
|
|
|
|
|
color = colorResource(id = R.color.colorAnnotation),
|
|
|
|
|
fontStyle = FontStyle.Italic,
|
|
|
|
|
)
|
|
|
|
|
val annotatedString = buildAnnotatedString {
|
|
|
|
|
append(log.raw_message)
|
|
|
|
|
REGEX_ANNOTATED_NODE_ID.findAll(log.raw_message).toList().reversed().forEach {
|
|
|
|
|
addStyle(style = style, start = it.range.first, end = it.range.last + 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Text(
|
|
|
|
|
text = annotatedString,
|
|
|
|
|
softWrap = false,
|
|
|
|
|
style = TextStyle(
|
|
|
|
|
fontSize = 9.sp,
|
|
|
|
|
fontFamily = FontFamily.Monospace,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-13 07:26:45 -03:00
|
|
|
@PreviewLightDark
|
2024-07-01 23:04:15 -03:00
|
|
|
@Composable
|
|
|
|
|
private fun DebugScreenPreview() {
|
|
|
|
|
AppTheme {
|
|
|
|
|
DebugItem(
|
|
|
|
|
MeshLog(
|
|
|
|
|
uuid = "",
|
|
|
|
|
message_type = "NodeInfo",
|
|
|
|
|
received_date = 1601251258000L,
|
|
|
|
|
raw_message = "from: 2885173132\n" +
|
|
|
|
|
"decoded {\n" +
|
|
|
|
|
" position {\n" +
|
|
|
|
|
" altitude: 60\n" +
|
|
|
|
|
" battery_level: 81\n" +
|
|
|
|
|
" latitude_i: 411111136\n" +
|
|
|
|
|
" longitude_i: -711111805\n" +
|
|
|
|
|
" time: 1600390966\n" +
|
|
|
|
|
" }\n" +
|
|
|
|
|
"}\n" +
|
|
|
|
|
"hop_limit: 3\n" +
|
|
|
|
|
"id: 1737414295\n" +
|
|
|
|
|
"rx_snr: 9.5\n" +
|
|
|
|
|
"rx_time: 316400569\n" +
|
|
|
|
|
"to: -1409790708",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|