Permalink
Browse files

Implemented delegate pattern - so you can now receive messages using …

…MosquittoClient.

Added second switch for Green LED.
Implemented listening to messages Marquette.
  • Loading branch information...
1 parent 810cc0e commit 780ed1b4ee752a9f4b7cd2a654931f47fadd5a9b @njh committed Jan 19, 2012
@@ -38,10 +38,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// FIXME: only if compiled in debug mode?
[mosquittoClient setLogPriorities:MOSQ_LOG_ALL destinations:MOSQ_LOG_STDERR];
-
- // Connect
- [mosquittoClient setHost: @"test.mosquitto.org"];
- [mosquittoClient connect];
+ [mosquittoClient setDelegate: self.viewController];
return YES;
}
@@ -7,15 +7,26 @@
//
#import <UIKit/UIKit.h>
+#import "MosquittoClient.h"
-@interface MarquetteViewController : UIViewController {
- UISwitch *ledSwitch;
+@interface MarquetteViewController : UIViewController <MosquittoClientDeligate> {
+ UISwitch *redLedSwitch;
+ UISwitch *greenLedSwitch;
+ UITextField *hostField;
+ UIButton *connectButton;
}
-@property (nonatomic, retain) IBOutlet UISwitch *ledSwitch;
+@property (nonatomic, retain) IBOutlet UISwitch *redLedSwitch;
+@property (nonatomic, retain) IBOutlet UISwitch *greenLedSwitch;
+@property (nonatomic, retain) IBOutlet UITextField *hostField;
+@property (nonatomic, retain) IBOutlet UIButton *connectButton;
-- (IBAction) ledSwitchAction:(id)sender;
+- (IBAction) redLedSwitchAction:(id)sender;
+- (IBAction) greenLedSwitchAction:(id)sender;
+- (IBAction) connectButtonAction:(id)sender;
+
+- (void) didConnect:(NSUInteger)code;
@end
@@ -12,7 +12,10 @@
@implementation MarquetteViewController
-@synthesize ledSwitch;
+@synthesize redLedSwitch;
+@synthesize greenLedSwitch;
+@synthesize hostField;
+@synthesize connectButton;
/*
@@ -61,19 +64,75 @@ - (void)viewDidUnload {
// e.g. self.myOutlet = nil;
}
-- (IBAction) ledSwitchAction:(id)sender {
+- (IBAction) redLedSwitchAction:(id)sender {
MarquetteAppDelegate *app = [[UIApplication sharedApplication] delegate];
MosquittoClient *mosq = [app mosquittoClient];
if ([sender isOn]) {
- NSLog(@"LED On");
+ NSLog(@"Red LED On");
[mosq publishString:@"1" toTopic:@"nanode/red_led" retain:YES];
}
else {
- NSLog(@"LED Off");
+ NSLog(@"Red LED Off");
[mosq publishString:@"0" toTopic:@"nanode/red_led" retain:YES];
}
}
+- (IBAction) greenLedSwitchAction:(id)sender {
+ MarquetteAppDelegate *app = [[UIApplication sharedApplication] delegate];
+ MosquittoClient *mosq = [app mosquittoClient];
+ if ([sender isOn]) {
+ NSLog(@"Green LED On");
+ [mosq publishString:@"1" toTopic:@"nanode/green_led" retain:YES];
+ }
+ else {
+ NSLog(@"Green LED Off");
+ [mosq publishString:@"0" toTopic:@"nanode/green_led" retain:YES];
+ }
+}
+
+- (IBAction) connectButtonAction:(id)sender {
+ MarquetteAppDelegate *app = [[UIApplication sharedApplication] delegate];
+ MosquittoClient *mosq = [app mosquittoClient];
+
+ // (Re-)connect
+ //[mosq disconnect]; UITextField
+ [mosq setHost: [[self hostField] text]];
+ [mosq connect];
+
+ [mosq subscribe:@"nanode/red_led"];
+ [mosq subscribe:@"nanode/green_led"];
+}
+
+- (void) didConnect:(NSUInteger)code {
+ [[self connectButton] setTitle:@"Reconnect" forState:UIControlStateNormal];
+}
+
+- (void) didDisconnect {
+ [[self connectButton] setTitle:@"Connect" forState:UIControlStateNormal];
+}
+
+- (void) didReceiveMessage: (NSString*)message topic:(NSString*)topic {
+ NSLog(@"%@ => %@", topic, message);
+
+ UISwitch *sw = nil;
+ if ([topic isEqualToString:@"nanode/red_led"]) {
+ sw = redLedSwitch;
+ } else if ([topic isEqualToString:@"nanode/green_led"]) {
+ sw = greenLedSwitch;
+ } else {
+ return;
+ }
+
+ if ([message isEqualToString:@"1"]) {
+ [sw setOn: YES];
+ } else if ([message isEqualToString:@"0"]) {
+ [sw setOn: NO];
+ }
+}
+
+- (void) didPublish: (NSUInteger)messageId {}
+- (void) didSubscribe: (NSUInteger)messageId grantedQos:(NSArray*)qos {}
+- (void) didUnsubscribe: (NSUInteger)messageId {}
- (void)dealloc {
[super dealloc];
View
@@ -13,10 +13,12 @@
unsigned short keepAlive;
BOOL cleanSession;
+ id delegate;
NSTimer *timer;
}
@property (readwrite,retain) NSString *host;
+@property (readwrite,assign) id delegate;
@property (readwrite,assign) unsigned short port;
@property (readwrite,assign) unsigned short keepAlive;
@property (readwrite,assign) BOOL cleanSession;
@@ -27,6 +29,7 @@
- (MosquittoClient*) initWithClientId: (NSString *)clientId;
- (void) setLogPriorities: (int)priorities destinations:(int)destinations;
+- (void) setMessageRetry: (NSUInteger)seconds;
- (void) connect;
//- (void) connectToHost: (NSString*) host;
- (void) reconnect;
@@ -35,7 +38,28 @@
- (void)publishString: (NSString *)payload toTopic:(NSString *)topic retain:(BOOL)retain;
//- (void)publishData
+- (void)subscribe: (NSString *)topic;
+- (void)subscribe: (NSString *)topic withQos:(NSUInteger)qos;
+- (void)unsubscribe: (NSString *)topic;
+
+
// This is called automatically when connected
- (void) loop: (NSTimer *)timer;
@end
+
+
+
+@protocol MosquittoClientDeligate
+
+- (void) didConnect: (NSUInteger)code;
+- (void) didDisconnect;
+- (void) didPublish: (NSUInteger)messageId;
+
+// FIXME: create MosquittoMessage class
+- (void) didReceiveMessage: (NSString*)message topic:(NSString*)topic;
+- (void) didSubscribe: (NSUInteger)messageId grantedQos:(NSArray*)qos;
+- (void) didUnsubscribe: (NSUInteger)messageId;
+
+@end
+
View
@@ -13,6 +13,62 @@ @implementation MosquittoClient
@synthesize port;
@synthesize keepAlive;
@synthesize cleanSession;
+@synthesize delegate;
+
+
+static void on_connect(void *ptr, int rc)
+{
+ MosquittoClient* self = (MosquittoClient*)ptr;
+ // FIXME: cache respondsToSelector
+ if ([[self delegate] respondsToSelector:@selector(didConnect:)])
+ [[self delegate] didConnect:(NSUInteger)rc];
+}
+
+static void on_disconnect(void *ptr)
+{
+ MosquittoClient* self = (MosquittoClient*)ptr;
+ // FIXME: cache respondsToSelector
+ if ([[self delegate] respondsToSelector:@selector(didDisconnect)])
+ [[self delegate] didDisconnect];
+}
+
+static void on_publish(void *ptr, uint16_t message_id)
+{
+ MosquittoClient* self = (MosquittoClient*)ptr;
+ // FIXME: cache respondsToSelector
+ if ([[self delegate] respondsToSelector:@selector(didPublish:)])
+ [[self delegate] didPublish:(NSUInteger)message_id];
+}
+
+static void on_message(void *ptr, const struct mosquitto_message *message)
+{
+ MosquittoClient* self = (MosquittoClient*)ptr;
+ NSString *topic = [NSString stringWithUTF8String: message->topic];
+ NSString *payload = [NSString stringWithCharacters:(const unichar *)message->payload
+ length:message->payloadlen];
+
+ // FIXME: cache respondsToSelector
+ if ([[self delegate] respondsToSelector:@selector(didReceiveMessage:topic:)])
+ [[self delegate] didReceiveMessage:payload topic:topic];
+}
+
+static void on_subscribe(void *ptr, uint16_t message_id, int qos_count, const uint8_t *granted_qos)
+{
+ MosquittoClient* self = (MosquittoClient*)ptr;
+ // FIXME: cache respondsToSelector
+ if ([[self delegate] respondsToSelector:@selector(didSubscribe:grantedQos:)])
+ [[self delegate] didSubscribe:message_id grantedQos:nil];
+ // FIXME: implement this
+}
+
+static void on_unsubscribe(void *ptr, uint16_t message_id)
+{
+ MosquittoClient* self = (MosquittoClient*)ptr;
+ // FIXME: cache respondsToSelector
+ if ([[self delegate] respondsToSelector:@selector(didUnsubscribe:)])
+ [[self delegate] didUnsubscribe:message_id];
+}
+
// Initialize is called just before the first object is allocated
+ (void)initialize {
@@ -34,6 +90,12 @@ - (MosquittoClient*) initWithClientId: (NSString*) clientId {
[self setCleanSession: YES];
mosq = mosquitto_new(cstrClientId, self);
+ mosquitto_connect_callback_set(mosq, on_connect);
+ mosquitto_disconnect_callback_set(mosq, on_disconnect);
+ mosquitto_publish_callback_set(mosq, on_publish);
+ mosquitto_message_callback_set(mosq, on_message);
+ mosquitto_subscribe_callback_set(mosq, on_subscribe);
+ mosquitto_unsubscribe_callback_set(mosq, on_unsubscribe);
timer = nil;
}
return self;
@@ -70,13 +132,33 @@ - (void) loop: (NSTimer *)timer {
mosquitto_loop(mosq, 0);
}
-// FIXME: add retained parameter?
+// FIXME: add QoS parameter?
- (void)publishString: (NSString *)payload toTopic:(NSString *)topic retain:(BOOL)retain {
const char* cstrTopic = [topic cStringUsingEncoding:NSUTF8StringEncoding];
const uint8_t* cstrPayload = (const uint8_t*)[payload cStringUsingEncoding:NSUTF8StringEncoding];
mosquitto_publish(mosq, NULL, cstrTopic, [payload length], cstrPayload, 0, retain);
}
+- (void)subscribe: (NSString *)topic {
+ [self subscribe:topic withQos:0];
+}
+
+- (void)subscribe: (NSString *)topic withQos:(NSUInteger)qos {
+ const char* cstrTopic = [topic cStringUsingEncoding:NSUTF8StringEncoding];
+ mosquitto_subscribe(mosq, NULL, cstrTopic, qos);
+}
+
+- (void)unsubscribe: (NSString *)topic {
+ const char* cstrTopic = [topic cStringUsingEncoding:NSUTF8StringEncoding];
+ mosquitto_unsubscribe(mosq, NULL, cstrTopic);
+}
+
+
+- (void) setMessageRetry: (NSUInteger)seconds
+{
+ mosquitto_message_retry_set(mosq, (unsigned int)seconds);
+}
+
- (void) dealloc {
if (mosq) {
mosquitto_destroy(mosq);
Oops, something went wrong.

0 comments on commit 780ed1b

Please sign in to comment.