Skip to content

Commit

Permalink
Merge pull request #362 from nic547/desktop
Browse files Browse the repository at this point in the history
Desktop: Display which engines use the same dcc address in the decoder programming window
  • Loading branch information
nic547 committed Jun 9, 2024
2 parents 7208491 + 3fd5a96 commit 0579c9d
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/TauStellwerk.Client/Resources/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/TauStellwerk.Client/Resources/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
<value>Status noch nicht bekannt</value>
</data>
<data name="StatusRunningSubtitle" xml:space="preserve">
<value>TauStellwerk wurde gestartet von:</value>
<value>Gestartet von:</value>
</data>
<data name="Open" xml:space="preserve">
<value>Öffnen</value>
Expand All @@ -252,4 +252,7 @@
<data name="NonePlaceholder" xml:space="preserve">
<value>Keine</value>
</data>
<data name="EngineSameAddress" xml:space="preserve">
<value>Lokomotiven mit gleicher Adresse</value>
</data>
</root>
5 changes: 4 additions & 1 deletion src/TauStellwerk.Client/Resources/Resources.gsw.resx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<value>Status nonig bekannt</value>
</data>
<data name="StatusRunningSubtitle" xml:space="preserve">
<value>TauStellwerk isch gstarted worde vo:</value>
<value>Gstarted worde vo:</value>
</data>
<data name="Open" xml:space="preserve">
<value>Öffne</value>
Expand All @@ -153,4 +153,7 @@
<data name="Write" xml:space="preserve">
<value>Schribe</value>
</data>
<data name="EngineSameAddress" xml:space="preserve">
<value>Loks mit gliicher Adresse</value>
</data>
</root>
5 changes: 4 additions & 1 deletion src/TauStellwerk.Client/Resources/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
<value>State not yet loaded</value>
</data>
<data name="StatusRunningSubtitle" xml:space="preserve">
<value>TauStellwerk was started by:</value>
<value>Started by:</value>
</data>
<data name="Open" xml:space="preserve">
<value>Open</value>
Expand All @@ -252,4 +252,7 @@
<data name="NonePlaceholder" xml:space="preserve">
<value>None</value>
</data>
<data name="EngineSameAddress" xml:space="preserve">
<value>Engines with same address</value>
</data>
</root>
11 changes: 11 additions & 0 deletions src/TauStellwerk.Client/Services/EngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public async Task ReleaseEngine(int id)
}
}

public async Task<List<EngineOverviewDto>> GetEnginesForAddress(int address)
{
var connection = await _service.TryGetHubConnection();
if (connection != null)
{
return await connection.InvokeAsync<List<EngineOverviewDto>>("GetEnginesForAddress", address);
}

return [];
}

public async Task SetSpeed(int id, int speed, Direction direction)
{
var limiter = _activeEngines.TryGet(id) ?? throw new InvalidOperationException();
Expand Down
15 changes: 15 additions & 0 deletions src/TauStellwerk.Data/Dao/EngineDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public async Task<Result<Engine>> GetEngine(int id)
return result.Select(e => e.ToEngineDto()).ToList();
}

public async Task<List<EngineOverviewDto>> GetEnginesWithAddress(int address)
{
var stopwatch = Stopwatch.StartNew();

var result = await _dbContext.Engines
.AsSingleQuery()
.Where(e => e.Address == address)
.Take(6)
.Select(e => e.ToEngineDto())
.ToListAsync();

_logger.LogDebug("Engines with address {address} were queried in {time}ms", address, stopwatch.Elapsed.TotalMilliseconds.ToString("F2", CultureInfo.CurrentCulture));
return result;
}

public async Task<Result<EngineFullDto>> UpdateOrAdd(EngineFullDto engineDto)
{
Engine? engine;
Expand Down
31 changes: 29 additions & 2 deletions src/TauStellwerk.Desktop/ViewModels/DecoderProgrammingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Splat;
using TauStellwerk.Client.Services;
using TauStellwerk.Client.Services.DecoderProgramming;

namespace TauStellwerk.Desktop.ViewModels;

public partial class DecoderProgrammingViewModel : ViewModelBase
{
private readonly IDecoderProgrammingService _decoderProgrammingService;
private readonly EngineService _engineService;

[ObservableProperty]
private int? _dccAddress;
Expand All @@ -21,9 +23,34 @@ public partial class DecoderProgrammingViewModel : ViewModelBase
[ObservableProperty]
private bool _isNotProgramming = true;

public DecoderProgrammingViewModel(IDecoderProgrammingService? programmingService = null)
[ObservableProperty]
private List<string> _enginesWithSameAddress = [];

[ObservableProperty]
private bool _hasManyEnginesWithSameAddress;

public DecoderProgrammingViewModel(IDecoderProgrammingService? programmingService = null, EngineService? engineService = null)
{
_decoderProgrammingService = programmingService ?? Locator.Current.GetService<IDecoderProgrammingService>() ?? throw new InvalidOperationException();
_decoderProgrammingService = programmingService ?? Locator.Current.GetRequiredService<IDecoderProgrammingService>();
_engineService = engineService ?? Locator.Current.GetRequiredService<EngineService>();

PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(DccAddress))
{
HandleChangedAddress();
}
};
}

private async void HandleChangedAddress()
{
if (DccAddress is > 0 and < 10240)
{
var engines = await _engineService.GetEnginesForAddress((int)DccAddress);
EnginesWithSameAddress = engines.Take(5).Select(e => e.Name).ToList();
HasManyEnginesWithSameAddress = engines.Count > 5;
}
}

[RelayCommand]
Expand Down
12 changes: 11 additions & 1 deletion src/TauStellwerk.Desktop/Views/DecoderProgrammingWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
xmlns:viewModels="clr-namespace:TauStellwerk.Desktop.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="TauStellwerk.Desktop.Views.DecoderProgrammingWindow"
Title="ProgrammingWindow">
Title="ProgrammingWindow"
Icon="/Assets/tauStellwerk.ico">
<Design.DataContext>
<viewModels:DecoderProgrammingViewModel />
</Design.DataContext>
Expand All @@ -22,6 +23,15 @@
<Button Command="{Binding WriteDccAddressCommand}" Content="{x:Static r:Resources.Write}" IsEnabled="{Binding IsNotProgramming}"/>
</StackPanel>
<Label Content="{Binding Message}" Classes="Warning"></Label>
<Label Content="{x:Static r:Resources.EngineSameAddress}"/>
<ItemsControl ItemsSource="{Binding EnginesWithSameAddress}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Label IsVisible="{Binding HasManyEnginesWithSameAddress}" Content="..."/>
</StackPanel>
</DockPanel>
</Window>
7 changes: 7 additions & 0 deletions src/TauStellwerk.Server/Hub/TauHubEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public async Task<Result<EngineFullDto>> GetEngine([FromServices] EngineDao engi
return list;
}

public async Task<List<EngineOverviewDto>> GetEnginesForAddress(
[FromServices] EngineDao engineDao,
int address)
{
return await engineDao.GetEnginesWithAddress(address);
}

public async Task<ResultDto<EngineFullDto>> AddOrUpdateEngine([FromServices] EngineDao engineDao, EngineFullDto engine)
{
if (engine.Id is not 0)
Expand Down

0 comments on commit 0579c9d

Please sign in to comment.