@ -24,8 +24,7 @@
defaults = [ NSUserDefaults standardUserDefaults ] ;
defaults = [ NSUserDefaults standardUserDefaults ] ;
/ / check if the app is configured yet
/ / check if the app is configured yet
if ( ![ defaults boolForKey : @ "configured "] )
if ( ![ defaults boolForKey : @ "configured "] ) {
{
[ self initDefaults ] ;
[ self initDefaults ] ;
} else {
} else {
[ self initialize ] ;
[ self initialize ] ;
@ -41,10 +40,7 @@
wrt_client = [ [ WRTStatusClient alloc ] initWithHostname : hostname port : port protocol : protocol username : username password : password ] ;
wrt_client = [ [ WRTStatusClient alloc ] initWithHostname : hostname port : port protocol : protocol username : username password : password ] ;
[ wrt_client getConnectionStatus ] ;
[ wrt_client getConnectionStatus ] ;
NSLog ( @ "Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client getWanPort]);
[ self updateThroughput : nil ] ;
_readTimer = [ NSTimer scheduledTimerWithTimeInterval : refreshTime + 1 target : self selector : @ selector ( updateThroughput : ) userInfo : nil repeats : YES ] ;
[ _readTimer fire ] ;
}
}
- ( void ) deinitialize
- ( void ) deinitialize
@ -333,9 +329,9 @@
[ self hideConfigPanel : nil ] ;
[ self hideConfigPanel : nil ] ;
}
}
- ( void ) updateThroughput : ( NSTimer * ) timer
- ( void ) updateThroughput : ( NSTimer * ) timer
{
{
NSLog ( @ "Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client getWanPort]);
/ / NSLog ( @ "Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client getWanPort]);
if ( [ wrt_client wrtReachable ] == true && _wrtReachable == false ) {
if ( [ wrt_client wrtReachable ] == true && _wrtReachable == false ) {
_wrtReachable = true ;
_wrtReachable = true ;
if ( showMenuIcon == true ) {
if ( showMenuIcon == true ) {
@ -351,41 +347,49 @@
if ( _wrtReachable == true ) {
if ( _wrtReachable == true ) {
[ wrt_client getStatusUpdate : [ NSString stringWithFormat : @ "fetchif . cgi ?%@", [wrt_client getWanPort]] delegate:self callback:@selector(throughputCallback:)];
[ wrt_client getStatusUpdate : [ NSString stringWithFormat : @ "fetchif . cgi ?%@", [wrt_client getWanPort]] delegate:self callback:@selector(throughputCallback:)];
}
}
if ( _readTimer == nil ) {
_readTimer = [ NSTimer scheduledTimerWithTimeInterval : refreshTime + 1 target : self selector : @ selector ( updateThroughput : ) userInfo : nil repeats : YES ] ;
[ _readTimer fire ] ;
}
}
}
- ( void ) throughputCallback : ( NSData * ) data
- ( void ) throughputCallback : ( NSData * ) data
{
{
NSString * stringData = [ [ NSString alloc ] initWithData : data encoding : NSASCIIStringEncoding ] ;
NSString * stringData = [ [ NSString alloc ] initWithData : data encoding : NSASCIIStringEncoding ] ;
NSString * key = [ NSString stringWithFormat : @ "%@:", [wrt_client getWanPort]];
NSString * key = [ NSString stringWithFormat : @ "%@:", [wrt_client getWanPort]];
stringData = [ stringData substringFromIndex : ( [ stringData rangeOfString : key ] . location + [ key length ] ) ] ;
stringData = [ stringData substringFromIndex : ( [ stringData rangeOfString : key ] . location + [ key length ] ) ] ;
NSArray * parts = [ stringData componentsSeparatedByCharactersInSet : [ NSCharacterSet whitespaceCharacterSet ] ] ;
NSArray * parts = [ stringData componentsSeparatedByCharactersInSet : [ NSCharacterSet whitespaceCharacterSet ] ] ;
NSArray * filteredArray = [ parts filteredArrayUsingPredicate : [ NSPredicate predicateWithFormat : @ "SELF != ' ' "] ] ;
NSArray * filteredArray = [ parts filteredArrayUsingPredicate : [ NSPredicate predicateWithFormat : @ "SELF != ' ' "] ] ;
stringData = [ filteredArray componentsJoinedByString : @ " "] ;
stringData = [ filteredArray componentsJoinedByString : @ " "] ;
NSArray * listItems = [ stringData componentsSeparatedByString : @ " "] ;
NSArray * listItems = [ stringData componentsSeparatedByString : @ " "] ;
uint64_t ifIn = strtoull ( [ [ listItems objectAtIndex : 0 ] UTF8String ] , NULL , 0 ) ;
uint64_t ifIn = strtoull ( [ [ listItems objectAtIndex : 0 ] UTF8String ] , NULL , 0 ) ;
uint64_t ifOut = strtoull ( [ [ listItems objectAtIndex : 8 ] UTF8String ] , NULL , 0 ) ;
uint64_t ifOut = strtoull ( [ [ listItems objectAtIndex : 8 ] UTF8String ] , NULL , 0 ) ;
if ( _lastInThroughput != 0 ) {
if ( _lastInThroughput != 0 ) {
uint64_t diffIn = ifIn - _lastInThroughput ;
uint64_t diffIn = ifIn - _lastInThroughput ;
uint64_t diffOut = ifOut - _lastOutThroughput ;
uint64_t diffOut = ifOut - _lastOutThroughput ;
double timeDiff = fabs ( [ _lastDateThroughput timeIntervalSinceNow ] ) ;
double timeDiff = fabs ( [ _lastDateThroughput timeIntervalSinceNow ] ) ;
_lastDateThroughput = [ [ NSDate date ] retain ] ;
_lastDateThroughput = [ [ NSDate date ] retain ] ;
if ( timeDiff < = 0 ) timeDiff = 1 ; / / avoid division by zero
if ( timeDiff < = 0 )
timeDiff = 1 ; / / avoid division by zero
double speedIn = diffIn / timeDiff ;
double speedIn = diffIn / timeDiff ;
double speedOut = diffOut / timeDiff ;
double speedOut = diffOut / timeDiff ;
speedIn = round ( 2.2 f * speedIn ) / 2.2 f ;
speedIn = round ( 2.2 f * speedIn ) / 2.2 f ;
speedOut = round ( 2.2 f * speedOut ) / 2.2 f ;
speedOut = round ( 2.2 f * speedOut ) / 2.2 f ;
[ self setMenubarText : [ NSString stringWithFormat : @ "%@s\n%@s", [self stringFromSpeed:speedOut], [self stringFromSpeed: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,
/ / 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 ) ;
/ / _lastOutThroughput , diffIn , diffOut , speedIn , speedOut , timeDiff ) ;
}
}
_lastInThroughput = ifIn ;
_lastInThroughput = ifIn ;
@ -398,16 +402,17 @@
NSString * stringData = [ [ NSString alloc ] initWithData : data encoding : NSASCIIStringEncoding ] ;
NSString * stringData = [ [ NSString alloc ] initWithData : data encoding : NSASCIIStringEncoding ] ;
NSString * ipconn = [ wrt_client getKey : stringData key : @ "ip_conntrack "] ;
NSString * ipconn = [ wrt_client getKey : stringData key : @ "ip_conntrack "] ;
[ menuClientsConnections setTitle : [ NSString stringWithFormat : @ "%@ Connection%@", ipconn, ([ipconn isEqualToString:@"1"] ? @"" : @"s")]];
[ menuClientsConnections setTitle : [ NSString stringWithFormat : @ "%@ Connection%@", ipconn, ([ipconn isEqualToString:@"1"] ? @"" : @"s")]];
NSString * wanip = [ wrt_client getKey : stringData key : @ "ipinfo "] ;
NSString * wanip = [ wrt_client getKey : stringData key : @ "ipinfo "] ;
NSRange startRange = [ wanip rangeOfString : @ " "] ;
NSRange startRange = [ wanip rangeOfString : @ " "] ;
wanip = [ wanip substringFromIndex : ( startRange . location + 1 ) ] ;
wanip = [ wanip substringFromIndex : ( startRange . location + 1 ) ] ;
[ menuWanIP setTitle : wanip ] ;
[ menuWanIP setTitle : wanip ] ;
NSString * uptime = [ wrt_client getKey : stringData key : @ "uptime "] ;
NSString * uptime = [ wrt_client getKey : stringData key : @ "uptime "] ;
NSString * loadSplit = @ ", load average : ";
NSString * loadSplit = @ ", load average : ";
if ( [ uptime length ] > 0 ) {
if ( [ uptime length ] > 0 ) {
NSString * load = [ uptime substringFromIndex : [ uptime rangeOfString : loadSplit ] . location + [ loadSplit length ] ] ;
NSString * load = [ uptime substringFromIndex : [ uptime rangeOfString : loadSplit ] . location + [ loadSplit length ] ] ;
uptime = [ uptime substringToIndex : [ uptime rangeOfString : @ ", load "] . location ] ; / / Cut the load
uptime = [ uptime substringToIndex : [ uptime rangeOfString : @ ", load "] . location ] ; / / Cut the load
@ -441,8 +446,8 @@
memInfo = [ filteredArray componentsJoinedByString : @ ""] ;
memInfo = [ filteredArray componentsJoinedByString : @ ""] ;
NSArray * memData = [ memInfo componentsSeparatedByString : @ ", "] ;
NSArray * memData = [ memInfo componentsSeparatedByString : @ ", "] ;
unint32_t memTotal = strtoull ( [ [ memData objectAtIndex : 17 ] UTF8String ] , NULL , 0 ) ;
unint32_t memTotal = strtoull ( [ [ memData objectAtIndex : 17 ] UTF8String ] , NULL , 0 ) ;
unint32_t memFree = strtoull ( [ [ memData objectAtIndex : 21 ] UTF8String ] , NULL , 0 ) ;
unint32_t memFree = strtoull ( [ [ memData objectAtIndex : 21 ] UTF8String ] , NULL , 0 ) ;
* /
* /
}
}
@ -476,17 +481,17 @@
NSString * stringData = [ [ NSString alloc ] initWithData : data encoding : NSASCIIStringEncoding ] ;
NSString * stringData = [ [ NSString alloc ] initWithData : data encoding : NSASCIIStringEncoding ] ;
NSString * wanStatus = [ wrt_client getKey : stringData key : @ "wan_status "] ;
NSString * wanStatus = [ wrt_client getKey : stringData key : @ "wan_status "] ;
NSString * wanName = [ wrt_client getKey : stringData key : @ "wan_shortproto "] ;
NSString * wanName = [ wrt_client getKey : stringData key : @ "wan_shortproto "] ;
if ( [ wanName isEqualToString : @ "pppoe "] == true ) {
if ( [ wanName isEqualToString : @ "pppoe "] == true ) {
wanName = @ "PPPoE ";
wanName = @ "PPPoE ";
} else if ( [ wanName isEqualToString : @ "static "] == true ) {
} else if ( [ wanName isEqualToString : @ "static "] == true ) {
wanName = @ "Static ";
wanName = @ "Static ";
} else {
} else {
wanName = [ wanName uppercaseString ] ;
wanName = [ wanName uppercaseString ] ;
}
}
wanStatus = [ wanStatus substringToIndex : [ wanStatus rangeOfString : @ "& "] . location ] ;
wanStatus = [ wanStatus substringToIndex : [ wanStatus rangeOfString : @ "& "] . location ] ;
[ menuWanStatus setTitle : [ NSString stringWithFormat : @ "%@ %@", wanName, wanStatus]];
[ menuWanStatus setTitle : [ NSString stringWithFormat : @ "%@ %@", wanName, wanStatus]];
/ *
/ *
@ -495,7 +500,7 @@
}
}
* /
* /
NSString * wanUptime = [ wrt_client getKey : stringData key : @ "wan_uptime "] ;
NSString * wanUptime = [ wrt_client getKey : stringData key : @ "wan_uptime "] ;
if ( [ wanStatus isEqualToString : @ "Connected "] == true ) {
if ( [ wanStatus isEqualToString : @ "Connected "] == true ) {
[ menuWanUptime setTitle : wanUptime ] ;
[ menuWanUptime setTitle : wanUptime ] ;
[ menuWanUptime setHidden : false ] ;
[ menuWanUptime setHidden : false ] ;
@ -520,9 +525,9 @@
[ menuWanTrafficIn setTitle : [ NSString stringWithFormat : @ "In : %@", trafficIn]];
[ menuWanTrafficIn setTitle : [ NSString stringWithFormat : @ "In : %@", trafficIn]];
[ menuWanTrafficOut setTitle : [ NSString stringWithFormat : @ "Out : %@", trafficOut]];
[ menuWanTrafficOut setTitle : [ NSString stringWithFormat : @ "Out : %@", trafficOut]];
NSString * wanDNS0 = [ wrt_client getKey : stringData key : @ "wan_dns0 "] ;
NSString * wanDNS0 = [ wrt_client getKey : stringData key : @ "wan_dns0 "] ;
NSString * wanDNS1 = [ wrt_client getKey : stringData key : @ "wan_dns1 "] ;
NSString * wanDNS1 = [ wrt_client getKey : stringData key : @ "wan_dns1 "] ;
NSString * wanDNS2 = [ wrt_client getKey : stringData key : @ "wan_dns2 "] ;
NSString * wanDNS2 = [ wrt_client getKey : stringData key : @ "wan_dns2 "] ;
if ( [ wanDNS0 length ] > 0 ) {
if ( [ wanDNS0 length ] > 0 ) {
[ menuWanDNS setEnabled : true ] ;
[ menuWanDNS setEnabled : true ] ;
@ -550,7 +555,7 @@
}
}
- ( void ) cbClientsRequest : ( NSData * ) data
- ( void ) cbClientsRequest : ( NSData * ) data
{
{
NSString * clientsStringData ;
NSString * clientsStringData ;
@ -626,7 +631,7 @@
[ _readTimer fire ] ;
[ _readTimer fire ] ;
}
}
- ( NSMenu * ) createCopyMenuItem
- ( NSMenu * ) createCopyMenuItem
{
{
NSMenu * copyMenu = [ [ NSMenu alloc ] init ] ;
NSMenu * copyMenu = [ [ NSMenu alloc ] init ] ;
NSMenuItem * copyMenuItem = [ [ NSMenuItem alloc ] initWithTitle : @ "Copy " action : @ selector ( copyParentMenuTitle : ) keyEquivalent : @ ""] ;
NSMenuItem * copyMenuItem = [ [ NSMenuItem alloc ] initWithTitle : @ "Copy " action : @ selector ( copyParentMenuTitle : ) keyEquivalent : @ ""] ;
@ -666,32 +671,30 @@
- ( NSString * ) stringFromSpeed : ( uint64_t ) theBytes
- ( NSString * ) stringFromSpeed : ( uint64_t ) theBytes
{
{
unsigned int minSize = / * ( useBytes ? * / 1023 / * : 999 ) * / ;
unsigned int divSize = / * ( useBytes ? * / 1024 / * : 1000 ) * / ;
NSString * suffix ;
NSString * suffix ;
if ( useBytes == false ) {
if ( useBytes == false ) {
theBytes * = 8 ;
theBytes * = 8 ;
}
}
double floatSize = theBytes ;
double floatSize = theBytes ;
suffix = ( useBytes ? @ "B " : @ "b ") ;
suffix = ( useBytes ? @ "B " : @ "b ") ;
if ( theBytes < minSize )
if ( theBytes < 1023 )
return ( [ NSString stringWithFormat : @ "%llu%@", theBytes, suffix]);
return ( [ NSString stringWithFormat : @ "%llu%@", theBytes, suffix]);
floatSize = floatSize / divSize ;
floatSize = floatSize / 1024 ;
suffix = ( useBytes ? @ "KiB " : @ "Kb ") ;
suffix = ( useBytes ? @ "KiB " : @ "Kb ") ;
if ( floatSize < minSize )
if ( floatSize < 1023 )
return ( [ NSString stringWithFormat : @ "%1.1f%@", floatSize, suffix]);
return ( [ NSString stringWithFormat : @ "%1.1f%@", floatSize, suffix]);
floatSize = floatSize / divSize ;
floatSize = floatSize / 1024 ;
suffix = ( useBytes ? @ "MiB " : @ "Mb ") ;
suffix = ( useBytes ? @ "MiB " : @ "Mb ") ;
if ( floatSize < minSize )
if ( floatSize < 1023 )
return ( [ NSString stringWithFormat : @ "%1.2f%@", floatSize, suffix]);
return ( [ NSString stringWithFormat : @ "%1.2f%@", floatSize, suffix]);
floatSize = floatSize / divSize ;
floatSize = floatSize / 1024 ;
suffix = ( useBytes ? @ "GiB " : @ "Gb ") ;
suffix = ( useBytes ? @ "GiB " : @ "Gb ") ;
if ( floatSize < minSize )
if ( floatSize < 1023 )
return ( [ NSString stringWithFormat : @ "%1.2f%@", floatSize, suffix]);
return ( [ NSString stringWithFormat : @ "%1.2f%@", floatSize, suffix]);
floatSize = floatSize / divSize ;
floatSize = floatSize / 1024 ;
suffix = ( useBytes ? @ "TiB " : @ "Tb ") ;
suffix = ( useBytes ? @ "TiB " : @ "Tb ") ;
return ( [ NSString stringWithFormat : @ "%1.2f%@", floatSize, suffix]);
return ( [ NSString stringWithFormat : @ "%1.2f%@", floatSize, suffix]);