Skip to content

Commit

Permalink
Merge pull request #3 from pat2man/keepAlive
Browse files Browse the repository at this point in the history
Keep alive
  • Loading branch information
ptescher committed Jul 17, 2015
2 parents b9e4f8a + e677f5f commit 80429db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions WatchTheThrone.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
TargetAttributes = {
CEA669251B507C5A00C37BDC = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = 6P9TD8XRR3;
};
CEA669351B507C5A00C37BDC = {
CreatedOnToolsVersion = 6.4;
Expand Down Expand Up @@ -429,6 +430,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Developer ID Application";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -448,6 +450,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Developer ID Application";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
33 changes: 30 additions & 3 deletions WatchTheThrone/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 {
Expand Down Expand Up @@ -61,13 +63,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate {
mqttClient.delegate = self
mqttClient.username = "macosx"
mqttClient.password = "blahblahblah"
mqttClient.keepAlive = 10
return mqttClient
}()

func applicationDidFinishLaunching(aNotification: NSNotification) {
Fabric.with([Crashlytics()])
statusItem.menu = menu
mqttClient.connect()
self.connect()
vacantImage?.setTemplate(true)
occupiedImage?.setTemplate(true)
}
Expand All @@ -80,6 +83,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
Expand All @@ -100,15 +125,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) {
Expand Down

0 comments on commit 80429db

Please sign in to comment.