diff options
Diffstat (limited to 'libs/ihealth-sdk/ios/ReactNativeIOSLibrary/PT3SBTModule.m')
| -rw-r--r-- | libs/ihealth-sdk/ios/ReactNativeIOSLibrary/PT3SBTModule.m | 347 |
1 files changed, 347 insertions, 0 deletions
diff --git a/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/PT3SBTModule.m b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/PT3SBTModule.m new file mode 100644 index 0000000..525bc70 --- /dev/null +++ b/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/PT3SBTModule.m | |||
| @@ -0,0 +1,347 @@ | |||
| 1 | // | ||
| 2 | // PT3SBTModule.m | ||
| 3 | // ReactNativeIOSLibrary | ||
| 4 | // | ||
| 5 | // Created by daiqingquan on 2016/12/4. | ||
| 6 | // Copyright © 2016年 daiqingquan. All rights reserved. | ||
| 7 | // | ||
| 8 | |||
| 9 | #import "PT3SBTModule.h" | ||
| 10 | #import "PT3SBT.h" | ||
| 11 | #import "PT3SBTController.h" | ||
| 12 | #import "PT3SBTProfileModule.h" | ||
| 13 | |||
| 14 | @implementation PT3SBTModule{ | ||
| 15 | |||
| 16 | |||
| 17 | NSMutableDictionary*resultDic; | ||
| 18 | |||
| 19 | } | ||
| 20 | |||
| 21 | #define EVENT_NOTIFY @"event_notify_pt3sbt" | ||
| 22 | #define kMAC_KEY @"mac" | ||
| 23 | #define kACTION_KEY @"action" | ||
| 24 | |||
| 25 | |||
| 26 | RCT_EXPORT_MODULE() | ||
| 27 | - (NSArray<NSString *> *)supportedEvents { | ||
| 28 | return @[@"event_notify", @"event_scan_device", @"event_scan_finish", | ||
| 29 | @"event_device_connected", @"event_device_connect_failed", | ||
| 30 | @"event_device_disconnect", @"event_authenticate_result", | ||
| 31 | @"event_notify_ts28b", @"event_notify_bg1", | ||
| 32 | @"action_connect_result_for_bg1"]; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | #pragma mark | ||
| 38 | #pragma mark - constantsToExport | ||
| 39 | - (NSDictionary *)constantsToExport | ||
| 40 | { | ||
| 41 | return @{ @"Event_Notify": EVENT_NOTIFY }; | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | + (BOOL)requiresMainQueueSetup | ||
| 46 | { | ||
| 47 | return YES; | ||
| 48 | } | ||
| 49 | #pragma mark | ||
| 50 | #pragma mark - Init | ||
| 51 | -(id)init | ||
| 52 | { | ||
| 53 | if (self=[super init]) | ||
| 54 | { | ||
| 55 | |||
| 56 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(devicePT3SBTMeasure:) name:@"PT3SBTNotificationGetResult" object:nil]; | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | return self; | ||
| 62 | } | ||
| 63 | |||
| 64 | //Content of measurement results: | ||
| 65 | //{ | ||
| 66 | // Distance = 1663; | ||
| 67 | // Tamb = 275; | ||
| 68 | // Tbody = 3521; | ||
| 69 | // Tex = 269; | ||
| 70 | // Tobj = 274; | ||
| 71 | // Vbat = 281; | ||
| 72 | //} | ||
| 73 | |||
| 74 | |||
| 75 | -(void)devicePT3SBTMeasure:(NSNotification *)tempNoti{ | ||
| 76 | |||
| 77 | NSDictionary*measureDataDic= [tempNoti userInfo]; | ||
| 78 | |||
| 79 | NSLog(@"devicePT3SBTMeasure:%@",measureDataDic); | ||
| 80 | |||
| 81 | NSDictionary* deviceInfo = @{ | ||
| 82 | kACTION_KEY:kACTION_TEMPERATURE_MEASUREMENT,TEMPERATURE:[measureDataDic valueForKey:@"Tbody"]}; | ||
| 83 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | -(void)dealloc{ | ||
| 88 | [[NSNotificationCenter defaultCenter]removeObserver:self]; | ||
| 89 | } | ||
| 90 | |||
| 91 | -(PT3SBT*)getPT3SBTWithMac:(NSString*)mac{ | ||
| 92 | |||
| 93 | PT3SBTController *controller = [PT3SBTController shareIHPT3SBTController]; | ||
| 94 | NSArray *poDeviceArray = [controller getAllCurrentPT3SBTInstace]; | ||
| 95 | |||
| 96 | for(PT3SBT *tempPT3SBT in poDeviceArray){ | ||
| 97 | if([mac isEqualToString:tempPT3SBT.serialNumber]){ | ||
| 98 | |||
| 99 | return tempPT3SBT; | ||
| 100 | break; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | return nil; | ||
| 105 | } | ||
| 106 | |||
| 107 | #pragma mark | ||
| 108 | #pragma mark - Method | ||
| 109 | |||
| 110 | RCT_EXPORT_METHOD(getAllConnectedDevices){ | ||
| 111 | |||
| 112 | |||
| 113 | NSArray*PT3SBTarray= [[PT3SBTController shareIHPT3SBTController] getAllCurrentPT3SBTInstace]; | ||
| 114 | |||
| 115 | NSMutableArray*deviceMacArray=[NSMutableArray array]; | ||
| 116 | |||
| 117 | for (int i=0; i<[PT3SBTarray count]; i++) { | ||
| 118 | |||
| 119 | PT3SBT*PT3SBT=[PT3SBTarray objectAtIndex:i]; | ||
| 120 | |||
| 121 | [deviceMacArray addObject:PT3SBT.serialNumber]; | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | NSDictionary* deviceInfo = @{kACTION_KEY:kACTION_GET_ALL_CONNECTED_DEVICES,@"devices":deviceMacArray}; | ||
| 126 | |||
| 127 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 128 | |||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | #pragma mark 设置时间 | ||
| 133 | RCT_EXPORT_METHOD(setTime:(nonnull NSString *)mac){ | ||
| 134 | |||
| 135 | |||
| 136 | if ([self getPT3SBTWithMac:mac]) { | ||
| 137 | __weak typeof(self) weakSelf = self; | ||
| 138 | |||
| 139 | [[self getPT3SBTWithMac:mac] commandFunction:^(NSDictionary *functionDict) { | ||
| 140 | |||
| 141 | [weakSelf sendEventWithName:EVENT_NOTIFY body:@{ | ||
| 142 | kMAC_KEY:mac, | ||
| 143 | kACTION_KEY:kACTION_SET_TIME, | ||
| 144 | }]; | ||
| 145 | |||
| 146 | } DisposeErrorBlock:^(PT3SBTDeviceError error) { | ||
| 147 | |||
| 148 | }]; | ||
| 149 | |||
| 150 | } | ||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | #pragma mark 设置单位 | ||
| 155 | RCT_EXPORT_METHOD(setUnit:(nonnull NSString *)mac :(nonnull NSNumber *)unit){ | ||
| 156 | |||
| 157 | if ([self getPT3SBTWithMac:mac]) { | ||
| 158 | |||
| 159 | PT3SBTTemperatureUnit tempUnit = PT3SBTTemperatureUnit_C; | ||
| 160 | if ([unit isEqual:@(PT3SBTTemperatureUnit_C)]) { | ||
| 161 | tempUnit = PT3SBTTemperatureUnit_C; | ||
| 162 | } else if([unit isEqual:@(PT3SBTTemperatureUnit_F)]) { | ||
| 163 | tempUnit = PT3SBTTemperatureUnit_F; | ||
| 164 | } else { | ||
| 165 | tempUnit = PT3SBTTemperatureUnit_C; | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | __weak typeof(self) weakSelf = self; | ||
| 169 | |||
| 170 | [[self getPT3SBTWithMac:mac] commandPT3SBTSetUnit:tempUnit DisposeSetUnitResult:^(BOOL setResult) { | ||
| 171 | |||
| 172 | [weakSelf sendEventWithName:EVENT_NOTIFY body:@{ | ||
| 173 | kMAC_KEY:mac, | ||
| 174 | kACTION_KEY:kACTION_SET_UNIT, | ||
| 175 | }]; | ||
| 176 | |||
| 177 | } DisposeErrorBlock:^(PT3SBTDeviceError error) { | ||
| 178 | |||
| 179 | }]; | ||
| 180 | |||
| 181 | |||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | #pragma mark 获取单位 | ||
| 186 | RCT_EXPORT_METHOD(getUnit:(nonnull NSString *)mac){ | ||
| 187 | |||
| 188 | if ([self getPT3SBTWithMac:mac]) { | ||
| 189 | |||
| 190 | [[self getPT3SBTWithMac:mac] commandPT3SBTGetUnit:^(PT3SBTTemperatureUnit unit) { | ||
| 191 | |||
| 192 | [self sendEventWithName:EVENT_NOTIFY body:@{ | ||
| 193 | kMAC_KEY:mac, | ||
| 194 | UNIT:[NSNumber numberWithInt:unit], | ||
| 195 | kACTION_KEY:kACTION_GET_UNIT, | ||
| 196 | }]; | ||
| 197 | |||
| 198 | } DisposeErrorBlock:^(PT3SBTDeviceError error) { | ||
| 199 | |||
| 200 | }]; | ||
| 201 | |||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | #pragma mark 获取数据条数 | ||
| 206 | RCT_EXPORT_METHOD(getHistoryCount:(nonnull NSString *)mac){ | ||
| 207 | |||
| 208 | if ([self getPT3SBTWithMac:mac]) { | ||
| 209 | |||
| 210 | [[self getPT3SBTWithMac:mac] commandPT3SBTGetMemoryCount:^(NSNumber *count) { | ||
| 211 | |||
| 212 | [self sendEventWithName:EVENT_NOTIFY body:@{ | ||
| 213 | kMAC_KEY:mac, | ||
| 214 | COUNT:count, | ||
| 215 | kACTION_KEY:kACTION_GET_HISTORY_COUNT, | ||
| 216 | }]; | ||
| 217 | |||
| 218 | } DisposeErrorBlock:^(PT3SBTDeviceError error) { | ||
| 219 | |||
| 220 | }]; | ||
| 221 | |||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | #pragma mark 获取数据 | ||
| 226 | RCT_EXPORT_METHOD(getHistoryData:(nonnull NSString *)mac){ | ||
| 227 | |||
| 228 | if ([self getPT3SBTWithMac:mac]) { | ||
| 229 | |||
| 230 | [[self getPT3SBTWithMac:mac] commandGetMemorryData:^(NSMutableArray *memoryDataArray) { | ||
| 231 | |||
| 232 | |||
| 233 | if(memoryDataArray != nil){ | ||
| 234 | |||
| 235 | if(memoryDataArray.count){ | ||
| 236 | |||
| 237 | NSMutableArray*dataArray=[NSMutableArray array]; | ||
| 238 | |||
| 239 | for (int i=0; i<memoryDataArray.count; i++) { | ||
| 240 | |||
| 241 | NSNumber*ts=[[memoryDataArray objectAtIndex:i] objectForKey:@"TS"]; | ||
| 242 | |||
| 243 | NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:[ts intValue]]; | ||
| 244 | |||
| 245 | |||
| 246 | NSDateFormatter *mydateFormatter = [[NSDateFormatter alloc] init]; | ||
| 247 | [mydateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; | ||
| 248 | NSString *dateStr = [mydateFormatter stringFromDate:tempDate]; | ||
| 249 | |||
| 250 | |||
| 251 | NSDictionary*dic=@{TS:dateStr,TEMPERATURE:[[memoryDataArray objectAtIndex:i] objectForKey:@"Tbody"]}; | ||
| 252 | |||
| 253 | [dataArray addObject:dic]; | ||
| 254 | } | ||
| 255 | |||
| 256 | |||
| 257 | NSDictionary *deviceInfo = @{kMAC_KEY:mac,kACTION_KEY:kACTION_GET_HISTORY_DATA,HISTORY:dataArray}; | ||
| 258 | |||
| 259 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 260 | NSLog(@"historyDataArray:%@",memoryDataArray); | ||
| 261 | |||
| 262 | }else{ | ||
| 263 | |||
| 264 | NSDictionary *deviceInfo = @{kMAC_KEY:mac,kACTION_KEY:kACTION_GET_HISTORY_DATA,HISTORY:[NSArray array]}; | ||
| 265 | |||
| 266 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | } DisposeErrorBlock:^(PT3SBTDeviceError error) { | ||
| 275 | |||
| 276 | }]; | ||
| 277 | |||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | #pragma mark 删除数据 | ||
| 282 | RCT_EXPORT_METHOD(deleteHistory:(nonnull NSString *)mac){ | ||
| 283 | |||
| 284 | if ([self getPT3SBTWithMac:mac]) { | ||
| 285 | |||
| 286 | [[self getPT3SBTWithMac:mac] commandDeleteMemorryData:^(BOOL deleteResult) { | ||
| 287 | |||
| 288 | [self sendEventWithName:EVENT_NOTIFY body:@{ | ||
| 289 | kMAC_KEY:mac, | ||
| 290 | kACTION_KEY:kACTION_DELETE_HISTORY_DATA, | ||
| 291 | }]; | ||
| 292 | |||
| 293 | } DisposeErrorBlock:^(PT3SBTDeviceError error) { | ||
| 294 | |||
| 295 | }]; | ||
| 296 | |||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | |||
| 301 | RCT_EXPORT_METHOD(getBattery:(nonnull NSString *)mac){ | ||
| 302 | |||
| 303 | if ([self getPT3SBTWithMac:mac]!=nil) { | ||
| 304 | |||
| 305 | |||
| 306 | [[self getPT3SBTWithMac:mac] commandGetPT3SBTBattery:^(NSNumber *battary) { | ||
| 307 | |||
| 308 | NSDictionary* deviceInfo = @{kACTION_KEY:kACTION_GET_BATTERY,BATTERY:battary}; | ||
| 309 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 310 | |||
| 311 | } DiaposeErrorBlock:^(PT3SBTDeviceError error) { | ||
| 312 | |||
| 313 | }]; | ||
| 314 | |||
| 315 | |||
| 316 | }else{ | ||
| 317 | |||
| 318 | NSDictionary* deviceInfo = @{kACTION_KEY:@"ACTION_ERROR_PO",@"error_po":@"disconnect"}; | ||
| 319 | [self sendEventWithName:EVENT_NOTIFY body:deviceInfo]; | ||
| 320 | |||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | } | ||
| 325 | |||
| 326 | RCT_EXPORT_METHOD(disconnect:(nonnull NSString *)mac){ | ||
| 327 | |||
| 328 | |||
| 329 | if ([self getPT3SBTWithMac:mac]!=nil) { | ||
| 330 | |||
| 331 | [[self getPT3SBTWithMac:mac] commandDisconnectDevice]; | ||
| 332 | |||
| 333 | }else{ | ||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | |||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | @end | ||
