- MAC vendor lookup (IEEE OUI database) - BLE company_id to manufacturer mapping - Device profile enrichment in API responses - Export endpoints: devices.csv, devices.json, alerts.csv, probes.csv - Auto-populate vendor on device creation - CLI command: flask download-oui - Makefile target: make oui 13 tests passing
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""Device endpoints."""
|
|
from flask import request
|
|
from . import bp
|
|
from ..models import Device, Sighting
|
|
from ..extensions import db
|
|
from ..services.device_service import enrich_device
|
|
|
|
|
|
@bp.route('/devices')
|
|
def list_devices():
|
|
"""List all devices."""
|
|
device_type = request.args.get('type') # 'ble' or 'wifi'
|
|
limit = min(int(request.args.get('limit', 100)), 1000)
|
|
offset = int(request.args.get('offset', 0))
|
|
|
|
query = db.select(Device).order_by(Device.last_seen.desc())
|
|
if device_type:
|
|
query = query.where(Device.device_type == device_type)
|
|
query = query.limit(limit).offset(offset)
|
|
|
|
devices = db.session.scalars(query).all()
|
|
return {'devices': [enrich_device(d) for d in devices], 'limit': limit, 'offset': offset}
|
|
|
|
|
|
@bp.route('/devices/<mac>')
|
|
def get_device(mac):
|
|
"""Get device by MAC."""
|
|
mac = mac.lower()
|
|
device = db.session.scalar(db.select(Device).where(Device.mac == mac))
|
|
if not device:
|
|
return {'error': 'Device not found'}, 404
|
|
|
|
# Include recent sightings
|
|
sightings = db.session.scalars(
|
|
db.select(Sighting)
|
|
.where(Sighting.device_id == device.id)
|
|
.order_by(Sighting.timestamp.desc())
|
|
.limit(20)
|
|
).all()
|
|
|
|
result = enrich_device(device)
|
|
result['sightings'] = [s.to_dict() for s in sightings]
|
|
return result
|