EepromController.m 16.4キロバイト
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
//
//  EepromController.m
//  jacket_test_ios
//
//  Created by ドラッサル 亜嵐 on 2017/07/24.
//  Copyright © 2017年 ドラッサル 亜嵐. All rights reserved.
//

#import "EepromController.h"
#import "EepromTemplateController.h"
#import "DBManager.h"

@interface EepromController ()

@end

@implementation EepromController

- (void)viewDidLoad {
    [super viewDidLoad];

    //The setup code (in viewDidLoad in your view controller)
    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:singleFingerTap];

    // Do any additional setup after loading the view from its nib.
    
    txtId.text = @"";
    txtModel.text = @"";
    txtType.text = @"";
    txtOs.text = @"";
    txtPin.text = @"";
    
    bleCommandState = EEPROM__EEPROM_READ_ID;
    [self bleCommandTask];
}

- (void)viewWillAppear:(BOOL)animated {
    [self registerForKeyboardNotifications];
    self.lastProtocolDelegate = self.protocol.delegate;
    self.protocol.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [self unregisterFromKeyboardNotifications];
    self.protocol.delegate = self.lastProtocolDelegate;
}

- (void)showAlert:(NSString*)title message:(NSString*)message{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)unregisterFromKeyboardNotifications
{
    /*
     [[NSNotificationCenter defaultCenter] addObserver:self
     selector:@selector(keyboardWasShown:)
     name:UIKeyboardDidShowNotification object:nil];
     
     [[NSNotificationCenter defaultCenter] addObserver:self
     selector:@selector(keyboardWillBeHidden:)
     name:UIKeyboardWillHideNotification object:nil];
     */
    
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
    
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        [scrollView scrollRectToVisible:activeField.frame animated:YES];
    }
}
/*
 There are other ways you can scroll the edited area in a scroll view above an obscuring keyboard. Instead of altering the bottom content inset of the scroll view, you can extend the height of the content view by the height of the keyboard and then scroll the edited text object into view. Although the UIScrollView class has a contentSize property that you can set for this purpose, you can also adjust the frame of the content view, as shown in Listing 4-3. This code also uses the setContentOffset:animated: method to scroll the edited field into view, in this case scrolling it just above the top of the keyboard.
 */
/*
- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = activeField.superview.frame;
    bkgndRect.size.height += kbSize.height;
    [activeField.superview setFrame:bkgndRect];
    [scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];
}
*/
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    //CGPoint location = [recognizer locationInView:[recognizer.view superview]];
    
    //Do stuff here...
    [self.view endEditing:YES];
}

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

- (IBAction)cancelButtonClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (IBAction)saveButtonClicked:(id)sender {
    bleCommandState = EEPROM__EEPROM_WP_OFF;
    [self bleCommandTask];
}

- (IBAction)resetButtonClicked:(id)sender {
    txtId.text = @"";
    txtModel.text = @"";
    txtType.text = @"";
    txtOs.text = @"";
    
    bleCommandState = EEPROM__EEPROM_READ_ID;
    [self bleCommandTask];
}

- (IBAction)templateLoadButtonClicked:(id)sender {
    EepromTemplateController *viewObj=[[EepromTemplateController alloc] initWithNibName:@"EepromTemplateController" bundle:nil];
    viewObj.delegate = self;
    [self presentViewController:viewObj animated:YES completion: nil];
}

- (IBAction)templateSaveButtonClicked:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // optionally configure the text field
        textField.keyboardType = UIKeyboardTypeAlphabet;
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"キャンセル"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
                                                         UITextField *textField = [alert.textFields firstObject];
                                                         textField.placeholder = @"Enter Input";
                                                     }];
    [alert addAction:cancelAction];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"保存"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
                                                         UITextField *textField = [alert.textFields firstObject];
                                                         textField.placeholder = @"Enter Input";
                                                         [self clickedSaveNameInput:textField.text];
                                                     }];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)clickedSaveNameInput:(NSString *)textFieldText {
    NSLog(@"clickedSaveNameInput:'%@'",textFieldText);
    
    NSString *dataIdText = txtId.text;
    NSString *dataModelText = txtModel.text;
    NSString *dataTypeText = txtType.text;
    NSString *dataOsText = txtOs.text;
    NSString *dataPinText = txtPin.text;
    
    [[DBManager sharedInstance] saveEepromTemplateData:nil
                                            recordName:textFieldText
                                                dataId:dataIdText
                                           deviceModel:dataModelText
                                              dataType:dataTypeText
                                                dataOs:dataOsText
                                               dataPin:dataPinText];
}

