Files
HawkeyeVision/VisionBuilder.UI.IOCommander/Modules/IOCommanderModule.cs
2025-08-19 14:27:36 +02:00

74 lines
2.1 KiB
C#

using System.Collections.Concurrent;
using Serilog;
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()
{
if(!_settings.Enabled)return;
try
{
_ioCommander = _factory.Create();
_ioCommander.OnPinChanged += OnPinChanged;
}
catch (Exception e)
{
Log.Warning("IOCommander did non load: {e}",e);
_ioCommander= null;
}
if (_settings.ShowDebugView)_debugViewService.ShowDebugView();
}
ConcurrentDictionary<int, bool> _pinStates = new ConcurrentDictionary<int, bool>();
public event Action<int, bool>? 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;
}
}