533 lines
19 KiB
Mathematica
Raw Normal View History

//
// FirstViewController.m
// Talk
//
// Created by xuzs on 22/10/11.
// Copyright (c) 2022 xuzs. All rights reserved.
//
#import "EquConfig.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 "WifiList.h"
@interface EquConfig ()
@property (nonatomic, strong)UIView *rview;
@property (nonatomic, strong)UILabel *WIFILabel,*PwdLabel,*hitLabel;
@property (nonatomic, strong)HKHTextField *WIFITextField, *PwdTextField;
@property (nonatomic, strong)UIButton *searchWIFIButton,*configButton;
@end
@implementation EquConfig
- (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 setNavTitle:@"配置网关"];
[self setupViews];
[self setUI];
[self setupBle];
}
- (void)updateViewConstraints
{
[super updateViewConstraints];
}
- (void)setupViews
{
[self.view addSubview:self.rview];
[self.rview addSubview:self.WIFILabel];
[self.rview addSubview:self.WIFITextField];
[self.rview addSubview:self.PwdLabel];
[self.rview addSubview:self.PwdTextField];
[self.rview addSubview:self.searchWIFIButton];
[self.rview addSubview:self.configButton];
[self.rview addSubview:self.hitLabel];
[self.searchWIFIButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SelectWifi)]];
[self.configButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ConfigEqu)]];
}
- (void)setUI
{
[UI SetRView:self.rview Top:[[NSString alloc] initWithFormat:@"%f",[UI getStatuAndStatuHeight]+10] Right:@"20" Bottom:@"0" Left:@"20"];
[UI setFrameInView:self.WIFILabel Width:@"70" Height:@"44" TranslateX:@"0" TranslateY:@"10" InView:nil Position:UITopLeft];
[UI setFrameInView:self.WIFITextField Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"10" InView:nil Position:UITopRight];
//[UI setFrameInView:self.searchWIFIButton Width:@"80" Height:@"44" TranslateX:@"0" TranslateY:@"10" InView:nil Position:UITopRight];
[UI setFrameInView:self.PwdLabel Width:@"70" Height:@"44" TranslateX:@"0" TranslateY:@"64" InView:nil Position:UITopLeft];
[UI setFrameInView:self.PwdTextField Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"64" InView:nil Position:UITopRight];
//[UI chgMargin:self.WIFITextField Css:@"0 90 0 80"];
[UI chgMargin:self.WIFITextField Css:@"0 0 0 80"];
[UI chgMargin:self.PwdTextField Css:@"0 0 0 80"];
[UI setFrameInView:self.configButton Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"140" InView:nil Position:UITopMid];
[UI setFrameInView:self.hitLabel Width:@"100%" Height:@"44" TranslateX:@"0" TranslateY:@"190" InView:nil Position:UITopMid];
}
- (void)setupBle{
_connected = NO;
_needSend = NO;
_sending = NO;
_ble = [[CBCentralManager alloc] initWithDelegate:self queue:(nil)];
}
#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 isEqual:self.equid])return;
NSLog(@"发现蓝牙:%@,%@",peripheral,advertisementData);
if(self.device==nil){
self.hitLabel.text = @"发现门锁";
self.device = peripheral;
}
if(!_connected){
self.hitLabel.text = @"正在连接门锁";
[_ble connectPeripheral:self.device options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(nonnull CBPeripheral *)peripheral{
NSLog(@"已连接门锁");
self.hitLabel.text = @"已连接门锁";
[self.device setDelegate:self];
[self.device discoverServices:nil];
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(nonnull CBPeripheral *)peripheral error:(nullable NSError *)error{
NSLog(@"连接门锁失败,重新连接");
self.hitLabel.text = @"连接门锁失败,重新连接";
[_ble connectPeripheral:self.device options:nil];
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(nonnull CBPeripheral *)peripheral error:(nullable NSError *)error{
NSLog(@"连接断开,正在重新连接");
self.hitLabel.text = @"连接断开,正在重新连接";
_connected = NO;
if(_sending){
_sending = NO;
_needSend = YES;
}
[_ble connectPeripheral:self.device options:nil];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{
if(error)return;
NSLog(@"已连接至门锁");
self.hitLabel.text = @"已连接至门锁";
for(CBService *service in peripheral.services){
NSLog(@"Service:%@;",service.UUID);
if([service.UUID isEqual:[CBUUID UUIDWithString:@"FFF0"]]){
NSLog(@"discoverCharacteristics:%@",service.UUID);
[_device discoverCharacteristics:nil forService:service];
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error{
NSLog(@"已连接到门锁");
self.hitLabel.text = @"已连接到门锁";
for(CBCharacteristic *characteristic in service.characteristics){
NSLog(@"Characteristic:%@",characteristic.UUID);
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF1"]]){
self.reader = characteristic;
[_device setNotifyValue:YES forCharacteristic:self.reader];
[_device readValueForCharacteristic:self.reader];
}
else if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF2"]]){
self.writer = characteristic;
}
}
_connected = YES;
if(_needSend){
[self ConfigEqu_go];
}
}
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error{
if(error){
_sending = NO;
_needSend = NO;
self.configButton.enabled = YES;
self.configButton.backgroundColor = [UI colorWithHex:0x378fe8];
NSLog(@"通信失败");
self.hitLabel.text = @"通信失败";
return;
}
NSLog(@"didWriteValueForCharacteristic :发送成功");
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error{
//
int len = characteristic.value.length;
NSLog(@"didUpdateValueForCharacteristic%ld",len);
Byte bb[len];
[characteristic.value getBytes:&bb length:len];
NSLog(@"%@",[[NSData alloc] initWithBytes:bb length:len]);
if(len==20 && bb[0]==0x22){
Byte temp[16];
NSData *temp2;
memcpy(temp,bb+3,16);
temp2 = [Pub AES128_Decrypt:@"1123456789ABCDEE" encryptData:[[NSData alloc] initWithBytes:temp length:16]];
NSLog(@"解密后:%@",temp2);
if(temp2){
Byte temp3[16];
memcpy(temp3,temp2.bytes,16);
if(temp3[0] == 0xf1){
//
if( bb[1]== bb[2]+1){//
if(temp3[3]==1){
NSLog(@"已下发WIFI信息请等待门锁连接网关");
self.hitLabel.text = @"已下发WIFI信息请等待门锁连接网关";
_sending = NO;
_needSend = NO;
self.configButton.enabled = YES;
self.configButton.backgroundColor = [UI colorWithHex:0x378fe8];
}
else{
NSLog(@"配网失败");
self.hitLabel.text = @"配网失败" ;
_sending = NO;
_needSend = NO;
self.configButton.enabled = YES;
self.configButton.backgroundColor = [UI colorWithHex:0x378fe8];
}
}
else{
if(temp3[3]==1){
[self Send20];
}
else{
NSLog(@"配网失败");
self.hitLabel.text = @"配网失败";
_sending = NO;
_needSend = NO;
self.configButton.enabled = YES;
self.configButton.backgroundColor = [UI colorWithHex:0x378fe8];
}
}
}
else if(temp3[0] == 0xf2){
//
if(temp3[3] == 0){
NSLog(@"配网成功");
self.hitLabel.text = @"配网成功";
//[Msg Alert:self Msg:@"配网成功"];
[Msg MessageBox:self Msg:@"配网成功" SelectedHandler:^(int index) {
[self pageBack];
}];
}
else{
NSLog(@"配网失败");
self.hitLabel.text = @"无法连接网关请检查WIFI名称和密码是否正确";
[Msg Alert:self Msg:@"无法连接网关请检查WIFI名称和密码是否正确"];
}
}
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error{
//NSLog(@"didUpdateNotificationStateForCharacteristic%ld",characteristic.value.length);
}
- (void)SelectWifi{
WifiList *wifi = [[WifiList alloc] init];
[self.navigationController pushViewController:wifi animated:YES];
}
- (void)ScanEqu{
NSLog(@"ScanEqu");
self.hitLabel.text = @"请打开门锁蓝牙,正在搜索门锁…";
[_ble scanForPeripheralsWithServices:nil options:nil];
}
- (void)ConfigEqu{
if(self.device==nil){
_needSend = YES;
[self ScanEqu];
return;
}
if(!_connected){_needSend = YES;return;}
if(_sending){return;}
[self ConfigEqu_go];
}
- (BOOL)SetUpData{
if([self.WIFITextField.text isEqualToString:@""]){[self alert:@"请输入wifi名称"];return NO;}
if([self.PwdTextField.text isEqualToString:@""]){[self alert:@"请输入wifi密码"];return NO;}
int bblen = 64;
Byte bb[bblen];
memset(bb, 0, 64);
[Pub addGKB:self.WIFITextField.text toByte:bb at:0];
[Pub addGKB:self.PwdTextField.text toByte:bb at:30];
[Pub addGKB:[Pub getApp].sysinfo.equid toByte:bb at:50];
int bag_len = 14;
int bag_len_last = bblen % bag_len;
int bag_num = (bblen-bag_len_last)/bag_len;
if(bag_len_last!=0)bag_num++;
Byte package1[bag_num*20];
memset(package1, 0, bag_num*20);
for(int i=0;i<bag_num;i++){
package1[0+i*20] = 0x22;
package1[1+i*20] = bag_num;
package1[2+i*20] = i;
package1[3+i*20] = 0xF1;
package1[4+i*20] = 1;
if(i<bag_num-1){
package1[5+i*20] = bag_len;
memcpy(package1+6+i*20,bb+i*bag_len,bag_len);
}
else{
package1[5+i*20] = bag_len_last;
memcpy(package1+6+i*20,bb+i*bag_len,bag_len_last);
}
}
//NSLog(@"package1 %@",[[NSData alloc] initWithBytes:package1 length:bag_num*20]);
//
int templen = 16;
Byte temp[templen];
memset(temp, 0, templen);
NSData *temp2;
Byte package2[bag_num*20];
memset(package2, 0, bag_num*20);
for(int i=0;i<bag_num;i++){
package2[0+i*20] = 0x22;
package2[1+i*20] = bag_num;
package2[2+i*20] = i;
package2[3+i*20] = 0xF1;
package2[4+i*20] = 1;
memcpy(temp,package1+3 + i*20,16);
NSLog(@"加密前 %@",[[NSData alloc] initWithBytes:temp length:templen]);
temp2 = [Pub AES128_Encrypt:@"1123456789ABCDEE" encryptData:[[NSData alloc] initWithBytes:temp length:templen]];
NSLog(@"加密后 %@",temp2);
memcpy(package2+3 + i*20,temp2.bytes ,16);
package2[19+i*20] = package1[19+i*20];
}
_data2send = [[NSData alloc] initWithBytes:package2 length:bag_num*20];
//NSLog(@"_data2send %@",_data2send);
return YES;
}
- (void)Send20{
NSLog(@"Send20");
if(_data2send){
NSLog(@"Send20:%d",_data2send.length);
if(_data2send.length>=20){
NSData *send = [[NSData alloc] initWithBytes:_data2send.bytes length:20];
[_device writeValue:send forCharacteristic:_writer type:CBCharacteristicWriteWithResponse];
int leftlen = _data2send.length - 20;
if(leftlen>0){
Byte left[leftlen];
memcpy(left,_data2send.bytes+20,leftlen);
_data2send = [[NSData alloc] initWithBytes:left length:leftlen];
}
else{
_data2send = nil;
_sending = NO;
self.configButton.enabled = YES;
self.configButton.backgroundColor = [UI colorWithHex:0x378fe8];
self.hitLabel.text = @"通信结束";
}
}
else{
_data2send = nil;
_sending = NO;
self.configButton.enabled = YES;
self.configButton.backgroundColor = [UI colorWithHex:0x378fe8];
self.hitLabel.text = @"通信失败";
}
}
else{
_sending = NO;
self.configButton.enabled = YES;
self.configButton.backgroundColor = [UI colorWithHex:0x378fe8];
self.hitLabel.text = @"通信结束";
}
}
- (void)ConfigEqu_go{
if(![self SetUpData])return;
self.hitLabel.text = @"正在配置";
self.configButton.enabled = NO;
self.configButton.backgroundColor = [UI colorWithHex:0x999999];
_sending = YES;
[self Send20];
}
#pragma mark Property Accessors
- (UIView *)rview
{
if (!_rview) {
_rview = [[UIView alloc] init];
//_rview.backgroundColor = [UIColor redColor];
}
return _rview;
}
- (UILabel *)hitLabel
{
if (!_hitLabel) {
_hitLabel = [[UILabel alloc] init];
//_WIFILabel.backgroundColor = [UIColor orangeColor];
//_WIFILabel.numberOfLines = 0;
_hitLabel.font = [UIFont systemFontOfSize:15.0f];
_hitLabel.textColor = [UIColor grayColor];
_hitLabel.textAlignment = NSTextAlignmentCenter;
_hitLabel.text = NSLocalizedString(@"", nil);
}
return _hitLabel;
}
- (UILabel *)WIFILabel
{
if (!_WIFILabel) {
_WIFILabel = [[UILabel alloc] init];
//_WIFILabel.backgroundColor = [UIColor orangeColor];
//_WIFILabel.numberOfLines = 0;
_WIFILabel.font = [UIFont systemFontOfSize:15.0f];
_WIFILabel.textColor = [UIColor blackColor];
_WIFILabel.textAlignment = NSTextAlignmentLeft;
_WIFILabel.text = NSLocalizedString(@"WIFI名称", nil);
}
return _WIFILabel;
}
- (UILabel *)PwdLabel
{
if (!_PwdLabel) {
_PwdLabel = [[UILabel alloc] init];
//_PwdLabel.backgroundColor = [UIColor orangeColor];
//_PwdLabel.numberOfLines = 0;
_PwdLabel.font = [UIFont systemFontOfSize:15.0f];
_PwdLabel.textColor = [UIColor blackColor];
_PwdLabel.textAlignment = NSTextAlignmentLeft;
_PwdLabel.text = NSLocalizedString(@"WIFI密码", nil);
}
return _PwdLabel;
}
- (HKHTextField *)WIFITextField
{
if (!_WIFITextField) {
_WIFITextField = [[HKHTextField alloc] init];
_WIFITextField.borderStyle = UITextBorderStyleRoundedRect;
_WIFITextField.placeholder = @"门锁连接WIFI的名称";
}
return _WIFITextField;
}
- (HKHTextField *)PwdTextField
{
if (!_PwdTextField) {
_PwdTextField = [[HKHTextField alloc] init];
_PwdTextField.borderStyle = UITextBorderStyleRoundedRect;
_PwdTextField.placeholder = @"门锁连接WIFI的密码";
_PwdTextField.secureTextEntry = YES;
}
return _PwdTextField;
}
- (UIButton *)searchWIFIButton
{
if (!_searchWIFIButton) {
_searchWIFIButton = [[UIButton alloc] init];
[_searchWIFIButton setTitle:@"搜索WIFI" forState:UIControlStateNormal];
[_searchWIFIButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_searchWIFIButton setTitleColor:[UIColor colorWithRed:255/255. green:128/255. blue:0/255. alpha:1] forState:UIControlStateHighlighted];
_searchWIFIButton.backgroundColor = [UI colorWithHex:0x378fe8];
[_searchWIFIButton.layer setMasksToBounds:YES];
[_searchWIFIButton.layer setCornerRadius:10.0]; //
_searchWIFIButton.font = [UIFont systemFontOfSize:14.0f];
//
//[_searchWIFIButton.layer setBorderWidth:1.0];
//_searchWIFIButton.layer.borderColor=[UIColor grayColor].CGColor;
}
return _searchWIFIButton;
}
- (UIButton *)configButton
{
if (!_configButton) {
_configButton = [[UIButton alloc] init];
[_configButton setTitle:@"开始配置" forState:UIControlStateNormal];
[_configButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_configButton setTitleColor:[UIColor colorWithRed:255/255. green:128/255. blue:0/255. alpha:1] forState:UIControlStateHighlighted];
_configButton.backgroundColor = [UI colorWithHex:0x378fe8];
[_configButton.layer setMasksToBounds:YES];
[_configButton.layer setCornerRadius:10.0]; //
//
//[_configButton.layer setBorderWidth:1.0];
//_configButton.layer.borderColor=[UIColor grayColor].CGColor;
}
return _configButton;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end