node-switchbot icon indicating copy to clipboard operation
node-switchbot copied to clipboard

[help] Scan unique ids only

Open duceduc opened this issue 5 years ago • 6 comments

Hello. The below code will scan and display any devices it finds within xx seconds. This will include duplicates. How to display only unique ids'. Thank you.

// Load the node-switchbot and get a `Switchbot` constructor object
const Switchbot = require('node-switchbot');
// Create an `Switchbot` object
let switchbot = new Switchbot();

// Start to monitor advertisement packets
switchbot.startScan().then(() => {
  // Set an event hander
  switchbot.onadvertisement = (ad) => {
    console.log(JSON.stringify(ad, null, '  '));
  };
  // Wait 10 seconds
  return switchbot.wait(10000);
}).then(() => {
  // Stop to monitor
  switchbot.stopScan();
  process.exit();
});

duceduc avatar Nov 24 '20 01:11 duceduc

I'm not sure it would make sense for this to be part of the module. You can achieve the desired functionality quite easily:

const Switchbot = require('node-switchbot');
const switchbot = new Switchbot();
const foundAddresses = [];

switchbot.startScan().then(() => {
  switchbot.onadvertisement = ad => {
    if (foundAddresses.indexOf(ad.address) === -1) {
      console.log(ad);
      foundAddresses.push(ad.address);
    }
  };
  return switchbot.wait(10000);
}).then(() => {
  switchbot.stopScan();
  process.exit();
});

dnicolson avatar Nov 28 '20 19:11 dnicolson

@duceduc, startScan() method reports all advertising packets. The purpose of this method is monitoring status of device.

futomi avatar Nov 29 '20 05:11 futomi

Thanks for the write-up. I am not sure how to go about it, but I want to get details for all the devices I have by creating a rest sensor (Home Assistant) from this json list. So far, I have save it to a file and able to view it via locally on web. Can the code be further tweak to sort it in a fix order? Perhaps by address?

duceduc avatar Dec 02 '20 22:12 duceduc

Perhaps by address?

Yes, you should use the value of the address. The value of the address is a MAC address which is globally unique.

futomi avatar Dec 02 '20 22:12 futomi

If you guys have any pointers as to how to sort the unique list, that would be awesome. my js knowledge is very limited. Thanks.

duceduc avatar Dec 04 '20 10:12 duceduc

This issue has been automatically closed because it has not had recent activity. Thank you for your contributions.

github-actions[bot] avatar Sep 26 '22 05:09 github-actions[bot]