Skip to content

Commit

Permalink
stability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gicastel committed Dec 8, 2023
1 parent 8531615 commit 82928df
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 51 deletions.
6 changes: 3 additions & 3 deletions api/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<RootNamespace>API</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices" Version="1.38.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="5.1.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.39.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
6 changes: 4 additions & 2 deletions api/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public static async Task Run([EventHubTrigger("%EventHubName%", Connection = "Ne
return;
}

log.LogMetric("Temperature", msg.Temperature);
log.LogMetric("Humidity", msg.Humidity);
if (msg.Temperature.HasValue)
log.LogMetric("Temperature", msg.Temperature.Value);
if (msg.Humidity.HasValue)
log.LogMetric("Humidity", msg.Humidity.Value);
log.LogMetric("Setpoint", msg.CurrentSetpoint);
log.LogMetric("HeaterOn", msg.HeaterOn);

Expand Down
16 changes: 8 additions & 8 deletions device_dotnet/Nerdostat.Device.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Iot.Device.Bindings" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.41.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Iot.Device.Bindings" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.42.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<None Update=".gitignore">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>None</CopyToOutputDirectory>
</None>
<None Update="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion device_dotnet/Services/HostedThermostat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Task StartAsync(CancellationToken cancellationToken)
{
//pokemon handler
// we don't want a connection problem preventing the thermostat to work
log.LogError("Exception in main loop", ex);
log.LogError(ex, "Exception in main loop");
}
}

Expand Down
10 changes: 5 additions & 5 deletions device_dotnet/Services/HubManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private async void ConnectionStatusChangedAsync(ConnectionStatus status, Connect
case ConnectionStatus.Disconnected:
case ConnectionStatus.Disabled:
log.LogWarning(message);
// questo che blocca tutto?
// è questo che blocca tutto?
//await Initialize();
break;
}
Expand Down Expand Up @@ -227,18 +227,18 @@ private async Task<bool> SendMessage(APIMessage message, CancellationToken token
try
{
await client.SendEventAsync(iotMessage, token);
log.LogInformation($"{messageString}");
log.LogInformation("{messageString}", messageString);
}
catch (OperationCanceledException canc)
{
//this only runs if the process is cancelled from the main loop.
log.LogError("Operation cancelled", canc);
log.LogError("Operation cancelled: {canc}", canc);
EnqueueMessage(message, messageString);
return false;
}
catch (Exception ex)
{
log.LogError($"Generic exception", ex.ToString());
log.LogError("Generic exception: {exception}", ex.ToString());
EnqueueMessage(message, messageString);
return false;
}
Expand All @@ -248,7 +248,7 @@ private async Task<bool> SendMessage(APIMessage message, CancellationToken token
private void EnqueueMessage(APIMessage message, string messageString = null)
{
skippedMessages.Enqueue(message);
log.LogWarning($"Enqueued: {messageString ?? JsonConvert.SerializeObject(message)}");
log.LogWarning("Enqueued: {messageString}", messageString ?? JsonConvert.SerializeObject(message));
}

private bool ShouldClientBeInitialized(ConnectionStatus connectionStatus)
Expand Down
61 changes: 40 additions & 21 deletions device_dotnet/Services/Thermostat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,42 +149,61 @@ private decimal GetCurrentSetpoint()
bool humOk = false;
Temperature temp;
RelativeHumidity hum;
int wait = 1000;
int wait = 2000;
int loop = 1;

try
using (var controller = new GpioController())
{
using var controller = new GpioController();
try
{
var sensor = new Dht22(DhtPinNumber, PinNumberingScheme.Board, controller);

var sensor = new Dht22(DhtPinNumber, PinNumberingScheme.Board, controller);
tempOk = sensor.TryReadTemperature(out temp);
humOk = sensor.TryReadHumidity(out hum);

tempOk = sensor.TryReadTemperature(out temp);
humOk = sensor.TryReadHumidity(out hum);
humOk = humOk && hum.Percent >= 0 && hum.Percent <= 100;
tempOk = tempOk && humOk;

while (!tempOk || !humOk)
{
while (!tempOk || !humOk)
{
log.LogWarning("Sensor read failed");
if (wait < 4999)
wait += 500;
await Task.Delay(wait, token).ConfigureAwait(false);

if (!tempOk)
tempOk = sensor.TryReadTemperature(out temp);

log.LogWarning("Sensor read failed");
if (wait < 4999)
wait += 500;
await Task.Delay(wait, token).ConfigureAwait(false);
if (!humOk)
humOk = sensor.TryReadHumidity(out hum);

if (!tempOk)
tempOk = sensor.TryReadTemperature(out temp);
humOk = humOk && hum.Percent >= 0 && hum.Percent <= 100;
tempOk = tempOk && humOk;

if (!humOk)
humOk = sensor.TryReadHumidity(out hum);
loop++;

if (loop > 20)
break;
}

}
catch (OperationCanceledException ex)
{
log.LogError(ex, "Sensor read cancelled!");
return (null, null);
}
}
catch (OperationCanceledException ex)

if (loop > 20)
{
log.LogError("Sensor read cancelled!");
return (null, null);
}

log.LogInformation("Sensor read OK");

return (temp.DegreesCelsius, hum.Percent);
else
{
log.LogInformation("Sensor read OK");
return (temp.DegreesCelsius, hum.Percent);
}
}

private async ValueTask<(double? temperature, double? relativeHumidity)> GenerateValues(CancellationToken token)
Expand Down
4 changes: 2 additions & 2 deletions shared/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class APIMessage
public double? Humidity { get; set; }
public decimal CurrentSetpoint { get; set; }
public bool IsHeaterOn { get; set; }
public long OverrideEnd { get; set; }
public long HeaterOn { get; set; }
public long? OverrideEnd { get; set; }
public long? HeaterOn { get; set; }
}

public class APIResponse<T>
Expand Down
8 changes: 4 additions & 4 deletions webapp_blazor/BlazorClient.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Fast.Components.FluentUI" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Fast.Components.FluentUI" Version="3.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions webapp_blazor/Pages/Program.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ else
<div class="jumbotron vertical-center">
<div class="container">
<FluentAccordion>
@foreach (var day in status.Keys)
@* @foreach (var day in status.Keys)
{
<FluentAccordionItem [email protected](typeof(Nerdostat.Shared.PyWeekDays), day)>
<FluentDataGrid GenerateHeader=GenerateHeaderOption.None GridTemplateColumns="1fr 1fr" TItem=string>
Expand All @@ -38,7 +38,7 @@ else
</FluentDataGrid>
</FluentAccordionItem>
}
} *@
</FluentAccordion>
</div>
<div>
Expand Down
6 changes: 3 additions & 3 deletions webapp_blazor/Pages/Thermostat.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ else
<div class="container">
<div class="row "><input type="button" id="temp_plus" class="btn btn-lg btn-danger btn-block" value="+" @onclick="TempUp"/></div>
<div class="row row-eq-height">
<div id="tempValue" class="h3 font-weight-normal text-right col-6">@status.Temperature.ToString("##.#")</div>
<div id="setpointValue" class="h3 font-weight-normal text-right setpoint col-6">@status.CurrentSetpoint.ToString("##.#")</div>
<div id="tempValue" class="h3 font-weight-normal text-right col-6">@status.Temperature.Value.ToString("F1")</div>
<div id="setpointValue" class="h3 font-weight-normal text-right setpoint col-6">@status.CurrentSetpoint.ToString()</div>
</div>
<div class="row"><input type="button" id="temp_minus" class="btn btn-lg btn-primary btn-block" value="-" @onclick="TempDown" /></div>
<div class="row row-eq-height">
Expand All @@ -37,7 +37,7 @@ else
<div class="row row-eq-height">
<div class="h5 font-weight-normal text-center col-3">Humidity:</div>
<div class="h5 font-weight-normal text-center col-3">
<div id="humidityValue">@status.Humidity.ToString("##")</div>
<div id="humidityValue">@status.Humidity.Value.ToString()</div>
<div class="h5 font-weight-normal text-center col-3"></div>
<div class="h5 font-weight-normal text-center col-3"></div>
</div>
Expand Down

0 comments on commit 82928df

Please sign in to comment.