Skip to content

Commit

Permalink
Add single new device view
Browse files Browse the repository at this point in the history
  • Loading branch information
neooxu committed Nov 24, 2014
1 parent f1caf6d commit c4d0fa0
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 209 deletions.
12 changes: 4 additions & 8 deletions EMW ToolBox/EasyLink/Classes/iPhone/EasyLinkMainViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
#import "EasyLinkFTCTableViewController.h"
#import "CustomIOS7AlertView.h"
#import "EasyLinkOTATableViewController.h"
#import "EasyLinkFoundTableViewController.h"


@interface EasyLinkMainViewController : UIViewController<UITextFieldDelegate, EasyLinkFTCDelegate, EasyLinkFTCDataDelegate, EasyLinkOTADelegate>{
NSMutableArray *foundModules;
@private
IBOutlet UITableView *configTableView;
IBOutlet UITableView *foundModuleTableView;
UITextField *ssidField,*bssidField,*passwordField,*userInfoField,*ipAddress;

IBOutlet UIButton *EasylinkV2Button;
IBOutlet UIButton *newDevicesButton;
UIAlertView *alertView;
CustomIOS7AlertView *customAlertView, *otaAlertView;
NSMutableDictionary *deviceIPConfig;
Expand All @@ -32,19 +34,13 @@

EASYLINK *easylink_config;
CustomIOS7AlertView *easyLinkSendingView;
__weak EasyLinkFoundTableViewController *foundTableViewController;

Reachability *wifiReachability;
}

@property (strong, nonatomic) NSMutableArray *foundModules;

/*
This method waits for an acknowledge from the remote device than it stops the transmit to the remote device and returns with data it got from the remote device.
This method blocks until it gets respond.
The method will return true if it got the ack from the remote device or false if it got aborted by a call to stopTransmitting.
In case of a failure the method throws an OSFailureException.
*/
- (void) waitForAck: (id)sender;

/*
This method start the transmitting the data to connected
Expand Down
156 changes: 46 additions & 110 deletions EMW ToolBox/EasyLink/Classes/iPhone/EasyLinkMainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#import "EasyLinkFTCTableViewController.h"
#import "EasyLinkIpConfigTableViewController.h"

#define MOVE_UP_ON_3_5_INCH (-65)

extern BOOL newModuleFound;
BOOL configTableMoved = NO;

Expand All @@ -28,7 +26,7 @@ @interface EasyLinkMainViewController (Private)
/* button action, where we need to start or stop the request
@param: button ... tag value defines the action
*/

- (void)updateButtonTitle_NewDevices;
- (IBAction)easyLinkV1ButtonAction:(UIButton*)button;
- (IBAction)easyLinkV2ButtonAction:(UIButton*)button;
- (void)handleSingleTapPhoneImage:(UIGestureRecognizer *)gestureRecognizer;
Expand Down Expand Up @@ -71,7 +69,6 @@ -(void) enableUIAccess:(BOOL) isEnable;

@implementation EasyLinkMainViewController
@synthesize foundModules;
@synthesize waitForAckThread;

