|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. Smart Operating Block |
| 3 | + * |
| 4 | + * Use of this source code is governed by an MIT-style |
| 5 | + * license that can be found in the LICENSE file or at |
| 6 | + * https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | + |
| 9 | +package infrastructure.digitaltwins |
| 10 | + |
| 11 | +import application.presenter.EventParser |
| 12 | +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper |
| 13 | +import com.fasterxml.jackson.module.kotlin.readValue |
| 14 | +import entities.environment.EnvironmentData.Humidity |
| 15 | +import entities.environment.EnvironmentData.Luminosity |
| 16 | +import entities.environment.EnvironmentData.Presence |
| 17 | +import entities.environment.EnvironmentData.Temperature |
| 18 | +import entities.events.EmptyEvent |
| 19 | +import entities.events.Event |
| 20 | +import entities.events.ProcessEvent |
| 21 | +import entities.events.RoomEvent |
| 22 | +import entities.events.TrackingEvent |
| 23 | +import entities.process.ProcessData.MedicalDeviceUsage |
| 24 | +import entities.process.ProcessData.ProcessInfo |
| 25 | +import infrastructure.digitaltwins.events.RelationshipEvents.RelationshipEvent |
| 26 | +import infrastructure.digitaltwins.events.UpdateEvents.UpdateTwinEvent |
| 27 | + |
| 28 | +/** |
| 29 | + * The parser of Azure Digital Twins Events. |
| 30 | + */ |
| 31 | +class DTEventParser : EventParser<String> { |
| 32 | + |
| 33 | + override fun parseEvent(inputEvent: String): Event<Any> { |
| 34 | + val mapper = jacksonObjectMapper() |
| 35 | + val eventMap = mapper.readValue<MutableMap<Any, Any>>(inputEvent) |
| 36 | + return when (eventMap["eventType"]) { |
| 37 | + "Microsoft.DigitalTwins.Twin.Update" -> { |
| 38 | + manageUpdateTwinEvent( |
| 39 | + mapper.readValue(inputEvent, UpdateTwinEvent::class.java) |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + "Microsoft.DigitalTwins.Relationship.Create" -> { |
| 44 | + manageRelationshipCreationEvent( |
| 45 | + mapper.readValue(inputEvent.replace("$", ""), RelationshipEvent::class.java) |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + "Microsoft.DigitalTwins.Relationship.Delete" -> { |
| 50 | + manageRelationshipDeleteEvent( |
| 51 | + mapper.readValue(inputEvent.replace("$", ""), RelationshipEvent::class.java) |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + else -> EmptyEvent() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private fun manageUpdateTwinEvent(updateTwinEvent: UpdateTwinEvent): Event<Any> = |
| 60 | + when (updateTwinEvent.data.patch[0].path) { |
| 61 | + "/temperature" -> RoomEvent( |
| 62 | + roomId = updateTwinEvent.id, |
| 63 | + data = Temperature(updateTwinEvent.data.patch[0].value as Double) |
| 64 | + ) |
| 65 | + "/humidity" -> RoomEvent( |
| 66 | + roomId = updateTwinEvent.id, |
| 67 | + data = Humidity(updateTwinEvent.data.patch[0].value as Int) |
| 68 | + ) |
| 69 | + "/luminosity" -> RoomEvent( |
| 70 | + roomId = updateTwinEvent.id, |
| 71 | + data = Luminosity(updateTwinEvent.data.patch[0].value as Double) |
| 72 | + ) |
| 73 | + "/presence_inside" -> { |
| 74 | + RoomEvent( |
| 75 | + roomId = updateTwinEvent.id, |
| 76 | + data = Presence(updateTwinEvent.data.patch[0].value as Boolean) |
| 77 | + ) |
| 78 | + } |
| 79 | + "/is_on_operating_table" -> { |
| 80 | + ProcessEvent( |
| 81 | + data = ProcessInfo("Patient on Operating Bed", updateTwinEvent.id) |
| 82 | + ) |
| 83 | + } |
| 84 | + else -> EmptyEvent() |
| 85 | + } |
| 86 | + |
| 87 | + private fun manageRelationshipCreationEvent(createdRelationship: RelationshipEvent): Event<Any> = |
| 88 | + when (createdRelationship.data.relationshipName) { |
| 89 | + "rel_is_inside" -> TrackingEvent( |
| 90 | + healthProfessionalId = createdRelationship.data.sourceId, |
| 91 | + roomId = createdRelationship.data.targetId, |
| 92 | + data = true |
| 93 | + ) |
| 94 | + "rel_use" -> ProcessEvent( |
| 95 | + data = MedicalDeviceUsage(createdRelationship.data.targetId, createdRelationship.data.sourceId) |
| 96 | + ) |
| 97 | + else -> EmptyEvent() |
| 98 | + } |
| 99 | + |
| 100 | + private fun manageRelationshipDeleteEvent(deletedRelationship: RelationshipEvent): Event<Any> = |
| 101 | + when (deletedRelationship.data.relationshipName) { |
| 102 | + "rel_is_inside" -> TrackingEvent( |
| 103 | + healthProfessionalId = deletedRelationship.data.sourceId, |
| 104 | + roomId = deletedRelationship.data.targetId, |
| 105 | + data = false |
| 106 | + ) |
| 107 | + else -> EmptyEvent() |
| 108 | + } |
| 109 | +} |
0 commit comments