From 16847eafd37e391cbbe6344113591038b912dae4 Mon Sep 17 00:00:00 2001 From: krusta4711 Date: Sat, 10 Aug 2024 13:16:56 +0200 Subject: [PATCH 1/3] Add content to se attribute of XML --- .gitignore | 1 + src/ism7mqtt/ISM7/Ism7Client.cs | 7 +++++++ src/ism7mqtt/ISM7/Protocol/Utf8StringWriter .cs | 7 +++++++ src/ism7mqtt/ISM7/Protocol/XmlPayload.cs | 8 ++++---- 4 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 src/ism7mqtt/ISM7/Protocol/Utf8StringWriter .cs diff --git a/.gitignore b/.gitignore index c08ee2f..94ec162 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ Buildlog-*\.txt LaunchSettings.json .vs/ +.vscode Nuget/ packages/ diff --git a/src/ism7mqtt/ISM7/Ism7Client.cs b/src/ism7mqtt/ISM7/Ism7Client.cs index 4963173..7c31f62 100644 --- a/src/ism7mqtt/ISM7/Ism7Client.cs +++ b/src/ism7mqtt/ISM7/Ism7Client.cs @@ -30,6 +30,7 @@ public class Ism7Client private int _nextBundleId = 0; private short _lastKeepAlive = 0; private Stream _sslStream; + private int _nextSequenceId = 1; public int Interval { get; set; } @@ -246,6 +247,7 @@ private async Task SubscribeAsync(string busAddress, CancellationToken cancellat { infoRead.BusAddress = busAddress; infoRead.Intervall = Interval; + infoRead.Seq = generateNextSequenceValue(); } await SendAsync(new TelegramBundleReq { @@ -289,6 +291,7 @@ private async Task LoadInitialValuesAsync(CancellationToken cancellationToken) foreach (var infoRead in infoReads) { infoRead.BusAddress = busAddress; + infoRead.Seq = generateNextSequenceValue(); } await SendAsync(new TelegramBundleReq { @@ -403,5 +406,9 @@ private XmlSerializer GetSerializer() { return _serializers.GetOrAdd(typeof(T), x => new XmlSerializer(x)); } + + private String generateNextSequenceValue() { + return "A;" + _nextSequenceId++; + } } } \ No newline at end of file diff --git a/src/ism7mqtt/ISM7/Protocol/Utf8StringWriter .cs b/src/ism7mqtt/ISM7/Protocol/Utf8StringWriter .cs new file mode 100644 index 0000000..54f3db4 --- /dev/null +++ b/src/ism7mqtt/ISM7/Protocol/Utf8StringWriter .cs @@ -0,0 +1,7 @@ +using System.IO; +using System.Text; + +public sealed class Utf8StringWriter : StringWriter +{ + public override Encoding Encoding => Encoding.UTF8; +} \ No newline at end of file diff --git a/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs b/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs index c7e9974..b576af2 100644 --- a/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs +++ b/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs @@ -13,13 +13,13 @@ public abstract class XmlPayload : IPayload public byte[] Serialize() { - using var sw = new StringWriter(); - var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {Indent = false}); + using var stringWriter = new Utf8StringWriter(); + var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings {Indent = false}); var serializer = _serializers.GetOrAdd(GetType(), x => new XmlSerializer(x)); serializer.Serialize(xmlWriter, this); - sw.Flush(); - var xml = sw.ToString(); + stringWriter.Flush(); + var xml = stringWriter.ToString(); return Encoding.UTF8.GetBytes(xml); } From 59b03572fcc0751e7c54a2c4b7a1a6e9df139f1a Mon Sep 17 00:00:00 2001 From: zivillian Date: Sat, 10 Aug 2024 16:50:29 +0200 Subject: [PATCH 2/3] refactoring --- src/ism7mqtt/ISM7/Ism7Client.cs | 14 ++++++++------ src/ism7mqtt/ISM7/Protocol/XmlPayload.cs | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ism7mqtt/ISM7/Ism7Client.cs b/src/ism7mqtt/ISM7/Ism7Client.cs index 7c31f62..a744083 100644 --- a/src/ism7mqtt/ISM7/Ism7Client.cs +++ b/src/ism7mqtt/ISM7/Ism7Client.cs @@ -247,7 +247,7 @@ private async Task SubscribeAsync(string busAddress, CancellationToken cancellat { infoRead.BusAddress = busAddress; infoRead.Intervall = Interval; - infoRead.Seq = generateNextSequenceValue(); + infoRead.Seq = GenerateNextSequenceValue(); } await SendAsync(new TelegramBundleReq { @@ -291,7 +291,7 @@ private async Task LoadInitialValuesAsync(CancellationToken cancellationToken) foreach (var infoRead in infoReads) { infoRead.BusAddress = busAddress; - infoRead.Seq = generateNextSequenceValue(); + infoRead.Seq = GenerateNextSequenceValue(); } await SendAsync(new TelegramBundleReq { @@ -381,6 +381,12 @@ private string NextBundleId() return id.ToString(); } + private string GenerateNextSequenceValue() + { + var id = Interlocked.Increment(ref _nextSequenceId); + return $"A;{id}"; + } + private IResponse Deserialize(PayloadType type, Stream data) { switch (type) @@ -406,9 +412,5 @@ private XmlSerializer GetSerializer() { return _serializers.GetOrAdd(typeof(T), x => new XmlSerializer(x)); } - - private String generateNextSequenceValue() { - return "A;" + _nextSequenceId++; - } } } \ No newline at end of file diff --git a/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs b/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs index b576af2..3d628c0 100644 --- a/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs +++ b/src/ism7mqtt/ISM7/Protocol/XmlPayload.cs @@ -13,13 +13,13 @@ public abstract class XmlPayload : IPayload public byte[] Serialize() { - using var stringWriter = new Utf8StringWriter(); - var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings {Indent = false}); + using var sw = new Utf8StringWriter(); + var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {Indent = false}); var serializer = _serializers.GetOrAdd(GetType(), x => new XmlSerializer(x)); serializer.Serialize(xmlWriter, this); - stringWriter.Flush(); - var xml = stringWriter.ToString(); + sw.Flush(); + var xml = sw.ToString(); return Encoding.UTF8.GetBytes(xml); } From 397674fc05d0b17baebcde5f6ef16fe4c49aea5c Mon Sep 17 00:00:00 2001 From: zivillian Date: Sat, 10 Aug 2024 16:51:49 +0200 Subject: [PATCH 3/3] rename method --- src/ism7mqtt/ISM7/Ism7Client.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ism7mqtt/ISM7/Ism7Client.cs b/src/ism7mqtt/ISM7/Ism7Client.cs index a744083..25c2b60 100644 --- a/src/ism7mqtt/ISM7/Ism7Client.cs +++ b/src/ism7mqtt/ISM7/Ism7Client.cs @@ -247,7 +247,7 @@ private async Task SubscribeAsync(string busAddress, CancellationToken cancellat { infoRead.BusAddress = busAddress; infoRead.Intervall = Interval; - infoRead.Seq = GenerateNextSequenceValue(); + infoRead.Seq = NextSequenceId(); } await SendAsync(new TelegramBundleReq { @@ -291,7 +291,7 @@ private async Task LoadInitialValuesAsync(CancellationToken cancellationToken) foreach (var infoRead in infoReads) { infoRead.BusAddress = busAddress; - infoRead.Seq = GenerateNextSequenceValue(); + infoRead.Seq = NextSequenceId(); } await SendAsync(new TelegramBundleReq { @@ -381,7 +381,7 @@ private string NextBundleId() return id.ToString(); } - private string GenerateNextSequenceValue() + private string NextSequenceId() { var id = Interlocked.Increment(ref _nextSequenceId); return $"A;{id}";