react-native-bt-device/ios/RNBTDevice/Device/RNSanModule.m

592 lines
24 KiB
Mathematica
Raw Normal View History

2024-03-24 09:45:44 +00:00
//
// RNSanModule.m
// RNBTDevice
//
// Created by lvwang2002 on 2019/5/13.
// Copyright © 2019 zmxv. All rights reserved.
//
#import "RNSanModule.h"
@implementation RNSanModule
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
-(instancetype)init{
self = [super init];
if(self){
}
return self;
}
RCT_EXPORT_METHOD(connectDynamicGlucose:(NSString *)deviceMac){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
device.deviceDelegate = self;
device.nativeDevice.deviceDelegate = self;
[device connect];
NSArray *devices = [manager getSearchedDevices];
for(BLEDevice *myDevice in devices){
NSString *myDeviceUUID = [[myDevice getUUID] UUIDString];
if([myDeviceUUID isEqualToString:deviceMac]){
continue;
}
if((myDevice.connectStatus != DeviceConnectingStatus) &&
(myDevice.connectStatus != DeviceConnectedStatus)){
continue;
}
dispatch_async([manager getPostQueue], ^{
[myDevice disconnect];
});
}
}
RCT_EXPORT_METHOD(didConnectedDevice:(BLEDevice *)device){
device.nativeDevice.deviceDelegate = self;
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"CONNECTED_BT_DEVICE" body:body];
}
-(void)didDisconnectDevice:(BLEDevice *)device{
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatus":@"disconnect"
};
// [self sendEventWithName:@"DISCONNECTED_BT_DEVICE" body:body];
[self.bridge.eventDispatcher sendAppEventWithName:@"DISCONNECT_BT_DEVICE" body:body];
}
-(void)didOverTimeForConnectDevice:(BLEDevice *)device{
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatus":@"connect time over"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
}
RCT_EXPORT_METHOD(doAuthorizationDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice doAuthorization];
}
RCT_EXPORT_METHOD(startMonitorDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice startMonitor];
}
RCT_EXPORT_METHOD(stopMonitorDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice stopMonitor];
}
RCT_EXPORT_METHOD(disconnecAllDYGDevicetWithout:(NSString *)deviceMac){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSArray *devices = [manager getSearchedDevices];
for(BLEDevice *myDevice in devices){
NSString *myDeviceUUID = [[myDevice getUUID] UUIDString];
if([myDeviceUUID isEqualToString:deviceMac]){
continue;
}
if([[myDevice getDeviceName] hasPrefix:@"TH-"]){
continue;
}
dispatch_async([manager getPostQueue], ^{
[myDevice disconnect];
});
}
}
//RCT_EXPORT_METHOD(stopMonitorDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
// ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
// NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
// BLEDevice *device = [manager getDeviceWithUUID:myUUID];
// if(!device){
// NSDictionary *body = @{
// @"deviceName":@"no device",
// @"deviceMac":deviceMac,
// @"deviceStatusDescription":@"no device",
// @"deviceStatus":@"error"
// };
// [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
// return;
// }
//
// ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
//
//
// [nativeDevice stopMonitor];
//}
//RCT_EXPORT_METHOD(doAuthorizationDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
// ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
// NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
// BLEDevice *device = [manager getDeviceWithUUID:myUUID];
// if(!device){
// NSDictionary *body = @{
// @"deviceName":@"no device",
// @"deviceMac":deviceMac,
// @"deviceStatusDescription":@"no device",
// @"deviceStatus":@"error"
// };
// [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
// return;
// }
//
// ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
//
//
// [nativeDevice doAuthorization];
//}
RCT_EXPORT_METHOD(getMonitorStatusDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getMonitorStatus];
}
RCT_EXPORT_METHOD(getNameDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getName];
}
RCT_EXPORT_METHOD(getVersionDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getVersion];
}
RCT_EXPORT_METHOD(getBatteryDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getBattery];
}
RCT_EXPORT_METHOD(getAllDataCountDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getAllDataCount];
}
RCT_EXPORT_METHOD(getAllDataDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getAllData];
}
RCT_EXPORT_METHOD(getNumberDataFromIndexDYG:(NSString *)deviceMac
RequestCode:(int)requestCode
Index:(int)index
Count:(int)count){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getNumberDataFromIndex:index Count:count];
}
RCT_EXPORT_METHOD(getAllDataFromIndexDYG:(NSString *)deviceMac RequestCode:(int)requestCode Index:(int)index){
ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
BLEDevice *device = [manager getDeviceWithUUID:myUUID];
if(!device){
NSDictionary *body = @{
@"deviceName":@"no device",
@"deviceMac":deviceMac,
@"deviceStatusDescription":@"no device",
@"deviceStatus":@"error"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE" body:body];
return;
}
ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
[nativeDevice getAllDataFromIndex:index];
}
RCT_EXPORT_METHOD(caculateBloodGlucose:(NSDictionary *)info
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
Algorithm20 *algorithm = [[Algorithm20 alloc] init];
NSArray *values = info[@"values"];
NSMutableArray<NSNumber *> *valueList = [NSMutableArray array];
for (id value in values) {
[valueList addObject:@([value doubleValue])];
}
NSInteger index = [RCTConvert NSInteger:info[@"index"]];
double refValue = [RCTConvert double:info[@"refValue"]];
NSMutableArray<NSNumber *> *bgValues = [algorithm calculateBloodGlucose:index
refValue:@(refValue)
values:valueList];
NSMutableArray *bgRNValues = [NSMutableArray array];
for (NSNumber *bgValue in bgValues) {
RCTLogInfo(@"%@", [bgValue stringValue]);
[bgRNValues addObject:bgValue];
}
resolve(bgRNValues);
}
RCT_EXPORT_METHOD(caculateDyRoc:(NSDictionary *)info
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
Algorithm20 *algorithm = [[Algorithm20 alloc] init];
// values NSMutableArray
NSArray *values = info[@"values"];
NSMutableArray<NSNumber *> *valueList = [NSMutableArray array];
for (id value in values) {
[valueList addObject:@([value doubleValue])];
}
NSInteger index = [RCTConvert NSInteger:info[@"index"]];
double refValue = [RCTConvert double:info[@"refValue"]];
// Algorithm20
[algorithm calculateBloodGlucose:index refValue:@(refValue) values:valueList];
int result = [algorithm calRoc];
// JavaScript
resolve(@(result));
}
2024-03-24 09:45:44 +00:00
-(void)onCreatedChannel:(BLEDevice *)device{
device.nativeDevice.deviceDelegate = self;
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc"
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_CREATED_CHANNEL" body:body];
}
/** */
-(void)onAuthenticated:(BLEDevice *)device IsSuc:(Boolean) isSuc{
device.nativeDevice.deviceDelegate = self;
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"isSuc":[NSNumber numberWithBool:isSuc]
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_DO_AUTHORIZATION" body:body];
}
///** */
//
/** */
-(void)onGetMonitorStatus:(BLEDevice *)device IsStartMonitor:(Boolean)isStartMonitor{
device.nativeDevice.deviceDelegate = self;
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"isStartMonitor":[NSNumber numberWithBool:isStartMonitor]
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_MONITOR_STATUS" body:body];
}
/** */
-(void)onGetBattery:(BLEDevice *)device Value:(double)value{
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"value":@(value)
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_BATTERY" body:body];
}
/** */
-(void)onGetAllDataCount:(BLEDevice *)device Count:(UInt16)count{
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"count":[NSNumber numberWithInt:count]
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_ALL_DATA_COUNT" body:body];
}
//
/** */
-(void)onGetProgressData:(BLEDevice *)device
DataList:(NSArray *)datalist
Index:(int)index{
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"dataList":datalist,
@"dataIndex":@(index)
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_ALL_DATA" body:body];
}
-(void)onStartMonitor:(BLEDevice *)device IsSuc:(Boolean)isSuc{
device.nativeDevice.deviceDelegate = self;
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"isSuc":[NSNumber numberWithBool:isSuc]
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_START_MONITOR" body:body];
}
//
-(void)onStopMonitor:(BLEDevice *)device IsSuc:(Boolean)isSuc{
device.nativeDevice.deviceDelegate = self;
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"isSuc":[NSNumber numberWithBool:isSuc]
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_STOP_MONITOR" body:body];
}
/////** */
-(void)onUpdateNewDataDevice:(BLEDevice *)device
Index:(UInt16)index
ElecValue:(double)elecValue{
device.nativeDevice.deviceDelegate = self;
NSDictionary *body =@{
@"deviceName":[device getDeviceName],
@"deviceMac":[[device getUUID] UUIDString],
@"deviceStatusDescription":@"连接成功",
@"deviceStatus":@"connectSuc",
@"index":@(index),
@"value":@(elecValue)
};
[self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_UPDATE_NEW_DATA" body:body];
}
@end