More logging

This commit is contained in:
Christoph Hagen 2023-08-08 16:32:25 +02:00
parent a53c12b02c
commit 5d4adf8b15
2 changed files with 19 additions and 11 deletions

View File

@ -54,27 +54,28 @@ struct DeviceResponse {
the remaining bytes contain the message. the remaining bytes contain the message.
- Parameter buffer: The buffer where the message bytes are stored - Parameter buffer: The buffer where the message bytes are stored
*/ */
init?(_ buffer: ByteBuffer, request: String) { init?(_ data: Data, request: String) {
guard let byte = buffer.getBytes(at: 0, length: 1) else { guard let byte = data.first else {
log("\(request): No bytes received from device") log("\(request): No bytes received from device")
return nil return nil
} }
guard let event = MessageResult(rawValue: byte[0]) else { guard let event = MessageResult(rawValue: byte) else {
log("\(request): Unknown response \(byte[0]) received from device") log("\(request): Unknown response \(byte) received from device")
return nil return nil
} }
self.event = event self.event = event
guard let data = buffer.getSlice(at: 1, length: Message.length) else { let messageData = data.dropFirst()
log("\(request): Insufficient data received from device (expected \(Message.length + 1), got \(buffer.readableBytes))") guard !messageData.isEmpty else {
// TODO: Check if event should have response message
self.response = nil self.response = nil
return return
} }
guard let message = Message(decodeFrom: data) else { guard messageData.count == Message.length else {
log("\(request): Failed to decode message received from device") log("\(request): Insufficient message data received from device (expected \(Message.length), got \(messageData.count))")
self.response = nil self.response = nil
return return
} }
self.response = message self.response = Message(decodeFrom: data)
} }
/** /**

View File

@ -106,12 +106,19 @@ final class DeviceManager {
return hash == remoteKey return hash == remoteKey
} }
func processDeviceResponse(_ data: ByteBuffer) { func processDeviceResponse(_ buffer: ByteBuffer) {
guard let data = buffer.getData(at: 0, length: buffer.readableBytes) else {
log("Failed to get data buffer received from device")
return
}
guard let promise = requestInProgress else { guard let promise = requestInProgress else {
log("Received device response \(data) without an active request")
return return
} }
defer { requestInProgress = nil } defer { requestInProgress = nil }
promise.succeed(DeviceResponse(data, request: RouteAPI.socket.rawValue) ?? .unexpectedSocketEvent) let response = DeviceResponse(data, request: RouteAPI.socket.rawValue) ?? .unexpectedSocketEvent
log("Device response received")
promise.succeed(response)
} }
func didCloseDeviceSocket() { func didCloseDeviceSocket() {