1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
|
//
// AM4Module.m
// ReactNativeIOSLibrary
//
// Created by daiqingquan on 2016/11/23.
// Copyright © 2016年 daiqingquan. All rights reserved.
//
#import "AM4Module.h"
#import "AMProfileModule.h"
#import "AMMacroFile.h"
#import "AM4Controller.h"
#import "AM4.h"
#define EVENT_NOTIFY @"event_notify_am4"
@implementation AM4Module
RCT_EXPORT_MODULE()
- (NSArray<NSString *> *)supportedEvents {
return @[@"event_notify", @"event_scan_device", @"event_scan_finish",
@"event_device_connected", @"event_device_connect_failed",
@"event_device_disconnect", @"event_authenticate_result",
@"event_notify_ts28b", @"event_notify_bg1",
@"action_connect_result_for_bg1"];
}
- (NSDictionary *)constantsToExport
{
return @{
@"Event_Notify": EVENT_NOTIFY
};
}
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
#pragma mark
#pragma mark - Init
-(id)init
{
if (self=[super init])
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(DeviceConnectForAM4:) name:AM4ConnectNoti object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(DeviceDisConnectForAM4:) name:AM4DisConnectNoti object:nil];
[AM4Controller shareIHAM4Controller];
}
return self;
}
-(AM4*)getAM4WithMac:(NSString*)mac{
AM4Controller *controller = [AM4Controller shareIHAM4Controller];
NSArray *amDeviceArray = [controller getAllCurrentAM4Instace];
for(AM4 *tempAM4 in amDeviceArray){
if([mac isEqualToString:tempAM4.serialNumber]){
return tempAM4;
break;
}
}
return nil;
}
#pragma mark
#pragma mark - Notification
#pragma mark - AM4
-(void)DeviceConnectForAM4:(NSNotification *)tempNoti{
AM4Controller *controller = [AM4Controller shareIHAM4Controller];
NSArray *amDeviceArray = [controller getAllCurrentAM4Instace];
AM4 *AMInstance = [amDeviceArray objectAtIndex:0];
}
-(void)DeviceDisConnectForAM4:(NSNotification *)tempNoti{
AM4Controller *controller = [AM4Controller shareIHAM4Controller];
}
#pragma mark-获取连接设备
RCT_EXPORT_METHOD(getAllConnectedDevices){
NSArray*am4array= [[AM4Controller shareIHAM4Controller] getAllCurrentAM4Instace];
NSMutableArray*deviceMacArray=[NSMutableArray array];
for (int i=0; i<[am4array count]; i++) {
AM4*am4=[am4array objectAtIndex:i];
[deviceMacArray addObject:am4.serialNumber];
}
NSDictionary* deviceInfo = @{@"action":@"ACTION_GET_ALL_CONNECTED_DEVICES",@"devices":deviceMacArray};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}
#pragma mark - 恢复出厂
RCT_EXPORT_METHOD(reset:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4ResetDevice:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"reset_am",@"reset":[NSNumber numberWithInteger:resetSuc]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-得到用户ID
RCT_EXPORT_METHOD(getUserId:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4GetDeviceUserID:^(unsigned int userID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"userid_am",@"userid":[NSNumber numberWithInteger:userID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-设置用户ID
RCT_EXPORT_METHOD(setUserId:(nonnull NSString *)mac :(nonnull NSNumber *)uesrID){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4SetUserID:uesrID withFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_userid_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-同步时间
RCT_EXPORT_METHOD(syncRealTime:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4SyncTime:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_sync_time_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-设置用户信息
RCT_EXPORT_METHOD(setUserInfo:(nonnull NSString *)mac :(nonnull NSNumber *)age :(nonnull NSNumber *)height :(nonnull NSNumber *)weight :(nonnull NSNumber *)gender :(nonnull NSNumber *)unit :(nonnull NSNumber *)target :(nonnull NSNumber *)activityLevel :(nonnull NSNumber *)min){
if ([self getAM4WithMac:mac]!=nil) {
HealthUser *myUser = [[HealthUser alloc]init];
myUser.age = age;
myUser.sex = UserSex_Male;
myUser.height = height;
myUser.weight = weight;
myUser.activityLevel = activityLevel;
NSDictionary *dic = [[NSMutableDictionary alloc]init];
[[self getAM4WithMac:mac] commandAM4SetUserInfo:myUser withUnit:unit withActiveGoal:target withSwimmingGoal:min withSetUserInfoFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_userinfo_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withSetBMR:^(BOOL resetSuc) {
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-得到用户信息
RCT_EXPORT_METHOD(getUserInfo:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4GetUserInfo:^(NSDictionary *userInfo) {
NSNumber *swimGoal = [userInfo valueForKey:@"SwimmingGoal"];
if (swimGoal != nil || swimGoal == 0){
swimGoal = @0;
}
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"get_userinfo_am",@"age":[userInfo valueForKey:@"Age"],@"step":[userInfo valueForKey:@"Step"],@"height":[userInfo valueForKey:@"Height"],@"gender":[userInfo valueForKey:@"Gender"],@"weight":[userInfo valueForKey:@"Weight"],@"unit":[userInfo valueForKey:@"Unit"],@"target1":[userInfo valueForKey:@"TotalStep1"],@"target2":[userInfo valueForKey:@"TotalStep2"],@"target3":[userInfo valueForKey:@"TotalStep3"],@"swim_target":swimGoal};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-得到闹钟数量
RCT_EXPORT_METHOD(getAlarmClockNum:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4GetTotoalAlarmInfo:^(NSMutableArray *totoalAlarmArray) {
NSMutableArray * IDArray = [[NSMutableArray alloc]init];
if (totoalAlarmArray.count > 0){
for (NSDictionary *dic in totoalAlarmArray) {
[IDArray addObject:[dic valueForKey:@"AlarmId"]];
}
}
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"get_alarmnum_am",@"alarmclocknumber":[NSNumber numberWithInt:IDArray.count],@"alarmclocknumberid":IDArray};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-查闹钟信息
RCT_EXPORT_METHOD(getAlarmClockDetail:(nonnull NSString *)mac :(nonnull NSArray *)alarmIDS){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4GetTotoalAlarmInfo:^(NSMutableArray *totoalAlarmArray) {
NSMutableArray * alarmInfoArray = [[NSMutableArray alloc]init];
if (totoalAlarmArray.count > 0){
for (NSDictionary *dic in totoalAlarmArray) {
NSMutableDictionary *alartDic = [[NSMutableDictionary alloc]init];
for (NSNumber * alarmID in alarmIDS) {
NSNumber *deviceAlarmID = [dic valueForKey:@"AlarmId"];
if(deviceAlarmID.intValue == alarmID.intValue){
[alartDic setValue:[dic valueForKey:@"AlarmId"] forKey:@"alarmid"];
NSDate *date = [dic valueForKey:@"Time"];
NSDateFormatter *dateFormater=[[NSDateFormatter alloc]init];
[dateFormater setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormater setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormater setDateFormat:@"HH:mm"];
NSString *dateString = [dateFormater stringFromDate:date];
[alartDic setValue:dateString forKey:@"time"];
[alartDic setValue:[dic valueForKey:@"IsRepeat"] forKey:@"repeat"];
NSMutableDictionary * weekDic = [[NSMutableDictionary alloc]init];
[weekDic setValue:[[dic valueForKey:@"Week"] objectAtIndex:0] forKey:@"sun"];
[weekDic setValue:[[dic valueForKey:@"Week"] objectAtIndex:1] forKey:@"mon"];
[weekDic setValue:[[dic valueForKey:@"Week"] objectAtIndex:2] forKey:@"tue"];
[weekDic setValue:[[dic valueForKey:@"Week"] objectAtIndex:3] forKey:@"wed"];
[weekDic setValue:[[dic valueForKey:@"Week"] objectAtIndex:4] forKey:@"thu"];
[weekDic setValue:[[dic valueForKey:@"Week"] objectAtIndex:5] forKey:@"fri"];
[weekDic setValue:[[dic valueForKey:@"Week"] objectAtIndex:6] forKey:@"sat"];
[alartDic setValue:weekDic forKey:@"get_alarm_week"];
[alartDic setValue:[dic valueForKey:@"Switch"] forKey:@"switch"];
[alarmInfoArray addObject:alartDic];
}
}
}
}
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"get_alarminfo_am",@"alarmclockdetail":alarmInfoArray};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-设置闹钟
RCT_EXPORT_METHOD(setAlarmClock:(nonnull NSString *)mac :(nonnull NSNumber *)alarmID :(nonnull NSNumber *)hour :(nonnull NSNumber *)min :(nonnull NSNumber*)isRepeat :(nonnull NSArray *)weekArray :(nonnull NSNumber *)isOn){
if ([self getAM4WithMac:mac]!=nil) {
if(alarmID.integerValue < 0 || alarmID.integerValue > 3 ||
hour.integerValue > 23 || hour.integerValue < 0 ||
min.integerValue > 59 || min.integerValue < 0){
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:400]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
return;
}
NSDateFormatter *formater=[[NSDateFormatter alloc]init];
[formater setTimeZone:[NSTimeZone defaultTimeZone]];
[formater setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formater setDateFormat:@"HH-mm"];
NSString * timeStr = [NSString stringWithFormat:@"%@-%@",hour,min];
NSDate *date = [formater dateFromString:timeStr];
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:alarmID,@"AlarmId",date,@"Time",isRepeat,@"IsRepeat",weekArray,@"Week",isOn,@"Switch", nil];
[[self getAM4WithMac:mac] commandAM4SetAlarmDictionary:dic withFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_alarminfo_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-删除闹钟
RCT_EXPORT_METHOD(deleteAlarmClock:(nonnull NSString *)mac :(nonnull NSNumber *)alarmID){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4DeleteAlarmID:alarmID withFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"delete_alarm_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-得闹钟信息
RCT_EXPORT_METHOD(getActivityRemind:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4GetReminderInfo:^(NSArray *remindInfo) {
NSDictionary *dic = [remindInfo objectAtIndex:0];
NSDate *date = [dic valueForKey:@"Time"];
NSDateFormatter *dateFormater=[[NSDateFormatter alloc]init];
[dateFormater setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormater setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormater setDateFormat:@"HH:mm"];
NSString *dateString = [dateFormater stringFromDate:date];
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"get_activity_remind_am",@"time":dateString,@"switch":[dic valueForKey:@"Switch"]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-设置提醒
RCT_EXPORT_METHOD(setActivityRemind:(nonnull NSString *)mac :(nonnull NSNumber *)hour :(nonnull NSNumber *)min :(nonnull NSNumber *)isOn){
if ([self getAM4WithMac:mac]!=nil) {
if(hour.integerValue > 23 || hour.integerValue < 0 ||
min.integerValue > 59 || min.integerValue < 0){
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:400]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
return;
}
NSDateFormatter *formater=[[NSDateFormatter alloc]init];
[formater setTimeZone:[NSTimeZone defaultTimeZone]];
[formater setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formater setDateFormat:@"HH-mm"];
NSString * timeStr = [NSString stringWithFormat:@"%@-%@",hour,min];
NSDate *date = [formater dateFromString:timeStr];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:date,@"Time",isOn,@"Switch", nil];
[[self getAM4WithMac:mac] commandAM4SetReminderDictionary:dic withFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_activityremind_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark - 运动数据
RCT_EXPORT_METHOD(syncActivityData:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4StartSyncActiveData:^(NSDictionary *startDataDictionary) {
} withActiveHistoryData:^(NSArray *historyDataArray) {
NSMutableArray *lastArray = [[NSMutableArray alloc]init];
for (NSDictionary *dic in historyDataArray) {
NSMutableDictionary * lastDic = [[NSMutableDictionary alloc]init];
NSDateFormatter *dateFormat = nil;
if (dateFormat == nil) {
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
}
NSString *locationString=[dateFormat stringFromDate:[dic valueForKey:AMDate]];
[lastDic setValue:locationString forKey:@"time"];
[lastDic setValue:[dic valueForKey:AMStepSize] forKey:@"stepsize"];
[lastDic setValue:[dic valueForKey:AMCalorie] forKey:@"calorie"];
[lastDic setValue:[dic valueForKey:AMStepNum] forKey:@"step"];
[lastDic setValue:[dic valueForKey:@"dataID"] forKey:@"dataID"];
[lastArray addObject:lastDic];
}
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:lastArray forKey:@"activity_each_data"];
NSArray *array = [NSArray arrayWithObjects:dictionary, nil];
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"sync_activity_data_am",@"activity":array};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withActiveFinishTransmission:^{
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark - 睡眠数据
RCT_EXPORT_METHOD(syncSleepData:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4StartSyncSleepData:^(NSDictionary *startDataDictionary) {
} withSleepHistoryData:^(NSArray *historyDataArray) {
NSMutableArray *lastArray = [[NSMutableArray alloc]init];
for (NSArray *array in historyDataArray) {
for (NSDictionary *dic in array) {
NSMutableDictionary * lastDic = [[NSMutableDictionary alloc]init];
NSDateFormatter *dateFormat = nil;
if (dateFormat == nil) {
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
}
NSString *locationString=[dateFormat stringFromDate:[dic valueForKey:AMDate]];
[lastDic setValue:locationString forKey:@"time"];
[lastDic setValue:[dic valueForKey:@"SleepData"] forKey:@"level"];
[lastDic setValue:[dic valueForKey:@"dataID"] forKey:@"dataID"];
[lastArray addObject:lastDic];
}
}
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:lastArray forKey:@"sleep_each_data"];
NSArray *array = [NSArray arrayWithObjects:dictionary, nil];
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"sync_sleep_data_am",@"sleep":array};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withSleepFinishTransmission:^{
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-阶段性数据
RCT_EXPORT_METHOD(syncStageReportData:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4StartSyncStageData:^(NSArray *measureDataArray) {
NSMutableArray *lastArray = [[NSMutableArray alloc]init];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"ReportState == 1"];
NSArray *workoutArray = [measureDataArray filteredArrayUsingPredicate:pre];
if (workoutArray.count != 0)
{
for (NSDictionary *dic in workoutArray) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setValue:@"stage_data_type_workout" forKey:@"type"];
NSDateFormatter *dateFormat = nil;
if (dateFormat == nil) {
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
}
NSString *locationString=[dateFormat stringFromDate:[dic valueForKey:Work_outMeasureDate]];
[dictionary setValue:locationString forKey:@"stoptime"];
[dictionary setValue:[dic valueForKey:Work_outTimeNumber] forKey:@"usedtime"];
[dictionary setValue:[dic valueForKey:Work_outStepNumber] forKey:@"stage_data_workout_step"];
[dictionary setValue:[dic valueForKey:Work_outLengthNumber] forKey:@"stage_data_distance"];
[dictionary setValue:[dic valueForKey:Work_outCalories] forKey:@"calorie"];
[dictionary setValue:[dic valueForKey:@"dataID"] forKey:@"dataID"];
[lastArray addObject:dictionary];
}
}
NSPredicate *predicateSwim = [NSPredicate predicateWithFormat:@"ReportState == 0"];
NSArray *swimArray = [measureDataArray filteredArrayUsingPredicate:predicateSwim];
if (swimArray.count != 0)
{
for (NSDictionary *dic in swimArray) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setValue:@"swim" forKey:@"type"];
NSDateFormatter *dateFormat = nil;
if (dateFormat == nil) {
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
}
NSString *locationString=[dateFormat stringFromDate:[dic valueForKey:AM4SwimmingMeasureDate]];
[dictionary setValue:locationString forKey:@"stoptime"];
[dictionary setValue:[dic valueForKey:AM4SwimmingTimeNumber] forKey:@"usedtime"];
[dictionary setValue:[dic valueForKey:AM4SwimmingTimes] forKey:@"number of strokes"];
[dictionary setValue:[dic valueForKey:AM4Swimmingcalories] forKey:@"calorie"];
[dictionary setValue:[dic valueForKey:AM4SwimmingAct] forKey:@"swimming stroke"];
[dictionary setValue:[dic valueForKey:AM4SwimmingCircleCount] forKey:@"number of turns"];
[dictionary setValue:[dic valueForKey:AM4SwimmingPoollength] forKey:@"stage_data_swimpool_length"];
[dictionary setValue:[dic valueForKey:AM4EnterSwimmingTime] forKey:@"stage_data_cutindif"];
[dictionary setValue:[dic valueForKey:AM4OutSwimmingTime] forKey:@"stage_data_cutoutdif"];
[dictionary setValue:[dic valueForKey:AM4SwimmingProcessMark] forKey:@"stage_data_processflag"];
[dictionary setValue:[dic valueForKey:@"dataID"] forKey:@"dataID"];
[lastArray addObject:dictionary];
}
}
NSPredicate *predicateSleep = [NSPredicate predicateWithFormat:@"ReportState == 2"];
NSArray *sleepArray = [measureDataArray filteredArrayUsingPredicate:predicateSleep];
if (sleepArray.count != 0)
{
for (NSDictionary *dic in sleepArray) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setValue:@"sleep" forKey:@"type"];
NSDateFormatter *dateFormat = nil;
if (dateFormat == nil) {
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
}
NSString *locationString=[dateFormat stringFromDate:[dic valueForKey:Sleep_summaryMeasureDate]];
[dictionary setValue:locationString forKey:@"stoptime"];
[dictionary setValue:[dic valueForKey:Sleep_summarySleepTime] forKey:@"usedtime"];
[dictionary setValue:[dic valueForKey:Sleep_summarysleepEfficiency] forKey:@"sleepefficiency"];
[dictionary setValue:[dic valueForKey:Sleep_summarysleepAddMinute] forKey:@"is50min"];
[lastArray addObject:dictionary];
}
}
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"sync_stage_data_am",@"stage_data":lastArray};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withStageDataFinishTransmission:^(BOOL resetSuc) {
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark - 实时数据
RCT_EXPORT_METHOD(syncRealData:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4StartSyncCurrentActiveData:^(NSDictionary *activeDictionary) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"sync_real_data_am",@"step":[activeDictionary valueForKey:@"Step"],@"calorie":[activeDictionary valueForKey:@"Calories"],@"totalcalories":[activeDictionary valueForKey:@"TotalCalories"]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark - 查询状态
RCT_EXPORT_METHOD(queryAMState:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
__block NSInteger index;
[[self getAM4WithMac:mac] commandAM4GetDeviceStateInfo:^(AM4QueryState queryState) {
index = queryState;
} withBattery:^(NSNumber *battery) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"query_state_am",@"query_state":[NSNumber numberWithInteger:index],@"battery":battery};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark - 设置BMR
RCT_EXPORT_METHOD(setUserBmr:(nonnull NSString *)mac :(nonnull NSNumber *)bmr){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4SetBMR:bmr withFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_bmr_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-发随机数
RCT_EXPORT_METHOD(sendRandom:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4SetRandomNumber:^(NSString *randomNumString) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"get_random_am",@"random":randomNumString};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-断开
RCT_EXPORT_METHOD(disconnect:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4Disconnect:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-设置游泳
RCT_EXPORT_METHOD(setSwimPara:(nonnull NSString *)mac :(nonnull BOOL *)isOpen :(nonnull NSNumber *)poolLength :(nonnull NSNumber *)hours :(nonnull NSNumber *)minutes :(nonnull NSNumber *)unit ){
if ([self getAM4WithMac:mac]!=nil) {
NSDateFormatter *formater=[[NSDateFormatter alloc]init];
[formater setTimeZone:[NSTimeZone defaultTimeZone]];
[formater setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formater setDateFormat:@"HH-mm"];
NSString * timeStr = [NSString stringWithFormat:@"%@-%@",hours,minutes];
NSDate *date = [formater dateFromString:timeStr];
[[self getAM4WithMac:mac] commandAM4SetSwimmingState:isOpen withSwimmingPoolLength:poolLength withNOSwimmingTime:date withUnit:unit.integerValue withFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_swiminfo_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-获取游泳信息
RCT_EXPORT_METHOD(checkSwimPara:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4GetSwimmingInfo:^(BOOL swimmingIsOpen, NSNumber *swimmingLaneLength, NSNumber *NOSwimmingTime, AM4SwimmingUnit unit) {
int hour = NOSwimmingTime.intValue / 60;
int min = NOSwimmingTime.intValue % 60;
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"get_swiminfo_am",@"get_swimlane_length":swimmingLaneLength,@"get_swim_unit_am":[NSNumber numberWithInt:unit],@"get_swim_switch_am":@"swimmingIsOpen",@"get_swim_cutout_hour_am":[NSNumber numberWithInt:hour],@"get_swim_cutout_min_am":[NSNumber numberWithInt:min]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-设置时间制
RCT_EXPORT_METHOD(setHourMode:(nonnull NSString *)mac :(nonnull NSNumber *)hourMode){
if ([self getAM4WithMac:mac]!=nil) {
AM4TimeFormatAndNation formatAndNation;
formatAndNation = hourMode.intValue;
[[self getAM4WithMac:mac] commandAM4SetTimeFormatAndNation:formatAndNation withFinishResult:^(BOOL resetSuc) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"set_hour_mode_success_am"};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
#pragma mark-得到时间制
RCT_EXPORT_METHOD(getHourMode:(nonnull NSString *)mac){
if ([self getAM4WithMac:mac]!=nil) {
[[self getAM4WithMac:mac] commandAM4GetTimeFormatAndNation:^(AM4TimeFormatAndNation timeFormatAndNation) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"get_hour_mode_am",@"hourtype":[NSNumber numberWithInt:timeFormatAndNation]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
} withErrorBlock:^(AM4ErrorID errorID) {
NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"error_am",@"error":[NSNumber numberWithInteger:errorID]};
[self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}];
}
}
@end
|