Skip to content

Commit 3ac0066

Browse files
committed
New characteristic updated signature (Supports async)
1 parent d8aa961 commit 3ac0066

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

ExampleClient/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ private static async Task Main(string[] args)
4242
Console.ReadLine();
4343
}
4444

45-
private static void ConfiguredName_Updated(object? sender, string? newValue)
45+
private static Task ConfiguredName_Updated(Service service, StringCharacteristic characteristic, string? newValue)
4646
{
47-
Console.WriteLine("Previous Name: " + ((ConfiguredName)sender!).LastValue);
47+
Console.WriteLine("Previous Name: " + ((ConfiguredName)characteristic).LastValue);
4848
Console.WriteLine("New Value: " + newValue);
4949
//After this event is handled, LastValue is set to newValue
50+
return Task.CompletedTask;
5051
}
5152
}

HomeKitDotNet/Models/Characteristic.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace HomeKitDotNet.Models
1717
{
1818
public class Characteristic<T> : CharacteristicBase where T : struct
1919
{
20-
public event EventHandler<T?>? Updated;
20+
public delegate Task AsyncEventHandler<TEventArgs>(Service service, Characteristic<TEventArgs> characteristic, TEventArgs? newValue) where TEventArgs : struct;
21+
public event AsyncEventHandler<T>? Updated;
2122
protected Characteristic(Service service, CharacteristicJSON json) : base(service, json)
2223
{
2324
this.LastValue = MapValue(json.Value);
@@ -52,6 +53,8 @@ protected async Task<bool> Write(T value)
5253
{
5354
if (value.HasValue)
5455
{
56+
if (typeof(T).IsEnum && Enum.GetUnderlyingType(typeof(T)) == typeof(byte) && value.Value.ValueKind == JsonValueKind.Number)
57+
return (T)(object)value.Value.GetByte();
5558
if (typeof(T) == typeof(float) && value.Value.ValueKind == JsonValueKind.Number)
5659
return (T)(object)value.Value.GetSingle();
5760
else if (typeof(T) == typeof(int) && value.Value.ValueKind == JsonValueKind.Number)
@@ -81,7 +84,7 @@ internal override void FireUpdate(JsonElement? value)
8184
{
8285
T? newVal = MapValue(value);
8386
if (Updated != null)
84-
Updated.Invoke(this, newVal);
87+
Updated.Invoke(service, this, newVal);
8588
LastValue = newVal;
8689
}
8790

HomeKitDotNet/Models/StringCharacteristic.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace HomeKitDotNet.Models
55
{
66
public class StringCharacteristic : CharacteristicBase
77
{
8-
public event EventHandler<string?>? Updated;
8+
public delegate Task AsyncEventHandler(Service service, StringCharacteristic characteristic, string? newValue);
9+
public event AsyncEventHandler? Updated;
910
protected StringCharacteristic(Service service, CharacteristicJSON json) : base(service, json)
1011
{
1112
LastValue = MapValue(json.Value);
@@ -53,7 +54,7 @@ internal override void FireUpdate(JsonElement? value)
5354
{
5455
string? newVal = MapValue(value);
5556
if (Updated != null)
56-
Updated.Invoke(this, newVal);
57+
Updated.Invoke(service, this, newVal);
5758
LastValue = newVal;
5859
}
5960

0 commit comments

Comments
 (0)