Skip to content

Commit 1196160

Browse files
Added observation validation properties introduced in MTConnect v2.5
1 parent ff732b7 commit 1196160

12 files changed

Lines changed: 267 additions & 20 deletions

File tree

build/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Reflection;
22

3-
[assembly: AssemblyVersion("6.6.0")]
4-
[assembly: AssemblyFileVersion("6.6.0")]
3+
[assembly: AssemblyVersion("6.7.0")]
4+
[assembly: AssemblyFileVersion("6.7.0")]
55
[assembly: AssemblyCompany("TrakHound Inc.")]
6-
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]
6+
[assembly: AssemblyCopyright("Copyright (c) 2025 TrakHound Inc., All Rights Reserved.")]

libraries/MTConnect.NET-Common/Agents/MTConnectAgent.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,9 @@ private IObservationOutput CreateObservation(IDataItem dataItem, IObservation ob
699699
observationOutput._instanceId = _instanceId;
700700
observationOutput._sequence = observation.Sequence;
701701
observationOutput._timestamp = observation.Timestamp;
702+
observationOutput._quality = observation.Quality;
703+
observationOutput._deprecated = observation.Deprecated;
704+
observationOutput._extended = observation.Extended;
702705
observationOutput._values = observation.Values?.ToArray();
703706
return observationOutput;
704707
}
@@ -1932,12 +1935,11 @@ public bool AddObservation(string deviceKey, IObservationInput observationInput,
19321935
}
19331936

19341937
var success = false;
1935-
var validationResult = new ValidationResult(true);
1938+
var validationResult = dataItem.Validate(MTConnectVersion, input);
19361939

19371940
if (_configuration.InputValidationLevel > InputValidationLevel.Ignore)
19381941
{
19391942
// Validate Observation Input with DataItem type
1940-
validationResult = dataItem.Validate(MTConnectVersion, input);
19411943
if (!validationResult.IsValid) validationResult.Message = $"{dataItem.Type} : {dataItem.Id} : {validationResult.Message}";
19421944
}
19431945

