Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Bjorn committed Sep 28, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 6cada86 + fd37b4c commit 4cf7dd1
Showing 9 changed files with 114 additions and 22 deletions.
3 changes: 3 additions & 0 deletions FloatingGlucose/App.config
Original file line number Diff line number Diff line change
@@ -62,6 +62,9 @@
<setting name="GuiOpacity" serializeAs="String">
<value>85</value>
</setting>
<setting name="AllowFileURIScheme" serializeAs="String">
<value>False</value>
</setting>
</FloatingGlucose.Properties.Settings>
</userSettings>
<startup>
20 changes: 18 additions & 2 deletions FloatingGlucose/Classes/Pebble/PebbleData.cs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
@@ -161,12 +162,27 @@ public string DirectionArrow
}


public static async Task<PebbleData> GetNightscoutPebbleDataAsync(string url)
public static async Task<PebbleData> GetNightscoutPebbleDataAsync(string uri)
{

var client = new HttpClient();
var pebbleData = new PebbleData();
string urlContents = await client.GetStringAsync(url);
string urlContents;

if(Default.AllowFileURIScheme && uri.ToLower().StartsWith("file:///"))
{
using (var reader = File.OpenText(new Uri(uri).LocalPath))
{
urlContents = await reader.ReadToEndAsync();

}

}
else
{
urlContents = await client.GetStringAsync(uri);
}


Bg bgs = null;

7 changes: 5 additions & 2 deletions FloatingGlucose/Classes/Validators.cs
Original file line number Diff line number Diff line change
@@ -8,14 +8,17 @@ namespace FloatingGlucose.Classes
{
class Validators
{
public static bool IsUrl(string url) {
public static bool IsUrl(string url, bool allowFileScheme) {
var isWellFormed = url != null && Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
if (!isWellFormed) {
return false;
}
System.Uri uriResult;
return Uri.TryCreate(url, UriKind.Absolute, out uriResult) &&
(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
(
(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps) ||
(uriResult.Scheme == Uri.UriSchemeFile && allowFileScheme)
);



24 changes: 17 additions & 7 deletions FloatingGlucose/FloatingGlucose.Designer.cs

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

34 changes: 27 additions & 7 deletions FloatingGlucose/FloatingGlucose.cs
Original file line number Diff line number Diff line change
@@ -27,7 +27,13 @@ public partial class FloatingGlucose : Form//AutoPositionedForm
private string nsURL {
get {

//AlarmUrgentLow
if (Default.AllowFileURIScheme &&
Default.NightscoutSite.ToLower().StartsWith("file:///") )
{
// not adding any query string params for file uri's
return Default.NightscoutSite;
}

// With raw glucose display we need to have two
// data points to calculate raw glucose diff
var count = Default.EnableRawGlucoseDisplay ? 2 : 1;
@@ -182,7 +188,7 @@ private void setLabelsColor(Color color) {
//
private async void LoadGlucoseValue()
{
if (!Validators.IsUrl(this.nsURL)) {
if (!Validators.IsUrl(this.nsURL, Default.AllowFileURIScheme)) {
this.showErrorMessage("The nightscout_site setting is not specifed or invalid. Please update it from the settings!");
return;

@@ -250,7 +256,7 @@ private async void LoadGlucoseValue()
$"{data.Glucose:N1} {arrow}" : $"{data.Glucose:N0} {arrow}";

this.notifyIcon1.Text = "BG: " + this.lblGlucoseValue.Text;
var status = GlucoseStatus.GetGlucoseStatus((decimal) data.Glucose);
var status = GlucoseStatus.GetGlucoseStatus((decimal)data.Glucose);


this.lblDelta.Text = data.FormattedDelta + " " + (Default.GlucoseUnits == "mmol" ? "mmol/L" : "mg/dL");
@@ -288,6 +294,13 @@ private async void LoadGlucoseValue()
}


}
catch (FileNotFoundException ex)
{
//will only happen during debugging, when the allow file:/// scheme is set
this.showErrorMessage($"Could not find file '{ex.FileName}'!");
this.SetErrorState(ex);
return;
}
catch (IOException ex)
{
@@ -307,7 +320,8 @@ private async void LoadGlucoseValue()
//typically happens during azure site restarts
this.SetErrorState(ex);
}
catch (JsonSerializationException ex) {
catch (JsonSerializationException ex)
{
//typically happens during azure site restarts
this.SetErrorState(ex);
}
@@ -319,6 +333,7 @@ private async void LoadGlucoseValue()
this.settingsForm.Visible = false;
this.settingsForm.ShowDialog();
}

catch (Exception ex)
{
var msg = "An unknown error occured of type " + ex.GetType().ToString() + ": " + ex.Message;
@@ -446,7 +461,7 @@ private void FloatingGlucose_Load(object sender, EventArgs e)
AppShared.RegisterSettingsChangedCallback(Settings_Changed_Event);


if (!Validators.IsUrl(this.nsURL)) {
if (!Validators.IsUrl(this.nsURL , Default.AllowFileURIScheme)) {
this.settingsForm.Visible = false;
this.settingsForm.ShowDialog();

@@ -562,15 +577,15 @@ private void reenableAlarmsToolStripMenuItem_Click(object sender, EventArgs e)
private void openNightscoutSiteToolStripMenuItem_Click(object sender, EventArgs e)
{
var url = Default.NightscoutSite;
if (Validators.IsUrl(url))
if (Validators.IsUrl(url, Default.AllowFileURIScheme))
{
try
{
Process.Start(url);
}
catch (Win32Exception ex)
{
this.showErrorMessage("Could not open your nightscout site in the system default browser!");
this.showErrorMessage($"Could not open your nightscout site in the system default browser! {ex.Message}");
}

}
@@ -581,5 +596,10 @@ private void openNightscoutSiteToolStripMenuItem_Click(object sender, EventArgs
}

}

private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LoadGlucoseValue();
}
}
}
27 changes: 26 additions & 1 deletion FloatingGlucose/FormGlucoseSettings.Designer.cs

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

6 changes: 3 additions & 3 deletions FloatingGlucose/FormGlucoseSettings.cs
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ private void updateFormControlsFromSettings() {
this.chkEnableRAWGlucose.Checked = Default.EnableRawGlucoseDisplay;

this.chkDisableSoundOnWorkstationLock.Checked = Default.DisableSoundAlarmsOnWorkstationLock;

this.chkAllowFileURIScheme.Checked = Default.AllowFileURIScheme;
//this is the default in the settings file
//override it so it makes sense
if (nsurl == "https://...")
@@ -166,7 +166,7 @@ private void btnEnableAlarms_CheckedChanged(object sender, EventArgs e)

private void btnVerifySubmit_Click(object sender, EventArgs e)
{
if (!Validators.IsUrl(this.txtNSURL.Text) || this.txtNSURL.Text == "https://mysite.azurewebsites.net") {
if (!Validators.IsUrl(this.txtNSURL.Text, this.chkAllowFileURIScheme.Checked) || this.txtNSURL.Text == "https://mysite.azurewebsites.net") {
MessageBox.Show("You have entered an invalid nightscout site URL", AppShared.AppName, MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
@@ -192,7 +192,7 @@ private void btnVerifySubmit_Click(object sender, EventArgs e)

Default.DisableSoundAlarmsOnWorkstationLock = this.chkDisableSoundOnWorkstationLock.Checked;
Default.EnableRawGlucoseDisplay = this.chkEnableRAWGlucose.Checked;

Default.AllowFileURIScheme = this.chkAllowFileURIScheme.Checked;
Default.Save();


12 changes: 12 additions & 0 deletions FloatingGlucose/Properties/Settings.Designer.cs

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

3 changes: 3 additions & 0 deletions FloatingGlucose/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -50,5 +50,8 @@
<Setting Name="GuiOpacity" Type="System.Int32" Scope="User">
<Value Profile="(Default)">85</Value>
</Setting>
<Setting Name="AllowFileURIScheme" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>

0 comments on commit 4cf7dd1

Please sign in to comment.