Verify data types

This commit is contained in:
Peter Butzhammer
2024-02-08 18:06:55 +01:00
parent 829dee14af
commit 5d85823167
2 changed files with 16 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
using System.Globalization;
#nullable enable
using System.Globalization;
using System.Text.RegularExpressions;
using Sharp7.Rx.Enums;
using Sharp7.Rx.Interfaces;
@@ -7,8 +8,8 @@ namespace Sharp7.Rx;
internal class S7VariableNameParser : IS7VariableNameParser
{
private static readonly Regex regex = new Regex(@"^(?<operand>db)(?<dbNo>\d+)\.?(?<type>[a-z]+)(?<start>\d+)(\.(?<bitOrLength>\d+))?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly Regex regex = new(@"^(?<operand>db)(?<dbNo>\d+)\.?(?<type>[a-z]+)(?<start>\d+)(\.(?<bitOrLength>\d+))?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly IReadOnlyDictionary<string, DbType> types = new Dictionary<string, DbType>(StringComparer.OrdinalIgnoreCase)
{

View File

@@ -77,8 +77,7 @@ public class Sharp7Plc : IPlc
{
return Observable.Create<TValue>(observer =>
{
var address = varaibleNameParser.Parse(variableName);
if (address == null) throw new ArgumentException("Input variable name is not valid", nameof(variableName));
var address = ParseAndVerify(variableName, typeof(TValue));
var disp = new CompositeDisposable();
var disposeableContainer = multiVariableSubscriptions.GetOrCreateObservable(variableName);
@@ -103,6 +102,15 @@ public class Sharp7Plc : IPlc
});
}
private S7VariableAddress ParseAndVerify(string variableName, Type type)
{
var address = varaibleNameParser.Parse(variableName);
if (!address.MatchesType(type))
throw new DataTypeMissmatchException($"Address \"{variableName}\" does not match type {type}.", type, address);
return address;
}
public Task<TValue> GetValue<TValue>(string variableName)
{
return GetValue<TValue>(variableName, CancellationToken.None);
@@ -117,8 +125,7 @@ public class Sharp7Plc : IPlc
public async Task<TValue> GetValue<TValue>(string variableName, CancellationToken token)
{
var address = varaibleNameParser.Parse(variableName);
if (address == null) throw new ArgumentException("Input variable name is not valid", nameof(variableName));
var address = ParseAndVerify(variableName, typeof(TValue));
var data = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
return S7ValueConverter.ReadFromBuffer<TValue>(data, address);
@@ -150,8 +157,7 @@ public class Sharp7Plc : IPlc
public async Task SetValue<TValue>(string variableName, TValue value, CancellationToken token)
{
var address = varaibleNameParser.Parse(variableName);
if (address == null) throw new ArgumentException("Input variable name is not valid", "variableName");
var address = ParseAndVerify(variableName, typeof(TValue));
if (typeof(TValue) == typeof(bool))
{