using MaxwellFramework.Core.Attributes; using System; using System.Collections.Generic; namespace MainShell.Hardware.Acs { [Singleton] public sealed class AcsAddressRepository { private readonly object _syncRoot = new object(); private readonly Dictionary _bondingCommunicationDataCache = new Dictionary(StringComparer.Ordinal); private readonly Dictionary _bondingLogDataCache = new Dictionary(StringComparer.Ordinal); private readonly Lazy _commonData = new Lazy(() => new AcsCommonAdress()); private readonly Lazy _interferometerCompensationData = new Lazy(() => new AcsInterferometerCompensationAdress()); private readonly Lazy _homeOffsetData = new Lazy(() => new AcsHomeOffsetAdress()); private readonly Lazy _homeSettingData = new Lazy(() => new AcsHomeSettingAdress()); private readonly Lazy _newBondingData = new Lazy(() => new AcsBondingAdress()); public AcsBondingCommunicationAdress GetBondingCommunicationData(string addressSuffix) { string normalizedSuffix = NormalizeAddressSuffix(addressSuffix); AcsBondingCommunicationAdress communicationData; if (_bondingCommunicationDataCache.TryGetValue(normalizedSuffix, out communicationData)) { return communicationData; } lock (_syncRoot) { if (_bondingCommunicationDataCache.TryGetValue(normalizedSuffix, out communicationData)) { return communicationData; } communicationData = new AcsBondingCommunicationAdress(normalizedSuffix); _bondingCommunicationDataCache[normalizedSuffix] = communicationData; return communicationData; } } public AcsBondingLogAdress GetBondingLogData(string addressSuffix) { string normalizedSuffix = NormalizeAddressSuffix(addressSuffix); AcsBondingLogAdress logData; if (_bondingLogDataCache.TryGetValue(normalizedSuffix, out logData)) { return logData; } lock (_syncRoot) { if (_bondingLogDataCache.TryGetValue(normalizedSuffix, out logData)) { return logData; } logData = new AcsBondingLogAdress(normalizedSuffix); _bondingLogDataCache[normalizedSuffix] = logData; return logData; } } public AcsInterferometerCompensationAdress GetInterferometerCompensationData() { return _interferometerCompensationData.Value; } public AcsCommonAdress GetCommonData() { return _commonData.Value; } public AcsHomeOffsetAdress GetHomeOffsetData() { return _homeOffsetData.Value; } public AcsHomeSettingAdress GetHomeSettingData() { return _homeSettingData.Value; } public AcsBondingAdress GetNewBondingData() { return _newBondingData.Value; } private static string NormalizeAddressSuffix(string addressSuffix) { if (addressSuffix == null) { throw new ArgumentNullException(nameof(addressSuffix)); } string normalizedSuffix = addressSuffix.Trim(); if (normalizedSuffix.Length == 0) { throw new ArgumentException("ACS address suffix cannot be empty.", nameof(addressSuffix)); } return normalizedSuffix; } } }