Skip to content

Commit

Permalink
* Updated Dependencies
Browse files Browse the repository at this point in the history
* HomematicIpClient ILogging Support
* File cleanup
* Version bump
  • Loading branch information
AleRoe committed Nov 11, 2023
1 parent e44e0a2 commit 32e6be3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 562 deletions.
14 changes: 3 additions & 11 deletions src/AleRoe.HomematicIPApi/AleRoe.HomematicIpApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Company>AleRoe</Company>
<Copyright>Alexander Röthinger 2022</Copyright>
<PackageOutputPath>C:\Users\Alexander\Source\LocalPackages</PackageOutputPath>
<Version>2.1.4</Version>
<Version>2.1.7</Version>
<AssemblyName>AleRoe.HomematicIpApi</AssemblyName>
<RootNamespace>AleRoe.HomematicIpApi</RootNamespace>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
Expand All @@ -26,17 +26,9 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="HomematicListener.cs" />
<Compile Remove="HomematicRpcService.cs" />
<Compile Remove="JsonClient.cs" />
<Compile Remove="Model\Devices\DeviceBase.cs" />
<Compile Remove="Model\Devices\SabotageDeviceBase.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Websocket.Client" Version="4.6.1" />
<PackageReference Include="Websocket.Client" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
50 changes: 40 additions & 10 deletions src/AleRoe.HomematicIPApi/HomematicIpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using AleRoe.HomematicIpApi.Model.Devices;
using AleRoe.HomematicIpApi.Model.Groups;
using AleRoe.HomematicIpApi.Rpc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
Expand Down Expand Up @@ -31,8 +33,9 @@ namespace AleRoe.HomematicIpApi
public class HomematicIpClient : IDisposable, IHomematicRpcService
{
private bool disposedValue;
private readonly bool disposeHttpClient;

private readonly bool disposeHttpClient = true;
private readonly bool disposeLoggerFactory = true;

private const string GetHostUri = @"https://lookup.homematic.com:48335/getHost";
private const string CLIENTAUTH = "CLIENTAUTH";
private const string AUTHTOKEN = "AUTHTOKEN";
Expand All @@ -45,6 +48,7 @@ public class HomematicIpClient : IDisposable, IHomematicRpcService

internal readonly HttpClient httpClient;
internal WebsocketClient socketClient;
internal readonly ILoggerFactory loggerFactory;

private readonly Subject<PushEventArgs> messageReceivedSubject = new();
private IDisposable messageSubscription;
Expand Down Expand Up @@ -83,27 +87,49 @@ public class HomematicIpClient : IDisposable, IHomematicRpcService
/// </summary>
/// <param name="homematicIpConfiguration">The <see cref="HomematicIpConfiguration"/> to use for sending requests.</param>
/// <exception cref="ArgumentNullException"></exception>
public HomematicIpClient(HomematicIpConfiguration homematicIpConfiguration) : this(homematicIpConfiguration, new HttpClient())
public HomematicIpClient(HomematicIpConfiguration homematicIpConfiguration) : this(homematicIpConfiguration, new HttpClient(), new NullLoggerFactory()) {}

/// <summary>
/// Initializes a new instance of the <see cref="HomematicIpClient"/> class with the specified <see cref="HomematicIpConfiguration"/> configuration and <see cref="HttpClient"/> instance.
/// </summary>
/// <param name="homematicIpConfiguration">The <see cref="HomematicIpConfiguration"/> to use for sending requests.</param>
/// <param name="httpClient">The <see cref="HttpClient"/> instance to use for sending requests. The instance will not be disposed.</param>
/// <exception cref="ArgumentNullException"></exception>
public HomematicIpClient(HomematicIpConfiguration homematicIpConfiguration, HttpClient httpClient) : this(homematicIpConfiguration, httpClient, new NullLoggerFactory())
{
disposeHttpClient = true;
disposeHttpClient = false;
}

/// <summary>
/// Initializes a new instance of the <see cref="HomematicIpClient"/> class with the specified <see cref="HomematicIpConfiguration"/> configuration and <see cref="HttpClient"/> instance.
/// Initializes a new instance of the <see cref="HomematicIpClient"/> class with the specified <see cref="HomematicIpConfiguration"/> configuration and <see cref="ILoggerFactory"/> instance.
/// </summary>
/// <param name="homematicIpConfiguration">The <see cref="HomematicIpConfiguration"/> to use for sending requests.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> instance to use for logger creation. The instance will not be disposed.</param>
/// <exception cref="ArgumentNullException"></exception>
public HomematicIpClient(HomematicIpConfiguration homematicIpConfiguration, ILoggerFactory loggerFactory) : this(homematicIpConfiguration, new HttpClient(), loggerFactory)
{
disposeLoggerFactory = false;
}


/// <summary>
/// Initializes a new instance of the <see cref="HomematicIpClient"/> class with the specified <see cref="HomematicIpConfiguration"/> configuration and <see cref="HttpClient"/> and <see cref="ILoggerFactory"/> instances.
/// </summary>
/// <param name="homematicIpConfiguration">The <see cref="HomematicIpConfiguration"/> to use for sending requests.</param>
/// <param name="httpClient">The <see cref="HttpClient"/> instance to use for sending requests. The instance will not be disposed.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> instance to use for logger creation. The instance will not be disposed.</param>
/// <exception cref="ArgumentNullException"></exception>
public HomematicIpClient(HomematicIpConfiguration homematicIpConfiguration, HttpClient httpClient)
public HomematicIpClient(HomematicIpConfiguration homematicIpConfiguration, HttpClient httpClient, ILoggerFactory loggerFactory)
{
this.homematicIpConfiguration = homematicIpConfiguration ?? throw new ArgumentNullException(nameof(homematicIpConfiguration));
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
this.JsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
TraceWriter = new DiagnosticsTraceWriter(),
MissingMemberHandling = MissingMemberHandling.Error,
NullValueHandling = NullValueHandling.Include
NullValueHandling = NullValueHandling.Include,
};
this.JsonSerializerSettings.Error += SerializerError;
}
Expand Down Expand Up @@ -134,7 +160,9 @@ public async Task InitializeAsync(CancellationToken cancellationToken)
return client;
});

socketClient = new WebsocketClient(new Uri(hosts.WebSocketUrl), factory)
var logger = loggerFactory.CreateLogger<WebsocketClient>();

socketClient = new WebsocketClient(new Uri(hosts.WebSocketUrl), logger, factory)
{
ReconnectTimeout = homematicIpConfiguration.IdleReconnectTimout,
IsReconnectionEnabled = homematicIpConfiguration.ReconnectionEnabled
Expand Down Expand Up @@ -288,16 +316,18 @@ private void Dispose(bool disposing)
{
if (disposing)
{
JsonSerializerSettings.Error -= SerializerError;
messageSubscription?.Dispose();
connectSubscription?.Dispose();
disconnectSubscription?.Dispose();
socketClient?.StopOrFail(WebSocketCloseStatus.NormalClosure, "Closing").Wait(TimeSpan.FromMilliseconds(5000));
socketClient?.Dispose();

if (disposeHttpClient)
{
httpClient?.Dispose();
}

if (disposeLoggerFactory)
loggerFactory?.Dispose();
}
disposedValue = true;
}
Expand Down
212 changes: 0 additions & 212 deletions src/AleRoe.HomematicIPApi/HomematicListener.cs

This file was deleted.

Loading

0 comments on commit 32e6be3

Please sign in to comment.