diff --git a/.gitignore b/.gitignore index 0e0f5fa..d148e8a 100644 --- a/.gitignore +++ b/.gitignore @@ -362,8 +362,5 @@ MigrationBackup/ # Fody - auto-generated XML schema FodyWeavers.xsd -presets/* -!presets/.keep - trustedca/* !trustedca/.keep \ No newline at end of file diff --git a/Components/Pages/Home.razor b/Components/Pages/Home.razor index 72f4fe4..974503e 100644 --- a/Components/Pages/Home.razor +++ b/Components/Pages/Home.razor @@ -5,22 +5,29 @@ @inject SolaceSubscribeService SubscribeService @inject Blazored.Toast.Services.IToastService toastService @implements IDisposable +@inject PresetService PresetService

Solace PubSub+

Publish Message

+
+ + +
-
- - -
@@ -52,16 +59,22 @@

Subscribe to Topic

+
+ + +
-
- - -
@@ -105,7 +118,6 @@ private string publishPassword; private string publishTopic; private string publishMessage; - private bool publishUseTcps = true; private bool publishSslVerify = true; private string subscribeHost; private string subscribeVpnName; @@ -113,25 +125,69 @@ private string subscribePassword; private string subscribeTopic; private bool subscribeSslVerify = true; - private bool subscribeUseTcps = true; + private string selectedPresetName; + private string presetName; + private List presets = new List(); private List messages = new List(); - private string FormatHost(string host, bool useTcps) + protected override async Task OnInitializedAsync() + { + presets = await PresetService.GetPresetsAsync(); + } + + private async Task LoadPresetPublish(ChangeEventArgs e) { - if (useTcps && !host.StartsWith("tcps://")) + selectedPresetName = e.Value?.ToString(); + Console.WriteLine($"Selected Preset Name: {selectedPresetName}"); + if (!string.IsNullOrEmpty(selectedPresetName)) { - return "tcps://" + host; + var preset = presets.FirstOrDefault(p => p.Name == selectedPresetName); + if (preset != null) + { + publishHost = preset.Host; + publishVpnName = preset.VpnName; + publishUsername = preset.Username; + publishTopic = preset.Topic; + + Console.WriteLine($"Loaded Preset: {preset.Name}"); + } + else + { + Console.WriteLine($"Preset with name '{selectedPresetName}' not found."); + } + } + } + + private async Task LoadPresetSubscribe(ChangeEventArgs e) + { + selectedPresetName = e.Value?.ToString(); + Console.WriteLine($"Selected Preset Name: {selectedPresetName}"); + if (!string.IsNullOrEmpty(selectedPresetName)) + { + var preset = presets.FirstOrDefault(p => p.Name == selectedPresetName); + if (preset != null) + { + subscribeHost = preset.Host; + subscribeVpnName = preset.VpnName; + subscribeUsername = preset.Username; + subscribeTopic = preset.Topic; + + Console.WriteLine($"Loaded Preset: {preset.Name}"); + } + else + { + Console.WriteLine($"Preset with name '{selectedPresetName}' not found."); + } } - return host; } + private void ConnectAndPublish() { try { - string formattedHost = FormatHost(publishHost, publishUseTcps); - PublishService.PublishMessage(formattedHost, publishVpnName, publishUsername, publishPassword, publishTopic, publishMessage, publishSslVerify); + PublishService.PublishMessage(publishHost, publishVpnName, publishUsername, publishPassword, publishTopic, publishMessage, publishSslVerify); toastService.ShowSuccess("Successfully published message"); } catch (SolaceSystems.Solclient.Messaging.OperationErrorException ex) @@ -144,8 +200,7 @@ { try { - string formattedHost = FormatHost(subscribeHost, subscribeUseTcps); - SubscribeService.SubscribeToTopic(formattedHost, subscribeVpnName, subscribeUsername, subscribePassword, subscribeTopic, subscribeSslVerify, message => + SubscribeService.SubscribeToTopic(subscribeHost, subscribeVpnName, subscribeUsername, subscribePassword, subscribeTopic, subscribeSslVerify, message => { InvokeAsync(() => { diff --git a/Components/Pages/QueueBrowser.razor b/Components/Pages/QueueBrowser.razor index 7e53c21..8e2f17a 100644 --- a/Components/Pages/QueueBrowser.razor +++ b/Components/Pages/QueueBrowser.razor @@ -7,8 +7,6 @@ @inject Blazored.Toast.Services.IToastService toastService @inject NavigationManager Navigation @inject PresetService PresetService -@inject IModalService ModalService -@inject IJSRuntime JsRuntime Queue Browser @@ -22,9 +20,6 @@ } - - -
@@ -45,10 +40,6 @@
-
- - -
@@ -84,7 +75,7 @@ RowOverlayBackground="Background.Transparent" DetailRowStartsVisible="false" Filterable - FilterMethod="DataGridFilterMethod.Contains" + FilterMethod="DataGridFilterMethod.StartsWith" ShowPager ShowPageSizes PagerPosition="DataGridPagerPosition.TopAndBottom" @@ -144,68 +135,8 @@
- - - - - Save Preset - - - - - - Preset Name - - - Please enter a valid preset name - Please enter a valid preset name - - - - - - - Host - - - Please enter a valid host - Please enter a valid host - - - - - - - VPN Name - - - Please enter a valid VPN name - Please enter a valid VPN name - - - - - - Username - - - - Queue Name - - - - - - - - - - - - - @code { private string host; private string vpnName; @@ -214,33 +145,21 @@ private string queueName; private int maxMessages = 50; private bool sslVerify = true; - private bool useTcps = true; private List messages = new List(); private DataGrid dataGrid; private string selectedPresetName; - private Modal savePresetModal; private string presetName; private List presets = new List(); private bool isBrowsing = false; - private string FormatHost(string host, bool useTcps) - { - if (useTcps && !host.StartsWith("tcps://")) - { - return "tcps://" + host; - } - return host; - } - private async Task BrowseQueueAsync() { Logger.LogInformation("BrowseQueue method called."); try { - string formattedHost = FormatHost(host, useTcps); isBrowsing = true; - messages = await QueueBrowserService.BrowseQueueAsync(formattedHost, vpnName, username, password, queueName, sslVerify, maxMessages); + messages = await QueueBrowserService.BrowseQueueAsync(host, vpnName, username, password, queueName, sslVerify, maxMessages); Logger.LogInformation("Messages retrieved: {count}", messages.Count); } catch (SolaceSystems.Solclient.Messaging.OperationErrorException ex) @@ -263,10 +182,9 @@ { try { - string formattedHost = FormatHost(host, useTcps); isDeleting = true; deletingMessageId = adMessageId.ToString(); - await QueueBrowserService.DeleteMessage(formattedHost, vpnName, username, password, queueName, sslVerify, adMessageId); + await QueueBrowserService.DeleteMessage(host, vpnName, username, password, queueName, sslVerify, adMessageId); toastService.ShowSuccess("Deleting message with ID: " + deletingMessageId); await BrowseQueueAsync(); } @@ -342,61 +260,4 @@ } } } - private void ShowSavePresetModal() - { - presetName = ""; - savePresetModal.Show(); - } - - private void HideSavePresetModal() - { - savePresetModal.Hide(); - } - - private async Task SavePreset() - { - var existingPreset = presets.FirstOrDefault(p => p.Name == presetName); - - if (existingPreset != null) - { - if (await JsRuntime.InvokeAsync("confirm", $"A preset with the name '{presetName}' already exists. Do you want to overwrite it?")) - { - existingPreset.Host = host; - existingPreset.VpnName = vpnName; - existingPreset.Username = username; - existingPreset.QueueName = queueName; - - await PresetService.SavePresetAsync(existingPreset); - presets = await PresetService.GetPresetsAsync(); - toastService.ShowSuccess("Preset overwritten successfully"); - } - } - else - { - var newPreset = new PresetModel - { - Name = presetName, - Host = host, - VpnName = vpnName, - Username = username, - QueueName = queueName - }; - - await PresetService.SavePresetAsync(newPreset); - presets = await PresetService.GetPresetsAsync(); - toastService.ShowSuccess("Preset saved successfully"); - } - - savePresetModal.Hide(); - } - - private async Task DeletePreset() - { - if (await JsRuntime.InvokeAsync("confirm", $"Deleting '{selectedPresetName}' Are you sure ?")) - { - await PresetService.DeletePresetAsync(selectedPresetName); - presets = await PresetService.GetPresetsAsync(); - toastService.ShowSuccess("Deleted Preset successfully"); - } - } } diff --git a/Services/PresetService.cs b/Services/PresetService.cs index 750e8d3..95b6ab2 100644 --- a/Services/PresetService.cs +++ b/Services/PresetService.cs @@ -14,53 +14,37 @@ public class PresetModel public string VpnName { get; set; } public string Username { get; set; } public string QueueName { get; set; } + public string Topic { get; set; } } public class PresetService { - private readonly string presetsPath = "presets"; + private readonly string presetsFilePath = Path.Combine("presets", "presets.json"); public async Task> GetPresetsAsync() { - if (!Directory.Exists(presetsPath)) + if (!File.Exists(presetsFilePath)) { - Directory.CreateDirectory(presetsPath); + return new List(); } - var presets = new List(); + var json = await File.ReadAllTextAsync(presetsFilePath); + var presets = JsonSerializer.Deserialize>(json); - foreach (var file in Directory.GetFiles(presetsPath, "*.json")) - { - var json = await File.ReadAllTextAsync(file); - var preset = JsonSerializer.Deserialize(json); - presets.Add(preset); - } - - return presets; + return presets ?? new List(); } - public async Task SavePresetAsync(PresetModel preset) + public async Task SavePresetsAsync(List presets) { - if (!Directory.Exists(presetsPath)) - { - Directory.CreateDirectory(presetsPath); - } + var json = JsonSerializer.Serialize(presets, new JsonSerializerOptions { WriteIndented = true }); + var directory = Path.GetDirectoryName(presetsFilePath); - var json = JsonSerializer.Serialize(preset); - var filePath = Path.Combine(presetsPath, $"{preset.Name}.json"); - await File.WriteAllTextAsync(filePath, json); - } - - public async Task DeletePresetAsync(string presetName) - { - if (!Directory.Exists(presetsPath)) + if (!Directory.Exists(directory)) { - Directory.CreateDirectory(presetsPath); + Directory.CreateDirectory(directory); } - var filePath = Path.Combine(presetsPath, $"{presetName}.json"); - File.Delete(filePath); + await File.WriteAllTextAsync(presetsFilePath, json); } } } - diff --git a/SolaceWebClient.csproj b/SolaceWebClient.csproj index c7e55fa..8bfbd55 100644 --- a/SolaceWebClient.csproj +++ b/SolaceWebClient.csproj @@ -10,12 +10,14 @@ - + + + + - @@ -32,4 +34,8 @@ + + + + diff --git a/presets/.keep b/presets/.keep deleted file mode 100644 index 5f28270..0000000 --- a/presets/.keep +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/presets/example.json b/presets/example.json new file mode 100644 index 0000000..45e5a8b --- /dev/null +++ b/presets/example.json @@ -0,0 +1,24 @@ +[ + { + "Name": "demo", + "Host": "localhost:5555", + "VpnName": "default", + "Username": "demo", + "QueueName": "demo", + }, + { + "Name": "demo2", + "Host": "localhost:5555", + "VpnName": "default", + "Username": "demo", + "Topic": "demo" + }, + { + "Name": "demo3", + "Host": "localhost:5555", + "VpnName": "default", + "Username": "demo", + "QueueName": "demo", + "Topic": "demo" + } +] \ No newline at end of file