summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/ios/ReactNativeIOSLibrary/PO1Module.m
blob: 1fb2b9c793426619c31a313b590c7631cc14f5c2 (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
//
//  PO1Module.m
//  ReactNativeIOSLibrary
//
//  Created by daiqingquan on 2016/12/4.
//  Copyright © 2016年 daiqingquan. All rights reserved.
//

#import "PO1Module.h"
#import "PO1.h"
#import "PO1Controller.h"
#import "PO1ProfileModule.h"

@implementation PO1Module{


    NSMutableDictionary*resultDic;

}

#define EVENT_NOTIFY @"event_notify_po1"
#define kMAC_KEY        @"mac"
#define kACTION_KEY     @"action"


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
#pragma mark - constantsToExport
- (NSDictionary *)constantsToExport
{
    return @{ @"Event_Notify": EVENT_NOTIFY };
    
}

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}
#pragma mark
#pragma mark - Init
-(id)init
{
    if (self=[super init])
    {
       
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(devicePO1Measure:) name:@"PO1NotificationMeasureData" object:nil];
        
    }
    
   
    return self;
}


-(void)devicePO1Measure:(NSNotification *)tempNoti{
 
    NSDictionary*measureDataDic= [tempNoti userInfo];
    
    NSLog(@"devicePO1Measure:%@",measureDataDic);
    
    NSDictionary* deviceInfo = @{kACTION_KEY:@"ACTION_BO_MEASUREMENT",@"WAVE":[measureDataDic valueForKey:@"wave"],@"PI":[measureDataDic valueForKey:@"PI"],@"PULSE_FORCE":[measureDataDic valueForKey:@"height"],@"BLOOD_OXYGEN":[measureDataDic valueForKey:@"spo2"],@"PULSE":[measureDataDic valueForKey:@"bpm"]};
    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(PO1*)getPO1WithMac:(NSString*)mac{
    
    PO1Controller *controller = [PO1Controller shareIHPO1Controller];
    NSArray *poDeviceArray = [controller getAllCurrentPO1Instace];
    
    for(PO1 *tempPO1 in poDeviceArray){
        if([mac isEqualToString:tempPO1.serialNumber]){
            
            return tempPO1;
            break;
        }
    }
    
    return nil;
}

#pragma mark
#pragma mark - Method

RCT_EXPORT_METHOD(getAllConnectedDevices){
    
    
    NSArray*po1array= [[PO1Controller shareIHPO1Controller] getAllCurrentPO1Instace];
    
    NSMutableArray*deviceMacArray=[NSMutableArray array];
    
    for (int i=0; i<[po1array count]; i++) {
        
        PO1*po1=[po1array objectAtIndex:i];
        
        [deviceMacArray addObject:po1.serialNumber];
        
    }
    
    NSDictionary* deviceInfo = @{kACTION_KEY:@"ACTION_GET_ALL_CONNECTED_DEVICES",@"devices":deviceMacArray};
    
    [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
    
    
}



RCT_EXPORT_METHOD(getBattery:(nonnull NSString *)mac){
    
    if ([self getPO1WithMac:mac]!=nil) {
        
        
        [[self getPO1WithMac:mac] commandPO1GetDeviceBattery:^(NSNumber *battery) {
            
            NSDictionary* deviceInfo = @{kACTION_KEY:@"ACTION_GET_BATTERY",@"BATTERY":battery};
            [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
            
        } withErrorBlock:^(PO1ErrorID errorID) {
            
        }];
    }else{
        
        NSDictionary* deviceInfo = @{kACTION_KEY:@"ACTION_ERROR_PO1",@"ERROR_DESCRIPTION":@"disconnect"};
        [self sendEventWithName:EVENT_NOTIFY body:deviceInfo];
        
    }
    
    
}

RCT_EXPORT_METHOD(disconnect:(nonnull NSString *)mac){
    
    
    if ([self getPO1WithMac:mac]!=nil) {
        
        [[self getPO1WithMac:mac] commandDisconnectDevice];
        
    }else{
        
        
        
    }
    
    
    
}





@end