summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BP5Module.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BP5Module.m')
-rw-r--r--libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BP5Module.m501
1 files changed, 501 insertions, 0 deletions
diff --git a/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BP5Module.m b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BP5Module.m
new file mode 100644
index 0000000..4804e9c
--- /dev/null
+++ b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BP5Module.m
@@ -0,0 +1,501 @@
1//
2// BP5Module.m
3// ReactNativeIOSLibrary
4//
5// Created by daiqingquan on 2016/11/23.
6// Copyright © 2016年 daiqingquan. All rights reserved.
7//
8
9#import "BP5Module.h"
10#import "BPProfileModule.h"
11#import "BPMacroFile.h"
12#import "BP5Controller.h"
13#import "BP5.h"
14#import "iHealthDeviceManagerModule.h"
15
16@interface BP5Module()
17@property (nonatomic, strong) NSNumber* previousPressure;
18@property (nonatomic, assign) BOOL startSendWavelet;
19@property (nonatomic, assign) BOOL isMeasuring;
20
21@end
22@implementation BP5Module
23#define EVENT_NOTIFY @"BP5.MODULE.NOTIFY"
24RCT_EXPORT_MODULE()
25- (NSArray<NSString *> *)supportedEvents {
26 return @[@"event_notify", @"event_scan_device", @"event_scan_finish",
27 @"event_device_connected", @"event_device_connect_failed",
28 @"event_device_disconnect", @"event_authenticate_result",
29 @"event_notify_ts28b", @"event_notify_bg1",
30 @"action_connect_result_for_bg1"];
31}
32
33
34
35- (NSDictionary *)constantsToExport
36{
37 return @{
38 @"Event_Notify":EVENT_NOTIFY,
39
40 };
41}
42+ (BOOL)requiresMainQueueSetup
43{
44 return YES;
45}
46
47#pragma mark
48#pragma mark - Init
49
50-(BP5*)getBP5WithMac:(NSString*)mac{
51
52 BP5Controller *controller = [BP5Controller shareBP5Controller];
53 NSArray *bpDeviceArray = [controller getAllCurrentBP5Instace];
54
55 for(BP5 *tempDevice in bpDeviceArray){
56 if([mac isEqualToString:tempDevice.serialNumber]){
57
58 return tempDevice;
59 }
60 }
61
62 return nil;
63
64// static BP5* bp5Device = nil;
65// if (!bp5Device) {
66// BP5Controller *controller = [BP5Controller shareBP5Controller];
67// NSArray *bpDeviceArray = [controller getAllCurrentBP5Instace];
68//
69// for(BP5 *tempBP5 in bpDeviceArray){
70// if([mac isEqualToString:tempBP5.serialNumber]){
71//
72// bp5Device = tempBP5;
73// break;
74// }
75// }
76// }
77//
78//
79// return bp5Device;
80}
81
82#pragma mark
83#pragma mark - Notification
84#pragma mark - BP5
85//-(void)DeviceConnectForBP5:(NSNotification *)tempNoti{
86// BP5Controller *controller = [BP5Controller shareBP5Controller];
87// NSArray *bpDeviceArray = [controller getAllCurrentBP5Instace];
88//
89// BP5 *bpInstance = [bpDeviceArray objectAtIndex:0];
90//
91//}
92//
93//- (void)DeviceDisConnectForBP5:(NSNotification*)tempNoti{
94//
95//}
96
97
98
99
100#pragma mark
101#pragma mark - Method
102
103
104#pragma mark-获取连接设备
105RCT_EXPORT_METHOD(getAllConnectedDevices){
106
107
108 NSArray*bp5array= [[BP5Controller shareBP5Controller] getAllCurrentBP5Instace];
109
110 NSMutableArray*deviceMacArray=[NSMutableArray array];
111
112 for (int i=0; i<[bp5array count]; i++) {
113
114 BP5*bp5=[bp5array objectAtIndex:i];
115
116 [deviceMacArray addObject:bp5.serialNumber];
117
118 }
119
120 NSDictionary* deviceInfo = @{@"action":@"ACTION_GET_ALL_CONNECTED_DEVICES",@"devices":deviceMacArray};
121
122 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
123
124
125}
126
127
128
129
130
131RCT_EXPORT_METHOD(startMeasure:(nonnull NSString *)mac){
132
133 _previousPressure = @(0);
134 self.startSendWavelet = NO;
135 if ([self getBP5WithMac:mac]!=nil) {
136 __weak typeof(self) weakSelf = self;
137 [[self getBP5WithMac:mac] commandStartMeasureWithZeroingState:^(BOOL isComplete) {
138 weakSelf.isMeasuring = YES;
139 NSDictionary* response = @{
140 kACTION:isComplete ? kACTION_ZOREING_BP : kACTION_ZOREOVER_BP,
141 };
142 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
143 } pressure:^(NSArray *pressureArr) {
144 NSLog(@"pressure %@",pressureArr);
145 weakSelf.isMeasuring = YES;
146 [weakSelf sendPresssre:pressureArr.firstObject wavelet:nil isHeartPulse:NO];
147 } waveletWithHeartbeat:^(NSArray *waveletArr) {
148 NSLog(@"xiaoboWithHeart %@",waveletArr);
149 weakSelf.isMeasuring = YES;
150 [weakSelf sendPresssre:nil wavelet:waveletArr isHeartPulse:YES];
151 } waveletWithoutHeartbeat:^(NSArray *waveletArr) {
152 weakSelf.isMeasuring = YES;
153 NSLog(@"xiaoboNoHeart %@",waveletArr);
154 [weakSelf sendPresssre:nil wavelet:waveletArr isHeartPulse:NO];
155 } result:^(NSDictionary *resultDict) {
156 NSLog(@"result %@",resultDict);
157 weakSelf.startSendWavelet = NO;
158 weakSelf.isMeasuring = NO;
159 NSDictionary* response = @{
160 kACTION:kACTION_ONLINE_RESULT_BP,
161 kHIGH_BLOOD_PRESSURE_BP:resultDict[@"sys"],
162 kLOW_BLOOD_PRESSURE_BP:resultDict[@"dia"],
163 kPULSE_BP:resultDict[@"heartRate"],
164 kMEASUREMENT_AHR_BP:resultDict[@"irregular"],
165 kDATAID:resultDict[@"dataID"],
166 };
167 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
168 } errorBlock:^(BPDeviceError error) {
169 NSLog(@"error %lu",(unsigned long)error);
170 weakSelf.startSendWavelet = NO;
171 weakSelf.isMeasuring = NO;
172 [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
173
174 }];
175
176
177 }else{
178
179 NSLog(@"error %d",BPDidDisconnect);
180 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
181 self.isMeasuring = NO;
182 }
183
184
185}
186
187- (void)sendPresssre:(NSNumber*)pressure wavelet:(NSArray*)waveletArray isHeartPulse:(BOOL)heartPulse{
188
189 if (pressure) {
190 self.previousPressure = pressure;
191 }
192 if (waveletArray.count > 0) {
193 self.startSendWavelet = YES;
194 }
195 if (pressure && !self.startSendWavelet) {
196 NSDictionary* response = @{
197 kACTION:kACTION_ONLINE_PRESSURE_BP,
198 kBLOOD_PRESSURE_BP:pressure,
199 };
200 [BPProfileModule sendEventToEmitter:self eventNotify:EVENT_NOTIFY WithDict:response];
201 }else if (waveletArray.count > 0 && self.startSendWavelet){
202 NSDictionary* response = @{
203 kACTION:kACTION_ONLINE_PULSEWAVE_BP,
204 kBLOOD_PRESSURE_BP:self.previousPressure,
205 kFLAG_HEARTBEAT_BP:@(heartPulse),
206 kPULSEWAVE_BP:waveletArray
207 };
208 [BPProfileModule sendEventToEmitter:self eventNotify:EVENT_NOTIFY WithDict:response];
209 }
210}
211
212
213RCT_EXPORT_METHOD(stopMeasure:(nonnull NSString *)mac){
214
215 if (!self.isMeasuring) {
216 NSLog(@"error %d",401);
217 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:401];
218 return;
219 }
220 if ([self getBP5WithMac:mac]!=nil) {
221 __weak typeof(self) weakSelf = self;
222
223 [[self getBP5WithMac:mac] stopBPMeassureSuccessBlock:^{
224
225 } errorBlock:^(BPDeviceError error) {
226
227 NSLog(@"error %lu",(unsigned long)error);
228 [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
229
230 }];
231
232 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
233 NSDictionary* response = @{
234 kACTION:kACTION_INTERRUPTED_BP,
235 };
236 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
237 weakSelf.isMeasuring = NO;
238 });
239 }else{
240
241 NSLog(@"error %lu",(unsigned long)BPDidDisconnect);
242 self.isMeasuring = NO;
243 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
244
245 }
246
247}
248
249RCT_EXPORT_METHOD(getBattery:(nonnull NSString *)mac){
250
251
252 if ([self getBP5WithMac:mac]!=nil) {
253 __weak typeof(self) weakSelf = self;
254 [[self getBP5WithMac:mac] commandEnergy:^(NSNumber *energyValue) {
255 NSDictionary* response = @{
256 kACTION:kACTION_BATTERY_BP,
257 kBATTERY_BP:energyValue
258 };
259 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
260 } errorBlock:^(BPDeviceError error) {
261 NSLog(@"error %lu",(unsigned long)error);
262 [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
263 }];
264
265 }else{
266
267 NSLog(@"error %lu",(unsigned long)BPDidDisconnect);
268 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
269
270 }
271
272
273
274
275}
276
277
278RCT_EXPORT_METHOD(enbleOffline:(nonnull NSString *)mac){
279
280
281 if ([self getBP5WithMac:mac]!=nil) {
282 __block BOOL success = YES;
283 __weak typeof(self) weakSelf = self;
284 [[self getBP5WithMac:mac] commandSetOffline:YES errorBlock:^(BPDeviceError error) {
285 success = NO;
286 [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
287 }];
288
289 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
290 if (success) {
291 NSDictionary* response = @{
292 kACTION:kACTION_ENABLE_OFFLINE_BP,
293 };
294 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
295 }
296 });
297
298 }else{
299 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
300 }
301
302}
303
304
305RCT_EXPORT_METHOD(disableOffline:(nonnull NSString *)mac){
306
307
308 if ([self getBP5WithMac:mac]!=nil) {
309 __block BOOL success = YES;
310 __weak typeof(self) weakSelf = self;
311 [[self getBP5WithMac:mac] commandSetOffline:NO errorBlock:^(BPDeviceError error) {
312 success = NO;
313 [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
314 }];
315
316 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
317 if (success) {
318 NSDictionary* response = @{
319 kACTION:kACTION_DISENABLE_OFFLINE_BP,
320 };
321 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
322 }
323 });
324
325 }else{
326 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
327 }
328
329
330}
331
332
333RCT_EXPORT_METHOD(isEnableOffline:(nonnull NSString *)mac){
334
335
336 if ([self getBP5WithMac:mac]!=nil) {
337 __weak typeof(self) weakSelf = self;
338 [[self getBP5WithMac:mac] commandFunction:^(NSDictionary *dic) {
339 NSDictionary* response = @{
340 kACTION:kACTION_IS_ENABLE_OFFLINE,
341 kIS_ENABLE_OFFLINE:dic[@"offlineOpen"]
342 };
343 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
344 } errorBlock:^(BPDeviceError error) {
345 [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
346 }];
347 }else{
348 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
349 }
350
351}
352
353
354
355RCT_EXPORT_METHOD(getOfflineNum:(nonnull NSString *)mac){
356
357
358
359 if ([self getBP5WithMac:mac]!=nil) {
360
361 __weak typeof(self) weakSelf = self;
362
363 [[self getBP5WithMac:mac] commandTransferMemoryTotalCount:^(NSNumber *count) {
364
365 NSDictionary* response = @{
366 kACTION:kACTION_HISTORICAL_NUM_BP,
367 kHISTORICAL_NUM_BP:count
368 };
369 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
370
371 } errorBlock:^(BPDeviceError error) {
372
373 }];
374 }else{
375 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
376 }
377
378
379
380// if ([self getBP5WithMac:mac]!=nil) {
381// __weak typeof(self) weakSelf = self;
382//
383// [[self getBP5WithMac:mac] commandBatchUpload:^(NSNumber *count) {
384// NSDictionary* response = @{
385// kACTION:kACTION_HISTORICAL_NUM_BP,
386// kHISTORICAL_NUM_BP:count
387// };
388// [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
389//
390// } progress:^(NSNumber *progressValue) {
391//
392// } dataArray:^(NSArray *bachArray) {
393// NSMutableArray * historyDataArray = [NSMutableArray array];
394//
395// for(NSDictionary *dataDict in bachArray)
396// {
397//
398// NSDate *date = [dataDict objectForKey:@"time"];
399//
400// //将时间格式转化成字符串,适配plugin和react native
401// NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
402// [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
403// NSString *dateStr = [mydateFormatter stringFromDate:date];
404// NSDictionary* historyDataDict = @{
405// kMEASUREMENT_DATE_BP:dateStr,
406// kHIGH_BLOOD_PRESSURE_BP:dataDict[@"sys"],
407// kLOW_BLOOD_PRESSURE_BP:dataDict[@"dia"],
408// kPULSE_BP:dataDict[@"heartRate"],
409// kMEASUREMENT_AHR_BP:dataDict[@"irregular"],
410// // kMEASUREMENT_HSD_BP:dataDict[@"hsdValue"],
411// kDATAID:dataDict[@"dataID"]
412// };
413// [historyDataArray addObject:historyDataDict];
414//
415//
416// }
417//
418// if (historyDataArray.count > 0) {
419// NSDictionary* deviceInfo = @{kACTION:kACTION_HISTORICAL_DATA_BP,kHISTORICAL_DATA_BP:[historyDataArray copy] };
420// [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:deviceInfo];
421// }
422//
423// } errorBlock:^(BPDeviceError error) {
424// [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
425//
426// }];
427//
428// }else{
429// [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
430// }
431}
432
433
434
435RCT_EXPORT_METHOD(getOfflineData:(nonnull NSString *)mac){
436
437
438 if ([self getBP5WithMac:mac]!=nil) {
439 __weak typeof(self) weakSelf = self;
440 [[self getBP5WithMac:mac] commandBatchUpload:^(NSNumber *count) {
441 NSDictionary* response = @{
442 kACTION:kACTION_HISTORICAL_NUM_BP,
443 kHISTORICAL_NUM_BP:count
444 };
445 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
446
447 } progress:^(NSNumber *progressValue) {
448
449 } dataArray:^(NSArray *bachArray) {
450 NSMutableArray * historyDataArray = [NSMutableArray array];
451
452 for(NSDictionary *dataDict in bachArray)
453 {
454
455 NSDate *date = [dataDict objectForKey:@"time"];
456
457 //将时间格式转化成字符串,适配plugin和react native
458 NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
459 [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
460 NSString *dateStr = [mydateFormatter stringFromDate:date];
461 NSDictionary* historyDataDict = @{
462 kMEASUREMENT_DATE_BP:dateStr,
463 kHIGH_BLOOD_PRESSURE_BP:dataDict[@"sys"],
464 kLOW_BLOOD_PRESSURE_BP:dataDict[@"dia"],
465 kPULSE_BP:dataDict[@"heartRate"],
466 kMEASUREMENT_AHR_BP:dataDict[@"irregular"],
467 // kMEASUREMENT_HSD_BP:dataDict[@"hsdValue"],
468 kDATAID:dataDict[@"dataID"]
469 };
470 [historyDataArray addObject:historyDataDict];
471
472
473 }
474
475 if (historyDataArray.count > 0) {
476 NSDictionary* deviceInfo = @{kACTION:kACTION_HISTORICAL_DATA_BP,kHISTORICAL_DATA_BP:[historyDataArray copy] };
477 [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:deviceInfo];
478 }
479
480 } errorBlock:^(BPDeviceError error) {
481 [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
482
483 }];
484
485 }else{
486 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
487 }
488
489}
490
491
492
493RCT_EXPORT_METHOD(disconnect:(nonnull NSString *)mac){
494 NSLog(@"iOS doesn't support disconnect normal BT devices");
495 [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:900];
496
497}
498
499
500
501@end