diff --git a/MQTT_GUI/App.xaml b/MQTT_GUI/App.xaml index b635e5f..c2ccd12 100644 --- a/MQTT_GUI/App.xaml +++ b/MQTT_GUI/App.xaml @@ -1,41 +1,40 @@  - - - - - - - - - + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - + \ No newline at end of file diff --git a/MQTT_GUI/Core/ObservableObject.cs b/MQTT_GUI/Core/ObservableObject.cs index ab5b889..29b33e0 100644 --- a/MQTT_GUI/Core/ObservableObject.cs +++ b/MQTT_GUI/Core/ObservableObject.cs @@ -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)); } diff --git a/MQTT_GUI/Core/RelayCommand.cs b/MQTT_GUI/Core/RelayCommand.cs index b31efb7..8aa22b1 100644 --- a/MQTT_GUI/Core/RelayCommand.cs +++ b/MQTT_GUI/Core/RelayCommand.cs @@ -5,8 +5,8 @@ namespace MQTT_GUI.Core { public class RelayCommand : ICommand { - private Action _execute; - private Func _canExecute; + private readonly Action _execute; + private readonly Func _canExecute; public bool CanExecute(object parameter) { diff --git a/MQTT_GUI/MQTT/MQTTClient.cs b/MQTT_GUI/MQTT/MQTTClient.cs index 2e10019..fa6f1a6 100644 --- a/MQTT_GUI/MQTT/MQTTClient.cs +++ b/MQTT_GUI/MQTT/MQTTClient.cs @@ -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() @@ -34,7 +31,7 @@ public bool CreateTcpConnection() TcpClient = new TcpClient(); try { - TcpClient.Connect(Ip, Port); + TcpClient.Connect(_ip, _port); } catch (Exception) { @@ -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(); diff --git a/MQTT_GUI/MQTT/ReceiveThread.cs b/MQTT_GUI/MQTT/ReceiveThread.cs index 35a52ad..f3904fa 100644 --- a/MQTT_GUI/MQTT/ReceiveThread.cs +++ b/MQTT_GUI/MQTT/ReceiveThread.cs @@ -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; @@ -77,9 +75,6 @@ public ReceiveThread(MQTTClient client) case publish: SubQueue.Enqueue(packet); break; - default: - Debug.Write(""); - break; } } } diff --git a/MQTT_GUI/MQTT/messages/Connect.cs b/MQTT_GUI/MQTT/messages/Connect.cs index 1f8f98c..0136864 100644 --- a/MQTT_GUI/MQTT/messages/Connect.cs +++ b/MQTT_GUI/MQTT/messages/Connect.cs @@ -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, diff --git a/MQTT_GUI/MQTT/messages/Publish.cs b/MQTT_GUI/MQTT/messages/Publish.cs index af42b3f..5291874 100644 --- a/MQTT_GUI/MQTT/messages/Publish.cs +++ b/MQTT_GUI/MQTT/messages/Publish.cs @@ -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); diff --git a/MQTT_GUI/MQTT/messages/Subscribe.cs b/MQTT_GUI/MQTT/messages/Subscribe.cs index 31aa8b1..93196e5 100644 --- a/MQTT_GUI/MQTT/messages/Subscribe.cs +++ b/MQTT_GUI/MQTT/messages/Subscribe.cs @@ -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, @@ -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)}; @@ -36,7 +36,7 @@ public Subscribe(string topic, QOS qos) Header = header.ToArray(); - Payload = new[] {(byte) qos}; + Payload = new[] {(byte) qoS}; } } } \ No newline at end of file diff --git a/MQTT_GUI/MQTT/models/MQTT.cs b/MQTT_GUI/MQTT/models/MQTT.cs index 157b9c2..f53cbba 100644 --- a/MQTT_GUI/MQTT/models/MQTT.cs +++ b/MQTT_GUI/MQTT/models/MQTT.cs @@ -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; @@ -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]; @@ -79,7 +73,6 @@ public void FromBytes(byte[] data) catch (Exception) { // ignored - return; } } diff --git a/MQTT_GUI/MVVM/Controller/SubscriptionsController.cs b/MQTT_GUI/MVVM/Controller/SubscriptionsController.cs index f8a9770..6f186f0 100644 --- a/MQTT_GUI/MVVM/Controller/SubscriptionsController.cs +++ b/MQTT_GUI/MVVM/Controller/SubscriptionsController.cs @@ -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) { @@ -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(); } diff --git a/MQTT_GUI/MVVM/Controller/TopicsController.cs b/MQTT_GUI/MVVM/Controller/TopicsController.cs index 4bf2687..ef1e974 100644 --- a/MQTT_GUI/MVVM/Controller/TopicsController.cs +++ b/MQTT_GUI/MVVM/Controller/TopicsController.cs @@ -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; @@ -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) { @@ -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); @@ -99,6 +96,7 @@ public static void AddTopics(Dispatcher dispatcher) dispatcher.Invoke(() => { context1.ProgressBar = Visibility.Hidden; }); TopicsView.Context = context1; } + _alreadyRunning = false; }).Start(); } diff --git a/MQTT_GUI/MVVM/ViewModel/ConnectViewModel.cs b/MQTT_GUI/MVVM/ViewModel/ConnectViewModel.cs index bf8e861..6bdadb9 100644 --- a/MQTT_GUI/MVVM/ViewModel/ConnectViewModel.cs +++ b/MQTT_GUI/MVVM/ViewModel/ConnectViewModel.cs @@ -1,5 +1,4 @@ using System.Windows; -using System.Windows.Controls; using MQTT_GUI.Core; namespace MQTT_GUI.MVVM.ViewModel @@ -7,7 +6,7 @@ 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 diff --git a/MQTT_GUI/MVVM/ViewModel/MainWindowViewModel.cs b/MQTT_GUI/MVVM/ViewModel/MainWindowViewModel.cs index 1ae0d6e..285f057 100644 --- a/MQTT_GUI/MVVM/ViewModel/MainWindowViewModel.cs +++ b/MQTT_GUI/MVVM/ViewModel/MainWindowViewModel.cs @@ -1,30 +1,27 @@ -using System.Windows; -using System.Windows.Controls; -using MQTT_GUI.Core; -using MQTT_GUI.MVVM.Views; +using MQTT_GUI.Core; namespace MQTT_GUI.MVVM.ViewModel { public class MainWindowViewModel : ObservableObject { - public static RelayCommand ConnectViewCommand { get; set; } - public static RelayCommand PublishViewCommand { get; set; } - public static RelayCommand TopicsViewCommand { get; set; } - public static RelayCommand SubscribeViewCommand { get; set; } - public static RelayCommand SubscriptionsViewCommand { get; set; } + public static RelayCommand ConnectViewCommand { get; private set; } + public static RelayCommand PublishViewCommand { get; private set; } + public static RelayCommand TopicsViewCommand { get; private set; } + public static RelayCommand SubscribeViewCommand { get; private set; } + public static RelayCommand SubscriptionsViewCommand { get; private set; } - public static ConnectViewModel ConnectViewModel { get; set; } - public static PublishViewModel PublishViewModel { get; set; } - public static TopicsViewModel TopicsViewModel { get; set; } - public static SubscribeViewModel SubscribeViewModel { get; set; } - public static SubscriptionsViewModel SubscriptionsViewModel { get; set; } + public static ConnectViewModel ConnectViewModel { get; private set; } + private static PublishViewModel PublishViewModel { get; set; } + public static TopicsViewModel TopicsViewModel { get; private set; } + private static SubscribeViewModel SubscribeViewModel { get; set; } + public static SubscriptionsViewModel SubscriptionsViewModel { get; private set; } private object _currentView; public object CurrentView { get => _currentView; - set + private set { _currentView = value; OnPropertyChanged(); diff --git a/MQTT_GUI/MVVM/ViewModel/PublishViewModel.cs b/MQTT_GUI/MVVM/ViewModel/PublishViewModel.cs index d25add2..2555836 100644 --- a/MQTT_GUI/MVVM/ViewModel/PublishViewModel.cs +++ b/MQTT_GUI/MVVM/ViewModel/PublishViewModel.cs @@ -1,5 +1,4 @@ using System.Collections.ObjectModel; -using System.Windows; using MQTT_GUI.Core; namespace MQTT_GUI.MVVM.ViewModel diff --git a/MQTT_GUI/MVVM/ViewModel/SubscribeViewModel.cs b/MQTT_GUI/MVVM/ViewModel/SubscribeViewModel.cs index f492db5..df66f81 100644 --- a/MQTT_GUI/MVVM/ViewModel/SubscribeViewModel.cs +++ b/MQTT_GUI/MVVM/ViewModel/SubscribeViewModel.cs @@ -1,5 +1,4 @@ using System.Collections.ObjectModel; -using System.Windows.Controls; using MQTT_GUI.Core; namespace MQTT_GUI.MVVM.ViewModel diff --git a/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs b/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs index ead09d4..a6d66e2 100644 --- a/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs +++ b/MQTT_GUI/MVVM/ViewModel/SubscriptionsViewModel.cs @@ -1,10 +1,7 @@ -using System.Collections.Generic; -using System.Collections.ObjectModel; +using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; -using System.Threading; using MQTT_GUI.Core; -using MQTT_GUI.MVVM.Views; namespace MQTT_GUI.MVVM.ViewModel { @@ -12,32 +9,32 @@ public class SubscriptionsViewModel : ObservableObject { public static BindingList Roots { get; } = new BindingList(); public string Topic { get; } - private SubscriptionsViewModel _parent { get; set; } + private SubscriptionsViewModel Parent { get; set; } private string _message = ""; public string Message { get => _message; - set + private set { _message = value; OnPropertyChanged(); } - } + } + public ObservableCollection Items { get; set; } - public SubscriptionsViewModel(string topic, string message) + private SubscriptionsViewModel(string topic, string message) { Topic = topic; Message = message; - _parent = null; + Parent = null; Items = new ObservableCollection(); } public SubscriptionsViewModel() { - } public void AddMessage(string topic, string message) @@ -67,7 +64,7 @@ public void AddMessage(string topic, string message) var t = topic.Substring(sub + 1); Insert(root, t, message); } - + OnPropertyChanged(); } @@ -86,7 +83,7 @@ private void Insert(SubscriptionsViewModel parent, string topic, string message) var child = new SubscriptionsViewModel(topic, message) { - _parent = parent + Parent = parent }; parent.Items.Add(child); return; @@ -104,7 +101,7 @@ private void Insert(SubscriptionsViewModel parent, string topic, string message) var node = new SubscriptionsViewModel(sub, "") { - _parent = parent + Parent = parent }; parent.Items.Add(node); Insert(node, t, message); @@ -123,6 +120,7 @@ public static void Remove(string topic) return; } + var sub = topic.IndexOf('/'); if (sub == -1) { @@ -157,10 +155,10 @@ private static void RecRemove(SubscriptionsViewModel parent, string topic) if (topic == "#") { parent.Items.Clear(); - parent._parent?.Items.Remove(parent); + parent.Parent?.Items.Remove(parent); return; } - + var next = topic.IndexOf('/'); if (next == -1) { @@ -174,7 +172,7 @@ private static void RecRemove(SubscriptionsViewModel parent, string topic) if (parent.Items.Count != 0) return; - parent._parent?.Items.Remove(parent); + parent.Parent?.Items.Remove(parent); return; } @@ -187,9 +185,9 @@ private static void RecRemove(SubscriptionsViewModel parent, string topic) RecRemove(item, t); if (parent.Items.Count != 0) return; - parent._parent?.Items.Remove(parent); + parent.Parent?.Items.Remove(parent); break; } - } + } } } \ No newline at end of file diff --git a/MQTT_GUI/MVVM/ViewModel/TopicsViewModel.cs b/MQTT_GUI/MVVM/ViewModel/TopicsViewModel.cs index f36884a..59fb051 100644 --- a/MQTT_GUI/MVVM/ViewModel/TopicsViewModel.cs +++ b/MQTT_GUI/MVVM/ViewModel/TopicsViewModel.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; +using System.ComponentModel; using System.Linq; using System.Windows; -using System.Windows.Threading; using MQTT_GUI.Core; namespace MQTT_GUI.MVVM.ViewModel @@ -12,17 +8,9 @@ namespace MQTT_GUI.MVVM.ViewModel public class TopicsViewModel : ObservableObject { private static Visibility _progressBar = Visibility.Hidden; - private static BindingList _topics = new BindingList(); + private static readonly BindingList _topics = new BindingList(); - public BindingList Topics - { - get => _topics; - set - { - _topics = value; - OnPropertyChanged(); - } - } + public BindingList Topics => _topics; public Visibility ProgressBar { diff --git a/MQTT_GUI/MVVM/Views/ConnectView.xaml b/MQTT_GUI/MVVM/Views/ConnectView.xaml index f387169..958b812 100644 --- a/MQTT_GUI/MVVM/Views/ConnectView.xaml +++ b/MQTT_GUI/MVVM/Views/ConnectView.xaml @@ -8,28 +8,28 @@ d:DesignWidth="720" Height="700" Background="Transparent"> - + - + - + - - - + + + - + + FontSize="14" /> + FontSize="14" />