implemented automatically saved settings in UI,

implemented config of splitflap hw ids and display splitflap message
main
Jonas Arnold 4 years ago
parent a3fc254f44
commit a526c7f01f
  1. 15
      ADIS_Csharp/RobotClientWpf/App.config
  2. 16
      ADIS_Csharp/RobotClientWpf/MainWindow.xaml
  3. 45
      ADIS_Csharp/RobotClientWpf/MainWindow.xaml.cs
  4. 12
      ADIS_Csharp/RobotClientWpf/RobotClientWpf.csproj
  5. 23
      ADIS_Csharp/RobotClientWpf/Views/ConfigView.xaml
  6. 59
      ADIS_Csharp/RobotClientWpf/Views/ConfigView.xaml.cs
  7. 13
      ADIS_Csharp/RobotClientWpf/Views/MainView.xaml
  8. 29
      ADIS_Csharp/RobotClientWpf/Views/MainView.xaml.cs

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="RobotClientWpf.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<RobotClientWpf.Properties.Settings>
<setting name="MqttBrokerIp" serializeAs="String">
<value />
</setting>
</RobotClientWpf.Properties.Settings>
</userSettings>
</configuration>

@ -7,22 +7,22 @@
xmlns:views="clr-namespace:RobotClientWpf.Views" xmlns:views="clr-namespace:RobotClientWpf.Views"
xmlns:dj="clr-namespace:DJ;assembly=NLogViewer" xmlns:dj="clr-namespace:DJ;assembly=NLogViewer"
mc:Ignorable="d" FontSize="15" mc:Ignorable="d" FontSize="15"
Title="Challenge UI" Height="700" Width="1200" WindowStartupLocation="CenterScreen" ResizeMode="NoResize"> Title="Challenge UI" Height="700" Width="1200" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Closing="Window_Closing" Loaded="Window_Loaded">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="40"/> <RowDefinition Height="40"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="150"/> <RowDefinition Height="200"/>
<RowDefinition Height="30"/> <RowDefinition Height="30"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5"> <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,5,5" Grid.ColumnSpan="2">
<Label HorizontalAlignment="Center">MQTT Broker IP:</Label> <Label HorizontalAlignment="Center">MQTT Broker IP:</Label>
<TextBox x:Name="tbIp" Width="120" Height="25" FontSize="15" TextAlignment="Left" Margin="10 0 0 0">192.168.156.156</TextBox> <TextBox x:Name="tbIp" Width="120" Height="25" FontSize="15" TextAlignment="Left" Margin="10 0 0 0">192.168.156.156</TextBox>
<Button x:Name="btnConnectDisconnect" Content="Connect" Width="100" Margin="20 0 0 0" Click="btnConnectDisconnect_Click"></Button> <Button x:Name="btnConnectDisconnect" Content="Connect" Width="100" Margin="20 0 0 0" Click="btnConnectDisconnect_Click"></Button>
</StackPanel> </StackPanel>
<TabControl Grid.Row="1" x:Name="tabControlMain"> <TabControl Grid.Row="1" x:Name="tabControlMain" Grid.ColumnSpan="2">
<TabControl.ItemContainerStyle> <TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}"> <Style TargetType="{x:Type TabItem}">
<Setter Property="Header" Value="{Binding Header}" /> <Setter Property="Header" Value="{Binding Header}" />
@ -35,7 +35,7 @@
<TextBlock Text=" Main" Foreground="Green" FontSize="16" /> <TextBlock Text=" Main" Foreground="Green" FontSize="16" />
</StackPanel> </StackPanel>
</TabItem.Header> </TabItem.Header>
<views:MainView/> <views:MainView x:Name="mainView"/>
</TabItem> </TabItem>
<TabItem> <TabItem>
<TabItem.Header> <TabItem.Header>
@ -44,11 +44,11 @@
<TextBlock Text=" Config" Foreground="Blue" FontSize="16"/> <TextBlock Text=" Config" Foreground="Blue" FontSize="16"/>
</StackPanel> </StackPanel>
</TabItem.Header> </TabItem.Header>
<views:ConfigView/> <views:ConfigView x:Name="configView"/>
</TabItem> </TabItem>
</TabControl> </TabControl>
<dj:NLogViewer Grid.Row="2" Margin="10 0 10 0" DebugForeground="Black" MaxCount="150" ErrorForeground="Black" FatalForeground="Black" TraceForeground="Black" InfoForeground="Black" WarnForeground="Black"> <dj:NLogViewer Grid.Row="2" Margin="5" DebugForeground="Black" MaxCount="150" ErrorForeground="Black" FatalForeground="Black" TraceForeground="Black" InfoForeground="Black" WarnForeground="Black" >
<dj:NLogViewer.WarnBackground> <dj:NLogViewer.WarnBackground>
<SolidColorBrush Color="#FFFF9D00" Opacity="0.5"/> <SolidColorBrush Color="#FFFF9D00" Opacity="0.5"/>
</dj:NLogViewer.WarnBackground> </dj:NLogViewer.WarnBackground>
@ -72,7 +72,7 @@
</RichTextBox>--> </RichTextBox>-->
<StackPanel Grid.Row="3" Orientation="Horizontal"> <StackPanel Grid.Row="3" Orientation="Horizontal" Grid.ColumnSpan="2" Margin="0,150,0,0" Grid.RowSpan="2">
<TextBlock x:Name="tbBottomMessage" FontSize="15" Margin="10 5"/> <TextBlock x:Name="tbBottomMessage" FontSize="15" Margin="10 5"/>
</StackPanel> </StackPanel>
</Grid> </Grid>

