347 lines
11 KiB
Objective-C
Executable File
347 lines
11 KiB
Objective-C
Executable File
//
|
|
// FirstViewController.m
|
|
// Talk
|
|
//
|
|
// Created by xuzs on 22/10/11.
|
|
// Copyright (c) 2022年 xuzs. All rights reserved.
|
|
//
|
|
|
|
#import "EquAdd.h"
|
|
#import "HKHTextField.h"
|
|
//#import "MainTabBar.h"
|
|
#import "UI.h"
|
|
#import "HttpManager.h"
|
|
#import "Msg.h"
|
|
#import "Pub.h"
|
|
#import "sysInfo.h"
|
|
#import "Sformat.h"
|
|
#import "EquList.h"
|
|
#import "Opendoor.h"
|
|
#import "EquConfig.h"
|
|
|
|
|
|
@interface EquAdd ()
|
|
|
|
@property (nonatomic, strong)UIView *rview;
|
|
@property (nonatomic, strong)UILabel *equidLabel,*nameLabel;
|
|
@property (nonatomic, strong)HKHTextField *equidTextField, *nameTextField;
|
|
@property (nonatomic, strong)UIButton *addButton ,*selEquButton;
|
|
@property (nonatomic, strong)UIAlertController *waiting;
|
|
|
|
@property (nonatomic, strong) NSMutableArray *equArray;
|
|
|
|
@end
|
|
|
|
@implementation EquAdd
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
// Do any additional setup after loading the view, typically from a nib.
|
|
|
|
//UIView *RootView = [[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil] objectAtIndex:0];
|
|
//[self.title autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0,0,0,0)];
|
|
|
|
|
|
|
|
[self setupViews];
|
|
[self setUI];
|
|
if(self.equid == nil){
|
|
[self setupBle];
|
|
}
|
|
|
|
//https://blog.csdn.net/liumude123/article/details/80579152
|
|
//[self.view setNeedsUpdateConstraints];
|
|
|
|
|
|
|
|
}
|
|
- (void)updateViewConstraints
|
|
{
|
|
[super updateViewConstraints];
|
|
NSLog(@"updateViewConstraints");
|
|
|
|
[self setUI];
|
|
}
|
|
- (void)setupViews
|
|
{
|
|
|
|
|
|
[self.view addSubview:self.rview];
|
|
[self.rview addSubview:self.equidLabel];
|
|
[self.rview addSubview:self.equidTextField];
|
|
[self.rview addSubview:self.nameLabel];
|
|
[self.rview addSubview:self.nameTextField];
|
|
[self.rview addSubview:self.addButton];
|
|
|
|
|
|
if(self.equid){
|
|
[self setNavTitle:@"命名设备"];
|
|
|
|
self.equidTextField.enabled = NO;
|
|
self.equidTextField.text = self.equid;
|
|
self.nameTextField.text = self.name;
|
|
|
|
|
|
|
|
}
|
|
else{
|
|
|
|
[self.rview addSubview:self.selEquButton];
|
|
self.equidTextField.enabled = NO;
|
|
self.equidTextField.backgroundColor = [UIColor whiteColor];
|
|
[self setNavTitle:@"添加设备"];
|
|
|
|
|
|
|
|
[self.equidTextField resignFirstResponder];
|
|
//[self.equidTextField setUserInteractionEnabled:YES];
|
|
[self.selEquButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SelEqu)]];
|
|
[self.addButton setTitle:@"点击搜索门锁" forState:UIControlStateNormal];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.addButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(AddEqu)]];
|
|
}
|
|
- (void)setUI
|
|
{
|
|
[UI SetRView:self.rview Top:[[NSString alloc] initWithFormat:@"%f",[UI getStatuAndStatuHeight]+10] Right:@"20" Bottom:@"0" Left:@"20"];
|
|
|
|
|
|
[UI setFrameInView:self.equidLabel Width:@"70" Height:@"44" TranslateX:@"0" TranslateY:@"10" InView:nil Position:UITopLeft];
|
|
[UI setFrameInView:self.equidTextField Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"10" InView:nil Position:UITopRight];
|
|
[UI setFrameInView:self.nameLabel Width:@"70" Height:@"44" TranslateX:@"0" TranslateY:@"64" InView:nil Position:UITopLeft];
|
|
[UI setFrameInView:self.nameTextField Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"64" InView:nil Position:UITopRight];
|
|
[UI chgMargin:self.equidTextField Css:@"0 0 0 80"];
|
|
[UI chgMargin:self.nameTextField Css:@"0 0 0 80"];
|
|
|
|
|
|
[UI setFrameInView:self.addButton Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"140" InView:nil Position:UITopMid];
|
|
|
|
|
|
if(self.equid==nil){
|
|
[UI setFrameInView:self.selEquButton Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"10" InView:nil Position:UITopRight];
|
|
[UI chgMargin:self.selEquButton Css:@"0 0 0 80"];
|
|
}
|
|
}
|
|
|
|
- (void)setupBle{
|
|
_ble = [[CBCentralManager alloc] initWithDelegate:self queue:(nil)];
|
|
self.equArray = [[NSMutableArray alloc] init];
|
|
}
|
|
|
|
#pragma mark-CBCentralManagerDelegate
|
|
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
|
|
switch (central.state){
|
|
case CBCentralManagerStatePoweredOff:{
|
|
NSLog(@"蓝牙关闭");
|
|
[self alert:@"手机蓝牙未开启"];
|
|
}
|
|
|
|
break;
|
|
case CBCentralManagerStatePoweredOn:{
|
|
NSLog(@"蓝牙打开");
|
|
[self ScanEqu];
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
|
|
if(peripheral.name==nil)return;
|
|
if(![peripheral.name hasPrefix:@"T9A"])return;
|
|
NSLog(@"发现蓝牙:%@,%@",peripheral,advertisementData);
|
|
if(self.waiting){
|
|
[self.waiting dismissViewControllerAnimated:YES completion:nil];
|
|
self.waiting = nil;
|
|
}
|
|
|
|
if(self.device==nil){
|
|
self.device = peripheral;
|
|
self.equidTextField.text = self.device.name;
|
|
self.nameTextField.placeholder = self.device.name;
|
|
[self.addButton setTitle:@"添加设备" forState:UIControlStateNormal];
|
|
}
|
|
for(int i=0;i<self.equArray.count;i++){
|
|
CBPeripheral *item = self.equArray[i];
|
|
if([item.name isEqual:peripheral.name])return;
|
|
}
|
|
[self.equArray addObject:peripheral];
|
|
}
|
|
|
|
|
|
- (void)ScanEqu{
|
|
self.waiting = [Msg Waiting:self Msg:@"请打开门锁蓝牙,正在搜索门锁…" SelectedHandler:^{
|
|
self.waiting = nil;
|
|
if(self.device==nil){
|
|
[self alert:@"搜索失败,请确认门锁已开启蓝牙"];
|
|
}
|
|
}];
|
|
[NSThread detachNewThreadSelector:@selector(ScanEqu_thr) toTarget:self withObject:nil];
|
|
|
|
|
|
}
|
|
- (void)ScanEqu_thr{
|
|
|
|
sleep(1);
|
|
[_ble scanForPeripheralsWithServices:nil options:nil];
|
|
sleep(10);
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{//转到主线程处理
|
|
if(self.waiting){
|
|
[self.waiting dismissViewControllerAnimated:YES completion:nil];
|
|
self.waiting = nil;
|
|
if(self.device==nil){
|
|
[self alert:@"搜索失败,请确认门锁已开启蓝牙"];
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
- (void)SelEqu{
|
|
NSLog(@"SelEqu");
|
|
|
|
[Msg Select:self SourceView:self.equidTextField Title:@"选择设备" Options:self.equArray SelectedHandler:^(int index) {
|
|
if(index>0 && index<self.equArray.count){
|
|
self.device = self.equArray[index];
|
|
self.equidTextField.text = self.device.name;
|
|
self.nameTextField.placeholder = self.device.name;
|
|
|
|
}
|
|
}];
|
|
}
|
|
- (void)AddEqu{
|
|
if([self.equidTextField.text isEqualToString:@""]){
|
|
[self ScanEqu];
|
|
return;
|
|
}
|
|
|
|
NSString *name = self.nameTextField.text;
|
|
if([name isEqual:@""]){
|
|
name = self.equidTextField.text;
|
|
}
|
|
[HttpManager BindEquSucc:[Pub getApp].sysinfo.username Token:[Pub getApp].sysinfo.token Equid:self.equidTextField.text Name:name HttpResultHandler:^(NSDictionary *json) {
|
|
|
|
if([[json objectForKey:@"result"] isEqualToString:@"ok"]){
|
|
[Opendoor refEqulist:^{
|
|
if(self.parent)
|
|
[self.parent setData];
|
|
}];
|
|
[self pageBack];
|
|
if(self.equid==nil){
|
|
EquConfig *equconfig = [[EquConfig alloc] init];
|
|
equconfig.equid = self.device.name;
|
|
[self.navigationController pushViewController:equconfig animated:YES];
|
|
}
|
|
}
|
|
else{
|
|
[self alert:[json objectForKey:@"result"]];
|
|
}
|
|
}];
|
|
}
|
|
#pragma mark Property Accessors
|
|
- (UIView *)rview
|
|
{
|
|
if (!_rview) {
|
|
_rview = [[UIView alloc] init];
|
|
//_rview.backgroundColor = [UIColor redColor];
|
|
}
|
|
return _rview;
|
|
}
|
|
|
|
- (UILabel *)equidLabel
|
|
{
|
|
if (!_equidLabel) {
|
|
_equidLabel = [[UILabel alloc] init];
|
|
//_equidLabel.backgroundColor = [UIColor orangeColor];
|
|
//_equidLabel.numberOfLines = 0;
|
|
_equidLabel.font = [UIFont systemFontOfSize:15.0f];
|
|
_equidLabel.textColor = [UIColor blackColor];
|
|
_equidLabel.textAlignment = NSTextAlignmentLeft;
|
|
_equidLabel.text = NSLocalizedString(@"设备号", nil);
|
|
}
|
|
return _equidLabel;
|
|
}
|
|
- (UILabel *)nameLabel
|
|
{
|
|
if (!_nameLabel) {
|
|
_nameLabel = [[UILabel alloc] init];
|
|
//_nameLabel.backgroundColor = [UIColor orangeColor];
|
|
//_nameLabel.numberOfLines = 0;
|
|
_nameLabel.font = [UIFont systemFontOfSize:15.0f];
|
|
_nameLabel.textColor = [UIColor blackColor];
|
|
_nameLabel.textAlignment = NSTextAlignmentLeft;
|
|
_nameLabel.text = NSLocalizedString(@"设备名称", nil);
|
|
}
|
|
return _nameLabel;
|
|
}
|
|
- (HKHTextField *)equidTextField
|
|
{
|
|
if (!_equidTextField) {
|
|
_equidTextField = [[HKHTextField alloc] init];
|
|
_equidTextField.borderStyle = UITextBorderStyleRoundedRect;
|
|
_equidTextField.placeholder = @"设备号";
|
|
|
|
}
|
|
return _equidTextField;
|
|
}
|
|
- (HKHTextField *)nameTextField
|
|
{
|
|
if (!_nameTextField) {
|
|
_nameTextField = [[HKHTextField alloc] init];
|
|
_nameTextField.borderStyle = UITextBorderStyleRoundedRect;
|
|
_nameTextField.placeholder = @"设备名称";
|
|
|
|
}
|
|
return _nameTextField;
|
|
}
|
|
|
|
|
|
- (UIButton *)selEquButton
|
|
{
|
|
if (!_selEquButton) {
|
|
_selEquButton = [[UIButton alloc] init];
|
|
[_selEquButton setTitle:@"" forState:UIControlStateNormal];
|
|
[_selEquButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
|
|
[_selEquButton.layer setMasksToBounds:YES];
|
|
[_selEquButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
|
|
//边框宽度
|
|
[_selEquButton.layer setBorderWidth:1.0];
|
|
_selEquButton.layer.opacity = 0.1;
|
|
_selEquButton.layer.borderColor=[UIColor grayColor].CGColor;
|
|
|
|
}
|
|
return _selEquButton;
|
|
}
|
|
|
|
- (UIButton *)addButton
|
|
{
|
|
if (!_addButton) {
|
|
_addButton = [[UIButton alloc] init];
|
|
[_addButton setTitle:@"提交" forState:UIControlStateNormal];
|
|
[_addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
[_addButton setTitleColor:[UIColor colorWithRed:255/255. green:128/255. blue:0/255. alpha:1] forState:UIControlStateHighlighted];
|
|
_addButton.backgroundColor = [UI colorWithHex:0x378fe8];
|
|
[_addButton.layer setMasksToBounds:YES];
|
|
[_addButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
|
|
//边框宽度
|
|
//[_addButton.layer setBorderWidth:1.0];
|
|
//_addButton.layer.borderColor=[UIColor grayColor].CGColor;
|
|
|
|
}
|
|
return _addButton;
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning {
|
|
[super didReceiveMemoryWarning];
|
|
// Dispose of any resources that can be recreated.
|
|
}
|
|
|
|
@end
|