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

View File

@ -106,12 +106,19 @@ final class DeviceManager {
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 {
log("Received device response \(data) without an active request")
return
}
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() {