BleControl.m 4.17キロバイト
//
//  BleControl.m
//  jacket_ios
//
//  Created by ドラッサル 亜嵐 on 2016/04/27.
//  Copyright © 2016年 ドラッサル 亜嵐. All rights reserved.
//

#import "BleControl.h"

@interface BleControl ()

@property (nonatomic) NSInteger slotCounter;
@property (nonatomic) NSInteger slotCounterMin;
@property (nonatomic) NSInteger slotCounterMax;
@property (nonatomic) NSTimer * tmr;

@end

@implementation BleControl
@synthesize ble;
@synthesize protocol;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.slotCounterMin = 0;
    self.slotCounterMax = 5;
    self.slotCounter = self.slotCounterMin;
}

- (void)viewWillAppear:(BOOL)animated {
    protocol.delegate = self;
    
    self.tmr = [NSTimer timerWithTimeInterval:0.1
                                       target:self
                                     selector:@selector(tmrMethod:)
                                     userInfo:nil
                                      repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.tmr forMode:NSRunLoopCommonModes];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.tmr invalidate];
}

- (void)tmrMethod:(NSTimer *)timer {
    [self getNextSlot];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getNextSlot {
    // TODO: Fix
    /*
    if(self.slotCounter == 0) {
        [protocol getTime];
    } else if(self.slotCounter == 1) {
        [protocol getUsetime];
    } else if(self.slotCounter == 2) {
        [protocol getState];
    } else if(self.slotCounter == 3) {
        [protocol getSleep];
    } else if(self.slotCounter == 4) {
        [protocol getWake];
    } else if(self.slotCounter == 5) {
        [protocol getWalking];
    }
     */
    self.slotCounter++;

    if(self.slotCounter > self.slotCounterMax) {
        self.slotCounter = self.slotCounterMin;
    }
}

- (IBAction)btnEditState:(id)sender {
    [self performSegueWithIdentifier:@"idSegueEditState" sender:self];
}

- (IBAction)btnEditTime:(id)sender {
    [self performSegueWithIdentifier:@"idSegueEditTime" sender:self];
}

- (IBAction)btnEditSleep:(id)sender {
    [self performSegueWithIdentifier:@"idSegueEditSleep" sender:self];
}

- (IBAction)btnEditWake:(id)sender {
    [self performSegueWithIdentifier:@"idSegueEditWake" sender:self];
}

- (IBAction)btnEditWalking:(id)sender {
    // TODO: Fix
    if(stateCurrentWalking == false) {
        //[protocol setWalking:true];
    } else {
        //[protocol setWalking:false];
    }
}

- (IBAction)btnResetUsetime:(id)sender {
    // TODO: Fix
    //[protocol resetUsetime];
}

- (IBAction)btnUsetime:(id)sender {
    [self performSegueWithIdentifier:@"idSegueUsetime" sender:self];
}

- (IBAction)btnCaseUnlock:(id)sender {
    // TODO: Fix
    //[protocol setUnlock];
}

- (void) protocolDidReceiveState:(NSString *)data text:(NSString *)text {
    [currentState setText:text];
}

- (void) protocolDidReceiveTime:(NSString *) data {
    [currentTime setText:data];
}

- (void) protocolDidReceiveSleepingBegin:(NSString *) data {
    [currentSleep setText:data];
}

- (void) protocolDidReceiveSleepingEnd:(NSString *) data {
    [currentWake setText:data];
}

- (void) protocolDidReceiveUsetime:(NSInteger) data {
    int seconds = data % 60;
    int minutes = (data / 60) % 60;
    int hours = (int)data / 3600;
    
    NSString *strUsetime = [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
    [currentUsetime setText:strUsetime];
}

- (void) protocolDidReceiveUsetimeSlot:(NSInteger) dataSlot dataStamp:(NSTimeInterval) dataStamp dataUsetime: (NSInteger) dataUsetime {
    
}

- (void) protocolDidReceiveWalking:(BOOL)data {
    stateCurrentWalking = data;
    NSString *strData = [NSString alloc];

    if(stateCurrentWalking == false) {
        strData = @"無効";
    } else {
        strData = @"有効";
    }
    [currentWalking setText:strData];
}


@end