Skip to content

Commit

Permalink
Optimise and allow more nullable types
Browse files Browse the repository at this point in the history
Make adding tags fluent
  • Loading branch information
ChrisPulman committed Aug 3, 2023
1 parent 1c5c16b commit b2ef142
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.1.3",
"version": "1.1.4",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
26 changes: 13 additions & 13 deletions src/S7PlcRx.TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
var plc = new RxS7(S7PlcRx.Enums.CpuType.S71500, "172.16.17.1", 0, 1, interval: 5);
plc.LastError.Subscribe(ex => Console.WriteLine(ex));
plc.Status.Subscribe(status => Console.WriteLine(status));
const string DB100_DBB3 = nameof(DB100_DBB3);
const string StartLogging = nameof(StartLogging);
const string PlcData = nameof(PlcData);
const string TestItems = nameof(TestItems);
const string TagNames1 = nameof(TagNames1);
const string TagNames2 = nameof(TagNames2);
const string TagValues = nameof(TagValues);

plc.AddUpdateTagItem<byte>(DB100_DBB3, "DB100.DBB3");
plc.AddUpdateTagItem<byte[]>(PlcData, "DB100.DBB0", 64).SetTagPollIng(false);
plc.AddUpdateTagItem<byte[]>(TestItems, "DB101.DBB0", 520).SetTagPollIng(false);
plc.AddUpdateTagItem<byte[]>(TagNames1, "DB102.DBB0", 4096).SetTagPollIng(false);
plc.AddUpdateTagItem<double[]>(TagValues, "DB103.DBD0", 99).SetTagPollIng(false);
plc.AddUpdateTagItem<byte>(StartLogging, "DB100.DBB3")
.AddUpdateTagItem<byte[]>(PlcData, "DB100.DBB0", 64).SetTagPollIng(false)
.AddUpdateTagItem<byte[]>(TestItems, "DB101.DBB0", 520).SetTagPollIng(false)
.AddUpdateTagItem<byte[]>(TagNames1, "DB102.DBB0", 4096).SetTagPollIng(false)
.AddUpdateTagItem<double[]>(TagValues, "DB103.DBD4", 98).SetTagPollIng(false);

