summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BG5SModule.m
blob: 085795630ec72123c36d1186949bc8a48594fc37 (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
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
//
//  BG5SModule.m
//  ReactNativeIOSLibrary
//
//  Created by soso on 2019/4/24.
//  Copyright © 2019 daiqingquan. All rights reserved.
//

#import "BG5SModule.h"

#import "BG5SProfileModule.h"
#import "BGMacroFile.h"
#import "BG5SController.h"
#import "BG5S.h"
#import "ManageDeviceController.h"


#define EVENT_NOTIFY    @"event_notify_bg5s"
#define kMAC_KEY        @"mac"
#define kACTION_KEY     @"action"

#define kFUNCTION_BatteryValue @"batteryValue"
#define kFUNCTION_DeviceDate @"deviceDate"
#define kFUNCTION_DeviceTimeZone @"deviceTimeZone"
#define kFUNCTION_StripUsedValue @"stripUsedValue"
#define kFUNCTION_OfflineDataQuantity @"offlineDataQuantity"
#define kFUNCTION_BloodCodeVersion @"bloodCodeVersion"
#define kFUNCTION_CtlCodeVersion @"ctlCodeVersion"
#define kFUNCTION_Unit @"unit"


@interface BG5SModule ()<BG5SDelegate>
@property (nonatomic, assign) BOOL isMeasuring;

@end
@implementation BG5SModule
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;
}


-(BG5S*)getDeviceWithMac:(NSString*)mac{
    
    BG5SController *controller = [BG5SController sharedController];
    NSArray *bgDeviceArray = [controller getAllCurrentInstace];
    
    for(BG5S *tempDevice in bgDeviceArray){
        if([mac isEqualToString:tempDevice.serialNumber]){
            tempDevice.delegate = self;
            return tempDevice;
        }
    }
    return nil;
}

RCT_EXPORT_METHOD(getAllConnectedDevices){
    
    
    NSArray*bgDeviceArray= [[BG5SController sharedController] getAllCurrentInstace];
    
    NSMutableArray*deviceMacArray=[NSMutableArray array];
    
    for (int i=0; i<[bgDeviceArray count]; i++) {
        
        BG5S *bg5s = [bgDeviceArray objectAtIndex:i];
        
        [deviceMacArray addObject:bg5s.serialNumber];
        
    }
    
    NSDictionary* deviceInfo = @{kACTION_KEY:@"ACTION_GET_ALL_CONNECTED_DEVICES",@"devices":deviceMacArray};
    
    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}


//综合查询
RCT_EXPORT_METHOD(getStatusInfo:(nonnull NSString *)mac){
    
    if ([self getDeviceWithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac] queryStateInfoWithSuccess:^(BG5SStateInfo *stateInfo) {
            
            
            NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
            [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateStr = [mydateFormatter stringFromDate:stateInfo.deviceDate];
            
            NSDictionary* response = @{
                                       kMAC_KEY:mac,
                                       kACTION_KEY:kACTION_GET_STATUS_INFO,
                                       INFO_BATTERY_LEVEL:@(stateInfo.batteryValue),
                                       INFO_TIME:dateStr,
                                       INFO_TIMEZONE:@(stateInfo.deviceTimeZone),
                                       INFO_USED_STRIP:@(stateInfo.stripUsedValue),
                                       INFO_OFFLINE_DATA_NUM:@(stateInfo.offlineDataQuantity),
                                       INFO_CODE_VERSION_BLOOD:@(stateInfo.bloodCodeVersion),
                                       INFO_CODE_VERSION_CTL:@(stateInfo.ctlCodeVersion),
                                       INFO_UNIT:(stateInfo.unit == BGUnit_mmolPL)?@"mmol":((stateInfo.unit == BGUnit_mgPmL)?@"mg":@"unknown"),
                                       };
            
            [weakSelf sendEventWithName:EVENT_NOTIFY body:response];
            
            
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
        }];
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
    }
}

