Skip to content

Commit

Permalink
Fix ready status missed if token validation takes more than 1s
Browse files Browse the repository at this point in the history
Fixed: repeat a status check every 0.5s during 10s max
  • Loading branch information
romainPellerin committed Dec 3, 2015
1 parent eed0638 commit 2bb2a5e
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/ios/TCPlugin.m
Expand Up @@ -16,10 +16,12 @@ @interface TCPlugin() {
NSString *_callback;
}

@property(nonatomic, strong) TCDevice *device;
@property(nonatomic, strong) NSString *callback;
@property(atomic, strong) TCConnection *connection;
@property(atomic, strong) UILocalNotification *ringNotification;
@property(nonatomic, strong) TCDevice *device;
@property(nonatomic, strong) NSString *callback;
@property(atomic, strong) TCConnection *connection;
@property(atomic, strong) UILocalNotification *ringNotification;
@property(atomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger *nbRepeats;

-(void)javascriptCallback:(NSString *)event;
-(void)javascriptCallback:(NSString *)event withArguments:(NSDictionary *)arguments;
Expand All @@ -34,6 +36,7 @@ @implementation TCPlugin
@synthesize connection = _connection;
@synthesize ringNotification = _ringNotification;


# pragma mark device delegate method

-(void)device:(TCDevice *)device didStopListeningForIncomingConnections:(NSError *)error {
Expand Down Expand Up @@ -82,29 +85,52 @@ -(void)connectionDidDisconnect:(TCConnection*)connection {

-(void)deviceSetup:(CDVInvokedUrlCommand*)command {
self.callback = command.callbackId;
self.device = [[TCDevice alloc] initWithCapabilityToken:[command.arguments objectAtIndex:0] delegate:self];
_nbRepeats = 0;

self.device = [[TCDevice alloc] initWithCapabilityToken:command.arguments[0] delegate:self];

// Disable sounds. was getting EXC_BAD_ACCESS
//self.device.incomingSoundEnabled = NO;
//self.device.outgoingSoundEnabled = NO;
//self.device.disconnectSoundEnabled = NO;

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(deviceStatusEvent) userInfo:nil repeats:NO];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(deviceStatusEvent) userInfo:nil repeats:YES];
}

-(void)deviceStatusEvent {

NSLog(@"Device state: %ld",(long) self.device.state);

switch ([self.device state]) {

case TCDeviceStateReady:
[self javascriptCallback:@"onready"];
NSLog(@"State: Ready");

[_timer invalidate];
_timer = nil;

break;

case TCDeviceStateOffline:
[self javascriptCallback:@"onoffline"];
NSLog(@"State: Offline");

if ((long)_nbRepeats>20){

[_timer invalidate];
_timer = nil;
_nbRepeats = 0;
}
else _nbRepeats++;

break;

default:

[_timer invalidate];
_timer = nil;

break;
}
}
Expand All @@ -119,6 +145,9 @@ -(void)disconnectAll:(CDVInvokedUrlCommand*)command {

-(void)deviceStatus:(CDVInvokedUrlCommand*)command {
NSString *state;

NSLog(@"Device state: %ld",(long) self.device.state);

switch ([self.device state]) {
case TCDeviceStateBusy:
state = @"busy";
Expand Down Expand Up @@ -169,6 +198,7 @@ -(void)sendDigits:(CDVInvokedUrlCommand*)command {

-(void)connectionStatus:(CDVInvokedUrlCommand*)command {
NSString *state;

switch ([self.connection state]) {
case TCConnectionStateConnected:
state = @"open";
Expand Down

0 comments on commit 2bb2a5e

Please sign in to comment.