/// /// 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 { /// /// TBF bridge component for integration with the sibling GCI project. /// The component can be linked to UniDataStorage reader and writer components. /// 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; /// /// Linked UniDataStorage reader component. /// readonly UdsReaderType reader; /// /// Linked UniDataStorage writer component. /// readonly UdsWriterType writer; /// /// Placeholder for GCI GUI entry point. /// Replace object with real GCI MainForm type later. /// object gciMainForm; /// /// Placeholder for GCI external/public interface. /// Replace object with real GCI interface type later. /// 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 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); } /// /// Initializes access to GCI GUI. /// Replace placeholder implementation with real MainForm creation. /// 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); } } /// /// Initializes access to GCI external/public interface. /// Replace placeholder implementation with real interface creation. /// 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); } } /// /// Shows GCI GUI if GUI access is enabled. /// public void ShowGui() { if (!gciBridgeCfg.EnableGuiAccess) return; if (!IsGuiInitialized) { TryInitializeGui(); } /// TODO: /// Replace with real MainForm.Show() call. log.InfoFormat("{0}: ShowGui invoked.", Name); } /// /// Hides GCI GUI if GUI access is enabled. /// public void HideGui() { if (!gciBridgeCfg.EnableGuiAccess) return; if (!IsGuiInitialized) return; /// TODO: /// Replace with real MainForm.Hide() call. log.InfoFormat("{0}: HideGui invoked.", Name); } /// /// Connects the external GCI interface. /// public void ConnectExternal() { if (!gciBridgeCfg.EnableExternalAccess) return; if (!IsExternalInitialized) { TryInitializeExternalInterface(); } /// TODO: /// Replace with real external interface connect call. log.InfoFormat("{0}: ConnectExternal invoked.", Name); } /// /// Disconnects the external GCI interface. /// public void DisconnectExternal() { if (!gciBridgeCfg.EnableExternalAccess) return; if (!IsExternalInitialized) return; /// TODO: /// Replace with real external interface disconnect call. log.InfoFormat("{0}: DisconnectExternal invoked.", Name); } /// /// Returns linked reader component. /// public UdsReaderType GetReader() { return reader; } /// /// Returns linked writer component. /// public UdsWriterType GetWriter() { return writer; } } }