Skip to content

Commit 29fa6f3

Browse files
gbrookerBouke
authored andcommitted
Fix for HomePod (add boolean value type) (#61)
* Fix for HomePod (add boolean value type) * Rename byteCount to bytesRead
1 parent 8bf2839 commit 29fa6f3

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

Sources/HAP/Endpoints/Protocol.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ enum Protocol {
4444
case int(Int)
4545
case double(Double)
4646
case string(String)
47+
case bool(Bool)
4748

4849
enum DecodeError: Error {
4950
case unsupportedValueType
@@ -57,6 +58,8 @@ enum Protocol {
5758
self = .double(double)
5859
} else if let string = try? container.decode(String.self) {
5960
self = .string(string)
61+
} else if let bool = try? container.decode(Bool.self) {
62+
self = .bool(bool)
6063
} else {
6164
throw DecodeError.unsupportedValueType
6265
}
@@ -71,6 +74,8 @@ enum Protocol {
7174
try container.encode(double)
7275
case let .string(string):
7376
try container.encode(string)
77+
case let .bool(bool):
78+
try container.encode(bool)
7479
}
7580
}
7681
}

Sources/HAP/Endpoints/characteristics().swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ func characteristics(device: Device) -> Application {
104104

105105
case "PUT":
106106
var body = Data()
107-
guard
108-
(try? request.readAllData(into: &body)) != nil,
109-
let decoded = try? JSONDecoder().decode(Protocol.CharacteristicContainer.self, from: body) else
110-
{
107+
let bytesRead = try? request.readAllData(into: &body)
108+
logger.debug("PUT data: \(String(bytes: body, encoding: .utf8))")
109+
guard bytesRead != nil,
110+
let decoded = try? JSONDecoder().decode(Protocol.CharacteristicContainer.self, from: body)
111+
else {
111112
logger.warning("Could not decode JSON")
112113
return .badRequest
113114
}
@@ -148,6 +149,8 @@ func characteristics(device: Device) -> Application {
148149
try characteristic.setValue(int, fromConnection: connection)
149150
case let .double(double):
150151
try characteristic.setValue(double, fromConnection: connection)
152+
case let .bool(bool):
153+
try characteristic.setValue(bool, fromConnection: connection)
151154
}
152155
status.status = .success
153156
} catch {

0 commit comments

Comments
 (0)