Clean up unused routes, combine responses
This commit is contained in:
parent
54c2fda911
commit
9ab32cc758
@ -28,31 +28,16 @@ final class KeyManagement {
|
|||||||
/// The id of the key which was sent to the device
|
/// The id of the key which was sent to the device
|
||||||
private var keyInTransit: (id: UInt16, promise: EventLoopPromise<KeyResult>)?
|
private var keyInTransit: (id: UInt16, promise: EventLoopPromise<KeyResult>)?
|
||||||
|
|
||||||
/// The result transmitted by the device for the sent key
|
|
||||||
var keyResult: KeyResult = .none
|
|
||||||
|
|
||||||
init(deviceKey: String) {
|
init(deviceKey: String) {
|
||||||
self.deviceKey = deviceKey
|
self.deviceKey = deviceKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: API
|
// MARK: API
|
||||||
|
|
||||||
var deviceResponse: String {
|
|
||||||
guard let keyId = keyInTransit else {
|
|
||||||
return "No key"
|
|
||||||
}
|
|
||||||
return "\(keyId):\(keyResult.rawValue)"
|
|
||||||
}
|
|
||||||
|
|
||||||
var deviceStatus: String {
|
var deviceStatus: String {
|
||||||
deviceIsConnected ? "1" : "0"
|
deviceIsConnected ? "1" : "0"
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearClientRequest() {
|
|
||||||
keyInTransit = nil
|
|
||||||
keyResult = .none
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendKeyToDevice(_ key: Data, keyId: UInt16, on eventLoop: EventLoop) -> EventLoopFuture<KeyResult> {
|
func sendKeyToDevice(_ key: Data, keyId: UInt16, on eventLoop: EventLoop) -> EventLoopFuture<KeyResult> {
|
||||||
guard key.count == KeyManagement.keySize else {
|
guard key.count == KeyManagement.keySize else {
|
||||||
return eventLoop.makeSucceededFuture(.invalidPayloadSize)
|
return eventLoop.makeSucceededFuture(.invalidPayloadSize)
|
||||||
@ -107,11 +92,7 @@ final class KeyManagement {
|
|||||||
promise.succeed(.unexpectedSocketEvent)
|
promise.succeed(.unexpectedSocketEvent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
guard keyInTransit != nil else {
|
promise.succeed(response)
|
||||||
print("No key in transit for response \(response)")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
keyResult = response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func didCloseDeviceSocket() {
|
func didCloseDeviceSocket() {
|
||||||
|
@ -5,16 +5,13 @@ import Foundation
|
|||||||
*/
|
*/
|
||||||
enum KeyResult: UInt8 {
|
enum KeyResult: UInt8 {
|
||||||
|
|
||||||
/// No result from the device, or state not applicable
|
|
||||||
case none = 0
|
|
||||||
|
|
||||||
/// Text content was received, although binary data was expected
|
/// Text content was received, although binary data was expected
|
||||||
case textReceived = 1
|
case textReceived = 1
|
||||||
|
|
||||||
/// A socket event on the device was unexpected (not binary data)
|
/// A socket event on the device was unexpected (not binary data)
|
||||||
case unexpectedSocketEvent = 2
|
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
|
case invalidPayloadSize = 3
|
||||||
|
|
||||||
/// The index of the key was out of bounds
|
/// The index of the key was out of bounds
|
||||||
@ -33,23 +30,51 @@ enum KeyResult: UInt8 {
|
|||||||
case keyAccepted = 8
|
case keyAccepted = 8
|
||||||
|
|
||||||
/// The device produced an unknown error
|
/// The device produced an unknown error
|
||||||
case uknownDeviceError = 9
|
case unknownDeviceError = 9
|
||||||
|
|
||||||
/// The device is not connected through the socket
|
|
||||||
case notConnected = 10
|
|
||||||
|
|
||||||
/// The request did not contain body data with the key
|
/// The request did not contain body data with the key
|
||||||
case noBodyData = 11
|
case noBodyData = 10
|
||||||
|
|
||||||
/// The body data could not be read
|
/// The body data could not be read
|
||||||
case corruptkeyData = 12
|
case corruptkeyData = 11
|
||||||
|
|
||||||
/// The device is not connected
|
/// The device is not connected
|
||||||
case deviceNotConnected = 13
|
case deviceNotConnected = 12
|
||||||
|
|
||||||
/// The device did not respond within the timeout
|
/// The device did not respond within the timeout
|
||||||
case deviceTimedOut = 14
|
case deviceTimedOut = 13
|
||||||
|
}
|
||||||
/// The key was valid and the door will be opened
|
|
||||||
case success = 15
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,26 +38,6 @@ func routes(_ app: Application) throws {
|
|||||||
keyManager.deviceStatus
|
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.
|
Post a key to the device for unlocking.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user