Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read device state from data stream and report on state endpoint #1

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ChemDoserProxy/Dto/DataFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DataFrame

public DateTime ReceiveDate { get; init; }

public bool Flow { get; init; }
public DeviceState State { get; init; }

public int pHValue { get; init; }

Expand All @@ -27,7 +27,7 @@ public class DataFrame
public override string ToString()
{
return string.Join(Environment.NewLine, $"Serial={Serial}", $"Timestamp={Timestamp}", $"Received={ReceiveDate}",
$"Flow={Flow}", $"pH={pHValue}",
$"State={State}", $"pH={pHValue}",
$"clF={clFreeValue}", $"clF(mv)={clFreeMilliVolts}", $"WaterTemp={WaterTemperature}",
$"ActivePump={ActivePump}", $"Delay={DelaySeconds}s");
}
Expand All @@ -43,3 +43,16 @@ public class FlowRates

public int FlocPlusC { get; init; }
}

public enum DeviceState
{
NoError = 0,
HourDoseExceeded = 1,
TimeCorrectionError = 2,
NoFlow = 4,
EmptyBufferTank = 8,
BufferTankOverflow = 16,
LowFillingSpeed = 32,
NoChangeInpHValue = 64,
NoChangeInClValue = 128
}
2 changes: 1 addition & 1 deletion ChemDoserProxy/Dto/DoserState.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace ChemDoserProxy.Dto;

public record DoserState(decimal clFree, decimal clFreeMv, decimal pH, decimal WaterTemp);
public record DoserState(DeviceState State, decimal clFree, decimal clFreeMv, decimal pH, decimal WaterTemp);
3 changes: 1 addition & 2 deletions ChemDoserProxy/Logic/DataFrameProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Threading.Channels;
using ChemDoserProxy.Dto;
using ChemDoserProxy.State;
using ChemDoserProxy.Tcp;

namespace ChemDoserProxy.Logic;

Expand Down Expand Up @@ -57,7 +56,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
_pumpLastActive = null;
}

_stateManager.Set(frame.clFreeValue / 100m, frame.clFreeMilliVolts, frame.pHValue / 100m,
_stateManager.Set(frame.State, frame.clFreeValue / 100m, frame.clFreeMilliVolts, frame.pHValue / 100m,
frame.WaterTemperature / 10m);
}
}
Expand Down
1 change: 0 additions & 1 deletion ChemDoserProxy/Logic/DataFrameQueue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Threading.Channels;
using ChemDoserProxy.Dto;
using ChemDoserProxy.Tcp;

namespace ChemDoserProxy.Logic;

Expand Down
6 changes: 3 additions & 3 deletions ChemDoserProxy/State/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public StateManager(ILogger<StateManager> logger)
_logger = logger;
}

public DoserState State { get; private set; } = new(0, 0, 0, 0);
public DoserState State { get; private set; } = new(DeviceState.NoError, 0, 0, 0, 0);

public void Set(decimal clFree, decimal clFreeMv, decimal pH, decimal waterTemp)
public void Set(DeviceState state, decimal clFree, decimal clFreeMv, decimal pH, decimal waterTemp)
{
State = new DoserState(clFree, clFreeMv, pH, waterTemp);
State = new DoserState(state, clFree, clFreeMv, pH, waterTemp);
_logger.LogDebug("State updated to {}", State);
}
}
3 changes: 1 addition & 2 deletions ChemDoserProxy/Tcp/ClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Channels;
using ChemDoserProxy.Dto;
using ChemDoserProxy.Logic;
using Microsoft.Extensions.Logging;

namespace ChemDoserProxy.Tcp;

Expand Down Expand Up @@ -43,7 +42,7 @@ public async Task Receive(CancellationToken ct)
ReceiveDate = DateTime.UtcNow,
Timestamp = new DateTime(2000 + buffer[6], buffer[7], buffer[8],
buffer[9], buffer[10], buffer[11]),
Flow = buffer[13] == 0,
State = (DeviceState)buffer[13],
pHValue = BitConverter.ToInt16(buffer.AsSpan().GetBytes(14, 2)),
clFreeValue = BitConverter.ToInt16(buffer.AsSpan().GetBytes(16, 2)),
clFreeMilliVolts = BitConverter.ToInt16(buffer.AsSpan().GetBytes(20, 2)),
Expand Down
4 changes: 1 addition & 3 deletions ChemDoserProxy/Tcp/Forwarder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Net;
using System.Net.Sockets;
using System.Net.Sockets;
using ChemDoserProxy.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace ChemDoserProxy.Tcp;
Expand Down
2 changes: 0 additions & 2 deletions ChemDoserProxy/Tcp/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Net.Sockets;
using ChemDoserProxy.Configuration;
using ChemDoserProxy.Logic;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace ChemDoserProxy.Tcp;
Expand Down
Loading