@@ -8,6 +8,8 @@ import ai.ancf.lmos.wot.thing.ExposedThing
88import ai.ancf.lmos.wot.thing.ThingDescription
99import ai.ancf.lmos.wot.thing.form.Form
1010import ai.ancf.lmos.wot.thing.form.Operation
11+ import ai.ancf.lmos.wot.thing.schema.ContentListener
12+ import ai.ancf.lmos.wot.thing.schema.DataSchemaValue
1113import ai.ancf.lmos.wot.thing.schema.InteractionAffordance
1214import ai.ancf.lmos.wot.thing.schema.WoTExposedThing
1315import ai.anfc.lmos.wot.binding.ProtocolServer
@@ -219,31 +221,77 @@ fun Application.setupRouting(servient: Servient) {
219221 call.response.status(HttpStatusCode .NotFound )
220222 }
221223 }
224+ route(" /properties" ) {
225+ get {
226+ val id = call.parameters[" id" ] ? : return @get call.response.status(HttpStatusCode .BadRequest )
227+ val thing = servient.things[id] ? : return @get call.response.status(HttpStatusCode .NotFound )
228+ val properties : Map <String , Content > = thing.handleReadAllProperties()
229+ val response: MutableMap <String , Any ?> = mutableMapOf ()
230+ for ((key, value) in properties) {
231+ // Assuming content is not null as it's checked earlier
232+ when (val schemaValue: DataSchemaValue = ContentManager .contentToValue(value, null )) {
233+ is DataSchemaValue .BooleanValue -> {
234+ response[key] = schemaValue.value
235+ }
236+ is DataSchemaValue .IntegerValue -> {
237+ response[key] = schemaValue.value
238+ }
239+ is DataSchemaValue .NumberValue -> {
240+ response[key] = schemaValue.value
241+ }
242+ is DataSchemaValue .StringValue -> {
243+ response[key] = schemaValue.value
244+ }
245+ is DataSchemaValue .ObjectValue -> {
246+ response[key] = schemaValue.value
247+ }
248+ is DataSchemaValue .ArrayValue -> {
249+ response[key] = schemaValue.value
250+ }
251+ is DataSchemaValue .NullValue -> {
252+ response[key] = null
253+ }
254+ }
255+ }
256+ call.respond(response)
257+ }
258+ }
222259 route(" /properties/{name}" ) {
223- /*
224260 get(" /observable" ) {
225- call.respond("Observing property", typeInfo<String>())
261+ val id = call.parameters[" id" ] ? : return @get call.response.status(HttpStatusCode .BadRequest )
262+ val propertyName = call.parameters[" name" ] ? : return @get call.response.status(HttpStatusCode .BadRequest )
263+ val thing = servient.things[id] ? : return @get call.response.status(HttpStatusCode .NotFound )
264+ val property = thing.properties[propertyName] ? : return @get call.response.status(HttpStatusCode .NotFound )
265+ val contentListener = ContentListener { content: Content ->
266+ call.respondBytes { content.body }
267+ }
268+ thing.handleObserveProperty(propertyName, contentListener)
269+ }
270+ delete(" /observable" ) {
271+ val id = call.parameters[" id" ] ? : return @delete call.response.status(HttpStatusCode .BadRequest )
272+ val propertyName = call.parameters[" name" ] ? : return @delete call.response.status(HttpStatusCode .BadRequest )
273+ val thing = servient.things[id] ? : return @delete call.response.status(HttpStatusCode .NotFound )
274+ val property = thing.properties[propertyName] ? : return @delete call.response.status(HttpStatusCode .NotFound )
275+ val contentListener = ContentListener { content: Content ->
276+ call.respondBytes { content.body }
277+ }
278+ thing.handleUnobserveProperty(propertyName, contentListener)
226279 }
227- */
228280 get {
229281 val id = call.parameters[" id" ] ? : return @get call.response.status(HttpStatusCode .BadRequest )
230282 val propertyName = call.parameters[" name" ] ? : return @get call.response.status(HttpStatusCode .BadRequest )
231283 val thing = servient.things[id] ? : return @get call.response.status(HttpStatusCode .NotFound )
232- val property = thing.properties[propertyName]
233- if (property != null ) {
234- if (! property.writeOnly) {
235- try {
236- val content = thing.handleReadProperty(propertyName)
237- call.respondBytes { content.body }
238- }
239- catch (e: ContentCodecException ) {
240- call.response.status(HttpStatusCode .InternalServerError )
241- }
242- } else {
243- call.response.status(HttpStatusCode .BadRequest )
284+ val property = thing.properties[propertyName] ? : return @get call.response.status(HttpStatusCode .NotFound )
285+ if (! property.writeOnly) {
286+ try {
287+ val content = thing.handleReadProperty(propertyName)
288+ call.respondBytes { content.body }
289+ }
290+ catch (e: ContentCodecException ) {
291+ call.response.status(HttpStatusCode .InternalServerError )
244292 }
245293 } else {
246- call.response.status(HttpStatusCode .NotFound )
294+ call.response.status(HttpStatusCode .BadRequest )
247295 }
248296 }
249297 put {
@@ -271,7 +319,7 @@ fun Application.setupRouting(servient: Servient) {
271319 call.response.status(HttpStatusCode .BadRequest )
272320 }else {
273321 val actionResult = thing.handleInvokeAction(actionName, content)
274- if (actionResult != null && actionResult .body.isNotEmpty()) {
322+ if (actionResult.body.isNotEmpty()) {
275323 call.respondBytes { actionResult.body }
276324 } else {
277325 call.response.status(HttpStatusCode .NoContent )
@@ -280,6 +328,25 @@ fun Application.setupRouting(servient: Servient) {
280328
281329 }
282330 get(" /events/{name}" ) {
331+ val id = call.parameters[" id" ] ? : return @get call.response.status(HttpStatusCode .BadRequest )
332+ val eventName = call.parameters[" name" ] ? : return @get call.response.status(HttpStatusCode .BadRequest )
333+ val thing = servient.things[id] ? : return @get call.response.status(HttpStatusCode .NotFound )
334+ val event = thing.events[eventName] ? : return @get call.response.status(HttpStatusCode .NotFound )
335+ val contentListener = ContentListener { content: Content ->
336+ call.respondBytes { content.body }
337+ }
338+ thing.handleSubscribeEvent(eventName, contentListener)
339+ call.response.status(HttpStatusCode .OK )
340+ }
341+ delete(" /events/{name}" ) {
342+ val id = call.parameters[" id" ] ? : return @delete call.response.status(HttpStatusCode .BadRequest )
343+ val eventName = call.parameters[" name" ] ? : return @delete call.response.status(HttpStatusCode .BadRequest )
344+ val thing = servient.things[id] ? : return @delete call.response.status(HttpStatusCode .NotFound )
345+ val event = thing.events[eventName] ? : return @delete call.response.status(HttpStatusCode .NotFound )
346+ val contentListener = ContentListener { content: Content ->
347+ call.respondBytes { content.body }
348+ }
349+ thing.handleUnsubscribeEvent(eventName, contentListener)
283350 call.response.status(HttpStatusCode .OK )
284351 }
285352 }
0 commit comments