116 lines
2.7 KiB
C#
116 lines
2.7 KiB
C#
namespace Common.Hardware.SIRT.Tasks
|
|
{
|
|
using System;
|
|
using System.Threading;
|
|
|
|
public abstract class SIRTTask
|
|
{
|
|
protected readonly ManualResetEventSlim awaiter = new ManualResetEventSlim();
|
|
protected readonly SIRTMessage request = new SIRTMessage();
|
|
protected readonly Guid taskId = Guid.NewGuid();
|
|
|
|
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 DateTimeOffset Timestamp
|
|
{
|
|
get => this.request.Timestamp;
|
|
set => this.request.Timestamp = value;
|
|
}
|
|
|
|
public int Timeout { get; set; } = 10_000;
|
|
|
|
public abstract void Push(SIRTMessage response);
|
|
|
|
public void SetCancelled()
|
|
{
|
|
this.State = SIRTTaskState.Cancelled;
|
|
|
|
this.awaiter.Set();
|
|
}
|
|
|
|
public void SetCompleted()
|
|
{
|
|
this.State = SIRTTaskState.Completed;
|
|
|
|
this.awaiter.Set();
|
|
}
|
|
|
|
public void SetPending()
|
|
{
|
|
this.State = SIRTTaskState.Pending;
|
|
|
|
this.awaiter.Set();
|
|
}
|
|
|
|
public void SetRunning()
|
|
{
|
|
this.State = SIRTTaskState.Running;
|
|
|
|
this.awaiter.Reset();
|
|
}
|
|
|
|
public virtual byte[] Start()
|
|
{
|
|
this.awaiter.Reset();
|
|
|
|
this.State = SIRTTaskState.Running;
|
|
this.Timestamp = DateTimeOffset.UtcNow;
|
|
|
|
return this.request.GetBytes();
|
|
}
|
|
|
|
public void Wait()
|
|
{
|
|
this.awaiter.Wait(this.Timeout);
|
|
this.awaiter.Reset();
|
|
}
|
|
|
|
public override bool Equals(object other)
|
|
=> other is SIRTTask _task && _task.taskId == this.taskId;
|
|
|
|
public static implicit operator SIRTMessage(SIRTTask task) => task.request;
|
|
}
|
|
}
|