forked from ddrilling/AsbCloudServer
141 lines
4.4 KiB
C#
141 lines
4.4 KiB
C#
using AsbCloudApp.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SaubPanelOnlineSender
|
|
{
|
|
public enum DbPlayerServiceState
|
|
{
|
|
Unaviable,
|
|
Stopped,
|
|
Paused,
|
|
Working
|
|
}
|
|
|
|
public class DbPlayerService
|
|
{
|
|
public DbPlayerServiceState State
|
|
{
|
|
get => state;
|
|
private set
|
|
{
|
|
if (state == value)
|
|
return;
|
|
state = value;
|
|
OnStateUpdate?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public DateTime LastSentDate { get; private set; }
|
|
|
|
public EventHandler<double> OnProcessUpdate;
|
|
public EventHandler<Exception> OnError;
|
|
public EventHandler OnStateUpdate;
|
|
|
|
private CancellationTokenSource cancellationTokenSource;
|
|
private DbPlayerServiceState state;
|
|
|
|
private static readonly Random rnd = new Random((int)DateTime.Now.Ticks);
|
|
|
|
public void Start()
|
|
{
|
|
State = DbPlayerServiceState.Working;
|
|
cancellationTokenSource = new CancellationTokenSource();
|
|
//dataSender = new ModbusDataSender(Host);
|
|
Task.Run(async () => await Work(cancellationTokenSource.Token));
|
|
}
|
|
|
|
public void Pause()
|
|
{
|
|
State = State == DbPlayerServiceState.Paused
|
|
? DbPlayerServiceState.Working
|
|
: DbPlayerServiceState.Paused;
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
State = DbPlayerServiceState.Stopped;
|
|
cancellationTokenSource.Cancel();
|
|
}
|
|
|
|
async Task Work(CancellationToken token)
|
|
{
|
|
var period = TimeSpan.FromSeconds(1);
|
|
DateTime nextTime;
|
|
var dataSender = new CloudDataSender();
|
|
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
nextTime = DateTime.Now + period;
|
|
if ((State != DbPlayerServiceState.Working)
|
|
&& ((State != DbPlayerServiceState.Paused)))
|
|
break;
|
|
|
|
if (State == DbPlayerServiceState.Paused)
|
|
{
|
|
await Task.Delay(100, token).ConfigureAwait(false);
|
|
continue;
|
|
}
|
|
|
|
if (State == DbPlayerServiceState.Working)
|
|
{
|
|
|
|
try
|
|
{
|
|
dataSender.Send(MakeDataSaubBaseDto());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
OnError?.Invoke(this, ex);
|
|
}
|
|
|
|
OnProcessUpdate?.Invoke(this, 0);
|
|
}
|
|
|
|
var waitTime = nextTime - DateTime.Now;
|
|
if (waitTime.TotalMilliseconds > 0)
|
|
await Task.Delay(waitTime, token).ConfigureAwait(false);
|
|
}
|
|
|
|
Stop();
|
|
}
|
|
|
|
|
|
private static DataSaubBaseDto MakeDataSaubBaseDto()
|
|
{
|
|
return new DataSaubBaseDto
|
|
{
|
|
Date = DateTime.Now,
|
|
AxialLoad = rnd.NextDouble()*100d,
|
|
AxialLoadLimitMax = rnd.NextDouble()*100d,
|
|
AxialLoadSp = rnd.NextDouble()*100d,
|
|
BitDepth = rnd.NextDouble()*100d,
|
|
BlockHeight = rnd.NextDouble()*100d,
|
|
BlockSpeed = rnd.NextDouble()*100d,
|
|
WellDepth = rnd.NextDouble()*100d,
|
|
BlockSpeedSp = rnd.NextDouble()*100d,
|
|
Flow = rnd.NextDouble()*100d,
|
|
FlowDeltaLimitMax = rnd.NextDouble()*100d,
|
|
FlowIdle = rnd.NextDouble()*100d,
|
|
HookWeight = rnd.NextDouble()*100d,
|
|
HookWeightIdle = rnd.NextDouble()*100d,
|
|
HookWeightLimitMax = rnd.NextDouble()*100d,
|
|
HookWeightLimitMin= rnd.NextDouble() * 100d,
|
|
Mode= 1,
|
|
Pressure= rnd.NextDouble() * 100d,
|
|
PressureDeltaLimitMax= rnd.NextDouble() * 100d,
|
|
PressureIdle= rnd.NextDouble() * 100d,
|
|
PressureSp= rnd.NextDouble() * 100d,
|
|
RotorSpeed= rnd.NextDouble() * 100d,
|
|
RotorTorque= rnd.NextDouble() * 100d,
|
|
RotorTorqueIdle= rnd.NextDouble() * 100d,
|
|
RotorTorqueLimitMax= rnd.NextDouble() * 100d,
|
|
RotorTorqueSp= rnd.NextDouble() * 100d,
|
|
};
|
|
}
|
|
}
|
|
}
|