102 lines
2.5 KiB
C#
102 lines
2.5 KiB
C#
namespace Common.Hardware.SIRT.Tasks
|
|
{
|
|
using System;
|
|
using System.Threading;
|
|
|
|
public abstract class SIRTTask
|
|
{
|
|
protected readonly SIRTMessage request = new SIRTMessage();
|
|
|
|
public event Action<SIRTTask> StateChanged;
|
|
|
|
public byte Command => this.request.Command;
|
|
|
|
private SIRTTaskState state;
|
|
public SIRTTaskState State
|
|
{
|
|
get => this.state;
|
|
private set
|
|
{
|
|
this.state = value;
|
|
|
|
this.StateChanged?.Invoke(this);
|
|
}
|
|
}
|
|
|
|
public uint RequestAddress
|
|
{
|
|
get => this.request.Address;
|
|
set
|
|
{
|
|
this.request.Address = value;
|
|
|
|
if (this.ResponseAddress == 0)
|
|
{
|
|
this.ResponseAddress = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public uint ResponseAddress { get; set; }
|
|
|
|
public string SirtId
|
|
{
|
|
get => this.request.SirtId;
|
|
set => this.request.SirtId = value;
|
|
}
|
|
|
|
public int Frequency
|
|
{
|
|
get => this.request.Frequency;
|
|
set => this.request.Frequency = value;
|
|
}
|
|
|
|
public int Timeout { get; set; } = 10_000;
|
|
|
|
public abstract void Receive(SIRTMessage response);
|
|
|
|
public void SetCancelled() => this.State = SIRTTaskState.Cancelled;
|
|
|
|
public void SetCompleted() => this.State = SIRTTaskState.Completed;
|
|
|
|
public void SetPending() => this.State = SIRTTaskState.Pending;
|
|
|
|
public void SetRunning() => this.State = SIRTTaskState.Running;
|
|
|
|
public virtual byte[] Start()
|
|
{
|
|
this.SetRunning();
|
|
|
|
return this.request.GetBytes();
|
|
}
|
|
|
|
public void Wait()
|
|
{
|
|
var awaiter = new ManualResetEventSlim();
|
|
|
|
void OnStateChanged(SIRTTask task)
|
|
{
|
|
if (task.State != SIRTTaskState.Running)
|
|
{
|
|
awaiter.Set();
|
|
}
|
|
}
|
|
|
|
this.StateChanged += OnStateChanged;
|
|
|
|
awaiter.Wait(this.Timeout);
|
|
|
|
this.StateChanged -= OnStateChanged;
|
|
}
|
|
|
|
public override bool Equals(object other)
|
|
=> other?.ToString() == this.ToString();
|
|
|
|
public override int GetHashCode() => this.ToString().GetHashCode();
|
|
|
|
public override String ToString() => this.request.ToString();
|
|
|
|
public static implicit operator SIRTMessage(SIRTTask task) => task.request;
|
|
}
|
|
}
|