From 33e1aafa012f9dc4fcdad79a4b888bf3378e7a3b Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 8 Jun 2023 10:03:41 +0300 Subject: [PATCH] WIP fixing angles --- .../kotlin/space/kscience/controls/misc/timeMeta.kt} | 8 ++++---- .../kscience/controls/modbus/ModbusRegistryMap.kt | 1 + .../kscience/controls/opcua/client/MetaBsdParser.kt | 10 ++++++---- 3 files changed, 11 insertions(+), 8 deletions(-) rename controls-core/src/{jvmMain/kotlin/space/kscience/controls/misc/javaTimeMeta.kt => commonMain/kotlin/space/kscience/controls/misc/timeMeta.kt} (69%) diff --git a/controls-core/src/jvmMain/kotlin/space/kscience/controls/misc/javaTimeMeta.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/misc/timeMeta.kt similarity index 69% rename from controls-core/src/jvmMain/kotlin/space/kscience/controls/misc/javaTimeMeta.kt rename to controls-core/src/commonMain/kotlin/space/kscience/controls/misc/timeMeta.kt index 891c30b..11683d9 100644 --- a/controls-core/src/jvmMain/kotlin/space/kscience/controls/misc/javaTimeMeta.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/misc/timeMeta.kt @@ -1,18 +1,18 @@ package space.kscience.controls.misc +import kotlinx.datetime.Instant import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.long -import java.time.Instant // TODO move to core public fun Instant.toMeta(): Meta = Meta { - "seconds" put epochSecond - "nanos" put nano + "seconds" put epochSeconds + "nanos" put nanosecondsOfSecond } -public fun Meta.instant(): Instant = value?.long?.let { Instant.ofEpochMilli(it) } ?: Instant.ofEpochSecond( +public fun Meta.instant(): Instant = value?.long?.let { Instant.fromEpochMilliseconds(it) } ?: Instant.fromEpochSeconds( get("seconds")?.long ?: 0L, get("nanos")?.long ?: 0L, ) \ No newline at end of file diff --git a/controls-modbus/src/main/kotlin/space/kscience/controls/modbus/ModbusRegistryMap.kt b/controls-modbus/src/main/kotlin/space/kscience/controls/modbus/ModbusRegistryMap.kt index 80e997a..22f2619 100644 --- a/controls-modbus/src/main/kotlin/space/kscience/controls/modbus/ModbusRegistryMap.kt +++ b/controls-modbus/src/main/kotlin/space/kscience/controls/modbus/ModbusRegistryMap.kt @@ -142,6 +142,7 @@ public abstract class ModbusRegistryMap { } public fun print(map: ModbusRegistryMap, to: Appendable = System.out) { + validate(map) map.entries.entries.sortedBy { it.key.address }.forEach { (key, description) -> val typeString = when (key) { is ModbusRegistryKey.Coil -> "Coil" diff --git a/controls-opcua/src/main/kotlin/space/kscience/controls/opcua/client/MetaBsdParser.kt b/controls-opcua/src/main/kotlin/space/kscience/controls/opcua/client/MetaBsdParser.kt index 74257af..574343e 100644 --- a/controls-opcua/src/main/kotlin/space/kscience/controls/opcua/client/MetaBsdParser.kt +++ b/controls-opcua/src/main/kotlin/space/kscience/controls/opcua/client/MetaBsdParser.kt @@ -1,5 +1,7 @@ package space.kscience.controls.opcua.client +import kotlinx.datetime.toJavaInstant +import kotlinx.datetime.toKotlinInstant import org.eclipse.milo.opcua.binaryschema.AbstractCodec import org.eclipse.milo.opcua.binaryschema.parser.BsdParser import org.eclipse.milo.opcua.stack.core.UaSerializationException @@ -66,7 +68,7 @@ internal fun opcToMeta(value: Any?): Meta = when (value) { is Boolean -> Meta(value.asValue()) is String -> Meta(value.asValue()) is Char -> Meta(value.toString().asValue()) - is DateTime -> value.javaInstant.toMeta() + is DateTime -> value.javaInstant.toKotlinInstant().toMeta() is UUID -> Meta(value.toString().asValue()) is QualifiedName -> Meta { "namespaceIndex" put value.namespaceIndex @@ -79,9 +81,9 @@ internal fun opcToMeta(value: Any?): Meta = when (value) { is DataValue -> Meta { "value" put opcToMeta(value.value) // need SerializationContext to do that properly value.statusCode?.value?.let { "status" put Meta(it.asValue()) } - value.sourceTime?.javaInstant?.let { "sourceTime" put it.toMeta() } + value.sourceTime?.javaInstant?.let { "sourceTime" put it.toKotlinInstant().toMeta() } value.sourcePicoseconds?.let { "sourcePicoseconds" put Meta(it.asValue()) } - value.serverTime?.javaInstant?.let { "serverTime" put it.toMeta() } + value.serverTime?.javaInstant?.let { "serverTime" put it.toKotlinInstant().toMeta() } value.serverPicoseconds?.let { "serverPicoseconds" put Meta(it.asValue()) } } is ByteString -> Meta(value.bytesOrEmpty().asValue()) @@ -145,7 +147,7 @@ internal class MetaStructureCodec( "Float" -> member.value?.numberOrNull?.toFloat() "Double" -> member.value?.numberOrNull?.toDouble() "String" -> member.string - "DateTime" -> DateTime(member.instant()) + "DateTime" -> DateTime(member.instant().toJavaInstant()) "Guid" -> member.string?.let { UUID.fromString(it) } "ByteString" -> member.value?.list?.let { list -> ByteString(list.map { it.number.toByte() }.toByteArray())