From c56f14a25903a5d15b78895f9cbf2803a12d34d4 Mon Sep 17 00:00:00 2001 From: Ludovic Goix Date: Thu, 23 Jul 2020 23:12:01 -0400 Subject: [PATCH 1/2] Fix last time issue with nodes --- .../geeksville/mesh/service/MeshService.kt | 24 ++++++++------ .../java/com/geeksville/mesh/PositionTest.kt | 5 +++ .../mesh/service/MeshServiceTest.kt | 31 +++++++++++++++++++ 3 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index 819f9b592..5a0469426 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -866,9 +866,10 @@ class MeshService : Service(), Logging { } /// Update our DB of users based on someone sending out a Position subpacket - private fun handleReceivedPosition(fromNum: Int, p: MeshProtos.Position) { + private fun handleReceivedPosition(fromNum: Int, p: MeshProtos.Position, defaultTime: Int = Position.currentTime()) { updateNodeInfo(fromNum) { - it.position = Position(p, it.position?.time ?: 0) + it.position = Position(p) + updateNodeInfoTime(it, defaultTime) } } @@ -942,22 +943,22 @@ class MeshService : Service(), Logging { val p = packet.decoded // If the rxTime was not set by the device (because device software was old), guess at a time - val rxTime = if (packet.rxTime == 0) packet.rxTime else currentSecond() + val rxTime = if (packet.rxTime != 0) packet.rxTime else currentSecond() // Update last seen for the node that sent the packet, but also for _our node_ because anytime a packet passes // through our node on the way to the phone that means that local node is also alive in the mesh - updateNodeInfo(fromNum) { - // Update our last seen based on any valid timestamps. If the device didn't provide a timestamp make one - val lastSeen = rxTime - it.position = it.position?.copy(time = lastSeen) - } updateNodeInfo(myNodeNum) { it.position = it.position?.copy(time = currentSecond()) } if (p.hasPosition()) - handleReceivedPosition(fromNum, p.position) + handleReceivedPosition(fromNum, p.position, rxTime) + else + updateNodeInfo(fromNum) { + // Update our last seen based on any valid timestamps. If the device didn't provide a timestamp make one + updateNodeInfoTime(it, rxTime) + } if (p.hasData()) handleReceivedData(packet) @@ -1620,3 +1621,8 @@ class MeshService : Service(), Logging { } } } + +public fun updateNodeInfoTime(it: NodeInfo, rxTime: Int) { + if (it.position?.time == null || it.position?.time!! < rxTime) + it.position = it.position?.copy(time = rxTime) +} \ No newline at end of file diff --git a/app/src/test/java/com/geeksville/mesh/PositionTest.kt b/app/src/test/java/com/geeksville/mesh/PositionTest.kt index f2053928a..4f7f4e171 100644 --- a/app/src/test/java/com/geeksville/mesh/PositionTest.kt +++ b/app/src/test/java/com/geeksville/mesh/PositionTest.kt @@ -14,5 +14,10 @@ class PositionTest { Assert.assertEquals(Position.degD(Position.degI(-89.0)), -89.0, 0.01) } + @Test + fun givenPositionCreatedWithoutTime_thenTimeIsSet() { + val position = Position(37.1, 121.1, 35) + Assert.assertTrue(position.time != 0) + } } diff --git a/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt b/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt new file mode 100644 index 000000000..f8db6328c --- /dev/null +++ b/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt @@ -0,0 +1,31 @@ +package com.geeksville.mesh.service + +import com.geeksville.mesh.MeshUser +import com.geeksville.mesh.NodeInfo +import com.geeksville.mesh.Position +import org.junit.Assert +import org.junit.Test + + +class MeshServiceTest { + + val nodeInfo = NodeInfo(4, MeshUser("+one", "User One", "U1"), Position(37.1, 121.1, 35, 10)) + + @Test + fun givenNodeInfo_whenUpdatingWithNewTime_thenPositionTimeIsUpdated() { + + val newerTime = 20 + updateNodeInfoTime(nodeInfo, newerTime) + Assert.assertEquals(newerTime, nodeInfo.position?.time) + } + + @Test + fun givenNodeInfo_whenUpdatingWithOldTime_thenPositionTimeIsNotUpdated() { + val olderTime = 5 + val timeBeforeTryingToUpdate = nodeInfo.position?.time + updateNodeInfoTime(nodeInfo, olderTime) + Assert.assertEquals(timeBeforeTryingToUpdate, nodeInfo.position?.time) + } +} + + From 241f4d978a026d5512b7771f768123f8484c3b13 Mon Sep 17 00:00:00 2001 From: Ludovic Goix Date: Thu, 23 Jul 2020 23:12:46 -0400 Subject: [PATCH 2/2] Try to fix Toast not showing when notification are disabled --- app/src/main/java/com/geeksville/mesh/MainActivity.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index a251abde5..787784e77 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -858,7 +858,7 @@ class MainActivity : AppCompatActivity(), Logging, return true } R.id.connectStatusImage -> { - Toast.makeText(this, item.title, Toast.LENGTH_SHORT).show() + Toast.makeText(applicationContext, item.title, Toast.LENGTH_SHORT).show() return true } else -> super.onOptionsItemSelected(item) @@ -869,7 +869,7 @@ class MainActivity : AppCompatActivity(), Logging, try { val packageInfo: PackageInfo = packageManager.getPackageInfo(packageName, 0) val versionName = packageInfo.versionName - Toast.makeText(this, versionName, Toast.LENGTH_LONG).show() + Toast.makeText(applicationContext, versionName, Toast.LENGTH_LONG).show() } catch (e: PackageManager.NameNotFoundException) { errormsg("Can not find the version: ${e.message}") }