-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a88381f
commit 44cf3b0
Showing
13 changed files
with
600 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/MTConnect.NET-JSON-cppagent/Streams/JsonDataSetEntries.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved. | ||
// TrakHound Inc. licenses this file to you under the MIT license. | ||
|
||
using MTConnect.Observations; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MTConnect.NET_JSON_cppagent.Streams | ||
{ | ||
[JsonConverter(typeof(JsonDataSetEntriesConverter))] | ||
public class JsonDataSetEntries | ||
{ | ||
public Dictionary<string, object> Entries { get; set; } | ||
|
||
public int Count { get; set; } | ||
|
||
public bool IsUnavailable { get; set; } | ||
|
||
|
||
public JsonDataSetEntries() { } | ||
|
||
public JsonDataSetEntries(bool isUnavailable) | ||
{ | ||
IsUnavailable = isUnavailable; | ||
} | ||
|
||
public JsonDataSetEntries(Dictionary<string, object> entries) | ||
{ | ||
Entries = entries; | ||
Count = entries != null ? entries.Count : 0; | ||
} | ||
|
||
|
||
public class JsonDataSetEntriesConverter : JsonConverter<JsonDataSetEntries> | ||
{ | ||
public override bool HandleNull => true; | ||
|
||
|
||
public override JsonDataSetEntries Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.StartObject) | ||
{ | ||
var obj = JsonObject.Parse(ref reader); | ||
var entries = obj.Deserialize<Dictionary<string, object>>(); | ||
return new JsonDataSetEntries(entries); | ||
} | ||
else | ||
{ | ||
reader.Skip(); // Unavailable | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, JsonDataSetEntries value, JsonSerializerOptions options) | ||
{ | ||
if (value != null && !value.IsUnavailable) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
if (!value.Entries.IsNullOrEmpty()) | ||
{ | ||
foreach (var entry in value.Entries) | ||
{ | ||
if (entry.Value.IsNumeric()) | ||
{ | ||
writer.WriteNumber(entry.Key, entry.Value.ToDouble()); | ||
} | ||
else | ||
{ | ||
writer.WriteString(entry.Key, entry.Value?.ToString()); | ||
} | ||
} | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
else | ||
{ | ||
writer.WriteStringValue(Observation.Unavailable); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.