123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- //
- // RDSBindDeviceVC.m
- // Temperature
- //
- // Created by RD on 2022/11/21.
- //
- #import "RDSBindDeviceVC.h"
- #import "RDSBabyBluetooth.h"
- #import <WebKit/WebKit.h>
- #import <SDWebImage/SDWebImage.h>
- #import "RDSMapHelper.h"
- #import "GCDTimer.h"// 轮询定时器
- #import "RDSRootControl.h"
- @interface RDSBindDeviceVC ()
- @property (weak, nonatomic) IBOutlet UILabel *tipsLab;
- @property (weak, nonatomic) IBOutlet UILabel *countDownLab;
- @property (strong, nonatomic) IBOutlet UIImageView *ringImgView;
- @property (weak, nonatomic) IBOutlet UILabel *stepLabel;
- @property (nonatomic, strong) CADisplayLink *displayLink;
- @property (nonatomic, strong) CADisplayLink *displayLinkCountDown;//倒计时
- @property (nonatomic, assign) int countDown;
- @property (nonatomic, strong) RDSBabyBluetooth *baby;
- @property (nonatomic, strong) GCDTimer *gcdTimer;
- @property (nonatomic, strong) dispatch_source_t textTimer;
- @property (nonatomic, strong) dispatch_source_t fkTimer;//分控
- @property (nonatomic, strong) dispatch_source_t yxjlTimer;//永续精灵
- @property (nonatomic, strong) dispatch_source_t afterTimer;//延时
- @property (nonatomic, assign) BOOL isFirstBind;
- @property (nonatomic, assign) BOOL firstMark;
- @property (nonatomic, assign) BOOL secondMark;
- @property (nonatomic, assign) BOOL yxjlStep5Mark;
- @end
- @implementation RDSBindDeviceVC
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor colorWithHexString:@"#FEFEFE"];
- _isFirstBind = YES;
- _firstMark = YES;
- _secondMark = YES;
- // _yxjlStep5Mark = YES;
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([self.bluetooth boolValue]) {
- //主控 、永续精灵
- NSString *string = @"大概需要20S,请耐心等待";
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
- [attributedString addAttribute:NSForegroundColorAttributeName
- value:[UIColor colorWithHexString:@"#267AFF"]
- range:NSMakeRange(4, 3)];
- self.countDownLab.attributedText = attributedString;
-
- self.tipsLab.text = @"正在搜索设备";
- [self playeGifWithName:@"gif_state_2"];
- [self startTimer];
- }
- });
-
- if ([self.bluetooth boolValue]) {
- RDSBabyBluetooth *baby = TheBabyBluetooth;
- self.baby = baby;
- // [RDSHudShower showBottomToast:[NSString stringWithFormat:@"blueCode:%@",self.blueCode]];
- [TheBabyBluetooth rds_refreshScanDeviceName:self.blueCode];
- RDS_WEAKSELF(weakSelf)
- [TheBabyBluetooth rds_cmdResponceObserve:[self class] responce:^(RDSDeviceReply *reply) {
- [weakSelf p_receiveCmd:reply];
- }];
-
- TheBabyBluetooth.onConnectDeviceSuccessAndCanWrite = ^{
- [weakSelf p_sendWifiToDecive];
- };
- }else{
- [self timerAnimation];
- }
- }
- - (void)viewDidDisappear:(BOOL)animated{
- [super viewDidDisappear:animated];
-
- [_gcdTimer pauseTimer];// 暂停轮询
- if (_textTimer != nil) {
- dispatch_source_cancel(_textTimer);
- _textTimer = nil;
- }
-
- if (_fkTimer != nil) {
- dispatch_source_cancel(_fkTimer);
- _fkTimer = nil;
- }
-
- if (_yxjlTimer != nil) {
- dispatch_source_cancel(_yxjlTimer);
- _yxjlTimer = nil;
- }
-
- if (_afterTimer != nil) {
- dispatch_source_cancel(_afterTimer);
- _afterTimer = nil;
- }
-
- [TheBabyBluetooth rds_cancelScan];
- [TheBabyBluetooth rds_cancelConnection];
-
- [kNotificationCenter removeObserver:self];
- }
- #pragma - mark 分控倒计时动画
- -(void)timerAnimation
- {
- __block int timeout = 4; //倒计时时间
- dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- _fkTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
- dispatch_source_set_timer(_fkTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
- dispatch_source_set_event_handler(_fkTimer, ^{
- if(timeout == 4){
- dispatch_async(dispatch_get_main_queue(), ^{
- self.tipsLab.text = @"正在搜索设备";
- [self playeGifWithName:@"gif_state_2"];
- [self startTimer];
- });
- }
- if(timeout == 2){
- dispatch_async(dispatch_get_main_queue(), ^{
- self.tipsLab.text = @"正在为设备配网";
- [self playeGifWithName:@"gif_state_3"];
- [self startTimer];
- });
- }
- if (timeout == 0){
- //添加设备
- [self bindDevices];
- dispatch_source_cancel(self->_fkTimer);
- }
-
- timeout--;
- NSLog(@"---timeout---%d",timeout);
- });
- dispatch_resume(_fkTimer);
- }
- -(void)startTimer
- {
- RDS_WEAKSELF(weakSelf)
- dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- _textTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
- __block int timeout = 20; //倒计时时间
- dispatch_source_set_timer(_textTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
- dispatch_source_set_event_handler(_textTimer, ^{
-
- if(timeout == 0) {
- dispatch_source_cancel(self->_textTimer);
- //倒计时结束
- dispatch_async(dispatch_get_main_queue(), ^{
- [RDSHudShower showCenterToast:@"配网超时!"];
- [weakSelf.navigationController popViewControllerAnimated:YES];
- });
- } else {
- NSString *strTime = [NSString stringWithFormat:@"%dS",timeout];
- dispatch_async(dispatch_get_main_queue(), ^{
- //设置界面的按钮显示 根据自己需求设置
- NSString *string = @"大概需要";
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
- [attributedString addAttribute:NSForegroundColorAttributeName
- value:[UIColor colorWithHexString:@"#333333"]
- range:NSMakeRange(0, string.length)];
-
- NSMutableAttributedString *timeAttributedString = [[NSMutableAttributedString alloc] initWithString:strTime];
- [timeAttributedString addAttribute:NSForegroundColorAttributeName
- value:[UIColor colorWithHexString:@"#267AFF"]
- range:NSMakeRange(0, strTime.length)];
-
- NSString *lastString = @",请耐心等待";
- NSMutableAttributedString *lastAttributedString = [[NSMutableAttributedString alloc] initWithString:lastString];
- [lastAttributedString addAttribute:NSForegroundColorAttributeName
- value:[UIColor colorWithHexString:@"#333333"]
- range:NSMakeRange(0, lastString.length)];
-
- [attributedString appendAttributedString:timeAttributedString];
- [attributedString appendAttributedString:lastAttributedString];
- weakSelf.countDownLab.attributedText = attributedString;
- });
- timeout--;
- }
- });
- dispatch_resume(_textTimer);
- }
- #pragma mark - 设备信息
- - (void)p_receiveCmd:(RDSDeviceReply *)reply{
- RDS_WEAKSELF(weakSelf)
- NSString *msg = [NSString stringWithFormat:@"Step %ld ---- %d", reply.bindStep, reply.code];
- // [RDSHudShower showCenterToast:msg];
-
- // dispatch_async(dispatch_get_main_queue(), ^{
- // self.stepLabel.text = msg;
- // });
- if (reply.code != 0){
- [RDSHudShower showCenterToast:@"配网失败!!"];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.navigationController popViewControllerAnimated:YES];
- });
-
- return;
- }
-
- if (self.isYXJL == NO) {
- //主控
- if ((reply.bindStep == 1 || reply.bindStep == 2 ) && reply.code == 0){
- dispatch_async(dispatch_get_main_queue(), ^{
- weakSelf.tipsLab.text = @"正在监测网络状态";
- [weakSelf playeGifWithName:@"gif_state_1"];
- if (self->_firstMark) {
- self->_firstMark = NO;
- [weakSelf startTimer];
- }
-
- });
- }
-
- if ((reply.bindStep == 3 || reply.bindStep == 4 || reply.bindStep == 5) && reply.code == 0 && self.isYXJL == NO) {
- dispatch_async(dispatch_get_main_queue(), ^{
- weakSelf.tipsLab.text = @"正在为设备配网";
- [weakSelf playeGifWithName:@"gif_state_3"];
- if (self->_secondMark) {
- self->_secondMark = NO;
- [weakSelf startTimer];
- }
-
- //设备id type_id string 设备编号设备类型id
- //device_code 设备编号
- //is_master 是否是主控
- //home_id : 家庭id
- // room_id: 房间id
- NSLog(@"---reply.deviceCode---%@",reply.deviceCode);
- NSLog(@"---deviceCode---%@",self.deviceCode);
- });
-
- if (reply.bindStep == 5 ) {
-
- if (self->_isFirstBind == YES) {
- self->_isFirstBind = NO;
- NSLog(@"_isFirstBind------%d",self->_isFirstBind);
- [self afterBindDevice];
- // [self performSelector:@selector(bindDevices) withObject:nil afterDelay:2.0];
- }
- }
- }
- }else{
- //永续精灵
- if (reply.bindStep == -1 && self.isYXJL == YES && reply.code == 0) {
- [RDSHudShower showCenterToast:@"配网失败!!!"];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.navigationController popViewControllerAnimated:YES];
- });
- return;
- }
-
- if ((reply.bindStep == 1 || reply.bindStep == 5 ) && reply.code == 0){
- dispatch_async(dispatch_get_main_queue(), ^{
- weakSelf.deviceCode = reply.deviceCode;
-
- if (reply.bindStep == 1 ) {
- [weakSelf YXJL_timerAnimation];
- }
-
- if (reply.bindStep == 5 ) {
- // self->_yxjlStep5Mark = NO;
- // [self->_gcdTimer stopTimer];
- // [weakSelf startTimer];
- [weakSelf afterBindDevice];
- }
-
- });
- }
- }
- }
- #pragma - mark 永续精灵 倒计时动画
- -(void)YXJL_timerAnimation
- {
- __block int timeout = 4; //倒计时时间
- dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- _yxjlTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
- dispatch_source_set_timer(_yxjlTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
- dispatch_source_set_event_handler(_yxjlTimer, ^{
- // if(timeout == 6){
- // dispatch_async(dispatch_get_main_queue(), ^{
- // self.tipsLab.text = @"正在搜索设备";
- // [self playeGifWithName:@"gif_state_2"];
- // [self startTimer];
- // });
- // }
- if(timeout == 4){
- dispatch_async(dispatch_get_main_queue(), ^{
- self.tipsLab.text = @"正在监测网络状态";
- [self playeGifWithName:@"gif_state_1"];
- [self startTimer];
- });
- }
- if(timeout == 2){
- dispatch_async(dispatch_get_main_queue(), ^{
- self.tipsLab.text = @"正在为设备配网";
- [self playeGifWithName:@"gif_state_3"];
- [self startTimer];
- });
- }
- if (timeout == 0){
- dispatch_source_cancel(self->_yxjlTimer);
- // if (self->_yxjlStep5Mark) {
- // [self p_setUpTimer];
- // }
- }
-
- timeout--;
- NSLog(@"---timeout---%d",timeout);
- });
- dispatch_resume(_yxjlTimer);
- }
- #pragma - mark 精灵设备状态 3秒一次
- - (void)p_setUpTimer{
- self.gcdTimer = [[GCDTimer alloc] init];
- RDS_WEAKSELF(weakSelf)
- [_gcdTimer scheduledTimerWithTimeInterval:3 afterTime:0 repeats:YES block:^{
- [weakSelf getDeviceOnlineWithDeivceId:self.deviceCode];
- }];
- }
- -(void)getDeviceOnlineWithDeivceId:(NSString *)deivceId
- {
- RDS_WEAKSELF(weakSelf)
- [RDSDemoApiHelper rds_getDeviceOnlineWithId:deivceId Success:^(id responseObject) {
- if ([responseObject[@"code"] intValue] == 9999) {
- [RDSRootControl shareControl].isLoginSuccess = NO;
- TheDataManager.token = @"";
- }
-
- if ([responseObject[@"code"] intValue] == 0) {
- NSDictionary *dataDic = responseObject[@"data"];
- NSNumber *status = dataDic[@"status"];
- if ([status integerValue] == 1) {
- [self->_gcdTimer stopTimer];
-
- [weakSelf bindDevices];
- }
- }
- } failure:^(NSError *error) {
-
- }];
- }
- -(void)afterBindDevice
- {
- __block int timeout = 2; //倒计时时间
- dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- _afterTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
- dispatch_source_set_timer(_afterTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
- dispatch_source_set_event_handler(_afterTimer, ^{
- if (timeout == 0){
- [self bindDevices];
- dispatch_source_cancel(self->_afterTimer);
- }
-
- timeout--;
- });
- dispatch_resume(_afterTimer);
- }
- -(void)bindDevices
- {
- DDLog(@"开始添加设备");
- RDS_WEAKSELF(weakSelf)
- [RDSDemoApiHelper rds_AddDevicesWithName:self.deviceName deviceCode:self.deviceCode subCode:self.subCode typeId:self.type_id isMaster:self.is_master homeId:self.home_id roomId:self.room_id success:^(id responseObject) {
- if ([responseObject[@"code"] intValue] == 9999) {
- [RDSRootControl shareControl].isLoginSuccess = NO;
- TheDataManager.token = @"";
- }
- DDLog(@"rds_AddDevicesWithName请求结果:%@",responseObject);
-
- if ([responseObject[@"code"] intValue] != 0) {
- [RDSHudShower showCenterToast:responseObject[@"message"]];
- [weakSelf.navigationController popViewControllerAnimated:YES];
- }else{
- [RDSHudShower showCenterToast:@"绑定成功"];
- [weakSelf.navigationController popToRootViewControllerAnimated:YES];
- }
- } failure:^(NSError *error) {
- [RDSHudShower showBottomToast:@"连接服务器失败"];
- [weakSelf.navigationController popViewControllerAnimated:YES];
- }];
- }
- - (void)p_sendWifiToDecive{
-
- NSLog(@"---wifiName==%@---pwd==%@",_wifiName,_pwd);
- // [RDSHudShower showBottomToast:_wifiName];
- NSDictionary *param = @{
- @"device_type_id":self.type_id,
- @"ssid" : _wifiName.length == 0 ? @"" : _wifiName,
- @"pwd" : _pwd.length == 0 ? @"" :_pwd,
- @"user_id" : TheDataManager.currentUser.record_id,
- @"home_id" : TheDataManager.current_home_id,
- @"room_name" : @"aaa",
- @"env" :@0
- };
- NSLog(@"---home_id==%@---user_id==%@",self.home_id,self.room_id);
-
- DDLog(@"发送WiFi账号密码:%@",param);
-
- NSError *parseError = nil;
- // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingSortedKeys error:&parseError];
-
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:0 error:&parseError];
- if (jsonData) {
- [TheBabyBluetooth rds_writeValue:jsonData];
- }else{
- [RDSHudShower showBottomToast:@"蓝牙写入失败"];
- }
- }
- #pragma mark - GIF
- -(void)playeGifWithName:(NSString *)name
- {
- NSData *gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"gif"]];
- self.ringImgView.image = [UIImage sd_imageWithGIFData:gifData];
- }
- @end
|