Skip to content

Commit

Permalink
More work towards encapsulating our glucose result entity. Moved dexc…
Browse files Browse the repository at this point in the history
…om and nightscout classes into their own folders.
  • Loading branch information
Delubear committed Sep 26, 2024
1 parent d5548d2 commit 3da2cfc
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 185 deletions.
2 changes: 2 additions & 0 deletions GlucoseTray.Domain/DependencyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.FetchResults;
using GlucoseTray.Domain.FetchResults.Dexcom;
using GlucoseTray.Domain.FetchResults.Nightscout;
using Microsoft.Extensions.DependencyInjection;

namespace GlucoseTray.Domain;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlucoseTray.Domain.FetchResults;
namespace GlucoseTray.Domain.FetchResults.Dexcom;

/// <summary>
/// Class that maps to the JSON received from DexCom queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.Enums;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Text.Json;

namespace GlucoseTray.Domain.FetchResults;
namespace GlucoseTray.Domain.FetchResults.Dexcom;

public interface IDexcomService
{
Task<GlucoseResult> GetLatestReadingAsync();
}

public class DexcomService : IDexcomService
public class DexcomService(ISettingsProxy settings, ILogger<DexcomService> logger, UrlAssembler urlBuilder, IExternalCommunicationAdapter externalAdapter, DebugService debug) : IDexcomService
{
private readonly ISettingsProxy _options;
private readonly ILogger _logger;
private readonly UrlAssembler _urlBuilder;
private readonly IExternalCommunicationAdapter _externalAdapter;
private readonly DebugService _debug;

public DexcomService(ISettingsProxy options, ILogger<DexcomService> logger, UrlAssembler urlBuilder, IExternalCommunicationAdapter externalAdapter, DebugService debug)
{
_options = options;
_logger = logger;
_urlBuilder = urlBuilder;
_externalAdapter = externalAdapter;
_debug = debug;
}
private readonly ISettingsProxy _settings = settings;
private readonly ILogger _logger = logger;
private readonly UrlAssembler _urlBuilder = urlBuilder;
private readonly IExternalCommunicationAdapter _externalAdapter = externalAdapter;
private readonly DebugService _debug = debug;

public async Task<GlucoseResult> GetLatestReadingAsync()
{
Expand All @@ -51,7 +40,7 @@ public async Task<GlucoseResult> GetLatestReadingAsync()
catch (Exception ex)
{
_logger.LogError(ex, "Dexcom fetching failed or received incorrect format.");
if (_options.IsDebugMode)
if (_settings.IsDebugMode)
_debug.ShowDebugAlert(ex, "Dexcom result fetch");

glucoseResult = GlucoseResult.Default;
Expand All @@ -62,16 +51,16 @@ public async Task<GlucoseResult> GetLatestReadingAsync()

private GlucoseResult MapToResult(DexcomResult data)
{
GlucoseResult result = new();
GlucoseResult glucoseResult = new();

var unixTime = string.Join("", data.ST.Where(char.IsDigit));
var trend = data.Trend;

GlucoseMath.CalculateValues(result, data.Value, _options);
result.DateTimeUTC = !string.IsNullOrWhiteSpace(unixTime) ? DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(unixTime)).UtcDateTime : DateTime.MinValue;
result.Trend = trend.GetTrend();
result.Source = FetchMethod.DexcomShare;
return result;
glucoseResult.SetGlucoseValues(data.Value, _settings);
glucoseResult.SetDateTimeUtc(!string.IsNullOrWhiteSpace(unixTime) ? DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(unixTime)).UtcDateTime : DateTime.MinValue);
glucoseResult.SetTrend(trend.GetTrend());

return glucoseResult;
}

private async Task<string> GetApiResponse(string sessionId)
Expand All @@ -87,7 +76,7 @@ private async Task<string> GetSessionId(string accountId)
{
accountId,
applicationId = "d8665ade-9673-4e27-9ff6-92db4ce13d13",
password = _options.DexcomPassword
password = _settings.DexcomPassword
});

