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

WIP: Limit number of datapoints client subscribes to #92

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
44 changes: 44 additions & 0 deletions src/ism7mqtt/ISM7/Ism7Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Buffers.Binary;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
Expand Down Expand Up @@ -237,6 +238,35 @@ private static string HexEncode(ReadOnlySpan<byte> data)
return s.ToString();
}

#region Debug

private static int GetEnvInt32(string name, int defaultValue = default)
{
var value = Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
if (value is null) return defaultValue;
if (Int32.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out var parsed))
return parsed;
return defaultValue;
}

private int _subscriptionLimit = -1;

private int SubscriptionLimit
{
get
{
if (_subscriptionLimit < 0)
{
_subscriptionLimit = GetEnvInt32("ISM7_SUBSCR_LIMIT");
}
return _subscriptionLimit;
}
}

private int _subscriptionCount;

#endregion

private async Task SubscribeAsync(string busAddress, CancellationToken cancellationToken)
{
var infoReads = _config.GetInfoReadForDevice(busAddress).ToList();
Expand All @@ -247,6 +277,20 @@ private async Task SubscribeAsync(string busAddress, CancellationToken cancellat
infoRead.BusAddress = busAddress;
infoRead.Intervall = Interval;
}
if (SubscriptionLimit > 0)
{
int remaining = SubscriptionLimit - _subscriptionCount;
if (remaining == 0)
{
return;
}
if (remaining < infoReads.Count)
{
infoReads = infoReads.Take(remaining).ToList();
}
_subscriptionCount += infoReads.Count;
Console.WriteLine($"Total subscribed infopoints: {_subscriptionCount}");
}
await SendAsync(new TelegramBundleReq
{
AbortOnError = false,
Expand Down
2 changes: 1 addition & 1 deletion src/ism7mqtt/ISM7/Ism7Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void AddDevice(string ip, string ba)
public IEnumerable<InfoRead> GetInfoReadForDevice(string ba)
{
var devices = _devices[Converter.FromHex(ba)];
return devices.SelectMany(x => x.InfoReads);
return devices.SelectMany(x => x.InfoReads).DistinctBy(ir => ir.InfoNumber);
}

public bool ProcessData(IEnumerable<InfonumberReadResp> data)
Expand Down