Add timestamp to all messages

This commit is contained in:
Alexander Nozik 2021-09-29 12:10:16 +03:00
parent 553d819c54
commit b3898d2ab3
5 changed files with 41 additions and 6 deletions

View File

@ -17,6 +17,7 @@ kotlin {
commonMain{
dependencies {
api("space.kscience:dataforge-io:$dataforgeVersion")
api(npm.kotlinx.datetime)
}
}
}

View File

@ -1,5 +1,7 @@
package ru.mipt.npm.controls.api
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@ -16,6 +18,7 @@ public sealed class DeviceMessage {
public abstract val sourceDevice: Name?
public abstract val targetDevice: Name?
public abstract val comment: String?
public abstract val time: Instant?
/**
* Update the source device name for composition. If the original name is null, resulting name is also null.
@ -52,6 +55,7 @@ public data class PropertyChangedMessage(
override val sourceDevice: Name = Name.EMPTY,
override val targetDevice: Name? = null,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = block(sourceDevice))
}
@ -67,6 +71,7 @@ public data class PropertySetMessage(
override val sourceDevice: Name? = null,
override val targetDevice: Name,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = sourceDevice?.let(block))
}
@ -82,6 +87,7 @@ public data class PropertyGetMessage(
override val sourceDevice: Name? = null,
override val targetDevice: Name,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = sourceDevice?.let(block))
}
@ -95,6 +101,7 @@ public data class GetDescriptionMessage(
override val sourceDevice: Name? = null,
override val targetDevice: Name,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = sourceDevice?.let(block))
}
@ -109,6 +116,7 @@ public data class DescriptionMessage(
override val sourceDevice: Name,
override val targetDevice: Name? = null,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = block(sourceDevice))
}
@ -124,6 +132,7 @@ public data class ActionExecuteMessage(
override val sourceDevice: Name? = null,
override val targetDevice: Name,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = sourceDevice?.let(block))
}
@ -139,6 +148,7 @@ public data class ActionResultMessage(
override val sourceDevice: Name,
override val targetDevice: Name? = null,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = block(sourceDevice))
}
@ -153,6 +163,7 @@ public data class BinaryNotificationMessage(
override val sourceDevice: Name,
override val targetDevice: Name? = null,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = block(sourceDevice))
}
@ -167,6 +178,7 @@ public data class EmptyDeviceMessage(
override val sourceDevice: Name? = null,
override val targetDevice: Name? = null,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = sourceDevice?.let(block))
}
@ -182,6 +194,7 @@ public data class DeviceLogMessage(
override val sourceDevice: Name? = null,
override val targetDevice: Name? = null,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = sourceDevice?.let(block))
}
@ -198,6 +211,7 @@ public data class DeviceErrorMessage(
override val sourceDevice: Name,
override val targetDevice: Name? = null,
override val comment: String? = null,
override val time: Instant? = Clock.System.now()
) : DeviceMessage(){
override fun changeSource(block: (Name) -> Name):DeviceMessage = copy(sourceDevice = block(sourceDevice))
}

View File

@ -1,16 +1,24 @@
package ru.mipt.npm.controls.opcua.server
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.MetaSerializer
import space.kscience.dataforge.meta.isLeaf
import space.kscience.dataforge.values.*
import java.time.Instant
internal fun Meta.toOpc(statusCode: StatusCode = StatusCode.GOOD, time: DateTime? = null): DataValue {
/**
* Convert Meta to OPC data value using
*/
internal fun Meta.toOpc(
statusCode: StatusCode = StatusCode.GOOD,
sourceTime: DateTime? = null,
serverTime: DateTime? = null
): DataValue {
val variant: Variant = if (isLeaf) {
when (value?.type) {
null, ValueType.NULL -> Variant.NULL_VALUE
@ -24,7 +32,7 @@ internal fun Meta.toOpc(statusCode: StatusCode = StatusCode.GOOD, time: DateTime
}
}
} else {
Variant(Json.encodeToString(MetaSerializer, this))
Variant(Json.encodeToString(this))
}
return DataValue(variant, statusCode, time)
return DataValue(variant, statusCode, sourceTime,serverTime ?: DateTime(Instant.now()))
}

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -7,7 +7,6 @@ pluginManagement {
val toolsVersion = "0.10.4"
repositories {
mavenLocal()
maven("https://repo.kotlin.link")
mavenCentral()
gradlePluginPortal()
@ -21,6 +20,19 @@ pluginManagement {
}
}
dependencyResolutionManagement {
repositories {
maven("https://repo.kotlin.link")
mavenCentral()
}
versionCatalogs {
create("npm") {
from("ru.mipt.npm:version-catalog:0.10.4")
}
}
}
include(
":controls-core",
":controls-tcp",