Sesame-Server/Sources/App/DeviceManager.swift

109 lines
3.2 KiB
Swift
Raw Normal View History

2022-01-24 17:17:06 +01:00
import Foundation
import WebSocketKit
import Vapor
2022-04-07 23:53:25 +02:00
final class DeviceManager {
/// The seconds to wait for a response from the device
static let deviceTimeout: Int64 = 20
2022-01-24 17:17:06 +01:00
/// The connection to the device
private var connection: WebSocket?
2022-04-07 23:53:25 +02:00
/// The authentication token of the device for the socket connection
2022-01-24 17:17:06 +01:00
private let deviceKey: String
2022-04-07 23:53:25 +02:00
/// Indicate that the socket is fully initialized with an authorized device
2022-01-24 17:17:06 +01:00
var deviceIsAuthenticated = false
2022-04-08 23:38:22 +02:00
private var isOpeningNewConnection = false
2022-01-24 17:17:06 +01:00
/// Indicator for device availability
var deviceIsConnected: Bool {
2022-04-08 23:38:22 +02:00
deviceIsAuthenticated && !(connection?.isClosed ?? true)
2022-01-24 17:17:06 +01:00
}
2022-04-07 23:53:25 +02:00
/// A promise to finish the request once the device responds or times out
private var requestInProgress: EventLoopPromise<DeviceResponse>?
2022-01-24 17:17:06 +01:00
init(deviceKey: String) {
self.deviceKey = deviceKey
}
// MARK: API
var deviceStatus: String {
deviceIsConnected ? "1" : "0"
}
2022-04-07 23:53:25 +02:00
func sendMessageToDevice(_ message: Message, on eventLoop: EventLoop) -> EventLoopFuture<DeviceResponse> {
2022-01-24 17:17:06 +01:00
guard let socket = connection, !socket.isClosed else {
connection = nil
return eventLoop.makeSucceededFuture(.deviceNotConnected)
2022-01-24 17:17:06 +01:00
}
2022-04-07 23:53:25 +02:00
guard requestInProgress == nil else {
return eventLoop.makeSucceededFuture(.operationInProgress)
}
requestInProgress = eventLoop.makePromise(of: DeviceResponse.self)
socket.send(message.bytes, promise: nil)
eventLoop.scheduleTask(in: .seconds(Self.deviceTimeout)) { [weak self] in
2022-04-07 23:53:25 +02:00
guard let promise = self?.requestInProgress else {
return
}
2022-04-07 23:53:25 +02:00
self?.requestInProgress = nil
promise.succeed(.deviceTimedOut)
}
2022-04-07 23:53:25 +02:00
return requestInProgress!.futureResult
2022-01-24 17:17:06 +01:00
}
func authenticateDevice(psk: String) {
guard psk == self.deviceKey else {
print("Invalid device key")
_ = connection?.close()
deviceIsAuthenticated = false
return
}
print("Device authenticated")
deviceIsAuthenticated = true
}
func processDeviceResponse(_ data: ByteBuffer) {
2022-04-07 23:53:25 +02:00
guard let promise = requestInProgress else {
2022-01-24 17:17:06 +01:00
return
}
2022-04-07 23:53:25 +02:00
defer { requestInProgress = nil }
promise.succeed(DeviceResponse(data) ?? .unexpectedSocketEvent)
2022-01-24 17:17:06 +01:00
}
func didCloseDeviceSocket() {
2022-04-08 23:38:22 +02:00
guard !isOpeningNewConnection else {
return
}
2022-01-24 17:17:06 +01:00
deviceIsAuthenticated = false
guard connection != nil else {
2022-04-08 23:38:22 +02:00
print("Socket closed, but no connection anyway")
2022-01-24 17:17:06 +01:00
return
}
connection = nil
print("Socket closed")
}
func removeDeviceConnection() {
2022-04-08 23:38:22 +02:00
deviceIsAuthenticated = false
guard let socket = connection else {
return
}
try? socket.close().wait()
2022-01-24 17:17:06 +01:00
connection = nil
2022-04-08 23:38:22 +02:00
print("Removed device connection")
2022-01-24 17:17:06 +01:00
}
func createNewDeviceConnection(_ socket: WebSocket) {
2022-04-08 23:38:22 +02:00
isOpeningNewConnection = true
2022-01-24 17:17:06 +01:00
removeDeviceConnection()
connection = socket
print("Socket connected")
2022-04-08 23:38:22 +02:00
isOpeningNewConnection = false
2022-01-24 17:17:06 +01:00
}
}