Skip to content

Commit 17dcdaa

Browse files
committed
Refactor characteristics
1 parent a9fe50b commit 17dcdaa

File tree

6 files changed

+51
-65
lines changed

6 files changed

+51
-65
lines changed

HomeKitDotNet/HomeKitDotNet.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
<PackageIcon>logo.png</PackageIcon>
1919
</PropertyGroup>
2020

21+
<ItemGroup>
22+
<None Include="..\README.md">
23+
<Pack>True</Pack>
24+
<PackagePath>\</PackagePath>
25+
</None>
26+
</ItemGroup>
27+
2128
<ItemGroup>
2229
<None Update="logo.png">
2330
<Pack>True</Pack>

HomeKitDotNet/Models/Characteristic.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515

1616
namespace HomeKitDotNet.Models
1717
{
18-
public class Characteristic<T> : ICharacteristic where T : struct
18+
public class Characteristic<T> : CharacteristicBase where T : struct
1919
{
20-
private Service service;
21-
private CharacteristicJSON _json;
22-
protected Characteristic(Service service, CharacteristicJSON json)
20+
protected Characteristic(Service service, CharacteristicJSON json) : base(service, json)
2321
{
24-
this.service = service;
25-
this._json = json;
26-
this.Type = json.Type;
2722
MapValue(json.Value);
2823
}
2924

@@ -52,13 +47,6 @@ protected async Task<bool> Write(T value)
5247
return LastValue;
5348
}
5449

55-
protected async Task Subscribe()
56-
{
57-
if (!CanSubscribe)
58-
throw new InvalidOperationException("Subscriptions are prohibited");
59-
//TODO
60-
}
61-
6250
protected void MapValue(JsonElement? value)
6351
{
6452
if (value.HasValue)
@@ -88,21 +76,11 @@ protected void MapValue(JsonElement? value)
8876
LastValue = null;
8977
}
9078

91-
public string Type { get; init; }
9279
public T? LastValue { get; private set; }
93-
public int InstanceID => _json.InstanceID;
94-
public CharacteristicPermission[] Permissions => _json.Permissions;
95-
public string? Description => _json.Description;
96-
public string? Unit => _json.Description;
9780
public float? MinValue => _json.MinValue;
9881
public float? MaxValue => _json.MaxValue;
9982
public float? MinStep => _json.MinStep;
10083
public float[]? ValidValues => _json.ValidValues;
10184
public float[]? ValidValuesRange => _json.ValidValuesRange;
102-
103-
104-
public bool CanRead { get { return Permissions.Contains(CharacteristicPermission.pr); } }
105-
public bool CanWrite { get { return Permissions.Contains(CharacteristicPermission.pw); } }
106-
public bool CanSubscribe { get { return Permissions.Contains(CharacteristicPermission.ev); } }
10785
}
10886
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using HomeKitDotNet.JSON;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace HomeKitDotNet.Models
9+
{
10+
public class CharacteristicBase
11+
{
12+
protected Service service;
13+
protected CharacteristicJSON _json;
14+
15+
public CharacteristicBase(Service service, CharacteristicJSON json)
16+
{
17+
this._json = json;
18+
this.Type = json.Type;
19+
this.service = service;
20+
}
21+
22+
protected async Task<bool> Events(bool subscribe)
23+
{
24+
if (!CanSubscribe)
25+
throw new InvalidOperationException("Subscriptions are prohibited");
26+
//TODO
27+
return false;
28+
}
29+
30+
public string Type { get; init; }
31+
public int InstanceID => _json.InstanceID;
32+
public string? Description => _json.Description;
33+
public string? Unit => _json.Description;
34+
public CharacteristicPermission[] Permissions => _json.Permissions;
35+
public bool CanRead { get { return Permissions.Contains(CharacteristicPermission.pr); } }
36+
public bool CanWrite { get { return Permissions.Contains(CharacteristicPermission.pw); } }
37+
public bool CanSubscribe { get { return Permissions.Contains(CharacteristicPermission.ev); } }
38+
}
39+
}

HomeKitDotNet/Models/ICharacteristic.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

HomeKitDotNet/Models/Service.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected Service(Accessory accessory, string type)
2525
public string Type { get; init; }
2626
public Accessory Accessory { get; init; }
2727

28-
protected ICharacteristic? GetCharacteristic(string type, ServiceJSON service)
28+
protected CharacteristicBase? GetCharacteristic(string type, ServiceJSON service)
2929
{
3030
CharacteristicJSON? json = service.Characteristics.FirstOrDefault(c => c.Type == type);
3131
switch (type)

HomeKitDotNet/Models/StringCharacteristic.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33

44
namespace HomeKitDotNet.Models
55
{
6-
public class StringCharacteristic : ICharacteristic
6+
public class StringCharacteristic : CharacteristicBase
77
{
8-
private Service service;
9-
private CharacteristicJSON _json;
10-
protected StringCharacteristic(Service service, CharacteristicJSON json)
8+
protected StringCharacteristic(Service service, CharacteristicJSON json) : base(service, json)
119
{
12-
this._json = json;
13-
this.Type = json.Type;
1410
MapValue(json.Value);
15-
this.service = service;
1611
}
1712

1813
protected async Task<bool> Write(string value)
@@ -40,11 +35,6 @@ protected async Task<bool> Write(string value)
4035
return LastValue;
4136
}
4237

43-
protected async Task Subscribe()
44-
{
45-
//TODO
46-
}
47-
4838
protected void MapValue(JsonElement? value)
4939
{
5040
if (value.HasValue)
@@ -58,18 +48,8 @@ protected void MapValue(JsonElement? value)
5848
LastValue = null;
5949
}
6050

61-
public string Type { get; init; }
6251
public string? LastValue { get; set; }
63-
public int InstanceID => _json.InstanceID;
64-
public CharacteristicPermission[] Permissions => _json.Permissions;
65-
66-
public string? Description => _json.Description;
67-
public string? Unit => _json.Description;
6852
public float? MaxLength => _json.MaxLength;
6953
public float? MaxDataLength => _json.MaxDataLength;
70-
71-
public bool CanRead { get { return Permissions.Contains(CharacteristicPermission.pr); } }
72-
public bool CanWrite { get { return Permissions.Contains(CharacteristicPermission.pw); } }
73-
public bool CanSubscribe { get { return Permissions.Contains(CharacteristicPermission.ev); } }
7454
}
7555
}

0 commit comments

Comments
 (0)