Skip to content

Commit

Permalink
refactoring and reformating
Browse files Browse the repository at this point in the history
  • Loading branch information
kilian-kier committed Jan 22, 2023
1 parent abc0b22 commit 0662384
Show file tree
Hide file tree
Showing 35 changed files with 241 additions and 362 deletions.
41 changes: 20 additions & 21 deletions MQTT_GUI/App.xaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
<Application x:Class="MQTT_GUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MQTT_GUI"
xmlns:viewmodel="clr-namespace:MQTT_GUI.MVVM.ViewModel"
xmlns:view="clr-namespace:MQTT_GUI.MVVM.Views"
StartupUri="MVVM/Views/MainWindowView.xaml"
Exit="App_OnExit">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/MenuButtonTheme.xaml"/>
<ResourceDictionary Source="Theme/TextBoxTheme.xaml"/>
<ResourceDictionary Source="Theme/ButtonTheme.xaml"/>
<ResourceDictionary Source="Theme/MessagesTheme.xaml"/>
<ResourceDictionary Source="Theme/ComboBoxTheme.xaml"/>
<ResourceDictionary Source="Theme/TreeViewTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/MenuButtonTheme.xaml" />
<ResourceDictionary Source="Theme/TextBoxTheme.xaml" />
<ResourceDictionary Source="Theme/ButtonTheme.xaml" />
<ResourceDictionary Source="Theme/MessagesTheme.xaml" />
<ResourceDictionary Source="Theme/ComboBoxTheme.xaml" />
<ResourceDictionary Source="Theme/TreeViewTheme.xaml" />
</ResourceDictionary.MergedDictionaries>

<DataTemplate DataType="{x:Type viewmodel:ConnectViewModel}">
<view:Connect/>
<view:Connect />
</DataTemplate>

<DataTemplate DataType="{x:Type viewmodel:PublishViewModel}">
<view:PublishView/>
<view:PublishView />
</DataTemplate>

<DataTemplate DataType="{x:Type viewmodel:TopicsViewModel}">
<view:TopicsView/>
<view:TopicsView />
</DataTemplate>

<DataTemplate DataType="{x:Type viewmodel:SubscribeViewModel}">
<view:SubscribeView/>
<view:SubscribeView />
</DataTemplate>

