Skip to content

Commit b7c726e

Browse files
xelahaloWhathecode
authored andcommitted
Add Website primary device
1 parent 5d3ee7c commit b7c726e

File tree

9 files changed

+144
-0
lines changed

9 files changed

+144
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
@file:Suppress( "NON_EXPORTABLE_TYPE" )
2+
3+
package dk.cachet.carp.common.application.devices
4+
5+
import dk.cachet.carp.common.application.Trilean
6+
import dk.cachet.carp.common.application.data.DataType
7+
import dk.cachet.carp.common.application.sampling.DataTypeSamplingSchemeMap
8+
import dk.cachet.carp.common.application.sampling.SamplingConfiguration
9+
import dk.cachet.carp.common.application.tasks.TaskConfigurationList
10+
import dk.cachet.carp.common.infrastructure.serialization.NotSerializable
11+
import kotlinx.serialization.Required
12+
import kotlinx.serialization.Serializable
13+
import kotlin.js.JsExport
14+
import kotlin.reflect.KClass
15+
16+
17+
/**
18+
* A website which participates in a study as a primary device.
19+
*/
20+
@Serializable
21+
@JsExport
22+
data class Website(
23+
override val roleName: String,
24+
override val isOptional: Boolean = false
25+
) : PrimaryDeviceConfiguration<WebsiteDeviceRegistration, WebsiteDeviceRegistrationBuilder>()
26+
{
27+
object Sensors : DataTypeSamplingSchemeMap()
28+
object Tasks : TaskConfigurationList()
29+
30+
override fun getSupportedDataTypes(): Set<DataType> = Sensors.keys
31+
override fun getDataTypeSamplingSchemes(): DataTypeSamplingSchemeMap = Sensors
32+
33+
override val defaultSamplingConfiguration: Map<DataType, SamplingConfiguration> = emptyMap()
34+
35+
override fun createDeviceRegistrationBuilder(): WebsiteDeviceRegistrationBuilder =
36+
WebsiteDeviceRegistrationBuilder()
37+
override fun getRegistrationClass(): KClass<WebsiteDeviceRegistration> = WebsiteDeviceRegistration::class
38+
override fun isValidRegistration( registration: WebsiteDeviceRegistration ): Trilean = Trilean.TRUE
39+
}
40+
41+
42+
/**
43+
* A [DeviceRegistration] for a [Website], specifying the [url] where the study runs.
44+
*/
45+
@Serializable
46+
@JsExport
47+
data class WebsiteDeviceRegistration(
48+
val url: String,
49+
/**
50+
* The HTTP User-Agent header of the user agent which made the HTTP request to [url].
51+
*/
52+
val userAgent: String,
53+
@Required
54+
override val deviceDisplayName: String? = userAgent
55+
) : DeviceRegistration()
56+
{
57+
@Required
58+
override val deviceId: String = url
59+
}
60+
61+
62+
@Suppress( "SERIALIZER_TYPE_INCOMPATIBLE" )
63+
@Serializable( NotSerializable::class )
64+
@JsExport
65+
class WebsiteDeviceRegistrationBuilder : DeviceRegistrationBuilder<WebsiteDeviceRegistration>()
66+
{
67+
/**
68+
* The web URL from which the [Website] is accessed.
69+
*/
70+
var url: String = ""
71+
72+
/**
73+
* The HTTP User-Agent header of the user agent which made the HTTP request to [url].
74+
*/
75+
var userAgent: String = ""
76+
77+
override fun build(): WebsiteDeviceRegistration = WebsiteDeviceRegistration( url, userAgent, deviceDisplayName )
78+
}

