summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS4SModule.m
blob: 8538c6f391cd44bc804ea9e58e019230b6cca504 (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
//
//  HS4SModule.m
//  ReactNativeIOSLibrary
//
//  Created by ihealth on 16/12/2.
//  Copyright © 2016年 daiqingquan. All rights reserved.
//

#import "HS4SModule.h"
#import "HSProfileModule.h"
#import "HSMacroFile.h"
#import "HS4Controller.h"
#import "HS4.h"
#import "iHealthDeviceManagerModule.h"
#import "ManageDeviceController.h"


#define EVENT_NOTIFY @"HS4.MODULE.NOTIFY" 

@implementation HS4SModule


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;
}
-(HS4 *)getHS4WithMac:(NSString *)mac{
    HS4Controller *controller = [HS4Controller shareIHHs4Controller];
    NSArray *hs4DeviceArray = [controller getAllCurrentHS4Instace];
    for (HS4 *tempHS4 in  hs4DeviceArray) {
        if ([mac isEqualToString:tempHS4.deviceID]){
            return tempHS4;
            break;
        }
    }
    return nil;
}

#pragma mark
#pragma mark - Notification
#pragma mark - HS4
-(void)DeviceConnectForHS4S:(NSNotification *)notify{
    HS4Controller *controller = [HS4Controller shareIHHs4Controller];
    NSArray *hs4DeviceArray = [controller getAllCurrentHS4Instace];
    
    HS4 *hs4Instance = [hs4DeviceArray objectAtIndex:0];
}

#pragma mark
#pragma mark - Method


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


RCT_EXPORT_METHOD(getOfflineData:(nonnull NSString*)mac){
    if ([self getHS4WithMac:mac] != nil) {
        
        NSLog(@"上传离线数据");
        [[self getHS4WithMac:mac]commandTransferMemorryData:^(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:^(HS4DeviceError 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 getHS4WithMac:mac] != nil) {
       
        [[self getHS4WithMac:mac]commandMeasureWithUint:unit.intValue  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:^(HS4DeviceError 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 getHS4WithMac:mac] != nil){
        
        [[self getHS4WithMac: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:@"HS4.MODULE.NOTIFY"  body:@{@"action":actionName,key:value}];
}

@end