- (void)awakeFromNib
{
Expand Down Expand Up @@ -106,13 +103,6 @@ - (void)viewDidLoad {
self.foundModules = [[NSMutableArray alloc]initWithCapacity:10];

deviceIPConfig = [[NSMutableDictionary alloc] initWithCapacity:5];

// CGRect screenBounds = [UIScreen mainScreen].bounds;

// if( screenBounds.size.height == 480.0){
// imagePhoneView.center = CGPointMake(imagePhoneView.center.x, imagePhoneView.center.y+MOVE_UP_ON_3_5_INCH/2);
// imagePhoneView.transform = CGAffineTransformMakeTranslation(0, MOVE_UP_ON_3_5_INCH);
// }


//按钮加边框
Expand All @@ -136,8 +126,6 @@ - (void)viewDidLoad {
wifiReachability = [Reachability reachabilityForLocalWiFi]; //监测Wi-Fi连接状态
[wifiReachability startNotifier];

waitForAckThread = nil;

NetworkStatus netStatus = [wifiReachability currentReachabilityStatus];
if ( netStatus == NotReachable ) {// No activity if no wifi
alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"WiFi not available. Please check your WiFi connection" delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
Expand All @@ -150,12 +138,16 @@ - (void)viewDidLoad {
[deviceIPConfig setObject:[EASYLINK getGatewayAddress] forKey:@"DnsServer"];
}

[self updateButtonTitle_NewDevices];


//// stoping the process in app backgroud state
NSLog(@"regisister notificationcenter");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterInBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterInforground:) name:UIApplicationDidBecomeActiveNotification object:nil];


}

- (void)viewWillAppear:(BOOL)animated {
Expand Down Expand Up @@ -209,6 +201,17 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - New Devices Button -

- (void)updateButtonTitle_NewDevices
{
if ([foundModules count] == 0) {
//newDevicesButton.titleLabel.text = @"hello";
[newDevicesButton setTitle:@"New Devices(0)" forState: UIControlStateNormal];
[newDevicesButton setUserInteractionEnabled:false];
}
}

#pragma mark - TRASMITTING DATA -

/*
Expand All @@ -218,8 +221,6 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
-(void) sendAction{
newModuleFound = NO;
[easylink_config transmitSettings];
//waitForAckThread = [[NSThread alloc] initWithTarget:self selector:@selector(waitForAck:) object:nil];
//[waitForAckThread start];
}

/*
Expand All @@ -228,8 +229,6 @@ -(void) sendAction{
*/
-(void) stopAction{
[easylink_config stopTransmitting];
//[waitForAckThread cancel];
//waitForAckThread= nil;
}

/*
Expand Down Expand Up @@ -301,15 +300,6 @@ - (void)startTransmitting: (int)version {
[self enableUIAccess:NO];
}

/*!!!!!!
This is the button action, where we need to start or stop the request
@param: button ... tag value defines the action !!!!!!!!!
!!!*/
- (void)handleSingleTapPhoneImage:(UIGestureRecognizer *)gestureRecognizer
{
[self easyLinkV2ButtonAction:EasylinkV2Button];
}

- (IBAction)easyLinkV2ButtonAction:(UIButton*)button{

CATransition *animation = [CATransition animation];
Expand Down Expand Up @@ -391,7 +381,6 @@ - (IBAction)easyLinkV2ButtonAction:(UIButton*)button{
__weak EasyLinkMainViewController *_self = self;
[easyLinkSendingView setOnButtonTouchUpInside:^(CustomIOS7AlertView *customIOS7AlertView, NSInteger buttonIndex) {
if(buttonIndex == 0){
[button setTitle:@"START" forState:UIControlStateNormal];
[_self enableUIAccess:YES];
}
[button setBackgroundColor:[UIColor clearColor]];
Expand Down Expand Up @@ -424,30 +413,11 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableCellIdentifier;
UITableViewCell *cell = nil;
NSString *currentVerStr;

if(tableView == configTableView){
tableCellIdentifier = @"APInfo";

cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableCellIdentifier];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableCellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setBackgroundColor:[UIColor colorWithRed:0.100 green:0.478 blue:1.000 alpha:0.1]];
cell = [self prepareCell:cell atIndexPath:indexPath];
}
}else{
tableCellIdentifier = @"New Module";

cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableCellIdentifier];
[cell setBackgroundColor:[UIColor colorWithRed:0.100 green:0.478 blue:1.000 alpha:0.4]];
[cell setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.6]];
cell.textLabel.text = [[self.foundModules objectAtIndex:indexPath.row] objectForKey:@"N"];
currentVerStr = [[self.foundModules objectAtIndex:indexPath.row] objectForKey:@"FW"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Firmware: %@",currentVerStr? currentVerStr:@"unkown"];
}
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"APInfo"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setBackgroundColor:[UIColor colorWithRed:0.100 green:0.478 blue:1.000 alpha:0.1]];
cell = [self prepareCell:cell atIndexPath:indexPath];

return cell;
}

Expand All @@ -461,32 +431,16 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == configTableView)
return 4;
else
return [self.foundModules count];
return 4;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView == foundModuleTableView)
return @"Press the new device to continue...";
else
return nil;
return nil;
}


- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([self.foundModules count])
view.hidden = false;
else
view.hidden = true;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"Ignore";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
Expand All @@ -499,10 +453,7 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
if(tableView == foundModuleTableView)
return YES;
else
return NO;
return NO;
}

#pragma mark - UITextfiled delegate -
Expand Down Expand Up @@ -548,16 +499,10 @@ - (void)onFoundByFTC:(NSNumber *)ftcClientTag currentConfig: (NSData *)config;
[easylink_config closeFTCClient:[object objectForKey:@"client"]];
}
}
/*Add a new device*/
if([self.foundModules count]==0){
UIView *sectionHead = [foundModuleTableView headerViewForSection:0];
sectionHead.hidden = NO;
[sectionHead setNeedsDisplay];
}