<DataTemplate DataType="{x:Type viewmodel:SubscriptionsViewModel}">
<view:SubscriptionsView/>
<view:SubscriptionsView />
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary>
</Application.Resources>
</Application>
</Application>
2 changes: 1 addition & 1 deletion MQTT_GUI/Core/ObservableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged([CallerMemberName] string propertyName = null)
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Expand Down
4 changes: 2 additions & 2 deletions MQTT_GUI/Core/RelayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace MQTT_GUI.Core
{
public class RelayCommand : ICommand
{
private Action<object> _execute;
private Func<object, bool> _canExecute;
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute;

public bool CanExecute(object parameter)
{
Expand Down
62 changes: 5 additions & 57 deletions MQTT_GUI/MQTT/MQTTClient.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using MQTT_GUI.MQTT.messages;

namespace MQTT_GUI.MQTT
{
public class MQTTClient
{
public static MQTTClient Client;
public static string Ip;
public static int Port;
private static string _ip;
private static int _port;
public ReceiveThread Receiver;
public TcpClient TcpClient;

public MQTTClient(string ipAddress, int port)
{
Ip = ipAddress;
Port = port;
_ip = ipAddress;
_port = port;
}

public MQTTClient()
Expand All @@ -34,7 +31,7 @@ public bool CreateTcpConnection()
TcpClient = new TcpClient();
try
{
TcpClient.Connect(Ip, Port);
TcpClient.Connect(_ip, _port);
}
catch (Exception)
{
Expand All @@ -46,55 +43,6 @@ public bool CreateTcpConnection()
return true;
}

/*public MQTTClient(string ipAddress, int port, string clientId)
{
_ip = ipAddress;
_port = port;
var connect = new Connect(clientId);
SendObject(connect);
var connAck = Receiver.GetConnAck();
if (connAck.Header == null || connAck.Header.Length >= 2 && connAck.Header[1] != 0)
{
Console.Error.Write("Can not connect");
Environment.Exit(1);
}
var publish = new Publish("shellies/shelly1/relay/0/command", "toggle");
SendObject(publish);
publish = new Publish("shellies/shelly1/relay/0/command", "on");
SendObject(publish);
publish = new Publish("shellies/shelly1/relay/0/command", "on");
SendObject(publish);
publish = new Publish("shellies/shelly1/relay/0/command", "off");
SendObject(publish);
var subscribe = new Subscribe("esp32/temperature");
SendObject(subscribe);
_ = Receiver.GetSubAck();
subscribe = new Subscribe("esp32/humidity");
SendObject(subscribe);
_ = Receiver.GetSubAck();
subscribe = new Subscribe("esp32/heatIndex");
SendObject(subscribe);
_ = Receiver.GetSubAck();
while (true)
{
if (Receiver.SubQueue.Count == 0)
{
Thread.Sleep(100);
continue;
}
var subscribed = Receiver.SubQueue.Dequeue();
var topic = Encoding.ASCII.GetString(subscribed.Header, 1, subscribed.Header.Length - 1);
var msg = Encoding.ASCII.GetString(subscribed.Payload, 0, subscribed.Payload.Length);
Console.Out.WriteLine(topic + ": " + msg);
}
}*/

public void SendObject(models.MQTT command)
{
var stream = TcpClient.GetStream();
Expand Down
5 changes: 0 additions & 5 deletions MQTT_GUI/MQTT/ReceiveThread.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Timers;
using System.Windows;
using MQTT_GUI.MQTT.messages;
using MQTT_GUI.MQTT.models;
using Timer = System.Timers.Timer;
Expand Down Expand Up @@ -77,9 +75,6 @@ public ReceiveThread(MQTTClient client)
case publish:
SubQueue.Enqueue(packet);
break;
default:
Debug.Write("");
break;
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions MQTT_GUI/MQTT/messages/Connect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ public class Connect : models.MQTT
{
private enum ConnectFlags : byte
{
CleanSession = 2,
Password = 2 ^ 6,
UserName = 2 ^ 7,
CleanSession = 2
}

public Connect(string clientId)
{
ControlHeader = (byte) MessageType.Connect;
RemainingLength = new byte[] {(byte)(12 + clientId.Length)};
RemainingLength = new[] {(byte) (12 + clientId.Length)};
Header = new byte[]
{
0x0, 0x4,
Expand Down
4 changes: 2 additions & 2 deletions MQTT_GUI/MQTT/messages/Publish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class Publish : models.MQTT
{
private static int _idCounter = 1;

public enum QOS : byte
public enum Qos : byte
{
ExactlyOnce = 4,
AtLeastOnce = 2,
AtMostOnce = 0
}

public Publish(string topic, string message, QOS qos)
public Publish(string topic, string message, Qos qos)
{
ControlHeader = (byte) (MessageType.Publish + (byte) qos);

Expand Down
6 changes: 3 additions & 3 deletions MQTT_GUI/MQTT/messages/Subscribe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MQTT_GUI.MQTT.messages
{
public class Subscribe : models.MQTT
{
public enum QOS : byte
public enum QoS : byte
{
ExactlyOnce = 2,
AtLeastOnce = 1,
Expand All @@ -16,7 +16,7 @@ public enum QOS : byte

private static short _idCounter = 1;

public Subscribe(string topic, QOS qos)
public Subscribe(string topic, QoS qoS)
{
ControlHeader = (byte) MessageType.Subscribe + 2;
RemainingLength = new[] {(byte) (5 + topic.Length)};
Expand All @@ -36,7 +36,7 @@ public Subscribe(string topic, QOS qos)

Header = header.ToArray();

Payload = new[] {(byte) qos};
Payload = new[] {(byte) qoS};
}
}
}
15 changes: 4 additions & 11 deletions MQTT_GUI/MQTT/models/MQTT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace MQTT_GUI.MQTT.models
public class MQTT
{
public byte ControlHeader;
public byte[] RemainingLength;
protected byte[] RemainingLength;
public byte[] Header;
public byte[] Payload;

Expand Down Expand Up @@ -57,15 +57,9 @@ public void FromBytes(byte[] data)
RemainingLength[i] = data[1 + i];
}

var remainingLength = 0;
if (remainingLengthBytes == 1)
{
remainingLength = RemainingLength[0];
}
else
{
remainingLength = BitConverter.ToInt32(RemainingLength, 0);
}
var remainingLength = remainingLengthBytes == 1
? RemainingLength[0]
: BitConverter.ToInt32(RemainingLength, 0);

Header = new byte[remainingLength];

Expand All @@ -79,7 +73,6 @@ public void FromBytes(byte[] data)
catch (Exception)
{
// ignored
return;
}
}

Expand Down
15 changes: 2 additions & 13 deletions MQTT_GUI/MVVM/Controller/SubscriptionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
using System.Windows.Threading;
using MQTT_GUI.MQTT;
using MQTT_GUI.MVVM.ViewModel;
using MQTT_GUI.MVVM.Views;

namespace MQTT_GUI.MVVM.Controller
{
public class SubscriptionsController
public static class SubscriptionsController
{
public static void SubscriptionThread(Dispatcher dispatcher)
{
Expand All @@ -23,17 +22,7 @@ public static void SubscriptionThread(Dispatcher dispatcher)
var subscribed = MQTTClient.Client.Receiver.SubQueue.Dequeue();
var topic = Encoding.ASCII.GetString(subscribed.Header, 1, subscribed.Header.Length - 1);
var message = Encoding.ASCII.GetString(subscribed.Payload, 0, subscribed.Payload.Length);
dispatcher.Invoke(() =>
{
/*while (SubscriptionsView.Context == null)
{
Thread.Sleep(100);
}
var context = (SubscriptionsViewModel) SubscriptionsView.Context;
SubscriptionsView.Context = null;*/
MainWindowViewModel.SubscriptionsViewModel.AddMessage(topic, message);
// SubscriptionsView.Context = context;
});
dispatcher.Invoke(() => { MainWindowViewModel.SubscriptionsViewModel.AddMessage(topic, message); });
}
}).Start();
}
Expand Down
12 changes: 5 additions & 7 deletions MQTT_GUI/MVVM/Controller/TopicsController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Threading;
using MQTT_GUI.MQTT;
using MQTT_GUI.MQTT.messages;
Expand All @@ -14,9 +11,9 @@

namespace MQTT_GUI.MVVM.Controller
{
public class TopicsController
public static class TopicsController
{
private static bool _alreadyRunning = false;
private static bool _alreadyRunning;

public static void AddTopics(Dispatcher dispatcher)
{
Expand Down Expand Up @@ -50,8 +47,8 @@ public static void AddTopics(Dispatcher dispatcher)
return;
}
var subscribeToSystemTopics = new Subscribe("$SYS/#", Subscribe.QOS.AtLeastOnce);
var subscribeToOtherTopics = new Subscribe("#", Subscribe.QOS.AtLeastOnce);
var subscribeToSystemTopics = new Subscribe("$SYS/#", Subscribe.QoS.AtLeastOnce);
var subscribeToOtherTopics = new Subscribe("#", Subscribe.QoS.AtLeastOnce);
topicsClient.SendObject(subscribeToSystemTopics);
topicsClient.SendObject(subscribeToOtherTopics);
Expand Down Expand Up @@ -99,6 +96,7 @@ public static void AddTopics(Dispatcher dispatcher)
dispatcher.Invoke(() => { context1.ProgressBar = Visibility.Hidden; });
TopicsView.Context = context1;
}
_alreadyRunning = false;
}).Start();
}
Expand Down
3 changes: 1 addition & 2 deletions MQTT_GUI/MVVM/ViewModel/ConnectViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Windows;
using System.Windows.Controls;
using MQTT_GUI.Core;

namespace MQTT_GUI.MVVM.ViewModel
{
public class ConnectViewModel : ObservableObject
{
private static string _connectedBroker = "Not connected";
private static bool _buttonEnabled = false;
private static bool _buttonEnabled;
private Visibility _showProgressBar = Visibility.Collapsed;

public string ConnectedBroker
Expand Down
Loading

0 comments on commit 0662384

Please sign in to comment.