Example #1
0
        public static IObservable <object> PollValues(this IAdsConnection connection, string instancePath, Type type, int[] args, IObservable <Unit> trigger, Func <Exception, object> errorHandler)
        {
            string[]            symbolPaths = new string[] { instancePath };
            DisposableHandleBag bag         = new DisposableHandleBag(connection, symbolPaths);
            Func <Unit, object> func        = delegate(Unit o) {
                try
                {
                    return(connection.ReadAny(0xf005, bag.GetHandle(instancePath), type, args));
                }
                catch (Exception exception)
                {
                    if (errorHandler == null)
                    {
                        throw;
                    }
                    return(errorHandler(exception));
                }
            };
            Action action = delegate {
                bag.Dispose();
                bag = null;
            };

            return(Observable.Finally <object>(Observable.Select <Unit, object>(trigger, func), action));
        }
Example #2
0
        public static IDisposable WriteValues <T>(this IAdsConnection connection, string instancePath, IObservable <T> valueSequence, Action <Exception> errorHandler)
        {
            string[]            symbolPaths = new string[] { instancePath };
            DisposableHandleBag bag         = new DisposableHandleBag(connection, symbolPaths);
            Action <T>          action      = delegate(T v) {
                try
                {
                    uint handle = 0;
                    if (!bag.TryGetHandle(instancePath, out handle))
                    {
                        throw new AdsException($"Handle for '{instancePath}' is not created!");
                    }
                    connection.WriteAny((int)handle, v);
                }
                catch (Exception exception)
                {
                    errorHandler(exception);
                }
            };

            return(ObservableExtensions.Subscribe <T>(valueSequence, action, delegate(Exception ex) {
                try
                {
                    if (errorHandler == null)
                    {
                        throw ex;
                    }
                    errorHandler(ex);
                }
                finally
                {
                    bag.Dispose();
                }
            }, delegate {
                bag.Dispose();
            }));
        }