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

#import "BG1Module.h"
#import "BG1ProfileModule.h"
#import "BGMacroFile.h"
#import "BG1Controller.h"
#import "BG1.h"
#define EVENT_NOTIFY @"event_notify_bg1"

@implementation BG1Module


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_bg1",
             
             };
}
+ (BOOL)requiresMainQueueSetup
{
    return YES;
}

#pragma mark
#pragma mark - Init
-(id)init
{
    if (self=[super init])
    {
        
        [BG1Controller shareBG1Controller];
        
    }
    return self;
}



-(BG1*)getBG1Instance{
    
    BG1Controller *controller = [BG1Controller shareBG1Controller];
    BG1 *bg1Instance = [controller getCurrentBG1Instance];
    if(bg1Instance != nil)
    {
        return bg1Instance;
    }
    else
    {
        return nil;
    }
    
}


#pragma mark
#pragma mark - Method


RCT_EXPORT_METHOD(sendCode:(nonnull NSString *)QR:(nonnull NSNumber *)codeType:(nonnull NSNumber *)testType){
    
    
    if ([self getBG1Instance]!=nil) {
        
        if (QR ==nil || QR.length<30) {
            NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_error_for_bg1",@"action_measure_error_for_bg1":@400,@"description":@"Parameter input error."};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
            return;
        }
        
        NSDictionary *codeDic = [[self getBG1Instance]codeStripStrAnalysis:QR];
        NSNumber *bottleID = [codeDic objectForKey:@"bottleId"];
        NSDate *dueDate = [codeDic objectForKey:@"overDate"];
        NSNumber *remainNum = [codeDic objectForKey:@"stripNum"];
        
        BGMeasureMode bgMeasureModel = BGMeasureMode_Blood;
        if(codeType.integerValue == 2)
        {
            bgMeasureModel = BGMeasureMode_NoBlood;
        }
        
        BGCodeMode bgCodeModel =BGCodeMode_GOD;
        if(codeType.integerValue == 2)
        {
            bgCodeModel = BGCodeMode_GDH;
        }
        [[self getBG1Instance] commandBG1MeasureMode:bgMeasureModel withCodeMode:bgCodeModel withCodeString:QR withSendCodeResultBlock:^{
            
            NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_sendcode_result_for_bg1",@"set_bottle_message":@true};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withStripInBlock:^{
            
            NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_strip_in_for_bg1"};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withBloodBlock:^{
            
            NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_get_blood_for_bg1"};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withResultBlock:^(NSDictionary *result) {
            
            NSDate *measureDate = [result objectForKey:@"Date"];
            
            //将时间格式转化成字符串,适配plugin和react native
            NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
            [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateStr = [mydateFormatter stringFromDate:measureDate];
            
            NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:dateStr,@"date",[result objectForKey:@"Result"],@"value",[result objectForKey:@"dataID"],@"dataID", nil];
            
            NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_result_for_bg1",@"result":resultDic};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withStripOutBlock:^{
            
            NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_strip_out_for_bg1"};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withErrorBlock:^(BG1Error errorID) {
            
            
            NSString *descriptionStr = @"";
            
            switch (errorID) {
                case BG1Error_LowBattery:
                    descriptionStr=@"Battery is low.";
                    break;
                case BG1Error_ResultOutOfMeasurementRage:
                    descriptionStr=@"Glucose test result is out of the measurement range.";
                    break;
                case BG1Error_UnvalidReferenceVoltage:
                    descriptionStr=@"2.5V reference voltage error, not normal measurement, please repeat the test.";
                    break;
                case BG1Error_StripUsed:
                    descriptionStr=@"Strip is used or unknown moisture detected, discard the test strip and repeat the test with a new strip.";
                    break;
                case BG1Error_CodeError:
                    descriptionStr=@"CODE value check error. This error need let user scan code and call the send code function again,no alert need to show.";
                    break;
                case BG1Error_RoomTemperatureOutOfRange1:
                case BG1Error_RoomTemperatureOutOfRange2:
                    descriptionStr=@"The environmental temperature is beyond normal range, place the meter at room temperature for at least 30 minutes, then repeat the test.";
                    break;
                case BG1Error_ResultLow:
                    descriptionStr=@"Glucose test result is low.";
                    break;
                case BG1Error_ResultHigh:
                    descriptionStr=@"Glucose test result is high.";
                    break;
                case BG1Error_DisConnented:
                    descriptionStr=@"BG disConnented.";
                    break;
                case BG1Error_SleepingMode:
                    descriptionStr=@"BG sleeping mode.";
                    break;
                case BG1Error_ParameterError:
                    descriptionStr=@"Parameter input error.";
                    break;
                default:
                    descriptionStr=@"BG unknown.";
                    break;
            }
            
            NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_error_for_bg1",@"action_measure_error_for_bg1":[NSNumber numberWithInt:errorID],@"description":descriptionStr};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        }];
        
       
        
                
        
    }else{
        
        NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_error_for_bg1",@"action_measure_error_for_bg1":@100,@"description":@"BG disConnented." };
        [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        
    }
    
}





RCT_EXPORT_METHOD(getBottleInfoFromQR:(nonnull NSString *)QR){
    
    
    if ([self getBG1Instance]!=nil) {
        
        NSDictionary *codeDic = [[self getBG1Instance]codeStripStrAnalysis:QR];
        NSDate *tempDate = [codeDic objectForKey:@"DueDate"];
        
        //将时间格式转化成字符串,适配plugin和react native
        NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
        [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSTimeZone *dongBaTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:8*60*60];
        [mydateFormatter setTimeZone:dongBaTimeZone];
        NSString *dateStr = [mydateFormatter stringFromDate:tempDate];
        
        NSNumber *bottleID = [codeDic objectForKey:@"BottleID"];
        NSNumber *remainNum = [codeDic objectForKey:@"StripNum"];
        
        NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_code_analysis_bg",@"stripNum":remainNum,@"overDate":dateStr,@"bottleId":bottleID};
        [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        
    }else{
        
        NSDictionary* deviceInfo = @{@"mac":@"",@"action":@"action_measure_error_for_bg1",@"action_measure_error_for_bg1":@100,@"description":@"BG disConnented."};
        [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        
    }
    
}


@end