feat(mqtt): flatten Sparkplug Template/DataSet metrics to sibling tags (#515)

`SparkplugCodec` handled scalars; `DataSet`, `Template` and `PropertySet` were skipped
with a warning. They now flatten to addressable tags (owner decision, recorded on the
issue):

  Template  → sibling metrics, `/`-joined, recursively: Motor → Motor/Speed, Motor/Stator/Temp
  DataSet   → one metric per cell, {column}/{rowIndex}: Recipe → Recipe/Step/0, Recipe/Target/1
  PropertySet → node metadata, never tags — it describes a metric rather than being one

Flattening was chosen over OPC UA Structures because flattened scalars are consumable by
any client with no type dictionary, and the result mirrors how the `/raw` tree works.

**The row index is always present, including on row 0.** Emitting a bare column name for
a single-row table and an indexed one otherwise would make a tag's name depend on how many
rows the plant published — a table growing from one row to two would rename every tag
already authored against it.

**A DATA metric carries the parent's alias and no name**, so member names cannot be
composed until the alias table has resolved the parent. Expansion therefore happens in
`SparkplugIngestor.PublishStructuredMembers` — after resolution, at the single funnel both
the DATA and birth-value paths already pass through — not at decode time. The alias table
installs a binding per member so browse enumerates them and a DDATA member that omits its
datatype falls back to the birth's (guessing from the CLR type would read a signed Int32 of
-42 out of its unsigned field as 4294967254).

**Skip-and-warn survives, and refusal is whole** — the explicit requirement on the issue.
A template definition, an unnamed member, an extension value, a column/type mismatch, a
short row, or a payload past MaxDepth(5)/MaxMembers(512) refuses the ENTIRE metric. A
silently half-flattened UDT looks exactly like a complete one in the picker.

The browse picker offers the members and deliberately NOT the structured parent, which has
no value and no servable datatype. The rediscovery signature likewise tracks members rather
than the parent, or a UDT gaining a member would leave the signature unmoved.

Member paths contain `/`, which #509 already solved: the slashed name travels intact as
`metricName` while the derived tag Name is coerced to a legal RawPath segment.

Live-gated 3/3 against the Mosquitto fixture with the project-owned simulator extended to
publish a nested Template and a 2x2 DataSet. Falsified at three points: dropping the member
bindings, not expanding the parent, and refusing to flatten templates each turn exactly the
expected tests red.

Also fixes a regression from the #511 commit in the same branch: `AddOtOpcUaDriverFactories`
resolved `IDriverPrimaryRoleSource` with `GetRequiredService` but only `AddOtOpcUaRuntime`
registered it, so a graph wiring driver factories without the full runtime failed at
resolution. Two Host integration suites do exactly that and caught it; both extensions now
TryAdd the same concrete `RedundancyRoleView`, so the registration is order-independent and
the fail-loud resolve is kept.

Claude-Session: https://claude.ai/code/session_015p7wGqy3YpZNCpDzTpGMKo
This commit is contained in:
Joseph Doherty
2026-07-31 08:14:04 -04:00
parent 7f2108dcf2
commit c74ccc55b5
15 changed files with 1900 additions and 53 deletions
@@ -186,35 +186,82 @@ public sealed class SparkplugCodecTests
}
/// <summary>
/// <c>DataSet</c> and <c>Template</c> metrics are out of scope for v1. They must decode to an
/// explicit "unsupported" kind — never an exception on the MQTT dispatcher thread, and never a
/// silently-dropped metric that looks identical to a metric that was never published.
/// A <c>DataSet</c> metric decodes to <see cref="SparkplugValueKind.Structured"/> with its cells
/// flattened (Gitea #515) — never an exception on the MQTT dispatcher thread, and never a
/// silently-dropped metric that looks identical to one that was never published.
/// </summary>
[Fact]
public void Decode_GoldenNbirth_DataSetMetric_IsUnsupported_NotAThrow()
public void Decode_GoldenNbirth_DataSetMetric_FlattensToCells()
{
var payload = SparkplugCodec.Decode(SparkplugGoldenPayloads.Read(SparkplugGoldenPayloads.NbirthFile));
var config = payload.Metrics.Single(m => m.Name == "Config");
config.ValueKind.ShouldBe(SparkplugValueKind.Unsupported);
config.ValueKind.ShouldBe(SparkplugValueKind.Structured);
// The PARENT carries no value — its members do. A parent that reported a value would be a
// guess about which cell "the" value is.
config.Value.ShouldBeNull();
config.DataType.ShouldBe(DataType.DataSet);
var cell = config.MemberList.Single();
cell.Path.ShouldBe("col0/0");
cell.DataType.ShouldBe(DataType.Int32);
cell.Value.ShouldBe(1U);
}
/// <summary>A <c>Template</c> metric takes the same unsupported path as <c>DataSet</c>.</summary>
/// <summary>A <c>Template</c> metric flattens its members to sibling paths.</summary>
[Fact]
public void Decode_TemplateMetric_IsUnsupported()
public void Decode_TemplateMetric_FlattensMembersToSiblingPaths()
{
var template = new Payload.Types.Template { Version = "1", IsDefinition = false };
template.Metrics.Add(new Payload.Types.Metric
{
Name = "Speed", Datatype = (uint)DataType.Double, DoubleValue = 12.5,
});
template.Metrics.Add(new Payload.Types.Metric
{
Name = "Running", Datatype = (uint)DataType.Boolean, BooleanValue = true,
});
var payload = new Payload();
payload.Metrics.Add(new Payload.Types.Metric
{
Name = "Motor",
Datatype = (uint)DataType.Template,
TemplateValue = new Payload.Types.Template { Version = "1", IsDefinition = false },
TemplateValue = template,
});
SparkplugCodec.Decode(payload.ToByteArray()).Metrics.Single().ValueKind
.ShouldBe(SparkplugValueKind.Unsupported);
var metric = SparkplugCodec.Decode(payload.ToByteArray()).Metrics.Single();
metric.ValueKind.ShouldBe(SparkplugValueKind.Structured);
metric.MemberList.Select(m => m.Path).ShouldBe(["Speed", "Running"]);
metric.MemberList[0].Value.ShouldBe(12.5);
metric.MemberList[1].Value.ShouldBe(true);
}
/// <summary>
/// A template <b>definition</b> is refused rather than flattened, and refusal is still the
/// visible <see cref="SparkplugValueKind.Unsupported"/> kind.
/// </summary>
[Fact]
public void Decode_TemplateDefinition_StaysUnsupported()
{
// A definition is the UDT's SHAPE, published so consumers can understand instances — its member
// metrics carry no values. Flattening it would materialise a parallel set of tags that can
// never read, indistinguishable in the picker from the real instance's.
var definition = new Payload.Types.Template { Version = "1", IsDefinition = true };
definition.Metrics.Add(new Payload.Types.Metric { Name = "Speed", Datatype = (uint)DataType.Double });
var payload = new Payload();
payload.Metrics.Add(new Payload.Types.Metric
{
Name = "MotorType", Datatype = (uint)DataType.Template, TemplateValue = definition,
});
var metric = SparkplugCodec.Decode(payload.ToByteArray()).Metrics.Single();
metric.ValueKind.ShouldBe(SparkplugValueKind.Unsupported);
metric.MemberList.ShouldBeEmpty();
}
/// <summary>Payload and per-metric timestamps survive as raw Sparkplug epoch milliseconds.</summary>