Skip to content

Commit

Permalink
update UI with an unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
kilian-kier committed Jan 22, 2023
1 parent e0df257 commit cacdb9d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 98 deletions.
179 changes: 97 additions & 82 deletions MQTT_GUI/MQTT/models/MQTT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>();
Payload = Array.Empty<byte>();
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<byte>();
Payload = Array.Empty<byte>();
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<byte>();
}
catch (Exception)
{
Header[i] = data[1 + remainingLengthBytes + i];
// ignored
return;
}

Payload = Array.Empty<byte>();
}

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;
}
}
}
57 changes: 43 additions & 14 deletions MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SubscriptionsViewModel : ObservableObject
{
public static BindingList<SubscriptionsViewModel> Roots { get; } = new BindingList<SubscriptionsViewModel>();
public string Topic { get; }
private SubscriptionsViewModel _parent { get; set; }
private string _message = "";

public string Message
Expand All @@ -30,6 +31,7 @@ public SubscriptionsViewModel(string topic, string message)
{
Topic = topic;
Message = message;
_parent = null;
Items = new ObservableCollection<SubscriptionsViewModel>();
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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;
}
}
}
}
3 changes: 1 addition & 2 deletions MQTT_GUI/MVVM/Views/SubscribeView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit cacdb9d

Please sign in to comment.