Skip to content

Commit

Permalink
Fix baseurl and device-connectivity usage string (Azure-Samples#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoviton authored Oct 15, 2018
1 parent b6d32af commit 861eca7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
14 changes: 10 additions & 4 deletions device-connectivity/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class Program
private static IConfigurationSection settings;
static void Main(string[] args)
{
if (args.Length != 0)
{
Console.WriteLine("Usage: dotnet run\nNo arguments are supported");
return;
}

settings = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
Expand Down Expand Up @@ -57,15 +63,15 @@ static Func<string> CreateGetRandomSensorReading(string sensorDataType, int iter
case "Motion":
if (iteration % 6 < 3)
return () => "false";
else
else
return () => "true";

case "Temperature":
return () => rnd.Next(70, 100).ToString(CultureInfo.InvariantCulture);
case "CarbonDioxide":
case "CarbonDioxide":
if (iteration % 6 < 3)
return () => rnd.Next(800, 1000).ToString(CultureInfo.InvariantCulture);
else
else
return () => rnd.Next(1000, 1100).ToString(CultureInfo.InvariantCulture);
}
}
Expand Down Expand Up @@ -106,7 +112,7 @@ static async Task SendEvent(DeviceClient deviceClient)
await deviceClient.SendEventAsync(eventMessage);
}
}

await Task.Delay(TimeSpan.FromSeconds(delayPerMessageSend));

} while (++curIteration < maxIterations);
Expand Down
15 changes: 14 additions & 1 deletion occupancy-quickstart/docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@
Exception: YamlDotNet.Core.SyntaxErrorException: (Line: X, Col: Y, Idx: ZZ) - (Line: X, Col: Y, Idx: ZZ): While scanning for the next token, find character that cannot start any token.
```

Yaml files don't support tab characters. NOTE: that visual studio 2017 doesn't know (without customizations) to insert spaces instead of tabs into yaml files. More details [here](https://developercommunity.visualstudio.com/content/problem/71238/editor-inserts-tabs-instead-of-spaces-for-yaml-fil.html)
Yaml files don't support tab characters. NOTE: that visual studio 2017 doesn't know (without customizations) to insert spaces instead of tabs into yaml files. More details [here](https://developercommunity.visualstudio.com/content/problem/71238/editor-inserts-tabs-instead-of-spaces-for-yaml-fil.html)


#### Response Status: 404, NotFound

```
trce: DigitalTwinsQuickstart[0]
Request: GET https://yourResource.yourLocation.azuresmartspaces.net/management/somethingWrong
trce: DigitalTwinsQuickstart[0]
Response Status: 404, NotFound,
```

A 404 usually indicates the `BaseUrl` in appSettings.json (or appSettings.dev.json) is misconfigured.

20 changes: 18 additions & 2 deletions occupancy-quickstart/src/appSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.IO;
using Microsoft.Extensions.Configuration;

Expand All @@ -15,12 +16,27 @@ public class AppSettings {
public string BaseUrl { get; set; }
public string Authority => AADInstance + Tenant;

public static AppSettings Load() =>
new ConfigurationBuilder()
public static AppSettings Load()
{
var appSettings = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.dev.json", optional: true)
.Build()
.Get<AppSettings>();

// Sanitize input

// This is because httpClient will behave differently if the
// passed in Uri has a trailing slash when using GetAsync.
appSettings.BaseUrl = EnsureTrailingSlash(appSettings.BaseUrl);

return appSettings;
}

private static string EnsureTrailingSlash(string baseUrl)
=> baseUrl.Length == 0 || baseUrl[baseUrl.Length-1] == '/'
? baseUrl
: baseUrl + '/';
}
}
4 changes: 2 additions & 2 deletions occupancy-quickstart/src/loggers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public static class Loggers
{
public static ILogger SilentLogger =
new Microsoft.Extensions.Logging.LoggerFactory()
.CreateLogger("DigitalTwinsQuickstartTests");
.CreateLogger("DigitalTwinsQuickstart");
public static ILogger ConsoleLogger =
new Microsoft.Extensions.Logging.LoggerFactory()
.AddConsole(LogLevel.Trace)
.CreateLogger("DigitalTwinsQuickstartTests");
.CreateLogger("DigitalTwinsQuickstart");
}
}

0 comments on commit 861eca7

Please sign in to comment.