리크 테스트 gui
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.8 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using leak_test_project.Infrastructure;
using leak_test_project.Models;
namespace leak_test_project.Services
{
/// <summary>
/// I/O 보드 서비스. IDioBoard를 통해 실제 DIO 상태를 제공합니다.
/// 실제 DIO 보드를 주입받아 동작합니다.
/// </summary>
public class IoBoardService
{
private readonly IDioBoard _dioBoard;
public List<InOutItem> Inputs { get; private set; } = new List<InOutItem>();
public List<InOutItem> Outputs { get; private set; } = new List<InOutItem>();
public IoBoardService(IDioBoard dioBoard)
{
_dioBoard = dioBoard;
LoadFromDioBoard();
}
/// <summary>
/// DIO 보드에서 입출력 포인트를 가져와 InOutItem 목록을 구성합니다.
/// </summary>
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++;
}
}
/// <summary>
/// 현재 DIO 상태를 갱신합니다.
/// </summary>
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;
}
/// <summary>
/// 지정된 출력 포인트의 값을 설정합니다.
/// </summary>
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()
{
// 필요시 추가 정리 작업
}
}
}