summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/ECGModule.m
blob: 9cc04ea2bd31db6b781c8c4ad78571460041957c (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
//
//  ECGModule.m
//  ReactNativeIOSLibrary
//
//  Created by daiqingquan on 2018/1/5.
//  Copyright © 2018年 daiqingquan. All rights reserved.
//

#import "ECGModule.h"
#import "ECGHeader.h"
#import "ECGProfileModule.h"
#import "ManageDeviceController.h"

#define EVENT_NOTIFY @"event_notify_ecg"

@implementation ECGModule


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"];
}


#pragma mark
#pragma mark - constantsToExport
- (NSDictionary *)constantsToExport{
    return @{@"Event_Notify": EVENT_NOTIFY };
    
}

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}
#pragma mark
#pragma mark - Init
-(id)init{
    if (self=[super init]){
    }
    return self;
}

-(ECG3*)getECG3WithMac:(nonnull NSString*)mac{
    
    ECG3Controller *controller = [ECG3Controller shareECG3Controller];
    NSArray *ecgDeviceArray = [controller getAllCurrentECG3Instace];
    
    for(ECG3 *tempECG3 in ecgDeviceArray){
        if([mac isEqualToString:tempECG3.serialNumber]){
            return tempECG3;
        }
    }
    
    return nil;
}

#pragma mark -
#pragma mark - Method

RCT_EXPORT_METHOD(getAllConnectedDevices){
    
    
    NSArray*ecg3array= [[ECG3Controller shareECG3Controller] getAllCurrentECG3Instace];
    
    NSMutableArray*deviceMacArray=[NSMutableArray array];
    
    for (int i=0; i<[ecg3array count]; i++) {
        
        ECG3 *ecg3=[ecg3array objectAtIndex:i];
        [deviceMacArray addObject:ecg3.serialNumber];
    }
    
    NSDictionary* deviceInfo = @{kACTION:kACTION_GET_ALL_CONNECTED_DEVICES,kDEVICES:deviceMacArray};
    
    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
    
    
}



RCT_EXPORT_METHOD(getBattery:(nonnull NSString *)mac){
    
    if ([self getECG3WithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getECG3WithMac:mac] commandECG3GetBatteryInfo:^(NSNumber *battery) {
            
            NSDictionary* deviceInfo = @{kACTION:kACTION_BATTERY_ECG,kBATTERY_ECG:battery,kMAC:mac,kType:@"ECG3"};
            [weakSelf sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withErrorBlock:^(ECG3ErrorID errorID) {
            [weakSelf commandReturnECGError:errorID MAC:mac];
        }];
    }else{
        
        [self commandReturnECGError:ECG3Error_Disconnect MAC:mac];
        
    }
    
    
}

RCT_EXPORT_METHOD(sysTime:(nonnull NSString *)mac){
    
    if ([self getECG3WithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getECG3WithMac:mac] commandECG3SyncTime:^{
            
            NSDictionary* deviceInfo = @{kACTION:kACTION_SYSTIME,kMAC:mac,kType:@"ECG3"};
            [weakSelf sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withErrorBlock:^(ECG3ErrorID errorId) {
            [weakSelf commandReturnECGError:errorId MAC:mac];
        }];
       
    }else{
        [self commandReturnECGError:ECG3Error_Disconnect MAC:mac];
    }
}

RCT_EXPORT_METHOD(stopMeasure:(nonnull NSString *)mac){
    
    
    if ([self getECG3WithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getECG3WithMac:mac] commandECG3FinishMeasure:^{
            
            
            NSDictionary* deviceInfo = @{kACTION:kACTION_STOPMEASURE_ECG,kMAC:mac,kType:@"ECG3"};
            [weakSelf sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withErrorBlock:^(ECG3ErrorID errorId) {
            [weakSelf commandReturnECGError:errorId MAC:mac];
        }];
        
    }else{
        
        [self commandReturnECGError:ECG3Error_Disconnect MAC:mac];
    }
    
}


RCT_EXPORT_METHOD(startMeasure:(nonnull NSString *)mac){
    
    if ([self getECG3WithMac:mac]!=nil) {
        __weak typeof(self) weakSelf = self;
        [[self getECG3WithMac:mac] commandECG3StartMeasure:^{
            
        } withWaveData:^(NSArray *waveDataArray) {
            
            NSDictionary* deviceInfo = @{kACTION:kACTION_MEASURE_WAVEData,kMEASURE_WAVEData:waveDataArray,kMAC:mac,kType:@"ECG3"};
            [weakSelf sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withPulseResult:^(BOOL hasHR, NSUInteger HR) {
            
            NSDictionary* deviceInfo = @{kACTION:kACTION_MEASURE_ECGPulse,kMEASURE_ECGPulse:@(HR),kMAC:mac,kType:@"ECG3"};
            [weakSelf sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withErrorBlock:^(ECG3ErrorID errorId) {
            
            if (errorId>= ECG3Error_ElectrodeLoss && errorId <= ECG3Error_ElectrodeLossTimeout) {
                NSDictionary* deviceInfo = @{kACTION:kACTION_ELECTRODE_STATUS,kELECTRODE_STATUS:@(errorId),kMAC:mac,kType:@"ECG3"};
                [weakSelf sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            } else {
                [weakSelf commandReturnECGError:errorId MAC:mac];
            }
            
        }];
        
        
    }else{
        [self commandReturnECGError:ECG3Error_Disconnect MAC:mac];
    }
    
}

RCT_EXPORT_METHOD(disconnect:(nonnull NSString *)mac){
    if ([self getECG3WithMac:mac]!=nil) {
        [[ManageDeviceController commandGetInstance] commandDisconnectSelectDevice:[self getECG3WithMac:mac].currentUUID];
    }
}

-(void)commandReturnECGError:(ECG3ErrorID)errorID MAC:(NSString *)MAC{
    
    
    NSMutableDictionary*deviceInfo=[NSMutableDictionary dictionary];
    
    [deviceInfo setValue:kACTION_ERROR_ECG forKey:kACTION];
    
    NSString*errorDes=[NSString string];
    
    switch (errorID) {
        case ECG3Error_ElectrodeLoss:
            errorDes=@"Electrode Loss";
            break;
        case ECG3Error_ElectrodeLossRecovery:
            errorDes=@"Electrode Loss Recovery";
            break;
        case ECG3Error_ElectrodeLossTimeout:
            errorDes=@"Electrode Loss Timeout";
            break;
        case ECG3Error_SDCardCommunicationError:
            errorDes=@"SDCard Communication Error";
            break;
        case ECG3Error_SampleModuleError:
            errorDes=@"Sample Module Error";
            break;
        case ECG3Error_LowPower:
            errorDes=@"Low Power";
            break;
        case ECG3Error_DeviceMemoryFull:
            errorDes=@"Device Memory Full";
            break;
        case ECG3Error_Disconnect:
            errorDes=@"Disconnect";
            break;
        case ECG3Error_ParameterError:
            errorDes=@"Parameter Error";
            break;
        case ECG3Error_CommandTimeout:
            errorDes=@"Command timeout";
            break;
        default:
            break;
    }
    
    [deviceInfo setValue:[NSNumber numberWithInt:errorID] forKey:kERROR_NUM_ECG];
    
    [deviceInfo setValue:errorDes forKey:kERROR_DESCRIPTION_ECG];
    
    [deviceInfo setValue:@"ECG3" forKey:kType];
    if (MAC) {
        [deviceInfo setValue:MAC forKey:kMAC];
    }
    
    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}

@end