mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-16 21:51:39 +00:00
* style: apply SwiftFormat 0.62.1 rules Refs #103202 * ci: enforce deterministic Swift lint Refs #103202 * refactor: keep gateway connect lint-clean Refs #103202 * style: keep iOS typography checks warning-free * ci: route MLX Swift changes through pre-push * fix: preserve native i18n extraction after Swift cleanup * refactor: keep rebased Swift surfaces lint-clean * style: format latest Swift additions * chore: refresh native i18n inventory * style: keep generated Swift formatter-clean * fix: preserve node route invalidation callbacks * fix: keep native translation IDs stable * fix: retain native translation identifiers * fix: preserve translations across Swift source moves
80 lines
2.5 KiB
Swift
80 lines
2.5 KiB
Swift
import Foundation
|
|
import Network
|
|
import OpenClawKit
|
|
|
|
final class NetworkStatusService: @unchecked Sendable {
|
|
func currentStatus(timeoutMs: Int = 1500) async -> OpenClawNetworkStatusPayload {
|
|
await withCheckedContinuation { cont in
|
|
let monitor = NWPathMonitor()
|
|
let queue = DispatchQueue(label: "ai.openclawfoundation.app.network-status")
|
|
let state = NetworkStatusState()
|
|
|
|
monitor.pathUpdateHandler = { path in
|
|
guard state.markCompleted() else { return }
|
|
monitor.cancel()
|
|
cont.resume(returning: Self.payload(from: path))
|
|
}
|
|
|
|
monitor.start(queue: queue)
|
|
|
|
queue.asyncAfter(deadline: .now() + .milliseconds(timeoutMs)) {
|
|
guard state.markCompleted() else { return }
|
|
monitor.cancel()
|
|
cont.resume(returning: Self.fallbackPayload())
|
|
}
|
|
}
|
|
}
|
|
|
|
private static func payload(from path: NWPath) -> OpenClawNetworkStatusPayload {
|
|
let status: OpenClawNetworkPathStatus = switch path.status {
|
|
case .satisfied: .satisfied
|
|
case .requiresConnection: .requiresConnection
|
|
case .unsatisfied: .unsatisfied
|
|
@unknown default: .unsatisfied
|
|
}
|
|
|
|
var interfaces: [OpenClawNetworkInterfaceType] = []
|
|
if path.usesInterfaceType(.wifi) {
|
|
interfaces.append(.wifi)
|
|
}
|
|
if path.usesInterfaceType(.cellular) {
|
|
interfaces.append(.cellular)
|
|
}
|
|
if path.usesInterfaceType(.wiredEthernet) {
|
|
interfaces.append(.wired)
|
|
}
|
|
if interfaces.isEmpty {
|
|
interfaces.append(.other)
|
|
}
|
|
|
|
return OpenClawNetworkStatusPayload(
|
|
status: status,
|
|
isExpensive: path.isExpensive,
|
|
isConstrained: path.isConstrained,
|
|
interfaces: interfaces)
|
|
}
|
|
|
|
private static func fallbackPayload() -> OpenClawNetworkStatusPayload {
|
|
OpenClawNetworkStatusPayload(
|
|
status: .unsatisfied,
|
|
isExpensive: false,
|
|
isConstrained: false,
|
|
interfaces: [.other])
|
|
}
|
|
}
|
|
|
|
private final class NetworkStatusState: @unchecked Sendable {
|
|
private let lock = NSLock()
|
|
private var completed = false
|
|
|
|
func markCompleted() -> Bool {
|
|
self.lock.lock()
|
|
defer { self.lock.unlock() }
|
|
if self.completed {
|
|
return false
|
|
}
|
|
self.completed = true
|
|
return true
|
|
}
|
|
}
|