Skip to content

Commit 261499b

Browse files
committed
Only connect to saved networks found nearby
1 parent 60839e1 commit 261499b

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

HeliPort/Appearance/StatusMenu.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ final class StatusMenu: NSMenu, NSMenuDelegate {
383383
get_power_state(&power)
384384
if get_80211_state(&state) && power &&
385385
(state != ITL80211_S_RUN.rawValue || get_station_info(&stationInfo) != KERN_SUCCESS) {
386-
NetworkManager.connectSavedNetworks()
386+
NetworkManager.scanSavedNetworks()
387387
}
388388
}
389389
}

HeliPort/NetworkManager.swift

+27-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,36 @@ final class NetworkManager {
109109
}
110110
}
111111

112-
class func connectSavedNetworks() {
112+
class func scanSavedNetworks() {
113+
DispatchQueue.global(qos: .background).async {
114+
let scanTimer: Timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
115+
let dispatchSemaphore = DispatchSemaphore(value: 0)
116+
var targetNetworks: [NetworkInfo]?
117+
NetworkManager.scanNetwork { networkList in
118+
targetNetworks = CredentialsManager.instance.getSavedNetworks().filter { networkList.contains($0) }
119+
dispatchSemaphore.signal()
120+
}
121+
dispatchSemaphore.wait()
122+
if targetNetworks != nil, targetNetworks!.count > 0 {
123+
// This will stop the timer completely
124+
timer.invalidate()
125+
Log.debug("Auto connect timer stopped")
126+
connectSavedNetworks(networks: targetNetworks!)
127+
}
128+
}
129+
// Start executing code inside the timer immediately
130+
scanTimer.fire()
131+
let currentRunLoop = RunLoop.current
132+
currentRunLoop.add(scanTimer, forMode: .common)
133+
currentRunLoop.run()
134+
}
135+
}
136+
137+
private class func connectSavedNetworks(networks: [NetworkInfo]) {
113138
DispatchQueue.global(qos: .background).async {
114139
let dispatchSemaphore = DispatchSemaphore(value: 0)
115140
var connected = false
116-
for network in CredentialsManager.instance.getSavedNetworks() where !connected {
141+
for network in networks where !connected {
117142
connect(networkInfo: network) { (result: Bool) -> Void in
118143
connected = result
119144
dispatchSemaphore.signal()

HeliPort/Supporting files/Log.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import os.log
1919
final class Log {
2020
static func debug(_ message: String) {
2121
if #available(OSX 11.0, *) {
22-
Logger.heliPort.info("DEBUG: \(message, privacy: .public))")
22+
Logger.heliPort.info("DEBUG: \(message, privacy: .public)")
2323
} else {
2424
os_log("%{public}@", log: .heliPort, type: .info, "DEBUG: " + message)
2525
}
2626
}
2727

2828
static func error(_ message: String) {
2929
if #available(OSX 11.0, *) {
30-
Logger.heliPort.error("ERROR: \(message, privacy: .public))")
30+
Logger.heliPort.error("ERROR: \(message, privacy: .public)")
3131
} else {
3232
os_log("%{public}@", log: .heliPort, type: .error, "ERROR: " + message)
3333
}

0 commit comments

Comments
 (0)