Skip to content

Create library for representing Health Connect data in IEEE 1752 Open mHealth #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions HealthConnectOnOMH/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
44 changes: 44 additions & 0 deletions HealthConnectOnOMH/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "edu.stanford.healthconnectonomh"
compileSdk = 33

defaultConfig {
minSdk = 26

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.health.connect:connect-client:1.1.0-alpha02")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
Empty file.
21 changes: 21 additions & 0 deletions HealthConnectOnOMH/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.stanford.healthconnectonomh

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("edu.stanford.healthconnectonomh.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions HealthConnectOnOMH/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package edu.stanford.healthconnectonomh.converter

import androidx.health.connect.client.records.Record
import androidx.health.connect.client.records.StepsRecord
import edu.stanford.healthconnectonomh.omhmodels.DataPoint
import edu.stanford.healthconnectonomh.omhmodels.Header
import edu.stanford.healthconnectonomh.omhmodels.StartEndTimeInterval
import edu.stanford.healthconnectonomh.omhmodels.StepCount
import edu.stanford.healthconnectonomh.omhmodels.StepCountUnit
import edu.stanford.healthconnectonomh.omhmodels.StepCountUnitValue
import edu.stanford.healthconnectonomh.omhmodels.TimeIntervalTimeFrame
import java.time.ZoneOffset
import java.util.UUID

fun Record.toOMHDataPoint(): DataPoint? {
val schema = when(this) {
is StepsRecord -> {
StepCount(
stepCount = StepCountUnitValue(unit = StepCountUnit.STEPS, value = this.count.toDouble()),
effectiveTimeFrame = TimeIntervalTimeFrame(
StartEndTimeInterval(
this.startTime.atOffset(ZoneOffset.UTC),
this.endTime.atOffset(ZoneOffset.UTC)
)
)
)
}
else -> null
}

return schema?.let {
val timeInterval = schema.effectiveTimeFrame as TimeIntervalTimeFrame
DataPoint(
header = Header(
uuid = UUID.randomUUID(),
schemaId = schema.schemaId,
sourceCreationDateTime = (timeInterval.timeInterval as StartEndTimeInterval).startDateTime
),
body = it
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.stanford.healthconnectonomh.omhmodels



data class DataPoint(
val header: Header,
val body: Schema
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package edu.stanford.healthconnectonomh.omhmodels

import java.time.OffsetDateTime

typealias DateTime = OffsetDateTime
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.stanford.healthconnectonomh.omhmodels

enum class DescriptiveStatistic(
private val customSchemaValue: String? = null
) : SchemaEnumValue, Schema {
AVERAGE,
COUNT,
MAXIMUM,
MEDIAN,
MINIMUM,
STANDARD_DEVIATION,
SUM,
VARIANCE,
TWENTIETH_PERCENTILE("20th percentile"),
EIGHTIETH_PERCENTILE("80th percentile"),
LOWER_QUARTILE,
UPPER_QUARTILE,
QUARTILE_DEVIATION,
FIRST_QUINTILE("1st quintile"),
SECOND_QUINTILE("2nd quintile"),
THIRD_QUINTILE("3rd quintile"),
FOURTH_QUINTILE("4th quintile");

override val schemaValue: String
get() = customSchemaValue ?: name.lowercase().replace("_", " ")

override val schemaId: SchemaId
get() = SchemaId(IEEE_NAMESPACE.value, "descriptive-statistic", "1.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.stanford.healthconnectonomh.omhmodels

enum class DurationUnit(
override val schemaValue: String
) : Unit {
PICOSECOND("ps"),
NANOSECOND("ns"),
MICROSECOND("us"),
MILLISECOND("ms"),
SECOND("sec"),
MINUTE("min"),
HOUR("h"),
DAY("d"),
WEEK("wk"),
MONTH("Mo"),
YEAR("yr");
}

typealias DurationUnitValue = TypedUnitValue<DurationUnit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.stanford.healthconnectonomh.omhmodels

import java.util.*

data class Header(
val uuid: UUID,
val schemaId: SchemaId,
val sourceCreationDateTime: DateTime,
val modality: Modality? = null,
val externalDatasheets: List<Datasheet>? = null
) {
data class Datasheet(
val datasheetType: String? = null,
val datasheetReference: String
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package edu.stanford.healthconnectonomh.omhmodels

enum class Modality {
SENSED,
SELF_REPORTED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package edu.stanford.healthconnectonomh.omhmodels

interface Schema {
val schemaId: SchemaId
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package edu.stanford.healthconnectonomh.omhmodels

interface SchemaEnumValue {
val schemaValue: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package edu.stanford.healthconnectonomh.omhmodels

import java.util.regex.Pattern

/**
* A schema identifier. It consists of a namespace, a name, and a version. A schema identifier unambiguously identifies
* a single, immutable schema. The namespace is used to avoid naming collisions in schemas written by different groups
* or organisations.
*
* @author Emerson Farrugia
* @version 1.0
* @see [schema-id](https://www.openmhealth.org/documentation/#/schema-docs/schema-library/schemas/omh_schema-id)
*/

data class SchemaId(
val namespace: SchemaNamespace = OMH_NAMESPACE,
val name: SchemaName,
val version: SchemaVersion
) : Comparable<SchemaId> {

constructor(namespace: String, name: String, version: String) : this(
namespace = SchemaNamespace(namespace),
name = SchemaName(name),
version = SchemaVersion.fromString(version)
)

companion object {
val PATTERN: Pattern = Pattern.compile("([^:]+):([^:]+):([^:]+)")

fun fromString(id: String): SchemaId {
val matcher = PATTERN.matcher(id)
require(matcher.matches()) { "A malformed schema ID has been specified." }

return SchemaId(
namespace = matcher.group(1),
name = matcher.group(2),
version = matcher.group(3)
)
}

private val comparator = compareBy<SchemaId> { it.namespace }
.thenBy { it.name }
.thenBy { it.version }
}

override fun toString(): String = "$namespace:$name:$version"

override fun compareTo(other: SchemaId): Int =
comparator.compare(this, other)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package edu.stanford.healthconnectonomh.omhmodels

import java.util.regex.Pattern

/**
* A schema name, which uniquely [identifies][SchemaId] a schema when combined with a [SchemaNamespace] and [SchemaVersion].
*
* @author Emerson Farrugia
*/

@JvmInline
value class SchemaName(
val value: String
) : Comparable<SchemaName> {

init {
require(isValid(value)) { "A malformed name has been specified." }
}

companion object {
private const val PATTERN_STRING = "[a-zA-Z0-9-]+"
private val PATTERN: Pattern = Pattern.compile(PATTERN_STRING)

fun isValid(string: String?): Boolean =
string
?.let { PATTERN.matcher(it).matches() }
?: true
}

override fun toString(): String = value

override fun compareTo(other: SchemaName): Int =
value.compareTo(other.value)
}
Loading