Home Do not allow call of a subscriber with the same argument twice until the first is done
Reply: 0

Do not allow call of a subscriber with the same argument twice until the first is done

user5376
1#
user5376 Published in September 21, 2018, 8:07 am

I am currently experimenting with beacons and I want to get the rssi measurements of the beacons in order to trigger an alarm function.

My problem is the following:

alarm function is triggered multiple times once the subscriber is called

What I want:

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.

My code (I am using noble for beacons and firebase for my db):

// 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))
      }

  });

}

I tried using queues and async-lock without success. It works the first time alarm is called and then has the same behaviour as above.

Thanks in advance

share|improve this question

2 Answers 2

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.503001 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO