From cacdb9dd86bfe7eba64b10ef4daf30b20a15a79e Mon Sep 17 00:00:00 2001 From: Kilian Kier Date: Sun, 22 Jan 2023 13:43:04 +0100 Subject: [PATCH] update UI with an unsubscribe --- MQTT_GUI/MQTT/models/MQTT.cs | 179 ++++++++++-------- .../MVVM/ViewModel/SubscriptionsViewModel.cs | 57 ++++-- MQTT_GUI/MVVM/Views/SubscribeView.xaml.cs | 3 +- 3 files changed, 141 insertions(+), 98 deletions(-) diff --git a/MQTT_GUI/MQTT/models/MQTT.cs b/MQTT_GUI/MQTT/models/MQTT.cs index ffd38c4..157b9c2 100644 --- a/MQTT_GUI/MQTT/models/MQTT.cs +++ b/MQTT_GUI/MQTT/models/MQTT.cs @@ -23,118 +23,133 @@ public byte[] GetBytes() public void FromBytes(byte[] data) { - if (data.Length == 0) - return; - ControlHeader = data[0]; - const byte continuation = 0b10000000; - var remainingLengthBytes = 1; - if (data.Length == 2) + try { - RemainingLength = new[] {data[1]}; - Header = Array.Empty(); - Payload = Array.Empty(); - return; - } + if (data.Length == 0) + return; + ControlHeader = data[0]; + const byte continuation = 0b10000000; + var remainingLengthBytes = 1; + if (data.Length == 2) + { + RemainingLength = new[] {data[1]}; + Header = Array.Empty(); + Payload = Array.Empty(); + return; + } - for (var i = 2; i < 5; i++) - { - var check = data[i] & continuation; - if (check != 0) + for (var i = 2; i < 5; i++) { - remainingLengthBytes++; + var check = data[i] & continuation; + if (check != 0) + { + remainingLengthBytes++; + } + else + { + break; + } } - else + + RemainingLength = new byte[remainingLengthBytes]; + for (var i = 0; i < remainingLengthBytes; i++) { - break; + RemainingLength[i] = data[1 + i]; } - } - RemainingLength = new byte[remainingLengthBytes]; - for (var i = 0; i < remainingLengthBytes; i++) - { - RemainingLength[i] = data[1 + i]; - } + var remainingLength = 0; + if (remainingLengthBytes == 1) + { + remainingLength = RemainingLength[0]; + } + else + { + remainingLength = BitConverter.ToInt32(RemainingLength, 0); + } - var remainingLength = 0; - if (remainingLengthBytes == 1) - { - remainingLength = RemainingLength[0]; - } - else - { - remainingLength = BitConverter.ToInt32(RemainingLength, 0); - } + Header = new byte[remainingLength]; - Header = new byte[remainingLength]; + for (var i = 0; i < remainingLength; i++) + { + Header[i] = data[1 + remainingLengthBytes + i]; + } - for (var i = 0; i < remainingLength; i++) + Payload = Array.Empty(); + } + catch (Exception) { - Header[i] = data[1 + remainingLengthBytes + i]; + // ignored + return; } - - Payload = Array.Empty(); } public int FromSubscribedBytes(byte[] data) { - if (data.Length == 0) - return 0; - ControlHeader = data[0]; - const byte continuation = 0b10000000; - var remainingByteLength = 1; - for (var i = 2; i < 5; i++) + try { - var check = data[i] & continuation; - if (check != 0) + if (data.Length == 0) + return 0; + ControlHeader = data[0]; + const byte continuation = 0b10000000; + var remainingByteLength = 1; + for (var i = 2; i < 5; i++) { - remainingByteLength++; + var check = data[i] & continuation; + if (check != 0) + { + remainingByteLength++; + } + else + { + break; + } } - else + + RemainingLength = new byte[remainingByteLength]; + for (var i = 0; i < remainingByteLength; i++) { - break; + RemainingLength[i] = data[1 + i]; } - } - RemainingLength = new byte[remainingByteLength]; - for (var i = 0; i < remainingByteLength; i++) - { - RemainingLength[i] = data[1 + i]; - } + var remainingLength = 0; + if (remainingByteLength == 1) + { + remainingLength = RemainingLength[0]; + } + else + { + var bytes = new byte[remainingLength]; + for (var i = 0; i < remainingByteLength; i++) + { + bytes[i] = RemainingLength[remainingLength - 1 - i]; + } - var remainingLength = 0; - if (remainingByteLength == 1) - { - remainingLength = RemainingLength[0]; - } - else - { - var bytes = new byte[remainingLength]; - for (var i = 0; i < remainingByteLength; i++) + remainingLength = BitConverter.ToInt32(bytes, 0); + } + + var headerLength = + BitConverter.ToInt16(new[] {data[remainingByteLength + 2], data[remainingByteLength + 1]}, 0); + Header = new byte[headerLength + 1]; + Header[0] = data[remainingByteLength + 1]; + for (var i = remainingByteLength + 2; i < remainingByteLength + 2 + headerLength + 1; i++) { - bytes[i] = RemainingLength[remainingLength - 1 - i]; + Header[i - remainingByteLength - 2] = data[i]; } - remainingLength = BitConverter.ToInt32(bytes, 0); - } + var payloadLength = remainingLength - headerLength - 2; + Payload = new byte[payloadLength]; + var idx = remainingByteLength + headerLength + 3; + for (var i = idx; i < idx + payloadLength; i++) + { + Payload[i - remainingByteLength - headerLength - 3] = data[i]; + } - var headerLength = - BitConverter.ToInt16(new[] {data[remainingByteLength + 2], data[remainingByteLength + 1]}, 0); - Header = new byte[headerLength + 1]; - Header[0] = data[remainingByteLength + 1]; - for (var i = remainingByteLength + 2; i < remainingByteLength + 2 + headerLength + 1; i++) - { - Header[i - remainingByteLength - 2] = data[i]; + return idx + payloadLength; } - - var payloadLength = remainingLength - headerLength - 2; - Payload = new byte[payloadLength]; - var idx = remainingByteLength + headerLength + 3; - for (var i = idx; i < idx + payloadLength; i++) + catch (Exception) { - Payload[i - remainingByteLength - headerLength - 3] = data[i]; + return 0; } - - return idx + payloadLength; } } } \ No newline at end of file diff --git a/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs b/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs index 26a44ca..ead09d4 100644 --- a/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs +++ b/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs @@ -12,6 +12,7 @@ public class SubscriptionsViewModel : ObservableObject { public static BindingList Roots { get; } = new BindingList(); public string Topic { get; } + private SubscriptionsViewModel _parent { get; set; } private string _message = ""; public string Message @@ -30,6 +31,7 @@ public SubscriptionsViewModel(string topic, string message) { Topic = topic; Message = message; + _parent = null; Items = new ObservableCollection(); } @@ -81,7 +83,12 @@ private void Insert(SubscriptionsViewModel parent, string topic, string message) item.Message = message; return; } - parent.Items.Add(new SubscriptionsViewModel(topic, message)); + + var child = new SubscriptionsViewModel(topic, message) + { + _parent = parent + }; + parent.Items.Add(child); return; } @@ -94,13 +101,28 @@ private void Insert(SubscriptionsViewModel parent, string topic, string message) Insert(item, t, message); return; } - var node = new SubscriptionsViewModel(sub, ""); + + var node = new SubscriptionsViewModel(sub, "") + { + _parent = parent + }; parent.Items.Add(node); Insert(node, t, message); } public static void Remove(string topic) { + if (topic == "#") + { + foreach (var item in Roots.ToList()) + { + if (item.Topic.StartsWith("$")) + return; + Roots.Remove(item); + } + + return; + } var sub = topic.IndexOf('/'); if (sub == -1) { @@ -124,12 +146,21 @@ public static void Remove(string topic) { var t = topic.Substring(sub + 1); RecRemove(root, t); + if (root.Items.Count != 0) + return; + Roots.Remove(root); } } private static void RecRemove(SubscriptionsViewModel parent, string topic) { - // TODO: ref + if (topic == "#") + { + parent.Items.Clear(); + parent._parent?.Items.Remove(parent); + return; + } + var next = topic.IndexOf('/'); if (next == -1) { @@ -141,26 +172,24 @@ private static void RecRemove(SubscriptionsViewModel parent, string topic) break; } - if (parent.Items.Count == 0) - { - parent = null; - } + if (parent.Items.Count != 0) return; + + parent._parent?.Items.Remove(parent); return; } var sub = topic.Substring(0, next); var t = topic.Substring(next + 1); - for (var i = 0; i < parent.Items.Count; i++) + foreach (var item in parent.Items) { - if (parent.Items[i].Topic != sub) + if (item.Topic != sub) continue; - RecRemove(parent.Items[i], t); + RecRemove(item, t); + if (parent.Items.Count != 0) return; + + parent._parent?.Items.Remove(parent); break; } - if (parent.Items.Count == 0) - { - parent = null; - } } } } \ No newline at end of file diff --git a/MQTT_GUI/MVVM/Views/SubscribeView.xaml.cs b/MQTT_GUI/MVVM/Views/SubscribeView.xaml.cs index dc9d078..045e645 100644 --- a/MQTT_GUI/MVVM/Views/SubscribeView.xaml.cs +++ b/MQTT_GUI/MVVM/Views/SubscribeView.xaml.cs @@ -120,9 +120,8 @@ private void ButtonUnsubscribe(object sender, RoutedEventArgs e) SubSuc.Visibility = Visibility.Visible; SubscribeViewModel.Unsubscribable.Remove(topic); UnsubscribeBox.SelectedIndex = 0; + SubscriptionsViewModel.Remove(topic); }); - // TODO: remove - // SubscriptionsViewModel.Remove(topic); }).Start(); } }