diff options
Diffstat (limited to 'libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BTMModule.m')
| -rw-r--r-- | libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BTMModule.m | 370 |
1 files changed, 370 insertions, 0 deletions
diff --git a/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BTMModule.m b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BTMModule.m new file mode 100644 index 0000000..779b141 --- /dev/null +++ b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/BTMModule.m | |||
| @@ -0,0 +1,370 @@ | |||
| 1 | // | ||
| 2 | // BTMModule.m | ||
| 3 | // ReactNativeIOSLibrary | ||
| 4 | // | ||
| 5 | // Created by daiqingquan on 2016/12/4. | ||
| 6 | // Copyright © 2016年 daiqingquan. All rights reserved. | ||
| 7 | // | ||
| 8 | |||
| 9 | #import "BTMModule.h" | ||
| 10 | #import "THV3.h" | ||
| 11 | #import "THV3Controller.h" | ||
| 12 | #import "BTMProfileModule.h" | ||
| 13 | |||
| 14 | @implementation BTMModule{ | ||
| 15 | |||
| 16 | |||
| 17 | NSMutableDictionary*resultDic; | ||
| 18 | |||
| 19 | } | ||
| 20 | |||
| 21 | #define EVENT_NOTIFY @"event_notify_po3" | ||
| 22 | |||
| 23 | |||
| 24 | RCT_EXPORT_MODULE() | ||
| 25 | - (NSArray<NSString *> *)supportedEvents { | ||
| 26 | return @[@"event_notify", @"event_scan_device", @"event_scan_finish", | ||
| 27 | @"event_device_connected", @"event_device_connect_failed", | ||
| 28 | @"event_device_disconnect", @"event_authenticate_result", | ||
| 29 | @"event_notify_ts28b", @"event_notify_bg1", | ||
| 30 | @"action_connect_result_for_bg1"]; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | #pragma mark | ||
| 36 | #pragma mark - constantsToExport | ||
| 37 | - (NSDictionary *)constantsToExport | ||
| 38 | { | ||
| 39 | return @{ @"Event_Notify": EVENT_NOTIFY }; | ||
| 40 | |||
| 41 | } | ||
| 42 | + (BOOL)requiresMainQueueSetup | ||
| 43 | { | ||
| 44 | return YES; | ||
| 45 | } | ||
| 46 | |||
| 47 | #pragma mark | ||
| 48 | #pragma mark - Init | ||
| 49 | -(id)init | ||
| 50 | { | ||
| 51 | if (self=[super init]) | ||
| 52 | { | ||
| 53 | |||
| 54 | [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveDeviceData:) name:@"THV3NewDataCome" object:nil]; | ||
| 55 | |||
| 56 | } | ||
| 57 | return self; | ||
| 58 | } | ||
| 59 | |||
| 60 | -(THV3*)getTHV3WithMac:(NSString*)mac{ | ||
| 61 | |||
| 62 | THV3Controller *controller = [THV3Controller sharedController]; | ||
| 63 | NSArray *thvDeviceArray = [controller allCurrentInstance]; | ||
| 64 | |||
| 65 | for(THV3 *tempTHV3 in thvDeviceArray){ | ||
| 66 | if([mac isEqualToString:tempTHV3.serialNumber]){ | ||
| 67 | |||
| 68 | return tempTHV3; | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | return nil; | ||
| 74 | } | ||
| 75 | |||
| 76 | -(void)reciveDeviceData:(NSNotification *)tempNoti{ | ||
| 77 | |||
| 78 | NSDictionary *infoDic=[tempNoti object]; | ||
| 79 | |||
| 80 | THV3*myTHV3=[self getTHV3WithMac:[infoDic valueForKey:@"SerialNumber"]]; | ||
| 81 | |||
| 82 | THV3RcvDataType rtDataType=myTHV3.rtDataType; | ||
| 83 | |||
| 84 | NSNumber*target; | ||
| 85 | |||
| 86 | if (myTHV3.isTargetHuman) { | ||
| 87 | |||
| 88 | target=@1; | ||
| 89 | |||
| 90 | }else{ | ||
| 91 | |||
| 92 | target=@2; | ||
| 93 | } | ||
| 94 | |||
| 95 | if (myTHV3!=nil) { | ||
| 96 | |||
| 97 | if (rtDataType==THV3RcvDataRT) { | ||
| 98 | |||
| 99 | THV3HistoryData* rtTemperature=myTHV3.rtTemperature; | ||
| 100 | |||
| 101 | NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init]; | ||
| 102 | [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; | ||
| 103 | |||
| 104 | if (rtTemperature.measureDate!=nil) { | ||
| 105 | |||
| 106 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_MEASURE,BTM_TEMPERATURE:[NSNumber numberWithFloat:rtTemperature.temperature],BTM_MEASURE_TIME:[mydateFormatter stringFromDate:rtTemperature.measureDate],BTM_TEMPERATURE_TARGET:target}; | ||
| 107 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | }else{ | ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | } | ||
| 118 | }else{ | ||
| 119 | |||
| 120 | |||
| 121 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_ERROR,BTM_ERROR_DESCRIPTION:@"disconnect"}; | ||
| 122 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | #pragma mark | ||
| 131 | #pragma mark - Method | ||
| 132 | |||
| 133 | RCT_EXPORT_METHOD(getAllConnectedDevices){ | ||
| 134 | |||
| 135 | |||
| 136 | NSArray*thv3array= [[THV3Controller sharedController] allCurrentInstance]; | ||
| 137 | |||
| 138 | NSMutableArray*deviceMacArray=[NSMutableArray array]; | ||
| 139 | |||
| 140 | for (int i=0; i<[thv3array count]; i++) { | ||
| 141 | |||
| 142 | THV3*thv3=[thv3array objectAtIndex:i]; | ||
| 143 | |||
| 144 | [deviceMacArray addObject:thv3.serialNumber]; | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | NSDictionary* deviceInfo = @{@"action":@"ACTION_GET_ALL_CONNECTED_DEVICES",BTM_DEVICE:deviceMacArray}; | ||
| 149 | |||
| 150 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 151 | |||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | RCT_EXPORT_METHOD(getBattery:(nonnull NSString *)mac){ | ||
| 158 | |||
| 159 | BOOL voltIsNormal = [self getTHV3WithMac:mac].voltIsNormal; | ||
| 160 | |||
| 161 | NSNumber*battNum; | ||
| 162 | |||
| 163 | if (voltIsNormal) { | ||
| 164 | battNum=@1; | ||
| 165 | }else{ | ||
| 166 | |||
| 167 | battNum=@0; | ||
| 168 | } | ||
| 169 | |||
| 170 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_BATTERY,BTM_BATTERY:battNum}; | ||
| 171 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | RCT_EXPORT_METHOD(getMemoryData:(nonnull NSString *)mac){ | ||
| 176 | |||
| 177 | |||
| 178 | if ([self getTHV3WithMac:mac]!=nil) { | ||
| 179 | |||
| 180 | [[self getTHV3WithMac:mac] readHistoryDataWithResultBlock:^(NSArray<THV3HistoryData *> *dataArray) { | ||
| 181 | |||
| 182 | NSMutableArray * tempArr = [[NSMutableArray alloc]init]; | ||
| 183 | |||
| 184 | for (int i=0; i<dataArray.count; i++) { | ||
| 185 | //将时间格式转化成字符串,适配plugin和react native | ||
| 186 | NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init]; | ||
| 187 | [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; | ||
| 188 | NSString *dateStr = [mydateFormatter stringFromDate:[dataArray objectAtIndex:i].measureDate]; | ||
| 189 | |||
| 190 | NSNumber*target; | ||
| 191 | |||
| 192 | if ([dataArray objectAtIndex:i].isHumanBody) { | ||
| 193 | |||
| 194 | target=@1; | ||
| 195 | }else{ | ||
| 196 | |||
| 197 | target=@2; | ||
| 198 | } | ||
| 199 | |||
| 200 | NSDictionary *dic = @{BTM_MEASURE_TIME:dateStr,BTM_TEMPERATURE:[NSNumber numberWithFloat:[dataArray objectAtIndex:i].temperature],BTM_TEMPERATURE_TARGET:target}; | ||
| 201 | |||
| 202 | [tempArr addObject:dic]; | ||
| 203 | } | ||
| 204 | |||
| 205 | if (tempArr.count>0) { | ||
| 206 | |||
| 207 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_MEMORY,BTM_TEMPERATURE_ARRAY:tempArr,BTM_MEMORY_COUNT:[NSNumber numberWithInt:tempArr.count]}; | ||
| 208 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | }]; | ||
| 213 | |||
| 214 | |||
| 215 | }else{ | ||
| 216 | |||
| 217 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_ERROR,BTM_ERROR_DESCRIPTION:@"disconnect"}; | ||
| 218 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 219 | |||
| 220 | } | ||
| 221 | |||
| 222 | } | ||
| 223 | |||
| 224 | RCT_EXPORT_METHOD(setStandbyTime:(nonnull NSString *)mac :(nonnull NSNumber *)hour:(nonnull NSNumber *)min:(nonnull NSNumber *)sec){ | ||
| 225 | |||
| 226 | if ([self getTHV3WithMac:mac]!=nil) { | ||
| 227 | |||
| 228 | [[self getTHV3WithMac:mac] configIdleTime:[[NSDate date]timeIntervalSince1970] withResultBlock:^(BOOL success) { | ||
| 229 | |||
| 230 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_CALLBACK}; | ||
| 231 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 232 | |||
| 233 | }]; | ||
| 234 | |||
| 235 | |||
| 236 | }else{ | ||
| 237 | |||
| 238 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_ERROR,BTM_ERROR_DESCRIPTION:@"disconnect"}; | ||
| 239 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 240 | |||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | } | ||
| 248 | RCT_EXPORT_METHOD(setTemperatureUnit:(nonnull NSString *)mac:(nonnull NSNumber *)unit){ | ||
| 249 | |||
| 250 | if ([self getTHV3WithMac:mac]!=nil) { | ||
| 251 | |||
| 252 | BOOL isUnitC=YES; | ||
| 253 | |||
| 254 | if ([unit intValue]==1) { | ||
| 255 | |||
| 256 | isUnitC=YES; | ||
| 257 | |||
| 258 | }else{ | ||
| 259 | |||
| 260 | isUnitC=NO; | ||
| 261 | } | ||
| 262 | |||
| 263 | [[self getTHV3WithMac:mac] configTemperUnit:isUnitC withResultBlock:^(BOOL success) { | ||
| 264 | |||
| 265 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_CALLBACK}; | ||
| 266 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 267 | |||
| 268 | }]; | ||
| 269 | |||
| 270 | |||
| 271 | }else{ | ||
| 272 | |||
| 273 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_ERROR,BTM_ERROR_DESCRIPTION:@"disconnect"}; | ||
| 274 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 275 | |||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | } | ||
| 281 | RCT_EXPORT_METHOD(setMeasuringTarget:(nonnull NSString *)mac :(nonnull NSNumber *)target){ | ||
| 282 | |||
| 283 | if ([self getTHV3WithMac:mac]!=nil) { | ||
| 284 | |||
| 285 | BOOL istarget=YES; | ||
| 286 | |||
| 287 | if ([target intValue]==1) { | ||
| 288 | |||
| 289 | istarget=YES; | ||
| 290 | |||
| 291 | }else{ | ||
| 292 | |||
| 293 | istarget=NO; | ||
| 294 | } | ||
| 295 | |||
| 296 | [[self getTHV3WithMac:mac] configMeasureTarget:istarget withResultBlock:^(BOOL success) { | ||
| 297 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_CALLBACK}; | ||
| 298 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 299 | |||
| 300 | }]; | ||
| 301 | |||
| 302 | |||
| 303 | }else{ | ||
| 304 | |||
| 305 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_ERROR,BTM_ERROR_DESCRIPTION:@"disconnect"}; | ||
| 306 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 307 | |||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | } | ||
| 312 | RCT_EXPORT_METHOD(setOfflineTarget:(nonnull NSString *)mac:(nonnull NSNumber *)target){ | ||
| 313 | |||
| 314 | if ([self getTHV3WithMac:mac]!=nil) { | ||
| 315 | |||
| 316 | BOOL istarget=YES; | ||
| 317 | |||
| 318 | if ([target intValue]==1) { | ||
| 319 | |||
| 320 | istarget=NO; | ||
| 321 | |||
| 322 | }else{ | ||
| 323 | |||
| 324 | istarget=YES; | ||
| 325 | } | ||
| 326 | |||
| 327 | [[self getTHV3WithMac:mac] configOfflineMode:istarget withResultBlock:^(BOOL success) { | ||
| 328 | |||
| 329 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_CALLBACK}; | ||
| 330 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 331 | |||
| 332 | }]; | ||
| 333 | |||
| 334 | |||
| 335 | }else{ | ||
| 336 | |||
| 337 | NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_ERROR,BTM_ERROR_DESCRIPTION:@"disconnect"}; | ||
| 338 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 339 | |||
| 340 | } | ||
| 341 | |||
| 342 | } | ||
| 343 | |||
| 344 | RCT_EXPORT_METHOD(disconnect:(nonnull NSString *)mac){ | ||
| 345 | |||
| 346 | |||
| 347 | |||
| 348 | |||
| 349 | if ([self getTHV3WithMac:mac]!=nil) { | ||
| 350 | |||
| 351 | [[self getTHV3WithMac:mac] commandDisconnectDevice]; | ||
| 352 | |||
| 353 | // NSDictionary* deviceInfo = @{BTM_ACTION:BTM_ACTION_CALLBACK}; | ||
| 354 | // [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 355 | |||
| 356 | }else{ | ||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | } | ||
| 365 | |||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | @end | ||
