summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BP550BTModule.m
blob: e8976be7e6346e37c8311bc1dc6031bf7cb1dadc (plain)
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
//
//  BP550BTModule.m
//  ReactNativeIOSLibrary
//
//  Created by daiqingquan on 2016/11/23.
//  Copyright © 2016年 daiqingquan. All rights reserved.
//

#import "BP550BTModule.h"
#import "KN550BT.h"
#import "KN550BTController.h"
#import "BPProfileModule.h"
#import "iHealthDeviceManagerModule.h"
#import "ScanDeviceController.h"
#import "ConnectDeviceController.h"
#define EVENT_NOTIFY @"event_notify_bp550bt"

@implementation BP550BTModule

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(DeviceConnectForKN550BT:) name:KN550BTConnectNoti object:nil];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(DeviceDisConnectForKN550BT:) name:KN550BTDisConnectNoti object:nil];
        
        [KN550BTController shareKN550BTController];
        
    }
    return self;
}


-(KN550BT*)getDeviceWithMac:(NSString*)mac{
    
    KN550BTController *controller = [KN550BTController shareKN550BTController];
    NSArray *bpDeviceArray = [controller getAllCurrentKN550BTInstace];
    
    for(KN550BT *tempDevice in bpDeviceArray){
        if([mac isEqualToString:tempDevice.serialNumber]){
            
//            tempDevice.reactNativeFlg = @YES;
            return tempDevice;
        }
    }
    
    return nil;
}


//-(void)DeviceConnectForKN550BT:(NSNotification *)tempNoti{
//    KN550BTController *controller = [KN550BTController shareKN550BTController];
//    NSArray *BPDeviceArray = [controller getAllCurrentKN550BTInstace];
//    
//    KN550BT *bpInstance = [BPDeviceArray objectAtIndex:0];
//    NSString *mac = bpInstance.currentUUID;
//    NSDictionary *IDPSDic = [tempNoti userInfo];
//    
//    NSDictionary* deviceInfo = @{
//                                 kACTION:@"kn550bt_connected_bg",@"idps":IDPSDic };
//    [BPProfileModule sendEventToEmitter:self eventNotify:EVENT_NOTIFY WithDict:response];
//    
//}
//
//-(void)DeviceDisConnectForKN550BT:(NSNotification *)tempNoti{
//    
//    NSDictionary *IDPSDic = [tempNoti userInfo];
//    NSString *mac = [IDPSDic objectForKey:@"SerialNumber"];
//    
//    NSDictionary* deviceInfo = @{@"mac":mac,@"action":@"kn550bt_disconnected_bg",@"idps":IDPSDic};
//    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
//    
//}


#pragma mark-获取连接设备
RCT_EXPORT_METHOD(getAllConnectedDevices){
    
    
    NSArray*bp550array= [[KN550BTController shareKN550BTController] getAllCurrentKN550BTInstace];
    
    NSMutableArray*deviceMacArray=[NSMutableArray array];
    
    for (int i=0; i<[bp550array count]; i++) {
        
        KN550BT*kn550bt=[bp550array objectAtIndex:i];
        
        [deviceMacArray addObject:kn550bt.serialNumber];
        
    }
    
    NSDictionary* deviceInfo = @{@"action":@"ACTION_GET_ALL_CONNECTED_DEVICES",@"devices":deviceMacArray};
    
    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
    
    
}




#pragma mark - Method

