using System.Collections.Concurrent; using VisionBuilder.UI.Common; using VisionBuilder.UI.Common.Commands; using VisionBuilder.UI.Common.RecipeProcessing; using VisionBuilder.UI.Common.ViewModel; using VisionBuilder.UI.IOCommander.Interfaces; using VisionBuilder.UI.IOCommander.Settings; namespace VisionBuilder.UI.IOCommander.Modules; public class IOCommanderModule:IVisionBuilderModule { private readonly IOCommanderSettings _settings; private readonly IIOCommanderFactory _factory; private readonly IIOCommanderDebugViewService _debugViewService; private IIOCommander? _ioCommander; public IOCommanderModule(IOCommanderSettings settings, IIOCommanderFactory factory, IIOCommanderDebugViewService debugViewService) { _settings = settings; _factory = factory; _debugViewService = debugViewService; } public void InitializeModule() { _ioCommander = _factory.Create(); _ioCommander.OnPinChanged += OnPinChanged; if (_settings.ShowDebugView)_debugViewService.ShowDebugView(); } ConcurrentDictionary _pinStates = new ConcurrentDictionary(); public event Action? OnPinChangedEvent = delegate { }; private void OnPinChanged(int arg1, bool arg2) { _pinStates.TryAdd(arg1, arg2); _debugViewService.SetInputPinStatus(arg1, arg2); OnPinChangedEvent?.Invoke(arg1, arg2); } public void SetOutput(int pin, bool value) { if (_ioCommander != null) { _ioCommander.SetPins(pin, value); } _debugViewService.SetOutputPinStatus(pin,value); } public bool GetInput(int pin) { if (_ioCommander == null) return false; var found=_pinStates.TryGetValue(pin, out var value); if (found) { return value; } return false; } }