Compare commits
2 Commits
baf3a1c06d
...
f2f802a24f
Author | SHA1 | Date | |
---|---|---|---|
|
f2f802a24f | ||
|
151c6a432f |
@ -21,6 +21,4 @@ struct ServerConfiguration: Codable {
|
|||||||
let buttonPin: Int
|
let buttonPin: Int
|
||||||
|
|
||||||
let bounceTime: Double
|
let bounceTime: Double
|
||||||
|
|
||||||
let tokens: [String]
|
|
||||||
}
|
}
|
||||||
|
45
Sources/App/TokenStorage.swift
Normal file
45
Sources/App/TokenStorage.swift
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
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,12 +12,16 @@ private let apnsEventGroup = MultiThreadedEventLoopGroup(numberOfThreads: System
|
|||||||
private let apnsRequestEncoder = JSONEncoder()
|
private let apnsRequestEncoder = JSONEncoder()
|
||||||
private let apnsResponseDecoder = JSONDecoder()
|
private let apnsResponseDecoder = JSONDecoder()
|
||||||
|
|
||||||
|
var tokenStorage: TokenStorage!
|
||||||
|
|
||||||
// configures your application
|
// configures your application
|
||||||
public func configure(_ app: Application) throws {
|
public func configure(_ app: Application) throws {
|
||||||
|
|
||||||
let resourcesFolderUrl = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
let resourcesFolderUrl = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
let configUrl = resourcesFolderUrl.appendingPathComponent("config.json")
|
let configUrl = resourcesFolderUrl.appendingPathComponent("config.json")
|
||||||
|
|
||||||
|
tokenStorage = .init(in: resourcesFolderUrl)
|
||||||
|
|
||||||
let config: ServerConfiguration
|
let config: ServerConfiguration
|
||||||
do {
|
do {
|
||||||
let data = try Data(contentsOf: configUrl)
|
let data = try Data(contentsOf: configUrl)
|
||||||
@ -107,6 +111,10 @@ private func configureGPIO(_ config: ServerConfiguration) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func sendPush() {
|
private func sendPush() {
|
||||||
|
guard !tokenStorage.tokens.isEmpty else {
|
||||||
|
log(info: "No tokens registered to send push")
|
||||||
|
return
|
||||||
|
}
|
||||||
Task(priority: .userInitiated) {
|
Task(priority: .userInitiated) {
|
||||||
let client = APNSClient(
|
let client = APNSClient(
|
||||||
configuration: apnsConfiguration,
|
configuration: apnsConfiguration,
|
||||||
@ -115,7 +123,7 @@ private func sendPush() {
|
|||||||
requestEncoder: apnsRequestEncoder)
|
requestEncoder: apnsRequestEncoder)
|
||||||
log(info: "Client created")
|
log(info: "Client created")
|
||||||
do {
|
do {
|
||||||
for token in knownTokens {
|
for token in tokenStorage.tokens {
|
||||||
log(info: "Sending push to \(token.prefix(6))...")
|
log(info: "Sending push to \(token.prefix(6))...")
|
||||||
try await client.sendAlertNotification(
|
try await client.sendAlertNotification(
|
||||||
apnsNotification,
|
apnsNotification,
|
||||||
|
@ -6,8 +6,6 @@ private let requestDecoder = BinaryDecoder()
|
|||||||
|
|
||||||
var serverStatus: ServerStatus = .starting
|
var serverStatus: ServerStatus = .starting
|
||||||
|
|
||||||
var knownTokens = Set<String>()
|
|
||||||
|
|
||||||
func routes(_ app: Application) throws {
|
func routes(_ app: Application) throws {
|
||||||
|
|
||||||
app.post("token") { req async throws -> HTTPResponseStatus in
|
app.post("token") { req async throws -> HTTPResponseStatus in
|
||||||
@ -15,10 +13,7 @@ func routes(_ app: Application) throws {
|
|||||||
return .badRequest
|
return .badRequest
|
||||||
}
|
}
|
||||||
let tokenUpload: TokenUpload = try requestDecoder.decode(from: data)
|
let tokenUpload: TokenUpload = try requestDecoder.decode(from: data)
|
||||||
if let oldToken = tokenUpload.previousToken {
|
tokenStorage.add(tokenUpload)
|
||||||
knownTokens.remove(oldToken.hex)
|
|
||||||
}
|
|
||||||
knownTokens.insert(tokenUpload.currentToken.hex)
|
|
||||||
return .ok
|
return .ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user