tbf/TBF/Rig/Output/DataStorage/UniDataStorageWriter/Writer.cs

224 lines
7.4 KiB
C#

using Common;
using Config.Entities;
using System;
using System.Collections.Generic;
using TBF.Rig.Generic;
using TBF.Rig.Output.DataStorage.UniDataStorageWriter.Interfaces;
using TBF.Rig.Output.DataStorage.UniDataStorageWriter.Writers;
using TBF.Rig.Scales.MettlerToledo;
using static TBF.Rig.Output.DataStorage.UniDataStorageWriter.Types;
using static TBF.Rig.Output.DataStorage.UniDataStorageWriter.WriterCfg;
namespace TBF.Rig.Output.DataStorage.UniDataStorageWriter
{
/// <summary>
/// Main component that selects appropriate data writer based on configuration.
/// Acts as a dispatcher between different storage implementations.
/// </summary>
public class Writer : IComponent
{
private readonly WriterCfg cfg;
public Writer(WriterCfg cfg)
{
this.cfg = cfg ?? throw new ArgumentNullException(nameof(cfg));
}
public string Name { get { return cfg.Name; } }
public string ClassName { get { return cfg.ClassName; } }
public string ParentName { get { return cfg.ParentName; } }
public DebugMode DebugLevel { get { return cfg.DebugLevel; } }
public LogLevel LogLevel { get { return cfg.LogLevel; } }
public IComponentCfg Cfg { get { return cfg; } }
public IList<MeasurementCorrection> Corrections
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public IList<Uncertainty> Uncertainties
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
/// <summary>
/// Executes write operation using the configured storage writer.
/// </summary>
public WriterDiagnosticResult WriteDataToStorage(DataWriteRequest request)
{
IDataStorageWriter writer = CreateWriter();
return writer.WriteData(request);
}
/// <summary>
/// Tests configured storage source using the selected writer.
/// </summary>
public WriterDiagnosticResult TestSource(bool validateOnly)
{
try
{
IDataStorageWriter writer = CreateWriter();
WriterDiagnosticResult capabilityResult = ValidateCapabilities(writer, null);
if (!capabilityResult.Success)
return capabilityResult;
return writer.TestSource(validateOnly);
}
catch (Exception ex)
{
return new WriterDiagnosticResult
{
Success = false,
Message = ex.Message
};
}
}
/// <summary>
/// Creates appropriate writer implementation based on selected storage type.
/// </summary>
private IDataStorageWriter CreateWriter()
{
string storageType = (cfg.DataStorageType ?? string.Empty).Trim();
switch (storageType)
{
case StorageTypes.RestApi:
return new RestApiWriter(cfg);
case StorageTypes.LocalDatabase:
case StorageTypes.RemoteDatabase:
return new DatabaseWriter(cfg);
case StorageTypes.LocalFile:
case StorageTypes.RemoteFile:
switch ((cfg.TechnologyType ?? string.Empty).Trim())
{
case TechnologyTypes.Csv:
return new CsvWriter(cfg);
case TechnologyTypes.Json:
return new JsonWriter(cfg);
default:
throw new NotSupportedException(
string.Format("Unsupported file technology type: '{0}'", cfg.TechnologyType));
}
default:
throw new NotSupportedException(
string.Format("Unsupported DataStorageType: '{0}'", cfg.DataStorageType));
}
}
public WriterDiagnosticResult SetData(DataWriteRequest request)
{
try
{
if (request == null)
throw new ArgumentNullException(nameof(request));
IDataStorageWriter writer = CreateWriter();
WriterDiagnosticResult capabilityResult = ValidateCapabilities(writer, request);
if (!capabilityResult.Success)
return capabilityResult;
WriterDiagnosticResult sourceResult = writer.TestSource(false);
if (!sourceResult.Success)
return sourceResult;
return writer.WriteData(request);
}
catch (Exception ex)
{
return new WriterDiagnosticResult
{
Success = false,
Message = ex.Message
};
}
}
public void Initialize()
{
// throw new NotImplementedException();
}
public void StartChangeHandler()
{
//throw new NotImplementedException();
}
public void StopChangeHandler()
{
// throw new NotImplementedException();
}
private WriterDiagnosticResult ValidateCapabilities(IDataStorageWriter writer, DataWriteRequest request)
{
if (writer == null)
{
return new WriterDiagnosticResult
{
Success = false,
Message = "Writer instance is not available."
};
}
if (writer.Capabilities == null)
{
return new WriterDiagnosticResult
{
Success = false,
Message = "Writer capabilities are not defined."
};
}
string storageType = (cfg.DataStorageType ?? string.Empty).Trim();
string technologyType = (cfg.TechnologyType ?? string.Empty).Trim();
if (!writer.Capabilities.SupportsStorageType(storageType))
{
return new WriterDiagnosticResult
{
Success = false,
Message = string.Format("Storage type '{0}' is not supported by writer '{1}'.", storageType, writer.GetType().Name)
};
}
if (!string.IsNullOrWhiteSpace(technologyType) &&
!writer.Capabilities.SupportsTechnologyType(technologyType))
{
return new WriterDiagnosticResult
{
Success = false,
Message = string.Format("Technology type '{0}' is not supported by writer '{1}'.", technologyType, writer.GetType().Name)
};
}
if (request != null && !writer.Capabilities.SupportsWriteMode(request.Mode))
{
return new WriterDiagnosticResult
{
Success = false,
Message = string.Format("Write mode '{0}' is not supported by writer '{1}'.", request.Mode, writer.GetType().Name)
};
}
return new WriterDiagnosticResult
{
Success = true,
Message = "Writer capabilities validated successfully."
};
}
}
}