diff --git a/.gitignore b/.gitignore index e69de29..eb0ddd7 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +.idea +.vs +*.user \ No newline at end of file diff --git a/HttpKeys/App.xaml.cs b/HttpKeys/App.xaml.cs index a804fab..f0e5026 100644 --- a/HttpKeys/App.xaml.cs +++ b/HttpKeys/App.xaml.cs @@ -1,6 +1,5 @@ -using System.Configuration; -using System.Data; -using System.Windows; +using System.Windows; +using Application = System.Windows.Application; namespace HttpKeys; @@ -9,5 +8,43 @@ namespace HttpKeys; /// public partial class App : Application { + private NotifyIcon _trayIcon; + + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + _trayIcon = new NotifyIcon + { + Icon = new System.Drawing.Icon("app.ico"), + Visible = true, + Text = "My WPF Tray App" + }; + + var menu = new ContextMenuStrip(); + menu.Items.Add("Открыть", null, (s, ev) => ShowMainWindow()); + menu.Items.Add("Выход", null, (s, ev) => ExitApplication()); + + _trayIcon.ContextMenuStrip = menu; + _trayIcon.DoubleClick += (s, ev) => ShowMainWindow(); + } + + private void ShowMainWindow() + { + if (MainWindow == null) + MainWindow = new MainWindow(); + + MainWindow.ShowInTaskbar = true; // вернуть в панель задач + MainWindow.Show(); + MainWindow.WindowState = WindowState.Normal; + MainWindow.Activate(); + } + + private void ExitApplication() + { + _trayIcon.Visible = false; + _trayIcon.Dispose(); + Shutdown(); + } } diff --git a/HttpKeys/HttpKeys.csproj b/HttpKeys/HttpKeys.csproj index 212659c..9b070f9 100644 --- a/HttpKeys/HttpKeys.csproj +++ b/HttpKeys/HttpKeys.csproj @@ -6,6 +6,13 @@ enable enable true + true + + + PreserveNewest + + + diff --git a/HttpKeys/Listener.cs b/HttpKeys/Listener.cs new file mode 100644 index 0000000..8cf4d2e --- /dev/null +++ b/HttpKeys/Listener.cs @@ -0,0 +1,73 @@ +using System.IO; +using System.Net; +using System.Text; +using System.Text.Json; + +namespace HttpKeys; + + +class Params +{ + public string ActionId { get; set; } = null!; + public string State { get; set; } = null!; + public bool ToggleChanged { get; set; } +} + + +public class Listener +{ + public event Action Log; + + public async Task Run() + { + var listener = new HttpListener(); + listener.Prefixes.Add("http://127.0.0.1:16888/"); + + listener.Start(); + Log("Listening on http://127.0.0.1:16888/"); + + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true // чтобы Email/email/EMAIL не ломалось + }; + + while (true) + { + var ctx = await listener.GetContextAsync(); + + try + { + + if (ctx.Request.RawUrl != "/press") + { + continue; + } + + using var reader = new StreamReader( + ctx.Request.InputStream, + ctx.Request.ContentEncoding + ); + + var body = await reader.ReadToEndAsync(); + var p = JsonSerializer.Deserialize(body, options); + if (p == null) + continue; + + Log("---- REQUEST ----"); + Log("URL: " + p.ActionId); + Log("URL: " + p.State); + Log("URL: " + p.ToggleChanged); + + var responseBytes = "OK"u8.ToArray(); + ctx.Response.StatusCode = 200; + ctx.Response.ContentType = "text/plain; charset=utf-8"; + await ctx.Response.OutputStream.WriteAsync(responseBytes); + ctx.Response.Close(); + } + catch (Exception ex) + { + Log("Error: " + ex.Message); + } + } + } +} \ No newline at end of file diff --git a/HttpKeys/MainWindow.xaml b/HttpKeys/MainWindow.xaml index b9f82bb..e826a9e 100644 --- a/HttpKeys/MainWindow.xaml +++ b/HttpKeys/MainWindow.xaml @@ -5,8 +5,12 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:HttpKeys" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> + Title="MainWindow" Height="450" Width="800" + ShowInTaskbar="True"> - + diff --git a/HttpKeys/MainWindow.xaml.cs b/HttpKeys/MainWindow.xaml.cs index 0829d0d..2ff298b 100644 --- a/HttpKeys/MainWindow.xaml.cs +++ b/HttpKeys/MainWindow.xaml.cs @@ -1,4 +1,6 @@ -using System.Text; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; @@ -16,8 +18,34 @@ namespace HttpKeys; /// public partial class MainWindow : Window { + public ObservableCollection Logs { get; } = new(); + public Listener _listener; + public MainWindow() { + _listener = new Listener(); + _listener.Log += Log; + InitializeComponent(); + DataContext = this; + Closing += MainWindow_Closing; + Hide(); + ShowInTaskbar = false; // убрать из панели задач + + Task.Run(async () => await _listener.Run()); + } + + private void MainWindow_Closing(object? sender, CancelEventArgs e) + { + e.Cancel = true; + + Hide(); + ShowInTaskbar = false; // убрать из панели задач + } + + private void Log(string log) + { + Dispatcher.Invoke(() => Logs.Add($"{DateTime.Now}:: {log}")); + Console.WriteLine(log); } } \ No newline at end of file diff --git a/HttpKeys/app.ico b/HttpKeys/app.ico new file mode 100644 index 0000000..48c7844 Binary files /dev/null and b/HttpKeys/app.ico differ