tbf/TBFTests/Rig/RegisterReaders/GenesisRegReader/implementations/FakeSerialDriver.cs
Michal Buzik a5a68ec457 Refactor Genesis serial communication handling (ISerialDriver interface compatible with SerialPort):
- Replace method-based `IsOpen()` checks with property-based `IsOpen` usage in `ISerialDriver` and related classes.
- Simplify `Open()` and add `Close()` methods to unify connection management.
- Introduce `InitializeThreeChannelState()` helper in `GenesisSmartReaderTest` for multi-channel state initialization.
- Adjust calculations in `CalculateTimeByChannels()` and `CalculateVolumeByChannels()` for clarity.
- Add new tests for multi-channel processing and validation of updated telegram handling logic.
2026-03-24 10:04:30 +01:00

89 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using TBF.Rig.RegisterReaders.GenesisRegReader.communication.Utils;
namespace TBFTests.Rig.RegisterReaders.GenesisRegReader.implementations
{
public sealed class FakeSerialDriver : ISerialDriver
{
private readonly Queue<string> _lines = new Queue<string>();
private bool _isOpen;
public int OpenCalls { get; private set; }
public int CloseCalls { get; private set; }
public int DiscardInCalls { get; private set; }
public int DiscardOutCalls { get; private set; }
public Encoding Encoding { get; set; } = Encoding.ASCII;
public int BytesToRead => _lines.Count > 0 ? 1 : 0;
public int BytesToWrite => 0;
public void EnqueueLine(string line) => _lines.Enqueue(line);
public bool IsOpen => _isOpen;
public void Open()
{
OpenCalls++;
_isOpen = true;
}
public void Close()
{
CloseConnection();
}
public void CloseConnection()
{
CloseCalls++;
_isOpen = false;
}
public void DiscardInBuffer()
{
DiscardInCalls++;
_lines.Clear();
}
public void DiscardOutBuffer()
{
DiscardOutCalls++;
}
public string ReadLine()
{
if (!_isOpen)
throw new InvalidOperationException("Port not open.");
if (_lines.Count == 0)
throw new TimeoutException();
return _lines.Dequeue();
}
public string ReadExisting()
{
if (!_isOpen)
throw new InvalidOperationException("Port not open.");
if (_lines.Count == 0)
return string.Empty;
return _lines.Dequeue();
}
public byte[] SendAndWait(byte[] request, int timeout)
{
return Array.Empty<byte>();
}
public void Dispose()
{
_isOpen = false;
}
}
}