diff --git a/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/backend/BackendIntegrationManager.kt b/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/backend/BackendIntegrationManager.kt index ab65547..0f678e2 100644 --- a/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/backend/BackendIntegrationManager.kt +++ b/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/backend/BackendIntegrationManager.kt @@ -90,6 +90,55 @@ class BackendIntegrationManager @Inject constructor( enqueuePublish(topic, envelope) } + fun publishGps(payload: JSONObject) { + httpExecutor.execute { + var conn: HttpURLConnection? = null + try { + val token = runBlocking { + authSessionStore.token.first() + }?.takeIf { it.isNotBlank() } + ?: error("No hay sesion JWT activa") + + val url = URL( + BackendConfig.API_BASE_URL.trimEnd('/') + + "/mobile/v1/gps_locations" + ) + + conn = (url.openConnection() as HttpURLConnection).apply { + requestMethod = "POST" + connectTimeout = 15_000 + readTimeout = 30_000 + doOutput = true + setRequestProperty("Content-Type", "application/json") + setRequestProperty("Accept", "application/json") + setRequestProperty("Authorization", "Bearer $token") + setRequestProperty( + "X-Device-Serial", + BackendConfig.DEVICE_SERIAL + ) + } + + conn.outputStream.use { + it.write(payload.toString().toByteArray(Charsets.UTF_8)) + } + + val status = conn.responseCode + if (status in 200..299) { + Log.i(TAG, "GPS HTTPS OK: HTTP $status") + } else { + val errorBody = conn.errorStream + ?.bufferedReader() + ?.use { it.readText() } + .orEmpty() + Log.e(TAG, "GPS HTTPS ERROR: HTTP $status $errorBody") + } + } catch (e: Exception) { + Log.e(TAG, "GPS HTTPS ERROR", e) + } finally { + conn?.disconnect() + } + } + } fun publishTelemetry(payload: JSONObject) { val envelope = JSONObject() .put("deviceId", BackendConfig.DEVICE_SERIAL) diff --git a/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/device/M530DeviceController.kt b/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/device/M530DeviceController.kt index 49ac878..f489efd 100644 --- a/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/device/M530DeviceController.kt +++ b/app/src/main/kotlin/com/bodycamera/twentyfoulabs/data/device/M530DeviceController.kt @@ -410,6 +410,30 @@ Result.success(file) }.getOrDefault(emptyList()) } + fun logGps() { + // COORDENADAS SIMULADAS: exclusivamente para pruebas. + val latitude = -12.077 + val longitude = -77.092 + + val recordedAt = java.text.SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss'Z'", + java.util.Locale.US + ).apply { + timeZone = java.util.TimeZone.getTimeZone("UTC") + }.format(java.util.Date()) + + val payload = JSONObject() + .put("latitude", latitude) + .put("longitude", longitude) + .put("speed", 0) + .put("heading", 0) + .put("accuracy", 10) + .put("recorded_at", recordedAt) + + backend.publishGps(payload) + + Log.i(TAG, "GPS: envio HTTPS solicitado") + } fun logTelemetry(): File { val t = telemetry() val p = JSONObject().put("batteryPercent", t.batteryPercent).put("charging", t.charging) diff --git a/app/src/main/kotlin/com/bodycamera/twentyfoulabs/ui/screens/MainViewModel.kt b/app/src/main/kotlin/com/bodycamera/twentyfoulabs/ui/screens/MainViewModel.kt index 212877a..aac0489 100644 --- a/app/src/main/kotlin/com/bodycamera/twentyfoulabs/ui/screens/MainViewModel.kt +++ b/app/src/main/kotlin/com/bodycamera/twentyfoulabs/ui/screens/MainViewModel.kt @@ -143,7 +143,10 @@ private fun vibrateRecordingStopped() { var ticks = 0 while (true) { refreshDeviceStatus() - if (ticks > 0 && ticks % 60 == 0) deviceController.logTelemetry() + if (ticks > 0 && ticks % 60 == 0) { + deviceController.logTelemetry() + deviceController.logGps() + } ticks++ delay(5000) }