Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion PostCodeSerialMonitor/Assets/Resources.Designer.cs

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

16 changes: 16 additions & 0 deletions PostCodeSerialMonitor/Assets/Resources.pt-BR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@
<value>Falha ao baixar MetaDefinition de {0}</value>
<comment>In Services/MetaUpdateService.cs</comment>
</data>
<data name="FailedDeserializingReleaseDefinition" xml:space="preserve">
<value>Falha ao deserializar ReleaseDefinition</value>
<comment>In Services/MetaUpdateService.cs</comment>
</data>
<data name="FailedDownloadReleaseDefinition" xml:space="preserve">
<value>Falha ao baixar o último lançamento de {0}</value>
<comment>In Services/MetaUpdateService.cs</comment>
</data>
<data name="DecoderIgnoringLine" xml:space="preserve">
<value>Decodificador: Ignorando linha {0}</value>
<comment>In Services/SerialLineDecoder.cs</comment>
Expand Down Expand Up @@ -225,6 +233,14 @@
<value>Falha ao ataulizar o metadata</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
</data>
<data name="NewAppReleaseAvailable" xml:space="preserve">
<value>Uma nova versão do aplicativo está disponível em {0}.</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
</data>
<data name="NewFirmwareReleaseAvailable" xml:space="preserve">
<value>Uma nova versão do firmware está disponível em {0}.</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
</data>
<data name="Error" xml:space="preserve">
<value>Erro</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
Expand Down
16 changes: 16 additions & 0 deletions PostCodeSerialMonitor/Assets/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@
<value>Failed to download MetaDefinition from {0}</value>
<comment>In Services/MetaUpdateService.cs</comment>
</data>
<data name="FailedDeserializingReleaseDefinition" xml:space="preserve">
<value>Failed deserializing ReleaseDefinition</value>
<comment>In Services/MetaUpdateService.cs</comment>
</data>
<data name="FailedDownloadReleaseDefinition" xml:space="preserve">
<value>Failed to download latest release from {0}</value>
<comment>In Services/MetaUpdateService.cs</comment>
</data>
<data name="DecoderIgnoringLine" xml:space="preserve">
<value>Decoder: Ignoring line {0}</value>
<comment>In Services/SerialLineDecoder.cs</comment>
Expand Down Expand Up @@ -225,6 +233,14 @@
<value>Failed to update metadata</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
</data>
<data name="NewAppReleaseAvailable" xml:space="preserve">
<value>A new app release is available at {0}.</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
</data>
<data name="NewFirmwareReleaseAvailable" xml:space="preserve">
<value>A new firmware release is available at {0}.</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
</data>
<data name="Error" xml:space="preserve">
<value>Error</value>
<comment>In ViewModels/MainWindowViewModel.cs</comment>
Expand Down
5 changes: 5 additions & 0 deletions PostCodeSerialMonitor/Models/ReleaseDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace PostCodeSerialMonitor.Models;
public class ReleaseDefinition
{
public string tag_name { get; set; } = string.Empty;
}
60 changes: 55 additions & 5 deletions PostCodeSerialMonitor/Services/MetaUpdateService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
using PostCodeSerialMonitor.Models;
using PostCodeSerialMonitor.Utils;
using Microsoft.Extensions.Logging;

namespace PostCodeSerialMonitor.Services;

