diff --git a/Sources/HAP/Endpoints/Protocol.swift b/Sources/HAP/Endpoints/Protocol.swift index 610ba581..467c30ff 100644 --- a/Sources/HAP/Endpoints/Protocol.swift +++ b/Sources/HAP/Endpoints/Protocol.swift @@ -44,6 +44,7 @@ enum Protocol { case int(Int) case double(Double) case string(String) + case bool(Bool) enum DecodeError: Error { case unsupportedValueType @@ -57,6 +58,8 @@ enum Protocol { self = .double(double) } else if let string = try? container.decode(String.self) { self = .string(string) + } else if let bool = try? container.decode(Bool.self) { + self = .bool(bool) } else { throw DecodeError.unsupportedValueType } @@ -71,6 +74,8 @@ enum Protocol { try container.encode(double) case let .string(string): try container.encode(string) + case let .bool(bool): + try container.encode(bool) } } } diff --git a/Sources/HAP/Endpoints/characteristics().swift b/Sources/HAP/Endpoints/characteristics().swift index 78f7e00f..e44724c0 100644 --- a/Sources/HAP/Endpoints/characteristics().swift +++ b/Sources/HAP/Endpoints/characteristics().swift @@ -104,10 +104,11 @@ func characteristics(device: Device) -> Application { case "PUT": var body = Data() - guard - (try? request.readAllData(into: &body)) != nil, - let decoded = try? JSONDecoder().decode(Protocol.CharacteristicContainer.self, from: body) else - { + let bytesRead = try? request.readAllData(into: &body) + logger.debug("PUT data: \(String(bytes: body, encoding: .utf8))") + guard bytesRead != nil, + let decoded = try? JSONDecoder().decode(Protocol.CharacteristicContainer.self, from: body) + else { logger.warning("Could not decode JSON") return .badRequest } @@ -148,6 +149,8 @@ func characteristics(device: Device) -> Application { try characteristic.setValue(int, fromConnection: connection) case let .double(double): try characteristic.setValue(double, fromConnection: connection) + case let .bool(bool): + try characteristic.setValue(bool, fromConnection: connection) } status.status = .success } catch {