carp.common/src/commonMain/kotlin/dk/cachet/carp/common/infrastructure/serialization/Serialization.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ val COMMON_SERIAL_MODULE = SerializersModule {
6363
{
6464
subclass( CustomProtocolDevice::class )
6565
subclass( Smartphone::class )
66+
subclass( Website::class )
6667

6768
subclass( CustomPrimaryDeviceConfiguration::class )
6869
}
@@ -87,6 +88,7 @@ val COMMON_SERIAL_MODULE = SerializersModule {
8788
subclass( BLESerialNumberDeviceRegistration::class )
8889
subclass( DefaultDeviceRegistration::class )
8990
subclass( MACAddressDeviceRegistration::class )
91+
subclass( WebsiteDeviceRegistration::class )
9092

9193
subclass( CustomDeviceRegistration::class )
9294
defaultDeserializer { DeviceRegistrationSerializer }

carp.common/src/commonTest/kotlin/dk/cachet/carp/common/application/TestInstances.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ val commonInstances = listOf(
4848
BLESerialNumberDeviceRegistration( "123456789" ),
4949
CustomProtocolDevice( "User's phone" ),
5050
Smartphone( "User's phone" ),
51+
Website( "Study website" ),
52+
WebsiteDeviceRegistration(
53+
"https://www.examplestudy.com?studyId=42",
54+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
55+
),
5156

5257
// Shared device registrations in `devices` namespace.
5358
DefaultDeviceRegistration(),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dk.cachet.carp.common.application.devices
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
7+
class WebsiteTest
8+
{
9+
@Test
10+
fun registration_builder_sets_properties()
11+
{
12+
val expectedUrl = "https://www.example.com"
13+
val expectedUserAgent =
14+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
15+
16+
val registration = WebsiteDeviceRegistrationBuilder().apply {
17+
url = expectedUrl
18+
userAgent = expectedUserAgent
19+
}.build()
20+
21+
assertEquals( expectedUrl, registration.url )
22+
assertEquals( expectedUserAgent, registration.userAgent )
23+
}
24+
}

docs/carp-common.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ act as a hub to aggregate, synchronize, and upload incoming data received from o
7373
| Class | Primary | Description |
7474
|--------------------------------------------------------------------------------------------------------------------------------|:-------:|----------------------------------------------------------------------------------------------------------|
7575
| [Smartphone](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/Smartphone.kt) | Yes | An internet-connected phone with built-in sensors. |
76+
| [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. |
7677
| [AltBeacon](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/AltBeacon.kt) | | A beacon meeting the open AltBeacon standard. |
7778
| [BLEHeartRateDevice](../carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/BLEHeartRateDevice.kt) | | A Bluetooth device which implements a Heart Rate service. |
7879
| [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. |

rpc/schemas/common/devices/DeviceRegistration.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
{
1818
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.MACAddressDeviceRegistration" } } },
1919
"then": { "$ref": "MACAddressDeviceRegistration.json" }
20+
},
21+
{
22+
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.WebsiteDeviceRegistration" } } },
23+
"then": { "$ref": "WebsiteDeviceRegistration.json" }
2024
}
2125
],
2226
"$defs": {

rpc/schemas/common/devices/PrimaryDeviceConfiguration.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
{
66
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.Smartphone" } } },
77
"then": { "$ref": "Smartphone.json" }
8+
},
9+
{
10+
"if": { "properties": { "__type": { "const": "dk.cachet.carp.common.application.devices.Website" } } },
11+
"then": { "$ref": "Website.json" }
812
}
913
],
1014
"$defs": {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2019-09/schema",
3+
"type": "object",
4+
"allOf": [ { "$ref": "PrimaryDeviceConfiguration.json#PrimaryDeviceConfiguration" } ],
5+
"properties": {
6+
"__type": { "const": "dk.cachet.carp.common.application.devices.Website" }
7+
},
8+
"unevaluatedProperties": false,
9+
"$defs": {
10+
"DeviceRegistration": {
11+
"$anchor": "DeviceRegistration",
12+
"$ref": "WebsiteDeviceRegistration.json"
13+
}
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2019-09/schema",
3+
"type": "object",
4+
"allOf": [ { "$ref": "DeviceRegistration.json#DeviceRegistration" } ],
5+
"properties": {
6+
"url": { "type": "string", "format": "uri" },
7+
"userAgent": { "type": "string" }
8+
},
9+
"required": [ "url", "userAgent" ],
10+
"unevaluatedProperties": false
11+
}

0 commit comments

Comments
 (0)