summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BG1SModule.m
blob: f8410370b01a0155f0ffdac7043a3c58b865a491 (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
//
//  BG1SModule.m
//  ReactNativeIOSLibrary
//
//  Created by user on 2019/11/12.
//  Copyright © 2019 daiqingquan. All rights reserved.
//

#import "BG1SModule.h"
#import "BG1S.h"
#import "BG1SController.h"
#import "BG1SProfileModule.h"

#import "BGHeader.h"

@implementation BG1SModule
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": BG1S_EVENT_NOTIFY ,
              
    };
}

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}


-(BG1S*)getDeviceWithMac:(NSString*)mac{
    
    BG1SController *controller = [BG1SController shareIHBG1SController];
    NSArray *BG1SDeviceArray = [controller getAllCurrentBG1SInstace];
    
    for(BG1S *tempDevice in BG1SDeviceArray){
        if([mac isEqualToString:tempDevice.serialNumber]){
            
            return tempDevice;
        }
    }
    return nil;
}

RCT_EXPORT_METHOD(getAllConnectedDevices){
    
    
    NSArray*BG1SDeviceArray= [[BG1SController shareIHBG1SController] getAllCurrentBG1SInstace];
    
    NSMutableArray*deviceMacArray=[NSMutableArray array];
    
    for (int i=0; i<[BG1SDeviceArray count]; i++) {
        
        BG1S *BG1S = [BG1SDeviceArray objectAtIndex:i];
        
        [deviceMacArray addObject:BG1S.serialNumber];
        
    }
    
    NSDictionary* deviceInfo = @{BG1S_ACTION:kACTION_GET_ALL_CONNECTED_DEVICES,BG1S_DEVICE:deviceMacArray};
    
    [self sendEventWithName:BG1S_EVENT_NOTIFY body:deviceInfo];
}

RCT_EXPORT_METHOD(measure:(nonnull NSString *)mac measureMode:(nonnull NSNumber *)testType){
    
    
    if ([self getDeviceWithMac:mac] != nil) {
        
        BGMeasureMode models= BGMeasureMode_Blood;
        
        if ([testType intValue]==1) {
            
            models=BGMeasureMode_NoBlood;
            
        }else{
            
            models=BGMeasureMode_Blood;
        }
        
        [[self getDeviceWithMac:mac] commandCreateBG1STestModel:models DisposeBGStripInBlock:^(BOOL inORout) {
            
            if (inORout) {
                [self sendEventWithName:BG1S_EVENT_NOTIFY body:@{
                    BG1S_ACTION:ACTION_STRIP_INSERTION_STATUS,
                    BG1S_KEY_MAC:mac,
                    BG1S_TYPE:@"BG1S",
                    OPERATION_DESCRIBE:@"strip in",
                    STRIP_INSERTION_STATUS:@1,
                }];
            }else{
                
                [self sendEventWithName:BG1S_EVENT_NOTIFY body:@{
                    BG1S_ACTION:ACTION_STRIP_INSERTION_STATUS,
                    BG1S_KEY_MAC:mac,
                    BG1S_TYPE:@"BG1S",
                    OPERATION_DESCRIBE:@"strip put",
                    STRIP_INSERTION_STATUS:@2,
                }];
            }
            
        } DisposeBGBloodBlock:^{
            
            [self sendEventWithName:BG1S_EVENT_NOTIFY body:@{
                BG1S_ACTION:ACTION_GET_BLOOD,
                BG1S_KEY_MAC:mac,
                BG1S_TYPE:@"BG1S",
                OPERATION_DESCRIBE:@"get blood"
            }];
            
        } DisposeBGResultBlock:^(NSDictionary *result) {
            
            
            [self sendEventWithName:BG1S_EVENT_NOTIFY body:@{
                BG1S_ACTION:ACTION_MEASURE_RESULT,
                BG1S_KEY_MAC:mac,
                BG1S_TYPE:@"BG1S",
                MEASURE_RESULT:[result valueForKey:@"Result"],
                MEASURE_MODE:[result valueForKey:@"TestState"]
            }];
            
        } DisposeBGErrorBlock:^(BG1SDeviceError error) {
            
            [self sendBG1SErrorCode:error mac:mac];
            
        }];
        
        
    }
    
}

//getFunction
RCT_EXPORT_METHOD(getFunction:(nonnull NSString *)mac){
    if ([self getDeviceWithMac:mac]!=nil) {
        
        
        [[self getDeviceWithMac:mac] commandFunction:^(NSDictionary *functionDict) {
            
            
            [self sendEventWithName:BG1S_EVENT_NOTIFY body:@{
                BG1S_ACTION:ACTION_CODE_ANALYSIS,
                BG1S_KEY_MAC:mac,
                BG1S_TYPE:@"BG1S",
                INFO_BATTERY_BG1S:[functionDict valueForKey:@"Battary"],
                INFO_VERSION_CODE_BLOOD_BG1S:[functionDict valueForKey:@"CodeVersion"],
                INFO_VERSION_CODE_CTL_BG1S:[functionDict valueForKey:@"DcodeVersion"],
                
            }];
            
        } DisposeBGErrorBlock:^(BG1SDeviceError error) {
            
            
            [self sendBG1SErrorCode:error mac:mac];
            
        }];
        
        
    }
}

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

-(void)sendBG1SErrorCode:(BG1SDeviceError)errorID mac:(NSString*)mac{
    
                NSString *errorMassage = [NSString string];
                switch (errorID) {
                    case 0:
                         errorMassage = @"BG1SDeviceError0";
                        break;
                    case 1:
                        errorMassage = @"BG1SError_LowBattery";
                        break;
                    case 2:
                         errorMassage = @"BG1SError_ReferenceUnstable";
                        break;
                    case 3:
                        errorMassage = @"BG1SError_BadStrip";
                        break;
                    case 4:
                        errorMassage = @"BG1SError_BadEEPROM";
                        break;
                    case 5:
                         errorMassage = @"BG1SError_LowAmbientTemperature";
                        break;
                    case 6:
                        errorMassage = @"BG1SError_HighAmbientTemperature";
                        break;
                    case 7:
                        errorMassage = @"BG1SError_BleedEarly";
                        break;
                    case 8:
                       errorMassage = @"BG1SError_Other";
                        break;
                        

                    default:
                        break;
                }
    
                NSDictionary *deviceInfo = @{BG1S_KEY_MAC:mac,BG1S_ACTION:ACTION_ERROR_BG1S,ERROR_NUM_BG1S:[NSNumber numberWithInt:errorID],ERROR_DESCRIPTION_BG1S:errorMassage};
                [self sendEventWithName:BG1S_EVENT_NOTIFY body:deviceInfo];
    
    
}

@end