@ -1,8 +1,10 @@
using System; using System;
using System.Net; using System.Net;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using RobotClientWpf.Properties;
using RobotClientWpf.Utilities; using RobotClientWpf.Utilities;
using RobotLib; using RobotLib;
@ -13,14 +15,38 @@ namespace RobotClientWpf
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private bool startupCanceled;
private static Mutex? mutex; // mutex with unique key
private static readonly string mutexName = "00489402-435c-426e-9d11-9a2b839b39b6"; // random guid
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
private ChallengeFactory challenge; private ChallengeFactory? challenge;
private bool manualDisconnect; private bool manualDisconnect;
public MainWindow() public MainWindow()
{ {
// Check if another instance is already running
// There is already an instance running of this program
if (Mutex.TryOpenExisting(mutexName, out mutex))
{
MessageBox.Show("Another instance of RobotClientWpf is already running!" + Environment.NewLine +
"You cannot open multiple instances.", "Already running", MessageBoxButton.OK, MessageBoxImage.Exclamation);
startupCanceled = true;
Application.Current.Shutdown();
return;
}
// new mutex and keep Alive
mutex = new Mutex(false, mutexName);
GC.KeepAlive(mutex);
// start window
InitializeComponent(); InitializeComponent();
this.challenge = new ChallengeFactory(); this.challenge = new ChallengeFactory();
// inform other views about challenge factory
this.mainView.SetChallengeFactory(this.challenge);
this.configView.SetChallengeFactory(this.challenge);
// subscribe to events
this.challenge.PublisherSubscriber.ConnectionStateChanged += PublisherSubscriber_ConnectionStateChanged; this.challenge.PublisherSubscriber.ConnectionStateChanged += PublisherSubscriber_ConnectionStateChanged;
} }
@ -74,7 +100,7 @@ namespace RobotClientWpf
string ipAddress = UIAccessHelpers.GetTextboxText(tbIp); string ipAddress = UIAccessHelpers.GetTextboxText(tbIp);
if (IPAddress.TryParse(ipAddress, out _)) if (IPAddress.TryParse(ipAddress, out _))
{ {
var connectionSuccess = this.challenge.PublisherSubscriber.Connect(ipAddress); var connectionSuccess = this.challenge.PublisherSubscriber.Connect(ipAddress, "ADIS", "TEST");
if (connectionSuccess) if (connectionSuccess)
{ {
this.DisplayBottomMessage(MessageSeverity.Success, "Successfully connected to MQTT broker."); this.DisplayBottomMessage(MessageSeverity.Success, "Successfully connected to MQTT broker.");
@ -132,5 +158,20 @@ namespace RobotClientWpf
{ {
this.DisplayBottomMessage(MessageSeverity.Information, ""); this.DisplayBottomMessage(MessageSeverity.Information, "");
} }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!startupCanceled)
{
// save settings
Settings.Default.MqttBrokerIp = this.tbIp.Text;
Settings.Default.Save();
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.tbIp.Text = Settings.Default.MqttBrokerIp;
}
} }
} }

