RDSBindDeviceVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // RDSBindDeviceVC.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/11/21.
  6. //
  7. #import "RDSBindDeviceVC.h"
  8. #import "RDSBabyBluetooth.h"
  9. #import <WebKit/WebKit.h>
  10. #import <SDWebImage/SDWebImage.h>
  11. #import "RDSMapHelper.h"
  12. #import "GCDTimer.h"// 轮询定时器
  13. #import "RDSRootControl.h"
  14. @interface RDSBindDeviceVC ()
  15. @property (weak, nonatomic) IBOutlet UILabel *tipsLab;
  16. @property (weak, nonatomic) IBOutlet UILabel *countDownLab;
  17. @property (strong, nonatomic) IBOutlet UIImageView *ringImgView;
  18. @property (weak, nonatomic) IBOutlet UILabel *stepLabel;
  19. @property (nonatomic, strong) CADisplayLink *displayLink;
  20. @property (nonatomic, strong) CADisplayLink *displayLinkCountDown;//倒计时
  21. @property (nonatomic, assign) int countDown;
  22. @property (nonatomic, strong) RDSBabyBluetooth *baby;
  23. @property (nonatomic, strong) GCDTimer *gcdTimer;
  24. @property (nonatomic, strong) dispatch_source_t textTimer;
  25. @property (nonatomic, strong) dispatch_source_t fkTimer;//分控
  26. @property (nonatomic, strong) dispatch_source_t yxjlTimer;//永续精灵
  27. @property (nonatomic, strong) dispatch_source_t afterTimer;//延时
  28. @property (nonatomic, assign) BOOL isFirstBind;
  29. @property (nonatomic, assign) BOOL firstMark;
  30. @property (nonatomic, assign) BOOL secondMark;
  31. @property (nonatomic, assign) BOOL yxjlStep5Mark;
  32. @end
  33. @implementation RDSBindDeviceVC
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. self.view.backgroundColor = [UIColor colorWithHexString:@"#FEFEFE"];
  37. _isFirstBind = YES;
  38. _firstMark = YES;
  39. _secondMark = YES;
  40. // _yxjlStep5Mark = YES;
  41. dispatch_async(dispatch_get_main_queue(), ^{
  42. if ([self.bluetooth boolValue]) {
  43. //主控 、永续精灵
  44. NSString *string = @"大概需要20S,请耐心等待";
  45. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
  46. [attributedString addAttribute:NSForegroundColorAttributeName
  47. value:[UIColor colorWithHexString:@"#267AFF"]
  48. range:NSMakeRange(4, 3)];
  49. self.countDownLab.attributedText = attributedString;
  50. self.tipsLab.text = @"正在搜索设备";
  51. [self playeGifWithName:@"gif_state_2"];
  52. [self startTimer];
  53. }
  54. });
  55. if ([self.bluetooth boolValue]) {
  56. RDSBabyBluetooth *baby = TheBabyBluetooth;
  57. self.baby = baby;
  58. // [RDSHudShower showBottomToast:[NSString stringWithFormat:@"blueCode:%@",self.blueCode]];
  59. [TheBabyBluetooth rds_refreshScanDeviceName:self.blueCode];
  60. RDS_WEAKSELF(weakSelf)
  61. [TheBabyBluetooth rds_cmdResponceObserve:[self class] responce:^(RDSDeviceReply *reply) {
  62. [weakSelf p_receiveCmd:reply];
  63. }];
  64. TheBabyBluetooth.onConnectDeviceSuccessAndCanWrite = ^{
  65. [weakSelf p_sendWifiToDecive];
  66. };
  67. }else{
  68. [self timerAnimation];
  69. }
  70. }
  71. - (void)viewDidDisappear:(BOOL)animated{
  72. [super viewDidDisappear:animated];
  73. [_gcdTimer pauseTimer];// 暂停轮询
  74. if (_textTimer != nil) {
  75. dispatch_source_cancel(_textTimer);
  76. _textTimer = nil;
  77. }
  78. if (_fkTimer != nil) {
  79. dispatch_source_cancel(_fkTimer);
  80. _fkTimer = nil;
  81. }
  82. if (_yxjlTimer != nil) {
  83. dispatch_source_cancel(_yxjlTimer);
  84. _yxjlTimer = nil;
  85. }
  86. if (_afterTimer != nil) {
  87. dispatch_source_cancel(_afterTimer);
  88. _afterTimer = nil;
  89. }
  90. [TheBabyBluetooth rds_cancelScan];
  91. [TheBabyBluetooth rds_cancelConnection];
  92. [kNotificationCenter removeObserver:self];
  93. }
  94. #pragma - mark 分控倒计时动画
  95. -(void)timerAnimation
  96. {
  97. __block int timeout = 4; //倒计时时间
  98. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  99. _fkTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  100. dispatch_source_set_timer(_fkTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  101. dispatch_source_set_event_handler(_fkTimer, ^{
  102. if(timeout == 4){
  103. dispatch_async(dispatch_get_main_queue(), ^{
  104. self.tipsLab.text = @"正在搜索设备";
  105. [self playeGifWithName:@"gif_state_2"];
  106. [self startTimer];
  107. });
  108. }
  109. if(timeout == 2){
  110. dispatch_async(dispatch_get_main_queue(), ^{
  111. self.tipsLab.text = @"正在为设备配网";
  112. [self playeGifWithName:@"gif_state_3"];
  113. [self startTimer];
  114. });
  115. }
  116. if (timeout == 0){
  117. //添加设备
  118. [self bindDevices];
  119. dispatch_source_cancel(self->_fkTimer);
  120. }
  121. timeout--;
  122. NSLog(@"---timeout---%d",timeout);
  123. });
  124. dispatch_resume(_fkTimer);
  125. }
  126. -(void)startTimer
  127. {
  128. RDS_WEAKSELF(weakSelf)
  129. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  130. _textTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  131. __block int timeout = 20; //倒计时时间
  132. dispatch_source_set_timer(_textTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  133. dispatch_source_set_event_handler(_textTimer, ^{
  134. if(timeout == 0) {
  135. dispatch_source_cancel(self->_textTimer);
  136. //倒计时结束
  137. dispatch_async(dispatch_get_main_queue(), ^{
  138. [RDSHudShower showCenterToast:@"配网超时!"];
  139. [weakSelf.navigationController popViewControllerAnimated:YES];
  140. });
  141. } else {
  142. NSString *strTime = [NSString stringWithFormat:@"%dS",timeout];
  143. dispatch_async(dispatch_get_main_queue(), ^{
  144. //设置界面的按钮显示 根据自己需求设置
  145. NSString *string = @"大概需要";
  146. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
  147. [attributedString addAttribute:NSForegroundColorAttributeName
  148. value:[UIColor colorWithHexString:@"#333333"]
  149. range:NSMakeRange(0, string.length)];
  150. NSMutableAttributedString *timeAttributedString = [[NSMutableAttributedString alloc] initWithString:strTime];
  151. [timeAttributedString addAttribute:NSForegroundColorAttributeName
  152. value:[UIColor colorWithHexString:@"#267AFF"]
  153. range:NSMakeRange(0, strTime.length)];
  154. NSString *lastString = @",请耐心等待";
  155. NSMutableAttributedString *lastAttributedString = [[NSMutableAttributedString alloc] initWithString:lastString];
  156. [lastAttributedString addAttribute:NSForegroundColorAttributeName
  157. value:[UIColor colorWithHexString:@"#333333"]
  158. range:NSMakeRange(0, lastString.length)];
  159. [attributedString appendAttributedString:timeAttributedString];
  160. [attributedString appendAttributedString:lastAttributedString];
  161. weakSelf.countDownLab.attributedText = attributedString;
  162. });
  163. timeout--;
  164. }
  165. });
  166. dispatch_resume(_textTimer);
  167. }
  168. #pragma mark - 设备信息
  169. - (void)p_receiveCmd:(RDSDeviceReply *)reply{
  170. RDS_WEAKSELF(weakSelf)
  171. NSString *msg = [NSString stringWithFormat:@"Step %ld ---- %d", reply.bindStep, reply.code];
  172. // [RDSHudShower showCenterToast:msg];
  173. // dispatch_async(dispatch_get_main_queue(), ^{
  174. // self.stepLabel.text = msg;
  175. // });
  176. if (reply.code != 0){
  177. [RDSHudShower showCenterToast:@"配网失败!!"];
  178. dispatch_async(dispatch_get_main_queue(), ^{
  179. [self.navigationController popViewControllerAnimated:YES];
  180. });
  181. return;
  182. }
  183. if (self.isYXJL == NO) {
  184. //主控
  185. if ((reply.bindStep == 1 || reply.bindStep == 2 ) && reply.code == 0){
  186. dispatch_async(dispatch_get_main_queue(), ^{
  187. weakSelf.tipsLab.text = @"正在监测网络状态";
  188. [weakSelf playeGifWithName:@"gif_state_1"];
  189. if (self->_firstMark) {
  190. self->_firstMark = NO;
  191. [weakSelf startTimer];
  192. }
  193. });
  194. }
  195. if ((reply.bindStep == 3 || reply.bindStep == 4 || reply.bindStep == 5) && reply.code == 0 && self.isYXJL == NO) {
  196. dispatch_async(dispatch_get_main_queue(), ^{
  197. weakSelf.tipsLab.text = @"正在为设备配网";
  198. [weakSelf playeGifWithName:@"gif_state_3"];
  199. if (self->_secondMark) {
  200. self->_secondMark = NO;
  201. [weakSelf startTimer];
  202. }
  203. //设备id type_id string 设备编号设备类型id
  204. //device_code 设备编号
  205. //is_master 是否是主控
  206. //home_id : 家庭id
  207. // room_id: 房间id
  208. NSLog(@"---reply.deviceCode---%@",reply.deviceCode);
  209. NSLog(@"---deviceCode---%@",self.deviceCode);
  210. });
  211. if (reply.bindStep == 5 ) {
  212. if (self->_isFirstBind == YES) {
  213. self->_isFirstBind = NO;
  214. NSLog(@"_isFirstBind------%d",self->_isFirstBind);
  215. [self afterBindDevice];
  216. // [self performSelector:@selector(bindDevices) withObject:nil afterDelay:2.0];
  217. }
  218. }
  219. }
  220. }else{
  221. //永续精灵
  222. if (reply.bindStep == -1 && self.isYXJL == YES && reply.code == 0) {
  223. [RDSHudShower showCenterToast:@"配网失败!!!"];
  224. dispatch_async(dispatch_get_main_queue(), ^{
  225. [self.navigationController popViewControllerAnimated:YES];
  226. });
  227. return;
  228. }
  229. if ((reply.bindStep == 1 || reply.bindStep == 5 ) && reply.code == 0){
  230. dispatch_async(dispatch_get_main_queue(), ^{
  231. weakSelf.deviceCode = reply.deviceCode;
  232. if (reply.bindStep == 1 ) {
  233. [weakSelf YXJL_timerAnimation];
  234. }
  235. if (reply.bindStep == 5 ) {
  236. // self->_yxjlStep5Mark = NO;
  237. // [self->_gcdTimer stopTimer];
  238. // [weakSelf startTimer];
  239. [weakSelf afterBindDevice];
  240. }
  241. });
  242. }
  243. }
  244. }
  245. #pragma - mark 永续精灵 倒计时动画
  246. -(void)YXJL_timerAnimation
  247. {
  248. __block int timeout = 4; //倒计时时间
  249. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  250. _yxjlTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  251. dispatch_source_set_timer(_yxjlTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  252. dispatch_source_set_event_handler(_yxjlTimer, ^{
  253. // if(timeout == 6){
  254. // dispatch_async(dispatch_get_main_queue(), ^{
  255. // self.tipsLab.text = @"正在搜索设备";
  256. // [self playeGifWithName:@"gif_state_2"];
  257. // [self startTimer];
  258. // });
  259. // }
  260. if(timeout == 4){
  261. dispatch_async(dispatch_get_main_queue(), ^{
  262. self.tipsLab.text = @"正在监测网络状态";
  263. [self playeGifWithName:@"gif_state_1"];
  264. [self startTimer];
  265. });
  266. }
  267. if(timeout == 2){
  268. dispatch_async(dispatch_get_main_queue(), ^{
  269. self.tipsLab.text = @"正在为设备配网";
  270. [self playeGifWithName:@"gif_state_3"];
  271. [self startTimer];
  272. });
  273. }
  274. if (timeout == 0){
  275. dispatch_source_cancel(self->_yxjlTimer);
  276. // if (self->_yxjlStep5Mark) {
  277. // [self p_setUpTimer];
  278. // }
  279. }
  280. timeout--;
  281. NSLog(@"---timeout---%d",timeout);
  282. });
  283. dispatch_resume(_yxjlTimer);
  284. }
  285. #pragma - mark 精灵设备状态 3秒一次
  286. - (void)p_setUpTimer{
  287. self.gcdTimer = [[GCDTimer alloc] init];
  288. RDS_WEAKSELF(weakSelf)
  289. [_gcdTimer scheduledTimerWithTimeInterval:3 afterTime:0 repeats:YES block:^{
  290. [weakSelf getDeviceOnlineWithDeivceId:self.deviceCode];
  291. }];
  292. }
  293. -(void)getDeviceOnlineWithDeivceId:(NSString *)deivceId
  294. {
  295. RDS_WEAKSELF(weakSelf)
  296. [RDSDemoApiHelper rds_getDeviceOnlineWithId:deivceId Success:^(id responseObject) {
  297. if ([responseObject[@"code"] intValue] == 9999) {
  298. [RDSRootControl shareControl].isLoginSuccess = NO;
  299. TheDataManager.token = @"";
  300. }
  301. if ([responseObject[@"code"] intValue] == 0) {
  302. NSDictionary *dataDic = responseObject[@"data"];
  303. NSNumber *status = dataDic[@"status"];
  304. if ([status integerValue] == 1) {
  305. [self->_gcdTimer stopTimer];
  306. [weakSelf bindDevices];
  307. }
  308. }
  309. } failure:^(NSError *error) {
  310. }];
  311. }
  312. -(void)afterBindDevice
  313. {
  314. __block int timeout = 2; //倒计时时间
  315. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  316. _afterTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  317. dispatch_source_set_timer(_afterTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  318. dispatch_source_set_event_handler(_afterTimer, ^{
  319. if (timeout == 0){
  320. [self bindDevices];
  321. dispatch_source_cancel(self->_afterTimer);
  322. }
  323. timeout--;
  324. });
  325. dispatch_resume(_afterTimer);
  326. }
  327. -(void)bindDevices
  328. {
  329. DDLog(@"开始添加设备");
  330. RDS_WEAKSELF(weakSelf)
  331. [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) {
  332. if ([responseObject[@"code"] intValue] == 9999) {
  333. [RDSRootControl shareControl].isLoginSuccess = NO;
  334. TheDataManager.token = @"";
  335. }
  336. DDLog(@"rds_AddDevicesWithName请求结果:%@",responseObject);
  337. if ([responseObject[@"code"] intValue] != 0) {
  338. [RDSHudShower showCenterToast:responseObject[@"message"]];
  339. [weakSelf.navigationController popViewControllerAnimated:YES];
  340. }else{
  341. [RDSHudShower showCenterToast:@"绑定成功"];
  342. [weakSelf.navigationController popToRootViewControllerAnimated:YES];
  343. }
  344. } failure:^(NSError *error) {
  345. [RDSHudShower showBottomToast:@"连接服务器失败"];
  346. [weakSelf.navigationController popViewControllerAnimated:YES];
  347. }];
  348. }
  349. - (void)p_sendWifiToDecive{
  350. NSLog(@"---wifiName==%@---pwd==%@",_wifiName,_pwd);
  351. // [RDSHudShower showBottomToast:_wifiName];
  352. NSDictionary *param = @{
  353. @"device_type_id":self.type_id,
  354. @"ssid" : _wifiName.length == 0 ? @"" : _wifiName,
  355. @"pwd" : _pwd.length == 0 ? @"" :_pwd,
  356. @"user_id" : TheDataManager.currentUser.record_id,
  357. @"home_id" : TheDataManager.current_home_id,
  358. @"room_name" : @"aaa",
  359. @"env" :@0
  360. };
  361. NSLog(@"---home_id==%@---user_id==%@",self.home_id,self.room_id);
  362. DDLog(@"发送WiFi账号密码:%@",param);
  363. NSError *parseError = nil;
  364. // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingSortedKeys error:&parseError];
  365. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:0 error:&parseError];
  366. if (jsonData) {
  367. [TheBabyBluetooth rds_writeValue:jsonData];
  368. }else{
  369. [RDSHudShower showBottomToast:@"蓝牙写入失败"];
  370. }
  371. }
  372. #pragma mark - GIF
  373. -(void)playeGifWithName:(NSString *)name
  374. {
  375. NSData *gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"gif"]];
  376. self.ringImgView.image = [UIImage sd_imageWithGIFData:gifData];
  377. }
  378. @end