//同步时间
RCT_EXPORT_METHOD(setTime:(nonnull NSString *)mac date:(nonnull NSString *)date timezone:(nonnull NSNumber *)timezone){
    if ([self getDeviceWithMac:mac]) {
        
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
               
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
               
        NSDate*mydate=[dateFormatter dateFromString:date];


        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac] setTimeWithDate:mydate timezone:[timezone floatValue] successBlock:^{
            [weakSelf sendEventWithName:EVENT_NOTIFY body:@{
                                                                                         kMAC_KEY:mac,
                                                                                         kACTION_KEY:kACTION_SET_TIME,
                                                                                         }];
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
        }];
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
    }
}

////同步时间
//RCT_EXPORT_METHOD(setTime:(nonnull NSString *)mac){
//    if ([self getDeviceWithMac:mac]) {
//
//        __weak typeof(self) weakSelf = self;
//
//        NSString *zone = [[NSTimeZone systemTimeZone] description];
//        NSString *time = [[zone componentsSeparatedByString:@"offset "] objectAtIndex:1];
//        float floatTimeZone = time.floatValue/3600;
//
//        [[self getDeviceWithMac:mac] setTimeWithDate:[NSDate date] timezone:floatTimeZone successBlock:^{
//            [weakSelf sendEventWithName:EVENT_NOTIFY body:@{
//                                                                                         kMAC_KEY:mac,
//                                                                                         kACTION_KEY:kACTION_SET_TIME,
//                                                                                         }];
//        } errorBlock:^(BG5SError error, NSString *detailInfo) {
//            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
//        }];
//    }else{
//        [self sendNoMatchedDeviceEventWithMac:mac];
//    }
//}

//设置单位
RCT_EXPORT_METHOD(setUnit:(nonnull NSString *)mac type:(nonnull NSNumber *)type){
    if ([self getDeviceWithMac:mac]) {
        
        BGUnit tempUnit = BGUnit_mmolPL;
        if ([type isEqual:@(BGUnit_mmolPL)]) {
            tempUnit = BGUnit_mmolPL;
        } else if([type isEqual:@(BGUnit_mgPmL)]) {
            tempUnit = BGUnit_mgPmL;
        } else {
            [self sendNoMatchedDeviceEventWithMac:mac];
            return;
        }
        __weak typeof(self) weakSelf = self;
        
        [[self getDeviceWithMac:mac] setUnit:tempUnit successBlock:^{
            [weakSelf sendEventWithName:EVENT_NOTIFY body:@{
                                                                                         kMAC_KEY:mac,
                                                                                         kACTION_KEY:kACTION_SET_UNIT,
                                                                                         }];
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
            
        }];
        
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
    }
}

//设置离线模式  Is offline measurement allowed    0:allowed  1:Offline measurement is not allowed
RCT_EXPORT_METHOD(setOfflineModel:(nonnull NSString *)mac type:(nonnull NSNumber *)type){
    if ([self getDeviceWithMac:mac]) {
        
        BOOL modelType=YES;
        
        if ([type intValue]==0) {
            modelType=NO;
        }else{
            
            modelType=YES;
        }
        
        
        __weak typeof(self) weakSelf = self;
        
        
        [[self getDeviceWithMac:mac] setIsOfflineMeasurementAllowed:modelType successBlock:^{
             [weakSelf sendEventWithName:EVENT_NOTIFY body:@{
                                                                                                    kMAC_KEY:mac,
                                                                                                    kACTION_KEY:kACTION_SET_OFFINEMODEL,
                                                                                                    }];
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            
             [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
        }];
        
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
    }
}


//删除试条使用条数
RCT_EXPORT_METHOD(deleteUsedStrip:(nonnull NSString *)mac){
    if ([self getDeviceWithMac:mac]) {
        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac] deleteStripUsedInfoWithSuccessBlock:^{
            [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                     kMAC_KEY:mac,
                                                                                     kACTION_KEY:kACTION_DELETE_USED_STRIP,
                                                                                     }];
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
        }];
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
    }
}

//删除离线数据
RCT_EXPORT_METHOD(deleteOfflineData:(nonnull NSString *)mac){
    if ([self getDeviceWithMac:mac]) {
        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac] deleteRecordWithSuccessBlock:^{
            [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                     kMAC_KEY:mac,
                                                                                     kACTION_KEY:kACTION_DELETE_OFFLINE_DATA,
                                                                                     }];
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
            
        }];
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
    }
}


