|
| 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.adtpresentation |
| 10 | + |
| 11 | +import com.azure.digitaltwins.core.BasicDigitalTwin |
| 12 | +import com.azure.digitaltwins.core.BasicDigitalTwinMetadata |
| 13 | +import entity.environment.Humidity |
| 14 | +import entity.environment.LightUnit |
| 15 | +import entity.environment.Luminosity |
| 16 | +import entity.environment.Presence |
| 17 | +import entity.environment.Temperature |
| 18 | +import entity.environment.TemperatureUnit |
| 19 | +import entity.zone.Room |
| 20 | +import entity.zone.RoomEnvironmentalData |
| 21 | +import entity.zone.RoomID |
| 22 | +import entity.zone.RoomType |
| 23 | +import entity.zone.ZoneID |
| 24 | + |
| 25 | +/** |
| 26 | + * Module to wrap all the presentation-related stuff for Azure Digital Twins with [entity.zone.Room]. |
| 27 | + */ |
| 28 | +object RoomAdtPresentation { |
| 29 | + private const val OPERATING_ROOM_MODEL = "dtmi:io:github:smartoperatingblock:OperatingRoom;1" |
| 30 | + private const val PRE_OPERATING_ROOM_MODEL = "dtmi:io:github:smartoperatingblock:PrePostOperatingRoom;1" |
| 31 | + private const val NAME_PROPERTY = "name" |
| 32 | + private const val ZONE_ID_PROPERTY = "zone_id" |
| 33 | + private const val TEMPERATURE_PROPERTY = "temperature" |
| 34 | + private const val HUMIDITY_PROPERTY = "humidity" |
| 35 | + private const val PRESENCE_INSIDE_PROPERTY = "presence_inside" |
| 36 | + private const val LUMINOSITY_PROPERTY = "luminosity" |
| 37 | + |
| 38 | + /** |
| 39 | + * Convert a [Room] to a Digital Twin. |
| 40 | + * Specifically this extension method will convert it into the Azure Digital Twins SDK [BasicDigitalTwin]. |
| 41 | + * @return the corresponding [BasicDigitalTwin]. |
| 42 | + */ |
| 43 | + fun Room.toDigitalTwin(): BasicDigitalTwin = |
| 44 | + BasicDigitalTwin(this.id.value) |
| 45 | + .setMetadata( |
| 46 | + BasicDigitalTwinMetadata().setModelId( |
| 47 | + when (this.type) { |
| 48 | + RoomType.OPERATING_ROOM -> OPERATING_ROOM_MODEL |
| 49 | + RoomType.PRE_OPERATING_ROOM -> PRE_OPERATING_ROOM_MODEL |
| 50 | + } |
| 51 | + ) |
| 52 | + ) |
| 53 | + .addToContents(NAME_PROPERTY, this.name.orEmpty()) |
| 54 | + .addToContents(ZONE_ID_PROPERTY, this.zoneId.value) |
| 55 | + |
| 56 | + /** |
| 57 | + * Obtain a [Room] instance from its Azure Digital Twins' [BasicDigitalTwin]. |
| 58 | + * @return the corresponding room. |
| 59 | + */ |
| 60 | + fun BasicDigitalTwin.toRoom(): Room = |
| 61 | + Room( |
| 62 | + id = RoomID(this.id), |
| 63 | + zoneId = ZoneID(this.contents[ZONE_ID_PROPERTY] as String), |
| 64 | + name = this.contents[NAME_PROPERTY] as String, |
| 65 | + type = if (this.metadata.modelId == OPERATING_ROOM_MODEL) { |
| 66 | + RoomType.OPERATING_ROOM |
| 67 | + } else RoomType.PRE_OPERATING_ROOM, |
| 68 | + environmentalData = RoomEnvironmentalData( |
| 69 | + temperature = this.contents[TEMPERATURE_PROPERTY]?.let { |
| 70 | + Temperature((it as Number).toDouble(), TemperatureUnit.CELSIUS) |
| 71 | + }, |
| 72 | + humidity = this.contents[HUMIDITY_PROPERTY]?.let { Humidity((it as Number).toDouble()) }, |
| 73 | + luminosity = this.contents[LUMINOSITY_PROPERTY]?.let { |
| 74 | + Luminosity((it as Number).toDouble(), LightUnit.LUX) |
| 75 | + }, |
| 76 | + presence = this.contents[PRESENCE_INSIDE_PROPERTY]?.let { Presence(it as Boolean) } |
| 77 | + ) |
| 78 | + ) |
| 79 | +} |
0 commit comments