Skip to content

Commit

Permalink
Implement actual logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BtbN committed Jul 27, 2022
1 parent 780e6e4 commit 2df356d
Show file tree
Hide file tree
Showing 7 changed files with 528 additions and 23 deletions.
79 changes: 58 additions & 21 deletions ESAWindowTracker/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,88 @@
using System.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;
using Microsoft.Extensions.Logging.Console;

namespace ESAWindowTracker
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public IServiceProvider ServiceProvider { get; set; }
public IConfiguration Configuration { get; set; }
private readonly IHost host;

new public static App Current => (App)Application.Current;

public App()
{
var builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

host = new HostBuilder()
.ConfigureAppConfiguration((context, builder) =>
{
builder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
#if DEBUG
builder.AddUserSecrets<App>();
#endif
}).ConfigureServices((context, services) =>
{
ConfigureServices(context.Configuration, services);
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
#if DEBUG
builder.AddUserSecrets<App>();
logging.AddDebug();
#else
logging.AddEventLog();
#endif
})
.Build();

Configuration = builder.Build();
WriteConfig();

var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
ServiceProvider = serviceCollection.BuildServiceProvider();
}

private void ConfigureServices(ServiceCollection services)
private void ConfigureServices(IConfiguration configuration, IServiceCollection services)
{
services.AddTransient<MainWindow>();
services.AddLogging();

services.AddOptions();
services.Configure<Config>(configuration);
services.Configure<RabbitConfig>(configuration.GetSection("RabbitConfig"));

RabbitService.Register(services);
WindowTracker.Register(services);

services.AddSingleton<MainWindow>();
}

protected override async void OnStartup(StartupEventArgs e)
{
await host.StartAsync();
host.Services.GetRequiredService<MainWindow>();
base.OnStartup(e);
}

protected override void OnStartup(StartupEventArgs e)
protected override async void OnExit(ExitEventArgs e)
{
ServiceProvider.GetRequiredService<MainWindow>();
using (host)
{
await host.StopAsync(TimeSpan.FromSeconds(5));
}

base.OnExit(e);
}

public void WriteConfig(Config? config = null)
{
if (config == null)
config = Configuration.Get<Config>() ?? new Config();
if (config == null) {
using var scope = host.Services.CreateScope();
config = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<Config>>().Value;
}

var jsonWriteOptions = new JsonSerializerOptions()
{
Expand Down
3 changes: 3 additions & 0 deletions ESAWindowTracker/ESAWindowTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.4.0" />
</ItemGroup>

Expand Down
9 changes: 9 additions & 0 deletions ESAWindowTracker/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
/>
</Window.CommandBindings>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<tb:TaskbarIcon x:Name="ESAWindowTracker" ToolTipText="ESAWindowTracker" IconSource="/NetDrives.ico" DoubleClickCommand="{x:Static local:Commands.Show}">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
Expand All @@ -24,5 +31,7 @@
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
<Label Name="IDField" Grid.Row="0" Grid.Column="0"></Label>
<Label Name="RabbitStatusLabel" Grid.Row="1" Grid.Column="0"></Label>
</Grid>
</Window>
28 changes: 26 additions & 2 deletions ESAWindowTracker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand All @@ -21,9 +23,31 @@ namespace ESAWindowTracker
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
private readonly IOptionsMonitor<Config> options;
private readonly RabbitMessageSender rabbitMessageSender;

public MainWindow(IOptionsMonitor<Config> options, RabbitMessageSender rabbitMessageSender)
{
InitializeComponent();

this.options = options;
this.rabbitMessageSender = rabbitMessageSender;

rabbitMessageSender.StatusChanged += RabbitMessageSender_StatusChanged;
RabbitStatusLabel.Content = rabbitMessageSender.Status;

options.OnChange(OnConfigChange);
OnConfigChange(options.CurrentValue, "");
}

private void OnConfigChange(Config cfg, string _)
{
IDField.Content = $"This is PC {cfg.PCID} at {cfg.EventShort}";
}

private void RabbitMessageSender_StatusChanged(string status)
{
RabbitStatusLabel.Content = status;
}

private void Show_Executed(object sender, ExecutedRoutedEventArgs e)
Expand Down
17 changes: 17 additions & 0 deletions ESAWindowTracker/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net6.0-windows\publish\win-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net6.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
Loading

0 comments on commit 2df356d

Please sign in to comment.