plc.IsConnected
.Where(x => x)
Expand All @@ -28,13 +28,13 @@
Console.WriteLine("Connected");
plc.IsPaused.Subscribe(x => Console.WriteLine($"Paused: {x}"));
var setupComplete = false;
plc.Observe<byte>(DB100_DBB3)
.Select(v => v == 1)
.Where(_ => !setupComplete)
plc.Observe<byte>(StartLogging)
.Select(v => v == 2)
.Where(log => log && !setupComplete)
.Do(v =>
{
Console.WriteLine($"DB100 DBB3 value: {v}");
plc?.GetTag(DB100_DBB3)?.SetTagPollIng(false);
Console.WriteLine($"Start Logging value: {v}");
plc?.GetTag(StartLogging).SetTagPollIng(false);
})
.Subscribe(async _ =>
{
Expand All @@ -51,11 +51,11 @@
var bytesTagNames1 = await plc?.Value<byte[]>(TagNames1)!;
Console.WriteLine($"bytesTagNames1: {bytesTagNames1?.Length}");
await Task.Delay(500);
var dummy = await plc?.Value<byte>(DB100_DBB3)!;
var dummy = await plc?.Value<byte>(StartLogging)!;
Console.WriteLine($"dummy: {dummy}");
await Task.Delay(500);
Console.WriteLine("Setup complete");
plc?.GetTag(TagValues)?.SetTagPollIng(true);
plc?.GetTag(TagValues).SetTagPollIng(true);
setupComplete = true;
});
plc.Observe<double[]>(TagValues)
Expand Down
10 changes: 3 additions & 7 deletions src/S7PlcRx/Core/S7SocketRx.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
Expand Down Expand Up @@ -152,7 +151,7 @@ public S7SocketRx(string ip, CpuType plcType, short rack, short slot)
IDisposable tim;
_isAvailable = null;
var count = 0;
tim = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1)).Subscribe(_ =>
tim = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(10)).Subscribe(_ =>
{
count++;
if (_isAvailable == null || !_isAvailable.HasValue || (count == 1 && !_isAvailable.Value) || (count == 10 && _isAvailable.Value))
Expand All @@ -170,10 +169,7 @@ public S7SocketRx(string ip, CpuType plcType, short rack, short slot)
try
{
var result = ping.Send(IP);
if (result != null)
{
_isAvailable = result?.Status == IPStatus.Success;
}
_isAvailable = result?.Status == IPStatus.Success;
}
catch (PingException)
{
Expand All @@ -196,7 +192,7 @@ public S7SocketRx(string ip, CpuType plcType, short rack, short slot)
Observable.Create<bool>(obs =>
{
_isConnected = null;
var tim = Observable.Interval(TimeSpan.FromMilliseconds(500)).Subscribe(_ =>
var tim = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(10)).Subscribe(_ =>
{
if (_socket == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/Enums/CpuType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public enum CpuType
/// <summary>
/// The S71500.
/// </summary>
S71500
S71500,
}
2 changes: 1 addition & 1 deletion src/S7PlcRx/Enums/DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ internal enum DataType
/// <summary>
/// The data block.
/// </summary>
DataBlock = 132
DataBlock = 132,
}
2 changes: 1 addition & 1 deletion src/S7PlcRx/Enums/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ public enum ErrorCode
/// <summary>
/// The write data.
/// </summary>
WriteData = 50
WriteData = 50,
}
2 changes: 1 addition & 1 deletion src/S7PlcRx/Enums/VarType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ internal enum VarType
/// <summary>
/// The counter.
/// </summary>
Counter
Counter,
}
47 changes: 38 additions & 9 deletions src/S7PlcRx/ExtensionsMixins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static class ExtensionsMixins
/// <param name="address">The address.</param>
/// <param name="arrayLength">Length of the array.</param>
/// <returns>A Tag.</returns>
public static ITag AddUpdateTagItem<T>(this IRxS7 @this, string tagName, string address, int? arrayLength = null)
public static (ITag? tag, IRxS7? plc) AddUpdateTagItem<T>(this IRxS7 @this, string tagName, string address, int? arrayLength = null)
{
var tag = default(Tag)!;
var tag = default(Tag);
if (@this is RxS7 plc)
{
if ((typeof(T) == typeof(string) || typeof(T).IsArray) && arrayLength.HasValue)
Expand All @@ -36,7 +36,36 @@ public static ITag AddUpdateTagItem<T>(this IRxS7 @this, string tagName, string
plc.AddUpdateTagItem(tag);
}

return tag;
return (tag, @this);
}

/// <summary>
/// Adds the update tag item.
/// </summary>
/// <typeparam name="T">The Type.</typeparam>
/// <param name="this">The this.</param>
/// <param name="tagName">The tag name.</param>
/// <param name="address">The address.</param>
/// <param name="arrayLength">Length of the array.</param>
/// <returns>A Tag.</returns>
public static (ITag? tag, IRxS7? plc) AddUpdateTagItem<T>(this (ITag? _, IRxS7? plc) @this, string tagName, string address, int? arrayLength = null)
{
var tag = default(Tag);
if (@this.plc is RxS7 plc)
{
if ((typeof(T) == typeof(string) || typeof(T).IsArray) && arrayLength.HasValue)
{
tag = new(tagName, address, typeof(T), arrayLength.Value);
}
else
{
tag = new(tagName, address, typeof(T));
}

plc.AddUpdateTagItem(tag);
}

return (tag, @this.plc);
}

/// <summary>
Expand All @@ -45,9 +74,9 @@ public static ITag AddUpdateTagItem<T>(this IRxS7 @this, string tagName, string
/// <param name="this">The instance of tag.</param>
/// <param name="polling">if set to <c>true</c> [poll].</param>
/// <returns>The instance.</returns>
public static ITag? SetTagPollIng(this ITag? @this, bool polling = true)
public static (ITag? tag, IRxS7? plc) SetTagPollIng(this (ITag? tag, IRxS7? plc) @this, bool polling = true)
{
@this?.SetDoNotPoll(!polling);
@this.tag?.SetDoNotPoll(!polling);
return @this;
}

Expand All @@ -57,11 +86,11 @@ public static ITag AddUpdateTagItem<T>(this IRxS7 @this, string tagName, string
/// <param name="this">The rx s7 plc instance.</param>
/// <param name="tagName">Name of the tag.</param>
/// <returns>The instance of tag.</returns>
public static ITag? GetTag(this IRxS7 @this, string tagName) =>
public static (ITag? tag, IRxS7? plc) GetTag(this IRxS7 @this, string tagName) =>
@this?.TagList[tagName!] switch
{
Tag tag => tag,
_ => default
Tag tag => (tag, @this),
_ => (default, @this)
};

/// <summary>
Expand Down Expand Up @@ -90,7 +119,7 @@ public static IObservable<IDictionary<string, TValue>> TagToDictionary<TValue>(t
IDictionary<string, TValue> tagValues = new Dictionary<string, TValue>();
return source
.Where(t => t != null && t?.Value is TValue)
.Select(t => (Name: t!.Name!, Value: (TValue)t!.Value))
.Select(t => (Name: t!.Name!, Value: (TValue)t!.Value!))
.Where(t => t.Value != null)
.Select(t =>
{
Expand Down
1 change: 0 additions & 1 deletion src/S7PlcRx/IRxS7.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Reactive.Disposables;
using S7PlcRx.Enums;

Expand Down
2 changes: 0 additions & 2 deletions src/S7PlcRx/PlcTypes/ByteArray.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;

namespace S7PlcRx.PlcTypes;

internal class ByteArray
Expand Down
2 changes: 0 additions & 2 deletions src/S7PlcRx/PlcTypes/Class.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/S7PlcRx/PlcTypes/Conversion.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Globalization;

namespace S7PlcRx.PlcTypes;
Expand Down
4 changes: 1 addition & 3 deletions src/S7PlcRx/PlcTypes/Counter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down Expand Up @@ -51,7 +49,7 @@ public static ushort[] ToArray(byte[] bytes)
public static byte[] ToByteArray(ushort value)
{
var bytes = new byte[2];
var x = 2;
const int x = 2;
long valLong = value;
for (var cnt = 0; cnt < x; cnt++)
{
Expand Down
4 changes: 1 addition & 3 deletions src/S7PlcRx/PlcTypes/DInt.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down Expand Up @@ -70,7 +68,7 @@ public static int[] ToArray(byte[] bytes)
public static byte[] ToByteArray(int value)
{
var bytes = new byte[4];
var x = 4;
const int x = 4;
long valLong = value;
for (var cnt = 0; cnt < x; cnt++)
{
Expand Down
4 changes: 1 addition & 3 deletions src/S7PlcRx/PlcTypes/DWord.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down Expand Up @@ -53,7 +51,7 @@ public static uint[] ToArray(byte[] bytes)
public static byte[] ToByteArray(uint value)
{
var bytes = new byte[4];
var x = 4;
const int x = 4;
long valLong = value;
for (var cnt = 0; cnt < x; cnt++)
{
Expand Down
4 changes: 1 addition & 3 deletions src/S7PlcRx/PlcTypes/Int.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down Expand Up @@ -68,7 +66,7 @@ public static short[] ToArray(byte[] bytes)
public static byte[] ToByteArray(short value)
{
var bytes = new byte[2];
var x = 2;
const int x = 2;
long valLong = value;
for (var cnt = 0; cnt < x; cnt++)
{
Expand Down
4 changes: 1 addition & 3 deletions src/S7PlcRx/PlcTypes/Real.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down Expand Up @@ -37,7 +35,7 @@ public static double FromByteArray(byte[] bytes)

// first sign
var vz = int.Parse(txt.Substring(0, 1));
var exd = Conversion.BinStringToInt32(txt.Substring(1, 8));
var exd = txt.Substring(1, 8).BinStringToInt32();
var ma = txt.Substring(9, 23);
var mantisse = 1d;
var faktor = 1d;
Expand Down
2 changes: 0 additions & 2 deletions src/S7PlcRx/PlcTypes/Struct.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/S7PlcRx/PlcTypes/Timer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions src/S7PlcRx/PlcTypes/Word.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace S7PlcRx.PlcTypes;

/// <summary>
Expand Down Expand Up @@ -51,7 +49,7 @@ public static ushort[] ToArray(byte[] bytes)
public static byte[] ToByteArray(ushort value)
{
var bytes = new byte[2];
var x = 2;
const int x = 2;
long valLong = value;
for (var cnt = 0; cnt < x; cnt++)
{
Expand Down
Loading

0 comments on commit b2ef142

Please sign in to comment.