@ -29,10 +29,22 @@
</Resource> </Resource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="NLog.config"> <None Update="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -7,7 +7,28 @@
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"> d:DesignHeight="450" d:DesignWidth="800">
<Grid> <Grid>
<Label Content="Config" HorizontalAlignment="Left" Margin="190,132,0,0" VerticalAlignment="Top"/> <Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Grid.Column="0" Header="SplitFlap Configuration">
<DockPanel>
<Label DockPanel.Dock="Top">Configure Hardware IDs:</Label>
<Label VerticalAlignment="Center">0:</Label>
<TextBox x:Name="tbSf1" TextAlignment="Center" Width="30" Height="30" Margin="10 0"></TextBox>
<Label VerticalAlignment="Center">1:</Label>
<TextBox x:Name="tbSf2" TextAlignment="Center" Width="30" Height="30" Margin="10 0"></TextBox>
<Label VerticalAlignment="Center">2:</Label>
<TextBox x:Name="tbSf3" TextAlignment="Center" Width="30" Height="30" Margin="10 0"></TextBox>
<Label VerticalAlignment="Center">3:</Label>
<TextBox x:Name="tbSf4" TextAlignment="Center" Width="30" Height="30" Margin="10 0"></TextBox>
<Button x:Name="btnApplyConfiguration" Content="Apply" HorizontalAlignment="Right" Height="30" Width="100" Margin="10" Click="btnApplyConfiguration_Click"/>
</DockPanel>
</GroupBox>
</Grid> </Grid>
</UserControl> </UserControl>

@ -1,17 +1,8 @@
using System; using RobotClientWpf.Utilities;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RobotClientWpf.Views namespace RobotClientWpf.Views
{ {
@ -20,9 +11,55 @@ namespace RobotClientWpf.Views
/// </summary> /// </summary>
public partial class ConfigView : UserControl public partial class ConfigView : UserControl
{ {
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
private ChallengeFactory? challenge;
public ConfigView() public ConfigView()
{ {
InitializeComponent(); InitializeComponent();
} }
public void SetChallengeFactory(ChallengeFactory challenge)
{
this.challenge = challenge;
}
private void btnApplyConfiguration_Click(object sender, RoutedEventArgs e)
{
List<TextBox> splitFlapTextboxes = new List<TextBox>() { tbSf1, tbSf2, tbSf3, tbSf4 };
int setupIdCounter = 0; // counting setup ID from 0
foreach (var textbox in splitFlapTextboxes)
{
this.GetHwIdFromTbAndSendMqtt(textbox, setupIdCounter++);
UIAccessHelpers.SetTextboxText(textbox, "");
}
}
private void GetHwIdFromTbAndSendMqtt(TextBox tb, int id)
{
if(challenge != null)
{
string text = UIAccessHelpers.GetTextboxText(tb);
// if nothing is in the textbox
if (String.IsNullOrEmpty(text))
{
return;
}
int hwId;
if(int.TryParse(text, out hwId) == false)
{
log.Warn($"Failed to parse HardwareID={text} given for Splitflap id={id}");
return;
}
this.challenge.RobotStationary.SplitFlap.ConfigureSplitflap(id, hwId);
}
else
{
log.Error("challenge object is null");
}
}
} }
} }

@ -7,7 +7,18 @@
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"> d:DesignHeight="450" d:DesignWidth="800">
<Grid> <Grid>
<Label Content="Main" HorizontalAlignment="Left" Margin="443,192,0,0" VerticalAlignment="Top"/> <Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Grid.Column="1" Header="Splitflap Display" Margin="10" HorizontalAlignment="Right">
<TextBox x:Name="tbSplitflapText" Height="70" Width="150" FontSize="30" IsReadOnly="True" TextWrapping="NoWrap"/>
</GroupBox>
</Grid> </Grid>
</UserControl> </UserControl>

@ -1,17 +1,5 @@
using System; using RobotClientWpf.Utilities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RobotClientWpf.Views namespace RobotClientWpf.Views
{ {
@ -20,9 +8,24 @@ namespace RobotClientWpf.Views
/// </summary> /// </summary>
public partial class MainView : UserControl public partial class MainView : UserControl
{ {
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
private ChallengeFactory? challenge;
public MainView() public MainView()
{ {
InitializeComponent(); InitializeComponent();
} }
public void SetChallengeFactory(ChallengeFactory challenge)
{
this.challenge = challenge;
// subscribe to events
this.challenge.RobotStationary.SplitFlap.SplitFlapDisplayChanged += this.SplitFlap_SplitFlapDisplayChanged;
}
public void SplitFlap_SplitFlapDisplayChanged(object? sender, RobotLib.SplitFlap.SplitFlapDisplayEventArgs e)
{
UIAccessHelpers.SetTextboxText(this.tbSplitflapText, e.DisplayMessage);
}
} }
} }

Loading…
Cancel
Save