在第一次完成之前,不允许使用相同参数调用订户两次
Do not allow call of a subscriber with the same argument twice until the first is done
我目前正在试验信标,我想获取信标的 RSSI 测量值以触发警报功能。
我的问题如下:
alarm
function is triggered multiple times once the subscriber is
called
我想要的:
Do not allow alarm
to execute more than once with the previous argument. For example if the alarm function is called with argument
'x'
then someone cannot call alarm with 'x'
but can call it with
'y'
in order to avoid multiple pushes to the database.
我的代码(我将 noble 用于信标,将 firebase 用于我的数据库):
// alarm script
import { config, Device, db, auth, productsRef } from './index';
import * as noble from 'noble';
import * as firebase from 'firebase';
var acquiredRef = db.ref('acquired');
function condition(p) {
// a boolean function that does something
}
function alarm (address) {
console.log('Alarm!');
let incident = {
date : Date(),
uuid : address,
}
db.ref('alarms').push(incident);
}
if (require.main === module) {
noble.on('stateChange', function (state) {
if (state === 'poweredOn') noble.startScanning([], true);
else noble.stopScanning();
});
noble.on('discover', function(peripheral) {
peripheral.address = peripheral.address.toUpperCase();
if (condition(peripheral)) {
// query
acquiredRef.orderByChild('uuid').equalTo(peripheral.address).once('value')
.then(snap => {
if(snap.val() == null) alarm(peripheral.address);
}).catch(err => console.log(err))
}
});
}
我尝试使用队列和异步锁但没有成功。它在第一次调用警报时起作用,然后具有与上述相同的行为。
提前致谢
我遇到了和你类似的问题。
就我而言,一个简单的经过时间变量就可以解决问题。这是必要的,因为发现回调在同一个外围设备上执行多次。
看看吧
noble.on('discover', function(peripheral) {
if (peripheral.advertisement.localName == 'FinishLine') {
if (lastDetection == null) {
alert();
} else {
if ((Date.now() - lastDetection) / 1000 > 2) {
alert();
}
}
}
});
由于异步 firebase 调用行为,您不能依赖查询。像我一样暂停 2 秒或更长时间可能会解决您的问题。
如果您只想确保警报函数不会使用相同的参数(在本例中只是字符串 'address')连续调用两次(或更多次),您可以只存储一个变量并将您最近的参数与当前参数进行比较。如果参数相同,则不执行函数的其余部分。
let previousAddress = null;
function alarm (address) {
if (address === previousAddress) {
return; // skip
}
console.log('Alarm!');
let incident = {
date : Date(),
uuid : address,
}
db.ref('alarms').push(incident);
previousAddress = address; // update previous argument
}
我目前正在试验信标,我想获取信标的 RSSI 测量值以触发警报功能。
我的问题如下:
alarm
function is triggered multiple times once the subscriber is called
我想要的:
Do not allow
alarm
to execute more than once with the previous argument. For example if the alarm function is called with argument'x'
then someone cannot call alarm with'x'
but can call it with'y'
in order to avoid multiple pushes to the database.
我的代码(我将 noble 用于信标,将 firebase 用于我的数据库):
// alarm script
import { config, Device, db, auth, productsRef } from './index';
import * as noble from 'noble';
import * as firebase from 'firebase';
var acquiredRef = db.ref('acquired');
function condition(p) {
// a boolean function that does something
}
function alarm (address) {
console.log('Alarm!');
let incident = {
date : Date(),
uuid : address,
}
db.ref('alarms').push(incident);
}
if (require.main === module) {
noble.on('stateChange', function (state) {
if (state === 'poweredOn') noble.startScanning([], true);
else noble.stopScanning();
});
noble.on('discover', function(peripheral) {
peripheral.address = peripheral.address.toUpperCase();
if (condition(peripheral)) {
// query
acquiredRef.orderByChild('uuid').equalTo(peripheral.address).once('value')
.then(snap => {
if(snap.val() == null) alarm(peripheral.address);
}).catch(err => console.log(err))
}
});
}
我尝试使用队列和异步锁但没有成功。它在第一次调用警报时起作用,然后具有与上述相同的行为。
提前致谢
我遇到了和你类似的问题。
就我而言,一个简单的经过时间变量就可以解决问题。这是必要的,因为发现回调在同一个外围设备上执行多次。
看看吧
noble.on('discover', function(peripheral) {
if (peripheral.advertisement.localName == 'FinishLine') {
if (lastDetection == null) {
alert();
} else {
if ((Date.now() - lastDetection) / 1000 > 2) {
alert();
}
}
}
});
由于异步 firebase 调用行为,您不能依赖查询。像我一样暂停 2 秒或更长时间可能会解决您的问题。
如果您只想确保警报函数不会使用相同的参数(在本例中只是字符串 'address')连续调用两次(或更多次),您可以只存储一个变量并将您最近的参数与当前参数进行比较。如果参数相同,则不执行函数的其余部分。
let previousAddress = null;
function alarm (address) {
if (address === previousAddress) {
return; // skip
}
console.log('Alarm!');
let incident = {
date : Date(),
uuid : address,
}
db.ref('alarms').push(incident);
previousAddress = address; // update previous argument
}