Skip to content
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

Add Website as a device #465

Merged
merged 1 commit into from
Mar 16, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
@file:Suppress( "NON_EXPORTABLE_TYPE" )

package dk.cachet.carp.common.application.devices

import dk.cachet.carp.common.application.Trilean
import dk.cachet.carp.common.application.data.DataType
import dk.cachet.carp.common.application.sampling.DataTypeSamplingSchemeMap
import dk.cachet.carp.common.application.sampling.SamplingConfiguration
import dk.cachet.carp.common.application.tasks.TaskConfigurationList
import dk.cachet.carp.common.infrastructure.serialization.NotSerializable
import kotlinx.serialization.Required
import kotlinx.serialization.Serializable
import kotlin.js.JsExport
import kotlin.reflect.KClass


/**
* A website which participates in a study as a primary device.
*/
@Serializable
@JsExport
data class Website(
override val roleName: String,
override val isOptional: Boolean = false
) : PrimaryDeviceConfiguration<WebsiteDeviceRegistration, WebsiteDeviceRegistrationBuilder>()
{
object Sensors : DataTypeSamplingSchemeMap()
object Tasks : TaskConfigurationList()

override fun getSupportedDataTypes(): Set<DataType> = Sensors.keys
override fun getDataTypeSamplingSchemes(): DataTypeSamplingSchemeMap = Sensors

override val defaultSamplingConfiguration: Map<DataType, SamplingConfiguration> = emptyMap()

override fun createDeviceRegistrationBuilder(): WebsiteDeviceRegistrationBuilder =
WebsiteDeviceRegistrationBuilder()
override fun getRegistrationClass(): KClass<WebsiteDeviceRegistration> = WebsiteDeviceRegistration::class
override fun isValidRegistration( registration: WebsiteDeviceRegistration ): Trilean = Trilean.TRUE
}


/**
* A [DeviceRegistration] for a [Website], specifying the [url] where the study runs.
*/
@Serializable
@JsExport
data class WebsiteDeviceRegistration(
val url: String,
/**
* The HTTP User-Agent header of the user agent which made the HTTP request to [url].
*/
val userAgent: String,
@Required
override val deviceDisplayName: String? = userAgent
) : DeviceRegistration()
{
@Required
override val deviceId: String = url
}


@Suppress( "SERIALIZER_TYPE_INCOMPATIBLE" )
@Serializable( NotSerializable::class )
@JsExport
class WebsiteDeviceRegistrationBuilder : DeviceRegistrationBuilder<WebsiteDeviceRegistration>()
{
/**
* The web URL from which the [Website] is accessed.
*/
var url: String = ""

/**
* The HTTP User-Agent header of the user agent which made the HTTP request to [url].
*/
var userAgent: String = ""

override fun build(): WebsiteDeviceRegistration = WebsiteDeviceRegistration( url, userAgent, deviceDisplayName )
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ val COMMON_SERIAL_MODULE = SerializersModule {
{
subclass( CustomProtocolDevice::class )
subclass( Smartphone::class )
subclass( Website::class )

subclass( CustomPrimaryDeviceConfiguration::class )
}
Expand All @@ -87,6 +88,7 @@ val COMMON_SERIAL_MODULE = SerializersModule {
subclass( BLESerialNumberDeviceRegistration::class )
subclass( DefaultDeviceRegistration::class )
subclass( MACAddressDeviceRegistration::class )
subclass( WebsiteDeviceRegistration::class )

subclass( CustomDeviceRegistration::class )
defaultDeserializer { DeviceRegistrationSerializer }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ val commonInstances = listOf(
BLESerialNumberDeviceRegistration( "123456789" ),
CustomProtocolDevice( "User's phone" ),
Smartphone( "User's phone" ),
Website( "Study website" ),
WebsiteDeviceRegistration(
"https://www.examplestudy.com?studyId=42",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
),

// Shared device registrations in `devices` namespace.
DefaultDeviceRegistration(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dk.cachet.carp.common.application.devices

import kotlin.test.Test
import kotlin.test.assertEquals


class WebsiteTest
{
@Test
fun registration_builder_sets_properties()
{
val expectedUrl = "https://www.example.com"
val expectedUserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"

val registration = WebsiteDeviceRegistrationBuilder().apply {
url = expectedUrl
userAgent = expectedUserAgent
}.build()

assertEquals( expectedUrl, registration.url )
assertEquals( expectedUserAgent, registration.userAgent )
}
}
1 change: 1 addition & 0 deletions docs/carp-common.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ act as a hub to aggregate, synchronize, and upload incoming data received from o
| Class | Primary | Description |
|--------------------------------------------------------------------------------------------------------------------------------|:-------:|----------------------------------------------------------------------------------------------------------|
| [Smartphone](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/Smartphone.kt) | Yes | An internet-connected phone with built-in sensors. |
| [Website](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/Website.kt) | Yes | A website which participates in a study as a primary device. |
| [AltBeacon](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/AltBeacon.kt) | | A beacon meeting the open AltBeacon standard. |
| [BLEHeartRateDevice](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/BLEHeartRateDevice.kt) | | A Bluetooth device which implements a Heart Rate service. |
| [CustomProtocolDevice](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/CustomProtocolDevice.kt) | Yes | A primary device which uses a single `CustomProtocolTask` to determine how to run a study on the device. |
Expand Down
4 changes: 4 additions & 0 deletions rpc/schemas/common/devices/DeviceRegistration.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
{
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.MACAddressDeviceRegistration" } } },
"then": { "$ref": "MACAddressDeviceRegistration.json" }
},
{
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.WebsiteDeviceRegistration" } } },
"then": { "$ref": "WebsiteDeviceRegistration.json" }
}
],
"$defs": {
Expand Down
4 changes: 4 additions & 0 deletions rpc/schemas/common/devices/PrimaryDeviceConfiguration.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.Smartphone" } } },
"then": { "$ref": "Smartphone.json" }
},
{
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.Website" } } },
"then": { "$ref": "Website.json" }
}
],
"$defs": {
Expand Down
15 changes: 15 additions & 0 deletions rpc/schemas/common/devices/Website.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"allOf": [ { "$ref": "PrimaryDeviceConfiguration.json#PrimaryDeviceConfiguration" } ],
"properties": {
"__type": { "const": "dk.cachet.carp.common.application.devices.Website" }
},
"unevaluatedProperties": false,
"$defs": {
"DeviceRegistration": {
"$anchor": "DeviceRegistration",
"$ref": "WebsiteDeviceRegistration.json"
}
}
}
11 changes: 11 additions & 0 deletions rpc/schemas/common/devices/WebsiteDeviceRegistration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"allOf": [ { "$ref": "DeviceRegistration.json#DeviceRegistration" } ],
"properties": {
"url": { "type": "string", "format": "uri" },
"userAgent": { "type": "string" }
},
"required": [ "url", "userAgent" ],
"unevaluatedProperties": false
}
Loading