using System; using System.Collections.Generic; using System.Linq; using leak_test_project.Infrastructure; using leak_test_project.Models; namespace leak_test_project.Services { /// /// I/O 보드 서비스. IDioBoard를 통해 실제 DIO 상태를 제공합니다. /// 실제 DIO 보드를 주입받아 동작합니다. /// public class IoBoardService { private readonly IDioBoard _dioBoard; public List Inputs { get; private set; } = new List(); public List Outputs { get; private set; } = new List(); public IoBoardService(IDioBoard dioBoard) { _dioBoard = dioBoard; LoadFromDioBoard(); } /// /// DIO 보드에서 입출력 포인트를 가져와 InOutItem 목록을 구성합니다. /// private void LoadFromDioBoard() { var inputPoints = _dioBoard.GetInputPoints(); var outputPoints = _dioBoard.GetOutputPoints(); int idx = 0; foreach (var p in inputPoints) { Inputs.Add(new InOutItem { Address = $"I{idx:D3}", Name = p.Name, Description = p.Description, Value = p.Value }); idx++; } idx = 0; foreach (var p in outputPoints) { Outputs.Add(new InOutItem { Address = $"O{idx:D3}", Name = p.Name, Description = p.Description, Value = p.Value }); idx++; } } /// /// 현재 DIO 상태를 갱신합니다. /// public void RefreshState() { var inputPoints = _dioBoard.GetInputPoints(); var outputPoints = _dioBoard.GetOutputPoints(); for (int i = 0; i < Math.Min(Inputs.Count, inputPoints.Count); i++) Inputs[i].Value = inputPoints[i].Value; for (int i = 0; i < Math.Min(Outputs.Count, outputPoints.Count); i++) Outputs[i].Value = outputPoints[i].Value; } /// /// 지정된 출력 포인트의 값을 설정합니다. /// public void SetOutput(string pointName, bool value) { _dioBoard.WriteOutput(pointName, value); var item = Outputs.FirstOrDefault(o => o.Name == pointName); if (item != null) item.Value = value; } public void Stop() { // 필요시 추가 정리 작업 } } }