Exemplo n.º 1
0
        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 =>
            {
                var subscriptions = new CompositeDisposable();

                var notification = plc
                                   .CreateNotification <bool>(triggerAddress, TransmissionMode.OnChange, TimeSpan.Zero)
                                   .Publish()
                                   .RefCount();

                if (initialTransfer)
                {
                    var initialValue = await ReadData(plc, readData);
                    observer.OnNext(initialValue);
                }

                notification
                .Where(trigger => trigger)
                .Select(_ => ReadDataAndAcknowlodge(plc, readData, ackTriggerAddress))
                .Subscribe(observer)
                .AddDisposableTo(subscriptions);

                notification
                .Where(trigger => !trigger)
                .Select(_ => plc.SetValue(ackTriggerAddress, false))
                .Subscribe()
                .AddDisposableTo(subscriptions);

                return subscriptions;
            }));
        }