Skip to content

Commit a6dce3a

Browse files
committed
more fixups
Signed-off-by: Neil South <[email protected]>
1 parent 5175a3a commit a6dce3a

File tree

12 files changed

+74
-60
lines changed

12 files changed

+74
-60
lines changed

src/Api/Hl7ApplicationConfigEntity.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.ComponentModel;
2019
using System.ComponentModel.DataAnnotations;
20+
using System.ComponentModel.DataAnnotations.Schema;
2121
using System.Linq;
2222
using FellowOakDicom;
2323
using Monai.Deploy.InformaticsGateway.Api.Storage;
@@ -28,6 +28,13 @@ namespace Monai.Deploy.InformaticsGateway.Api
2828
{
2929
public class Hl7ApplicationConfigEntity : MongoDBEntityBase
3030
{
31+
/// <summary>
32+
/// Gets or sets the name of a Hl7 application entity.
33+
/// This value must be unique.
34+
/// </summary>
35+
[Key, Column(Order = 0)]
36+
public string Name { get; set; } = default!;
37+
3138
/// <summary>
3239
/// Gets or sets the sending identifier.
3340
/// </summary>
@@ -48,6 +55,11 @@ public class Hl7ApplicationConfigEntity : MongoDBEntityBase
4855
[JsonProperty("data_mapping")]
4956
public List<StringKeyValuePair> DataMapping { get; set; } = new();
5057

58+
/// <summary>
59+
/// Optional list of data input plug-in type names to be executed by the <see cref="IInputHL7DataPlugInEngine"/>.
60+
/// </summary>
61+
public List<string> PlugInAssemblies { get; set; } = default!;
62+
5163
public IEnumerable<string> Validate()
5264
{
5365
var errors = new List<string>();

src/Database/EntityFramework/Configuration/Hl7ApplicationConfigConfiguration.cs

100644100755
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17+
using System.Text.Json.Serialization;
18+
using System.Text.Json;
1719
using Microsoft.EntityFrameworkCore;
20+
using Microsoft.EntityFrameworkCore.ChangeTracking;
1821
using Microsoft.EntityFrameworkCore.Metadata.Builders;
1922
using Monai.Deploy.InformaticsGateway.Api;
2023

@@ -24,7 +27,24 @@ internal class Hl7ApplicationConfigConfiguration : IEntityTypeConfiguration<Hl7A
2427
{
2528
public void Configure(EntityTypeBuilder<Hl7ApplicationConfigEntity> builder)
2629
{
30+
var valueComparer = new ValueComparer<List<string>>(
31+
(c1, c2) => c1!.SequenceEqual(c2!),
32+
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
33+
c => c.ToList());
34+
35+
var jsonSerializerSettings = new JsonSerializerOptions
36+
{
37+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
38+
};
39+
2740
builder.HasKey(j => j.Id);
41+
builder.Property(j => j.PlugInAssemblies)
42+
.HasConversion(
43+
v => JsonSerializer.Serialize(v, jsonSerializerSettings),
44+
v => JsonSerializer.Deserialize<List<string>>(v, jsonSerializerSettings)!)
45+
.Metadata.SetValueComparer(valueComparer);
46+
47+
builder.HasIndex(p => p.Name, "idx_hl7_name").IsUnique();
2848
}
2949
}
3050
}

src/InformaticsGateway/Services/Export/ExportServiceBase.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ public async Task StopAsync(CancellationToken cancellationToken)
134134
_cancellationTokenSource.Cancel();
135135
_logger.ServiceStopping(ServiceName);
136136
Status = ServiceStatus.Stopped;
137+
#pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods
137138
await Task.Delay(250).ConfigureAwait(false);
139+
#pragma warning restore CA2016 // Forward the 'CancellationToken' parameter to methods
138140
_cancellationTokenSource.Dispose();
139141
return;
140142
}
@@ -459,12 +461,9 @@ private async Task<DestinationApplicationEntity> LookupDestinationAsync(string d
459461
var repository = scope.ServiceProvider.GetRequiredService<IDestinationApplicationEntityRepository>();
460462
var destination = await repository.FindByNameAsync(destinationName, cancellationToken).ConfigureAwait(false);
461463

462-
if (destination is null)
463-
{
464-
throw new ConfigurationException($"Specified destination '{destinationName}' does not exist.");
465-
}
466-
467-
return destination;
464+
return destination is null
465+
? throw new ConfigurationException($"Specified destination '{destinationName}' does not exist.")
466+
: destination;
468467
}
469468

470469
protected virtual async Task<DestinationApplicationEntity?> GetDestination(ExportRequestDataMessage exportRequestData, string destinationName, CancellationToken cancellationToken)

src/InformaticsGateway/Services/Export/Hl7ExportService.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ protected override async Task HandleDesination(ExportRequestDataMessage exportRe
8989
{
9090
Guard.Against.Null(exportRequestData, nameof(exportRequestData));
9191

92-
var manualResetEvent = new ManualResetEvent(false);
9392
var destination = await GetHL7Destination(exportRequestData, destinationName, cancellationToken).ConfigureAwait(false);
9493
if (destination is null)
9594
{
@@ -98,7 +97,7 @@ protected override async Task HandleDesination(ExportRequestDataMessage exportRe
9897

9998
try
10099
{
101-
await ExecuteHl7Export(exportRequestData, manualResetEvent, destination!, cancellationToken).ConfigureAwait(false);
100+
await ExecuteHl7Export(exportRequestData, destination!, cancellationToken).ConfigureAwait(false);
102101
}
103102
catch (Exception ex)
104103
{
@@ -108,7 +107,6 @@ protected override async Task HandleDesination(ExportRequestDataMessage exportRe
108107

109108
private async Task ExecuteHl7Export(
110109
ExportRequestDataMessage exportRequestData,
111-
ManualResetEvent manualResetEvent,
112110
HL7DestinationEntity destination,
113111
CancellationToken cancellationToken) => await Policy
114112
.Handle<Exception>()
@@ -139,12 +137,9 @@ private async Task<HL7DestinationEntity> LookupDestinationAsync(string destinati
139137
var repository = scope.ServiceProvider.GetRequiredService<IHL7DestinationEntityRepository>();
140138
var destination = await repository.FindByNameAsync(destinationName, cancellationToken).ConfigureAwait(false);
141139

142-
if (destination is null)
143-
{
144-
throw new ConfigurationException($"Specified destination '{destinationName}' does not exist.");
145-
}
146-
147-
return destination;
140+
return destination is null
141+
? throw new ConfigurationException($"Specified destination '{destinationName}' does not exist.")
142+
: destination;
148143
}
149144

150145
private async Task<HL7DestinationEntity?> GetHL7Destination(ExportRequestDataMessage exportRequestData, string destinationName, CancellationToken cancellationToken)
@@ -160,9 +155,9 @@ private async Task<HL7DestinationEntity> LookupDestinationAsync(string destinati
160155
}
161156
}
162157

163-
protected override async Task<ExportRequestDataMessage> ExecuteOutputDataEngineCallback(ExportRequestDataMessage exportDataRequest)
158+
protected override Task<ExportRequestDataMessage> ExecuteOutputDataEngineCallback(ExportRequestDataMessage exportDataRequest)
164159
{
165-
return exportDataRequest;
160+
return Task.FromResult(exportDataRequest);
166161
}
167162
}
168163
}

src/InformaticsGateway/Services/HealthLevel7/IMllpClientFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace Monai.Deploy.InformaticsGateway.Services.HealthLevel7
2222
{
2323
internal interface IMllpClientFactory
2424
{
25-
IMllpClient CreateClient(ITcpClientAdapter client, Hl7Configuration configurations, IMllpExtract mIIpExtract, ILogger<MllpClient> logger);
25+
IMllpClient CreateClient(ITcpClientAdapter client, Hl7Configuration configurations, ILogger<MllpClient> logger);
2626
}
2727

2828
internal class MllpClientFactory : IMllpClientFactory
2929
{
30-
public IMllpClient CreateClient(ITcpClientAdapter client, Hl7Configuration configurations, IMllpExtract mIIpExtract, ILogger<MllpClient> logger)
31-
=> new MllpClient(client, configurations, mIIpExtract, logger);
30+
public IMllpClient CreateClient(ITcpClientAdapter client, Hl7Configuration configurations, ILogger<MllpClient> logger)
31+
=> new MllpClient(client, configurations, logger);
3232
}
3333
}

src/InformaticsGateway/Services/HealthLevel7/MllpClient.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ internal sealed class MllpClient : IMllpClient
3838
private readonly List<Exception> _exceptions;
3939
private readonly List<Message> _messages;
4040
private readonly IDisposable _loggerScope;
41-
private readonly IMllpExtract _mIIpExtract;
4241
private bool _disposedValue;
4342

4443
public Guid ClientId { get; }
@@ -48,12 +47,11 @@ public string ClientIp
4847
get { return _client.RemoteEndPoint.ToString() ?? string.Empty; }
4948
}
5049

51-
public MllpClient(ITcpClientAdapter client, Hl7Configuration configurations, IMllpExtract mIIpExtract, ILogger<MllpClient> logger)
50+
public MllpClient(ITcpClientAdapter client, Hl7Configuration configurations, ILogger<MllpClient> logger)
5251
{
5352
_client = client ?? throw new ArgumentNullException(nameof(client));
5453
_configurations = configurations ?? throw new ArgumentNullException(nameof(configurations));
5554
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
56-
_mIIpExtract = mIIpExtract ?? throw new ArgumentNullException(nameof(_mIIpExtract));
5755

5856
ClientId = Guid.NewGuid();
5957
_exceptions = new List<Exception>();
@@ -112,8 +110,6 @@ private async Task<IList<Message>> ReceiveData(INetworkStream clientStream, Canc
112110
break;
113111
}
114112

115-
linkedCancellationTokenSource.Dispose();
116-
117113
data += Encoding.UTF8.GetString(messageBuffer.ToArray());
118114

119115
do
@@ -147,6 +143,7 @@ private async Task<IList<Message>> ReceiveData(INetworkStream clientStream, Canc
147143
}
148144
} while (true);
149145
}
146+
linkedCancellationTokenSource.Dispose();
150147
return messages;
151148
}
152149

src/InformaticsGateway/Services/HealthLevel7/MllpExtract.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public async Task<Message> ExtractInfo(Hl7FileStorageMetadata meta, Message mess
6565
}
6666
// extract data for the given fields
6767
// Use Id to get record from Db
68-
var details = await GetExtAppDetails(configItem, message);
68+
var details = await GetExtAppDetails(configItem, message).ConfigureAwait(false);
6969

7070
if (details is null)
7171
{
@@ -101,9 +101,9 @@ public async Task<Message> ExtractInfo(Hl7FileStorageMetadata meta, Message mess
101101
switch (type)
102102
{
103103
case DataLinkType.PatientId:
104-
return await _externalAppDetailsRepository.GetByPatientIdOutboundAsync(tagId, new CancellationToken());
104+
return await _externalAppDetailsRepository.GetByPatientIdOutboundAsync(tagId, new CancellationToken()).ConfigureAwait(false); ;
105105
case DataLinkType.StudyInstanceUid:
106-
return await _externalAppDetailsRepository.GetByStudyIdOutboundAsync(tagId, new CancellationToken());
106+
return await _externalAppDetailsRepository.GetByStudyIdOutboundAsync(tagId, new CancellationToken()).ConfigureAwait(false); ;
107107
default:
108108
break;
109109
}
@@ -115,7 +115,6 @@ public async Task<Message> ExtractInfo(Hl7FileStorageMetadata meta, Message mess
115115
{
116116
foreach (var item in config)
117117
{
118-
var t = message.GetValue(item.SendingId.Key);
119118
if (item.SendingId.Value == message.GetValue(item.SendingId.Key))
120119
{
121120
return item;

src/InformaticsGateway/Services/HealthLevel7/MllpService.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
using System.Threading;
2525
using System.Threading.Tasks;
2626
using Ardalis.GuardClauses;
27-
using CommunityToolkit.HighPerformance.Buffers;
28-
using FellowOakDicom.Network;
2927
using HL7.Dotnetcore;
3028
using Microsoft.Extensions.DependencyInjection;
3129
using Microsoft.Extensions.Hosting;
@@ -139,7 +137,7 @@ private async Task BackgroundProcessing(CancellationToken cancellationToken)
139137
continue;
140138
}
141139

142-
mllpClient = _mllpClientFactory.CreateClient(client, _configuration.Value.Hl7, _mIIpExtract, _logginFactory.CreateLogger<MllpClient>());
140+
mllpClient = _mllpClientFactory.CreateClient(client, _configuration.Value.Hl7, _logginFactory.CreateLogger<MllpClient>());
143141
_ = mllpClient.Start(OnDisconnect, cancellationToken);
144142
_activeTasks.TryAdd(mllpClient.ClientId, mllpClient);
145143
}
@@ -229,12 +227,9 @@ public async Task SendMllp(IPAddress address, int port, string hl7Message, Cance
229227
{
230228
try
231229
{
232-
using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
233-
linkedCancellationTokenSource.CancelAfter(_configuration.Value.Hl7.ClientTimeoutMilliseconds);
234-
235230
var body = $"{Resources.AsciiVT}{hl7Message}{Resources.AsciiFS}{Resources.AcsiiCR}";
236231
var sendMessageByteBuffer = Encoding.UTF8.GetBytes(body);
237-
await WriteMessage(sendMessageByteBuffer, address, port, linkedCancellationTokenSource.Token).ConfigureAwait(false);
232+
await WriteMessage(sendMessageByteBuffer, address, port).ConfigureAwait(false);
238233
}
239234
catch (ArgumentOutOfRangeException)
240235
{
@@ -248,7 +243,7 @@ public async Task SendMllp(IPAddress address, int port, string hl7Message, Cance
248243
}
249244
}
250245

251-
private async Task WriteMessage(byte[] sendMessageByteBuffer, IPAddress address, int port, CancellationToken linkedCancellationToken)
246+
private async Task WriteMessage(byte[] sendMessageByteBuffer, IPAddress address, int port)
252247
{
253248

254249
using var tcpClient = new TcpClient();

src/InformaticsGateway/Test/Repositories/MonaiServiceLocatorTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
using System;
18-
using Microsoft.AspNetCore.Http.Features;
1918
using Monai.Deploy.InformaticsGateway.Api.Rest;
2019
using Monai.Deploy.InformaticsGateway.Repositories;
2120
using Monai.Deploy.InformaticsGateway.Services.Common;

src/InformaticsGateway/Test/Services/HealthLevel7/MllpClientTest.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Text;
2121
using System.Threading;
2222
using System.Threading.Tasks;
23-
using FellowOakDicom;
2423
using HL7.Dotnetcore;
2524
using Microsoft.Extensions.Logging;
2625
using Monai.Deploy.InformaticsGateway.Configuration;
@@ -54,12 +53,12 @@ public MllpClientTest()
5453
[Fact(DisplayName = "Constructor")]
5554
public void Constructor()
5655
{
57-
Assert.Throws<ArgumentNullException>(() => new MllpClient(null, null, null, null));
58-
Assert.Throws<ArgumentNullException>(() => new MllpClient(_tcpClient.Object, null, null, null));
59-
Assert.Throws<ArgumentNullException>(() => new MllpClient(_tcpClient.Object, _config, null, null));
60-
Assert.Throws<ArgumentNullException>(() => new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, null));
56+
Assert.Throws<ArgumentNullException>(() => new MllpClient(null, null, null));
57+
Assert.Throws<ArgumentNullException>(() => new MllpClient(_tcpClient.Object, null, null));
58+
Assert.Throws<ArgumentNullException>(() => new MllpClient(_tcpClient.Object, _config, null));
59+
Assert.Throws<ArgumentNullException>(() => new MllpClient(_tcpClient.Object, _config, null));
6160

62-
new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
61+
new MllpClient(_tcpClient.Object, _config, _logger.Object);
6362
}
6463

6564
[Fact(DisplayName = "ReceiveData - records exception thrown by network stream")]
@@ -70,7 +69,7 @@ public async Task ReceiveData_ExceptionReadingStream()
7069
.ThrowsAsync(new Exception("error"));
7170

7271
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
73-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
72+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
7473

7574
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
7675
{
@@ -93,7 +92,7 @@ public async Task ReceiveData_ZeroByte()
9392
.ReturnsAsync(0);
9493

9594
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
96-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
95+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
9796

9897
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
9998
{
@@ -128,7 +127,7 @@ public async Task ReceiveData_InvalidMessage()
128127
});
129128

130129
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
131-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
130+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
132131

133132
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
134133
{
@@ -169,7 +168,7 @@ public async Task ReceiveData_DisabledAck()
169168
});
170169

171170
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
172-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
171+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
173172

174173
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
175174
{
@@ -210,7 +209,7 @@ public async Task ReceiveData_NeverSendAck()
210209
});
211210

212211
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
213-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
212+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
214213

215214
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
216215
{
@@ -251,7 +250,7 @@ public async Task ReceiveData_ExceptionSendingAck()
251250
});
252251

253252
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
254-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
253+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
255254

256255
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
257256
{
@@ -291,7 +290,7 @@ public async Task ReceiveData_CompleteWorkflow()
291290
});
292291

293292
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
294-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
293+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
295294

296295
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
297296
{
@@ -337,7 +336,7 @@ public async Task ReceiveData_CompleteWorkflow_WithMultipleMessages()
337336
});
338337

339338
_tcpClient.Setup(p => p.GetStream()).Returns(stream.Object);
340-
var client = new MllpClient(_tcpClient.Object, _config, _mIIpExtract.Object, _logger.Object);
339+
var client = new MllpClient(_tcpClient.Object, _config, _logger.Object);
341340

342341
var action = new Func<IMllpClient, MllpClientResult, Task>(async (client, results) =>
343342
{

0 commit comments

Comments
 (0)