tbf/TBF/Rig/BridgeComponents/GciBridge/GciBridge.cs

208 lines
6.5 KiB
C#

///
/// Copyright (c) 2015-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.Rig.Generic;
using UdsReaderType = TBF.Rig.Input.DataStorage.UniDataStorageReader.UniDataStorageReader;
using UdsWriterType = TBF.Rig.Output.DataStorage.UniDataStorageWriter.UniDataStorageWriter;
namespace TBF.Rig.BridgeComponents.GciBridge
{
/// <summary>
/// TBF bridge component for integration with the sibling GCI project.
/// The component can be linked to UniDataStorage reader and writer components.
/// </summary>
public class GciBridge : ComponentBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(GciBridge));
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly GciBridgeCfg gciBridgeCfg;
/// <summary>
/// Linked UniDataStorage reader component.
/// </summary>
readonly UdsReaderType reader;
/// <summary>
/// Linked UniDataStorage writer component.
/// </summary>
readonly UdsWriterType writer;
/// <summary>
/// Placeholder for GCI GUI entry point.
/// Replace object with real GCI MainForm type later.
/// </summary>
object gciMainForm;
/// <summary>
/// Placeholder for GCI external/public interface.
/// Replace object with real GCI interface type later.
/// </summary>
object gciExternalInterface;
public bool HasReader { get { return reader != null; } }
public bool HasWriter { get { return writer != null; } }
public bool IsGuiInitialized { get { return gciMainForm != null; } }
public bool IsExternalInitialized { get { return gciExternalInterface != null; } }
public GciBridge() { }
public GciBridge(IComponentCfg cfg, IList<IComponent> components)
: base(cfg)
{
gciBridgeCfg = cfg as GciBridgeCfg;
if (gciBridgeCfg == null) throw new Exception("Invalid GciBridgeCfg.");
if (!string.IsNullOrEmpty(gciBridgeCfg.ReaderName))
{
reader = TbfComponents.FindComponent(gciBridgeCfg.ReaderName, components) as UdsReaderType;
if (reader == null) throw new Exception("Cannot find reader component '" + gciBridgeCfg.ReaderName + "'");
}
if (!string.IsNullOrEmpty(gciBridgeCfg.WriterName))
{
writer = TbfComponents.FindComponent(gciBridgeCfg.WriterName, components) as UdsWriterType;
if (writer == null) throw new Exception("Cannot find writer component '" + gciBridgeCfg.WriterName + "'");
}
}
public override void Initialize()
{
if (gciBridgeCfg.EnableExternalAccess)
{
TryInitializeExternalInterface();
}
if (gciBridgeCfg.EnableGuiAccess)
{
TryInitializeGui();
if (gciBridgeCfg.ShowGuiOnInitialize)
{
ShowGui();
}
}
log.FatalFormat("{0} initialized: {1}", Name, this);
}
/// <summary>
/// Initializes access to GCI GUI.
/// Replace placeholder implementation with real MainForm creation.
/// </summary>
void TryInitializeGui()
{
try
{
/// TODO:
/// Replace with real GCI MainForm initialization.
gciMainForm = new object();
log.InfoFormat("{0}: GCI GUI initialized.", Name);
}
catch (Exception ex)
{
log.Error("Failed to initialize GCI GUI.", ex);
}
}
/// <summary>
/// Initializes access to GCI external/public interface.
/// Replace placeholder implementation with real interface creation.
/// </summary>
void TryInitializeExternalInterface()
{
try
{
/// TODO:
/// Replace with real GCI external interface initialization.
gciExternalInterface = new object();
log.InfoFormat("{0}: GCI external interface initialized.", Name);
}
catch (Exception ex)
{
log.Error("Failed to initialize GCI external interface.", ex);
}
}
/// <summary>
/// Shows GCI GUI if GUI access is enabled.
/// </summary>
public void ShowGui()
{
if (!gciBridgeCfg.EnableGuiAccess) return;
if (!IsGuiInitialized)
{
TryInitializeGui();
}
/// TODO:
/// Replace with real MainForm.Show() call.
log.InfoFormat("{0}: ShowGui invoked.", Name);
}
/// <summary>
/// Hides GCI GUI if GUI access is enabled.
/// </summary>
public void HideGui()
{
if (!gciBridgeCfg.EnableGuiAccess) return;
if (!IsGuiInitialized) return;
/// TODO:
/// Replace with real MainForm.Hide() call.
log.InfoFormat("{0}: HideGui invoked.", Name);
}
/// <summary>
/// Connects the external GCI interface.
/// </summary>
public void ConnectExternal()
{
if (!gciBridgeCfg.EnableExternalAccess) return;
if (!IsExternalInitialized)
{
TryInitializeExternalInterface();
}
/// TODO:
/// Replace with real external interface connect call.
log.InfoFormat("{0}: ConnectExternal invoked.", Name);
}
/// <summary>
/// Disconnects the external GCI interface.
/// </summary>
public void DisconnectExternal()
{
if (!gciBridgeCfg.EnableExternalAccess) return;
if (!IsExternalInitialized) return;
/// TODO:
/// Replace with real external interface disconnect call.
log.InfoFormat("{0}: DisconnectExternal invoked.", Name);
}
/// <summary>
/// Returns linked reader component.
/// </summary>
public UdsReaderType GetReader()
{
return reader;
}
/// <summary>
/// Returns linked writer component.
/// </summary>
public UdsWriterType GetWriter()
{
return writer;
}
}
}