//同步离线数据
RCT_EXPORT_METHOD(getOfflineData:(nonnull NSString *)mac){
    
    if ([self getDeviceWithMac:mac]) {
        __weak typeof(self) weakSelf = self;
        [[self getDeviceWithMac:mac] queryRecordWithSuccessBlock:^(NSArray *array) {
            
            NSMutableArray * tempArr = [[NSMutableArray alloc]init];
            
            for(BG5SRecordModel *model in array) {
                NSDate *tempDate = model.measureDate;
                BOOL flag = model.canCorrect;
                
                //将时间格式转化成字符串,适配plugin和react native
                NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
                [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSString *dateStr = [mydateFormatter stringFromDate:tempDate];
                
                NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:dateStr,@"data_measure_time",@(model.value),@"data_value",model.dataID,@"dataID",@(model.timeZone),@"data_measure_timezone",flag,@"data_time_proof", nil];
                
                [tempArr addObject:dic];
            }
            
            NSDictionary*hisDic=[NSDictionary dictionaryWithObjectsAndKeys:tempArr,@"history", nil];
            
            
            NSDictionary *deviceInfo = @{@"mac":mac,@"action":kACTION_GET_OFFLINE_DATA,kGET_OFFLINEDATA:hisDic,@"type":@"BG5S"};
            
            [weakSelf sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];
        }];
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
        
    }
}


//开始测量
RCT_EXPORT_METHOD(startMeasure:(nonnull NSString *)mac type:(nonnull NSNumber *)type){
    if ([self getDeviceWithMac:mac]) {
        __weak typeof(self) weakSelf = self;
        
        BGMeasureMode measureMode = BGMeasureMode_Blood;
        if ([type isEqual:@(BGMeasureMode_Blood)]) {
            measureMode = BGMeasureMode_Blood;
        } else if([type isEqual:@(BGMeasureMode_NoBlood)]) {
            measureMode = BGMeasureMode_NoBlood;
        } else {
            [self sendNoMatchedDeviceEventWithMac:mac];
            return;
        }
        
        [[self getDeviceWithMac:mac] startMeasure:measureMode withSuccessBlock:^{
            [weakSelf sendEventWithName:EVENT_NOTIFY body:@{
                                                                                     kMAC_KEY:mac,
                                                                                     kACTION_KEY:kACTION_START_MEASURE,
                                                                                     }];
        } errorBlock:^(BG5SError error, NSString *detailInfo) {
            [weakSelf sendMeasureErrorEventWithMac:mac errorId:error];

        }];
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];
    }
}

- (void)device:(BG5S *)device occurError:(BG5SError)error errorDescription:(NSString *)errorDescription{
    NSLog(@"下位机主发的错误信息:%d",(int)error);
    [self sendMeasureErrorEventWithMac:device.serialNumber errorId:error];
    
}
- (void)device:(BG5S *)device stripStateDidUpdate:(BG5SStripState)state{
    NSLog(@"试条状态:%@",(state == BG5SStripState_Insert)?@"插入":@"拔出");
    
    if (state == BG5SStripState_Insert) {
        [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                 kMAC_KEY:device.serialNumber,
                                                                                 kACTION_KEY:kACTION_STRIP_IN,
                                                                                 }];
    }else{
        [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                     kMAC_KEY:device.serialNumber,
                                                                                     kACTION_KEY:kACTION_STRIP_OUT,
                                                                                     }];
    }
    
  
}
- (void)deviceDropBlood:(BG5S *)device{
    NSLog(@"滴血");
    [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                             kMAC_KEY:device.serialNumber,
                                                                             kACTION_KEY:kACTION_GET_BLOOD,
                                                                             }];
}
- (void)device:(BG5S *)device dataID:(NSString *)dataID measureReult:(NSInteger)result{
    NSLog(@"结果:%d",(int)result);
//    NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:@(result),@"value",dataID,@"dataID", nil];
    
    [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                             kMAC_KEY:device.serialNumber,
                                                                             RESULT_VALUE:@(result),
                                                                             kDATA_ID:dataID,
                                                                             
                                                                             kACTION_KEY:kACTION_RESULT,
                                                                             }];
}
- (void)device:(BG5S *)device chargeStateDidUpdate:(BG5SChargeState)state{
    NSLog(@"充电线状态:%@",(state == BG5SChargeState_Charging)?@"插入":@"拔出");
    
    if (state == BG5SChargeState_Charging) {
        [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                 kMAC_KEY:device.serialNumber,
                                                                                 kACTION_KEY:kACTION_ENTER_CHARGED_STATE,
                                                                                 }];
    }else{
        [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                 kMAC_KEY:device.serialNumber,
                                                                                 kACTION_KEY:kACTION_LEAVE_CHARGED_STATE,
                                                                                 }];
        
    }

}


