diff --git a/DDWRT_MonitorAppDelegate.h b/DDWRT_MonitorAppDelegate.h index 8a09ddd..261aea8 100644 --- a/DDWRT_MonitorAppDelegate.h +++ b/DDWRT_MonitorAppDelegate.h @@ -79,7 +79,7 @@ WRTStatusClient *wrt_client; - NSTimeInterval _lastDateThroughput; + NSDate *_lastDateThroughput; unsigned long long _lastInThroughput; unsigned long long _lastOutThroughput; @@ -122,6 +122,7 @@ - (void) doUpdateStatus:(NSData *)data; - (IBAction) copyParentMenuTitle:(id)sender; -- (NSString *) stringFromFileSize:(unsigned long long)theSize; +- (NSString *) stringFromSize:(unsigned long long)theBytes; +- (NSString *) stringFromSpeed:(unsigned long long)theBytes; @end diff --git a/DDWRT_MonitorAppDelegate.m b/DDWRT_MonitorAppDelegate.m index bb9c64a..786a4ee 100644 --- a/DDWRT_MonitorAppDelegate.m +++ b/DDWRT_MonitorAppDelegate.m @@ -40,12 +40,6 @@ _readTimer = [NSTimer scheduledTimerWithTimeInterval:refreshTime+1 target:self selector:@selector(updateThroughput:) userInfo:nil repeats:YES]; [_readTimer fire]; - - //[self updateThroughput]; - - //WRTRequest *wrtr = [[WRTRequest alloc] init]; - - //[wrtr doRequest:self requestSelector:@selector(doUpdateStatus:) uri:@"Status_Router.live.asp"]; } - (void) dealloc { @@ -335,7 +329,11 @@ NSLog(@"Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client getWanPort]); if ([wrt_client wrtReachable] == true && _wrtReachable == false) { _wrtReachable = true; - [self showMenubarIcon:true]; + if (showMenuIcon == true) { + [self showMenubarIcon:true]; + } else { + [self hideMenubarIcon]; + } } else if ([wrt_client wrtReachable] == false && _wrtReachable == true) { _wrtReachable = false; [self showMenubarIcon:false]; @@ -343,13 +341,7 @@ if (_wrtReachable == true) { [wrt_client getStatusUpdate:[NSString stringWithFormat:@"fetchif.cgi?%@", [wrt_client getWanPort]] delegate:self callback:@selector(throughputCallback:)]; - //[wrt_client getStatusUpdate:@"Status_Router.live.asp" delegate:self callback:@selector(doUpdateStatus:)]; - //[wrt_client getStatusUpdate:@"Status_Internet.live.asp" delegate:self callback:@selector(wanRequestCallback:)]; } - - //WRTRequest *wrtr = [[WRTRequest alloc] init]; - - //[wrtr doRequest:self requestSelector:@selector(throughputCallback:) uri:@"fetchif.cgi?ppp1"]; } - (void) throughputCallback:(NSData *)data @@ -372,16 +364,21 @@ unsigned long long diffIn = ifIn - _lastInThroughput; unsigned long long diffOut = ifOut - _lastOutThroughput; - double speedIn = diffIn / ([NSDate timeIntervalSinceReferenceDate] - _lastDateThroughput); - double speedOut = diffOut / ([NSDate timeIntervalSinceReferenceDate] - _lastDateThroughput); - speedIn = round(2.2f * speedIn) / 2.2f; - speedOut = round(2.2f * speedOut) / 2.2f; + double timeDiff = fabs([_lastDateThroughput timeIntervalSinceNow]); + _lastDateThroughput = [[NSDate date] retain]; + if (timeDiff <= 0) timeDiff = 1; // avoid division by zero + double speedIn = diffIn / timeDiff; + double speedOut = diffOut / timeDiff; + speedIn = round(2.2f * speedIn) / 2.2f; + speedOut = round(2.2f * speedOut) / 2.2f; - [ self setMenubarText:[NSString stringWithFormat:@"%@s\n%@s", [self stringFromFileSize:speedOut], [self stringFromFileSize:speedIn]] ]; + [ self setMenubarText:[NSString stringWithFormat:@"%@s\n%@s", [self stringFromSpeed:speedOut], [self stringFromSpeed:speedIn]] ]; + + NSLog(@"In %llu Out %llu Last In %llu Last Out %llu diffIn %llu diffOut %llu In %f Out %f Interval %f", ifIn, ifOut, _lastInThroughput, + _lastOutThroughput, diffIn, diffOut, speedIn, speedOut, timeDiff); } - _lastDateThroughput = [NSDate timeIntervalSinceReferenceDate]; _lastInThroughput = ifIn; _lastOutThroughput = ifOut; } @@ -391,7 +388,7 @@ [wrt_client getStatusUpdate:@"Status_Internet.live.asp" delegate:self callback:@selector(wanRequestCallback:)]; NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; - + NSLog(@"%@", stringData); NSString *ipconn = [wrt_client getKey:stringData key:@"ip_conntrack"]; NSString *wanip = [wrt_client getKey:stringData key:@"ipinfo"]; @@ -415,10 +412,14 @@ - (void) refreshMenu:(id)sender { NSLog(@"Refresh Menu"); - [_readTimer invalidate]; - _readTimer = nil; + if ([wrt_client wrtReachable] == true) { + [_readTimer invalidate]; + _readTimer = nil; - [wrt_client getStatusUpdate:@"Status_Router.live.asp" delegate:self callback:@selector(doUpdateStatus:)]; + [wrt_client getStatusUpdate:@"Status_Router.live.asp" delegate:self callback:@selector(doUpdateStatus:)]; + } else { + [_appMenu popUpStatusItemMenu:AppMenu]; + } } @@ -476,8 +477,8 @@ NSString *trafficIn = [wrt_client getKey:stringData key:@"ttraff_in"]; NSString *trafficOut = [wrt_client getKey:stringData key:@"ttraff_out"]; - trafficIn = [self stringFromFileSize:([trafficIn longLongValue] * 1024 * 1024)]; - trafficOut = [self stringFromFileSize:([trafficOut longLongValue] * 1024 * 1024)]; + trafficIn = [self stringFromSize:([trafficIn longLongValue] * 1024 * 1024)]; + trafficOut = [self stringFromSize:([trafficOut longLongValue] * 1024 * 1024)]; [menuWanTrafficIn setTitle:[NSString stringWithFormat:@"In: %@", trafficIn]]; [menuWanTrafficOut setTitle:[NSString stringWithFormat:@"Out: %@", trafficOut]]; @@ -619,26 +620,58 @@ [pasteBoard setString:[[sender parentItem] title] forType:NSStringPboardType]; } -- (NSString *) stringFromFileSize:(unsigned long long)theSize +- (NSString *) stringFromSize:(unsigned long long)theBytes { + double floatSize = theBytes; - double floatSize = theSize; - if (theSize<1023) - return([NSString stringWithFormat:@"%qxB",theSize]); + if (theBytes<1023) + return([NSString stringWithFormat:@"%lluB", theBytes]); floatSize = floatSize / 1024; if (floatSize<1023) - return([NSString stringWithFormat:@"%1.1fKiB",floatSize]); + return([NSString stringWithFormat:@"%1.1fKiB", floatSize]); floatSize = floatSize / 1024; if (floatSize<1023) - return([NSString stringWithFormat:@"%1.1fMiB",floatSize]); + return([NSString stringWithFormat:@"%1.2fMiB", floatSize]); floatSize = floatSize / 1024; if (floatSize<1023) - return([NSString stringWithFormat:@"%1.1fGiB",floatSize]); + return([NSString stringWithFormat:@"%1.2fGiB", floatSize]); floatSize = floatSize / 1024; - return([NSString stringWithFormat:@"%1.1fTiB",floatSize]); + return([NSString stringWithFormat:@"%1.2fTiB", floatSize]); } +- (NSString *) stringFromSpeed:(unsigned long long)theBytes +{ + unsigned int minSize = /*(useBytes ? */1023/* : 999)*/; + unsigned int divSize = /*(useBytes ? */1024/* : 1000)*/; + NSString *suffix; + + if (useBytes == false) { + theBytes *= 8; + } + + double floatSize = theBytes; + suffix = (useBytes ? @"B" : @"b"); + + if (theBytes