- (void)didChoose:(NSString *)recordId
       recordName:(NSString *)recordName
           dataId:(NSString *)dataId
        dataModel:(NSString *)dataModel
         dataType:(NSString *)dataType
           dataOs:(NSString *)dataOs
          dataPin:(NSString *)dataPin {
    txtId.text = dataId;
    txtModel.text = dataModel;
    txtType.text = dataType;
    txtOs.text = dataOs;
    txtPin.text = dataPin;
}

- (BOOL)bleCommandTask {
    switch(bleCommandState) {
        case EEPROM__EEPROM_WP_OFF:
            [[self protocol]putData:@"eepromWriteProtect" data:@"0"];
            break;
        case EEPROM__EEPROM_WP_OFF_DONE:
            bleCommandState = EEPROM__EEPROM_WRITE_ID;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_WP_ON:
            [[self protocol]putData:@"eepromWriteProtect" data:@"1"];
            break;
        case EEPROM__EEPROM_WP_ON_DONE:
            bleCommandState = EEPROM__DONE;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_READ_ID:
            [[self protocol]putData:@"readDeviceId" data:nil];
            break;
        case EEPROM__EEPROM_READ_MODEL:
            [[self protocol]putData:@"readDeviceModel" data:nil];
            break;
        case EEPROM__EEPROM_READ_TYPE:
            [[self protocol]putData:@"readDeviceType" data:nil];
            break;
        case EEPROM__EEPROM_READ_OS:
            [[self protocol]putData:@"readDeviceOs" data:nil];
            break;
        case EEPROM__EEPROM_READ_PIN:
            [[self protocol]putData:@"readDevicePin" data:nil];
            break;
        case EEPROM__EEPROM_READ_ID_DONE:
            bleCommandState = EEPROM__EEPROM_READ_MODEL;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_READ_MODEL_DONE:
            bleCommandState = EEPROM__EEPROM_READ_TYPE;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_READ_TYPE_DONE:
            bleCommandState = EEPROM__EEPROM_READ_OS;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_READ_OS_DONE:
            bleCommandState = EEPROM__EEPROM_READ_PIN;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_READ_PIN_DONE:
            bleCommandState = EEPROM__DONE;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_WRITE_ID:
            if(![strId isEqualToString:txtId.text]) {
                [[self protocol]putData:@"writeDeviceId" data:txtId.text];
            } else {
                bleCommandState = EEPROM__EEPROM_WRITE_ID_DONE;
                [self bleCommandTask];
            }
            break;
        case EEPROM__EEPROM_WRITE_MODEL:
            if(![strModel isEqualToString:txtModel.text]) {
                [[self protocol]putData:@"writeDeviceModel" data:txtModel.text];
            } else {
                bleCommandState = EEPROM__EEPROM_WRITE_MODEL_DONE;
                [self bleCommandTask];
            }
            break;
        case EEPROM__EEPROM_WRITE_TYPE:
            if(![strType isEqualToString:txtType.text]) {
                [[self protocol]putData:@"writeDeviceType" data:txtType.text];
            } else {
                bleCommandState = EEPROM__EEPROM_WRITE_TYPE_DONE;
                [self bleCommandTask];
            }
            break;
        case EEPROM__EEPROM_WRITE_OS:
            if(![strOs isEqualToString:txtOs.text]) {
                [[self protocol]putData:@"writeDeviceOs" data:txtOs.text];
            } else {
                bleCommandState = EEPROM__EEPROM_WRITE_OS_DONE;
                [self bleCommandTask];
            }
            break;
        case EEPROM__EEPROM_WRITE_PIN:
            if(![strOs isEqualToString:txtPin.text]) {
                [[self protocol]putData:@"writeDevicePin" data:txtPin.text];
            } else {
                bleCommandState = EEPROM__EEPROM_WRITE_PIN_DONE;
                [self bleCommandTask];
            }
            break;
        case EEPROM__EEPROM_WRITE_ID_DONE:
            bleCommandState = EEPROM__EEPROM_WRITE_MODEL;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_WRITE_MODEL_DONE:
            bleCommandState = EEPROM__EEPROM_WRITE_TYPE;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_WRITE_TYPE_DONE:
            bleCommandState = EEPROM__EEPROM_WRITE_OS;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_WRITE_OS_DONE:
            bleCommandState = EEPROM__EEPROM_WRITE_PIN;
            [self bleCommandTask];
            break;
        case EEPROM__EEPROM_WRITE_PIN_DONE:
            bleCommandState = EEPROM__EEPROM_WP_ON;
            [self showAlert:@"EEPROM書き込み" message:@"EEPROMに保存しました。"];
            [self bleCommandTask];
            break;
        case EEPROM__DONE:
        {
            NSLog(@"Ble command set done!");
            break;
        }
    }
    return false;
}

