-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added dalayed initialization for server
- Loading branch information
Showing
2 changed files
with
26 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters