From e677f5fe0af5ce0a6ddffc866c9194d764fb870f Mon Sep 17 00:00:00 2001 From: Josh Pyles Date: Wed, 15 Jul 2015 16:05:50 -0700 Subject: [PATCH] Keep alive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added some code to ensure you’re always connected and subscribed. --- WatchTheThrone.xcodeproj/project.pbxproj | 3 +++ WatchTheThrone/AppDelegate.swift | 33 +++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/WatchTheThrone.xcodeproj/project.pbxproj b/WatchTheThrone.xcodeproj/project.pbxproj index 6991ce5..b77762c 100644 --- a/WatchTheThrone.xcodeproj/project.pbxproj +++ b/WatchTheThrone.xcodeproj/project.pbxproj @@ -200,6 +200,7 @@ TargetAttributes = { CEA669251B507C5A00C37BDC = { CreatedOnToolsVersion = 6.4; + DevelopmentTeam = 6P9TD8XRR3; }; CEA669351B507C5A00C37BDC = { CreatedOnToolsVersion = 6.4; @@ -406,6 +407,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = "Developer ID Application"; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = WatchTheThrone/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; @@ -421,6 +423,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = "Developer ID Application"; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = WatchTheThrone/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; diff --git a/WatchTheThrone/AppDelegate.swift b/WatchTheThrone/AppDelegate.swift index b2faf07..6d73496 100644 --- a/WatchTheThrone/AppDelegate.swift +++ b/WatchTheThrone/AppDelegate.swift @@ -26,6 +26,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate { let vacantImage: NSImage? = NSImage(named: "vacant") let occupiedImage: NSImage? = NSImage(named: "occupied") + + var connectTimer: MSWeakTimer? var throneState = ThroneOccupiedState.Unknown { didSet { @@ -60,12 +62,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate { mqttClient.delegate = self mqttClient.username = "macosx" mqttClient.password = "blahblahblah" + mqttClient.keepAlive = 10 return mqttClient }() func applicationDidFinishLaunching(aNotification: NSNotification) { statusItem.menu = menu - mqttClient.connect() + self.connect() vacantImage?.setTemplate(true) occupiedImage?.setTemplate(true) } @@ -78,6 +81,28 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate { sender.state = NSOffState } } + + func connect() { + if (connectTimer == nil) { + connectTimer = MSWeakTimer.scheduledTimerWithTimeInterval( + NSTimeInterval(10), + target: self, + selector: "_connectTimerFired", + userInfo: nil, + repeats: true, + dispatchQueue: dispatch_get_main_queue()) + connectTimer!.fire() + } + } + + func _connectTimerFired() { + if (mqttClient.connState == .CONNECTED) { + connectTimer?.invalidate() + connectTimer = nil + } else { + mqttClient.connect() + } + } func applicationWillTerminate(aNotification: NSNotification) { // Insert code here to tear down your application @@ -98,15 +123,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate { } func mqtt(mqtt: CocoaMQTT, didSubscribeTopic topic: String) { - + statusItem.button?.appearsDisabled = false } func mqtt(mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) { - + statusItem.button?.appearsDisabled = true + mqtt.subscribe("test", qos: CocoaMQTTQOS.QOS0) } func mqttDidDisconnect(mqtt: CocoaMQTT, withError err: NSError) { statusItem.button?.appearsDisabled = true + self.connect() } func mqttDidPing(mqtt: CocoaMQTT) {