//断开连接
RCT_EXPORT_METHOD(disConnect:(nonnull NSString *)mac){
    if ([self getDeviceWithMac:mac]!=nil) {
        
        [[self getDeviceWithMac:mac] disconnectDevice];
    }else{
        
    }
}

//开始离线数据修正
RCT_EXPORT_METHOD(adjustOfflineData:(nonnull NSString *)mac timeString:(nonnull NSString *)timeString array:(nonnull NSArray *)array ){
    if ([self getDeviceWithMac:mac]!=nil) {
        
        if (array.count > 0 ) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *date = [formatter dateFromString:timeString];
            
            NSArray *correctArray = [[self getDeviceWithMac:mac] processData:array deviceDate:date];
        }
        
        [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                 kMAC_KEY:mac,
                                                                                kACTION_KEY:kACTION_ADJUST_OFFLINE_DATA,
                                                                                 }];
        
    }else{
        [self sendNoMatchedDeviceEventWithMac:mac];

    }
}

- (void)sendMeasureErrorEventWithMac:(NSString *)mac errorId:(BG5SError)errorId{
    
    
    NSString *errorDescription = @"";
    
    switch (errorId) {
        case 0:
            errorDescription = @"Only showed in BG5S's screen and need charging.";
            break;
        case 1:
            errorDescription = @"Strip removed in the middle of reading, repeat the test with a new strip.";
            break;
        case 2:
            errorDescription = @"Reference voltage error, not normal measurement, please repeat the test.";
            break;
        case 3:
            errorDescription = @"Strip is used or unknown moisture detected, discard the test strip and repeat the test with a new strip.";
            break;
        case 4:
            errorDescription = @"Reading transmission error. Repeat the test with a new test strip. If the problem persists, contact iHealth customer service for assistance.";
            break;
        case 5:
            errorDescription = @"The environmental temperature is beyond normal range, place the meter at room temperature for at least 30 minutes, then repeat the test.";
            break;
        case 6:
            errorDescription = @"The environmental temperature is beyond normal range, place the meter at room temperature for at least 30 minutes, then repeat the test.";
            break;
        case 7:
            errorDescription = @"Only showed in BG5S's screen.";
            break;
        case 8:
            errorDescription = @"Glucose test result is low.";
            break;
        case 9:
            errorDescription = @"Glucose test result is high.";
            break;
        case 10:
            errorDescription = @"Reset and if the problem persists, contact iHealth customer service for assistance.";
            break;
        case 400:
            errorDescription = @"arameter input error.";
            break;
        case 401:
            errorDescription = @"Quantity not match";
            break;
        case 402:
            errorDescription = @"Single packet is not full.";
            break;
        case 403:
            errorDescription = @"Packet index not match.";
            break;
        case 500:
            errorDescription = @"Command timeout";
            break;
        case 501:
            errorDescription = @"Command is not supported for current device.";
            break;
        default:
            break;
    }
    
    [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                             kMAC_KEY:mac,
                                                                             kACTION_KEY:kACTION_ERROR_BG,
                                                                             kERROR_NUM_BG:@(errorId),
                                                                             kERROR_DESCRIPTION_BG:errorDescription
                                                                             }];
}


- (void)sendNoMatchedDeviceEventWithMac:(NSString *)mac{
    if (mac && mac.length > 0) {
        [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                 kMAC_KEY:mac,
                                                                                 kACTION_KEY:kACTION_ERROR_BG,
                                                                                 kERROR_NUM_BG:@100
                                                                                 }];
    } else {
        [self sendEventWithName:EVENT_NOTIFY body:@{
                                                                                 kACTION_KEY:kACTION_ERROR_BG,
                                                                                
                                                                                 kERROR_NUM_BG:@100,
                                                                                 kERROR_DESCRIPTION_BG:@"Device disconnect."

                                                                                 }];
    }
}



@end