Skip to content

Commit

Permalink
add all the things to program
Browse files Browse the repository at this point in the history
  • Loading branch information
katarinagud committed Jun 14, 2024
1 parent 4e207d1 commit 2255241
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
34 changes: 0 additions & 34 deletions Source/NodeReader.cs

This file was deleted.

7 changes: 6 additions & 1 deletion Source/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RaaLabs. All rights reserved.
// Licensed under the GPLv2 License. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics.CodeAnalysis;
using RaaLabs.Edge.Modules.EventHandling;
using RaaLabs.Edge.Modules.EdgeHub;
Expand All @@ -26,9 +27,13 @@ static void Main(string[] args)
.WithTask<Connector>()
.WithManualRegistration(_ =>
{
_.RegisterInstance(TimeProvider.System);
_.RegisterInstance(DefaultSessionFactory.Instance).As<ISessionFactory>();
_.RegisterType<Connector>().As<ICreateSessions>();
_.RegisterType<Client>().As<ICreateSessions>();
_.RegisterType<DataReader>().As<IRetrieveData>();
_.RegisterType<DataPointParser>().As<ICreateDatapointsFromDataValues>();
_.RegisterType<Subscriber>().As<ICanSubscribeToNodes>();
_.RegisterType<Reader>().As<ICanReadNodes>();
})
.Build();

Expand Down
40 changes: 40 additions & 0 deletions Source/Reader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
using Serilog;

namespace RaaLabs.Edge.Connectors.OPCUA;

public class Reader : ICanReadNodes
{
private readonly ILogger _logger;

public Reader(ILogger logger)
{
_logger = logger;
}

public async Task ReadNodesForever(ISession connection, IEnumerable<(NodeId node, TimeSpan readInterval)> nodes, Func<NodeValue, Task> handleValue, CancellationToken cancellationToken)
{
_logger.Information("Starting reading nodes...");
foreach (var (node, readInterval) in nodes)
{
_ = Task.Run(() => ReadNodeForever(connection, node, readInterval, handleValue, cancellationToken), cancellationToken);
}
await Task.CompletedTask.ConfigureAwait(false);
}

private async Task ReadNodeForever(ISession connection, NodeId node, TimeSpan readInterval, Func<NodeValue, Task> handleValue, CancellationToken cancellationToken)
{
using var timer = new PeriodicTimer(readInterval);
while (!cancellationToken.IsCancellationRequested)
{
var dataValue = await connection.ReadValueAsync(node, cancellationToken).ConfigureAwait(false);
await handleValue(new (node, dataValue)).ConfigureAwait(false);
await timer.WaitForNextTickAsync(cancellationToken).ConfigureAwait(false);
}
}
}

0 comments on commit 2255241

Please sign in to comment.