feature():1.修改dyglucose的消息发送模式适配新版程序 2.删除用不到的读卡库引用代码

This commit is contained in:
lvwang2002 2024-03-29 01:09:57 +08:00
parent bb1ee3228a
commit d25c64ea51
3 changed files with 377 additions and 329 deletions

View File

@ -17,14 +17,15 @@
#import "ENCholesterolDevice.h" #import "ENCholesterolDevice.h"
#import "RCTEventEmitter.h" #import "RCTEventEmitter.h"
#if TARGET_IPHONE_SIMULATOR//模拟器 //#if TARGET_IPHONE_SIMULATOR//模拟器
//真机
@interface RNMethodModule : NSObject<RCTBridgeModule,DeviceManagerProtocol,BLEDeviceProtocol,ENGlucoDeviceProtocal,ENGGlucoDeviceProtocal,ENBPDeviceProtocal,ENCholesterolDeviceProtocal> @interface RNMethodModule : NSObject<RCTBridgeModule,DeviceManagerProtocol,BLEDeviceProtocol,ENGlucoDeviceProtocal,ENGGlucoDeviceProtocal,ENBPDeviceProtocal,ENCholesterolDeviceProtocal>
#elif TARGET_OS_IPHONE//真机 //#elif TARGET_OS_IPHONE//真机
#import "INVSSDK/INVSSDK.h" //#import "INVSSDK/INVSSDK.h"
#import "INVSSDK/NSString+ThreeDES.h" //#import "INVSSDK/NSString+ThreeDES.h"
@interface RNMethodModule : NSObject<RCTBridgeModule,DeviceManagerProtocol,BLEDeviceProtocol,ENGlucoDeviceProtocal,ENGGlucoDeviceProtocal,ENBPDeviceProtocal,ENCholesterolDeviceProtocal,BR_Callback> //@interface RNMethodModule : NSObject<RCTBridgeModule,DeviceManagerProtocol,BLEDeviceProtocol,ENGlucoDeviceProtocal,ENGGlucoDeviceProtocal,ENBPDeviceProtocal,ENCholesterolDeviceProtocal,BR_Callback>
#endif //#endif

View File

