Skip to content

Commit

Permalink
Clean up reconnect code.
Browse files Browse the repository at this point in the history
ptescher committed Jul 17, 2015
1 parent 80429db commit 61e3401
Showing 3 changed files with 21 additions and 38 deletions.
10 changes: 5 additions & 5 deletions WatchTheThrone.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -209,7 +209,7 @@
TargetAttributes = {
CEA669251B507C5A00C37BDC = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = 6P9TD8XRR3;
DevelopmentTeam = 8NM3K5K288;
};
CEA669351B507C5A00C37BDC = {
CreatedOnToolsVersion = 6.4;
@@ -361,7 +361,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
@@ -405,7 +405,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
@@ -430,7 +430,6 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Developer ID Application";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
@@ -439,6 +438,7 @@
INFOPLIST_FILE = WatchTheThrone/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "WatchTheThrone/WatchTheThrone-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
@@ -450,7 +450,6 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Developer ID Application";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
@@ -459,6 +458,7 @@
INFOPLIST_FILE = WatchTheThrone/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "WatchTheThrone/WatchTheThrone-Bridging-Header.h";
};
name = Release;
45 changes: 14 additions & 31 deletions WatchTheThrone/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -24,11 +24,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate {
@IBOutlet weak var notificationMenuItem: NSMenuItem!

weak var notificationTimer: NSTimer?
weak var reconnectionTimer: NSTimer?

let vacantImage: NSImage? = NSImage(named: "vacant")
let occupiedImage: NSImage? = NSImage(named: "occupied")

var connectTimer: MSWeakTimer?

var throneState = ThroneOccupiedState.Unknown {
didSet {
@@ -63,14 +62,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate {
mqttClient.delegate = self
mqttClient.username = "macosx"
mqttClient.password = "blahblahblah"
mqttClient.keepAlive = 10
mqttClient.keepAlive = 30
return mqttClient
}()

func applicationDidFinishLaunching(aNotification: NSNotification) {
Fabric.with([Crashlytics()])
statusItem.menu = menu
self.connect()
statusItem.button?.appearsDisabled = true
mqttClient.connect()
vacantImage?.setTemplate(true)
occupiedImage?.setTemplate(true)
}
@@ -83,28 +83,6 @@ 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
@@ -113,7 +91,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, CocoaMQTTDelegate {
// MARK: - CocoaMQTTDelegate

func mqtt(mqtt: CocoaMQTT, didConnect host: String, port: Int) {

reconnectionTimer?.invalidate()
statusItem.button?.appearsDisabled = false
}

func mqtt(mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
@@ -125,17 +104,21 @@ 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()
if reconnectionTimer == nil {
reconnectionTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "reconnect:", userInfo: nil, repeats: true)
reconnectionTimer?.fire()
}
}

func reconnect(timer: NSTimer?) {
mqttClient.connect()
}

func mqttDidPing(mqtt: CocoaMQTT) {
4 changes: 2 additions & 2 deletions WatchTheThrone/Info.plist
Original file line number Diff line number Diff line change
@@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>3</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>

0 comments on commit 61e3401

Please sign in to comment.