feature():根据前后台自动切换时间间隔的实现模式

This commit is contained in:
lvwang2002 2024-10-26 07:04:32 +08:00
parent 8141e30438
commit d7abb64deb
1 changed files with 31 additions and 3 deletions

View File

@ -1,6 +1,6 @@
'use strict';
import React, { Component } from 'react';
import { NativeModules, DeviceEventEmitter, Platform } from 'react-native';
import { NativeModules, DeviceEventEmitter, Platform,AppState, } from 'react-native';
@ -74,9 +74,11 @@ export default class RNDYGlucoseDriver {
this._timeoutPromise = this._timeoutPromise.bind(this);
this.connectDevice = this.connectDevice.bind(this);
this._mixTimeoutPromise = this._mixTimeoutPromise.bind(this);
this._blockSleep = this._blockSleep.bind(this);
this._executeEvent = this._executeEvent.bind(this);
this._registerEvent = this._registerEvent.bind(this);
this._commadTimeInterval = this._commadTimeInterval.bind(this);
this._commadTimeIntervalWithTimer = this._commadTimeIntervalWithTimer.bind(this);
this.createAuthChannel = this.createAuthChannel.bind(this);
this.disconnecAllDevicestWithout = this.disconnecAllDevicestWithout.bind(this);
this.getAllDataFromIndex = this.getAllDataFromIndex.bind(this);
@ -335,7 +337,7 @@ export default class RNDYGlucoseDriver {
this._eventMap[DYG_ON_GET_BATTERY] = (deviceInfo) => {
console.log(TAG, 'resolve ' + DYG_ON_GET_BATTERY);
const { value } = deviceInfo;
resolve(percent);
resolve(value);
}
});
return this._mixTimeoutPromise(devicePromise);
@ -637,7 +639,7 @@ export default class RNDYGlucoseDriver {
});
}
_commadTimeInterval(interval) {
_commadTimeIntervalWithTimer(interval) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('command await time out');
@ -645,6 +647,30 @@ export default class RNDYGlucoseDriver {
});
}
// 阻塞方法,模拟 sleep 功能
_blockSleep(milliseconds) {
return new Promise(resolve => {
const start = new Date().getTime();
let current = start;
while (current - start < milliseconds) {
current = new Date().getTime();
}
resolve();
});
}
_commadTimeInterval(interval) {
if (Platform.OS === 'android' && AppState.currentState !== 'active') {
// 如果在后台运行,使用阻塞方法
console.log(TAG,'start blocker');
return this._blockSleep(interval);
} else {
// 如果在前台运行,使用 setTimeout
console.log(TAG,'start timer');
return this._commadTimeIntervalWithTimer(interval);
}
}
_registerEvent(eventName) {
this._eventMap[eventName] = (deviceInfo) => {
@ -670,6 +696,8 @@ export default class RNDYGlucoseDriver {
this._eventMap[eventName] = null;
}
/** 监控底层驱动发送的消息 */
startMonitorDriverEvent() {
this._createChannel = (deviceInfo) => {