@@ -16,7 +16,7 @@ class RideDbHelper(context: Context) :
1616
1717 companion object {
1818 // Incrementado para v11 para adicionar as novas colunas
19- const val DATABASE_VERSION = 19
19+ const val DATABASE_VERSION = 21
2020 const val DATABASE_NAME = " BikeRides.db"
2121 private const val TAG = " RideDbHelper"
2222
@@ -182,11 +182,34 @@ class RideDbHelper(context: Context) :
182182 if (rideId <= 0 ) return false
183183 val db = this .writableDatabase
184184
185+ // --- NOVO: BLOQUEIO DE DUPLICATAS ---
186+ // Verifica se já existe um registro com este mesmo ride_id e horário
187+ val packetTime = infoMap[" time" ] as ? String
188+
189+ if (packetTime != null ) {
190+ val cursor = db.query(
191+ TelemetryEntry .TABLE_NAME ,
192+ arrayOf(TelemetryEntry .COLUMN_TELEMETRY_ID ), // Só precisamos saber se tem ID
193+ " ${TelemetryEntry .COLUMN_RIDE_ID } = ? AND ${TelemetryEntry .COLUMN_PACKET_TIME } = ?" ,
194+ arrayOf(rideId.toString(), packetTime),
195+ null , null , null
196+ )
197+
198+ val exists = cursor.count > 0
199+ cursor.close()
200+
201+ if (exists) {
202+ Log .d(TAG , " Pacote duplicado ignorado para o tempo: $packetTime " )
203+ return false
204+ }
205+ }
206+ // ------------------------------------
207+
185208 val values = ContentValues ().apply {
186209 put(TelemetryEntry .COLUMN_RIDE_ID , rideId)
187210
188211 put(TelemetryEntry .COLUMN_PACKET_DATE , infoMap[" date" ] as ? String )
189- put(TelemetryEntry .COLUMN_PACKET_TIME , infoMap[ " time " ] as ? String )
212+ put(TelemetryEntry .COLUMN_PACKET_TIME , packetTime )
190213
191214 put(TelemetryEntry .COLUMN_GPS_TIMESTAMP , gpsMap[" timestamp" ] as ? String )
192215 put(TelemetryEntry .COLUMN_LATITUDE , (gpsMap[" latitude" ] as ? Number )?.toDouble())
@@ -198,7 +221,7 @@ class RideDbHelper(context: Context) :
198221 put(TelemetryEntry .COLUMN_FIX_QUALITY , gpsMap[" fix_quality" ] as ? Int )
199222
200223 crankMap?.let {
201- // AQUI ESTAVA O BUG: Usamos (Number) ?.toDouble() para aceitar Zeros
224+ // Mantendo a correção de segurança para números (Number?.toDouble)
202225 put(TelemetryEntry .COLUMN_POWER , (it[" power" ] as ? Number )?.toDouble())
203226 put(TelemetryEntry .COLUMN_CADENCE , (it[" cadence" ] as ? Number )?.toDouble())
204227 put(TelemetryEntry .COLUMN_JOULES , (it[" joules" ] as ? Number )?.toDouble())
0 commit comments