Skip to content

Commit c34b23b

Browse files
author
Robert Winkler
committed
Initial WebSocketProtocolServer and Tests for Properties and Actions
1 parent fc19070 commit c34b23b

File tree

22 files changed

+1012
-386
lines changed

22 files changed

+1012
-386
lines changed

kotlin-wot-binding-http/src/main/kotlin/http/HttpProtocolServer.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fun Application.setupRouting(servient: Servient) {
266266
val contentListener = ContentListener { content: Content ->
267267
call.respondBytes { content.body }
268268
}
269-
thing.handleObserveProperty(propertyName, contentListener)
269+
thing.handleObserveProperty(propertyName = propertyName, listener = contentListener)
270270
}
271271
delete("/observable") {
272272
val id = call.parameters["id"] ?: return@delete call.response.status(HttpStatusCode.BadRequest)
@@ -276,7 +276,7 @@ fun Application.setupRouting(servient: Servient) {
276276
val contentListener = ContentListener { content: Content ->
277277
call.respondBytes { content.body }
278278
}
279-
thing.handleUnobserveProperty(propertyName, contentListener)
279+
thing.handleUnobserveProperty(propertyName = propertyName)
280280
}
281281
get {
282282
val id = call.parameters["id"] ?: return@get call.response.status(HttpStatusCode.BadRequest)
@@ -336,7 +336,7 @@ fun Application.setupRouting(servient: Servient) {
336336
val contentListener = ContentListener { content: Content ->
337337
call.respondBytes { content.body }
338338
}
339-
thing.handleSubscribeEvent(eventName, contentListener)
339+
thing.handleSubscribeEvent(eventName = eventName, listener = contentListener)
340340
call.response.status(HttpStatusCode.OK)
341341
}
342342
delete("/events/{name}") {
@@ -347,7 +347,7 @@ fun Application.setupRouting(servient: Servient) {
347347
val contentListener = ContentListener { content: Content ->
348348
call.respondBytes { content.body }
349349
}
350-
thing.handleUnsubscribeEvent(eventName, contentListener)
350+
thing.handleUnsubscribeEvent(eventName = eventName)
351351
call.response.status(HttpStatusCode.OK)
352352
}
353353
}

kotlin-wot-binding-mqtt/src/main/kotlin/mqtt/MqttProtocolServer.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class MqttProtocolServer(
115115
property.forms += observableForm
116116
log.debug("Assign '{}' to observe Property '{}'", observableHref, name)
117117

118-
val observeListener: suspend (Content) -> Unit = suspend@{ content ->
118+
val observeListener = ContentListener { content ->
119119
log.debug("MqttServer at $baseUrl publishing to Property topic '$observableTopic'")
120120
val buffer = content.body
121121
val publishMessage = Mqtt5Publish.builder()
@@ -126,7 +126,7 @@ class MqttProtocolServer(
126126
.build()
127127
client.publish(publishMessage).await()
128128
}
129-
thing.handleObserveProperty(name, observeListener)
129+
thing.handleObserveProperty(propertyName = name, listener = observeListener)
130130
}
131131
}
132132
}
@@ -160,7 +160,7 @@ class MqttProtocolServer(
160160
event.forms += (form)
161161
log.debug("Assigned '{}' to Event '{}'", href, name)
162162

163-
thing.handleSubscribeEvent(name, { content ->
163+
thing.handleSubscribeEvent(eventName = name, listener = { content ->
164164
log.debug("MqttServer at $baseUrl publishing to Events topic '$topic")
165165
val publishMessage = Mqtt5Publish.builder()
166166
.topic(topic)
@@ -323,7 +323,7 @@ class MqttProtocolServer(
323323
val contentListener = ContentListener { content ->
324324
respondToTopic(content, message.topic)
325325
}
326-
thing.handleSubscribeEvent(eventName, contentListener)
326+
thing.handleSubscribeEvent(eventName = eventName, listener = contentListener)
327327
}
328328

329329
private suspend fun respondToTopic(content: Content?, responseTopic: MqttTopic) {

kotlin-wot-binding-mqtt/src/test/kotlin/integration/MqttProtocolServerTest.kt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,27 +173,27 @@ class MqttProtocolServerTest {
173173
}
174174

175175

176-
@Test
177-
fun `subscribe to event and verify publishing`() = runTest {
178-
179-
val events = protocolClient.subscribeResource(Form(
180-
"${brokerUrl}/${exposedThing.id}/events/$EVENT_NAME",
181-
"application/json"
182-
))
183-
184-
events.test(timeout = 5.seconds) {
185-
// Trigger event
186-
exposedThing.emitEvent(EVENT_NAME, "\"testEvent\"".toInteractionInputValue())
187-
188-
// Verify that the event is emitted in the flow
189-
val content = awaitItem()
190-
val stringValue = ContentManager.contentToValue(content, StringSchema()) as DataSchemaValue.StringValue
191-
assertEquals("\"testEvent\"", stringValue.value)
192-
193-
// Optionally verify no further emissions or complete the flow
194-
cancelAndIgnoreRemainingEvents()
195-
}
196-
}
176+
@Test
177+
fun `subscribe to event and verify publishing`() = runTest {
178+
179+
val events = protocolClient.subscribeResource(Form(
180+
"${brokerUrl}/${exposedThing.id}/events/$EVENT_NAME",
181+
"application/json"
182+
))
183+
184+
events.test(timeout = 5.seconds) {
185+
// Trigger event
186+
exposedThing.emitEvent(EVENT_NAME, "\"testEvent\"".toInteractionInputValue())
187+
188+
// Verify that the event is emitted in the flow
189+
val content = awaitItem()
190+
val stringValue = ContentManager.contentToValue(content, StringSchema()) as DataSchemaValue.StringValue
191+
assertEquals("\"testEvent\"", stringValue.value)
192+
193+
// Optionally verify no further emissions or complete the flow
194+
cancelAndIgnoreRemainingEvents()
195+
}
196+
}
197197

198198
@Test
199199
fun `subscribe to property change and verify publishing`() = runTest {

kotlin-wot-binding-websocket/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
implementation("io.ktor:ktor-client-cio")
1313
implementation("io.ktor:ktor-client-auth")
1414
implementation("io.ktor:ktor-client-logging")
15+
implementation("io.ktor:ktor-server-call-logging")
1516
implementation("io.ktor:ktor-serialization-jackson")
1617
//implementation("io.insert-koin:koin-ktor:4.0.0")
1718
testImplementation("io.ktor:ktor-server-test-host")

0 commit comments

Comments
 (0)