RCT_EXPORT_METHOD(getFunctionInfo:(nonnull NSString *)mac){
    
    if ([self getDeviceWithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac] commandFunction:^(NSDictionary *dic) {
           
            NSDictionary* response = @{
                                       kACTION:kACTION_FUNCTION_INFORMATION_BP,
                                       kFUNCTION_IS_UPAIR_MEASURE: [dic objectForKey:@"upAirMeasureFlg"],
                                       kFUNCTION_IS_ARM_MEASURE: [dic objectForKey:@"armMeasureFlg"],
                                       kFUNCTION_HAVE_ANGLE_SENSOR: [dic objectForKey:@"haveAngleSensor"],
                                       kFUNCTION_HAVE_OFFLINE: [dic objectForKey:@"haveOffline"],
                                       kFUNCTION_HAVE_HSD: [dic objectForKey:@"haveHSD"],
                                       kFUNCTION_HAVE_ANGLE_SETTING: [dic objectForKey:@"haveAngleSet"],
                                       kFUNCTION_IS_MULTI_UPLOAD: [dic objectForKey:@"mutableUpload"],
                                       kFUNCTION_HAVE_SELF_UPDATE: [dic objectForKey: @"selfUpdate"],
                                       kType:@"KN550",
                                       kMAC:mac
                                       };
            [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
            
        } errorBlock:^(BPDeviceError error) {
            
            [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
            
        }];
    }else{
        
        [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
        
    }
    
    
}

RCT_EXPORT_METHOD(getBattery:(nonnull NSString *)mac){
    
    if ([self getDeviceWithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac] commandEnergy:^(NSNumber *energyValue) {
            
            NSDictionary* response = @{
                                         kACTION:kACTION_BATTERY_BP,
                                         kBATTERY_BP:energyValue,
                                         kType:@"KN550",
                                         kMAC:mac
                                         };
            [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
            NSLog(@"%@",response);
            
        } errorBlock:^(BPDeviceError error) {
            
            [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
            
        }];
        
    }else{
        
        [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
    }
    
    
}

RCT_EXPORT_METHOD(getOffLineNum:(nonnull NSString *)mac){
    
    if ([self getDeviceWithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac]commandTransferMemoryTotalCount:^(NSNumber *num) {
            
            NSDictionary* response = @{
                                         kACTION:kACTION_HISTORICAL_NUM_BP,
                                         kHISTORICAL_NUM_BP:num,
                                         kType:@"KN550",
                                         kMAC:mac
                                         };
            [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
            
        } errorBlock:^(BPDeviceError error) {
            
            [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
            
        }];
         
         }else{
             [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
             
    }
    
    
}

RCT_EXPORT_METHOD(getOffLineData:(nonnull NSString *)mac){
    
    if ([self getDeviceWithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        
        [[self getDeviceWithMac:mac] commandTransferMemoryDataWithTotalCount:^(NSNumber *count) {
            if ([count integerValue] == 0) {
                NSDictionary* response = @{kACTION:kACTION_HISTORICAL_DATA_BP };
                [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
            }
        } progress:^(NSNumber *progress) {
            
        } dataArray:^(NSArray *array) {
            NSMutableArray * tempArr = [[NSMutableArray alloc]init];
            
            for(NSDictionary *history in array)
            {
                NSDate *tempDate = [history objectForKey:@"time"];
                
                //将时间格式转化成字符串,适配plugin和react native【
                NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
                [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSString *dateStr = [mydateFormatter stringFromDate:tempDate];
                
                NSNumber*bpHSD=[history valueForKey:@"hsdValue"];
                NSDictionary *dic=[NSDictionary dictionary];
                if (bpHSD!=nil) {
                    dic = @{
                                          kMEASUREMENT_DATE_BP: dateStr,
                                          kLOW_BLOOD_PRESSURE_BP: [history objectForKey:@"dia"],
                                          kHIGH_BLOOD_PRESSURE_BP: [history objectForKey:@"sys"],
                                          kMEASUREMENT_AHR_BP: [history objectForKey:@"irregular"],
                                          kPULSE_BP: [history objectForKey:@"heartRate"],
                                          kDATAID: [history objectForKey:@"dataID"],
                                          kMEASUREMENT_HSD_BP: history[@"hsdValue"],
                                          kType:@"KN550",
                                          kMAC:mac
                                          };
                }else{
                    
                    
                   dic = @{
                                          kMEASUREMENT_DATE_BP: dateStr,
                                          kLOW_BLOOD_PRESSURE_BP: [history objectForKey:@"dia"],
                                          kHIGH_BLOOD_PRESSURE_BP: [history objectForKey:@"sys"],
                                          kMEASUREMENT_AHR_BP: [history objectForKey:@"irregular"],
                                          kPULSE_BP: [history objectForKey:@"heartRate"],
                                          kDATAID: [history objectForKey:@"dataID"],
                                          kType:@"KN550",
                                          kMAC:mac
                                          
                                          };
                }
                
                
                
                [tempArr addObject:dic];
                
            }
            
            if (tempArr.count > 0) {
                NSDictionary* response = @{
                                           kACTION:kACTION_HISTORICAL_DATA_BP,
                                           kHISTORICAL_DATA_BP:[tempArr copy],
                                           kType:@"KN550",
                                           kMAC:mac
                                           };
                [BPProfileModule sendEventToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithDict:response];
            }

        } errorBlock:^(BPDeviceError error) {
            [BPProfileModule sendErrorToEmitter:weakSelf eventNotify:EVENT_NOTIFY WithCode:error];
        }];
    
    }else{
        
        [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
    }
    
    
}

RCT_EXPORT_METHOD(disconnect:(nonnull NSString *)mac){
    
    if ([self getDeviceWithMac:mac]!=nil) {
        
        [[self getDeviceWithMac:mac] commandDisconnectDevice];
        
    }else{
      
        [BPProfileModule sendErrorToEmitter:self eventNotify:EVENT_NOTIFY WithCode:BPDidDisconnect];
    
    }
    
    
}


@end