first commit
This commit is contained in:
commit
b31e9a7261
38 files changed
+3822
No files matched your search
@@ -0,0 +1,209 @@
|
||||
using System.Text.Json;
|
||||
using Gateway.Api.Contracts;
|
||||
using Gateway.Api.Domain;
|
||||
using Gateway.Api.Options;
|
||||
using Gateway.Api.Services;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
var validator = new MappingValidator();
|
||||
var sourceOne = CreatePoint(PointProtocol.Internal, PointScope.Internal, null);
|
||||
var sourceTwo = CreatePoint(PointProtocol.Internal, PointScope.Internal, null);
|
||||
var bacnetTarget = CreatePoint(PointProtocol.Bacnet, PointScope.External, "device:77/analog-value:3");
|
||||
|
||||
var firstMapping = CreateMapping(sourceOne.Id, bacnetTarget.Id, MappingDirection.OneWay, null, false);
|
||||
var validSingleWriter = validator.Validate([sourceOne, sourceTwo, bacnetTarget], [firstMapping]);
|
||||
Assert(validSingleWriter.IsValid, "A single external writer must be valid.");
|
||||
|
||||
var conflictingSecondWriter = CreateMapping(sourceTwo.Id, bacnetTarget.Id, MappingDirection.OneWay, null, false);
|
||||
var conflict = validator.Validate([sourceOne, sourceTwo, bacnetTarget], [firstMapping, conflictingSecondWriter]);
|
||||
Assert(!conflict.IsValid, "Two unqualified writers must be rejected.");
|
||||
|
||||
var priorityOne = firstMapping with { BacnetPriority = 16, AcknowledgeMultipleExternalWriters = true };
|
||||
var priorityTwo = conflictingSecondWriter with { BacnetPriority = 14, AcknowledgeMultipleExternalWriters = true };
|
||||
var explicitPriorityCase = validator.Validate([sourceOne, sourceTwo, bacnetTarget], [priorityOne, priorityTwo]);
|
||||
Assert(explicitPriorityCase.IsValid && explicitPriorityCase.Warnings.Count == 1, "Explicitly acknowledged BACnet priorities must be permitted with a warning.");
|
||||
|
||||
var reverseMapping = CreateMapping(bacnetTarget.Id, sourceOne.Id, MappingDirection.OneWay, null, false);
|
||||
var loop = validator.Validate([sourceOne, bacnetTarget], [firstMapping, reverseMapping]);
|
||||
Assert(!loop.IsValid, "Two opposing one-way mappings must be rejected.");
|
||||
|
||||
var journal = new WriteJournal();
|
||||
journal.Register("bacnet:77/av:3", "21.5", TimeSpan.FromSeconds(10));
|
||||
Assert(journal.ConsumeReflection("bacnet:77/av:3", "21.5"), "The first adapter reflection must be consumed.");
|
||||
Assert(!journal.ConsumeReflection("bacnet:77/av:3", "21.5"), "A reflection cannot be consumed twice.");
|
||||
|
||||
var dataDirectory = Path.Combine(Path.GetTempPath(), "seiugw-smoke-" + Guid.NewGuid().ToString("N"));
|
||||
var options = Options.Create(new GatewayOptions
|
||||
{
|
||||
DataDirectory = dataDirectory,
|
||||
Bacnet = new BacnetBootstrapOptions { Interface = "eth0", DeviceInstance = 23, IpPort = 47808 },
|
||||
OpcUa = new OpcUaBootstrapOptions { Port = 4840 }
|
||||
});
|
||||
var database = new GatewayDatabase(options);
|
||||
var store = new GatewayStore(database, new PasswordService(), validator, new SecretProtector(options), options);
|
||||
store.Initialize();
|
||||
Assert(!store.HasAdmin(), "A new database must not contain an admin password.");
|
||||
store.CreateInitialAdmin(new SetInitialAdminRequest("correct horse battery staple", "P190726", 1));
|
||||
Assert(store.HasAdmin() && store.VerifyAdminPassword("correct horse battery staple"), "The configured admin must authenticate.");
|
||||
Assert(store.GetGatewayConfiguration().VendorId == 815 && store.GetGatewayConfiguration().DeviceInstance == 23, "BACnet bootstrap defaults must be persisted.");
|
||||
Assert(store.GetGatewayConfiguration().Installation?.HostName == "SE-UGW-P190726-ISP01", "First commissioning must create the requested Komari-visible hostname.");
|
||||
Assert(File.Exists(Path.Combine(dataDirectory, "provisioning", "host.json")), "The NixOS hostname helper must receive an identity-only provisioning request.");
|
||||
|
||||
var sourcePoint = store.CreatePoint(new CreatePointRequest(
|
||||
"Internal temperature",
|
||||
PointProtocol.Internal,
|
||||
PointScope.Internal,
|
||||
"Real",
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
JsonElementFor("{}")));
|
||||
store.UpdateMqttConfiguration(new UpdateMqttConfigurationRequest(
|
||||
new MqttConfiguration(true, "mqtt.example.local", 8883, true, true, "seiugw-test", "gateway"),
|
||||
"broker-secret",
|
||||
false));
|
||||
var mqttConfiguration = store.GetMqttConfiguration();
|
||||
Assert(mqttConfiguration.Configuration.UseTls && mqttConfiguration.Configuration.IgnoreCertificateErrors && mqttConfiguration.HasPassword, "MQTTS configuration and encrypted password marker must be persisted.");
|
||||
Assert(store.GetMqttConnectionConfiguration().Password == "broker-secret", "The MQTT client must receive the locally protected broker password.");
|
||||
|
||||
var analogMqttMapping = store.CreateMqttMapping(new CreateMqttMappingRequest(
|
||||
sourcePoint.Id,
|
||||
MqttMappingFormat.SeMonitoring,
|
||||
MqttDirection.Publish,
|
||||
MqttQualityOfService.AtLeastOnce,
|
||||
true,
|
||||
2019,
|
||||
"P1909015",
|
||||
"ISP1",
|
||||
SeMonitoringCategory.Analog,
|
||||
"iVlTempFuehler4Speicher",
|
||||
null,
|
||||
null));
|
||||
Assert(
|
||||
GatewayStore.GetMqttTopic(analogMqttMapping) == "Projekte2019/P1909015/ISP1/Analog/iVlTempFuehler4Speicher_Wert",
|
||||
"SE Monitoring topics must match the requested hierarchy.");
|
||||
|
||||
var invalidNotifyMappingRejected = false;
|
||||
try
|
||||
{
|
||||
store.CreateMqttMapping(new CreateMqttMappingRequest(
|
||||
sourcePoint.Id,
|
||||
MqttMappingFormat.SeMonitoring,
|
||||
MqttDirection.Publish,
|
||||
MqttQualityOfService.AtLeastOnce,
|
||||
false,
|
||||
2019,
|
||||
"P1909015",
|
||||
"ISP1",
|
||||
SeMonitoringCategory.Notify,
|
||||
"temperatureAsNotify",
|
||||
null,
|
||||
null));
|
||||
}
|
||||
catch (DomainValidationException)
|
||||
{
|
||||
invalidNotifyMappingRejected = true;
|
||||
}
|
||||
Assert(invalidNotifyMappingRejected, "Analog values must not be mapped to Notify.");
|
||||
|
||||
var setpoint = store.CreatePoint(new CreatePointRequest(
|
||||
"Supply setpoint",
|
||||
PointProtocol.Internal,
|
||||
PointScope.Internal,
|
||||
"Real",
|
||||
true,
|
||||
true,
|
||||
null,
|
||||
JsonElementFor("{}")));
|
||||
var setpointMqttMapping = store.CreateMqttMapping(new CreateMqttMappingRequest(
|
||||
setpoint.Id,
|
||||
MqttMappingFormat.SeMonitoring,
|
||||
MqttDirection.Bidirectional,
|
||||
MqttQualityOfService.AtLeastOnce,
|
||||
false,
|
||||
2019,
|
||||
"P1909015",
|
||||
"ISP1",
|
||||
SeMonitoringCategory.Setpoints,
|
||||
"supplyTemperature",
|
||||
null,
|
||||
null));
|
||||
Assert(GatewayStore.GetMqttTopic(setpointMqttMapping).Contains("/Setpoints/", StringComparison.Ordinal), "Setpoints must use their dedicated bidirectional category.");
|
||||
store.SetPointValue(setpoint.Id, JsonElementFor("21.5"), PointProtocol.Mqtt, DateTimeOffset.UtcNow);
|
||||
Assert(store.ListPointValues().Single(value => value.PointId == setpoint.Id).Value.GetDouble() == 21.5, "Incoming MQTT values must persist as point values.");
|
||||
|
||||
var schedule = store.CreatePoint(new CreatePointRequest(
|
||||
"Week schedule",
|
||||
PointProtocol.Bacnet,
|
||||
PointScope.Gateway,
|
||||
"Schedule",
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
JsonElementFor("{}"),
|
||||
BacnetObjectKind.Schedule));
|
||||
var scheduleJsonMapping = store.CreateMqttMapping(new CreateMqttMappingRequest(
|
||||
schedule.Id,
|
||||
MqttMappingFormat.Json,
|
||||
MqttDirection.Publish,
|
||||
MqttQualityOfService.AtLeastOnce,
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
"plant/schedule",
|
||||
"weekProgram"));
|
||||
Assert(GatewayStore.GetMqttTopic(scheduleJsonMapping) == "plant/schedule", "Structured BACnet values must be mappable through JSON MQTT.");
|
||||
var trendLog = store.CreateTrendLog(new CreateTrendLogRequest(sourcePoint.Id, "Temperature history", 101, 60, 2, true));
|
||||
var historyStart = new DateTimeOffset(2026, 10, 7, 8, 0, 0, TimeSpan.Zero);
|
||||
store.AppendTrendRecord(trendLog.Id, new AppendTrendRecordRequest(JsonElementFor("21.5"), PointProtocol.Internal, null, historyStart));
|
||||
store.AppendTrendRecord(trendLog.Id, new AppendTrendRecordRequest(JsonElementFor("22.0"), PointProtocol.Internal, null, historyStart.AddMinutes(1)));
|
||||
store.AppendTrendRecord(trendLog.Id, new AppendTrendRecordRequest(JsonElementFor("22.5"), PointProtocol.Internal, null, historyStart.AddMinutes(2)));
|
||||
var retainedRecords = store.ListTrendRecords(trendLog.Id, 10);
|
||||
Assert(retainedRecords.Count == 2 && retainedRecords[0].SequenceNumber == 3, "Trend retention must remove only the oldest records.");
|
||||
|
||||
var sampledSourcePoint = store.CreatePoint(new CreatePointRequest(
|
||||
"Internal humidity",
|
||||
PointProtocol.Internal,
|
||||
PointScope.Internal,
|
||||
"Real",
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
JsonElementFor("{}")));
|
||||
var sampledLog = store.CreateTrendLog(new CreateTrendLogRequest(sampledSourcePoint.Id, "Humidity sampled", 102, 60, 10, true));
|
||||
var sampleTime = new DateTimeOffset(2026, 10, 7, 9, 0, 0, TimeSpan.Zero);
|
||||
Assert(store.RecordPointObservation(sampledSourcePoint.Id, JsonElementFor("20.0"), PointProtocol.Internal, null, sampleTime).Count == 1, "The first source observation must be recorded.");
|
||||
Assert(store.RecordPointObservation(sampledSourcePoint.Id, JsonElementFor("20.1"), PointProtocol.Internal, null, sampleTime.AddSeconds(30)).Count == 0, "Observations inside the configured interval must not be recorded twice.");
|
||||
Assert(store.RecordPointObservation(sampledSourcePoint.Id, JsonElementFor("20.2"), PointProtocol.Internal, null, sampleTime.AddSeconds(60)).Count == 1, "Observations at the configured interval must be recorded.");
|
||||
Assert(store.ListTrendRecords(sampledLog.Id, 10).Count == 2, "The sampled trend log must contain two records.");
|
||||
|
||||
Console.WriteLine("Gateway smoke tests passed.");
|
||||
|
||||
static GatewayPoint CreatePoint(PointProtocol protocol, PointScope scope, string? externalIdentity)
|
||||
{
|
||||
using var document = JsonDocument.Parse("{}");
|
||||
return new GatewayPoint(Guid.NewGuid(), Guid.NewGuid().ToString(), protocol, scope, "Real", true, true, externalIdentity, document.RootElement.Clone(), DateTimeOffset.UtcNow);
|
||||
}
|
||||
|
||||
static PointMapping CreateMapping(Guid source, Guid target, MappingDirection direction, int? priority, bool acknowledged)
|
||||
{
|
||||
using var document = JsonDocument.Parse("{}");
|
||||
return new PointMapping(Guid.NewGuid(), source, target, direction, true, priority, acknowledged, document.RootElement.Clone(), DateTimeOffset.UtcNow);
|
||||
}
|
||||
|
||||
static JsonElement JsonElementFor(string json)
|
||||
{
|
||||
using var document = JsonDocument.Parse(json);
|
||||
return document.RootElement.Clone();
|
||||
}
|
||||
|
||||
static void Assert(bool condition, string message)
|
||||
{
|
||||
if (!condition)
|
||||
{
|
||||
throw new InvalidOperationException(message);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user