summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS2Module.m
blob: 376f2f3a86c6a832501b97b1a2c0b5f357ae2623 (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
//
//  HS2Module.m
//  ReactNativeIOSLibrary
//
//  Created by jing on 2018/11/26.
//  Copyright © 2018年 daiqingquan. All rights reserved.
//

#import "HS2Module.h"
#import "HSProfileModule.h"
#import "HSMacroFile.h"
#import "HS2Controller.h"
#import "HS2.h"
#import "iHealthDeviceManagerModule.h"
#import "ManageDeviceController.h"

#define EVENT_NOTIFY @"HS2.MODULE.NOTIFY" 

@implementation HS2Module

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-init

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

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}

-(HS2 *)getHS2WithMac:(NSString *)mac{
    HS2Controller *controller = [HS2Controller shareIHHs2Controller];
    NSArray *hs2DeviceArray = [controller getAllCurrentHS2Instace];
    for (HS2 *tempHS2 in  hs2DeviceArray) {
        if ([mac isEqualToString:tempHS2.deviceID]){
            return tempHS2;
            break;
        }
    }
    return nil;
}

#pragma mark
#pragma mark - Method


#pragma mark-获取连接设备
RCT_EXPORT_METHOD(getAllConnectedDevices){
    
    
    NSArray*hs2array= [[HS2Controller shareIHHs2Controller] getAllCurrentHS2Instace];
    
    NSMutableArray*deviceMacArray=[NSMutableArray array];
    
    for (int i=0; i<[hs2array count]; i++) {
        
        HS2*hs2=[hs2array objectAtIndex:i];
        
        [deviceMacArray addObject:hs2.deviceID];
        
    }
    
    NSDictionary* deviceInfo = @{@"action":@"action_get_all_connected_devices",@"devices":deviceMacArray};
    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
    
    
}

RCT_EXPORT_METHOD(getBattery:(nonnull NSString*)mac){
    if ([self getHS2WithMac:mac] != nil) {
        
        [[self getHS2WithMac:mac]commandGetHS2Battery:^(NSNumber *battary) {
           
            NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_BATTARY_HS,BATTERY_HS:battary};
            
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } DiaposeErrorBlock:^(HS2DeviceError errorID) {
            
        }];
        
    }
}
RCT_EXPORT_METHOD(getOfflineData:(nonnull NSString*)mac){
    if ([self getHS2WithMac:mac] != nil) {
        
        NSLog(@"上传离线数据");
        
        [[self getHS2WithMac:mac]commandHS2TransferMemorryData:^(NSDictionary *startDataDictionary) {
            
        } DisposeProgress:^(NSNumber *progress) {
            
        } MemorryData:^(NSArray *historyDataArray) {
            if(historyDataArray != nil){
                
                if(historyDataArray.count){
                    
                    NSMutableArray*dataArray=[NSMutableArray array];
                    
                    for (int i=0; i<historyDataArray.count; i++) {
                        
                        NSDate *tempDate = [[historyDataArray objectAtIndex:i] objectForKey:@"date"];
                        
                        
                        NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
                        [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                        NSString *dateStr = [mydateFormatter stringFromDate:tempDate];
                        
                        
                        NSDictionary*dic=@{DATAID:[[historyDataArray objectAtIndex:i] objectForKey:@"dataID"],MEASUREMENT_DATE_HS:dateStr,WEIGHT_HS:[[historyDataArray objectAtIndex:i] objectForKey:@"weight"]};
                        
                        [dataArray addObject:dic];
                    }
                    
                    
                    NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_HISTORICAL_DATA_HS,HISTORDATA_HS:dataArray};
                    
                    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
                    NSLog(@"historyDataArray:%@",historyDataArray);
                    
                }
                
            }
        } FinishTransmission:^{
            NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_HISTORICAL_DATA_COMPLETE_HS};
            
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        } DisposeErrorBlock:^(HS2DeviceError errorID) {
            NSString *errorMassage = [NSString string];
            NSLog(@"errorID:%d",errorID);
            switch (errorID) {
                case 1:
                    errorMassage = @"Battery level is low";
                    break;
                case 2:
                    errorMassage = @"The Scale failed to initialize.";
                    break;
                case 5:
                    errorMassage = @"Bluetooth connection error.";
                    break;
                case 7:
                    errorMassage = @"Invalidate.";
                    break;
                case 8:
                    errorMassage = @"Scale memory access error.";
                    break;
                case 9:
                    errorMassage = @"No History Data.";
                    break;
                case 10:
                    errorMassage = @"Device disconnect.";
                    break;
                    //                case 11:
                    //                errorMassage = @"Communication error.";
                    //                break;
                default:
                    break;
            }
            if(errorID == 9){
                NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_NO_HISTORICALDATA,ERROR_DESCRIPTION_HS:@"No History Data."};
                
                [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            }
            else if(errorID != 11){
                NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_ERROR_HS,ERROR_NUM_HS:errorMassage};
                
                [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];}
        }];
       
    }
}


