Skip to content

Commit

Permalink
Merge pull request #4 from jiilaa/net60
Browse files Browse the repository at this point in the history
.NET 6.0
  • Loading branch information
jiilaa authored Jul 23, 2022
2 parents fd364ef + fe62817 commit 97dcbf5
Show file tree
Hide file tree
Showing 40 changed files with 654 additions and 408 deletions.
9 changes: 9 additions & 0 deletions Common/AlertSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace net.jommy.RuuviCore.Common
{
public class AlertSettings
{
public List<string> EndPoints { get; set; }
}
}
8 changes: 8 additions & 0 deletions Common/AlertThresholds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace net.jommy.RuuviCore.Common;

public class AlertThresholds
{
public decimal? MinValidValue { get; set; }

public decimal? MaxValidValue { get; set; }
}
4 changes: 2 additions & 2 deletions Common/Common.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>net.jommy.RuuviCore.Common</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Orleans.Streams.Utils" Version="9.0.0" />
<PackageReference Include="Orleans.Streams.Utils" Version="11.1.0" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions Common/InfluxSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class InfluxSettings
public string InfluxDatabase { get; set; }
public string InfluxMeasurementTable { get; set; }

public string Username { get; set; }

public string Password { get; set; }

public InfluxSettings()
{
InfluxAddress = DefaultInfluxAddress;
Expand Down
1 change: 1 addition & 0 deletions Common/RuuviCoreConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace net.jommy.RuuviCore.Common
public static class RuuviCoreConstants
{
public const string GrainStorageName = "RuuviStorage";
public const string StreamProviderName = "RuuviStreamProvider";
}
}
8 changes: 4 additions & 4 deletions FileStorageProvider/FileStorageProvider.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>net.jommy.Orleans</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Core" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Core" Version="3.6.2" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.2" />
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.6.2" />
</ItemGroup>

</Project>
10 changes: 7 additions & 3 deletions GrainInterfaces/GrainInterfaces.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>net.jommy.RuuviCore.Interfaces</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.3.0">
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

</Project>
21 changes: 8 additions & 13 deletions GrainInterfaces/MeasurementEnvelope.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using System;

namespace net.jommy.RuuviCore.Interfaces
{
[Serializable]
public class MeasurementEnvelope
{
public MeasurementEnvelope()
{
}
namespace net.jommy.RuuviCore.Interfaces;

public string MacAddress { get; set; }
public DateTime Timestamp { get; set; }
public short SignalStrength { get; set; }
public byte[] Data { get; set; }
}
[Serializable]
public class MeasurementEnvelope
{
public string MacAddress { get; set; }
public DateTime Timestamp { get; set; }
public short? SignalStrength { get; set; }
public byte[] Data { get; set; }
}
6 changes: 5 additions & 1 deletion GrainInterfaces/Measurements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public class Measurements

public override string ToString()
{
return $"[#{SequenceNumber} @ Timestamp:{Timestamp}. Humidity:{Humidity}, Temperature:{Temperature}, Pressure:{Pressure}, BatteryVoltage:{BatteryVoltage}, Acceleration:{Acceleration}, RSSI:{RSSI}, TX Power:{TransmissionPower}, MovementCounter:{MovementCounter}";
if (Acceleration != null)
{
return $"[#{SequenceNumber} @ {Timestamp}. Humidity:{Humidity}, Temperature:{Temperature}, Pressure:{Pressure}, Battery:{BatteryVoltage}, Acceleration:{Acceleration}, RSSI:{RSSI}, Movement:{MovementCounter}";
}
return $"[#{SequenceNumber} @ {Timestamp}. Humidity:{Humidity}, Temperature:{Temperature}, Pressure:{Pressure}, Battery:{BatteryVoltage}, RSSI:{RSSI}, Movement:{MovementCounter}";
}
}
}
4 changes: 3 additions & 1 deletion GrainInterfaces/RuuviTag.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using net.jommy.RuuviCore.Common;

