Files
VesiscanClinicalAndroid/app/src/main/java/com/medithings/vesiscan/services/BleDebugLogger.kt
T
dw.jang 1df1a4e6cd fix(logs): §A+B · scan_id 정합성 · events.jsonl 조인키 (LOG_STORAGE_ANALYSIS Priority 1)
배경 (LOG_STORAGE_ANALYSIS 2026-08-04):
  §A · ble.log 의 scan= 값이 실제 adc.csv scan_id 대비 1씩 밀림 (off-by-one)
       - 원인: measurementResult() 가 AdcCsvLogger.lastScanId 를 읽은 후에
         AdcCsvLogger.log() 가 scanCounter 를 증가시켜 실제 CSV 는 N+1 로 기록.
  §B · events.jsonl 에 scan_id 필드 부재 → 4개 로그 (adc.csv · imu.csv · ble.log ·
       events.jsonl) 간 조인 키 없음. timestamp 밀리초만으로는 autoScanIntervalMs=600ms
       에서 신뢰 불가.
  §F · BV 실패 케이스가 events.jsonl 에 아무 흔적 없음.

수정:
  AdcCsvLogger:
    - nextScanId(): Int 신규 · thread-safe counter 증가 후 id 반환
    - log(...): 새 scanId 파라미터 · 호출자가 미리 발급한 id 우선 사용
    - counterLock 으로 동시성 방어

  MeasurementLogService.logMeasurement(...):
    - scanId: Int? 파라미터 추가 · events.jsonl 에 "scan_id" 필드 기록

  BleDebugLogger.measurementResult(...):
    - scanId: Int? 파라미터 · 호출자가 발급한 id 우선 (미제공 시 legacy fallback)

  ImuCsvLogger.log(...):
    - scanId: Int? 파라미터 · 호출자가 발급한 id 우선

  PiezoMonitoringView (5 sites · onMultiChannelComplete):
    - 사이클 진입 시 val scanId = AdcCsvLogger.nextScanId() 미리 발급
    - useV41Bv · useMethodDBv · effectiveCenterWalls>=4 · totalWallCount>0 ·
      no-valid-channels 5경로 모두 scanId 전달
    - §F: BV_SKIP · BV_FAIL 케이스도 events.jsonl 에 기록 (null volume)

  ClinicalLiveView (1 site):
    - autoCapture 시 val sid = AdcCsvLogger.nextScanId() → AdcCsvLogger + ImuCsvLogger
      에 동일 id 전달

효과:
  ble.log · adc.csv · imu.csv · events.jsonl 이 scan_id 로 정합 조인 가능.
  Guardian 앱이 이벤트 재구성 시 timestamp 대신 scan_id 단일 키 사용.
  BV 실패도 audit trail 에 기록.

검증: BUILD SUCCESSFUL 8s
2026-08-04 15:58:01 +09:00

352 lines
13 KiB
Kotlin