@ -36,7 +36,7 @@
#if TARGET_IPHONE_SIMULATOR//模拟器 #if TARGET_IPHONE_SIMULATOR//模拟器
#elif TARGET_OS_IPHONE//真机 #elif TARGET_OS_IPHONE//真机
INVSBleTool *_bletool; // INVSBleTool *_bletool;
#endif #endif
BLEDevice *_cardDevice; BLEDevice *_cardDevice;
Boolean _isUpdateDeviceList; Boolean _isUpdateDeviceList;
@ -66,7 +66,7 @@ RCT_EXPORT_MODULE();
#if TARGET_IPHONE_SIMULATOR//模拟器 #if TARGET_IPHONE_SIMULATOR//模拟器
#elif TARGET_OS_IPHONE//真机 #elif TARGET_OS_IPHONE//真机
_bletool=[[INVSBleTool alloc]init:self]; // _bletool=[[INVSBleTool alloc]init:self];
#endif #endif
@ -757,7 +757,7 @@ RCT_REMAP_METHOD(stopCardReaderScan,
#if TARGET_IPHONE_SIMULATOR//模拟器 #if TARGET_IPHONE_SIMULATOR//模拟器
#elif TARGET_OS_IPHONE//真机 #elif TARGET_OS_IPHONE//真机
[_bletool disconnectBt]; // [_bletool disconnectBt];
#endif #endif
// if(!_cardDevice){ // if(!_cardDevice){
@ -789,7 +789,7 @@ RCT_EXPORT_METHOD(connectCardReader:(NSString *)uuid
#if TARGET_IPHONE_SIMULATOR//模拟器 #if TARGET_IPHONE_SIMULATOR//模拟器
#elif TARGET_OS_IPHONE//真机 #elif TARGET_OS_IPHONE//真机
[_bletool connectBt:[device getPeripheral]]; // [_bletool connectBt:[device getPeripheral]];
#endif #endif
_cardDevice = device; _cardDevice = device;
@ -819,7 +819,7 @@ RCT_EXPORT_METHOD(connectCardReader:(NSString *)uuid
#if TARGET_IPHONE_SIMULATOR//模拟器 #if TARGET_IPHONE_SIMULATOR//模拟器
#elif TARGET_OS_IPHONE//真机 #elif TARGET_OS_IPHONE//真机
[_bletool disconnectBt]; // [_bletool disconnectBt];
#endif #endif
return; return;
} }
@ -855,37 +855,37 @@ RCT_REMAP_METHOD(readCard,
#if TARGET_IPHONE_SIMULATOR//模拟器 #if TARGET_IPHONE_SIMULATOR//模拟器
#elif TARGET_OS_IPHONE//真机 #elif TARGET_OS_IPHONE//真机
if(!_bletool){ // if(!_bletool){
reject(@"400",@"no device",[NSError errorWithDomain:@"device" code:400 userInfo:@{}]); // reject(@"400",@"no device",[NSError errorWithDomain:@"device" code:400 userInfo:@{}]);
return; // return;
} // }
//
NSDictionary *obj = [_bletool readCard]; // NSDictionary *obj = [_bletool readCard];
if(!obj){ // if(!obj){
reject(@"400",@"no device",[NSError errorWithDomain:@"device" code:400 userInfo:@{}]); // reject(@"400",@"no device",[NSError errorWithDomain:@"device" code:400 userInfo:@{}]);
return; // return;
} // }
//
// NSMutableString *resultContent = [[NSMutableString alloc] init]; //// NSMutableString *resultContent = [[NSMutableString alloc] init];
//
NSInteger resultFlag = [obj[@"resultFlag"] integerValue]; // NSInteger resultFlag = [obj[@"resultFlag"] integerValue];
if(resultFlag == -1){ // if(resultFlag == -1){
reject(@"400",@"no data",[NSError errorWithDomain:@"device" code:400 userInfo:@{}]); // reject(@"400",@"no data",[NSError errorWithDomain:@"device" code:400 userInfo:@{}]);
}else{ // }else{
NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithCapacity:10]; // NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithCapacity:10];
//
[info setObject:obj[@"partyName"] forKey:@"name"]; // [info setObject:obj[@"partyName"] forKey:@"name"];
[info setObject:obj[@"certAddress"] forKey:@"address"]; // [info setObject:obj[@"certAddress"] forKey:@"address"];
[info setObject:obj[@"bornDay"] forKey:@"birth"]; // [info setObject:obj[@"bornDay"] forKey:@"birth"];
[info setObject:obj[@"expDate"] forKey:@"end"]; // [info setObject:obj[@"expDate"] forKey:@"end"];
[info setObject:obj[@"certNumber"] forKey:@"idNo"]; // [info setObject:obj[@"certNumber"] forKey:@"idNo"];
[info setObject:obj[@"nation"] forKey:@"nation"]; // [info setObject:obj[@"nation"] forKey:@"nation"];
[info setObject:obj[@"certOrg"] forKey:@"police"]; // [info setObject:obj[@"certOrg"] forKey:@"police"];
[info setObject:obj[@"gender"] forKey:@"sex"]; // [info setObject:obj[@"gender"] forKey:@"sex"];
[info setObject:obj[@"effDate"] forKey:@"start"]; // [info setObject:obj[@"effDate"] forKey:@"start"];
//
resolve([info copy]); // resolve([info copy]);
} // }
#endif #endif

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
import React, { Component } from 'react'; import React, { Component } from 'react';
import {NativeModules,DeviceEventEmitter,Platform} from 'react-native'; import { NativeModules, DeviceEventEmitter, Platform } from 'react-native';
@ -8,21 +8,21 @@ let MethodModule;
if (Platform.OS === 'ios') { if (Platform.OS === 'ios') {
MethodModule = NativeModules.RNSanModule; MethodModule = NativeModules.RNSanModule;
} else { } else {
MethodModule = NativeModules.RNMethodModule; MethodModule = NativeModules.RNMethodModule;
} }
const OVER_TIME = 5*1000; const OVER_TIME = 5 * 1000;
const CONNECT_OVER_TIME = 35*1000; const CONNECT_OVER_TIME = 35 * 1000;
const COMMAND_TIME_INTERVAL = 200; const COMMAND_TIME_INTERVAL = 200;
const PROGRESS_TIME_INTERVAL =8*1000; const PROGRESS_TIME_INTERVAL = 8 * 1000;
const TAG = 'MY_RNDY'; const TAG = 'MY_RNDY';
if(__DEV__){ if (__DEV__) {
const version = 201704011315; const version = 201704011315;
console.log(TAG,'dyg driver version:',version); console.log(TAG, 'dyg driver version:', version);
} }
/** 监控的消息参数 */ /** 监控的消息参数 */
@ -35,7 +35,7 @@ const DYG_ON_GET_ALL_DATA = 'DYG_ON_GET_ALL_DATA';
const DYG_ON_START_MONITOR = 'DYG_ON_START_MONITOR'; const DYG_ON_START_MONITOR = 'DYG_ON_START_MONITOR';
const DYG_ON_STOP_MONITOR = 'DYG_ON_STOP_MONITOR'; const DYG_ON_STOP_MONITOR = 'DYG_ON_STOP_MONITOR';
const DYG_ON_UPDATE_NEW_DATA = 'DYG_ON_UPDATE_NEW_DATA'; const DYG_ON_UPDATE_NEW_DATA = 'DYG_ON_UPDATE_NEW_DATA';
const DYG_ON_GET_BATTERY = 'DYG_ON_GET_BATTERY'; const DYG_ON_GET_BATTERY = 'DYG_ON_GET_BATTERY';
/* =============================== 常量 ======================== */ /* =============================== 常量 ======================== */
const START_MONITOR = 1; const START_MONITOR = 1;
@ -57,8 +57,10 @@ const GET_BATTERY = 16;
let driver = null; let driver = null;
export default class RNDYGlucoseDriver{ export default class RNDYGlucoseDriver {
constructor(){ subscriptionMap = {};
constructor() {
this._eventMap = { this._eventMap = {
}; };
@ -82,8 +84,8 @@ export default class RNDYGlucoseDriver{
} }
/** 创建 */ /** 创建 */
static shareDriver(){ static shareDriver() {
if(driver === null){ if (driver === null) {
driver = new RNDYGlucoseDriver(); driver = new RNDYGlucoseDriver();
} }
@ -91,8 +93,8 @@ export default class RNDYGlucoseDriver{
} }
/** 开始扫描 */ /** 开始扫描 */
startScan(){ startScan() {
console.log(TAG,'开始扫描!!!!!!'); console.log(TAG, '开始扫描!!!!!!');
if (Platform.OS === 'ios') { if (Platform.OS === 'ios') {
NativeModules.RNMethodModule.startScanDynamicGlucose(); NativeModules.RNMethodModule.startScanDynamicGlucose();
NativeModules.RNMethodModule.forceUpdateDeviceList(); NativeModules.RNMethodModule.forceUpdateDeviceList();
@ -103,78 +105,78 @@ export default class RNDYGlucoseDriver{
} }
/** 停止扫描 */ /** 停止扫描 */
stopScan(){ stopScan() {
console.log(TAG, '停止扫描'); console.log(TAG, '停止扫描');
if (Platform.OS === 'ios') { if (Platform.OS === 'ios') {
NativeModules.RNMethodModule.stopScanDynamicGlucose(); NativeModules.RNMethodModule.stopScanDynamicGlucose();
}else if (Platform.OS === 'android'){ } else if (Platform.OS === 'android') {
MethodModule.stopScanDynamicGlucose(); MethodModule.stopScanDynamicGlucose();
} }
} }
set onUpdateDeviceList(delegate){ set onUpdateDeviceList(delegate) {
if(typeof delegate !== 'function'){ if (typeof delegate !== 'function') {
throw new Error('update device list must be a function'); throw new Error('update device list must be a function');
} }
this._onUpdateDeviceList = delegate; this._onUpdateDeviceList = delegate;
} }
set onUpdateNewData(delegate){ set onUpdateNewData(delegate) {
if(typeof delegate !== 'function'){ if (typeof delegate !== 'function') {
throw new Error('update device list must be a function'); throw new Error('update device list must be a function');
} }
this._onUpdateNewData = delegate; this._onUpdateNewData = delegate;
} }
/** 开始监控 */ /** 开始监控 */
startMonitor(deviceMac){ startMonitor(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject(new Error('paras type error')); reject(new Error('paras type error'));
return; return;
} }
console.log(TAG,'start monitor'); console.log(TAG, 'start monitor');
MethodModule.startMonitorDYG(deviceMac,START_MONITOR); MethodModule.startMonitorDYG(deviceMac, START_MONITOR);
this._eventMap[DYG_ON_START_MONITOR] = (deviceInfo)=>{ this._eventMap[DYG_ON_START_MONITOR] = (deviceInfo) => {
console.log(TAG,'resolve connect'); console.log(TAG, 'resolve connect');
resolve(deviceInfo.isSuc); resolve(deviceInfo.isSuc);
} }
}); });
const timer = this._timeoutPromise(OVER_TIME); const timer = this._timeoutPromise(OVER_TIME);
const mixPromise = Promise.race([devicePromise,timer]); const mixPromise = Promise.race([devicePromise, timer]);
return mixPromise; return mixPromise;
} }
/** 停止监控 */ /** 停止监控 */
stopMonitor(deviceMac){ stopMonitor(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject(new Error('paras type error')); reject(new Error('paras type error'));
return; return;
} }
console.log(TAG,'stop monitor'); console.log(TAG, 'stop monitor');
MethodModule.stopMonitorDYG(deviceMac,STOP_MONITOR); MethodModule.stopMonitorDYG(deviceMac, STOP_MONITOR);
this._eventMap[DYG_ON_STOP_MONITOR] = (deviceInfo)=>{ this._eventMap[DYG_ON_STOP_MONITOR] = (deviceInfo) => {
console.log(TAG,'resolve stop monitor'); console.log(TAG, 'resolve stop monitor');
resolve(deviceInfo.isSuc); resolve(deviceInfo.isSuc);
} }
}); });
const timer = this._timeoutPromise(OVER_TIME); const timer = this._timeoutPromise(OVER_TIME);
const mixPromise = Promise.race([devicePromise,timer]); const mixPromise = Promise.race([devicePromise, timer]);
return mixPromise; return mixPromise;
} }
/** 创建通道 */ /** 创建通道 */
async createAuthChannel(deviceMac){ async createAuthChannel(deviceMac) {
return new Promise(async (resolve,reject)=>{ return new Promise(async (resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
try{ try {
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
MethodModule.stopScanDynamicGlucose(); MethodModule.stopScanDynamicGlucose();
} else if (Platform.OS === 'ios') { } else if (Platform.OS === 'ios') {
@ -204,8 +206,8 @@ export default class RNDYGlucoseDriver{
} }
resolve(info); resolve(info);
}catch(e){ } catch (e) {
console.log(TAG,e); console.log(TAG, e);
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
MethodModule.startScanDynamicGlucose(); MethodModule.startScanDynamicGlucose();
} else if (Platform.OS === 'ios') { } else if (Platform.OS === 'ios') {
@ -218,18 +220,18 @@ export default class RNDYGlucoseDriver{
} }
/** 断开除指定设备 */ /** 断开除指定设备 */
disconnecAllDevicestWithout(deviceMac){ disconnecAllDevicestWithout(deviceMac) {
MethodModule.disconnecAllDYGDevicetWithout(deviceMac); MethodModule.disconnecAllDYGDevicetWithout(deviceMac);
} }
/** 开始连接单独设备 */ /** 开始连接单独设备 */
connectDevice(deviceMac){ connectDevice(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'driver auth'); console.log(TAG, 'driver auth');
MethodModule.connectDynamicGlucose(deviceMac); MethodModule.connectDynamicGlucose(deviceMac);
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
MethodModule.forceUpdateDeviceList(); MethodModule.forceUpdateDeviceList();
@ -237,27 +239,27 @@ export default class RNDYGlucoseDriver{
NativeModules.RNMethodModule.forceUpdateDeviceList(); NativeModules.RNMethodModule.forceUpdateDeviceList();
} }
this._eventMap[DYG_CREATED_CHANNEL] = (deviceInfo)=>{ this._eventMap[DYG_CREATED_CHANNEL] = (deviceInfo) => {
console.log(TAG,'resolve connect'); console.log(TAG, 'resolve connect');
resolve(deviceInfo); resolve(deviceInfo);
} }
}); });
const timer = this._timeoutPromise(CONNECT_OVER_TIME); const timer = this._timeoutPromise(CONNECT_OVER_TIME);
const mixPromise = Promise.race([devicePromise,timer]); const mixPromise = Promise.race([devicePromise, timer]);
return mixPromise; return mixPromise;
} }
/** 认证设备 */ /** 认证设备 */
doAuthorization(deviceMac){ doAuthorization(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'driver start'); console.log(TAG, 'driver start');
MethodModule.doAuthorizationDYG(deviceMac,START_MONITOR); MethodModule.doAuthorizationDYG(deviceMac, START_MONITOR);
this._eventMap[DYG_ON_DO_AUTHORIZATION] = (deviceInfo)=>{ this._eventMap[DYG_ON_DO_AUTHORIZATION] = (deviceInfo) => {
console.log(TAG,'resolve auth'); console.log(TAG, 'resolve auth');
resolve(deviceInfo); resolve(deviceInfo);
} }
}); });
@ -265,16 +267,16 @@ export default class RNDYGlucoseDriver{
} }
/** 获得状态 */ /** 获得状态 */
getMonitorStatus(deviceMac){ getMonitorStatus(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'get monitor status'); console.log(TAG, 'get monitor status');
MethodModule.getMonitorStatusDYG(deviceMac,GET_MONITOR_STATUS); MethodModule.getMonitorStatusDYG(deviceMac, GET_MONITOR_STATUS);
this._eventMap[DYG_ON_GET_MONITOR_STATUS] = (deviceInfo)=>{ this._eventMap[DYG_ON_GET_MONITOR_STATUS] = (deviceInfo) => {
console.log(TAG,'resolve get monitor status'); console.log(TAG, 'resolve get monitor status');
resolve(deviceInfo.isStartMonitor); resolve(deviceInfo.isStartMonitor);
} }
}); });
@ -282,76 +284,76 @@ export default class RNDYGlucoseDriver{
} }
/** 获得名字 */ /** 获得名字 */
getName(deviceMac){ getName(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'get monitor status'); console.log(TAG, 'get monitor status');
MethodModule.getNameDYG(deviceMac,GET_NAME); MethodModule.getNameDYG(deviceMac, GET_NAME);
}); });
return this._mixTimeoutPromise(devicePromise); return this._mixTimeoutPromise(devicePromise);
} }
/** 获得版本号 */ /** 获得版本号 */
getVersion(deviceMac){ getVersion(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'get monitor status'); console.log(TAG, 'get monitor status');
MethodModule.getVersionDYG(deviceMac,getVersion); MethodModule.getVersionDYG(deviceMac, getVersion);
}); });
return this._mixTimeoutPromise(devicePromise); return this._mixTimeoutPromise(devicePromise);
} }
/** 获得序列号 */ /** 获得序列号 */
getSN(deviceMac){ getSN(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'get monitor status'); console.log(TAG, 'get monitor status');
MethodModule.getSnDYG(deviceMac,getSN); MethodModule.getSnDYG(deviceMac, getSN);
}); });
return this._mixTimeoutPromise(devicePromise); return this._mixTimeoutPromise(devicePromise);
} }
/** 获得电池电量 */ /** 获得电池电量 */
getBattery(deviceMac){ getBattery(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'get battery status'); console.log(TAG, 'get battery status');
MethodModule.getBatteryDYG(deviceMac,GET_BATTERY); MethodModule.getBatteryDYG(deviceMac, GET_BATTERY);
this._eventMap[DYG_ON_GET_BATTERY] = (deviceInfo)=>{ this._eventMap[DYG_ON_GET_BATTERY] = (deviceInfo) => {
console.log(TAG,'resolve '+DYG_ON_GET_BATTERY); console.log(TAG, 'resolve ' + DYG_ON_GET_BATTERY);
const {value} = deviceInfo; const { value } = deviceInfo;
const percent = (value-2.85)/(2.95-2.85); // 改成2.95貌似比较合适 const percent = (value - 2.85) / (2.95 - 2.85); // 改成2.95貌似比较合适
resolve(percent); resolve(percent);
} }
}); });
return this._mixTimeoutPromise(devicePromise); return this._mixTimeoutPromise(devicePromise);
} }
calculateDyRoc(index,refValue,values){ calculateDyRoc(index, refValue, values) {
return new Promise(async (resolve,reject)=>{ return new Promise(async (resolve, reject) => {
const info = { const info = {
index:index, index: index,
refValue:refValue, refValue: refValue,
values:values values: values
} }
try{ try {
const values = await MethodModule.caculateDyRoc(info); const values = await MethodModule.caculateDyRoc(info);
resolve(values); resolve(values);
}catch(e){ } catch (e) {
} }
}) })
@ -363,25 +365,25 @@ export default class RNDYGlucoseDriver{
* finish(error,[]); * finish(error,[]);
* progress(currentCount,amountCount,[],[]) * progress(currentCount,amountCount,[],[])
*/ */
async getAllData(deviceMac,progress,finish){ async getAllData(deviceMac, progress, finish) {
if(typeof deviceMac !== 'string' || typeof progress !== 'function' || typeof finish !== 'function'){ if (typeof deviceMac !== 'string' || typeof progress !== 'function' || typeof finish !== 'function') {
throw new Error('paras type error'); throw new Error('paras type error');
} }
console.log(TAG,'get all data'); console.log(TAG, 'get all data');
let receiveData = null; let receiveData = null;
let dataList = []; let dataList = [];
let timer = null; let timer = null;
try{ try {
const count = await this.getAllDataCount(deviceMac); const count = await this.getAllDataCount(deviceMac);
if(count===0){ if (count === 0) {
finish(null,[]); finish(null, []);
return; return;
} }
receiveData = (deviceInfo)=>{ receiveData = (deviceInfo) => {
if(deviceInfo.deviceMac !== deviceMac){ if (deviceInfo.deviceMac !== deviceMac) {
return; return;
} }
@ -389,59 +391,71 @@ export default class RNDYGlucoseDriver{
dataList = dataList.concat(deviceInfo.dataList); dataList = dataList.concat(deviceInfo.dataList);
if(dataList.length>=count){ if (dataList.length >= count) {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
finish(null,dataList); if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(null, dataList);
return; return;
}else{ } else {
progress(dataList.length,count,deviceInfo.dataList,dataList); progress(dataList.length, count, deviceInfo.dataList, dataList);
} }
timer = setTimeout(()=>{ timer = setTimeout(() => {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(new Error('receive data time out')) finish(new Error('receive data time out'))
},PROGRESS_TIME_INTERVAL); }, PROGRESS_TIME_INTERVAL);
}; };
DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA,receiveData); this.subscriptionMap[DYG_ON_GET_ALL_DATA] = DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA, receiveData);
await this._commadTimeInterval(COMMAND_TIME_INTERVAL); await this._commadTimeInterval(COMMAND_TIME_INTERVAL);
MethodModule.getAllDataDYG(deviceMac,GET_ALL_DATA); MethodModule.getAllDataDYG(deviceMac, GET_ALL_DATA);
timer = setTimeout(()=>{ timer = setTimeout(() => {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(new Error('receive data time out')) finish(new Error('receive data time out'))
},PROGRESS_TIME_INTERVAL); }, PROGRESS_TIME_INTERVAL);
}catch(e){ } catch (e) {
console.log(TAG,e); console.log(TAG, e);
finish(e); finish(e);
} }
} }
async getAllDataFromIndex(deviceMac,index,progress,finish){ async getAllDataFromIndex(deviceMac, index, progress, finish) {
if(typeof deviceMac !== 'string' || typeof progress !== 'function' || typeof finish !== 'function'){ if (typeof deviceMac !== 'string' || typeof progress !== 'function' || typeof finish !== 'function') {
throw new Error('paras type error'); throw new Error('paras type error');
} }
// index = 45 // index = 45
console.log(TAG,'get data from index'); console.log(TAG, 'get data from index');
let receiveData = null; let receiveData = null;
let dataList = []; let dataList = [];
let timer = null; let timer = null;
try{ try {
let count = await this.getAllDataCount(deviceMac); let count = await this.getAllDataCount(deviceMac);
if(count===0 || index>=count){ if (count === 0 || index >= count) {
finish(null,[]); finish(null, []);
return; return;
} }
count = count-index; count = count - index;
console.log(TAG,'count:',count); console.log(TAG, 'count:', count);
receiveData = (deviceInfo)=>{ receiveData = (deviceInfo) => {
if(deviceInfo.deviceMac !== deviceMac){ if (deviceInfo.deviceMac !== deviceMac) {
return; return;
} }
@ -449,91 +463,116 @@ export default class RNDYGlucoseDriver{
dataList = dataList.concat(deviceInfo.dataList); dataList = dataList.concat(deviceInfo.dataList);
console.log(TAG, 'rcv data count', dataList.length); console.log(TAG, 'rcv data count', dataList.length);
if(dataList.length>=count){ if (dataList.length >= count) {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
finish(null,dataList); if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(null, dataList);
return; return;
}else{ } else {
progress(dataList.length,count,deviceInfo.dataList,dataList); progress(dataList.length, count, deviceInfo.dataList, dataList);
} }
timer = setTimeout(()=>{ timer = setTimeout(() => {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(new Error('receive data time out')) finish(new Error('receive data time out'))
},PROGRESS_TIME_INTERVAL); }, PROGRESS_TIME_INTERVAL);
}; };
DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA,receiveData); this.subscriptionMap[DYG_ON_GET_ALL_DATA] = DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA, receiveData);
await this._commadTimeInterval(COMMAND_TIME_INTERVAL); await this._commadTimeInterval(COMMAND_TIME_INTERVAL);
MethodModule.getAllDataFromIndexDYG(deviceMac,GET_ALL_DATA_FROM_INDEX,index); MethodModule.getAllDataFromIndexDYG(deviceMac, GET_ALL_DATA_FROM_INDEX, index);
timer = setTimeout(()=>{ timer = setTimeout(() => {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(new Error('receive data time out')) finish(new Error('receive data time out'))
},PROGRESS_TIME_INTERVAL); }, PROGRESS_TIME_INTERVAL);
}catch(e){ } catch (e) {
console.log(TAG,e); console.log(TAG, e);
finish(e); finish(e);
} }
} }
async getNumberDataFromIndex(deviceMac,index,number,progress,finish){ async getNumberDataFromIndex(deviceMac, index, number, progress, finish) {
if(typeof deviceMac !== 'string' || typeof progress !== 'function' || typeof finish !== 'function'){ if (typeof deviceMac !== 'string' || typeof progress !== 'function' || typeof finish !== 'function') {
throw new Error('paras type error'); throw new Error('paras type error');
} }
console.log(TAG,'get data from index'); console.log(TAG, 'get data from index');
let receiveData = null; let receiveData = null;
let dataList = []; let dataList = [];
let timer = null; let timer = null;
try{ try {
let count = await this.getAllDataCount(deviceMac); let count = await this.getAllDataCount(deviceMac);
if(count===0 || index>=count){ if (count === 0 || index >= count) {
finish(null,[]); finish(null, []);
return; return;
} }
count = (count-index)>number?number:(count-index); count = (count - index) > number ? number : (count - index);
console.log(TAG,'count:',count); console.log(TAG, 'count:', count);
receiveData = (deviceInfo)=>{ receiveData = (deviceInfo) => {
if(deviceInfo.deviceMac !== deviceMac){ if (deviceInfo.deviceMac !== deviceMac) {
return; return;
} }
clearTimeout(timer); clearTimeout(timer);
dataList = dataList.concat(deviceInfo.dataList); dataList = dataList.concat(deviceInfo.dataList);
console.log(TAG, 'rcv device data length:', dataList.length); console.log(TAG, 'rcv device data length:', dataList.length);
if(dataList.length>=count){ if (dataList.length >= count) {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
finish(null,dataList); if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(null, dataList);
return; return;
}else{ } else {
progress(dataList.length,count,deviceInfo.dataList,dataList); progress(dataList.length, count, deviceInfo.dataList, dataList);
} }
timer = setTimeout(()=>{ timer = setTimeout(() => {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(new Error('receive data time out')); finish(new Error('receive data time out'));
},PROGRESS_TIME_INTERVAL); }, PROGRESS_TIME_INTERVAL);
}; };
DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA,receiveData); this.subscriptionMap[DYG_ON_GET_ALL_DATA] = DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA, receiveData);
await this._commadTimeInterval(COMMAND_TIME_INTERVAL); await this._commadTimeInterval(COMMAND_TIME_INTERVAL);
MethodModule.getNumberDataFromIndexDYG(deviceMac,GET_NUMBER_DATA_FROM_INDEX,index,count); MethodModule.getNumberDataFromIndexDYG(deviceMac, GET_NUMBER_DATA_FROM_INDEX, index, count);
timer = setTimeout(()=>{ timer = setTimeout(() => {
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA,receiveData); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA, receiveData);
if (this.subscriptionMap['DYG_ON_GET_ALL_DATA']) {
this.subscriptionMap['DYG_ON_GET_ALL_DATA'].remove();
delete this.subscriptionMap['DYG_ON_GET_ALL_DATA']; // 可选,从映射中删除键,避免内存泄漏
}
finish(new Error('receive data time out')); finish(new Error('receive data time out'));
},PROGRESS_TIME_INTERVAL); }, PROGRESS_TIME_INTERVAL);
}catch(e){ } catch (e) {
console.log(TAG,e); console.log(TAG, e);
finish(e); finish(e);
} }
} }
@ -541,16 +580,16 @@ export default class RNDYGlucoseDriver{
/** 得到所有数据数量 */ /** 得到所有数据数量 */
getAllDataCount(deviceMac){ getAllDataCount(deviceMac) {
const devicePromise = new Promise((resolve,reject)=>{ const devicePromise = new Promise((resolve, reject) => {
if(typeof deviceMac !== 'string'){ if (typeof deviceMac !== 'string') {
reject('paras type error'); reject('paras type error');
return; return;
} }
console.log(TAG,'get all data count'); console.log(TAG, 'get all data count');
MethodModule.getAllDataCountDYG(deviceMac,GET_ALL_DATA_COUNT); MethodModule.getAllDataCountDYG(deviceMac, GET_ALL_DATA_COUNT);
this._eventMap[DYG_ON_GET_ALL_DATA_COUNT] = (deviceInfo)=>{ this._eventMap[DYG_ON_GET_ALL_DATA_COUNT] = (deviceInfo) => {
console.log(TAG,'resolve '+DYG_ON_GET_ALL_DATA_COUNT); console.log(TAG, 'resolve ' + DYG_ON_GET_ALL_DATA_COUNT);
resolve(deviceInfo.count); resolve(deviceInfo.count);
} }
}); });
@ -567,152 +606,160 @@ export default class RNDYGlucoseDriver{
} }
calculateBloodGlucose(index,refValue,values){ calculateBloodGlucose(index, refValue, values) {
return new Promise(async (resolve,reject)=>{ return new Promise(async (resolve, reject) => {
const info = { const info = {
index:index, index: index,
refValue:refValue, refValue: refValue,
values:values values: values
} }
try{ try {
const values = await MethodModule.caculateBloodGlucose(info); const values = await MethodModule.caculateBloodGlucose(info);
resolve(values); resolve(values);
}catch(e){ } catch (e) {
} }
}) })
} }
_mixTimeoutPromise(devicePromise){ _mixTimeoutPromise(devicePromise) {
const timer = this._timeoutPromise(OVER_TIME); const timer = this._timeoutPromise(OVER_TIME);
const mixPromise = Promise.race([devicePromise,timer]); const mixPromise = Promise.race([devicePromise, timer]);
return mixPromise; return mixPromise;
} }
_timeoutPromise(delay){ _timeoutPromise(delay) {
return new Promise((resolve,reject)=>{ return new Promise((resolve, reject) => {
setTimeout(()=>{ setTimeout(() => {
reject(new Error('timeout')); reject(new Error('timeout'));
},delay); }, delay);
}); });
} }
_commadTimeInterval(interval){ _commadTimeInterval(interval) {
return new Promise((resolve,reject)=>{ return new Promise((resolve, reject) => {
setTimeout(()=>{ setTimeout(() => {
resolve('command await time out'); resolve('command await time out');
},interval); }, interval);
}); });
} }
_registerEvent(eventName){ _registerEvent(eventName) {
this._eventMap[eventName] = (deviceInfo)=>{ this._eventMap[eventName] = (deviceInfo) => {
console.log(TAG,'resolve '+eventName); console.log(TAG, 'resolve ' + eventName);
resolve(deviceInfo); resolve(deviceInfo);
} }
} }
_isFunctionEmpty(event){ _isFunctionEmpty(event) {
if(event===undefined || event===null){ if (event === undefined || event === null) {
return true; return true;
} }
return false; return false;
} }
_executeEvent(eventName,deviceInfo){ _executeEvent(eventName, deviceInfo) {
const event = this._eventMap[eventName]; const event = this._eventMap[eventName];
if(this._isFunctionEmpty(event)){ if (this._isFunctionEmpty(event)) {
return; return;
} }
event(deviceInfo); event(deviceInfo);
this._eventMap[eventName] = null; this._eventMap[eventName] = null;
} }
/** 监控底层驱动发送的消息 */ /** 监控底层驱动发送的消息 */
startMonitorDriverEvent(){ startMonitorDriverEvent() {
this._createChannel = (deviceInfo)=>{ this._createChannel = (deviceInfo) => {
console.log(TAG,'created channel'); console.log(TAG, 'created channel');
this._executeEvent(DYG_CREATED_CHANNEL,deviceInfo); this._executeEvent(DYG_CREATED_CHANNEL, deviceInfo);
}; };
DeviceEventEmitter.addListener(DYG_CREATED_CHANNEL,this._createChannel); this.subscriptionMap['DYG_CREATED_CHANNEL'] = DeviceEventEmitter.addListener(DYG_CREATED_CHANNEL, this._createChannel);
this._onDoAuthorization = (deviceInfo)=>{ this._onDoAuthorization = (deviceInfo) => {
console.log(TAG,'received '+DYG_ON_DO_AUTHORIZATION); console.log(TAG, 'received ' + DYG_ON_DO_AUTHORIZATION);
this._executeEvent(DYG_ON_DO_AUTHORIZATION,deviceInfo); this._executeEvent(DYG_ON_DO_AUTHORIZATION, deviceInfo);
}; };
DeviceEventEmitter.addListener(DYG_ON_DO_AUTHORIZATION,this._onDoAuthorization); this.subscriptionMap[DYG_ON_DO_AUTHORIZATION] = DeviceEventEmitter.addListener(DYG_ON_DO_AUTHORIZATION, this._onDoAuthorization);
this._onGetBattery = (deviceInfo)=>{ this._onGetBattery = (deviceInfo) => {
console.log(TAG,'received '+DYG_ON_GET_BATTERY); console.log(TAG, 'received ' + DYG_ON_GET_BATTERY);
this._executeEvent(DYG_ON_GET_BATTERY,deviceInfo); this._executeEvent(DYG_ON_GET_BATTERY, deviceInfo);
}; };
DeviceEventEmitter.addListener(DYG_ON_GET_BATTERY,this._onGetBattery); this.subscriptionMap[DYG_ON_GET_BATTERY] = DeviceEventEmitter.addListener(DYG_ON_GET_BATTERY, this._onGetBattery);
this._onGetAllDataCount = (deviceInfo)=>{ this._onGetAllDataCount = (deviceInfo) => {
console.log(TAG,'received '+DYG_ON_GET_ALL_DATA_COUNT); console.log(TAG, 'received ' + DYG_ON_GET_ALL_DATA_COUNT);
this._executeEvent(DYG_ON_GET_ALL_DATA_COUNT,deviceInfo); this._executeEvent(DYG_ON_GET_ALL_DATA_COUNT, deviceInfo);
}; };
DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA_COUNT,this._onGetAllDataCount); this.subscriptionMap[DYG_ON_GET_ALL_DATA_COUNT] = DeviceEventEmitter.addListener(DYG_ON_GET_ALL_DATA_COUNT, this._onGetAllDataCount);
this._updateDeviceList = (deviceInfos)=>{ this._updateDeviceList = (deviceInfos) => {
// console.log(TAG,deviceInfos); // console.log(TAG,deviceInfos);
if(this._onUpdateDeviceList === null){ if (this._onUpdateDeviceList === null) {
return; return;
} }
const deviceList = deviceInfos.filter((device)=>{ const deviceList = deviceInfos.filter((device) => {
return device.deviceName.indexOf('TH-')>=0 return device.deviceName.indexOf('TH-') >= 0
}); });
this._onUpdateDeviceList(deviceList); this._onUpdateDeviceList(deviceList);
}; };
DeviceEventEmitter.addListener(UPDATE_DEVICE_LIST,this._updateDeviceList); this.subscriptionMap[UPDATE_DEVICE_LIST] = DeviceEventEmitter.addListener(UPDATE_DEVICE_LIST, this._updateDeviceList);
/** 获得当前监控的状态 */ /** 获得当前监控的状态 */
this._onGetMonitorStatus = (deviceInfo)=>{ this._onGetMonitorStatus = (deviceInfo) => {
this._executeEvent(DYG_ON_GET_MONITOR_STATUS,deviceInfo); this._executeEvent(DYG_ON_GET_MONITOR_STATUS, deviceInfo);
} }
DeviceEventEmitter.addListener(DYG_ON_GET_MONITOR_STATUS,this._onGetMonitorStatus); this.subscriptionMap[DYG_ON_GET_MONITOR_STATUS] = DeviceEventEmitter.addListener(DYG_ON_GET_MONITOR_STATUS, this._onGetMonitorStatus);
/** 获得是否已经开始监控 */ /** 获得是否已经开始监控 */
this._onStartMonitor = (deviceInfo)=>{ this._onStartMonitor = (deviceInfo) => {
this._executeEvent(DYG_ON_START_MONITOR,deviceInfo); this._executeEvent(DYG_ON_START_MONITOR, deviceInfo);
} }
DeviceEventEmitter.addListener(DYG_ON_START_MONITOR,this._onStartMonitor); this.subscriptionMap[DYG_ON_START_MONITOR] = DeviceEventEmitter.addListener(DYG_ON_START_MONITOR, this._onStartMonitor);
this._onStopMonitor = (deviceInfo)=>{ this._onStopMonitor = (deviceInfo) => {
this._executeEvent(DYG_ON_STOP_MONITOR,deviceInfo); this._executeEvent(DYG_ON_STOP_MONITOR, deviceInfo);
} }
DeviceEventEmitter.addListener(DYG_ON_STOP_MONITOR,this._onStopMonitor); this.subscriptionMap[DYG_ON_STOP_MONITOR] = DeviceEventEmitter.addListener(DYG_ON_STOP_MONITOR, this._onStopMonitor);
/** 监听接收到的单条新消息 */ /** 监听接收到的单条新消息 */
this._updateNewData = (deviceInfo)=>{ this._updateNewData = (deviceInfo) => {
if(this._onUpdateNewData === null){ if (this._onUpdateNewData === null) {
return; return;
} }
this._onUpdateNewData(deviceInfo.index,deviceInfo.value); this._onUpdateNewData(deviceInfo.index, deviceInfo.value);
} }
DeviceEventEmitter.addListener(DYG_ON_UPDATE_NEW_DATA,this._updateNewData); this.subscriptionMap[DYG_ON_UPDATE_NEW_DATA] = DeviceEventEmitter.addListener(DYG_ON_UPDATE_NEW_DATA, this._updateNewData);
} }
endMonitorDriverEvent(){ endMonitorDriverEvent() {
DeviceEventEmitter.removeListener(DYG_CREATED_CHANNEL,this._createChannel); // DeviceEventEmitter.removeListener(DYG_CREATED_CHANNEL,this._createChannel);
DeviceEventEmitter.removeListener(DYG_ON_DO_AUTHORIZATION,this._onDoAuthorization); // DeviceEventEmitter.removeListener(DYG_ON_DO_AUTHORIZATION,this._onDoAuthorization);
DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA_COUNT,this._onGetAllDataCount); // DeviceEventEmitter.removeListener(DYG_ON_GET_ALL_DATA_COUNT,this._onGetAllDataCount);
DeviceEventEmitter.removeListener(DYG_ON_GET_MONITOR_STATUS,this._onGetMonitorStatus); // DeviceEventEmitter.removeListener(DYG_ON_GET_MONITOR_STATUS,this._onGetMonitorStatus);
DeviceEventEmitter.removeListener(UPDATE_DEVICE_LIST,this._updateDeviceList); // DeviceEventEmitter.removeListener(UPDATE_DEVICE_LIST,this._updateDeviceList);
DeviceEventEmitter.removeListener(DYG_ON_START_MONITOR,this._onStartMonitor); // DeviceEventEmitter.removeListener(DYG_ON_START_MONITOR,this._onStartMonitor);
DeviceEventEmitter.removeListener(DYG_ON_STOP_MONITOR,this._onStopMonitor); // DeviceEventEmitter.removeListener(DYG_ON_STOP_MONITOR,this._onStopMonitor);
DeviceEventEmitter.removeListener(DYG_ON_UPDATE_NEW_DATA,this._updateNewData); // DeviceEventEmitter.removeListener(DYG_ON_UPDATE_NEW_DATA,this._updateNewData);
DeviceEventEmitter.removeListener(DYG_ON_GET_BATTERY,this._onGetBattery); // DeviceEventEmitter.removeListener(DYG_ON_GET_BATTERY,this._onGetBattery);
// 移除监听器
Object.keys(this.subscriptionMap).forEach(key => {
this.subscriptionMap[key].remove();
});
// 清除订阅对象记录,避免内存泄漏
this.subscriptionMap = {};
} }
} }