Export Bluetooth devices as JSON or CSV.
()
| 759 | |
| 760 | @app.route("/export/bluetooth", methods=["GET"]) |
| 761 | def export_bluetooth() -> Response: |
| 762 | """Export Bluetooth devices as JSON or CSV.""" |
| 763 | import csv |
| 764 | import io |
| 765 | |
| 766 | format_type = request.args.get("format", "json").lower() |
| 767 | |
| 768 | if format_type == "csv": |
| 769 | output = io.StringIO() |
| 770 | writer = csv.writer(output) |
| 771 | writer.writerow(["mac", "name", "rssi", "type", "manufacturer", "last_seen"]) |
| 772 | |
| 773 | for mac, dev in bt_devices.items(): |
| 774 | writer.writerow( |
| 775 | [ |
| 776 | mac, |
| 777 | dev.get("name", "") if isinstance(dev, dict) else "", |
| 778 | dev.get("rssi", "") if isinstance(dev, dict) else "", |
| 779 | dev.get("type", "") if isinstance(dev, dict) else "", |
| 780 | dev.get("manufacturer", "") if isinstance(dev, dict) else "", |
| 781 | dev.get("lastSeen", "") if isinstance(dev, dict) else "", |
| 782 | ] |
| 783 | ) |
| 784 | |
| 785 | response = Response(output.getvalue(), mimetype="text/csv") |
| 786 | response.headers["Content-Disposition"] = "attachment; filename=bluetooth_devices.csv" |
| 787 | return response |
| 788 | else: |
| 789 | return jsonify( |
| 790 | { |
| 791 | "timestamp": __import__("datetime").datetime.utcnow().isoformat(), |
| 792 | "devices": bt_devices.values(), |
| 793 | "beacons": bt_beacons.values(), |
| 794 | } |
| 795 | ) |
| 796 | |
| 797 | |
| 798 | def _get_subghz_active() -> bool: |