namespace net.jommy.RuuviCore.Interfaces
{
Expand All @@ -12,7 +14,7 @@ public class RuuviTag
public bool StoreName { get; set; }
public bool DiscardMinMaxValues { get; set; }
public bool AllowMeasurementsThroughGateway { get; set; }

public bool IncludeInDashboard { get; set; }
public IDictionary<string, AlertThresholds> AlertRules { get; set; }
}
}
25 changes: 23 additions & 2 deletions GrainServices/AbstractDeviceListener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using bluez.DBus;
Expand All @@ -10,17 +11,21 @@ namespace net.jommy.RuuviCore.GrainServices
{
public abstract class AbstractDeviceListener : IDeviceListener
{
private const int AliveThreshold = 60;
private const string SignalStrengthKeyName = "RSSI";
private const string ManufacturerDataKeyName = "ManufacturerData";

private readonly IDevice1 _device;
private IDisposable _propertiesWatcher;
private int _aliveCounter;

protected readonly string DeviceAddress;
protected readonly IGrainFactory GrainFactory;

protected abstract Task HandlePropertiesChanged(byte[] manufacturerData);
protected abstract Task HandlePropertiesChanged(byte[] manufacturerData, short? signalStrength);

protected abstract Task OnStartListening();

protected abstract ushort ManufacturerKey { get; }

protected AbstractDeviceListener(IDevice1 device, string deviceAddress, IGrainFactory grainFactory)
Expand All @@ -43,13 +48,28 @@ public async Task StartListeningAsync()
_propertiesWatcher = await _device.WatchPropertiesAsync(OnPropertiesChanged);
}

public bool IsAlive()
{
_aliveCounter++;
return _aliveCounter <= AliveThreshold;
}

protected Task<short> GetSignalStrength()
{
return _device.GetAsync<short>(SignalStrengthKeyName);
}

public async Task HandleDataAsync(IDictionary<ushort, object> manufacturerData)
{
if (manufacturerData.TryGetValue(ManufacturerKey, out var bytes))
{
await HandlePropertiesChanged((byte[])bytes, null);
}
}

private async void OnPropertiesChanged(PropertyChanges changes)
{
_aliveCounter = 0;
try
{
var manufacturerDataChange = changes.Changed.FirstOrDefault(c => c.Key == ManufacturerDataKeyName);
Expand All @@ -61,7 +81,8 @@ private async void OnPropertiesChanged(PropertyChanges changes)
var dict = (IDictionary)manufacturerDataChange.Value;
if (dict.Contains(ManufacturerKey))
{
await HandlePropertiesChanged((byte[]) dict[ManufacturerKey]);
var signalStrength = await GetSignalStrength();
await HandlePropertiesChanged((byte[]) dict[ManufacturerKey], signalStrength);
}
}
catch (Exception e)
Expand Down
31 changes: 21 additions & 10 deletions GrainServices/DBusListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class DBusListener : GrainService, IRuuviDBusListener
private readonly DeviceListenerFactory _deviceListenerFactory;
private readonly ILogger<DBusListener> _logger;

public DBusListener(IGrainIdentity grainId, Silo silo, ILoggerFactory loggerFactory, IGrainFactory grainFactory, IOptions<DBusSettings> dbusOptions) : base(grainId, silo, loggerFactory)
public DBusListener(IGrainIdentity grainId, Silo silo, ILoggerFactory loggerFactory, IGrainFactory grainFactory, IOptions<DBusSettings> dbusOptions)
: base(grainId, silo, loggerFactory)
{
_dbusSettings = dbusOptions.Value;
_deviceListenerFactory = new DeviceListenerFactory(grainFactory, loggerFactory);
Expand Down Expand Up @@ -91,8 +92,8 @@ private async void MainLoop()
}
catch (Exception e)
{
_logger.LogWarning(e, "DBUS error: {errorMessage}", e.Message);
_logger.LogInformation("Ruuvi DBUS Listener NOT listening for bluetooth events.");
_logger.LogError(e, "DBUS error: {errorMessage}", e.Message);
_logger.LogError("Ruuvi DBUS Listener NOT listening for bluetooth events.");
}
}

Expand All @@ -111,7 +112,7 @@ private async void OnDeviceAdded((ObjectPath objectPath, IDictionary<string, IDi
}
catch (Exception e)
{
_logger.LogWarning(e, "Error occurred when discovering a device: {errorMessage}.", e.Message);
_logger.LogError(e, "Error occurred when discovering a device: {errorMessage}.", e.Message);
}
}

Expand All @@ -138,21 +139,31 @@ private async Task RegisterDevice(ObjectPath objectPath)
if (manufacturerData != null)
{
var address = await device.GetAddressAsync();
// It seems sometimes devices are found again, so let's dispose old observers so DBUS limits will not get exceeded.
if (_deviceListeners.Remove(address, out var oldListener))

if (_deviceListeners.TryGetValue(address, out var existingListener))
{
_logger.LogDebug("Disposing old observer.", address);
oldListener.Dispose();
}
if (existingListener.IsAlive())
{
_logger.LogDebug("Using old device listener with address {address} to handle manufacturer data.", address);
await existingListener.HandleDataAsync(manufacturerData);
return;
}

// Devices are found again with certain interval. If old listener hasn't had data for a while, let's dispose it and start a new one.
_logger.LogInformation("Disposing old device listener with address {address}.", address);
_deviceListeners.Remove(address);
existingListener.Dispose();
}

if (_deviceListenerFactory.TryConstructDeviceListener(device, address, manufacturerData, out var deviceListener))
{
_deviceListeners[address] = deviceListener;
await deviceListener.StartListeningAsync();
await deviceListener.HandleDataAsync(manufacturerData);
}
else
{
_logger.LogDebug("Unsupported manufacturer, ignoring.");
_logger.LogInformation("Unsupported manufacturer, ignoring: {data}.", manufacturerData);
}
}
else
Expand Down
12 changes: 6 additions & 6 deletions GrainServices/GrainServices.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>net.jommy.RuuviCore.GrainServices</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.3.0">
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.OrleansRuntime" Version="3.3.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Tmds.DBus" Version="0.9.0" />
<PackageReference Include="Microsoft.Orleans.OrleansRuntime" Version="3.6.2" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Tmds.DBus" Version="0.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions GrainServices/IDeviceListener.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace net.jommy.RuuviCore.GrainServices
{
public interface IDeviceListener : IDisposable
{
Task StartListeningAsync();

bool IsAlive();

Task HandleDataAsync(IDictionary<ushort, object> manufacturerData);
}
}
7 changes: 4 additions & 3 deletions GrainServices/RuuviTagListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ public RuuviTagListener(IDevice1 device, string deviceAddress, IGrainFactory gra
_grainAddress = deviceAddress.ToActorGuid();
}

