Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/HAP/Endpoints/Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum Protocol {
case int(Int)
case double(Double)
case string(String)
case bool(Bool)

enum DecodeError: Error {
case unsupportedValueType
Expand All @@ -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
}
Expand All @@ -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)
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions Sources/HAP/Endpoints/characteristics().swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 byteCount = try? request.readAllData(into: &body)
Comment thread
gbrooker marked this conversation as resolved.
Outdated
logger.debug("PUT data: \(String(bytes: body, encoding: .utf8))")
guard byteCount != nil,
let decoded = try? JSONDecoder().decode(Protocol.CharacteristicContainer.self, from: body)
else {
logger.warning("Could not decode JSON")
return .badRequest
}
Expand Down Expand Up @@ -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 {
Expand Down