智能照明系统APP-本地串口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LightingSystemApp-serial/.svn/pristine/2b/2b5a12f2bc2e34f270341263dfd...

56 lines
1.1 KiB

3 years ago
import {
DEVICE_FREQUENCY
} from '../constants'
import {
getLastWebview
} from '../util'
import {
publish
} from '../../bridge'
let watchOrientationId = false
let isWatchOrientation = false
const clearWatchOrientation = () => {
if (watchOrientationId) {
plus.orientation.clearWatch(watchOrientationId)
watchOrientationId = false
}
}
export function enableCompass ({
enable
}) {
if (enable) {
clearWatchOrientation()
watchOrientationId = plus.orientation.watchOrientation((o) => {
publish('onCompassChange', {
direction: o.magneticHeading,
errMsg: 'enableCompass:ok'
})
}, (e) => {
publish('onCompassChange', {
errMsg: 'enableCompass:fail'
})
}, {
frequency: DEVICE_FREQUENCY
})
if (!isWatchOrientation) {
isWatchOrientation = true
const webview = getLastWebview()
if (webview) {
webview.addEventListener('close', () => {
plus.orientation.clearWatch(watchOrientationId)
})
}
}
} else {
clearWatchOrientation()
}
return {
errMsg: 'enableCompass:ok'
}
}