187 lines
7.8 KiB
C#
187 lines
7.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using MySql.Data.MySqlClient;
|
|
using FellowOakDicom;
|
|
using FellowOakDicom.Network;
|
|
using FellowOakDicom.Network.Client;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace DicomMigratorApp
|
|
{
|
|
class Program
|
|
{
|
|
private static IConfiguration Configuration;
|
|
private static ILogger<Program> _logger;
|
|
|
|
private static string connectionString;
|
|
private static string targetIP;
|
|
private static string senderAET;
|
|
private static string targetAET;
|
|
private static string folderLocation;
|
|
private static int targetPort;
|
|
|
|
static async Task Main(string[] args)
|
|
{
|
|
var serviceProvider = new ServiceCollection()
|
|
.AddLogging(config =>
|
|
{
|
|
config.AddConsole();
|
|
config.SetMinimumLevel(LogLevel.Information);
|
|
})
|
|
.BuildServiceProvider();
|
|
|
|
_logger = serviceProvider.GetService<ILoggerFactory>()
|
|
.CreateLogger<Program>();
|
|
|
|
_logger.LogInformation("Application started");
|
|
|
|
connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection")
|
|
?? "Server=127.0.0.1;Port=3306;Database=dms_db;User Id=root;Password=Rootmatrix23@;";
|
|
targetIP = Environment.GetEnvironmentVariable("TARGET_IP") ?? "127.0.0.1";
|
|
senderAET = Environment.GetEnvironmentVariable("SENDER_AET") ?? "MYAE";
|
|
targetAET = Environment.GetEnvironmentVariable("TARGET_AET") ?? "ORTHANC";
|
|
folderLocation = Environment.GetEnvironmentVariable("FOLDER_LOCATION") ?? "D:\\dicom_images\\";
|
|
string targetPortStr = Environment.GetEnvironmentVariable("TARGET_PORT") ?? "4242";
|
|
|
|
if (!int.TryParse(targetPortStr, out targetPort))
|
|
{
|
|
_logger.LogError("TargetServer__Port environment variable is not a valid integer.");
|
|
return;
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
var migrationStudy = await GetMigrationStudy(connectionString);
|
|
if (migrationStudy == null)
|
|
{
|
|
await Task.Delay(5000);
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
var folderPath = Path.Combine(folderLocation, migrationStudy.StudyInstanceUID);
|
|
if (!Directory.Exists(folderPath))
|
|
{
|
|
_logger.LogError($"Study folder not found for studyInstanceUID {migrationStudy.StudyInstanceUID}");
|
|
await UpdateSourceCFind(connectionString, migrationStudy.StudyInstanceUID, "MIGRATION_SIUID_FOLDER_NOT_PRESENT");
|
|
continue;
|
|
}
|
|
|
|
await MigrateStudy(migrationStudy.StudyInstanceUID, folderPath, targetIP, targetPort, senderAET, targetAET);
|
|
await UpdateSourceCFind(connectionString, migrationStudy.StudyInstanceUID, "MIGRATION_COMPLETE");
|
|
}
|
|
catch (DicomAssociationRejectedException ex)
|
|
{
|
|
_logger.LogError(ex, $"Association rejected for studyInstanceUID {migrationStudy.StudyInstanceUID}");
|
|
await UpdateSourceCFind(connectionString, migrationStudy.StudyInstanceUID, "MIGRATION_ERRORED");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"An error occurred while retrieving or saving studyInstanceUID {migrationStudy.StudyInstanceUID}");
|
|
await UpdateSourceCFind(connectionString, migrationStudy.StudyInstanceUID, "MIGRATION_ERRORED");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "An error occurred in the main processing loop");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static async Task<SourceStudy> GetMigrationStudy(string connectionString)
|
|
{
|
|
var qrStudy = new SourceStudy();
|
|
|
|
try
|
|
{
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
{
|
|
await connection.OpenAsync();
|
|
var command = new MySqlCommand("SELECT study_instance_uid,patient_id,status FROM source_cfind WHERE status='QR_COMPLETE' and (schedule_date IS NOT NULL AND LEFT(schedule_date, 8) >= DATE_FORMAT(CURDATE(), '%Y%m%d')) LIMIT 1", connection);
|
|
var reader = await command.ExecuteReaderAsync();
|
|
|
|
while (await reader.ReadAsync())
|
|
{
|
|
qrStudy = new SourceStudy
|
|
{
|
|
StudyInstanceUID = reader.IsDBNull(0) ? null : reader.GetString(0),
|
|
PatientID = reader.IsDBNull(1) ? null : reader.GetString(1),
|
|
Status = reader.IsDBNull(2) ? null : reader.GetString(2)
|
|
};
|
|
}
|
|
|
|
reader.Close();
|
|
}
|
|
if (qrStudy.StudyInstanceUID != null)
|
|
{
|
|
_logger.LogInformation("Picked study to be migrated from the database");
|
|
await UpdateSourceCFind(connectionString, qrStudy.StudyInstanceUID, "MIGRATION_INPROGRESS");
|
|
}
|
|
else
|
|
{
|
|
qrStudy = null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error retrieving study to be migrated from the database");
|
|
}
|
|
|
|
return qrStudy;
|
|
}
|
|
|
|
private static async Task UpdateSourceCFind(string connectionString, string study_instance_uid, string updateStatus)
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
{
|
|
await connection.OpenAsync();
|
|
var command = new MySqlCommand("UPDATE source_cfind SET status = @Status WHERE study_instance_uid = @StudyInstanceUID", connection);
|
|
|
|
command.Parameters.AddWithValue("@StudyInstanceUID", study_instance_uid);
|
|
command.Parameters.AddWithValue("@Status", updateStatus);
|
|
|
|
await command.ExecuteNonQueryAsync();
|
|
_logger.LogInformation($"Updated source C-FIND record for study instance uid {study_instance_uid}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"Error updating source study record for study {study_instance_uid}");
|
|
}
|
|
}
|
|
|
|
private static async Task MigrateStudy(string studyInstanceUID, string folderPath, string targetIP, int targetPort, string senderAET, string targetAET)
|
|
{
|
|
var client = DicomClientFactory.Create(targetIP, targetPort, false, senderAET, targetAET);
|
|
|
|
// Gather all DICOM files in the directory and subdirectories
|
|
var files = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
|
|
foreach (var file in files)
|
|
{
|
|
var dicomFile = await DicomFile.OpenAsync(file);
|
|
await client.AddRequestAsync(new DicomCStoreRequest(dicomFile));
|
|
}
|
|
|
|
await client.SendAsync();
|
|
|
|
_logger.LogInformation($"Migrated studyInstanceUID {studyInstanceUID} to target AET {targetAET}");
|
|
}
|
|
}
|
|
|
|
class SourceStudy
|
|
{
|
|
public string PatientID { get; set; }
|
|
public string StudyInstanceUID { get; set; }
|
|
public string Status { get; set; }
|
|
}
|
|
}
|