RDSBindDeviceVC.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. @interface RDSBindDeviceVC ()
  13. @property (weak, nonatomic) IBOutlet UIView *gifView;
  14. @property (weak, nonatomic) IBOutlet UIView *searchView;
  15. @property (weak, nonatomic) IBOutlet UIButton *finishBtn;
  16. @property (weak, nonatomic) IBOutlet UILabel *tipsLab;
  17. @property (weak, nonatomic) IBOutlet UILabel *countDownLab;
  18. @property (weak, nonatomic) IBOutlet UIImageView *ringImgView;
  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. @end
  24. @implementation RDSBindDeviceVC
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. // Do any additional setup after loading the view from its nib.
  28. self.title = @"绑定设备";
  29. [self startAnimation];
  30. RDSBabyBluetooth *baby = TheBabyBluetooth;
  31. self.baby = baby;
  32. [TheBabyBluetooth rds_refreshScanDeviceName:_deviceID];
  33. RDS_WEAKSELF(weakSelf)
  34. [TheBabyBluetooth rds_cmdResponceObserve:[self class] responce:^(RDSDeviceReply *reply) {
  35. [weakSelf p_receiveCmd:reply];
  36. }];
  37. TheBabyBluetooth.onConnectDeviceSuccessAndCanWrite = ^{
  38. [weakSelf p_sendWifiToDecive];
  39. };
  40. dispatch_async(dispatch_get_main_queue(), ^{
  41. [self performSelector:@selector(p_timeout) withObject:nil afterDelay:120];
  42. });
  43. }
  44. // 配对超时 延时任务
  45. - (void)p_timeout{
  46. self.finishBtn.hidden = NO;
  47. _searchView.hidden = YES;
  48. [self stopAnimation];
  49. [TheAlertCtrl rds_alertWithTitle:@"提示" message:@"绑定失败,配对超时。"];
  50. }
  51. // 取消执行延时任务
  52. - (void)p_cancelTimeout{
  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(p_timeout) object:nil];
  55. });
  56. }
  57. - (void)viewDidDisappear:(BOOL)animated{
  58. [super viewDidDisappear:animated];
  59. [self stopAnimation];
  60. [self p_cancelTimeout];
  61. }
  62. - (IBAction)onSendCmdClick {
  63. [self p_sendWifiToDecive];
  64. }
  65. - (IBAction)onFinishClick:(UIButton *)sender {
  66. [self.navigationController popToRootViewControllerAnimated:YES];
  67. }
  68. #pragma mark - 设备信息
  69. - (void)p_receiveCmd:(RDSDeviceReply *)reply{
  70. NSString *msg = [NSString stringWithFormat:@"Step %ld ---- %d", reply.bindStep, reply.code];
  71. [RDSHudShower showCenterToast:msg];
  72. dispatch_async(dispatch_get_main_queue(), ^{
  73. self.tipsLab.text = @"正在配网";
  74. });
  75. if (reply.bindStep == Step5 && reply.code == 0) {
  76. RDS_WEAKSELF(weakSelf)
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. self.finishBtn.hidden = NO;
  79. self.tipsLab.text = @"配网完成";
  80. [weakSelf p_cancelTimeout];
  81. [weakSelf p_setupGif];
  82. [TheMapHelper rds_startLocationSevice:^(RDSUserLocation *userLocation) {
  83. [weakSelf p_updateLocation];
  84. }];
  85. });
  86. }
  87. }
  88. - (void)p_updateLocation{
  89. RDS_WEAKSELF(weakSelf)
  90. [RDSDemoApiHelper rds_updateHomeInfoWithHomeId:TheDataManager.current_home_id name:TheDataManager.current_home_name city:TheMapHelper.userLocation.cityString success:^(id responseObject) {
  91. if ([responseObject[@"code"] intValue] == 0) {
  92. DDLog(@"更新城市成功");
  93. } else{
  94. DDLog(@"更新城市失败:%@",responseObject[@"message"]);
  95. }
  96. } failure:^(NSError *error) {
  97. }];
  98. }
  99. - (void)p_sendWifiToDecive{
  100. NSDictionary *param = @{
  101. @"ssid" : _wifiName,
  102. @"pwd" : _pwd,
  103. @"user_id" : TheDataManager.currentUser.record_id,
  104. @"home_id" : TheDataManager.current_home_id,
  105. @"room_name" : _roomName,
  106. @"env" : @(TheApiHelper.isTest),
  107. };
  108. DDLog(@"发送WiFi账号密码:%@",param);
  109. NSError *parseError = nil;
  110. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingSortedKeys error:&parseError];
  111. [TheBabyBluetooth rds_writeValue:jsonData];
  112. }
  113. #pragma mark - 爱的魔力转圈圈
  114. - (void)startAnimation{
  115. // 爱的魔力转圈圈
  116. self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(d_displayLinkAnimation)];
  117. [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  118. // 每秒调用次数 = 60/frameInterval
  119. //self.displayLink.preferredFramesPerSecond = 3;
  120. self.displayLink.paused = NO;
  121. self.countDown = 120;
  122. self.displayLinkCountDown = [CADisplayLink displayLinkWithTarget:self selector:@selector(p_countDown)];
  123. [self.displayLinkCountDown addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  124. self.displayLinkCountDown.paused = NO;
  125. self.displayLinkCountDown.preferredFramesPerSecond = 1;
  126. }
  127. - (void)stopAnimation{
  128. self.displayLink.paused = YES;
  129. [self.displayLink invalidate];
  130. self.displayLink = nil;
  131. self.displayLinkCountDown.paused = YES;
  132. [self.displayLinkCountDown invalidate];
  133. self.displayLinkCountDown = nil;
  134. self.countDownLab.text = @"";
  135. }
  136. - (void)p_countDown{
  137. self.countDownLab.text = [NSString stringWithFormat:@"%d", self.countDown];
  138. self.countDown --;
  139. }
  140. static CGFloat const RDSNormalAngle = 0.08;// 1/60秒旋转的角度
  141. static CGFloat const RDSLowAngle = 0.08; // 秒旋转的角度
  142. static CGFloat const RDSHighAngle = 0.08; // 秒旋转的角度
  143. static CGFloat curAngle = 0;
  144. - (void)d_displayLinkAnimation {
  145. if (curAngle >= 2*M_PI) {
  146. curAngle -= 2*M_PI;
  147. }
  148. if (curAngle >= 0 && curAngle <= M_PI/3.0) {
  149. self.ringImgView.transform = CGAffineTransformRotate(self.ringImgView.transform, RDSNormalAngle);
  150. curAngle += RDSNormalAngle;
  151. }
  152. else if (curAngle > M_PI/3.0 && curAngle <= M_PI_2*3) {
  153. self.ringImgView.transform = CGAffineTransformRotate(self.ringImgView.transform, RDSHighAngle);
  154. curAngle += RDSHighAngle;
  155. }
  156. else {
  157. self.ringImgView.transform = CGAffineTransformRotate(self.ringImgView.transform, RDSLowAngle);
  158. curAngle += RDSLowAngle;
  159. }
  160. }
  161. #pragma mark - GIF
  162. - (void)p_setupGif{
  163. if(_searchView.hidden) return;
  164. _searchView.hidden = YES;
  165. [self stopAnimation];
  166. NSData *gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"finish" ofType:@"gif"]];
  167. // gif只播放1次
  168. CGImageSourceRef gifSource = CGImageSourceCreateWithData(CFBridgingRetain(gifData), nil);
  169. size_t gifCount = CGImageSourceGetCount(gifSource);
  170. NSMutableArray *frames = [NSMutableArray array];
  171. UIImage *lastImg;
  172. for (size_t i = 0; i < gifCount; i++) {
  173. CGImageRef imageRef =CGImageSourceCreateImageAtIndex(gifSource, i,NULL);
  174. UIImage*image = [UIImage imageWithCGImage:imageRef];
  175. [frames addObject:image];
  176. CGImageRelease(imageRef);
  177. if(i == gifCount-1){
  178. lastImg = image;
  179. }
  180. }
  181. UIImageView *imgView = [[UIImageView alloc] init];
  182. imgView.frame = CGRectMake(0, 0, _gifView.rds_width, _gifView.rds_height);
  183. [_gifView addSubview:imgView];
  184. imgView.animationImages = frames;
  185. imgView.animationDuration = 2.5;
  186. imgView.animationRepeatCount = 1;
  187. [imgView startAnimating];
  188. RDS_WEAKSELF(weakSelf)
  189. UIImageView *ok = [[UIImageView alloc] init];
  190. ok.frame = CGRectMake(0, 0, weakSelf.gifView.rds_width, weakSelf.gifView.rds_height);
  191. ok.alpha = 0;
  192. [self.gifView addSubview:ok];
  193. [ok setImage:lastImg];
  194. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  195. ok.alpha = 1;
  196. [RDSHudShower showCenterToast:@"绑定成功"];
  197. });
  198. // WKWebView *webView = [[WKWebView alloc] initWithFrame:_gifView.bounds];
  199. // webView.backgroundColor=[UIColor clearColor];
  200. // webView.scrollView.scrollEnabled=NO;
  201. // [webView loadData:gifData MIMEType:@"image/gif" characterEncodingName:@"" baseURL:[NSURL URLWithString:@""]];
  202. // [_gifView addSubview:webView];
  203. }
  204. - (void)dealloc{
  205. [TheBabyBluetooth rds_cancelScan];
  206. [TheBabyBluetooth rds_cancelConnection];
  207. [kNotificationCenter removeObserver:self];
  208. DDLog(@"dealloc~~ 绑定设备");
  209. }
  210. @end