【发布时间】:2018-02-12 15:10:47
【问题描述】:
我目前正在试验信标,我想获得信标的 rssi 测量值以触发警报功能。
我的问题如下:
alarm函数一旦订阅者被触发多次 叫
我想要什么:
不允许
alarm使用前一个参数多次执行。例如,如果使用参数调用警报函数'x'然后有人不能用'x'打电话报警,但可以用'y'以避免多次推送到数据库。
我的代码(我使用高贵的信标和我的数据库的火力基地):
// 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))
}
});
}
我尝试使用队列和异步锁但没有成功。它在第一次调用警报时起作用,然后具有与上述相同的行为。
提前致谢
【问题讨论】:
标签: node.js firebase asynchronous concurrency beacon