[self.foundModules addObject:foundModule];
indexPath = [NSIndexPath indexPathForRow:[self.foundModules indexOfObject:foundModule] inSection:0];
[foundModuleTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
[foundTableViewController.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationRight];

/*Correct AP info input, save to file*/
Expand All @@ -580,8 +525,9 @@ - (void)onDisconnectFromFTC:(NSNumber *)ftcClientTag
{
NSIndexPath* indexPath;
NSDictionary *disconnectedClient;
/*Reloace an old device*/
[self.navigationController popToViewController:self animated:YES];


//if([self.navigationController topViewController] == )

for( NSDictionary *object in self.foundModules){
if ([[object objectForKey:@"client"] isEqualToNumber:ftcClientTag] ){
Expand All @@ -593,7 +539,7 @@ - (void)onDisconnectFromFTC:(NSNumber *)ftcClientTag

if(disconnectedClient != nil){
[self.foundModules removeObject: disconnectedClient ];
[foundModuleTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
[foundTableViewController.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}

Expand Down Expand Up @@ -623,12 +569,11 @@ - (void)onDisconnectFromFTC:(NSNumber *)ftcClientTag
}
}
}

if([self.foundModules count]==0){
UIView *sectionHead = [foundModuleTableView headerViewForSection:0];
sectionHead.hidden = YES;
[sectionHead setNeedsDisplay];
}

if(foundTableViewController != nil && [foundModules count] != 0)
[self.navigationController popToViewController:foundTableViewController animated:YES];
else
[self.navigationController popToViewController:self animated:YES];
}

#pragma mark - EasyLinkFTCTableViewController delegate-
Expand Down Expand Up @@ -744,9 +689,6 @@ -(void) enableUIAccess:(BOOL) isEnable{
passwordField.userInteractionEnabled = isEnable;
userInfoField.userInteractionEnabled = isEnable;
ipAddress.userInteractionEnabled = isEnable;

//[halo startAnimation: !isEnable];

}

/*
Expand All @@ -769,7 +711,8 @@ -(UITableViewCell *) prepareCell:(UITableViewCell *)cell atIndexPath:(NSIndexPat
[ssidField setPlaceholder:@"SSID"];
[ssidField setBackgroundColor:[UIColor clearColor]];
[ssidField setReturnKeyType:UIReturnKeyDone];
[ssidField setText:SSID];
//[ssidField setText:SSID];
[ssidField setText:@"Xiaomi.Router"];
[cell addSubview:ssidField];

cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
Expand All @@ -789,7 +732,8 @@ -(UITableViewCell *) prepareCell:(UITableViewCell *)cell atIndexPath:(NSIndexPat
[cell addSubview:passwordField];
NSString *password = [apInforRecord objectForKey:ssidField.text];
if(password == nil) password = @"";
[passwordField setText:password];
//[passwordField setText:password];
[passwordField setText:@"stm32f215"];


cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
Expand All @@ -803,7 +747,7 @@ -(UITableViewCell *) prepareCell:(UITableViewCell *)cell atIndexPath:(NSIndexPat
CELL_iPHONE_FIELD_HEIGHT)];
[userInfoField setDelegate:self];
[userInfoField setClearButtonMode:UITextFieldViewModeNever];
[userInfoField setPlaceholder:@"Authenticator"];
[userInfoField setPlaceholder:@"Authenticator(Optional)"];
[userInfoField setReturnKeyType:UIReturnKeyDone];
[userInfoField setBackgroundColor:[UIColor clearColor]];

Expand Down Expand Up @@ -926,23 +870,15 @@ - (void)wifiStatusChanged:(NSNotification*)notification{

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"First Time Configuration"]) {
NSIndexPath *indexPath = [foundModuleTableView indexPathForSelectedRow];
[foundModuleTableView deselectRowAtIndexPath:indexPath animated:YES];
NSMutableDictionary *object = [self.foundModules objectAtIndex:indexPath.row];

//[easylink_config stopTransmitting];
//easylink_config = nil;
if ( EasylinkV2Button.selected )
[self easyLinkV2ButtonAction:EasylinkV2Button]; /// Simply revert the state

[[segue destinationViewController] setConfigData:object];
[(EasyLinkFTCTableViewController *)[segue destinationViewController] setDelegate:self];

}
else if ([[segue identifier] isEqualToString:@"IP config"]) {
if ([[segue identifier] isEqualToString:@"IP config"]) {
[[segue destinationViewController] setDeviceIPConfig: deviceIPConfig];
}
else if ([[segue identifier] isEqualToString:@"New Devices"]) {
foundTableViewController = [segue destinationViewController];
[foundTableViewController setFoundModules: self.foundModules];
[foundTableViewController setDelegate:self];
[foundTableViewController setEasylink_config:easylink_config];
}
}


Expand Down
22 changes: 22 additions & 0 deletions EMW ToolBox/EasyLinkFoundTableViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// EasyLinkFoundTableViewController.h
//
//
// Created by William Xu on 14/11/19.
//
//

#import <UIKit/UIKit.h>
#import "EASYLINK.h"

@interface EasyLinkFoundTableViewController : UITableViewController{
id theDelegate;
}

@property (strong, nonatomic) NSMutableArray *foundModules;
@property (strong, nonatomic) EASYLINK *easylink_config;

- (id)delegate;
- (void)setDelegate:(id)delegate;

@end
Loading

0 comments on commit c4d0fa0

Please sign in to comment.