mirror of
https://github.com/evopro-ag/Sharp7Reactive.git
synced 2025-12-16 11:42:52 +00:00
Use file scoped namespaces
This commit is contained in:
@@ -1,74 +1,71 @@
|
||||
using System;
|
||||
using System.Reactive;
|
||||
using System.Reactive;
|
||||
using System.Reactive.Disposables;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using Sharp7.Rx.Enums;
|
||||
using Sharp7.Rx.Interfaces;
|
||||
|
||||
namespace Sharp7.Rx.Extensions
|
||||
namespace Sharp7.Rx.Extensions;
|
||||
|
||||
public static class PlcExtensions
|
||||
{
|
||||
public static class PlcExtensions
|
||||
public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData, bool initialTransfer)
|
||||
{
|
||||
public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData, bool initialTransfer)
|
||||
return Observable.Create<TReturn>(async observer =>
|
||||
{
|
||||
return Observable.Create<TReturn>(async observer =>
|
||||
var subscriptions = new CompositeDisposable();
|
||||
|
||||
var notification = plc
|
||||
.CreateNotification<bool>(triggerAddress, TransmissionMode.OnChange)
|
||||
.Publish()
|
||||
.RefCount();
|
||||
|
||||
if (initialTransfer)
|
||||
{
|
||||
var subscriptions = new CompositeDisposable();
|
||||
await plc.ConnectionState.FirstAsync(state => state == ConnectionState.Connected).ToTask();
|
||||
var initialValue = await ReadData(plc, readData);
|
||||
observer.OnNext(initialValue);
|
||||
}
|
||||
|
||||
var notification = plc
|
||||
.CreateNotification<bool>(triggerAddress, TransmissionMode.OnChange)
|
||||
.Publish()
|
||||
.RefCount();
|
||||
notification
|
||||
.Where(trigger => trigger)
|
||||
.SelectMany(_ => ReadDataAndAcknowlodge(plc, readData, ackTriggerAddress))
|
||||
.Subscribe(observer)
|
||||
.AddDisposableTo(subscriptions);
|
||||
|
||||
if (initialTransfer)
|
||||
notification
|
||||
.Where(trigger => !trigger)
|
||||
.SelectMany(async _ =>
|
||||
{
|
||||
await plc.ConnectionState.FirstAsync(state => state == ConnectionState.Connected).ToTask();
|
||||
var initialValue = await ReadData(plc, readData);
|
||||
observer.OnNext(initialValue);
|
||||
}
|
||||
await plc.SetValue(ackTriggerAddress, false);
|
||||
return Unit.Default;
|
||||
})
|
||||
.Subscribe()
|
||||
.AddDisposableTo(subscriptions);
|
||||
|
||||
notification
|
||||
.Where(trigger => trigger)
|
||||
.SelectMany(_ => ReadDataAndAcknowlodge(plc, readData, ackTriggerAddress))
|
||||
.Subscribe(observer)
|
||||
.AddDisposableTo(subscriptions);
|
||||
return subscriptions;
|
||||
});
|
||||
}
|
||||
|
||||
notification
|
||||
.Where(trigger => !trigger)
|
||||
.SelectMany(async _ =>
|
||||
{
|
||||
await plc.SetValue(ackTriggerAddress, false);
|
||||
return Unit.Default;
|
||||
})
|
||||
.Subscribe()
|
||||
.AddDisposableTo(subscriptions);
|
||||
public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData)
|
||||
{
|
||||
return CreateDatatransferWithHandshake(plc, triggerAddress, ackTriggerAddress, readData, false);
|
||||
}
|
||||
|
||||
return subscriptions;
|
||||
});
|
||||
}
|
||||
private static async Task<TReturn> ReadData<TReturn>(IPlc plc, Func<IPlc, Task<TReturn>> receiveData)
|
||||
{
|
||||
return await receiveData(plc);
|
||||
}
|
||||
|
||||
public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData)
|
||||
private static async Task<TReturn> ReadDataAndAcknowlodge<TReturn>(IPlc plc, Func<IPlc, Task<TReturn>> readData, string ackTriggerAddress)
|
||||
{
|
||||
try
|
||||
{
|
||||
return CreateDatatransferWithHandshake(plc, triggerAddress, ackTriggerAddress, readData, false);
|
||||
return await ReadData(plc, readData);
|
||||
}
|
||||
|
||||
private static async Task<TReturn> ReadData<TReturn>(IPlc plc, Func<IPlc, Task<TReturn>> receiveData)
|
||||
finally
|
||||
{
|
||||
return await receiveData(plc);
|
||||
}
|
||||
|
||||
private static async Task<TReturn> ReadDataAndAcknowlodge<TReturn>(IPlc plc, Func<IPlc, Task<TReturn>> readData, string ackTriggerAddress)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await ReadData(plc, readData);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await plc.SetValue(ackTriggerAddress, true);
|
||||
}
|
||||
await plc.SetValue(ackTriggerAddress, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user