Integrar GPS cada cinco minutos
This commit is contained in:
parent
f9923c8981
commit
a12ae5da8f
3 changed files with 77 additions and 1 deletions
|
|
@ -90,6 +90,55 @@ class BackendIntegrationManager @Inject constructor(
|
||||||
enqueuePublish(topic, envelope)
|
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) {
|
fun publishTelemetry(payload: JSONObject) {
|
||||||
val envelope = JSONObject()
|
val envelope = JSONObject()
|
||||||
.put("deviceId", BackendConfig.DEVICE_SERIAL)
|
.put("deviceId", BackendConfig.DEVICE_SERIAL)
|
||||||
|
|
|
||||||
|
|
@ -410,6 +410,30 @@ Result.success(file)
|
||||||
}.getOrDefault(emptyList())
|
}.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 {
|
fun logTelemetry(): File {
|
||||||
val t = telemetry()
|
val t = telemetry()
|
||||||
val p = JSONObject().put("batteryPercent", t.batteryPercent).put("charging", t.charging)
|
val p = JSONObject().put("batteryPercent", t.batteryPercent).put("charging", t.charging)
|
||||||
|
|
|
||||||
|
|
@ -143,7 +143,10 @@ private fun vibrateRecordingStopped() {
|
||||||
var ticks = 0
|
var ticks = 0
|
||||||
while (true) {
|
while (true) {
|
||||||
refreshDeviceStatus()
|
refreshDeviceStatus()
|
||||||
if (ticks > 0 && ticks % 60 == 0) deviceController.logTelemetry()
|
if (ticks > 0 && ticks % 60 == 0) {
|
||||||
|
deviceController.logTelemetry()
|
||||||
|
deviceController.logGps()
|
||||||
|
}
|
||||||
ticks++
|
ticks++
|
||||||
delay(5000)
|
delay(5000)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue