Skip to content

Commit

Permalink
added settings for cycle time
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Jun 30, 2020
1 parent 30bbf20 commit 780abe1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
13 changes: 13 additions & 0 deletions TFU002/TFU002.Interfaces/Extensions/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace TFU002.Interfaces.Extensions
{
public static class TimeSpanExtensions
{
public static TimeSpan AtLeast(this TimeSpan timeSpan, TimeSpan minimalTimeSpan)
{
if (timeSpan < minimalTimeSpan) return minimalTimeSpan;
return timeSpan;
}
}
}
5 changes: 4 additions & 1 deletion TFU002/TFU002.Interfaces/Models/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace TFU002.Interfaces.Models
{
public class ApplicationSettings
{
public BeckhoffSettings BeckhoffSettings { get; set; } = new BeckhoffSettings();
public List<ExtenalPlcSetting> ExtenalPlcSettings { get; set; } = new List<ExtenalPlcSetting>();
public TimeSpan NotificationCycleTime { get; set; } = TimeSpan.FromMilliseconds(100);
public TimeSpan IntervalTransmissionCycleTime { get; set; } = TimeSpan.FromSeconds(1);
}
}
2 changes: 2 additions & 0 deletions TFU002/TFU002.Interfaces/Services/IBeckhoffService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public interface IBeckhoffService
IObservable<AdsState> AdsState { get; }
AdsClient Client { get; }
IObservable<ISymbolCollection<ISymbol>> Symbols { get; }
public TimeSpan NotificationCycleTime { get; }
public TimeSpan IntervalTransmissionCycleTime { get; }
}
}
20 changes: 10 additions & 10 deletions TFU002/TFU002.Logic/GatewayNotificationsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace TFU002.Logic
{
public static class GatewayNotificationsExtensions
{
public static IDisposable GetTypedS7Notification(this IPlc plc, Type type, string address, AdsClient beckhoff, ISymbol symbol)
public static IDisposable GetTypedS7Notification(this IPlc plc, Type type, string address, AdsClient beckhoff, ISymbol symbol, TimeSpan notificationCycle, TimeSpan regularTransmissionCycle)
{
if (type == typeof(byte[]))
{
return plc.CreateNotification<byte[]>(address, TransmissionMode.Cyclic, TimeSpan.FromMilliseconds(1000))
return plc.CreateNotification<byte[]>(address, TransmissionMode.Cyclic, regularTransmissionCycle)
.Do(value => Log.Logger.Debug($"Writing {address} to {symbol.InstancePath} {ByteToString(value)}"))
.SelectMany(value => beckhoff.Write(symbol, value))
.Subscribe();
Expand All @@ -30,44 +30,44 @@ public static IDisposable GetTypedS7Notification(this IPlc plc, Type type, strin
switch (typecode)
{
case TypeCode.Boolean:
return plc.CreateNotification<bool>(address, TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
return plc.CreateNotification<bool>(address, TransmissionMode.OnChange, notificationCycle)
.SelectMany(value => beckhoff.Write(symbol, value))
.Subscribe();
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Char:
return plc.CreateNotification<byte>(address, TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
return plc.CreateNotification<byte>(address, TransmissionMode.OnChange, notificationCycle)
.SelectMany(value => beckhoff.Write(symbol, value))
.Subscribe();
case TypeCode.Int16:
case TypeCode.UInt16:
return plc.CreateNotification<short>(address, TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
return plc.CreateNotification<short>(address, TransmissionMode.OnChange, notificationCycle)
.SelectMany(value => beckhoff.Write(symbol, value))
.Subscribe();
case TypeCode.Int32:
case TypeCode.UInt32:
return plc.CreateNotification<int>(address, TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
return plc.CreateNotification<int>(address, TransmissionMode.OnChange, notificationCycle)
.SelectMany(value => beckhoff.Write(symbol, value))
.Subscribe();
case TypeCode.Int64:
case TypeCode.UInt64:
return plc.CreateNotification<long>(address, TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
return plc.CreateNotification<long>(address, TransmissionMode.OnChange, notificationCycle)
.SelectMany(value => beckhoff.Write(symbol, value))
.Subscribe();
case TypeCode.Single:
return plc.CreateNotification<float>(address, TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
return plc.CreateNotification<float>(address, TransmissionMode.OnChange, notificationCycle)
.SelectMany(value => beckhoff.Write(symbol, value))
.Subscribe();
default:
throw new ArgumentException($"Unsupported Type {type.Name}");
}
}

public static IDisposable GetTypedBeckhoffNotification(this AdsClient beckhoff, ISymbol symbol, Type type, IPlc plc, string address)
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), TimeSpan.FromSeconds(1))
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))
Expand Down
2 changes: 2 additions & 0 deletions TFU002/TFU002.Logic/Services/BeckhoffService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,7 @@ private void UpdateSymbols(AdsState state)
}
private readonly BehaviorSubject<ISymbolCollection<ISymbol>> symbolsSubject = new BehaviorSubject<ISymbolCollection<ISymbol>>(null);
public IObservable<ISymbolCollection<ISymbol>> Symbols => symbolsSubject.AsObservable();
public TimeSpan NotificationCycleTime => settingsProvider.Settings.NotificationCycleTime.AtLeast(TimeSpan.FromMilliseconds(100));
public TimeSpan IntervalTransmissionCycleTime => settingsProvider.Settings.IntervalTransmissionCycleTime.AtLeast(TimeSpan.FromMilliseconds(100));
}
}
13 changes: 11 additions & 2 deletions TFU002/TFU002.Logic/Services/GatewayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,22 @@ private IDisposable GenerateObservers(List<GatewayVariable> variables)

if(gatewayVariable.Direction == Direction.Input)
{
plc.GetTypedS7Notification(gatewayVariable.TargetType, gatewayVariable.S7Address, beckhoffService.Client, gatewayVariable.Symbol)
plc.GetTypedS7Notification(gatewayVariable.TargetType,
gatewayVariable.S7Address,
beckhoffService.Client,
gatewayVariable.Symbol,
beckhoffService.NotificationCycleTime,
beckhoffService.IntervalTransmissionCycleTime)
.AddDisposableTo(subscriptions);
}

if(gatewayVariable.Direction == Direction.Output)
{
beckhoffService.Client.GetTypedBeckhoffNotification(gatewayVariable.Symbol, gatewayVariable.TargetType, plc, gatewayVariable.S7Address)
beckhoffService.Client.GetTypedBeckhoffNotification(gatewayVariable.Symbol,
gatewayVariable.TargetType,
plc,
gatewayVariable.S7Address,
beckhoffService.IntervalTransmissionCycleTime)
.AddDisposableTo(subscriptions);
}
}
Expand Down

0 comments on commit 780abe1

Please sign in to comment.