Skip to content

Commit 8e317c9

Browse files
author
Robert Winkler
committed
MQTT progress
1 parent 63fa322 commit 8e317c9

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class MqttProtocolClient(
147147

148148
try {
149149
log.debug(
150-
"Publishing to topic '{}' on broker '{}' with response expected on '{}'",
150+
"Publishing message to topic '{}' on broker '{}' with response expected on '{}'",
151151
topic,
152152
"${client.config.serverHost}:${client.config.serverPort}",
153153
responseTopic

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class MqttProtocolServer(
172172

173173
private suspend fun exposeTD(thing: ExposedThing) {
174174
val topic = thing.id
175-
log.debug("Publishing Thing Description '{}' to topic '{}'", thing.id, topic)
175+
//log.debug("Publishing Thing Description '{}' to topic '{}'", thing.id, topic)
176176

177177
try {
178178
val content = ContentManager.valueToContent(thing.toJson(), "application/json")
@@ -181,8 +181,10 @@ class MqttProtocolServer(
181181
.topicFilter(tdTopic)
182182
.qos(MqttQos.AT_LEAST_ONCE) // Use AT_LEAST_ONCE QoS level
183183
.callback { message ->
184+
val responseTopic = message.responseTopic.get()
185+
log.debug("Sending Thing Description of thing '{}' to topic '{}'", thing.id, responseTopic)
184186
CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
185-
respondToTopic(content, message.responseTopic.get())
187+
respondToTopic(content, responseTopic)
186188
}
187189
}
188190
.send().await() // Sending the subscription request

kotlin-wot/src/main/kotlin/Servient.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ai.ancf.lmos.wot.thing.filter.DiscoveryMethod.*
88
import ai.ancf.lmos.wot.thing.filter.ThingFilter
99
import ai.ancf.lmos.wot.thing.form.Form
1010
import ai.ancf.lmos.wot.thing.schema.DataSchemaValue
11-
import ai.ancf.lmos.wot.thing.schema.ObjectSchema
11+
import ai.ancf.lmos.wot.thing.schema.StringSchema
1212
import ai.ancf.lmos.wot.thing.schema.WoTExposedThing
1313
import ai.anfc.lmos.wot.binding.*
1414
import kotlinx.coroutines.async
@@ -166,18 +166,17 @@ class Servient(
166166
* @return
167167
*/
168168
suspend fun fetch(url: URI): ThingDescription {
169-
log.debug("Fetch thing from url '{}'", url)
169+
log.debug("Fetching thing description from '{}'", url)
170170
val scheme = url.scheme
171171
try {
172172
val client = getClientFor(scheme)
173173
if (client != null) {
174-
val form = Form(href = url.toString())
174+
val form = Form(href = url.toString(), contentType = "application/json")
175175
val content = client.readResource(form)
176176
try {
177-
val dataSchemaValue = ContentManager.contentToValue(content, ObjectSchema())
178-
dataSchemaValue as DataSchemaValue.ObjectValue
179-
180-
return ThingDescription.fromMap(dataSchemaValue.value)
177+
val dataSchemaValue = ContentManager.contentToValue(content, StringSchema())
178+
dataSchemaValue as DataSchemaValue.StringValue
179+
return ThingDescription.fromJson(dataSchemaValue.value)
181180
} catch (e: ContentCodecException) {
182181
throw ServientException("Error while fetching TD: ${e.message}", e)
183182
}

kotlin-wot/src/main/kotlin/thing/ExposedThing.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,12 @@ data class ThingDescription (
655655
* @param json JSON string to parse.
656656
* @return A Thing instance if parsing is successful, otherwise null.
657657
*/
658-
fun fromJson(json: String?): ThingDescription? {
658+
fun fromJson(json: String): ThingDescription {
659659
return try {
660660
JsonMapper.instance.readValue(json, ThingDescription::class.java)
661661
} catch (e: IOException) {
662662
log.warn("Unable to read json", e)
663-
null
663+
throw e
664664
}
665665
}
666666

kotlin-wot/src/main/kotlin/thing/form/Form.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ data class Form(
2929
val href: String, // Target IRI of a link or submission target of a form. Mandatory
3030

3131
@JsonInclude(NON_EMPTY)
32-
val contentType: String = "text/plain", // Assign a content type. Default is "text/plain"
32+
val contentType: String = "application/json", // Assign a content type. Default is "application/json"
3333

3434
@JsonInclude(NON_EMPTY)
3535
val contentCoding: String? = null, // Optional content coding values

0 commit comments

Comments
 (0)