@@ -1986,6 +1988,20 @@ public bool AddObservation(string deviceKey, IObservationInput observationInput,
19861988
observation.Timestamp = input.Timestamp.ToDateTime();
19871989
observation.AddValues(input.Values);
19881990

1991+
// Re-validate after adding default properties/values
1992+
validationResult = dataItem.Validate(_mtconnectVersion, observation);
1993+
1994+
if (_configuration.EnableValidation)
1995+
{
1996+
// Set Quality using Validation
1997+
if (validationResult.IsValid) observation.Quality = Quality.VALID;
1998+
else observation.Quality = Quality.INVALID;
1999+
2000+
// Set Deprecated / Extended flags
2001+
observation.Deprecated = dataItem.MaximumVersion != null && dataItem.MaximumVersion < _mtconnectVersion;
2002+
observation.Extended = dataItem.IsExtended || !validationResult.IsValid;
2003+
}
2004+
19892005
// Update Current Observations
19902006
if (dataItem.Category == DataItemCategory.CONDITION) UpdateCurrentCondition(deviceUuid, dataItem, observation);
19912007
else UpdateCurrentObservation(deviceUuid, dataItem, observation);

libraries/MTConnect.NET-Common/Agents/MTConnectAgentBroker.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.Linq;
18+
using System.Runtime.Serialization;
1819

1920
namespace MTConnect.Agents
2021
{
@@ -1257,6 +1258,17 @@ private IObservationOutput CreateObservation(IDataItem dataItem, ref BufferObser
12571258
observation._timestamp = bufferObservation.Timestamp.ToDateTime();
12581259
observation._timeZoneTimestamp = MTConnectTimeZone.GetTimestamp(observation._timestamp, TimeZoneOutput);
12591260

1261+
if (MTConnectVersion >= MTConnectVersions.Version25)
1262+
{
1263+
observation._quality = bufferObservation.Quality;
1264+
observation._deprecated = bufferObservation.Deprecated;
1265+
observation._extended = bufferObservation.Extended;
1266+
}
1267+
else
1268+
{
1269+
observation._quality = Quality.UNVERIFIABLE;
1270+
}
1271+
12601272
observation._values = bufferObservation.Values;
12611273
return observation;
12621274
}
@@ -1731,6 +1743,22 @@ public override void InitializeDataItems(IDevice device, long timestamp = 0)
17311743
case DataItemRepresentation.TIME_SERIES: observation.AddValue(ValueKeys.SampleCount, 0); break;
17321744
}
17331745

1746+
// Temporarily Set Sequence to a value for validation
1747+
observation.Sequence = 1;
1748+
var validationResult = dataItem.Validate(MTConnectVersion, observation);
1749+
observation.Sequence = 0;
1750+
1751+
if (Configuration.EnableValidation)
1752+
{
1753+
// Set Quality using Validation
1754+
if (validationResult.IsValid) observation.Quality = Quality.VALID;
1755+
else observation.Quality = Quality.INVALID;
1756+
1757+
// Set Deprecated / Extended flags
1758+
observation.Deprecated = dataItem.MaximumVersion != null && dataItem.MaximumVersion < MTConnectVersion;
1759+
observation.Extended = dataItem.IsExtended || !validationResult.IsValid;
1760+
}
1761+
17341762
var bufferObservation = new BufferObservation(bufferKey, observation);
17351763
var sequence = _observationBuffer.AddObservation(ref bufferObservation);
17361764
observation.Sequence = sequence;
@@ -1755,6 +1783,24 @@ protected override ulong OnAddObservation(string deviceUuid, IDataItem dataItem,
17551783
{
17561784
if (_observationBuffer != null && dataItem != null && observationInput != null)
17571785
{
1786+
var validationResult = dataItem.Validate(MTConnectVersion, observationInput);
1787+
1788+
var quality = Quality.UNVERIFIABLE;
1789+
var deprecated = false;
1790+
var extended = false;
1791+
1792+
if (Configuration.EnableValidation)
1793+
{
1794+
// Set Quality
1795+
if (validationResult.IsValid) quality = Quality.VALID;
1796+
else quality = Quality.INVALID;
1797+
1798+
// Set Deprecated / Extended flags
1799+
deprecated = dataItem.MaximumVersion != null && dataItem.MaximumVersion < MTConnectVersion;
1800+
extended = !validationResult.IsValid;
1801+
}
1802+
1803+
17581804
// Get the BufferKey to use for the ObservationBuffer
17591805
var bufferKey = GenerateBufferKey(deviceUuid, dataItem.Id);
17601806
var bufferObservation = new BufferObservation(
@@ -1763,7 +1809,10 @@ protected override ulong OnAddObservation(string deviceUuid, IDataItem dataItem,
17631809
dataItem.Representation,
17641810
observationInput.Values,
17651811
0,
1766-
observationInput.Timestamp
1812+
observationInput.Timestamp,
1813+
quality,
1814+
deprecated,
1815+
extended
17671816
);
17681817

17691818
// Add Observation to Streaming Buffer

libraries/MTConnect.NET-Common/Buffers/BufferObservation.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 TrakHound Inc., All Rights Reserved.
1+
// Copyright (c) 2025 TrakHound Inc., All Rights Reserved.
22
// TrakHound Inc. licenses this file to you under the MIT license.
33

44
using MTConnect.Devices;
@@ -16,6 +16,9 @@ public struct BufferObservation
1616
internal long _timestamp;
1717
internal byte _category;
1818
internal byte _representation;
19+
internal byte _quality;
20+
internal bool _deprecated;
21+
internal bool _extended;
1922

2023
public int Key
2124
{
@@ -81,6 +84,35 @@ public DataItemRepresentation Representation
8184
}
8285
}
8386

87+
public Quality Quality
88+
{
89+
get
90+
{
91+
switch (_quality)
92+
{
93+
case 0: return Quality.INVALID;
94+
case 2: return Quality.VALID;
95+
default: return Quality.UNVERIFIABLE;
96+
}
97+
}
98+
set
99+
{
100+
_quality = (byte)value;
101+
}
102+
}
103+
104+
public bool Deprecated
105+
{
106+
get => _deprecated;
107+
set => _deprecated = value;
108+
}
109+
110+
public bool Extended
111+
{
112+
get => _extended;
113+
set => _extended = value;
114+
}
115+
84116

85117
public bool IsValid =>
86118
Key >= 0 &&
@@ -97,6 +129,9 @@ public BufferObservation(int bufferKey, IObservation observation)
97129
_timestamp = observation.Timestamp.ToUnixTime();
98130
_category = (byte)observation.Category;
99131
_representation = (byte)observation.Representation;
132+
_quality = (byte)observation.Quality;
133+
_deprecated = observation.Deprecated;
134+
_extended = observation.Extended;
100135

101136
// Sort by Key Asc (needed for processing later on, and maybe better performance when searching)
102137
if (observation.Values != null)
@@ -113,6 +148,9 @@ public BufferObservation(int bufferKey, ulong sequence, IObservation observation
113148
_timestamp = observation.Timestamp.ToUnixTime();
114149
_category = (byte)observation.Category;
115150
_representation = (byte)observation.Representation;
151+
_quality = (byte)observation.Quality;
152+
_deprecated = observation.Deprecated;
153+
_extended = observation.Extended;
116154

117155
// Sort by Key Asc (needed for processing later on, and maybe better performance when searching)
118156
if (observation.Values != null)
@@ -128,14 +166,20 @@ public BufferObservation(
128166
DataItemRepresentation representation,
129167
IEnumerable<ObservationValue> values,
130168
ulong sequence,
131-
long timestamp
169+
long timestamp,
170+
Quality quality,
171+
bool deprecated,
172+
bool extended
132173
)
133174
{
134175
_key = bufferKey;
135176
_category = (byte)category;
136177
_representation = (byte)representation;
137178
_sequence = sequence;
138179
_timestamp = timestamp;
180+
_quality = (byte)quality;
181+
_deprecated = deprecated;
182+
_extended = extended;
139183

140184
// Sort by Key Asc (needed for processing later on, and maybe better performance when searching)
141185
if (values != null)

libraries/MTConnect.NET-Common/Devices/DataItem.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public string Hash
120120
/// </summary>
121121
public virtual Version MinimumVersion => DefaultMinimumVersion;
122122

123+
internal bool _isExtended;
124+
/// <summary>
125+
///
126+
/// </summary>
127+
public bool IsExtended => _isExtended;
128+
123129

124130
public DataItem()
125131
{
@@ -165,6 +171,7 @@ public DataItem(IDataItem dataItem)
165171
Filters = dataItem.Filters;
166172
InitialValue = dataItem.InitialValue;
167173
Discrete = dataItem.Discrete;
174+
_isExtended = dataItem.IsExtended;
168175
}
169176
}
170177

@@ -543,6 +550,7 @@ public static DataItem Create(IDataItem dataItem)
543550
di.Filters = dataItem.Filters;
544551
di.InitialValue = dataItem.InitialValue;
545552
di.Discrete = dataItem.Discrete;
553+
di._isExtended = type == typeof(DataItem);
546554
return di;
547555
}
548556

@@ -1060,6 +1068,8 @@ public static DataItem Process(IDataItem dataItem, Version mtconnectVersion = nu
10601068
obj.Container = dataItem.Container;
10611069
obj.Device = dataItem.Device;
10621070

1071+
obj._isExtended = obj.GetType() == typeof(DataItem);
1072+
10631073
// Check SampleRate
10641074
if (version >= MTConnectVersions.Version12) obj.SampleRate = dataItem.SampleRate;
10651075

libraries/MTConnect.NET-Common/Devices/IDataItem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public partial interface IDataItem : IMTConnectEntity
7575
/// </summary>
7676
Version MinimumVersion { get; }
7777

78+
/// <summary>
79+
///
80+
/// </summary>
81+
bool IsExtended { get; }
82+
7883

7984
/// <summary>
8085
/// Determine if the DataItem with the specified Observation is valid in the specified MTConnectVersion

libraries/MTConnect.NET-Common/Observations/IObservation.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 TrakHound Inc., All Rights Reserved.
1+
// Copyright (c) 2025 TrakHound Inc., All Rights Reserved.
22
// TrakHound Inc. licenses this file to you under the MIT license.
33

44
using MTConnect.Devices;
@@ -83,15 +83,30 @@ public interface IObservation : IMTConnectEntity
8383
DataItemRepresentation Representation { get; }
8484

8585
/// <summary>
86-
/// Gets the Values associated with this Observation. These values represent data recorded during an Observation.
86+
/// Indicates if the Observation is verifiable and is in accordance with the normative definitions within the MTConnect Standard.
8787
/// </summary>
88-
IEnumerable<ObservationValue> Values { get; }
88+
Quality Quality { get; }
89+
90+
/// <summary>
91+
/// Indicates if the Observation has any property or controlled vocabulary that has been deprecated in the MTConnect Standard.
92+
/// </summary>
93+
bool Deprecated { get; }
94+
95+
/// <summary>
96+
/// Indicates if the Observation has any property or controlled vocabulary that has been extended and cannot be validated.
97+
/// </summary>
98+
bool Extended { get; }
8999

90100
/// <summary>
91101
/// Returns whether the Observation is Unavailable meaning a valid value cannot be determined
92102
/// </summary>
93103
bool IsUnavailable { get; }
94104

105+
/// <summary>
106+
/// Gets the Values associated with this Observation. These values represent data recorded during an Observation.
107+
/// </summary>
108+
IEnumerable<ObservationValue> Values { get; }
109+
95110
/// <summary>
96111
/// Gets the Value with the specified ValueKey
97112
/// </summary>

0 commit comments

Comments
 (0)