diff --git a/DittoHeartbeat/build.gradle b/DittoHeartbeat/build.gradle index aa8e6ad..24c0dd4 100644 --- a/DittoHeartbeat/build.gradle +++ b/DittoHeartbeat/build.gradle @@ -3,11 +3,11 @@ plugins { id "org.jetbrains.kotlin.android" } +ext.libraryArtifactId = "dittoheartbeat" + apply from: "$rootProject.projectDir/gradle/android-common.gradle" apply from: "$rootProject.projectDir/gradle/deploy.gradle" -ext.libraryArtifactId = "dittoheartbeat" - android { namespace 'live.ditto.dittoheartbeat' } diff --git a/DittoHeartbeat/src/main/java/live.ditto.dittoheartbeat/Heartbeat.kt b/DittoHeartbeat/src/main/java/live.ditto.dittoheartbeat/Heartbeat.kt index df6732d..f191dd1 100644 --- a/DittoHeartbeat/src/main/java/live.ditto.dittoheartbeat/Heartbeat.kt +++ b/DittoHeartbeat/src/main/java/live.ditto.dittoheartbeat/Heartbeat.kt @@ -1,18 +1,13 @@ package live.ditto.dittoheartbeat import android.os.Build -import android.provider.SyncStateContract.Constants import androidx.annotation.RequiresApi -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.launch import live.ditto.Ditto import live.ditto.DittoConnectionType import live.ditto.DittoPeer -import live.ditto.DittoSyncSubscription import live.ditto.dittohealthmetrics.HealthMetric import live.ditto.dittohealthmetrics.HealthMetricProvider import org.joda.time.DateTime @@ -43,7 +38,6 @@ data class DittoHeartbeatInfo( var healthMetrics: MutableMap = mutableMapOf() ) -var heartbeatSubscription: DittoSyncSubscription? = null lateinit var info: DittoHeartbeatInfo private fun updateHealthMetrics(config: DittoHeartbeatConfig) { @@ -58,37 +52,30 @@ private fun updateHealthMetrics(config: DittoHeartbeatConfig) { fun startHeartbeat(ditto: Ditto, config: DittoHeartbeatConfig): Flow = flow { val cancelable = AtomicBoolean(false) - if (heartbeatSubscription == null) { - heartbeatSubscription = ditto.sync.registerSubscription("SELECT * FROM $HEARTBEAT_COLLECTION_COLLECTION_NAME") - } + while (!cancelable.get()) { + delay((config.secondsInterval * 1000L)) + val timestamp = DateTime().toISOString() + val presenceData = observePeers(ditto) + + info = + DittoHeartbeatInfo( + id = config.id, + lastUpdated = timestamp, + presenceSnapshotDirectlyConnectedPeers = getConnections(presenceData, ditto), + metaData = config.metaData, + presenceSnapshotDirectlyConnectedPeersCount = presenceData.size, + secondsInterval = config.secondsInterval, + sdk = ditto.sdkVersion, + schema = HEARTBEAT_COLLECTION_SCHEMA_VALUE, + peerKey = byteArrayToHash(ditto.presence.graph.localPeer.peerKey) + ) - try { - while (!cancelable.get()) { - delay((config.secondsInterval * 1000L)) - val timestamp = DateTime().toISOString() - val presenceData = observePeers(ditto) - - info = - DittoHeartbeatInfo( - id = config.id, - lastUpdated = timestamp, - presenceSnapshotDirectlyConnectedPeers = getConnections(presenceData, ditto) , - metaData = config.metaData, - presenceSnapshotDirectlyConnectedPeersCount = presenceData.size, - secondsInterval = config.secondsInterval, - sdk = ditto.sdkVersion, - schema = HEARTBEAT_COLLECTION_SCHEMA_VALUE, - peerKey = byteArrayToHash(ditto.presence.graph.localPeer.peerKey) - ) - - updateHealthMetrics(config) - - addToCollection(info, config, ditto) - emit(info) - } - } finally { - heartbeatSubscription!!.close() + updateHealthMetrics(config) + + addToCollection(info, config, ditto) + emit(info) } + } @RequiresApi(Build.VERSION_CODES.O) @@ -102,9 +89,8 @@ fun observePeers(ditto: Ditto): List { return presenceGraph.remotePeers } -val myCoroutineScope = CoroutineScope(Dispatchers.Main) fun addToCollection(info: DittoHeartbeatInfo, config: DittoHeartbeatConfig, ditto: Ditto) { - if(!config.publishToDittoCollection) return + if (!config.publishToDittoCollection) return val metaData = config.metaData ?: emptyMap() val doc = mapOf( "_id" to info.id, @@ -122,7 +108,10 @@ fun addToCollection(info: DittoHeartbeatInfo, config: DittoHeartbeatConfig, ditt } @RequiresApi(Build.VERSION_CODES.O) -fun getConnections(presenceSnapshotDirectlyConnectedPeers: List?, ditto: Ditto): Map { +fun getConnections( + presenceSnapshotDirectlyConnectedPeers: List?, + ditto: Ditto +): Map { val connectionsMap: MutableMap = mutableMapOf() @@ -136,7 +125,7 @@ fun getConnections(presenceSnapshotDirectlyConnectedPeers: List?, dit "bluetooth" to connectionsTypeMap["bt"], "p2pWifi" to connectionsTypeMap["p2pWifi"], "lan" to connectionsTypeMap["lan"], - ) + ) connectionsMap[byteArrayToHash(connection.peerKey)] = connectionMap } diff --git a/app/src/main/java/live/ditto/dittotoolsapp/HeartbeatView.kt b/app/src/main/java/live/ditto/dittotoolsapp/HeartbeatView.kt index 58f603e..afc26e3 100644 --- a/app/src/main/java/live/ditto/dittotoolsapp/HeartbeatView.kt +++ b/app/src/main/java/live/ditto/dittotoolsapp/HeartbeatView.kt @@ -22,6 +22,7 @@ import live.ditto.dittoheartbeat.startHeartbeat import java.util.* +@RequiresApi(Build.VERSION_CODES.O) @Composable fun ShowHeartbeatData(ditto: Ditto) { @@ -29,7 +30,7 @@ fun ShowHeartbeatData(ditto: Ditto) { var healthMetricProviders: MutableList = mutableListOf() val diskUsageViewModel = DiskUsageViewModel() diskUsageViewModel.isHealthyMBSizeLimit = 2048 //2GB worth of data - healthMetricProviders.add(diskUsageViewModel) +// healthMetricProviders.add(diskUsageViewModel) val config = DittoHeartbeatConfig( //id for testing only. Unique id will not persist