/**
 * 绑定加速计
 */
function bindAcc(callback,callbackValue) {
	let id = null;
	console.log("监听加速计");
	id = plus.accelerometer.watchAcceleration((a) => {
		uni.$emit('watchAcceleration', a);
		if(callbackValue){
			callbackValue(a);
		}
		// console.log(a);
	}, function(e) {
		console.log("监听失败:" + e.message);
		if (id) {
			plus.accelerometer.clearWatch(id);
			id = null;
		}
	}, {
		frequency: 20
	});
	console.log("监听加速计 id:",id);
	callback(id);
}

/**
 * 绑定陀螺仪
 */
function bindOri(callback,callbackValue) {
	let id = null;
	console.log("监听陀螺仪");
	id = plus.orientation.watchOrientation(function(o) {
		uni.$emit('watchOrientation', o);
		if(callbackValue){
			callbackValue(o);
		}
	}, function(e) {
		console.log("监听失败:" + e.message);
		if (id) {
			plus.orientation.clearWatch(id);
			id = null;
		}
	}, {
		frequency: 60
	});
	console.log("监听陀螺仪 id:",id);
	callback(id);
}

/**
 * 解除加速计绑定
 */
function unBindAcc(accId) {
	console.log("解除加速计绑定:",accId);
	if (accId) {
		plus.accelerometer.clearWatch(accId);
	} else {
		console.log("没有监听设备加速变化");
	}
}
/**
 * 解除陀螺仪绑定
 */
function unBindOri(oriId) {
	console.log("解除陀螺仪绑定:",oriId);
	if (oriId) {
		plus.orientation.clearWatch(oriId);
	} else {
		console.log("没有监听设备方向变化");
	}
}


export default {
	bindAcc,
	unBindAcc,
	bindOri,
	unBindOri
}
