84 lines
3.0 KiB
Swift
Executable File
84 lines
3.0 KiB
Swift
Executable File
import Vapor
|
|
|
|
extension RouteAPI {
|
|
|
|
var path: PathComponent {
|
|
.init(stringLiteral: rawValue)
|
|
}
|
|
|
|
var pathParameter: PathComponent {
|
|
.parameter(rawValue)
|
|
}
|
|
}
|
|
|
|
private func messageTransmission(_ req: Request) async throws -> Data {
|
|
guard let body = req.body.data else {
|
|
throw MessageResult.noBodyData
|
|
}
|
|
guard let message = ServerMessage(decodeFrom: body) else {
|
|
throw MessageResult.invalidMessageSize
|
|
}
|
|
guard deviceManager.authenticateRemote(message.authToken) else {
|
|
throw MessageResult.messageAuthenticationFailed
|
|
}
|
|
return try await deviceManager.sendMessageToDevice(message.message, on: req.eventLoop)
|
|
}
|
|
|
|
private func deviceStatus(_ req: Request) -> MessageResult {
|
|
guard let body = req.body.data else {
|
|
return .noBodyData
|
|
}
|
|
guard let authToken = ServerMessage.token(from: body) else {
|
|
return .invalidMessageSize
|
|
}
|
|
guard deviceManager.authenticateRemote(authToken) else {
|
|
return .messageAuthenticationFailed
|
|
}
|
|
guard deviceManager.deviceIsConnected else {
|
|
return .deviceNotConnected
|
|
}
|
|
return .deviceConnected
|
|
}
|
|
|
|
func routes(_ app: Application) throws {
|
|
|
|
/**
|
|
Get the connection status of the device.
|
|
|
|
The request expects the authentication token of the remote in the body data of the POST request.
|
|
|
|
The request returns one byte of data, which is the raw value of a `MessageResult`.
|
|
Possible results are `noBodyData`, `invalidMessageSize`, `deviceNotConnected`, `deviceConnected`.
|
|
*/
|
|
app.post(RouteAPI.getDeviceStatus.path) { request in
|
|
let result = deviceStatus(request)
|
|
return Response(status: .ok, body: .init(data: result.encoded))
|
|
}
|
|
|
|
/**
|
|
Post a message to the device for unlocking.
|
|
|
|
The expects a `ServerMessage` in the body data of the POST request, containing the valid remote authentication token and the message to send to the device.
|
|
|
|
The request returns one or `Message.length+1` bytes of data, where the first byte is the raw value of a `MessageResult`,
|
|
and the optional following bytes contain the response message of the device. This request does not complete until either the device responds or the request times out. The timeout is specified by `KeyManagement.deviceTimeout`.
|
|
*/
|
|
app.post(RouteAPI.postMessage.path) { request async throws in
|
|
do {
|
|
let result = try await messageTransmission(request)
|
|
return Response(status: .ok, body: .init(data: result))
|
|
} catch let error as MessageResult {
|
|
return Response(status: .ok, body: .init(data: error.encoded))
|
|
}
|
|
}
|
|
|
|
/**
|
|
Start a new websocket connection for the device to receive messages from the server
|
|
- Returns: Nothing
|
|
- Note: The first message from the device over the connection must be a valid auth token.
|
|
*/
|
|
app.webSocket(RouteAPI.socket.path) { req, socket async in
|
|
await deviceManager.createNewDeviceConnection(socket)
|
|
}
|
|
}
|