Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/backgrounds/plugins/rf_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def __init__(self, config: RFmapperConfig):
self.thread = threading.Thread(target=self._scan_task)
self.running = False

# Shared between scan thread and main run loop
self.scan_lock = threading.Lock()
self.scan_results: List[RFData] = []
self.scan_idx = 0
self.scan_last_sent = 0
Expand Down Expand Up @@ -113,8 +115,6 @@ def __init__(self, config: RFmapperConfig):

self.seen_devices: Dict[str, RFData] = {}

self.seen_names: List[str] = []

self.start()

async def scan(self) -> List[RFData]:
Expand Down Expand Up @@ -217,7 +217,8 @@ def detection_callback(device, advdata: AdvertisementData):
final_list.append(device)
logging.debug(f"Scan...{final_list}")

self.scan_idx += 1
with self.scan_lock:
self.scan_idx += 1

return final_list

Expand All @@ -229,9 +230,13 @@ def _scan_task(self):
logging.info("Starting RF scan thread...")
self.running = True
while self.running:
self.scan_results = self.loop.run_until_complete(self.scan())
logging.info(f"RF scan index: {self.scan_idx}")
logging.info(f"RF scan last sent: {self.scan_last_sent}")
scan_results = self.loop.run_until_complete(self.scan())
with self.scan_lock:
self.scan_results = scan_results
scan_idx = self.scan_idx
scan_last_sent = self.scan_last_sent
logging.info(f"RF scan index: {scan_idx}")
logging.info(f"RF scan last sent: {scan_last_sent}")
time.sleep(0.5)

def start(self):
Expand All @@ -245,8 +250,7 @@ def stop(self):
Stop the background process.
"""
self.running = False
time.sleep(1)
self.thread.join()
self.thread.join(timeout=1.0)

def run(self) -> None:
"""
Expand All @@ -259,14 +263,23 @@ def run(self) -> None:
logging.info(f"Sending to fabric: payload {self.payload_idx}")

# add scan results if they are new
logging.info(f"RF scan index: {self.scan_idx}")
logging.info(f"RF scan last sent: {self.scan_last_sent}")
fresh_scan_results = []
if self.scan_results and self.scan_idx > self.scan_last_sent:
fresh_scan_results = self.scan_results
self.scan_last_sent = self.scan_idx
self.scan_results = []
logging.info(f"RF scan sending new payload: {self.scan_last_sent}")
with self.scan_lock:
scan_idx = self.scan_idx
scan_last_sent = self.scan_last_sent

logging.info(f"RF scan index: {scan_idx}")
logging.info(f"RF scan last sent: {scan_last_sent}")

fresh_scan_results: List[RFData] = []
with self.scan_lock:
if self.scan_results and self.scan_idx > self.scan_last_sent:
fresh_scan_results = list(self.scan_results)
self.scan_last_sent = self.scan_idx
self.scan_results = []
scan_last_sent = self.scan_last_sent

if fresh_scan_results:
logging.info(f"RF scan sending new payload: {scan_last_sent}")

# basic gps data and occasional scan results
try:
Expand Down Expand Up @@ -347,7 +360,7 @@ def run(self) -> None:
except Exception as e:
logging.error(f"Error sharing to Fabric: {e}")

self.sleep(1) # we should send a payload every second
time.sleep(1.0) # we should send a payload every second

except KeyboardInterrupt:
logging.info("Stopping RF scanner...")
Expand Down