Extract config
This commit is contained in:
parent
bf755b4d50
commit
e6fc0308ed
13
Sources/App/Config.swift
Normal file
13
Sources/App/Config.swift
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
|
||||||
|
/// The port where the server runs
|
||||||
|
static let port = 6003
|
||||||
|
|
||||||
|
/// The name of the file in the `Resources` folder containing the device authentication token
|
||||||
|
static let keyFileName = "deviceKey"
|
||||||
|
|
||||||
|
/// The seconds to wait for a response from the device
|
||||||
|
static let deviceTimeout: Int64 = 20
|
||||||
|
}
|
@ -3,9 +3,6 @@ import WebSocketKit
|
|||||||
import Vapor
|
import Vapor
|
||||||
|
|
||||||
final class DeviceManager {
|
final class DeviceManager {
|
||||||
|
|
||||||
/// The seconds to wait for a response from the device
|
|
||||||
static let deviceTimeout: Int64 = 20
|
|
||||||
|
|
||||||
/// The connection to the device
|
/// The connection to the device
|
||||||
private var connection: WebSocket?
|
private var connection: WebSocket?
|
||||||
@ -46,7 +43,7 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
requestInProgress = eventLoop.makePromise(of: DeviceResponse.self)
|
requestInProgress = eventLoop.makePromise(of: DeviceResponse.self)
|
||||||
socket.send(message.bytes, promise: nil)
|
socket.send(message.bytes, promise: nil)
|
||||||
eventLoop.scheduleTask(in: .seconds(Self.deviceTimeout)) { [weak self] in
|
eventLoop.scheduleTask(in: .seconds(Config.deviceTimeout)) { [weak self] in
|
||||||
guard let promise = self?.requestInProgress else {
|
guard let promise = self?.requestInProgress else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,10 @@ var deviceManager: DeviceManager!
|
|||||||
|
|
||||||
// configures your application
|
// configures your application
|
||||||
public func configure(_ app: Application) throws {
|
public func configure(_ app: Application) throws {
|
||||||
app.http.server.configuration.port = 6003
|
app.http.server.configuration.port = Config.port
|
||||||
|
|
||||||
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
let keyFile = storageFolder.appendingPathComponent("deviceKey")
|
let keyFile = storageFolder.appendingPathComponent(Config.keyFileName)
|
||||||
let deviceKey = try String(contentsOf: keyFile)
|
let deviceKey = try String(contentsOf: keyFile)
|
||||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
deviceManager = DeviceManager(deviceKey: deviceKey)
|
deviceManager = DeviceManager(deviceKey: deviceKey)
|
||||||
|
Loading…
Reference in New Issue
Block a user