@@ -42,12 +42,6 @@ import com.geeksville.mesh.Portnums.PortNum
42
42
import kotlinx.coroutines.flow.MutableStateFlow
43
43
import kotlinx.coroutines.flow.asStateFlow
44
44
import com.geeksville.mesh.repository.datastore.RadioConfigRepository
45
- import com.google.protobuf.InvalidProtocolBufferException
46
- import com.geeksville.mesh.MeshProtos
47
- import com.geeksville.mesh.TelemetryProtos
48
- import com.geeksville.mesh.AdminProtos
49
- import com.geeksville.mesh.PaxcountProtos
50
- import com.geeksville.mesh.StoreAndForwardProtos
51
45
52
46
data class SearchMatch (
53
47
val logIndex : Int ,
@@ -162,7 +156,6 @@ class LogFilterManager {
162
156
}
163
157
}
164
158
165
- @Suppress(" TooManyFunctions" )
166
159
@HiltViewModel
167
160
class DebugViewModel @Inject constructor(
168
161
private val meshLogRepository : MeshLogRepository ,
@@ -213,41 +206,29 @@ class DebugViewModel @Inject constructor(
213
206
messageType = log.message_type,
214
207
formattedReceivedDate = TIME_FORMAT .format(log.received_date),
215
208
logMessage = annotateMeshLogMessage(log),
216
- decodedPayload = decodePayloadFromMeshLog(log),
217
209
)
218
210
}.toImmutableList()
219
211
220
212
/* *
221
213
* Transform the input [MeshLog] by enhancing the raw message with annotations.
222
214
*/
223
215
private fun annotateMeshLogMessage (meshLog : MeshLog ): String {
224
- return when (meshLog.message_type) {
216
+ val annotated = when (meshLog.message_type) {
225
217
" Packet" -> meshLog.meshPacket?.let { packet ->
226
- annotatePacketLog(packet)
227
- } ? : meshLog.raw_message
218
+ annotateRawMessage(meshLog.raw_message, packet.from, packet.to)
219
+ }
220
+
228
221
" NodeInfo" -> meshLog.nodeInfo?.let { nodeInfo ->
229
222
annotateRawMessage(meshLog.raw_message, nodeInfo.num)
230
- } ? : meshLog.raw_message
223
+ }
224
+
231
225
" MyNodeInfo" -> meshLog.myNodeInfo?.let { nodeInfo ->
232
226
annotateRawMessage(meshLog.raw_message, nodeInfo.myNodeNum)
233
- } ? : meshLog.raw_message
234
- else -> meshLog.raw_message
235
- }
236
- }
227
+ }
237
228
238
- private fun annotatePacketLog (packet : MeshProtos .MeshPacket ): String {
239
- val builder = packet.toBuilder()
240
- val hasDecoded = builder.hasDecoded()
241
- val decoded = if (hasDecoded) builder.decoded else null
242
- if (hasDecoded) builder.clearDecoded()
243
- val baseText = builder.build().toString().trimEnd()
244
- val result = if (hasDecoded && decoded != null ) {
245
- val decodedText = decoded.toString().trimEnd().prependIndent(" " )
246
- " $baseText \n decoded {\n $decodedText \n }"
247
- } else {
248
- baseText
229
+ else -> null
249
230
}
250
- return annotateRawMessage(result, packet.from, packet.to)
231
+ return annotated ? : meshLog.raw_message
251
232
}
252
233
253
234
/* *
@@ -293,7 +274,6 @@ class DebugViewModel @Inject constructor(
293
274
val messageType : String ,
294
275
val formattedReceivedDate : String ,
295
276
val logMessage : String ,
296
- val decodedPayload : String? = null ,
297
277
)
298
278
299
279
companion object {
@@ -315,54 +295,4 @@ class DebugViewModel @Inject constructor(
315
295
}
316
296
317
297
fun setSelectedLogId (id : String? ) { _selectedLogId .value = id }
318
-
319
- /* *
320
- * Attempts to fully decode the payload of a MeshLog's MeshPacket using the appropriate protobuf definition,
321
- * based on the portnum of the packet.
322
- *
323
- * For known portnums, the payload is parsed into its corresponding proto message and returned as a string.
324
- * For text and alert messages, the payload is interpreted as UTF-8 text.
325
- * For unknown portnums, the payload is shown as a hex string.
326
- *
327
- * @param log The MeshLog containing the packet and payload to decode.
328
- * @return A human-readable string representation of the decoded payload, or an error message if decoding fails,
329
- * or null if the log does not contain a decodable packet.
330
- */
331
- private fun decodePayloadFromMeshLog (log : MeshLog ): String? {
332
- var result: String? = null
333
- val packet = log.meshPacket
334
- if (packet == null || ! packet.hasDecoded()) {
335
- result = null
336
- } else {
337
- val portnum = packet.decoded.portnumValue
338
- val payload = packet.decoded.payload.toByteArray()
339
- result = try {
340
- when (portnum) {
341
- PortNum .TEXT_MESSAGE_APP_VALUE ,
342
- PortNum .ALERT_APP_VALUE ->
343
- payload.toString(Charsets .UTF_8 )
344
- PortNum .POSITION_APP_VALUE ->
345
- MeshProtos .Position .parseFrom(payload).toString()
346
- PortNum .WAYPOINT_APP_VALUE ->
347
- MeshProtos .Waypoint .parseFrom(payload).toString()
348
- PortNum .NODEINFO_APP_VALUE ->
349
- MeshProtos .User .parseFrom(payload).toString()
350
- PortNum .TELEMETRY_APP_VALUE ->
351
- TelemetryProtos .Telemetry .parseFrom(payload).toString()
352
- PortNum .ROUTING_APP_VALUE ->
353
- MeshProtos .Routing .parseFrom(payload).toString()
354
- PortNum .ADMIN_APP_VALUE ->
355
- AdminProtos .AdminMessage .parseFrom(payload).toString()
356
- PortNum .PAXCOUNTER_APP_VALUE ->
357
- PaxcountProtos .Paxcount .parseFrom(payload).toString()
358
- PortNum .STORE_FORWARD_APP_VALUE ->
359
- StoreAndForwardProtos .StoreAndForward .parseFrom(payload).toString()
360
- else -> payload.joinToString(" " ) { " %02x" .format(it) }
361
- }
362
- } catch (e: InvalidProtocolBufferException ) {
363
- " Failed to decode payload: ${e.message} "
364
- }
365
- }
366
- return result
367
- }
368
298
}
0 commit comments