protected override async Task HandlePropertiesChanged(byte[] manufacturerData)
protected override async Task HandlePropertiesChanged(byte[] manufacturerData, short? signalStrength)
{
_logger.LogDebug("Publishing ruuvi data to ruuvi actor {MAC}", DeviceAddress);
await GrainFactory.GetGrain<IRuuviStreamWorker>(0).Publish(
DeviceAddress,
new MeasurementEnvelope
{
MacAddress = DeviceAddress,
Timestamp = DateTime.UtcNow,
SignalStrength = await GetSignalStrength(),
SignalStrength = signalStrength,
Data = manufacturerData
});
}
Expand All @@ -42,7 +43,7 @@ protected override async Task OnStartListening()
var name = await ruuviTag.GetName();
if (name != null)
{
_logger.LogInformation("RuuviTag {name} ({mac}) found.", name, DeviceAddress);
_logger.LogInformation("Listening ruuvitag {name} ({mac}).", name, DeviceAddress);
}
else
{
Expand Down
18 changes: 9 additions & 9 deletions Grains/Grains.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>net.jommy.RuuviCore.Grains</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="InfluxDB.Collector" Version="1.1.1" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.31.2" />
<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Client" Version="1.16.1" />
<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Transport.Mqtt" Version="1.13.1" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.3.0">
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.41.0" />
<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Client" Version="1.19.1" />
<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Transport.Mqtt" Version="1.17.1" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.3.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.2" />
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.6.2" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 97dcbf5

Please sign in to comment.