|
|
|
@ -1,17 +1,9 @@ |
|
|
|
using System; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Net; |
|
|
|
using System.Linq; |
|
|
|
|
|
|
|
using System.Text; |
|
|
|
|
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Windows; |
|
|
|
using System.Windows; |
|
|
|
using System.Windows.Controls; |
|
|
|
|
|
|
|
using System.Windows.Data; |
|
|
|
|
|
|
|
using System.Windows.Documents; |
|
|
|
|
|
|
|
using System.Windows.Input; |
|
|
|
|
|
|
|
using System.Windows.Media; |
|
|
|
using System.Windows.Media; |
|
|
|
using System.Windows.Media.Imaging; |
|
|
|
using RobotClientWpf.Utilities; |
|
|
|
using System.Windows.Navigation; |
|
|
|
|
|
|
|
using System.Windows.Shapes; |
|
|
|
|
|
|
|
using RobotLib; |
|
|
|
using RobotLib; |
|
|
|
|
|
|
|
|
|
|
|
namespace RobotClientWpf |
|
|
|
namespace RobotClientWpf |
|
|
|
@ -21,11 +13,24 @@ namespace RobotClientWpf |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public partial class MainWindow : Window |
|
|
|
public partial class MainWindow : Window |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
private ChallengeFactory challenge; |
|
|
|
|
|
|
|
private bool manualDisconnect; |
|
|
|
|
|
|
|
|
|
|
|
public MainWindow() |
|
|
|
public MainWindow() |
|
|
|
{ |
|
|
|
{ |
|
|
|
InitializeComponent(); |
|
|
|
InitializeComponent(); |
|
|
|
Robot.Instance.Connect("host", 1818); |
|
|
|
this.challenge = new ChallengeFactory(); |
|
|
|
Robot.Instance.Battery.BatteryChanged += BatteryChanged; |
|
|
|
this.challenge.PublisherSubscriber.ConnectionStateChanged += PublisherSubscriber_ConnectionStateChanged; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void PublisherSubscriber_ConnectionStateChanged(object? sender, bool e) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if(e == false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (manualDisconnect == true) manualDisconnect = false; return; |
|
|
|
|
|
|
|
this.DisplayBottomMessage(MessageSeverity.Error, "Connection to MQTT broker lost."); |
|
|
|
|
|
|
|
this.SetIpFieldsState(true, true, "Connect"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void BatteryChanged(object? sender, BatteryEventArgs e) |
|
|
|
private void BatteryChanged(object? sender, BatteryEventArgs e) |
|
|
|
@ -37,13 +42,90 @@ namespace RobotClientWpf |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
labelBattery.Content = $"Battery: {e.Voltage} V"; |
|
|
|
//labelBattery.Content = $"Battery: {e.Voltage} V"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void buttonBuzz_Click(object sender, RoutedEventArgs e) |
|
|
|
private void buttonBuzz_Click(object sender, RoutedEventArgs e) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Robot.Instance.Buzzer.Beep(300, 500); |
|
|
|
//Robot.Instance.Buzzer.Beep(300, 500); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void btnConnectDisconnect_Click(object sender, RoutedEventArgs e) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Task.Run(delegate () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.ClearBottomMessage(); |
|
|
|
|
|
|
|
if (this.challenge.PublisherSubscriber.IsConnected) // is connected => disconnect |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
manualDisconnect = true; // prevent wrong message |
|
|
|
|
|
|
|
this.challenge.PublisherSubscriber.Disconnect(); |
|
|
|
|
|
|
|
this.DisplayBottomMessage(MessageSeverity.Success, "Disconnected from MQTT broker."); |
|
|
|
|
|
|
|
this.SetIpFieldsState(true, true, "Connect"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else // is not yet connected => connect |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.SetIpFieldsState(false, false, "Connecting..."); |
|
|
|
|
|
|
|
string ipAddress = UIAccessHelpers.GetTextboxText(tbIp); |
|
|
|
|
|
|
|
if (IPAddress.TryParse(ipAddress, out _)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var connectionSuccess = this.challenge.PublisherSubscriber.Connect(ipAddress); |
|
|
|
|
|
|
|
if (connectionSuccess) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.DisplayBottomMessage(MessageSeverity.Success, "Successfully connected to MQTT broker."); |
|
|
|
|
|
|
|
this.SetIpFieldsState(false, true, "Disconnect"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.DisplayBottomMessage(MessageSeverity.Error, $"Failed to connect to MQTT broker on address {ipAddress}."); |
|
|
|
|
|
|
|
this.SetIpFieldsState(true, true, "Connect"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.DisplayBottomMessage(MessageSeverity.Error, $"Unable to parse MQTT Broker IP address {ipAddress}."); |
|
|
|
|
|
|
|
this.SetIpFieldsState(true, true, "Connect"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SetIpFieldsState(bool ipTextbox, bool connectDisconnectButton, string buttonText) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
UIAccessHelpers.SetButtonState(btnConnectDisconnect, connectDisconnectButton); |
|
|
|
|
|
|
|
UIAccessHelpers.SetTextboxState(tbIp, ipTextbox); |
|
|
|
|
|
|
|
UIAccessHelpers.SetButtonContent(btnConnectDisconnect, buttonText); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void DisplayBottomMessage(MessageSeverity severity, string message) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Brush col; |
|
|
|
|
|
|
|
switch (severity) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
case MessageSeverity.Success: |
|
|
|
|
|
|
|
col = Brushes.Green; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case MessageSeverity.Warning: |
|
|
|
|
|
|
|
col = Brushes.Orange; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case MessageSeverity.Error: |
|
|
|
|
|
|
|
col = Brushes.Red; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case MessageSeverity.Information: |
|
|
|
|
|
|
|
col = Brushes.Blue; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case MessageSeverity.Unknown: |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
col = Brushes.DarkGray; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UIAccessHelpers.SetTextblockTextAndForegroundColor(tbBottomMessage, message, col); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ClearBottomMessage() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.DisplayBottomMessage(MessageSeverity.Information, ""); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|