package com.medithings.vesiscan.services
import android.content.Context
import android.content.Intent
import androidx.core.content.FileProvider
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
/**
* BLE 디버그 로거 — 펌웨어 디버깅용 상세 통신 로그.
*
* 모든 TX/RX 패킷, RSSI, 연결 상태, 에러를 기록하고
* 파일로 내보내기 가능.
*
* 파일 위치: vesiscan_ble_logs/YYYY-MM-DD_HHmmss.log
*/
class BleDebugLogger private constructor(private val context: Context) {
companion object {
@Volatile
private var instance: BleDebugLogger? = null
fun getInstance(context: Context) = instance ?: synchronized(this) {
instance ?: BleDebugLogger(context.applicationContext).also { instance = it }
}
private const val MAX_MEMORY_ENTRIES = 2000
}
enum class Level { TX, RX, INFO, WARN, ERROR, RSSI, CONN }
data class Entry(
val timestamp: Long,
val level: Level,
val message: String,
val dataSize: Int = 0,
val rssi: Int? = null
) {
private val fmt = SimpleDateFormat("HH:mm:ss.SSS", Locale.US)
val timeStr: String get() = fmt.format(Date(timestamp))
fun toLogLine(): String {
val tag = level.name.padEnd(5)
val extra = when {
dataSize > 0 -> " [${dataSize}B]"
rssi != null -> " [${rssi}dBm]"
else -> ""
}
return "$timeStr $tag $message$extra"
}
fun toJson(): JSONObject = JSONObject().apply {
put("t", timestamp)
put("level", level.name)
put("msg", message)
if (dataSize > 0) put("bytes", dataSize)
if (rssi != null) put("rssi", rssi)
}
}
// In-memory ring buffer
private val entries = CopyOnWriteArrayList<Entry>()
// Session tracking
private var sessionStart: Long = System.currentTimeMillis()
var deviceName: String = ""
private set
private var txCount = 0
private var rxCount = 0
private var errorCount = 0
private var lastRssi: Int = 0
private val rssiHistory = mutableListOf<Pair<Long, Int>>()
private var packetTimeouts = 0
private var channelMissCount = 0
// File
private val logDir = File(context.filesDir, "vesiscan_ble_logs").also { it.mkdirs() }
private val dateFmt = SimpleDateFormat("yyyy-MM-dd_HHmmss", Locale.US)
private val dayFmt = SimpleDateFormat("yyyy-MM-dd", Locale.US)
// 실시간 파일 기록 (화면 잠금/앱 종료에도 보존)
// 임상 모드: 세션 step 폴더의 ble.log로 라우팅 (step 전환 시 새 파일)
// 일반 모드: Downloads/VesiScan_BLE_*.log (BLE 연결 1회당 1파일)
private var liveLogFile: File? = null
private var liveLogLabel: String = "" // 임상 모드 측정 단위 변경 감지
private fun ensureLiveLogFile(): File {
val clinicalDir = ClinicalSessionStore.currentLogDir()
if (clinicalDir != null) {
clinicalDir.mkdirs()
val currentLabel = ClinicalSessionStore.currentLabel()
// 측정 단위가 바뀌었으면 새 파일로 전환
if (liveLogFile == null || liveLogLabel != currentLabel) {
liveLogFile = File(clinicalDir, "ble.log")
liveLogLabel = currentLabel
}
} else if (liveLogFile == null || liveLogLabel.isNotEmpty()) {
// 일반 모드 진입 또는 임상 모드 종료
val downloadsDir = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS)
liveLogFile = File(downloadsDir, "VesiScan_BLE_${dateFmt.format(Date())}.log")
liveLogLabel = ""
}
return liveLogFile!!
}
private fun appendToFile(line: String) {
try {
ensureLiveLogFile().appendText(line + "\n")
} catch (_: Exception) {}
}
// ── Logging Methods ──
fun tx(command: String, dataSize: Int = 0) {
txCount++
add(Entry(System.currentTimeMillis(), Level.TX, command, dataSize))
}
fun rx(prefix: String, dataSize: Int = 0, detail: String = "") {
rxCount++
val msg = if (detail.isNotEmpty()) "$prefix $detail" else prefix
add(Entry(System.currentTimeMillis(), Level.RX, msg, dataSize))
}
fun rssi(value: Int) {
lastRssi = value
rssiHistory.add(System.currentTimeMillis() to value)
if (rssiHistory.size > 500) rssiHistory.removeAt(0)
add(Entry(System.currentTimeMillis(), Level.RSSI, "RSSI=$value dBm", rssi = value))
}
/** mid?/rid: 응답 받은 후 호출 — 펌웨어 정보를 ble.log에 보기 좋게 헤더 형태로 기록 */
fun deviceInfo(fw: String, hw: String, sn: String) {
appendToFile("─── Device Info ───")
if (fw.isNotEmpty()) appendToFile("Firmware: $fw")
if (hw.isNotEmpty()) appendToFile("Hardware: $hw")
if (sn.isNotEmpty()) appendToFile("Serial: $sn")
appendToFile("───────────────────")
}
fun connected(name: String, address: String) {
deviceName = name
sessionStart = System.currentTimeMillis()
txCount = 0; rxCount = 0; errorCount = 0; packetTimeouts = 0; channelMissCount = 0
rssiHistory.clear()
liveLogFile = null // 새 세션 → 새 파일
appendToFile("=== VesiScan BLE Debug Log ===")
appendToFile("Device: $name")
appendToFile("Session: ${dateFmt.format(Date())}")
appendToFile("============================================================")
add(Entry(System.currentTimeMillis(), Level.CONN, "CONNECTED $name ($address)"))
}
fun disconnected(reason: String = "", gattStatus: Int = -1) {
val msg = buildString {
append("DISCONNECTED")
if (reason.isNotEmpty()) append(" reason=$reason")
if (gattStatus >= 0) append(" gatt_status=$gattStatus")
append(" session=${sessionDurationSec()}s tx=$txCount rx=$rxCount err=$errorCount")
}
add(Entry(System.currentTimeMillis(), Level.CONN, msg))
}
fun reconnecting(attempt: Int, maxAttempt: Int, delayMs: Long) {
add(Entry(System.currentTimeMillis(), Level.CONN, "RECONNECTING attempt=$attempt/$maxAttempt delay=${delayMs}ms"))
}
fun reconnectFailed() {
errorCount++
add(Entry(System.currentTimeMillis(), Level.ERROR, "RECONNECT_FAILED after all attempts"))
}
fun info(message: String) {
add(Entry(System.currentTimeMillis(), Level.INFO, message))
}
fun warn(message: String) {
add(Entry(System.currentTimeMillis(), Level.WARN, message))
}
fun error(message: String) {
errorCount++
add(Entry(System.currentTimeMillis(), Level.ERROR, message))
}
fun packetTimeout(channel: Int, retryCount: Int) {
packetTimeouts++
error("PACKET_TIMEOUT ch=$channel retry=$retryCount")
}
fun channelMissing(expectedChannels: Int, receivedChannels: Int) {
channelMissCount++
warn("CHANNEL_MISS expected=$expectedChannels received=$receivedChannels")
}
fun endianDetected(isBigEndian: Boolean, firstSampleLE: Int, firstSampleBE: Int) {
info("ENDIAN_DETECT ${if (isBigEndian) "BIG" else "LITTLE"} (LE=$firstSampleLE, BE=$firstSampleBE)")
}
fun measurementResult(volumeMl: Double?, lrRatio: Double, validChannels: Int, urineLines: List<Int>, otsuThreshold: Double = 0.0, scanId: Int? = null) {
// 2026-08-04: LOG_STORAGE_ANALYSIS §A fix — 호출자가 명시적 scanId 전달 시 사용.
// 미제공 시 legacy fallback (AdcCsvLogger.lastScanId · off-by-one 이었던 원인).
val id = scanId ?: AdcCsvLogger.lastScanId
val msg = buildString {
append("MEASUREMENT scan=$id")
append(" vol=${volumeMl?.let { "%.1f".format(it) } ?: "null"}ml")
append(" lr=${"%.2f".format(lrRatio)}")
append(" valid=$validChannels/6")
append(" urine=[${urineLines.joinToString(",")}]")
if (otsuThreshold > 0) append(" otsu=${"%.0f".format(otsuThreshold)}")
}
add(Entry(System.currentTimeMillis(), Level.INFO, msg))
}
// ── Access ──
private fun add(entry: Entry) {
entries.add(entry)
if (entries.size > MAX_MEMORY_ENTRIES) {
entries.removeAt(0)
}
appendToFile(entry.toLogLine())
}
fun getEntries(): List<Entry> = entries.toList()
fun getRecentEntries(count: Int = 100): List<Entry> {
val all = entries.toList()
return if (all.size > count) all.subList(all.size - count, all.size) else all
}
fun clear() {
entries.clear()
}
// ── Stats ──
private fun sessionDurationSec(): Long = (System.currentTimeMillis() - sessionStart) / 1000
fun getStats(): JSONObject = JSONObject().apply {
put("session_duration_sec", sessionDurationSec())
put("device", deviceName)
put("tx_count", txCount)
put("rx_count", rxCount)
put("error_count", errorCount)
put("packet_timeouts", packetTimeouts)
put("channel_misses", channelMissCount)
put("last_rssi", lastRssi)
if (rssiHistory.isNotEmpty()) {
put("avg_rssi", rssiHistory.map { it.second }.average().toInt())
put("min_rssi", rssiHistory.minOf { it.second })
put("max_rssi", rssiHistory.maxOf { it.second })
}
}
// ── File Export ──
private fun writeLogContent(writer: java.io.BufferedWriter) {
writer.write("=== VesiScan BLE Debug Log ===\n")
writer.write("Device: $deviceName\n")
writer.write("Session: ${dateFmt.format(Date(sessionStart))}\n")
writer.write("Duration: ${sessionDurationSec()}s\n")
writer.write("TX: $txCount RX: $rxCount Errors: $errorCount Timeouts: $packetTimeouts\n")
if (rssiHistory.isNotEmpty()) {
writer.write("RSSI: avg=${rssiHistory.map { it.second }.average().toInt()} ")
writer.write("min=${rssiHistory.minOf { it.second }} max=${rssiHistory.maxOf { it.second }}\n")
}
writer.write("=".repeat(60) + "\n\n")
for (entry in entries) {
writer.write(entry.toLogLine() + "\n")
}
}
fun exportToFile(): File {
val fileName = "ble_debug_${dateFmt.format(Date())}.log"
val file = File(logDir, fileName)
file.bufferedWriter().use { writeLogContent(it) }
return file
}
/** Downloads 폴더에 저장 (파일 탐색기에서 바로 접근 가능) */
fun exportToDownloads(): File? {
return try {
val downloadsDir = android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS
)
val fileName = "VesiScan_BLE_${dateFmt.format(Date())}.log"
val file = File(downloadsDir, fileName)
file.bufferedWriter().use { writeLogContent(it) }
file
} catch (e: Exception) {
null
}
}
fun exportToJsonFile(): File {
val fileName = "ble_debug_${dateFmt.format(Date())}.jsonl"
val file = File(logDir, fileName)
file.bufferedWriter().use { writer ->
val header = JSONObject().apply {
put("type", "header")
put("device", deviceName)
put("session_start", sessionStart)
put("stats", getStats())
}
writer.write(header.toString() + "\n")
for (entry in entries) {
writer.write(entry.toJson().toString() + "\n")
}
}
return file
}
fun getShareIntent(): Intent {
val file = exportToFile()
val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
return Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_STREAM, uri)
putExtra(Intent.EXTRA_SUBJECT, "VesiScan BLE Debug Log — $deviceName")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
}
fun getLogText(): String {
val sb = StringBuilder()
sb.appendLine("=== VesiScan BLE Debug ===")
sb.appendLine("Device: $deviceName | TX:$txCount RX:$rxCount Err:$errorCount Timeout:$packetTimeouts")
if (rssiHistory.isNotEmpty()) {
sb.appendLine("RSSI: avg=${rssiHistory.map { it.second }.average().toInt()} min=${rssiHistory.minOf { it.second }}")
}
sb.appendLine("---")
for (entry in entries.takeLast(200)) {
sb.appendLine(entry.toLogLine())
}
return sb.toString()
}
/** 7일 이상 된 로그 삭제 */
fun purgeOldLogs(keepDays: Int = 7) {
val cutoff = System.currentTimeMillis() - keepDays * 24 * 60 * 60 * 1000L
logDir.listFiles()?.filter { it.lastModified() < cutoff }?.forEach { it.delete() }
}
}