This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New worker: ble devices scanner (#46)
* Worker for ble devices scanning Added a worker that scan for ble devices and notify, by mqtt, presence of mac addresses and rssi. Configuration section example: .... workers: blescanmulti: args: devices: nut: xx:xx:xx:xx:xx:xx ionic: xx:xx:xx:xx:xx:xx topic_prefix: blescan update_interval: 60 ... * Update README.md blescanmulti worker added * Update config.yaml.example Added configuration section for blescanmulti worker * Converted blescanmulti.py indentation to 2 spaces * changed mqtt message syntax * Update README.md Added contributor repos url * Update .gitignore Removed .vscode/ * Delete settings.json * Changes by pull request review 11/01/2019 On branch master modified: workers/blescanmulti.py
- Loading branch information
1 parent
2e27c9a
commit 345b200
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
config.yaml | ||
__pycache__/ | ||
.idea/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import time | ||
from interruptingcow import timeout | ||
from bluepy.btle import Scanner, DefaultDelegate | ||
from mqtt import MqttMessage | ||
from workers.base import BaseWorker | ||
from logger import _LOGGER | ||
|
||
REQUIREMENTS = ['bluepy'] | ||
|
||
class ScanDelegate(DefaultDelegate): | ||
def __init__(self): | ||
DefaultDelegate.__init__(self) | ||
|
||
def handleDiscovery(self, dev, isNewDev, isNewData): | ||
if isNewDev: | ||
_LOGGER.debug("Discovered new device: %s" % dev.addr) | ||
|
||
class BlescanmultiWorker(BaseWorker): | ||
def searchmac(self, devices, mac): | ||
for dev in devices: | ||
if dev.addr == mac.lower(): | ||
return dev | ||
|
||
return None | ||
|
||
def status_update(self): | ||
scanner = Scanner().withDelegate(ScanDelegate()) | ||
devices = scanner.scan(10.0) | ||
ret = [] | ||
|
||
for name, mac in self.devices.items(): | ||
device = self.searchmac(devices, mac) | ||
if device is None: | ||
ret.append(MqttMessage(topic=self.format_topic('presence/'+name), payload="0")) | ||
else: | ||
ret.append(MqttMessage(topic=self.format_topic('presence/'+name+'/rssi'), payload=device.rssi)) | ||
ret.append(MqttMessage(topic=self.format_topic('presence/'+name), payload="1")) | ||
|
||
return ret |