Skip to content

Commit

Permalink
Calculate reactive speed relative to max of type
Browse files Browse the repository at this point in the history
  • Loading branch information
kilpkonn committed Aug 3, 2020
1 parent 830a0b8 commit 1aa46e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.preference:preference-ktx:1.1.1"
implementation "com.google.android.material:material:1.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import ee.taltech.iti0213.sportsapp.db.repository.TrackLocationsRepository
import ee.taltech.iti0213.sportsapp.db.repository.TrackSummaryRepository
import ee.taltech.iti0213.sportsapp.db.repository.UserRepository
import ee.taltech.iti0213.sportsapp.provider.FakeLocationProvider
import ee.taltech.iti0213.sportsapp.track.TrackType
import ee.taltech.iti0213.sportsapp.track.converters.Converter
import ee.taltech.iti0213.sportsapp.track.pracelable.TrackData
import ee.taltech.iti0213.sportsapp.track.pracelable.TrackSyncData
Expand Down Expand Up @@ -131,6 +132,7 @@ class MapsActivity : AppCompatActivity(), SensorEventListener, OnMapReadyCallbac
private var maxSpeed = 0.0
private var rabbits = hashMapOf<String, Long>()
private var rabbitTracks = hashMapOf<Long, TrackSummary>()
private var trackTypeMaxSpeeds = hashMapOf<TrackType, Double?>()

private var currentDegree = 0.0f
private var lastAccelerometer = FloatArray(3)
Expand Down Expand Up @@ -245,6 +247,10 @@ class MapsActivity : AppCompatActivity(), SensorEventListener, OnMapReadyCallbac
flingDetector.onFlingLeft = Runnable { onFlingLeft() }
flingDetector.onFlingRight = Runnable { onFlingRight() }

TrackType.values().forEach { type ->
trackTypeMaxSpeeds[type] = trackSummaryRepository.readMaxSpeed(type)
}

startLocationService()
}
// ================================================ MAPS CALLBACKS ===============================================
Expand Down Expand Up @@ -772,10 +778,8 @@ class MapsActivity : AppCompatActivity(), SensorEventListener, OnMapReadyCallbac

rabbitTracks[trackId] = trackSummaryRepository.readTrackSummary(trackId)!!


rabbits = HashMap(rabbits.filter { r -> r.value != trackId })


if (rabbitName != ReplaySpinnerItems.NONE) {
rabbits[rabbitName] = trackId
}
Expand Down Expand Up @@ -971,10 +975,11 @@ class MapsActivity : AppCompatActivity(), SensorEventListener, OnMapReadyCallbac
val location = LatLng(p.latitude, p.longitude)
if (lastLoc != null) {
val relSpeed = min(
1.0, TrackLocation.calcDistanceBetween(lastRabbitLoc ?: p, p) /
1.0,
TrackLocation.calcDistanceBetween(lastRabbitLoc ?: p, p) /
((p.elapsedTimestamp - (lastRabbitLoc?.elapsedTimestamp
?: p.elapsedTimestamp) + 1) / 1_000_000_000.0 / 3.6) /
(rabbitTracks[rabbit.value]?.maxSpeed ?: 1.0)
(trackTypeMaxSpeeds[TrackType.fromInt(rabbitTracks[rabbit.value]!!.type)] ?: 1.0)
)

Log.d(TAG, "Relspeed $relSpeed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import ee.taltech.iti0213.sportsapp.db.DatabaseHelper
import ee.taltech.iti0213.sportsapp.db.domain.TrackSummary
import ee.taltech.iti0213.sportsapp.track.Track
import ee.taltech.iti0213.sportsapp.track.TrackType

class TrackSummaryRepository private constructor(context: Context) : IRepository {

Expand Down Expand Up @@ -53,11 +54,26 @@ class TrackSummaryRepository private constructor(context: Context) : IRepository
databaseHelper.writableDatabase.endTransaction()
}

fun readMaxSpeed(trackType: TrackType): Double? {
val selectQuery = ("SELECT MAX(" + DatabaseHelper.KEY_TRACK_MAX_SPEED
+ ") FROM " + DatabaseHelper.TABLE_TRACKS
+ " WHERE " + DatabaseHelper.KEY_TRACK_TYPE + " = " + trackType.value)
val cursor = databaseHelper.readableDatabase.rawQuery(selectQuery, null)
var track: Double? = null

if (cursor != null) {
if (cursor.moveToFirst()) {
track = cursor.getDouble(0)
}
cursor.close()
}
return track
}

fun readTrackSummary(id: Long): TrackSummary? {

val selectQuery = ("SELECT * FROM " + DatabaseHelper.TABLE_TRACKS
+ " WHERE " + DatabaseHelper.KEY_ID + " = " + id.toString()
+ " LIMIT 1 ")
+ " WHERE " + DatabaseHelper.KEY_ID + " = " + id + " LIMIT 1 ")

val cursor = databaseHelper.readableDatabase.rawQuery(selectQuery, null)
var track: TrackSummary? = null
Expand Down Expand Up @@ -89,7 +105,7 @@ class TrackSummaryRepository private constructor(context: Context) : IRepository
val trackList = mutableListOf<TrackSummary>()

val selectQuery = ("SELECT * FROM " + DatabaseHelper.TABLE_TRACKS
+ " WHERE " + DatabaseHelper.KEY_ID + " BETWEEN " + startId.toString() + " AND " + endId.toString()
+ " WHERE " + DatabaseHelper.KEY_ID + " BETWEEN " + startId + " AND " + endId
+ " ORDER BY " + DatabaseHelper.KEY_ID + " DESC")

val cursor = databaseHelper.readableDatabase.rawQuery(selectQuery, null)
Expand Down

0 comments on commit 1aa46e3

Please sign in to comment.