BOB/DeviceCommand/Device/E36233A.cs

202 lines
6.3 KiB
C#

using Common.Attributes;
using DeviceCommand.Base;
using Logger;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
{
[BOBCommand]
public class E36233A : Tcp
{
public E36233A(string ipAddress, int port, int sendTimeout, int receiveTimeout)
{
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
}
#region
private CancellationTokenSource _cancellationTokenSource;
private Task? _heartbeatTask;
private const int HeartbeatInterval = 3000;
public bool IsActive = false;
public int ReConnectionAttempts = 0;
public const int MaxReconnectAttempts = 10;
public override async Task<bool> ConnectAsync(CancellationToken ct = default)
{
await _commLock.WaitAsync(ct);
try
{
if (TcpClient != null && !TcpClient.Connected) TcpClient = new();
if (TcpClient.Connected)
{
return true;
}
await TcpClient.ConnectAsync(IPAddress, Port, ct);
IsActive = true;
StartHeartbeat();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
_commLock.Release();
}
}
public override void Close()
{
if (!TcpClient.Connected)
{
throw new InvalidOperationException("TCP 没有连接成功");
}
if (TcpClient.Connected)
{
TcpClient.Close();
StopHeartbeat();
}
}
// 启动设备的心跳
public void StartHeartbeat()
{
if (_heartbeatTask != null)
return;
if (_cancellationTokenSource == null || _cancellationTokenSource.IsCancellationRequested)
_cancellationTokenSource = new();
_heartbeatTask = Task.Run(() => HeartbeatLoop(_cancellationTokenSource.Token));
}
// 停止设备的心跳
public void StopHeartbeat()
{
IsActive = false;
_cancellationTokenSource.Cancel();
_heartbeatTask = null;
}
private async Task HeartbeatLoop(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
await Task.Delay(HeartbeatInterval, ct);
try
{
await ();
}
catch (Exception ex)
{
IsActive = false;
ReConnectionAttempts++;
if (MaxReconnectAttempts < ReConnectionAttempts)
{
StopHeartbeat();
return;
}
await ReconnectDevice(ct);
}
}
}
private async Task ReconnectDevice(CancellationToken ct)
{
try
{
bool resultConnect = await ConnectAsync(ct);
if (resultConnect)
{
ReConnectionAttempts = 0;
}
}
catch (Exception ex)
{
}
}
#endregion
#region
public async Task (CancellationToken ct = default)
{
await SendAsync($"PROTection:CLEar\r\n", ct);
}
public async Task (bool , CancellationToken ct = default)
{
await SendAsync($"OUTPut {(开关 ? 1 : 0)}\r\n", ct);
}
public async Task (CancellationToken ct = default)
{
await SendAsync($"SYSTem:REMote\r\n", ct);
}
public async Task (CancellationToken ct = default)
{
await SendAsync($"SYSTem:BEEPer\r\n", ct);
}
public async Task (double , CancellationToken ct = default)
{
await SendAsync($"CURRent {电流}\r\n", ct);
}
public async Task OCP电流(double , CancellationToken ct = default)
{
await SendAsync($"CURRent:PROTection {电流}\r\n", ct);
}
public async Task OCP开关(bool , CancellationToken ct = default)
{
await SendAsync($"CURRent:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
}
public async Task (double , CancellationToken ct = default)
{
await SendAsync($"VOLTage {电压}\r\n", ct);
}
public async Task OVP电压(double , CancellationToken ct = default)
{
await SendAsync($"VOLT:PROTection {电压}\r\n", ct);
}
public async Task OVP开关(bool , CancellationToken ct = default)
{
await SendAsync($"VOLT:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
}
public async Task (double , CancellationToken ct = default)
{
await SendAsync($"POWer:PROTection {电压}\r\n", ct);
}
public async Task (bool , CancellationToken ct = default)
{
await SendAsync($"POWer:PROTection:STATe {(开关 ? 1 : 0)}\r\n", ct);
}
public async Task (double , double , CancellationToken ct = default)
{
await SendAsync($"CURRent:SLEW {上升斜率},{下降斜率}\r\n", ct);
}
public async Task (double , double , CancellationToken ct = default)
{
await SendAsync($"VOLTage:SLEW {上升斜率},{下降斜率}\r\n", ct);
}
public async Task (string , CancellationToken ct = default)
{
await SendAsync($"{指令}\r\n", ct);
}
public virtual async Task<string> (CancellationToken ct = default)
{
await SendAsync("*IDN?\r\n", ct);
return await ReadAsync(ct: ct);
}
#endregion
}
}