Skip to content

Commit

Permalink
added dalayed initialization for server
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Apr 26, 2020
1 parent e84cbdc commit 3c13d50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions SoftBeckhoff/Services/BeckhoffService.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
using System;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using TwinCAT.Ads.Server;

namespace SoftBeckhoff.Services
{
public class BeckhoffService : IPlcService
{
private readonly CompositeDisposable disposables = new CompositeDisposable();
private readonly ILogger logger;
public BeckhoffService(ILogger<BeckhoffService> logger)
{
var server = new BeckhoffServer(logger);
this.logger = logger;

var init = Observable.Timer(TimeSpan.FromMilliseconds(500))
.SelectMany(_ => InitializeAsync())
.Subscribe();
disposables.Add(init);
}

public void Dispose()
{

}
}

private Task<Unit> InitializeAsync()
{
logger.LogInformation("Initializing Beckhoff server...");
var server = new BeckhoffServer(logger);
return Task.FromResult(Unit.Default);
}
}

public class BeckhoffServer
{
private readonly ILogger _logger;
private readonly ILogger logger;

public BeckhoffServer(ILogger logger)
{
_logger = logger;
this.logger = logger;
var server = (AmsServerNet) typeof(AmsServerNet)
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null,
CallingConventions.Any,
new []{typeof(ILogger)},
null)
?.Invoke(new[] {logger});
logger.LogInformation($"Beckhoff server created");
Task.Delay(500).Wait();
var result = server.AmsConnect(851, "SoftPlc");
var connected = server.IsServerConnected;
logger.LogInformation($"Beckhoff server connected = {connected} with result = {result}");
}
}
}
2 changes: 1 addition & 1 deletion SoftBeckhoff/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"AmsRouter": {
"Name": "SoftBeckhoff",
"NetId": "192.168.1.20.1.1",
"TcpPort": 48899,
"TcpPort": 48898,
"RemoteConnections": [
{
"Name": "RemoteSystem1",
Expand Down

0 comments on commit 3c13d50

Please sign in to comment.