Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Uns/EquipmentCsvParser.cs
T
Joseph Doherty 746267c2a2 fix(adminui): equipment CSV import reads RFC 4180 via CsvParser (R04 S6)
ImportEquipmentModal split each line on commas, so a quoted Manufacturer with
a comma silently shifted every later column. EquipmentCsvParser reads through
the shared Commons CsvParser (the raw-tag importer's authority): quoted
fields, doubled quotes, CRLF and blank lines parse; malformed quoting is an
error, not a guess. Header/shape messages are unchanged.

Refs #745
Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
2026-10-22 17:56:26 -04:00

86 lines
3.5 KiB
C#

using ZB.MOM.WW.OtOpcUa.Commons.Csv;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
/// <summary>
/// Parses the equipment-import CSV (R04 S6). It used to be a naive comma split, so a quoted
/// <c>Manufacturer</c> containing a comma silently shifted every later column with no error. It now
/// reads through the RFC 4180 <see cref="CsvParser"/> — the same authority the raw-tag importer uses —
/// so quoted fields, embedded commas/newlines and doubled quotes parse correctly, and malformed quoting
/// is reported instead of guessed at.
/// </summary>
public static class EquipmentCsvParser
{
/// <summary>The leading header columns the import requires, in order (case-insensitive).</summary>
public static readonly IReadOnlyList<string> RequiredColumns = ["Name", "MachineCode", "UnsLineId"];
/// <summary>
/// Parses the CSV. Requires a header row whose first three columns are Name, MachineCode, UnsLineId;
/// the optional ZTag, SAPID, Manufacturer, Model follow. Cells are trimmed; blank optional cells parse
/// as null; blank lines are skipped.
/// </summary>
/// <param name="csv">The pasted CSV text.</param>
/// <returns>The rows, or a null list with an operator-facing error.</returns>
public static (List<EquipmentInput>? Rows, string? Error) Parse(string? csv)
{
if (string.IsNullOrWhiteSpace(csv)) return (null, "CSV is empty.");
IReadOnlyList<string[]> records;
try
{
records = CsvParser.Parse(csv);
}
catch (FormatException ex)
{
return (null, $"The CSV is malformed: {ex.Message}");
}
var lines = records
.Where(r => r.Length > 1 || r[0].Trim().Length > 0)
.Select(r => r.Select(c => c.Trim()).ToArray())
.ToList();
if (lines.Count < 2) return (null, "Need a header row and at least one data row.");
var header = lines[0];
for (var i = 0; i < RequiredColumns.Count; i++)
{
if (i >= header.Length || !string.Equals(header[i], RequiredColumns[i], StringComparison.OrdinalIgnoreCase))
{
return (null, $"Header column #{i + 1} must be '{RequiredColumns[i]}' (got '{(i < header.Length ? header[i] : "")}').");
}
}
var rows = new List<EquipmentInput>(lines.Count - 1);
for (var lineIdx = 1; lineIdx < lines.Count; lineIdx++)
{
var parts = lines[lineIdx];
if (parts.Length < RequiredColumns.Count)
{
return (null, $"Row {lineIdx}: too few columns (got {parts.Length}, need {RequiredColumns.Count}).");
}
rows.Add(new EquipmentInput(
Name: parts[0],
MachineCode: parts[1],
UnsLineId: parts[2],
ZTag: NullIfEmpty(parts, 3),
SAPID: NullIfEmpty(parts, 4),
Manufacturer: NullIfEmpty(parts, 5),
Model: NullIfEmpty(parts, 6),
SerialNumber: null,
HardwareRevision: null,
SoftwareRevision: null,
YearOfConstruction: null,
AssetLocation: null,
ManufacturerUri: null,
DeviceManualUri: null,
Enabled: true));
}
return (rows, null);
}
private static string? NullIfEmpty(string[] parts, int idx) =>
idx < parts.Length && !string.IsNullOrWhiteSpace(parts[idx]) ? parts[idx] : null;
}