Add comprehensive smoke and feature tests for Gateway API
- Introduced LtdFeatureTests to validate BACnet and OPC UA point updates, trend logging, and alarm rule functionality. - Added OpcUaClientTests to ensure proper OPC UA client configuration, point value synchronization, and session management. - Created OpcUaFeatureTests to test OPC UA server capabilities, including browsing, reading, writing, and subscription notifications. - Implemented native_bacnet_test.py for isolated BACnet wire testing, covering discovery, read/write operations, and command processing. - Added ui-theme-tabs.test.cjs to verify theme switching, tab interactions, and protocol form accessibility in the UI.
This commit is contained in:
1 parent
d9165a60ff
commit
98c48d9d78
80 files changed
+11120
-315
No files matched your search
@@ -0,0 +1,166 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Gateway.Api.Domain;
|
||||
|
||||
namespace Gateway.Api.Services;
|
||||
|
||||
internal enum ModbusArea
|
||||
{
|
||||
Coil,
|
||||
DiscreteInput,
|
||||
InputRegister,
|
||||
HoldingRegister
|
||||
}
|
||||
|
||||
internal enum ModbusDataType
|
||||
{
|
||||
Boolean,
|
||||
UInt16,
|
||||
Int16,
|
||||
UInt32,
|
||||
Int32,
|
||||
Float32,
|
||||
Float32WordSwap
|
||||
}
|
||||
|
||||
internal sealed record ModbusAddress(
|
||||
string Transport,
|
||||
Guid? ServerId,
|
||||
string? Host,
|
||||
int Port,
|
||||
byte UnitId,
|
||||
string? SerialPortAlias,
|
||||
ModbusArea Area,
|
||||
ushort Address,
|
||||
ModbusDataType DataType,
|
||||
int PollIntervalSeconds)
|
||||
{
|
||||
public int RegisterCount => Area is ModbusArea.Coil or ModbusArea.DiscreteInput ? 1 : DataType is ModbusDataType.UInt32 or ModbusDataType.Int32 or ModbusDataType.Float32 or ModbusDataType.Float32WordSwap ? 2 : 1;
|
||||
}
|
||||
|
||||
internal static class ModbusAddressing
|
||||
{
|
||||
public static bool TryParse(GatewayPoint point, out ModbusAddress address)
|
||||
{
|
||||
address = null!;
|
||||
if (point.Protocol != PointProtocol.Modbus || point.Address.ValueKind != JsonValueKind.Object)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var transport = GetString(point.Address, "transport")?.ToLowerInvariant() ?? "tcp";
|
||||
var serverIdText = GetString(point.Address, "serverId");
|
||||
var serverId = Guid.TryParse(serverIdText, out var parsedServerId) ? parsedServerId : (Guid?)null;
|
||||
var host = GetString(point.Address, "host");
|
||||
var port = GetInt(point.Address, "port") ?? 502;
|
||||
var unitId = GetInt(point.Address, "unitId") ?? 1;
|
||||
var serialPortAlias = GetString(point.Address, "serialPortAlias");
|
||||
var area = ParseArea(GetString(point.Address, "area"));
|
||||
var register = GetInt(point.Address, "address") ?? GetInt(point.Address, "register") ?? -1;
|
||||
var dataType = ParseDataType(GetString(point.Address, "dataType"), area);
|
||||
var interval = GetInt(point.Address, "pollIntervalSeconds") ?? 5;
|
||||
if (transport is not ("tcp" or "rtu") || port is < 1 or > 65_535 || unitId is < 1 or > 247 || register is < 0 or > 65_535 || interval is < 1 or > 86_400)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (transport == "tcp" && serverId is null && string.IsNullOrWhiteSpace(host))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (transport == "rtu" && string.IsNullOrWhiteSpace(serialPortAlias))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
address = new ModbusAddress(transport, serverId, host, port, (byte)unitId, serialPortAlias, area, (ushort)register, dataType, interval);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonElement Encode(ushort[] registers, bool? coil, ModbusAddress address)
|
||||
{
|
||||
object value = address.Area is ModbusArea.Coil or ModbusArea.DiscreteInput
|
||||
? coil ?? false
|
||||
: address.DataType switch
|
||||
{
|
||||
ModbusDataType.UInt16 => registers[0],
|
||||
ModbusDataType.Int16 => unchecked((short)registers[0]),
|
||||
ModbusDataType.UInt32 => ((uint)registers[0] << 16) | registers[1],
|
||||
ModbusDataType.Int32 => unchecked((int)(((uint)registers[0] << 16) | registers[1])),
|
||||
ModbusDataType.Float32 => BitConverter.Int32BitsToSingle((int)(((uint)registers[0] << 16) | registers[1])),
|
||||
ModbusDataType.Float32WordSwap => BitConverter.Int32BitsToSingle((int)(((uint)registers[1] << 16) | registers[0])),
|
||||
_ => registers[0]
|
||||
};
|
||||
return JsonSerializer.SerializeToElement(value);
|
||||
}
|
||||
|
||||
public static (ushort[] Registers, bool? Coil) Decode(JsonElement value, ModbusAddress address)
|
||||
{
|
||||
if (address.Area is ModbusArea.Coil or ModbusArea.DiscreteInput)
|
||||
{
|
||||
var coil = value.ValueKind == JsonValueKind.True || value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var number) && Math.Abs(number) > double.Epsilon ||
|
||||
value.ValueKind == JsonValueKind.String && bool.TryParse(value.GetString(), out var parsed) && parsed;
|
||||
return ([], coil);
|
||||
}
|
||||
|
||||
var numberValue = value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var numeric)
|
||||
? numeric
|
||||
: value.ValueKind == JsonValueKind.String && double.TryParse(value.GetString(), NumberStyles.Float, CultureInfo.InvariantCulture, out var parsedNumber)
|
||||
? parsedNumber
|
||||
: throw new DomainValidationException("Der Modbus-Wert muss numerisch sein.");
|
||||
return address.DataType switch
|
||||
{
|
||||
ModbusDataType.UInt16 => ([checked((ushort)Math.Round(numberValue))], null),
|
||||
ModbusDataType.Int16 => ([unchecked((ushort)checked((short)Math.Round(numberValue)))], null),
|
||||
ModbusDataType.UInt32 => ToRegisters(checked((uint)Math.Round(numberValue)), false),
|
||||
ModbusDataType.Int32 => ToRegisters(unchecked((uint)checked((int)Math.Round(numberValue))), false),
|
||||
ModbusDataType.Float32 => ToRegisters(unchecked((uint)BitConverter.SingleToInt32Bits(checked((float)numberValue))), false),
|
||||
ModbusDataType.Float32WordSwap => ToRegisters(unchecked((uint)BitConverter.SingleToInt32Bits(checked((float)numberValue))), true),
|
||||
_ => ([checked((ushort)Math.Round(numberValue))], null)
|
||||
};
|
||||
}
|
||||
|
||||
private static (ushort[] Registers, bool? Coil) ToRegisters(uint value, bool wordSwap)
|
||||
{
|
||||
var high = (ushort)(value >> 16);
|
||||
var low = (ushort)value;
|
||||
return (wordSwap ? [low, high] : [high, low], null);
|
||||
}
|
||||
|
||||
private static string? GetString(JsonElement element, string name) =>
|
||||
element.TryGetProperty(name, out var property) && property.ValueKind == JsonValueKind.String ? property.GetString()?.Trim() : null;
|
||||
|
||||
private static int? GetInt(JsonElement element, string name) =>
|
||||
element.TryGetProperty(name, out var property) && property.TryGetInt32(out var value) ? value : null;
|
||||
|
||||
private static ModbusArea ParseArea(string? raw) => raw?.Trim().ToLowerInvariant() switch
|
||||
{
|
||||
"coil" or "coils" => ModbusArea.Coil,
|
||||
"discrete" or "discreteinput" or "discrete-input" => ModbusArea.DiscreteInput,
|
||||
"input" or "inputregister" or "input-register" => ModbusArea.InputRegister,
|
||||
_ => ModbusArea.HoldingRegister
|
||||
};
|
||||
|
||||
private static ModbusDataType ParseDataType(string? raw, ModbusArea area)
|
||||
{
|
||||
if (area is ModbusArea.Coil or ModbusArea.DiscreteInput)
|
||||
{
|
||||
return ModbusDataType.Boolean;
|
||||
}
|
||||
return raw?.Trim().ToLowerInvariant() switch
|
||||
{
|
||||
"int16" => ModbusDataType.Int16,
|
||||
"uint32" => ModbusDataType.UInt32,
|
||||
"int32" => ModbusDataType.Int32,
|
||||
"float32" or "real" => ModbusDataType.Float32,
|
||||
"float32wordswap" or "float32-swap" or "float32_swap" => ModbusDataType.Float32WordSwap,
|
||||
_ => ModbusDataType.UInt16
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user