diff --git a/DDWRT_MonitorAppDelegate.h b/DDWRT_MonitorAppDelegate.h index b66360c..7a4f2cf 100644 --- a/DDWRT_MonitorAppDelegate.h +++ b/DDWRT_MonitorAppDelegate.h @@ -110,11 +110,10 @@ - (IBAction) refreshClientsMenu:(id)sender; - (void) saveConfig:(id)sender; -- (void) updateThroughput; +- (void) updateThroughput:(NSTimer*)timer; - (void) throughputCallback:(NSData *)data; - (void) doUpdateStatus:(NSData *)data; - (void) configSaveRequestCallback:(NSData *)data; -- (NSString *) getKey:(NSString * )data key:(NSString *)key; - (NSString *) stringFromFileSize:(unsigned long long)theSize; //@property (assign) IBOutlet NSWindow *window; diff --git a/DDWRT_MonitorAppDelegate.m b/DDWRT_MonitorAppDelegate.m index 3479add..405fe51 100644 --- a/DDWRT_MonitorAppDelegate.m +++ b/DDWRT_MonitorAppDelegate.m @@ -28,17 +28,19 @@ { [self initDefaults]; } - + NSLog(@"Reading defaults..."); [self readDefaults]; // initialize the client wrt_client = [[WRTStatusClient alloc] initWithHostname:hostname port:port protocol:protocol username:username password:password]; + + [wrt_client getConnectionStatus]; + NSLog(@"Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client wanPort]); + _readTimer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] integerForKey:@"refreshTime"] target:self selector:@selector(updateThroughput:) userInfo:nil repeats:NO]; + [_readTimer fire]; - //_readTimer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] integerForKey:@"refreshTime"] target:self selector:@selector(updateThroughput:) userInfo:nil repeats:YES]; - //[_readTimer fire]; - - [self updateThroughput]; + //[self updateThroughput]; //WRTRequest *wrtr = [[WRTRequest alloc] init]; @@ -112,6 +114,7 @@ } - (void) terminate:(id)sender { + [_readTimer invalidate]; [[NSUserDefaults standardUserDefaults] synchronize]; [self dealloc]; [[NSApplication sharedApplication] terminate:self]; @@ -280,31 +283,6 @@ [self configSaveRequestCallback:nil]; } --(NSString *)getKey:(NSString *)stringData key:(NSString *)key -{ - NSString *keyString = [NSString stringWithFormat:@"%@::", key]; - NSString *returnData; - NSRange formatValid = [stringData rangeOfString:@"}"]; - - if (formatValid.location != NSNotFound) { - NSRange startRange = [stringData rangeOfString:keyString]; - - if (startRange.location != NSNotFound) { - - returnData = [NSString stringWithString:stringData]; - returnData = [returnData substringFromIndex:(startRange.location + [keyString length])]; - returnData = [returnData substringToIndex:[returnData rangeOfString:@"}"].location]; - } - - returnData = [returnData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - } - - //[keyString release]; - - return returnData; - -} - -(void)configSaveRequestCallback:(NSData *)data { // TODO check that the request is actually successful @@ -344,8 +322,9 @@ [self hideConfigPanel:nil]; } -- (void) updateThroughput +- (void) updateThroughput:(NSTimer*)timer { + NSLog(@"Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client wanPort]); //WRTRequest *wrtr = [[WRTRequest alloc] init]; //[wrtr doRequest:self requestSelector:@selector(throughputCallback:) uri:@"fetchif.cgi?ppp1"]; @@ -372,7 +351,7 @@ -(void)doUpdateStatus:(NSData *)data { NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; - + /* NSString *ipconn = [self getKey:stringData key:@"ip_conntrack"]; NSString *wanip = [self getKey:stringData key:@"ipinfo"]; @@ -389,7 +368,7 @@ [menuUptime setTitle:uptime]; [menuLoad setTitle:load]; [menuLoad setHidden:false]; - [menuConnections setTitle:[NSString stringWithFormat:@"Connections: %@", ipconn]]; + [menuConnections setTitle:[NSString stringWithFormat:@"Connections: %@", ipconn]];*/ } - (void) refreshMenu:(id)sender @@ -489,7 +468,7 @@ - (void) refreshClientsMenu:(id)sender { - [self updateThroughput]; + [self updateThroughput:nil]; } - (NSString *) stringFromFileSize:(unsigned long long)theSize diff --git a/WRTStatusClient.h b/WRTStatusClient.h index 9f1bc33..edf5ec6 100644 --- a/WRTStatusClient.h +++ b/WRTStatusClient.h @@ -10,13 +10,14 @@ @interface WRTStatusClient : NSObject { - BOOL wrtReachable; + BOOL wrtReachable; + NSString *wanPort; @private - NSString *hostname; - NSString *protocol; - int port; - NSString *username; - NSString *password; + NSString *hostname; + NSString *protocol; + int port; + NSString *username; + NSString *password; NSMutableData *receivedData; NSURLRequest *theRequest; @@ -27,6 +28,9 @@ SEL errorCallback; } +@property BOOL wrtReachable; +@property(nonatomic, retain) NSString *wanPort; + @property(nonatomic, retain) NSMutableData *receivedData; @property(nonatomic, retain) id delegate; @property(nonatomic) SEL callback; @@ -37,6 +41,9 @@ - (NSMutableURLRequest*) buildRequest:(NSString*)uri; - (NSMutableURLRequest*) requestForBandwidthViewerForInterface:(NSString*)interface; - (void) getStatusUpdate:(NSString*)uri delegate:(id)requestDelegate callback:(SEL)requestSelector; +- (void) getConnectionStatus; + +-(NSString *)getKey:(NSString *)stringData key:(NSString *)key; - (NSString*) getHostname; - (void) setHostname:(NSString*)new_hostname; diff --git a/WRTStatusClient.m b/WRTStatusClient.m index eda29df..91663fd 100644 --- a/WRTStatusClient.m +++ b/WRTStatusClient.m @@ -11,6 +11,9 @@ @implementation WRTStatusClient +@synthesize wrtReachable; +@synthesize wanPort; + @synthesize receivedData; @synthesize delegate; @synthesize callback; @@ -25,7 +28,9 @@ [self setPort: new_port]; [self setUsername: new_username]; [self setPassword: new_password]; - + + wrtReachable = false; + return self; } @@ -77,6 +82,62 @@ // - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data } +- (void) getConnectionStatus +{ + [self getStatusUpdate:@"Status_Bandwidth.asp" delegate:self callback:@selector(cbConnectionStatus:)]; +} + +- (void) cbConnectionStatus:(NSData *)data +{ + if (data == nil) { + wrtReachable = false; + } else { + NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; + //Bandwidth Monitoring + NSRange pageValid = [stringData rangeOfString:@"Bandwidth Monitoring"]; + + if (pageValid.location != NSNotFound) { + wrtReachable = true; + + // grab the wan port name + NSRange startRange = [stringData rangeOfString:@"WAN"]; + stringData = [stringData substringFromIndex:startRange.location]; + NSString *key = @"