826312befe
Clears CommentChecker findings in src/ (3,280 -> 209, the remainder being false positives like PLC-5/IEEE-754/M0.0) by adding missing summaries/params/returns, normalising inheritdoc usage, and removing Gitea/review tracking IDs while keeping the rationale they annotated. Comment-only: comment-stripped sources are identical to the parent commit.
222 lines
11 KiB
C#
222 lines
11 KiB
C#
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS;
|
||
|
||
/// <summary>
|
||
/// Parsed FOCAS address covering the four addressing spaces a driver touches:
|
||
/// <see cref="FocasAreaKind.Pmc"/> (letter + byte + optional bit — <c>X0.0</c>, <c>R100</c>,
|
||
/// <c>F20.3</c>), <see cref="FocasAreaKind.Parameter"/> (CNC parameter number —
|
||
/// <c>PARAM:1020</c>, <c>PARAM:1815/0</c> for bit 0), <see cref="FocasAreaKind.Macro"/>
|
||
/// (macro variable number — <c>MACRO:100</c>, <c>MACRO:500</c>), and
|
||
/// <see cref="FocasAreaKind.Fixed"/> (a logical path into the driver's fixed tree —
|
||
/// <c>FIXED:Identity/SeriesNumber</c>, <c>FIXED:Axes/X/AbsolutePosition</c>), plus
|
||
/// <see cref="FocasAreaKind.Alarm"/> (a selector over the CNC's active alarm list — <c>ALARM:*</c>,
|
||
/// <c>ALARM:Servo/401</c>, <c>ALARM:OTHER</c>; see <see cref="FocasAlarmSelector"/>).
|
||
/// </summary>
|
||
/// <remarks>
|
||
/// <para>PMC letters: <c>X/Y</c> (IO), <c>F/G</c> (signals between PMC + CNC), <c>R</c> (internal
|
||
/// relay), <c>D</c> (data table), <c>C</c> (counter), <c>K</c> (keep relay), <c>A</c>
|
||
/// (message display), <c>E</c> (extended relay), <c>T</c> (timer). Byte numbering is 0-based;
|
||
/// bit index when present is 0–7 and uses <c>.N</c> for PMC or <c>/N</c> for parameters.</para>
|
||
/// <para>
|
||
/// <b><c>FIXED:</c> is not an area read.</b> The first three forms name a location the driver
|
||
/// asks the CNC for on demand; a <c>FIXED:</c> path names a field of the per-device fixed-tree
|
||
/// snapshot the driver's own poll loop already maintains (<c>cnc_sysinfo</c>, <c>cnc_rddynamic2</c>,
|
||
/// program info, timers, spindle data). It exists because the fixed tree is the only part of the
|
||
/// FOCAS surface with no address of its own, and v3 publishes nodes only for authored tags — so
|
||
/// without a form to author, the fixed tree could not be published at all.
|
||
/// </para>
|
||
/// <para>
|
||
/// <b>The path carries no device.</b> A tag's device comes from its raw-tag Device row, and an
|
||
/// address that also named a host would be a second, conflicting routing channel — which is
|
||
/// precisely the defect once reported: the browse leaf's <c>{host}/{path}</c> full reference was
|
||
/// written into the address field verbatim and then failed to parse, so every browse-committed
|
||
/// fixed-tree tag read <c>BadNodeIdUnknown</c> forever.
|
||
/// </para>
|
||
/// </remarks>
|
||
public sealed record FocasAddress(
|
||
FocasAreaKind Kind,
|
||
string? PmcLetter,
|
||
int Number,
|
||
int? BitIndex,
|
||
string? FixedPath = null,
|
||
FocasAlarmSelector? AlarmSelector = null)
|
||
{
|
||
/// <summary>The prefix that introduces a <see cref="FocasAreaKind.Fixed"/> address.</summary>
|
||
public const string FixedPrefix = "FIXED:";
|
||
|
||
/// <summary>The prefix that introduces a <see cref="FocasAreaKind.Alarm"/> address.</summary>
|
||
public const string AlarmPrefix = "ALARM:";
|
||
|
||
/// <summary>Gets the canonical string representation of this address.</summary>
|
||
public string Canonical => Kind switch
|
||
{
|
||
FocasAreaKind.Pmc => BitIndex is null
|
||
? $"{PmcLetter}{Number}"
|
||
: $"{PmcLetter}{Number}.{BitIndex}",
|
||
FocasAreaKind.Parameter => BitIndex is null
|
||
? $"PARAM:{Number}"
|
||
: $"PARAM:{Number}/{BitIndex}",
|
||
FocasAreaKind.Macro => $"MACRO:{Number}",
|
||
FocasAreaKind.Fixed => FixedPrefix + FixedPath,
|
||
FocasAreaKind.Alarm => AlarmPrefix + AlarmSelector?.Canonical,
|
||
_ => $"?{Number}",
|
||
};
|
||
|
||
/// <summary>
|
||
/// Builds the canonical authored address for a fixed-tree path. The one place the
|
||
/// <c>FIXED:</c> spelling is constructed, so the browse emitter and the parser cannot drift.
|
||
/// </summary>
|
||
/// <param name="path">The fixed-tree path, e.g. <c>Identity/SeriesNumber</c>.</param>
|
||
/// <returns>The canonical authored address string.</returns>
|
||
public static string ForFixedPath(string path) => FixedPrefix + path;
|
||
|
||
/// <summary>Builds an <see cref="FocasAreaKind.Alarm"/> address over <paramref name="selector"/>.</summary>
|
||
/// <param name="selector">The alarm selector.</param>
|
||
/// <returns>The address.</returns>
|
||
public static FocasAddress ForAlarm(FocasAlarmSelector selector) =>
|
||
new(FocasAreaKind.Alarm, null, 0, null, AlarmSelector: selector);
|
||
|
||
/// <summary>
|
||
/// Why <paramref name="value"/> is not a usable <c>ALARM:</c> address, or <see langword="null"/> when it
|
||
/// is one (or is not an <c>ALARM:</c> address at all). Shared by the AdminUI editor and the deploy-time
|
||
/// inspector so both say the same thing about the same string.
|
||
/// </summary>
|
||
/// <param name="value">The authored address.</param>
|
||
/// <returns>The refusal reason, or <see langword="null"/>.</returns>
|
||
public static string? DescribeAlarmAddressError(string? value)
|
||
{
|
||
var src = value?.Trim() ?? "";
|
||
if (!src.StartsWith(AlarmPrefix, StringComparison.OrdinalIgnoreCase)) return null;
|
||
return FocasAlarmSelector.TryParse(src[AlarmPrefix.Length..], out _, out var error) ? null : error;
|
||
}
|
||
|
||
/// <summary>Attempts to parse a FOCAS address from the given string.</summary>
|
||
/// <param name="value">The address string to parse.</param>
|
||
/// <returns>A FocasAddress if parsing succeeds; otherwise null.</returns>
|
||
public static FocasAddress? TryParse(string? value)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(value)) return null;
|
||
var src = value.Trim();
|
||
|
||
if (src.StartsWith("PARAM:", StringComparison.OrdinalIgnoreCase))
|
||
return ParseScoped(src["PARAM:".Length..], FocasAreaKind.Parameter, bitSeparator: '/');
|
||
|
||
if (src.StartsWith("MACRO:", StringComparison.OrdinalIgnoreCase))
|
||
return ParseScoped(src["MACRO:".Length..], FocasAreaKind.Macro, bitSeparator: null);
|
||
|
||
// Fixed-tree path. Tested BEFORE the PMC branch on purpose: "F" is a valid PMC letter, so a
|
||
// FIXED: address entering that branch would be diagnosed as a malformed PMC address rather
|
||
// than as the fixed-tree address it is. (It would still return null either way — this is
|
||
// about which rule owns the string, not about the result.)
|
||
if (src.StartsWith(FixedPrefix, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var path = NormalizeFixedPath(src[FixedPrefix.Length..]);
|
||
return path is null ? null : new FocasAddress(FocasAreaKind.Fixed, null, 0, null, path);
|
||
}
|
||
|
||
// Alarm selector. Tested BEFORE the PMC branch for the same reason as FIXED: — "A" is a
|
||
// valid PMC letter (message display), so ALARM:… must be owned by this rule, not diagnosed as PMC.
|
||
if (src.StartsWith(AlarmPrefix, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var selector = FocasAlarmSelector.TryParse(src[AlarmPrefix.Length..]);
|
||
return selector is null ? null : ForAlarm(selector);
|
||
}
|
||
|
||
// PMC path: letter + digits + optional .bit
|
||
if (src.Length < 2 || !char.IsLetter(src[0])) return null;
|
||
var letter = src[0..1].ToUpperInvariant();
|
||
if (!IsValidPmcLetter(letter)) return null;
|
||
|
||
var remainder = src[1..];
|
||
int? bit = null;
|
||
var dotIdx = remainder.IndexOf('.');
|
||
if (dotIdx >= 0)
|
||
{
|
||
if (!int.TryParse(remainder[(dotIdx + 1)..], out var bitValue) || bitValue is < 0 or > 7)
|
||
return null;
|
||
bit = bitValue;
|
||
remainder = remainder[..dotIdx];
|
||
}
|
||
if (!int.TryParse(remainder, out var number) || number < 0) return null;
|
||
return new FocasAddress(FocasAreaKind.Pmc, letter, number, bit);
|
||
}
|
||
|
||
private static FocasAddress? ParseScoped(string body, FocasAreaKind kind, char? bitSeparator)
|
||
{
|
||
int? bit = null;
|
||
if (bitSeparator is char sep)
|
||
{
|
||
var slashIdx = body.IndexOf(sep);
|
||
if (slashIdx >= 0)
|
||
{
|
||
if (!int.TryParse(body[(slashIdx + 1)..], out var bitValue) || bitValue is < 0 or > 31)
|
||
return null;
|
||
bit = bitValue;
|
||
body = body[..slashIdx];
|
||
}
|
||
}
|
||
if (!int.TryParse(body, out var number) || number < 0) return null;
|
||
return new FocasAddress(kind, PmcLetter: null, number, bit);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Validates a fixed-tree path and returns it, or null when it is not a usable path.
|
||
/// Accepts <c>Segment[/Segment…]</c> where each segment is non-blank and carries no
|
||
/// <c>:</c> — a colon would collide with the <c>PARAM:</c>/<c>MACRO:</c>/<c>FIXED:</c>
|
||
/// prefix grammar and make the address ambiguous to read back.
|
||
/// </summary>
|
||
/// <remarks>
|
||
/// Deliberately shape-only: it does NOT check the path against the fixed tree a particular
|
||
/// CNC actually exposes. What that tree contains is per-device and discovered at runtime
|
||
/// (spindles, program info and timers are each capability-gated), so a parser that judged
|
||
/// membership would have to reject addresses that are valid on the machine they were
|
||
/// authored against. An unknown path resolves to no value at read time, which is where the
|
||
/// device is known.
|
||
/// </remarks>
|
||
private static string? NormalizeFixedPath(string body)
|
||
{
|
||
var path = body.Trim();
|
||
if (path.Length == 0) return null;
|
||
if (path.Contains(':', StringComparison.Ordinal)) return null;
|
||
if (path[0] == '/' || path[^1] == '/') return null;
|
||
|
||
foreach (var segment in path.Split('/'))
|
||
if (string.IsNullOrWhiteSpace(segment)) return null;
|
||
|
||
return path;
|
||
}
|
||
|
||
private static bool IsValidPmcLetter(string letter) => letter switch
|
||
{
|
||
"X" or "Y" or "F" or "G" or "R" or "D" or "C" or "K" or "A" or "E" or "T" => true,
|
||
_ => false,
|
||
};
|
||
}
|
||
|
||
/// <summary>Addressing-space kinds the driver understands.</summary>
|
||
public enum FocasAreaKind
|
||
{
|
||
/// <summary>PMC area — letter + byte + optional bit (<c>X0.0</c>, <c>R100</c>).</summary>
|
||
Pmc,
|
||
|
||
/// <summary>CNC parameter number (<c>PARAM:1020</c>, <c>PARAM:1815/0</c>).</summary>
|
||
Parameter,
|
||
|
||
/// <summary>Macro variable number (<c>MACRO:100</c>).</summary>
|
||
Macro,
|
||
|
||
/// <summary>
|
||
/// A logical path into the driver's per-device fixed tree (<c>FIXED:Identity/SeriesNumber</c>).
|
||
/// Served from the fixed-tree poll snapshot, never from an area read — and read-only, because
|
||
/// nothing in that tree is a writable location on the CNC.
|
||
/// </summary>
|
||
Fixed,
|
||
|
||
/// <summary>
|
||
/// A selector over the device's active CNC alarm list (<c>ALARM:*</c>, <c>ALARM:Servo/401-453</c>,
|
||
/// <c>ALARM:OTHER</c>). Meaningful only on a raw tag that also carries the cross-driver
|
||
/// <c>alarm</c> object, which makes it ONE Part 9 condition at its RawPath; it has no value to read
|
||
/// and nothing to write.
|
||
/// </summary>
|
||
Alarm,
|
||
}
|