Example #1
0
        public static IDisposable GetTypedBeckhoffNotification(this AdsClient beckhoff, ISymbol symbol, Type type, IPlc plc, string address, TimeSpan regularTransmissionCycle)
        {
            if (type == typeof(byte[]))
            {
                return(Observable.Timer(TimeSpan.FromMilliseconds(100), regularTransmissionCycle)
                       .Select(_ => beckhoff.ReadVariable <byte[]>(symbol))
                       .Do(value => Log.Logger.Debug($"Writing {symbol.InstancePath} to {address}: {ByteToString(value)}"))
                       .SelectMany(value => plc.Write(address, value))
                       .Retry()
                       .Subscribe());
            }

            var typecode = Type.GetTypeCode(type);

            switch (typecode)
            {
            case TypeCode.Boolean:
                return(beckhoff.WhenNotification <bool>(symbol.InstancePath, NotificationSettings.Default)
                       .SelectMany(value => plc.Write(address, value))
                       .Subscribe());

            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
                return(beckhoff.WhenNotification <byte>(symbol.InstancePath, NotificationSettings.Default)
                       .SelectMany(value => plc.Write(address, value))
                       .Subscribe());

            case TypeCode.Int16:
            case TypeCode.UInt16:
                return(beckhoff.WhenNotification <short>(symbol.InstancePath, NotificationSettings.Default)
                       .SelectMany(value => plc.Write(address, value))
                       .Subscribe());

            case TypeCode.Int32:
            case TypeCode.UInt32:
                return(beckhoff.WhenNotification <int>(symbol.InstancePath, NotificationSettings.Default)
                       .SelectMany(value => plc.Write(address, value))
                       .Subscribe());

            case TypeCode.Int64:
            case TypeCode.UInt64:
                return(beckhoff.WhenNotification <long>(symbol.InstancePath, NotificationSettings.Default)
                       .SelectMany(value => plc.Write(address, value))
                       .Subscribe());

            case TypeCode.Single:
                return(beckhoff.WhenNotification <float>(symbol.InstancePath, NotificationSettings.Default)
                       .SelectMany(value => plc.Write(address, value))
                       .Subscribe());

            default:
                throw new ArgumentException($"Unsupported Type {type.Name}");
            }
        }