RCT_EXPORT_METHOD(measureOnline:(nonnull NSString*)mac :(nonnull NSNumber*)unit :(nonnull NSNumber*)userId){
    if ([self getHS2WithMac:mac] != nil) {
        HSUnit tempUnit;
        
        switch ([unit intValue]) {
            case 1:
                tempUnit=HSUnit_Kg;
                break;
            case 2:
                tempUnit=HSUnit_LB;
                break;
            case 3:
                tempUnit=HSUnit_ST;
                break;
            default:
                 tempUnit=HSUnit_Kg;
                break;
        }
        
        [[self getHS2WithMac:mac]commandHS2MeasureWithUint:tempUnit Weight:^(NSNumber *unStableWeight) {
            NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_LIVEDATA_HS,LIVEDATA_HS:unStableWeight};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        } StableWeight:^(NSDictionary *StableWeightDic) {
            NSDictionary *deviceInfo =@{@"mac":mac,@"action":ACTION_ONLINE_RESULT_HS,DATAID:[StableWeightDic valueForKey:@"dataID"],WEIGHT_HS:[StableWeightDic valueForKey:@"Weight"] };
            
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        } DisposeErrorBlock:^(HS2DeviceError errorID) {
            NSString *errorMassage = [NSString string];
            switch (errorID) {
                case 1:
                    errorMassage = @"Battery level is low.";
                    break;
                case 2:
                    errorMassage = @"The Scale failed to initialize.";
                    break;
                case 3:
                    errorMassage = @"Maximum weight has been exceeded.";
                    break;
                case 4:
                    errorMassage = @"The Scale can't capture a steady reading.";
                    break;
                case 5:
                    errorMassage = @"Bluetooth connection error.";
                    break;
                case 6:
                    errorMassage = @"Movement while measuring.";
                    break;
                case 7:
                    errorMassage = @"Invalidate.";
                    break;
                case 10:
                    errorMassage = @"Device disconnect.";
                    break;
                case 11:
                    errorMassage = @"Communication error.";
                    break;
                case 12:
                    errorMassage = @"HS4DeviceRecWeightError.";
                    break;
                default:
                    break;
            }
            
            NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_ERROR_HS,ERROR_NUM_HS:errorMassage};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        }];
        
        
    }
    
}

RCT_EXPORT_METHOD(disconnect:(nonnull NSString*)mac){
    if ([self getHS2WithMac:mac] != nil){
        
        [[self getHS2WithMac:mac]commandDisconnectDevice];
        NSLog(@"End device connnect!");
        
    }else{
        
    }
}
- (void)sendErrorWithCode:(NSInteger)errorCode{
    [self sendEventWithAction:@"ACTION_ERROR_HS" keyString:@"value" valueString:@(errorCode)];
}

- (void)sendEventWithAction:(NSString*)actionName keyString:(NSString*)key valueString:(id)value{
    [self sendEventWithName:@"HS2.MODULE.NOTIFY"  body:@{@"action":actionName,key:value}];
}

@end