Compare commits
No commits in common. "f2f802a24ff5f96085fcc5313a0c14c3f31cdff9" and "baf3a1c06dac23a80e681e8e6f3d740fb57035bd" have entirely different histories.
f2f802a24f
...
baf3a1c06d
@ -21,4 +21,6 @@ struct ServerConfiguration: Codable {
|
||||
let buttonPin: Int
|
||||
|
||||
let bounceTime: Double
|
||||
|
||||
let tokens: [String]
|
||||
}
|
||||
|
@ -1,45 +0,0 @@
|
||||
import Foundation
|
||||
import BinaryCodable
|
||||
|
||||
struct TokenStorage {
|
||||
|
||||
private(set) var tokens: Set<String>
|
||||
|
||||
private let fileUrl: URL
|
||||
|
||||
private let encoder = BinaryEncoder()
|
||||
|
||||
init(in folder: URL) {
|
||||
tokens = []
|
||||
fileUrl = folder.appendingPathComponent("tokens")
|
||||
|
||||
loadTokensFromDisk()
|
||||
}
|
||||
|
||||
private mutating func loadTokensFromDisk() {
|
||||
guard FileManager.default.fileExists(atPath: fileUrl.path) else {
|
||||
return
|
||||
}
|
||||
do {
|
||||
let data = try Data(contentsOf: fileUrl)
|
||||
tokens = try BinaryDecoder().decode(from: data)
|
||||
} catch {
|
||||
log(error: "Failed to read token file: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func add(_ tokenUpload: TokenUpload) {
|
||||
if let oldToken = tokenUpload.previousToken {
|
||||
tokens.remove(oldToken.hex)
|
||||
}
|
||||
tokens.insert(tokenUpload.currentToken.hex)
|
||||
do {
|
||||
let data = try encoder.encode(tokens)
|
||||
try data.write(to: fileUrl)
|
||||
} catch {
|
||||
log(error: "Failed to write token file: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,16 +12,12 @@ private let apnsEventGroup = MultiThreadedEventLoopGroup(numberOfThreads: System
|
||||
private let apnsRequestEncoder = JSONEncoder()
|
||||
private let apnsResponseDecoder = JSONDecoder()
|
||||
|
||||
var tokenStorage: TokenStorage!
|
||||
|
||||
// configures your application
|
||||
public func configure(_ app: Application) throws {
|
||||
|
||||
let resourcesFolderUrl = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||
let configUrl = resourcesFolderUrl.appendingPathComponent("config.json")
|
||||
|
||||
tokenStorage = .init(in: resourcesFolderUrl)
|
||||
|
||||
let config: ServerConfiguration
|
||||
do {
|
||||
let data = try Data(contentsOf: configUrl)
|
||||
@ -111,10 +107,6 @@ private func configureGPIO(_ config: ServerConfiguration) {
|
||||
}
|
||||
|
||||
private func sendPush() {
|
||||
guard !tokenStorage.tokens.isEmpty else {
|
||||
log(info: "No tokens registered to send push")
|
||||
return
|
||||
}
|
||||
Task(priority: .userInitiated) {
|
||||
let client = APNSClient(
|
||||
configuration: apnsConfiguration,
|
||||
@ -123,7 +115,7 @@ private func sendPush() {
|
||||
requestEncoder: apnsRequestEncoder)
|
||||
log(info: "Client created")
|
||||
do {
|
||||
for token in tokenStorage.tokens {
|
||||
for token in knownTokens {
|
||||
log(info: "Sending push to \(token.prefix(6))...")
|
||||
try await client.sendAlertNotification(
|
||||
apnsNotification,
|
||||
|
@ -6,6 +6,8 @@ private let requestDecoder = BinaryDecoder()
|
||||
|
||||
var serverStatus: ServerStatus = .starting
|
||||
|
||||
var knownTokens = Set<String>()
|
||||
|
||||
func routes(_ app: Application) throws {
|
||||
|
||||
app.post("token") { req async throws -> HTTPResponseStatus in
|
||||
@ -13,7 +15,10 @@ func routes(_ app: Application) throws {
|
||||
return .badRequest
|
||||
}
|
||||
let tokenUpload: TokenUpload = try requestDecoder.decode(from: data)
|
||||
tokenStorage.add(tokenUpload)
|
||||
if let oldToken = tokenUpload.previousToken {
|
||||
knownTokens.remove(oldToken.hex)
|
||||
}
|
||||
knownTokens.insert(tokenUpload.currentToken.hex)
|
||||
return .ok
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user