Skip to content

Commit

Permalink
Merge pull request #939 from PhenoApps/both-logging
Browse files Browse the repository at this point in the history
Add both logging mode
  • Loading branch information
trife authored Mar 27, 2024
2 parents 819a894 + 2b5c57e commit 3bb693e
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import com.fieldbook.tracker.R
import com.fieldbook.tracker.activities.PreferencesActivity
import com.fieldbook.tracker.utilities.GeoNavHelper
import dagger.hilt.android.AndroidEntryPoint
import org.phenoapps.security.Security
import javax.inject.Inject
Expand Down Expand Up @@ -187,15 +188,18 @@ class GeoNavPreferencesFragment : PreferenceFragmentCompat(),
val geoNavLoggingMode = findPreference<ListPreference>(GeneralKeys.GEONAV_LOGGING_MODE)
val currentMode = preferences.getString(GeneralKeys.GEONAV_LOGGING_MODE, "0")

if (currentMode == "0") {
if (currentMode == GeoNavHelper.GeoNavLoggingMode.OFF.value) {
geoNavLoggingMode?.summary = getString(R.string.pref_geonav_log_off_description)
geoNavLoggingMode?.setIcon(R.drawable.ic_note_off_outline)
} else if (currentMode == "1") {
} else if (currentMode == GeoNavHelper.GeoNavLoggingMode.LIMITED.value) {
geoNavLoggingMode?.summary = getString(R.string.pref_geonav_log_limited_description)
geoNavLoggingMode?.setIcon(R.drawable.ic_note_outline)
} else {
} else if (currentMode == GeoNavHelper.GeoNavLoggingMode.FULL.value) {
geoNavLoggingMode?.summary = getString(R.string.pref_geonav_log_full_description)
geoNavLoggingMode?.setIcon(R.drawable.ic_note_multiple_outline)
} else {
geoNavLoggingMode?.summary = getString(R.string.pref_geonav_log_both_description)
geoNavLoggingMode?.setIcon(R.drawable.ic_note_multiple_outline)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,26 @@ class FieldAudioHelper @Inject constructor(@ActivityContext private val context:
)
}

val geoNavLogWriter = context.getGeoNavHelper().getGeoNavLogWriterUri()
val geoNavFile = geoNavLogWriter?.let {
var geoNavLogWriter = context.getGeoNavHelper().getGeoNavLogLimitedUri()
val limitedGeoNavFile = geoNavLogWriter?.let {
DocumentFile.fromSingleUri(context,
it
)
}

geoNavLogWriter = context.getGeoNavHelper().getGeoNavLogFullUri()
val fullGeoNavFile = geoNavLogWriter?.let {
DocumentFile.fromSingleUri(context,
it
)
}

val paths = ArrayList<DocumentFile?>()
paths.add(audioDocumentFile)
paths.add(geoNavFile)
if(limitedGeoNavFile != null)
paths.add(limitedGeoNavFile)
if(fullGeoNavFile != null)
paths.add(fullGeoNavFile)
paths.add(traitsDocumentFile)

val mGeneratedName = "field_audio_log" + context.cRange.plot_id + "_" + fieldAlias + " " + timeStamp.format(c.time) + ".zip"
Expand Down
148 changes: 105 additions & 43 deletions app/src/main/java/com/fieldbook/tracker/utilities/GeoNavHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.documentfile.provider.DocumentFile
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager
import com.fieldbook.tracker.R
Expand Down Expand Up @@ -118,7 +119,8 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
private var mDeclination: Float? = null
private var mAzimuth: Double? = null
private var mNotWarnedInterference = true
private var mGeoNavLogWriterUri: Uri? = null
private var mGeoNavLogLimitedUri: Uri? = null
private var mGeoNavLogFullUri: Uri? = null

private var currentFixQuality = false

Expand All @@ -131,8 +133,12 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController

}

fun getGeoNavLogWriterUri(): Uri? {
return mGeoNavLogWriterUri
fun getGeoNavLogLimitedUri(): Uri? {
return mGeoNavLogLimitedUri
}

fun getGeoNavLogFullUri(): Uri? {
return mGeoNavLogFullUri
}


Expand Down Expand Up @@ -190,8 +196,8 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
val altLength = alt.length
alt = alt.substring(0, altLength - 1) //drop the "M"

//always log location updates for verbose log
if (currentLoggingMode() == "2") {
//always log location updates for full log
if (currentLoggingMode() == GeoNavLoggingMode.FULL.value) {

val geoNavLine = GeoNavLine(
startLat = lat,
Expand All @@ -202,7 +208,7 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController

writeGeoNavLog(
preferences,
mGeoNavLogWriter,
mLimitedGeoNavLogWriter,
geoNavLine
)
}
Expand Down Expand Up @@ -296,7 +302,8 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
private var lastPlotIdNav: String? = null
private var mGeoNavSnackbar: Snackbar? = null
private val preferences = PreferenceManager.getDefaultSharedPreferences(controller.getContext())
private var mGeoNavLogWriter: OutputStreamWriter? = null
private var mLimitedGeoNavLogWriter: OutputStreamWriter? = null
private var mFullGeoNavLogWriter: OutputStreamWriter? = null

var snackBarBottomMargin: Int = 0

Expand Down Expand Up @@ -528,7 +535,7 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController

//long toc = System.currentTimeMillis();
val (first) = impactZoneSearch(
mGeoNavLogWriter, preferences, currentLoggingMode(),
mLimitedGeoNavLogWriter, mFullGeoNavLogWriter, preferences, currentLoggingMode(),
start, coordinates.toTypedArray(),
mAzimuth, theta, mTeslas, geoNavMethod, d1, d2
)
Expand Down Expand Up @@ -679,9 +686,13 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
mMessageHandlerThread.quit()
//flush and close geo nav log writer
try {
if (mGeoNavLogWriter != null) {
mGeoNavLogWriter?.flush()
mGeoNavLogWriter?.close()
if (mLimitedGeoNavLogWriter != null) {
mLimitedGeoNavLogWriter?.flush()
mLimitedGeoNavLogWriter?.close()
}
if (mFullGeoNavLogWriter != null) {
mFullGeoNavLogWriter?.flush()
mFullGeoNavLogWriter?.close()
}
} catch (io: IOException) {
io.printStackTrace()
Expand Down Expand Up @@ -713,7 +724,7 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
* Starts a file in storage/geonav/log.txt
*/
fun setupGeoNavLogger() {
if (currentLoggingMode() != "0") {
if (currentLoggingMode() != GeoNavLoggingMode.OFF.value) {
try {
val resolver: ContentResolver = controller.getContext().contentResolver
val geoNavFolder = getDirectory(controller.getContext(), R.string.dir_geonav)
Expand All @@ -725,36 +736,80 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
.replace("\\s".toRegex(), "_")
val thetaPref = preferences.getString(GeneralKeys.SEARCH_ANGLE, "22.5")
// if the currentLoggingMode is for limited logging, use "limited_" as the prefix for filename
val prefixOfFile = if (currentLoggingMode() == "1") {
"limited_"
} else {
""
}
val fileName = "${prefixOfFile}log.csv"
var isNew = false
var geoNavLogFile = geoNavFolder.findFile(fileName)
if (geoNavLogFile == null) {
geoNavLogFile = geoNavFolder.createFile("*/csv", fileName)
isNew = true
}
if (geoNavLogFile != null && geoNavLogFile.exists()) {
//open the only log file in 'write append' mode
val outputStream = resolver.openOutputStream(geoNavLogFile.uri, "wa")
Log.d(CollectActivity.TAG, "GeoNav Logger started successfully.")
mGeoNavLogWriterUri = geoNavLogFile.uri
mGeoNavLogWriter = OutputStreamWriter(outputStream)

if (isNew) {
writeGeoNavLog(
preferences,
mGeoNavLogWriter,
GeoNavLine.HeaderLine,
isHeader = true
)
// val prefixOfFile = if (currentLoggingMode() == "1") {
// "limited_"
// } else {
// ""
// }

// file names for logging modes
val limitedModeFileName = "limited_log.csv"
val fullModeFileName = "log.csv"

// find log files for both logging modes
var limitedGeoNavLogFile: DocumentFile? = null
var fullGeoNavLogFile: DocumentFile? = null

if (currentLoggingMode() == GeoNavLoggingMode.LIMITED.value || currentLoggingMode() == GeoNavLoggingMode.BOTH.value)
limitedGeoNavLogFile = geoNavFolder.findFile(limitedModeFileName)

if (currentLoggingMode() == GeoNavLoggingMode.FULL.value || currentLoggingMode() == GeoNavLoggingMode.BOTH.value)
fullGeoNavLogFile = geoNavFolder.findFile(fullModeFileName)

// flags if new file(s) have to be created
var isLimitedLogNew = false
var isFullLogNew = false

// handle limited mode
if (currentLoggingMode() == GeoNavLoggingMode.LIMITED.value || currentLoggingMode() == GeoNavLoggingMode.BOTH.value){
if (limitedGeoNavLogFile == null) {
limitedGeoNavLogFile = geoNavFolder.createFile("*/csv", limitedModeFileName)
isLimitedLogNew = true
}
if (limitedGeoNavLogFile != null && limitedGeoNavLogFile.exists()) {
//open the only log file in 'write append' mode
val outputStream = resolver.openOutputStream(limitedGeoNavLogFile.uri, "wa")
Log.d(CollectActivity.TAG, "GeoNav Logger started successfully.")
mGeoNavLogLimitedUri = limitedGeoNavLogFile.uri
mLimitedGeoNavLogWriter = OutputStreamWriter(outputStream)

if (isLimitedLogNew) {
writeGeoNavLog(
preferences,
mLimitedGeoNavLogWriter,
GeoNavLine.HeaderLine,
isHeader = true
)
}
} else {
Log.d(CollectActivity.TAG, "GeoNav Logger start failed.")
}
}

} else {
Log.d(CollectActivity.TAG, "GeoNav Logger start failed.")
// handle full mode
if (currentLoggingMode() == GeoNavLoggingMode.FULL.value || currentLoggingMode() == GeoNavLoggingMode.BOTH.value){
if (fullGeoNavLogFile == null) {
fullGeoNavLogFile = geoNavFolder.createFile("*/csv", fullModeFileName)
isFullLogNew = true
}
if (fullGeoNavLogFile != null && fullGeoNavLogFile.exists()) {
//open the only log file in 'write append' mode
val outputStream = resolver.openOutputStream(fullGeoNavLogFile.uri, "wa")
Log.d(CollectActivity.TAG, "GeoNav Logger started successfully.")
mGeoNavLogFullUri = fullGeoNavLogFile.uri
mFullGeoNavLogWriter = OutputStreamWriter(outputStream)

if (isFullLogNew) {
writeGeoNavLog(
preferences,
mFullGeoNavLogWriter,
GeoNavLine.HeaderLine,
isHeader = true
)
}
} else {
Log.d(CollectActivity.TAG, "GeoNav Logger start failed.")
}
}
}
} catch (io: IOException) {
Expand Down Expand Up @@ -872,8 +927,8 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController

mInternalLocation = location

//always log location updates for verbose log
if (currentLoggingMode() == "2") {
//always log location updates for full log
if (currentLoggingMode() == GeoNavLoggingMode.FULL.value) {

val geoNavLine = GeoNavLine(
startLat = location.latitude.toString(),
Expand All @@ -883,7 +938,7 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController

writeGeoNavLog(
preferences,
mGeoNavLogWriter,
mLimitedGeoNavLogWriter,
geoNavLine
)
}
Expand All @@ -895,4 +950,11 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
fun getAverageHandler(): Handler? {
return averageHandler
}

enum class GeoNavLoggingMode (val value: String) {
OFF("0"),
LIMITED("1"),
FULL("2"),
BOTH("3")
}
}
16 changes: 11 additions & 5 deletions app/src/main/java/com/fieldbook/tracker/utilities/GeodeticUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.fieldbook.tracker.utilities

import android.content.SharedPreferences
import android.location.Location
import com.fieldbook.tracker.R
import com.fieldbook.tracker.database.models.ObservationUnitModel
import com.fieldbook.tracker.preferences.GeneralKeys
import com.google.gson.Gson
Expand Down Expand Up @@ -148,7 +149,8 @@ class GeodeticUtils {
* @return a object representing the returned location and it's distance
**/
fun impactZoneSearch(
log: OutputStreamWriter?,
limitedLog: OutputStreamWriter?,
fullLog: OutputStreamWriter?,
preferences: SharedPreferences,
currentLoggingMode: String,
start: Location,
Expand Down Expand Up @@ -223,18 +225,22 @@ class GeodeticUtils {
//after a full run of IZ, update the last CLOSEST_UPDATE to CLOSEST_FINAL
izLogArray.findLast { it.closest == CLOSEST_UPDATE.toString() }?.closest = CLOSEST_FINAL.toString()

if (currentLoggingMode == "1") {
// limited mode
if (currentLoggingMode == GeoNavHelper.GeoNavLoggingMode.LIMITED.value || currentLoggingMode == GeoNavHelper.GeoNavLoggingMode.BOTH.value) {
//print only the closest plant to the log
izLogArray.forEach {
if (it.closest == CLOSEST_FINAL.toString()) writeGeoNavLog(
preferences,
log,
limitedLog,
it
)
}
} else if (currentLoggingMode == "2") {
}

// full mode
if (currentLoggingMode == GeoNavHelper.GeoNavLoggingMode.FULL.value || currentLoggingMode == GeoNavHelper.GeoNavLoggingMode.BOTH.value) {
//print the entire array to log
izLogArray.forEach { writeGeoNavLog(preferences, log, it) }
izLogArray.forEach { writeGeoNavLog(preferences, fullLog, it) }
}

return closestPoint to closestDistance
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@
<item>@string/pref_geonav_log_off</item>
<item>@string/pref_geonav_log_limited</item>
<item>@string/pref_geonav_log_full</item>
<item>@string/pref_geonav_log_both</item>
</string-array>

<string-array name="pref_geonav_logging_mode_values">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>

<string-array name="pref_general_move_to_unique_id_entries">
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@
<string name="pref_geonav_log_limited_description">Limited: Logging closest entry to storage/geonav/limited_log.txt</string>
<string name="pref_geonav_log_full">Full</string>
<string name="pref_geonav_log_full_description">Full: Logging all location updates and algorithm steps to storage/geonav/log.txt</string>
<string name="pref_geonav_log_both">Both</string>
<string name="pref_geonav_log_both_description">Both: Logging all location updates and algorithm steps to storage/geonav/log.txt, closest entry to storage/geonav/limited_log.txt</string>

<string name="pref_geonav_update_interval_title">Update Interval (s)</string>
<string name="pref_geonav_update_interval_summary">The time between location updates is %ss.</string>
Expand Down

0 comments on commit 3bb693e

Please sign in to comment.