diff --git a/Sources/App/KeyManagement.swift b/Sources/App/KeyManagement.swift index 93e11c1..e76571a 100644 --- a/Sources/App/KeyManagement.swift +++ b/Sources/App/KeyManagement.swift @@ -28,31 +28,16 @@ final class KeyManagement { /// The id of the key which was sent to the device private var keyInTransit: (id: UInt16, promise: EventLoopPromise)? - /// The result transmitted by the device for the sent key - var keyResult: KeyResult = .none - init(deviceKey: String) { self.deviceKey = deviceKey } // MARK: API - var deviceResponse: String { - guard let keyId = keyInTransit else { - return "No key" - } - return "\(keyId):\(keyResult.rawValue)" - } - var deviceStatus: String { deviceIsConnected ? "1" : "0" } - func clearClientRequest() { - keyInTransit = nil - keyResult = .none - } - func sendKeyToDevice(_ key: Data, keyId: UInt16, on eventLoop: EventLoop) -> EventLoopFuture { guard key.count == KeyManagement.keySize else { return eventLoop.makeSucceededFuture(.invalidPayloadSize) @@ -107,11 +92,7 @@ final class KeyManagement { promise.succeed(.unexpectedSocketEvent) return } - guard keyInTransit != nil else { - print("No key in transit for response \(response)") - return - } - keyResult = response + promise.succeed(response) } func didCloseDeviceSocket() { diff --git a/Sources/App/Response.swift b/Sources/App/Response.swift index 1b4306b..44dcbf7 100644 --- a/Sources/App/Response.swift +++ b/Sources/App/Response.swift @@ -5,16 +5,13 @@ import Foundation */ enum KeyResult: UInt8 { - /// No result from the device, or state not applicable - case none = 0 - /// Text content was received, although binary data was expected case textReceived = 1 /// A socket event on the device was unexpected (not binary data) case unexpectedSocketEvent = 2 - /// The size of the payload (key id + key data) was invalid + /// The size of the payload (key id + key data, or just key) was invalid case invalidPayloadSize = 3 /// The index of the key was out of bounds @@ -33,23 +30,51 @@ enum KeyResult: UInt8 { case keyAccepted = 8 /// The device produced an unknown error - case uknownDeviceError = 9 - - /// The device is not connected through the socket - case notConnected = 10 + case unknownDeviceError = 9 /// The request did not contain body data with the key - case noBodyData = 11 + case noBodyData = 10 /// The body data could not be read - case corruptkeyData = 12 + case corruptkeyData = 11 /// The device is not connected - case deviceNotConnected = 13 + case deviceNotConnected = 12 /// The device did not respond within the timeout - case deviceTimedOut = 14 - - /// The key was valid and the door will be opened - case success = 15 + case deviceTimedOut = 13 +} + +extension KeyResult: CustomStringConvertible { + + var description: String { + switch self { + case .invalidKeyIndex: + return "Invalid key id (too large)" + case .noBodyData: + return "No body data included in the request" + case .invalidPayloadSize: + return "Invalid key size" + case .corruptkeyData: + return "Key data corrupted" + case .deviceNotConnected: + return "Device not connected" + case .textReceived: + return "The device received unexpected text" + case .unexpectedSocketEvent: + return "Unexpected socket event for the device" + case .invalidKey: + return "The transmitted key was not correct" + case .keyAlreadyUsed: + return "The transmitted key was already used" + case .keyWasSkipped: + return "A newer key was already used" + case .keyAccepted: + return "Key successfully sent" + case .unknownDeviceError: + return "The device experienced an unknown error" + case .deviceTimedOut: + return "The device did not respond" + } + } } diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 22d2f90..f6cfc98 100755 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -38,26 +38,6 @@ func routes(_ app: Application) throws { keyManager.deviceStatus } - /** - Get the response from the device. - - The response is a string of an integer `rawValue` of a `KeyResult` - */ - app.get(PublicAPI.getDeviceResponse.path) { req -> String in - keyManager.deviceResponse - } - - /** - Post a request to remove the information about the last key transmission. - - - The request always succeeds and returns the string "Success" - */ - app.post(PublicAPI.clearKeyRequest.path) { req -> String in - keyManager.clearClientRequest() - return "Success" - } - - /** Post a key to the device for unlocking.