286 lines
8.9 KiB
C#
286 lines
8.9 KiB
C#
using GenesisCordonelInterface.API;
|
|
using log4net;
|
|
///
|
|
/// Copyright (c) 2015-2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using TBF.Rig.Generic;
|
|
using GciGUIType = GenesisCordonelInterface.UI.MainView;
|
|
using GciType = GenesisCordonelInterface.API.InterfaceOutsideToGCI;
|
|
using UdsReaderType = TBF.Rig.Input.DataStorage.UniDataStorageReader.Reader;
|
|
using UdsWriterType = TBF.Rig.Output.DataStorage.UniDataStorageWriter.Writer;
|
|
|
|
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;
|
|
|
|
readonly UdsReaderType reader;
|
|
readonly UdsWriterType writer;
|
|
|
|
GciGUIType gciGUI;
|
|
Form gciGuiHostForm;
|
|
|
|
GciType gciExternalInterface;
|
|
|
|
public bool HasReader { get { return reader != null; } }
|
|
public bool HasWriter { get { return writer != null; } }
|
|
public bool IsGuiInitialized { get { return gciGUI != null; } }
|
|
public bool IsExternalInitialized { get { return gciExternalInterface != null; } }
|
|
public GciType GciExternalInterface { get { return gciExternalInterface; } }
|
|
public GciBridgeCfg GciBridgeCfg { get { return gciBridgeCfg; } }
|
|
|
|
public UdsReaderType GetReader()
|
|
{
|
|
return reader;
|
|
}
|
|
|
|
public UdsWriterType GetWriter()
|
|
{
|
|
return writer;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void TryInitializeGui()
|
|
{
|
|
try
|
|
{
|
|
if (gciGuiHostForm != null && !gciGuiHostForm.IsDisposed)
|
|
return;
|
|
|
|
gciGUI = new GciGUIType();
|
|
gciGUI.Dock = DockStyle.Fill;
|
|
|
|
gciGuiHostForm = new Form();
|
|
gciGuiHostForm.Text = "Genesis Cordonel Interface";
|
|
gciGuiHostForm.Width = 1300;
|
|
gciGuiHostForm.Height = 600;
|
|
gciGuiHostForm.StartPosition = FormStartPosition.CenterScreen;
|
|
|
|
gciGuiHostForm.Controls.Add(gciGUI);
|
|
|
|
log.InfoFormat("{0}: GCI GUI view initialized.", Name);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("Failed to initialize GCI GUI view.", ex);
|
|
}
|
|
}
|
|
|
|
void TryInitializeExternalInterface()
|
|
{
|
|
try
|
|
{
|
|
if (gciExternalInterface != null)
|
|
return;
|
|
|
|
gciExternalInterface = new GciType();
|
|
|
|
log.InfoFormat("{0}: GCI external interface initialized.", Name);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("Failed to initialize GCI external interface.", ex);
|
|
}
|
|
}
|
|
|
|
public void ShowGui()
|
|
{
|
|
if (!gciBridgeCfg.EnableGuiAccess) return;
|
|
|
|
if (gciGuiHostForm == null || gciGuiHostForm.IsDisposed)
|
|
{
|
|
TryInitializeGui();
|
|
}
|
|
|
|
if (gciGuiHostForm == null) return;
|
|
|
|
gciGuiHostForm.Show();
|
|
gciGuiHostForm.BringToFront();
|
|
|
|
log.InfoFormat("{0}: ShowGui invoked.", Name);
|
|
}
|
|
|
|
public void HideGui()
|
|
{
|
|
if (!gciBridgeCfg.EnableGuiAccess) return;
|
|
if (gciGuiHostForm == null || gciGuiHostForm.IsDisposed) return;
|
|
|
|
gciGuiHostForm.Hide();
|
|
|
|
log.InfoFormat("{0}: HideGui invoked.", Name);
|
|
}
|
|
|
|
void EnsureExternalInterface()
|
|
{
|
|
if (!gciBridgeCfg.EnableExternalAccess)
|
|
throw new Exception("GCI external access is disabled.");
|
|
|
|
if (!IsExternalInitialized)
|
|
TryInitializeExternalInterface();
|
|
|
|
if (gciExternalInterface == null)
|
|
throw new Exception("GCI external interface is not initialized.");
|
|
}
|
|
|
|
void EnsureReader()
|
|
{
|
|
if (reader == null)
|
|
throw new Exception("UniDataStorageReader is not linked to GciBridge.");
|
|
}
|
|
|
|
// API:
|
|
#region ======================================= GCI Public Interface =======================================
|
|
|
|
public async Task<GciPublicModels.GciInitSlotResult> InitSlotAsync(GciPublicModels.GciInitSlotRequest request)
|
|
{
|
|
EnsureExternalInterface();
|
|
|
|
if (request == null)
|
|
throw new ArgumentNullException("request");
|
|
|
|
if (request.SlotId <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
GciPublicModels.GciInitSlotResult result =
|
|
await gciExternalInterface.InitSlotAsync(request);
|
|
|
|
log.InfoFormat("{0}: InitSlotAsync invoked. {1}, Result={2}", Name, request, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciPublicModels.GciSlotInfo> GetSlotAsync(int slotId)
|
|
{
|
|
EnsureExternalInterface();
|
|
|
|
if (slotId <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
GciPublicModels.GciSlotInfo result =
|
|
await gciExternalInterface.GetSlotAsync(slotId);
|
|
|
|
log.InfoFormat("{0}: GetSlotAsync({1}) invoked. Result={2}", Name, slotId, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciPublicModels.GciCleanSlotsResult> CleanSlotsAsync()
|
|
{
|
|
EnsureExternalInterface();
|
|
|
|
GciPublicModels.GciCleanSlotsResult result =
|
|
await gciExternalInterface.CleanSlotsAsync();
|
|
|
|
log.InfoFormat("{0}: CleanSlotsAsync invoked.", Name);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciPublicModels.GciGetPcbIdResult> GetPcbIdAsync(
|
|
int slotId,
|
|
CancellationToken token = default)
|
|
{
|
|
EnsureExternalInterface();
|
|
|
|
if (slotId <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await gciExternalInterface.GetPcbIdAsync(slotId, token);
|
|
|
|
log.InfoFormat("{0}: GetPcbIdAsync({1}) invoked. Result={2}", Name, slotId, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciPublicModels.GciConnectResult> ConnectAsync(
|
|
int slotId,
|
|
CancellationToken token = default)
|
|
{
|
|
EnsureExternalInterface();
|
|
|
|
if (slotId <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await gciExternalInterface.ConnectOneSlotAsync(slotId, token);
|
|
|
|
log.InfoFormat("{0}: ConnectAsync({1}) invoked. Result={2}", Name, slotId, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<GciPublicModels.GciDisconnectResult> DisconnectAsync(
|
|
int slotId,
|
|
CancellationToken token = default)
|
|
{
|
|
EnsureExternalInterface();
|
|
|
|
if (slotId <= 0)
|
|
throw new ArgumentException("Invalid slot id.");
|
|
|
|
var result = await gciExternalInterface.DisconnectAsync(slotId, token);
|
|
|
|
log.InfoFormat("{0}: DisconnectAsync({1}) invoked. Result={2}", Name, slotId, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |