Skip to content

Commit 306af90

Browse files
committed
consolidate parameter count logging when decoding commands
1 parent 5b8fbea commit 306af90

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Sources/SwiftOCADevice/OCC/ControlClasses/Root+Commands.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@
1818
import SwiftOCA
1919

2020
public extension OcaRoot {
21+
private nonisolated static func _logUnexpectedParameterCount(
22+
_ command: Ocp1Command,
23+
expected responseParameterCount: UInt8? = nil
24+
) {
25+
Task {
26+
await OcaDevice.shared.logger.info(
27+
"OcaRoot.decodeCommand(\(command)): unexpected parameter count \(command.parameters.parameterCount), expected \(responseParameterCount != nil ? "\(responseParameterCount!)" : "none")"
28+
)
29+
}
30+
}
31+
2132
nonisolated static func decodeCommand<U: Decodable>(
2233
_ command: Ocp1Command
2334
) throws -> U {
2435
let responseParameterCount = _ocp1ParameterCount(type: U.self)
2536
let response = try Ocp1Decoder().decode(U.self, from: command.parameters.parameterData)
2637
if command.parameters.parameterCount != responseParameterCount {
27-
Task {
28-
await OcaDevice.shared.logger.info(
29-
"OcaRoot.decodeCommand(\(command)): unexpected parameter count \(command.parameters.parameterCount), expected \(responseParameterCount)"
30-
)
31-
}
38+
_logUnexpectedParameterCount(command, expected: responseParameterCount)
3239
throw Ocp1Error.status(.parameterOutOfRange)
3340
}
3441
return response
@@ -46,11 +53,7 @@ public extension OcaRoot {
4653
guard command.parameters.parameterCount == 0,
4754
command.parameters.parameterData.isEmpty
4855
else {
49-
Task {
50-
await OcaDevice.shared.logger.info(
51-
"OcaRoot.decodeCommand(\(command)): unexpected parameter count \(command.parameters.parameterCount), expected none"
52-
)
53-
}
56+
Self._logUnexpectedParameterCount(command)
5457
throw Ocp1Error.status(.parameterOutOfRange)
5558
}
5659
}

0 commit comments

Comments
 (0)