Skip to content

Commit

Permalink
Only connect to saved networks found nearby
Browse files Browse the repository at this point in the history
  • Loading branch information
williambj1 committed Mar 2, 2021
1 parent 60839e1 commit 261499b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion HeliPort/Appearance/StatusMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ final class StatusMenu: NSMenu, NSMenuDelegate {
get_power_state(&power)
if get_80211_state(&state) && power &&
(state != ITL80211_S_RUN.rawValue || get_station_info(&stationInfo) != KERN_SUCCESS) {
NetworkManager.connectSavedNetworks()
NetworkManager.scanSavedNetworks()
}
}
}
Expand Down
29 changes: 27 additions & 2 deletions HeliPort/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,36 @@ final class NetworkManager {
}
}

class func connectSavedNetworks() {
class func scanSavedNetworks() {
DispatchQueue.global(qos: .background).async {
let scanTimer: Timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
let dispatchSemaphore = DispatchSemaphore(value: 0)
var targetNetworks: [NetworkInfo]?
NetworkManager.scanNetwork { networkList in
targetNetworks = CredentialsManager.instance.getSavedNetworks().filter { networkList.contains($0) }
dispatchSemaphore.signal()
}
dispatchSemaphore.wait()
if targetNetworks != nil, targetNetworks!.count > 0 {
// This will stop the timer completely
timer.invalidate()
Log.debug("Auto connect timer stopped")
connectSavedNetworks(networks: targetNetworks!)
}
}
// Start executing code inside the timer immediately
scanTimer.fire()
let currentRunLoop = RunLoop.current
currentRunLoop.add(scanTimer, forMode: .common)
currentRunLoop.run()
}
}

private class func connectSavedNetworks(networks: [NetworkInfo]) {
DispatchQueue.global(qos: .background).async {
let dispatchSemaphore = DispatchSemaphore(value: 0)
var connected = false
for network in CredentialsManager.instance.getSavedNetworks() where !connected {
for network in networks where !connected {
connect(networkInfo: network) { (result: Bool) -> Void in
connected = result
dispatchSemaphore.signal()
Expand Down
4 changes: 2 additions & 2 deletions HeliPort/Supporting files/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import os.log
final class Log {
static func debug(_ message: String) {
if #available(OSX 11.0, *) {
Logger.heliPort.info("DEBUG: \(message, privacy: .public))")
Logger.heliPort.info("DEBUG: \(message, privacy: .public)")
} else {
os_log("%{public}@", log: .heliPort, type: .info, "DEBUG: " + message)
}
}

static func error(_ message: String) {
if #available(OSX 11.0, *) {
Logger.heliPort.error("ERROR: \(message, privacy: .public))")
Logger.heliPort.error("ERROR: \(message, privacy: .public)")
} else {
os_log("%{public}@", log: .heliPort, type: .error, "ERROR: " + message)
}
Expand Down

0 comments on commit 261499b

Please sign in to comment.