var sessionUrl = _urlBuilder.BuildDexComSessionUrl();
Expand All @@ -110,9 +99,9 @@ private async Task<string> GetAccountId()
{
var accountIdRequestJson = JsonSerializer.Serialize(new
{
accountName = _options.DexcomUsername,
accountName = _settings.DexcomUsername,
applicationId = "d8665ade-9673-4e27-9ff6-92db4ce13d13",
password = _options.DexcomPassword
password = _settings.DexcomPassword
});

var accountUrl = _urlBuilder.BuildDexComAccountIdUrl();
Expand Down
2 changes: 2 additions & 0 deletions GlucoseTray.Domain/FetchResults/GlucoseFetchService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GlucoseTray.Domain.Enums;
using GlucoseTray.Domain.FetchResults.Dexcom;
using GlucoseTray.Domain.FetchResults.Nightscout;
using Microsoft.Extensions.Logging;

namespace GlucoseTray.Domain.FetchResults;
Expand Down
34 changes: 0 additions & 34 deletions GlucoseTray.Domain/FetchResults/GlucoseMath.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace GlucoseTray.Domain.FetchResults;
namespace GlucoseTray.Domain.FetchResults.Nightscout;

/// <summary>
/// Class that maps to the JSON from NightScout queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.Enums;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Text.Json;

namespace GlucoseTray.Domain.FetchResults;
namespace GlucoseTray.Domain.FetchResults.Nightscout;

public interface INightscoutService
{
Task<GlucoseResult> GetLatestReadingAsync();
}

public class NightscoutService(ISettingsProxy options, ILogger<NightscoutService> logger, UrlAssembler urlBuilder, IExternalCommunicationAdapter externalAdapter, DebugService debug) : INightscoutService
public class NightscoutService(ISettingsProxy settings, ILogger<NightscoutService> logger, UrlAssembler urlBuilder, IExternalCommunicationAdapter externalAdapter, DebugService debug) : INightscoutService
{
private readonly ISettingsProxy _options = options;
private readonly ISettingsProxy _settings = settings;
private readonly ILogger _logger = logger;
private readonly UrlAssembler _urlBuilder = urlBuilder;
private readonly IExternalCommunicationAdapter _externalAdapter = externalAdapter;
Expand All @@ -23,7 +22,7 @@ public async Task<GlucoseResult> GetLatestReadingAsync()
{
_debug.ClearDebugText();
_debug.AddDebugText("Starting Nightscout Fetch");
_debug.AddDebugText(!string.IsNullOrWhiteSpace(_options.AccessToken) ? "Using access token." : "No access token.");
_debug.AddDebugText(!string.IsNullOrWhiteSpace(_settings.AccessToken) ? "Using access token." : "No access token.");

GlucoseResult result = new();

Expand All @@ -40,7 +39,7 @@ public async Task<GlucoseResult> GetLatestReadingAsync()
catch (Exception ex)
{
_logger.LogError(ex, "Nightscout fetching failed or received incorrect format.");
if (_options.IsDebugMode)
if (_settings.IsDebugMode)
_debug.ShowDebugAlert(ex, "Nightscout result fetch");
}

Expand All @@ -49,14 +48,11 @@ public async Task<GlucoseResult> GetLatestReadingAsync()

private GlucoseResult MapToResult(NightScoutResult data)
{
GlucoseResult result = new()
{
Source = FetchMethod.NightscoutApi,
DateTimeUTC = !string.IsNullOrEmpty(data.DateString) ? DateTime.Parse(data.DateString).ToUniversalTime() : DateTimeOffset.FromUnixTimeMilliseconds(data.Date).UtcDateTime,
Trend = data.Direction.GetTrend()
};
GlucoseResult result = new();

GlucoseMath.CalculateValues(result, data.Sgv, _options);
result.SetDateTimeUtc(!string.IsNullOrEmpty(data.DateString) ? DateTime.Parse(data.DateString).ToUniversalTime() : DateTimeOffset.FromUnixTimeMilliseconds(data.Date).UtcDateTime);
result.SetTrend(data.Direction.GetTrend());
result.SetGlucoseValues(data.Sgv, _settings);

if (result.Trend == TrendResult.Unknown)
_logger.LogWarning("Un-expected value for direction/Trend {Direction}", data.Direction);
Expand Down
46 changes: 37 additions & 9 deletions GlucoseTray.Domain/GlucoseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,48 @@ namespace GlucoseTray.Domain;

public class GlucoseResult
{
public int MgValue { get; set; }
public double MmolValue { get; set; }
public DateTime DateTimeUTC { get; set; }
public TrendResult Trend { get; set; }
public bool WasError { get; set; }
public FetchMethod Source { get; set; }
public bool IsCriticalLow { get; set; }
public int MgValue { get; private set; }
public double MmolValue { get; private set; }
public DateTime DateTimeUTC { get; private set; }
public TrendResult Trend { get; private set; }
public bool IsCriticalLow { get; private set; }

public static GlucoseResult Default => new()
{
MmolValue = 0,
MgValue = 0,
DateTimeUTC = DateTime.Now.ToUniversalTime(),
DateTimeUTC = DateTime.UtcNow,
Trend = TrendResult.Unknown,
WasError = true
};

public void SetTrend(TrendResult trend) => Trend = trend;

public void SetDateTimeUtc(DateTime dateTimeUtc) => DateTimeUTC = dateTimeUtc;

public void SetGlucoseValues(double value, ISettingsProxy currentSettings)
{
if (value == 0)
{
MmolValue = 0;
MgValue = 0;
}
else if (currentSettings.IsServerDataUnitTypeMmol)
{
MmolValue = value;
MgValue = Convert.ToInt32(value * 18);
}
else
{
MmolValue = value / 18;
MgValue = Convert.ToInt32(value);
}
IsCriticalLow = IsCriticalLowCalculation(currentSettings);
}

private bool IsCriticalLowCalculation(ISettingsProxy currentSettings)
{
if (MmolValue == 0) // Don't treat a zero / null / default result as critical low.
return false;
return (currentSettings.GlucoseUnit == GlucoseUnitType.MMOL && MmolValue <= currentSettings.CriticalLowBg) || (currentSettings.GlucoseUnit == GlucoseUnitType.MG && MgValue <= currentSettings.CriticalLowBg);
}
}
Loading

0 comments on commit 3da2cfc

Please sign in to comment.