- (void)protocolDidGetData:(NSString *)dataType data:(NSString *)dataData {
    if([dataType isEqualToString:@"eepromWriteProtect"]) {
        if([dataData isEqualToString:@"0"]) {
            NSLog(@"eepromWriteProtect 0");
            bleCommandState = EEPROM__EEPROM_WP_OFF_DONE;
            [self bleCommandTask];
        } else if([dataData isEqualToString:@"1"]) {
            NSLog(@"eepromWriteProtect 1");
            bleCommandState = EEPROM__EEPROM_WP_ON_DONE;
            [self bleCommandTask];
        }
    } else if([dataType isEqualToString:@"readDeviceId"]) {
        NSLog(@"infoDeviceId");
        strId = dataData;
        txtId.text = dataData;
        bleCommandState = EEPROM__EEPROM_READ_ID_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"readDeviceModel"]) {
        NSLog(@"infoDeviceModel");
        strModel = dataData;
        txtModel.text = dataData;
        bleCommandState = EEPROM__EEPROM_READ_MODEL_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"readDeviceType"]) {
        NSLog(@"infoDeviceType");
        strType = dataData;
        txtType.text = dataData;
        bleCommandState = EEPROM__EEPROM_READ_TYPE_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"readDeviceOs"]) {
        NSLog(@"infoDeviceOs");
        strOs = dataData;
        txtOs.text = dataData;
        bleCommandState = EEPROM__EEPROM_READ_OS_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"readDevicePin"]) {
        NSLog(@"infoDevicePin");
        strPin = dataData;
        txtPin.text = dataData;
        bleCommandState = EEPROM__EEPROM_READ_PIN_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"writeDeviceId"]) {
        NSLog(@"writeDeviceId");
        bleCommandState = EEPROM__EEPROM_WRITE_ID_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"writeDeviceModel"]) {
        NSLog(@"writeDeviceModel");
        bleCommandState = EEPROM__EEPROM_WRITE_MODEL_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"writeDeviceType"]) {
        NSLog(@"writeDeviceType");
        bleCommandState = EEPROM__EEPROM_WRITE_TYPE_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"writeDeviceOs"]) {
        NSLog(@"writeDeviceOs");
        bleCommandState = EEPROM__EEPROM_WRITE_OS_DONE;
        [self bleCommandTask];
    } else if([dataType isEqualToString:@"writeDevicePin"]) {
        NSLog(@"writeDevicePin");
        bleCommandState = EEPROM__EEPROM_WRITE_PIN_DONE;
        [self bleCommandTask];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end