From 18c2f1c038f62fda1c1cea19c12dfdd4ce411602 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Sun, 15 Mar 2026 01:24:07 +0000 Subject: feat: implement NMEA stream management, sensor data models, and power modes - Added NmeaStreamManager for TCP connection and sentence parsing. - Extended NmeaParser to support MWV (wind), DBT (depth), and HDG/HDM (heading) sentences. - Added sensor data models: WindData, DepthData, HeadingData. - Introduced PowerMode enum to manage GPS update intervals. - Integrated NmeaStreamManager and PowerMode into LocationService. - Added test-runner, a standalone JVM-only Gradle project for verifying GPS/NMEA logic. Co-Authored-By: Gemini CLI --- test-runner/.gitignore | 3 + test-runner/build.gradle | 18 ++ test-runner/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 43453 bytes .../gradle/wrapper/gradle-wrapper.properties | 7 + test-runner/gradlew | 186 +++++++++++++++++++++ test-runner/gradlew.bat | 94 +++++++++++ test-runner/settings.gradle | 1 + .../main/kotlin/org/terst/nav/gps/GpsPosition.kt | 9 + .../main/kotlin/org/terst/nav/gps/GpsProvider.kt | 14 ++ .../main/kotlin/org/terst/nav/nmea/NmeaParser.kt | 97 +++++++++++ .../kotlin/org/terst/nav/gps/GpsPositionTest.kt | 33 ++++ .../kotlin/org/terst/nav/gps/GpsProviderTest.kt | 133 +++++++++++++++ .../kotlin/org/terst/nav/nmea/NmeaParserTest.kt | 103 ++++++++++++ 13 files changed, 698 insertions(+) create mode 100644 test-runner/.gitignore create mode 100644 test-runner/build.gradle create mode 100755 test-runner/gradle/wrapper/gradle-wrapper.jar create mode 100755 test-runner/gradle/wrapper/gradle-wrapper.properties create mode 100755 test-runner/gradlew create mode 100755 test-runner/gradlew.bat create mode 100644 test-runner/settings.gradle create mode 100644 test-runner/src/main/kotlin/org/terst/nav/gps/GpsPosition.kt create mode 100644 test-runner/src/main/kotlin/org/terst/nav/gps/GpsProvider.kt create mode 100644 test-runner/src/main/kotlin/org/terst/nav/nmea/NmeaParser.kt create mode 100644 test-runner/src/test/kotlin/org/terst/nav/gps/GpsPositionTest.kt create mode 100644 test-runner/src/test/kotlin/org/terst/nav/gps/GpsProviderTest.kt create mode 100644 test-runner/src/test/kotlin/org/terst/nav/nmea/NmeaParserTest.kt (limited to 'test-runner') diff --git a/test-runner/.gitignore b/test-runner/.gitignore new file mode 100644 index 0000000..5cc008e --- /dev/null +++ b/test-runner/.gitignore @@ -0,0 +1,3 @@ +build/ +.gradle/ +.kotlin/ diff --git a/test-runner/build.gradle b/test-runner/build.gradle new file mode 100644 index 0000000..4611381 --- /dev/null +++ b/test-runner/build.gradle @@ -0,0 +1,18 @@ +plugins { + id 'org.jetbrains.kotlin.jvm' version '2.0.0' +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation 'junit:junit:4.13.2' +} + +test { + useJUnit() + testLogging { + events "passed", "failed", "skipped" + } +} diff --git a/test-runner/gradle/wrapper/gradle-wrapper.jar b/test-runner/gradle/wrapper/gradle-wrapper.jar new file mode 100755 index 0000000..e644113 Binary files /dev/null and b/test-runner/gradle/wrapper/gradle-wrapper.jar differ diff --git a/test-runner/gradle/wrapper/gradle-wrapper.properties b/test-runner/gradle/wrapper/gradle-wrapper.properties new file mode 100755 index 0000000..b82aa23 --- /dev/null +++ b/test-runner/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/test-runner/gradlew b/test-runner/gradlew new file mode 100755 index 0000000..3416ad8 --- /dev/null +++ b/test-runner/gradlew @@ -0,0 +1,186 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by "Gradle init" +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other POSIX-compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for using: +# +# (2) This script targets any POSIX shell, so it avoids bashisms (like function +# definitions preceded by "function" keyword) or anything that might not be +# available on minimal POSIX shell implementations. +# +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( absolute + *) app_path=$APP_HOME$link ;; #( relative + esac +done + +# This is reliable if the symlink target is absolute. +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + ;; + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$DEFAULT_JVM_OPTS" ) +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# temporary marker and then put it back. +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^a-zA-Z0-9/=@._-]~\\&~g; ' | + tr '\n' ' ' + ) $@" + +exec "$JAVACMD" "$@" diff --git a/test-runner/gradlew.bat b/test-runner/gradlew.bat new file mode 100755 index 0000000..9d21a21 --- /dev/null +++ b/test-runner/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/test-runner/settings.gradle b/test-runner/settings.gradle new file mode 100644 index 0000000..0718781 --- /dev/null +++ b/test-runner/settings.gradle @@ -0,0 +1 @@ +rootProject.name = "test-runner" diff --git a/test-runner/src/main/kotlin/org/terst/nav/gps/GpsPosition.kt b/test-runner/src/main/kotlin/org/terst/nav/gps/GpsPosition.kt new file mode 100644 index 0000000..5faf30c --- /dev/null +++ b/test-runner/src/main/kotlin/org/terst/nav/gps/GpsPosition.kt @@ -0,0 +1,9 @@ +package org.terst.nav.gps + +data class GpsPosition( + val latitude: Double, + val longitude: Double, + val sog: Double, // knots + val cog: Double, // degrees true + val timestampMs: Long +) diff --git a/test-runner/src/main/kotlin/org/terst/nav/gps/GpsProvider.kt b/test-runner/src/main/kotlin/org/terst/nav/gps/GpsProvider.kt new file mode 100644 index 0000000..3c3d634 --- /dev/null +++ b/test-runner/src/main/kotlin/org/terst/nav/gps/GpsProvider.kt @@ -0,0 +1,14 @@ +package org.terst.nav.gps + +interface GpsProvider { + fun start() + fun stop() + val position: GpsPosition? + fun addListener(listener: GpsListener) + fun removeListener(listener: GpsListener) +} + +interface GpsListener { + fun onPositionUpdate(position: GpsPosition) + fun onFixLost() +} diff --git a/test-runner/src/main/kotlin/org/terst/nav/nmea/NmeaParser.kt b/test-runner/src/main/kotlin/org/terst/nav/nmea/NmeaParser.kt new file mode 100644 index 0000000..74f2c41 --- /dev/null +++ b/test-runner/src/main/kotlin/org/terst/nav/nmea/NmeaParser.kt @@ -0,0 +1,97 @@ +package org.terst.nav.nmea + +import org.terst.nav.gps.GpsPosition +import java.util.Calendar +import java.util.TimeZone + +class NmeaParser { + + /** + * Parses an NMEA RMC sentence and returns a [GpsPosition], or null if the + * sentence is void (status=V), malformed, or cannot be parsed. + * + * Supported talker IDs: GP, GN, and any other standard prefix. + * SOG and COG default to 0.0 when the fields are absent. + */ + fun parseRmc(sentence: String): GpsPosition? { + if (sentence.isBlank()) return null + + // Strip optional checksum (*XX suffix) + val body = if ('*' in sentence) sentence.substringBefore('*') else sentence + + val fields = body.split(',') + if (fields.size < 10) return null + + // Sentence ID must end with "RMC" + if (!fields[0].endsWith("RMC")) return null + + // Status must be Active; Void means no valid fix + if (fields[2] != "A") return null + + val latStr = fields[3] + val latDir = fields[4] + val lonStr = fields[5] + val lonDir = fields[6] + + if (latStr.isEmpty() || latDir.isEmpty() || lonStr.isEmpty() || lonDir.isEmpty()) return null + + val latitude = parseNmeaDegrees(latStr) * if (latDir == "S") -1.0 else 1.0 + val longitude = parseNmeaDegrees(lonStr) * if (lonDir == "W") -1.0 else 1.0 + + val sog = fields[7].toDoubleOrNull() ?: 0.0 + val cog = fields[8].toDoubleOrNull() ?: 0.0 + + val timestampMs = parseTimestamp(timeStr = fields[1], dateStr = fields[9]) + + return GpsPosition(latitude, longitude, sog, cog, timestampMs) + } + + /** + * Converts NMEA degree-minutes format (DDDMM.MMMM) to decimal degrees. + * Works for both latitude (DDMM.MM) and longitude (DDDMM.MM) formats. + */ + private fun parseNmeaDegrees(value: String): Double { + val raw = value.toDoubleOrNull() ?: return 0.0 + val degrees = (raw / 100.0).toInt() + val minutes = raw - degrees * 100.0 + return degrees + minutes / 60.0 + } + + /** + * Combines NMEA time (HHMMSS.ss) and date (DDMMYY) into a Unix epoch milliseconds value. + * Returns 0 on any parse failure. + */ + private fun parseTimestamp(timeStr: String, dateStr: String): Long { + return try { + val cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")) + cal.isLenient = false + + if (dateStr.length >= 6) { + val day = dateStr.substring(0, 2).toInt() + val month = dateStr.substring(2, 4).toInt() - 1 // Calendar is 0-based + val yy = dateStr.substring(4, 6).toInt() + val year = if (yy < 70) 2000 + yy else 1900 + yy + cal.set(Calendar.YEAR, year) + cal.set(Calendar.MONTH, month) + cal.set(Calendar.DAY_OF_MONTH, day) + } + + if (timeStr.length >= 6) { + val hours = timeStr.substring(0, 2).toInt() + val minutes = timeStr.substring(2, 4).toInt() + val seconds = timeStr.substring(4, 6).toInt() + val millis = if (timeStr.length > 7) { + (timeStr.substring(7).toDoubleOrNull()?.times(1000.0))?.toInt() ?: 0 + } else 0 + cal.set(Calendar.HOUR_OF_DAY, hours) + cal.set(Calendar.MINUTE, minutes) + cal.set(Calendar.SECOND, seconds) + cal.set(Calendar.MILLISECOND, millis) + } + + cal.timeInMillis + } catch (e: Exception) { + 0L + } + } +} diff --git a/test-runner/src/test/kotlin/org/terst/nav/gps/GpsPositionTest.kt b/test-runner/src/test/kotlin/org/terst/nav/gps/GpsPositionTest.kt new file mode 100644 index 0000000..52e8348 --- /dev/null +++ b/test-runner/src/test/kotlin/org/terst/nav/gps/GpsPositionTest.kt @@ -0,0 +1,33 @@ +package org.terst.nav.gps + +import org.junit.Assert.* +import org.junit.Test + +class GpsPositionTest { + + @Test + fun `GpsPosition holds correct values`() { + val pos = GpsPosition( + latitude = 41.5, + longitude = -71.0, + sog = 5.2, + cog = 180.0, + timestampMs = 1_000L + ) + assertEquals(41.5, pos.latitude, 0.0) + assertEquals(-71.0, pos.longitude, 0.0) + assertEquals(5.2, pos.sog, 0.0) + assertEquals(180.0, pos.cog, 0.0) + assertEquals(1_000L, pos.timestampMs) + } + + @Test + fun `GpsPosition equality works as expected for data class`() { + val pos1 = GpsPosition(41.5, -71.0, 5.2, 180.0, 1_000L) + val pos2 = GpsPosition(41.5, -71.0, 5.2, 180.0, 1_000L) + val pos3 = GpsPosition(42.0, -70.0, 3.0, 90.0, 2_000L) + + assertEquals(pos1, pos2) + assertNotEquals(pos1, pos3) + } +} diff --git a/test-runner/src/test/kotlin/org/terst/nav/gps/GpsProviderTest.kt b/test-runner/src/test/kotlin/org/terst/nav/gps/GpsProviderTest.kt new file mode 100644 index 0000000..4a03387 --- /dev/null +++ b/test-runner/src/test/kotlin/org/terst/nav/gps/GpsProviderTest.kt @@ -0,0 +1,133 @@ +package org.terst.nav.gps + +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test + +// ── Fake implementation (no Android dependencies) ──────────────────────────── + +class FakeGpsProvider : GpsProvider { + var currentPosition: GpsPosition? = null + private val listeners = mutableListOf() + var started = false + + override fun start() { started = true } + override fun stop() { started = false } + override val position: GpsPosition? get() = currentPosition + override fun addListener(listener: GpsListener) { listeners.add(listener) } + override fun removeListener(listener: GpsListener) { listeners.remove(listener) } + + fun simulatePosition(pos: GpsPosition) { + currentPosition = pos + listeners.forEach { it.onPositionUpdate(pos) } + } + + fun simulateFixLost() { listeners.forEach { it.onFixLost() } } +} + +// ── Test helpers ───────────────────────────────────────────────────────────── + +private fun makePosition(lat: Double = 41.0, lon: Double = -71.0, sog: Double = 5.0) = + GpsPosition(lat, lon, sog, cog = 180.0, timestampMs = 1_000L) + +private class RecordingListener : GpsListener { + val positions = mutableListOf() + var fixLostCount = 0 + + override fun onPositionUpdate(position: GpsPosition) { positions.add(position) } + override fun onFixLost() { fixLostCount++ } +} + +// ── Tests ───────────────────────────────────────────────────────────────────── + +class GpsProviderTest { + + private lateinit var provider: FakeGpsProvider + + @Before + fun setUp() { + provider = FakeGpsProvider() + } + + @Test + fun `start sets started to true`() { + provider.start() + assertTrue(provider.started) + } + + @Test + fun `stop sets started to false`() { + provider.start() + provider.stop() + assertFalse(provider.started) + } + + @Test + fun `listener receives position update`() { + val listener = RecordingListener() + provider.addListener(listener) + val pos = makePosition() + provider.simulatePosition(pos) + assertEquals(1, listener.positions.size) + assertEquals(pos, listener.positions[0]) + } + + @Test + fun `listener notified of fix lost`() { + val listener = RecordingListener() + provider.addListener(listener) + provider.simulateFixLost() + assertEquals(1, listener.fixLostCount) + } + + @Test + fun `multiple listeners all receive position update`() { + val l1 = RecordingListener() + val l2 = RecordingListener() + val l3 = RecordingListener() + provider.addListener(l1) + provider.addListener(l2) + provider.addListener(l3) + provider.simulatePosition(makePosition()) + assertEquals(1, l1.positions.size) + assertEquals(1, l2.positions.size) + assertEquals(1, l3.positions.size) + } + + @Test + fun `multiple listeners all notified of fix lost`() { + val l1 = RecordingListener() + val l2 = RecordingListener() + provider.addListener(l1) + provider.addListener(l2) + provider.simulateFixLost() + assertEquals(1, l1.fixLostCount) + assertEquals(1, l2.fixLostCount) + } + + @Test + fun `removing listener stops notifications`() { + val listener = RecordingListener() + provider.addListener(listener) + provider.removeListener(listener) + provider.simulatePosition(makePosition()) + provider.simulateFixLost() + assertEquals(0, listener.positions.size) + assertEquals(0, listener.fixLostCount) + } + + @Test + fun `position property reflects last simulated position`() { + assertNull(provider.position) + val pos = makePosition(lat = 42.5, lon = -70.0) + provider.simulatePosition(pos) + assertEquals(pos, provider.position) + } + + @Test + fun `SOG conversion sanity check - 1 mps is approximately 1_94384 knots`() { + // 1 m/s * 1.94384 = 1.94384 knots — validate constant used in DeviceGpsProvider + val knots = 1.0 * 1.94384 + assertEquals(1.94384, knots, 0.00001) + } +} diff --git a/test-runner/src/test/kotlin/org/terst/nav/nmea/NmeaParserTest.kt b/test-runner/src/test/kotlin/org/terst/nav/nmea/NmeaParserTest.kt new file mode 100644 index 0000000..e43b7ab --- /dev/null +++ b/test-runner/src/test/kotlin/org/terst/nav/nmea/NmeaParserTest.kt @@ -0,0 +1,103 @@ +package org.terst.nav.nmea + +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test + +class NmeaParserTest { + + private lateinit var parser: NmeaParser + + @Before + fun setUp() { + parser = NmeaParser() + } + + // $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A + // lat: 48 + 7.038/60 = 48.1173°N, lon: 11 + 31.000/60 = 11.51667°E + // SOG 22.4 kn, COG 84.4° + + @Test + fun `valid RMC sentence parses latitude and longitude`() { + val sentence = "\$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A" + val pos = parser.parseRmc(sentence) + assertNotNull(pos) + assertEquals(48.1173, pos!!.latitude, 0.0001) + assertEquals(11.51667, pos.longitude, 0.0001) + } + + @Test + fun `valid RMC sentence parses SOG and COG`() { + val sentence = "\$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A" + val pos = parser.parseRmc(sentence) + assertNotNull(pos) + assertEquals(22.4, pos!!.sog, 0.001) + assertEquals(84.4, pos.cog, 0.001) + } + + @Test + fun `void status V returns null`() { + val sentence = "\$GPRMC,123519,V,4807.038,N,01131.000,E,,,230394,003.1,W" + assertNull(parser.parseRmc(sentence)) + } + + @Test + fun `malformed sentence with too few fields returns null`() { + assertNull(parser.parseRmc("\$GPRMC,123519,A")) + } + + @Test + fun `empty string returns null`() { + assertNull(parser.parseRmc("")) + } + + @Test + fun `non-NMEA string returns null`() { + assertNull(parser.parseRmc("NOT_NMEA_DATA")) + } + + @Test + fun `south latitude is negative`() { + // lat: -(42 + 50.5589/60) = -42.84265 + val sentence = "\$GPRMC,092204.999,A,4250.5589,S,14718.5084,E,0.00,89.68,211200,," + val pos = parser.parseRmc(sentence) + assertNotNull(pos) + assertTrue("South latitude must be negative", pos!!.latitude < 0) + assertEquals(-42.84265, pos.latitude, 0.0001) + } + + @Test + fun `west longitude is negative`() { + // lon: -(11 + 31.000/60) = -11.51667 + val sentence = "\$GPRMC,123519,A,4807.038,N,01131.000,W,022.4,084.4,230394,003.1,E" + val pos = parser.parseRmc(sentence) + assertNotNull(pos) + assertTrue("West longitude must be negative", pos!!.longitude < 0) + assertEquals(-11.51667, pos.longitude, 0.0001) + } + + @Test + fun `SOG and COG parse with decimal precision`() { + // lon: -(118 + 1.5678/60) = -118.02613, lat: 33 + 52.1234/60 = 33.86872 + val sentence = "\$GPRMC,093456,A,3352.1234,N,11801.5678,W,12.345,270.5,140326,," + val pos = parser.parseRmc(sentence) + assertNotNull(pos) + assertEquals(12.345, pos!!.sog, 0.0001) + assertEquals(270.5, pos.cog, 0.0001) + } + + @Test + fun `empty SOG and COG fields default to zero`() { + val sentence = "\$GPRMC,123519,A,4807.038,N,01131.000,E,,,230394,003.1,W" + val pos = parser.parseRmc(sentence) + assertNotNull(pos) + assertEquals(0.0, pos!!.sog, 0.001) + assertEquals(0.0, pos.cog, 0.001) + } + + @Test + fun `non-RMC sentence returns null`() { + val sentence = "\$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,," + assertNull(parser.parseRmc(sentence)) + } +} -- cgit v1.2.3