tbf/TBF/Rig/RegisterReaders/PoseidonCmdStartStop/PoseidonReadCycle.cs

85 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using TBF.Rig;
namespace TBF.Rig.RegisterReaders.PoseidonCmdStartStop
{
public interface IPoseidonReadOperation
{
string Name { get; }
bool IsNotStarted { get; }
bool IsFinished { get; }
bool HasError { get; }
void Start(bool readStart);
Event Run();
}
public sealed class PoseidonReaderOperation : IPoseidonReadOperation
{
private readonly PoseidonReader reader;
public PoseidonReaderOperation(PoseidonReader reader)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));
this.reader = reader;
}
public string Name { get { return reader.Name; } }
public bool IsNotStarted { get { return reader.CurrentOp == PoseidonReader.CurrentPoseidonOp.None; } }
public bool IsFinished { get { return reader.CurrentOp == PoseidonReader.CurrentPoseidonOp.Done || reader.CurrentOp == PoseidonReader.CurrentPoseidonOp.Error; } }
public bool HasError { get { return reader.CurrentOp == PoseidonReader.CurrentPoseidonOp.Error; } }
public void Start(bool readStart) { reader.SetCurrentOp(readStart ? PoseidonReader.CurrentPoseidonOp.ReadDataStream_Start : PoseidonReader.CurrentPoseidonOp.ReadDataStream_End); }
public Event Run() { return reader.Run(); }
}
public static class PoseidonReadCycle
{
public static bool RunIteration(IEnumerable<IPoseidonReadOperation> readers, bool readStart)
{
if (readers == null) return true;
bool allReadersFinished = true;
foreach (IPoseidonReadOperation reader in readers)
{
if (reader == null) continue;
if (reader.IsNotStarted) reader.Start(readStart);
reader.Run();
if (!reader.IsFinished) allReadersFinished = false;
}
return allReadersFinished;
}
}
/// <summary>
/// Owns one dialog phase (START or STOP). A reader is armed once per phase,
/// independently of its terminal state from a preceding phase.
/// </summary>
public sealed class PoseidonReadPhaseRunner
{
private readonly bool readStart;
private readonly HashSet<IPoseidonReadOperation> startedReaders =
new HashSet<IPoseidonReadOperation>();
public int IterationCount { get; private set; }
public PoseidonReadPhaseRunner(bool readStart)
{
this.readStart = readStart;
}
public bool RunIteration(IEnumerable<IPoseidonReadOperation> readers)
{
if (readers == null) return true;
IterationCount++;
bool allReadersFinished = true;
foreach (IPoseidonReadOperation reader in readers)
{
if (reader == null) continue;
if (startedReaders.Add(reader)) reader.Start(readStart);
reader.Run();
if (!reader.IsFinished) allReadersFinished = false;
}
return allReadersFinished;
}
}
}