public class MetaUpdateService
{
private readonly ConfigurationService _configurationService;
Expand All @@ -24,8 +27,8 @@ public class MetaUpdateService
public AppConfiguration Config => _configurationService.Config;

public MetaUpdateService(
ConfigurationService configurationService,
JsonSerializerOptions jsonOptions,
ConfigurationService configurationService,
JsonSerializerOptions jsonOptions,
ILogger<MetaUpdateService> logger)
{
_configurationService = configurationService ?? throw new ArgumentNullException(nameof(configurationService));
Expand All @@ -34,6 +37,10 @@ public MetaUpdateService(
?? throw new ArgumentNullException(nameof(_configurationService.Config.MetaStoragePath));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_httpClient = new HttpClient();

//All GitHub's API requests must include a valid User-Agent header.
//@see https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api?apiVersion=2022-11-28#user-agent
_httpClient.DefaultRequestHeaders.Add("User-Agent", "XboxPostcodeMonitor");
}

public async Task<bool> TryLoadLocalDefinition()
Expand All @@ -46,11 +53,35 @@ public async Task<bool> TryLoadLocalDefinition()
return true;
}

public async Task<bool> CheckForAppUpdatesAsync(string localVersion)
{
// Get the latest release from GitHub repo.
var remoteRelease = await GetRepositoryLatestReleaseAsync("xboxoneresearch", "XboxPostcodeMonitor");
var remoteVersion = (remoteRelease == null) ? string.Empty : remoteRelease.tag_name;

SemanticVersionUtils local = new SemanticVersionUtils(localVersion);
SemanticVersionUtils remote = new SemanticVersionUtils(remoteVersion);

return remote > local;
}

public async Task<bool> CheckForFirmwareUpdatesAsync(string localVersion)
{
// Get the latest release from GitHub repo.
var remoteRelease = await GetRepositoryLatestReleaseAsync("xboxoneresearch", "PicoDurangoPOST");
var remoteVersion = (remoteRelease == null) ? string.Empty : remoteRelease.tag_name;

SemanticVersionUtils local = new SemanticVersionUtils(localVersion);
SemanticVersionUtils remote = new SemanticVersionUtils(remoteVersion);

return remote > local;
}

public async Task<bool> CheckForMetaDefinitionUpdatesAsync()
{
var localMeta = await GetLocalMetaDefinitionAsync();
var remoteMeta = await GetRemoteMetaDefinitionAsync();

if (localMeta == null || remoteMeta == null)
{
// Update required
Expand All @@ -75,7 +106,7 @@ public async Task UpdateMetaDefinitionAsync()

// Ensure directory exists
Directory.CreateDirectory(_localPath);

// Save the new meta definition
await File.WriteAllTextAsync(LocalMetaPath, metaContentStr);

Expand Down Expand Up @@ -143,7 +174,7 @@ private async Task DownloadMetaFilesAsync()
{
var response = await _httpClient.GetAsync(Config.MetaJsonUrl);
response.EnsureSuccessStatusCode();

var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<MetaDefinition>(json, _jsonSerializeOptions);
}
Expand All @@ -153,4 +184,23 @@ private async Task DownloadMetaFilesAsync()
return null;
}
}

private async Task<ReleaseDefinition?> GetRepositoryLatestReleaseAsync(string owner, string repo)
{
var gitHubApiReleasesLatest = new Uri($"https://api.github.com/repos/{owner}/{repo}/releases/latest");

try
{
var response = await _httpClient.GetAsync(gitHubApiReleasesLatest);
response.EnsureSuccessStatusCode();

var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<ReleaseDefinition>(json, _jsonSerializeOptions);
}
catch (Exception ex)
{
_logger.LogError(ex, Assets.Resources.FailedDownloadReleaseDefinition, gitHubApiReleasesLatest);
return null;
}
}
}
34 changes: 34 additions & 0 deletions PostCodeSerialMonitor/Utils/SemanticVersionUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Avalonia.X11.Interop;

namespace PostCodeSerialMonitor.Utils;

public class SemanticVersionUtils
{
private string _version { get; set; } = string.Empty;

public SemanticVersionUtils(string version)
{
//Ignore the 'v' at the beginning of the version string
if (version.StartsWith("v"))
{
_version = version.Substring(1);
}
else
{
_version = version;
}
}

/// Override greater-than operator for SemanticVersionUtils
public static bool operator >(SemanticVersionUtils left, SemanticVersionUtils right)
{
return string.Compare(left._version, right._version) > 0;
}

/// Override less-than operator for SemanticVersionUtils
public static bool operator <(SemanticVersionUtils left, SemanticVersionUtils right)
{
return string.Compare(left._version, right._version) < 0;
}
}
28 changes: 27 additions & 1 deletion PostCodeSerialMonitor/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ await MessageBoxManager
ButtonEnum.Ok)
.ShowAsync();
}

if (_configurationService.Config.CheckForAppUpdates)
{
updateAvailable = await _metaUpdateService.CheckForAppUpdatesAsync(AppVersion);
if (updateAvailable)
{
var box = MessageBoxManager
.GetMessageBoxStandard(Assets.Resources.Warning,
string.Format(Assets.Resources.NewAppReleaseAvailable, "https://github.com/xboxoneresearch/XboxPostcodeMonitor/releases"), ButtonEnum.Ok);

await box.ShowAsync();
}
}
}

[RelayCommand]
Expand Down Expand Up @@ -267,10 +280,23 @@ private async Task ConnectAsync()
{
_logger.LogError(ex, Assets.Resources.ErrorConection);
await MessageBoxManager
.GetMessageBoxStandard(Assets.Resources.Error, string.Format(Assets.Resources.ErrorConectionMessageBoxError,ex.Message),
.GetMessageBoxStandard(Assets.Resources.Error, string.Format(Assets.Resources.ErrorConectionMessageBoxError, ex.Message),
ButtonEnum.Ok)
.ShowAsync();
}

if (IsConnected && _configurationService.Config.CheckForFwUpdates)
{
var updateAvailable = await _metaUpdateService.CheckForFirmwareUpdatesAsync(_serialService.FirmwareVersion);
if (updateAvailable)
{
var box = MessageBoxManager
.GetMessageBoxStandard(Assets.Resources.Warning,
string.Format(Assets.Resources.NewFirmwareReleaseAvailable, "https://github.com/xboxoneresearch/PicoDurangoPOST/releases"), ButtonEnum.Ok);

await box.ShowAsync();
}
}
}
}

Expand Down