summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS2Module.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS2Module.m')
-rw-r--r--libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS2Module.m277
1 files changed, 277 insertions, 0 deletions
diff --git a/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS2Module.m b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS2Module.m
new file mode 100644
index 0000000..376f2f3
--- /dev/null
+++ b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/HS2Module.m
@@ -0,0 +1,277 @@
1//
2// HS2Module.m
3// ReactNativeIOSLibrary
4//
5// Created by jing on 2018/11/26.
6// Copyright © 2018年 daiqingquan. All rights reserved.
7//
8
9#import "HS2Module.h"
10#import "HSProfileModule.h"
11#import "HSMacroFile.h"
12#import "HS2Controller.h"
13#import "HS2.h"
14#import "iHealthDeviceManagerModule.h"
15#import "ManageDeviceController.h"
16
17#define EVENT_NOTIFY @"HS2.MODULE.NOTIFY"
18
19@implementation HS2Module
20
21RCT_EXPORT_MODULE()
22- (NSArray<NSString *> *)supportedEvents {
23 return @[@"event_notify", @"event_scan_device", @"event_scan_finish",
24 @"event_device_connected", @"event_device_connect_failed",
25 @"event_device_disconnect", @"event_authenticate_result",
26 @"event_notify_ts28b", @"event_notify_bg1",
27 @"action_connect_result_for_bg1"];
28}
29
30
31#pragma mark-init
32
33-(NSDictionary *)constantsToExport{
34 return @{
35 @"Event_Notify" : EVENT_NOTIFY
36 };
37}
38
39+ (BOOL)requiresMainQueueSetup
40{
41 return YES;
42}
43
44-(HS2 *)getHS2WithMac:(NSString *)mac{
45 HS2Controller *controller = [HS2Controller shareIHHs2Controller];
46 NSArray *hs2DeviceArray = [controller getAllCurrentHS2Instace];
47 for (HS2 *tempHS2 in hs2DeviceArray) {
48 if ([mac isEqualToString:tempHS2.deviceID]){
49 return tempHS2;
50 break;
51 }
52 }
53 return nil;
54}
55
56#pragma mark
57#pragma mark - Method
58
59
60#pragma mark-获取连接设备
61RCT_EXPORT_METHOD(getAllConnectedDevices){
62
63
64 NSArray*hs2array= [[HS2Controller shareIHHs2Controller] getAllCurrentHS2Instace];
65
66 NSMutableArray*deviceMacArray=[NSMutableArray array];
67
68 for (int i=0; i<[hs2array count]; i++) {
69
70 HS2*hs2=[hs2array objectAtIndex:i];
71
72 [deviceMacArray addObject:hs2.deviceID];
73
74 }
75
76 NSDictionary* deviceInfo = @{@"action":@"action_get_all_connected_devices",@"devices":deviceMacArray};
77 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
78
79
80}
81
82RCT_EXPORT_METHOD(getBattery:(nonnull NSString*)mac){
83 if ([self getHS2WithMac:mac] != nil) {
84
85 [[self getHS2WithMac:mac]commandGetHS2Battery:^(NSNumber *battary) {
86
87 NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_BATTARY_HS,BATTERY_HS:battary};
88
89 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
90
91 } DiaposeErrorBlock:^(HS2DeviceError errorID) {
92
93 }];
94
95 }
96}
97RCT_EXPORT_METHOD(getOfflineData:(nonnull NSString*)mac){
98 if ([self getHS2WithMac:mac] != nil) {
99
100 NSLog(@"上传离线数据");
101
102 [[self getHS2WithMac:mac]commandHS2TransferMemorryData:^(NSDictionary *startDataDictionary) {
103
104 } DisposeProgress:^(NSNumber *progress) {
105
106 } MemorryData:^(NSArray *historyDataArray) {
107 if(historyDataArray != nil){
108
109 if(historyDataArray.count){
110
111 NSMutableArray*dataArray=[NSMutableArray array];
112
113 for (int i=0; i<historyDataArray.count; i++) {
114
115 NSDate *tempDate = [[historyDataArray objectAtIndex:i] objectForKey:@"date"];
116
117
118 NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init];
119 [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
120 NSString *dateStr = [mydateFormatter stringFromDate:tempDate];
121
122
123 NSDictionary*dic=@{DATAID:[[historyDataArray objectAtIndex:i] objectForKey:@"dataID"],MEASUREMENT_DATE_HS:dateStr,WEIGHT_HS:[[historyDataArray objectAtIndex:i] objectForKey:@"weight"]};
124
125 [dataArray addObject:dic];
126 }
127
128
129 NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_HISTORICAL_DATA_HS,HISTORDATA_HS:dataArray};
130
131 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
132 NSLog(@"historyDataArray:%@",historyDataArray);
133
134 }
135
136 }
137 } FinishTransmission:^{
138 NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_HISTORICAL_DATA_COMPLETE_HS};
139
140 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
141 } DisposeErrorBlock:^(HS2DeviceError errorID) {
142 NSString *errorMassage = [NSString string];
143 NSLog(@"errorID:%d",errorID);
144 switch (errorID) {
145 case 1:
146 errorMassage = @"Battery level is low";
147 break;
148 case 2:
149 errorMassage = @"The Scale failed to initialize.";
150 break;
151 case 5:
152 errorMassage = @"Bluetooth connection error.";
153 break;
154 case 7:
155 errorMassage = @"Invalidate.";
156 break;
157 case 8:
158 errorMassage = @"Scale memory access error.";
159 break;
160 case 9:
161 errorMassage = @"No History Data.";
162 break;
163 case 10:
164 errorMassage = @"Device disconnect.";
165 break;
166 // case 11:
167 // errorMassage = @"Communication error.";
168 // break;
169 default:
170 break;
171 }
172 if(errorID == 9){
173 NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_NO_HISTORICALDATA,ERROR_DESCRIPTION_HS:@"No History Data."};
174
175 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
176 }
177 else if(errorID != 11){
178 NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_ERROR_HS,ERROR_NUM_HS:errorMassage};
179
180 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];}
181 }];
182
183 }
184}
185
186
187RCT_EXPORT_METHOD(measureOnline:(nonnull NSString*)mac :(nonnull NSNumber*)unit :(nonnull NSNumber*)userId){
188 if ([self getHS2WithMac:mac] != nil) {
189 HSUnit tempUnit;
190
191 switch ([unit intValue]) {
192 case 1:
193 tempUnit=HSUnit_Kg;
194 break;
195 case 2:
196 tempUnit=HSUnit_LB;
197 break;
198 case 3:
199 tempUnit=HSUnit_ST;
200 break;
201 default:
202 tempUnit=HSUnit_Kg;
203 break;
204 }
205
206 [[self getHS2WithMac:mac]commandHS2MeasureWithUint:tempUnit Weight:^(NSNumber *unStableWeight) {
207 NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_LIVEDATA_HS,LIVEDATA_HS:unStableWeight};
208 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
209 } StableWeight:^(NSDictionary *StableWeightDic) {
210 NSDictionary *deviceInfo =@{@"mac":mac,@"action":ACTION_ONLINE_RESULT_HS,DATAID:[StableWeightDic valueForKey:@"dataID"],WEIGHT_HS:[StableWeightDic valueForKey:@"Weight"] };
211
212 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
213 } DisposeErrorBlock:^(HS2DeviceError errorID) {
214 NSString *errorMassage = [NSString string];
215 switch (errorID) {
216 case 1:
217 errorMassage = @"Battery level is low.";
218 break;
219 case 2:
220 errorMassage = @"The Scale failed to initialize.";
221 break;
222 case 3:
223 errorMassage = @"Maximum weight has been exceeded.";
224 break;
225 case 4:
226 errorMassage = @"The Scale can't capture a steady reading.";
227 break;
228 case 5:
229 errorMassage = @"Bluetooth connection error.";
230 break;
231 case 6:
232 errorMassage = @"Movement while measuring.";
233 break;
234 case 7:
235 errorMassage = @"Invalidate.";
236 break;
237 case 10:
238 errorMassage = @"Device disconnect.";
239 break;
240 case 11:
241 errorMassage = @"Communication error.";
242 break;
243 case 12:
244 errorMassage = @"HS4DeviceRecWeightError.";
245 break;
246 default:
247 break;
248 }
249
250 NSDictionary *deviceInfo = @{@"mac":mac,@"action":ACTION_ERROR_HS,ERROR_NUM_HS:errorMassage};
251 [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
252 }];
253
254
255 }
256
257}
258
259RCT_EXPORT_METHOD(disconnect:(nonnull NSString*)mac){
260 if ([self getHS2WithMac:mac] != nil){
261
262 [[self getHS2WithMac:mac]commandDisconnectDevice];
263 NSLog(@"End device connnect!");
264
265 }else{
266
267 }
268}
269- (void)sendErrorWithCode:(NSInteger)errorCode{
270 [self sendEventWithAction:@"ACTION_ERROR_HS" keyString:@"value" valueString:@(errorCode)];
271}
272
273- (void)sendEventWithAction:(NSString*)actionName keyString:(NSString*)key valueString:(id)value{
274 [self sendEventWithName:@"HS2.MODULE.NOTIFY" body:@{@"action":actionName,key:value}];
275}
276
277@end