Added /configure endpoint, moved to class structure for Radio
This commit is contained in:
parent
b3dece82f2
commit
9206cb6f08
@ -1,6 +1,7 @@
|
|||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
import SoapySDR as soapy
|
import SoapySDR as soapy
|
||||||
|
from radio import Radio
|
||||||
|
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
from flasgger import Swagger
|
from flasgger import Swagger
|
||||||
@ -8,7 +9,7 @@ from flasgger import Swagger
|
|||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
swag = Swagger(app)
|
swag = Swagger(app)
|
||||||
|
|
||||||
sdrs = {}
|
radios = {}
|
||||||
|
|
||||||
@app.route('/radio/report')
|
@app.route('/radio/report')
|
||||||
def report():
|
def report():
|
||||||
@ -40,19 +41,19 @@ def connect(radio):
|
|||||||
500:
|
500:
|
||||||
description: Failed to connect to radio.
|
description: Failed to connect to radio.
|
||||||
"""
|
"""
|
||||||
if radio in sdrs:
|
if radio in radios:
|
||||||
return "Radio device already connected", 400
|
return "Radio device already connected", 400
|
||||||
|
|
||||||
devices = [dict(device) for device in soapy.Device.enumerate()]
|
devices = [dict(device) for device in soapy.Device.enumerate()]
|
||||||
for device in devices:
|
for device in devices:
|
||||||
if radio in device.values():
|
if radio in device.values():
|
||||||
sdrs[radio] = {'device': soapy.Device(device)}
|
try:
|
||||||
|
radios[radio] = Radio(device)
|
||||||
if sdrs[radio]['device'] is None:
|
|
||||||
sdrs.pop(radio)
|
|
||||||
return "Failed to open device", 500
|
|
||||||
else:
|
|
||||||
return "", 200
|
return "", 200
|
||||||
|
except Exception as e:
|
||||||
|
radios.pop(radio)
|
||||||
|
return str(e), 500
|
||||||
|
|
||||||
|
|
||||||
return "Radio device not found", 400
|
return "Radio device not found", 400
|
||||||
|
|
||||||
@ -74,20 +75,42 @@ def disconnect(radio):
|
|||||||
500:
|
500:
|
||||||
description: An unknown error occurred.
|
description: An unknown error occurred.
|
||||||
"""
|
"""
|
||||||
if radio in sdrs:
|
if radio in radios:
|
||||||
if 'stream' in sdrs[radio] and sdrs[radio]['stream'] is not None:
|
radios.pop(radio)
|
||||||
return "Radio is currently streaming, cannot disconnect", 400
|
|
||||||
else:
|
|
||||||
sdrs.pop(radio)
|
|
||||||
return "", 200
|
return "", 200
|
||||||
else:
|
else:
|
||||||
return "Radio not connected", 400
|
return "Radio not connected", 400
|
||||||
|
|
||||||
'''
|
|
||||||
@app.route('/radio/<radio>/configure/<frequency>')
|
|
||||||
def configure(radio):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
@app.route('/radio/<radio>/configure/<frequency>')
|
||||||
|
def configure(radio, frequency):
|
||||||
|
"""Tune the radio to a frequency.
|
||||||
|
You must connect to the radio before attempting to configure it.
|
||||||
|
The radio must be configured before streaming can be started.
|
||||||
|
---
|
||||||
|
parameters:
|
||||||
|
- name: radio
|
||||||
|
description: Radio device driver name, or serial number.
|
||||||
|
in: path
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
- name: frequency
|
||||||
|
description: Frequency (in Hz) to tune the radio to.
|
||||||
|
in: path
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: JSON
|
||||||
|
400:
|
||||||
|
description: The specified radio is not connected.
|
||||||
|
"""
|
||||||
|
if radio in radios:
|
||||||
|
return jsonify(radios[radio].configure(frequency))
|
||||||
|
else:
|
||||||
|
return "Radio not connected", 400
|
||||||
|
|
||||||
|
'''
|
||||||
@app.route('/radio/<radio>/start')
|
@app.route('/radio/<radio>/start')
|
||||||
def start_stream(radio):
|
def start_stream(radio):
|
||||||
pass
|
pass
|
||||||
|
|||||||
33
radio.py
Normal file
33
radio.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import SoapySDR as soapy
|
||||||
|
import prefixed
|
||||||
|
|
||||||
|
|
||||||
|
class Radio:
|
||||||
|
def __init__(self, device_info):
|
||||||
|
self.device_info = device_info
|
||||||
|
self.stream = None
|
||||||
|
self.buffer = None
|
||||||
|
|
||||||
|
self.device = soapy.Device(device_info)
|
||||||
|
if self.device is None:
|
||||||
|
raise RuntimeError("Failed to connect to radio device")
|
||||||
|
|
||||||
|
|
||||||
|
def configure(self, frequency):
|
||||||
|
frequency = int(prefixed.Float(frequency))
|
||||||
|
sample_rate = 384000
|
||||||
|
bandwidth = 200000
|
||||||
|
|
||||||
|
self.device.setFrequency(soapy.SOAPY_SDR_RX, 0, frequency)
|
||||||
|
self.device.setSampleRate(soapy.SOAPY_SDR_RX, 0, sample_rate)
|
||||||
|
self.device.setBandwidth(soapy.SOAPY_SDR_RX, 0, bandwidth)
|
||||||
|
|
||||||
|
# Set automatic gain
|
||||||
|
self.device.setGainMode(soapy.SOAPY_SDR_RX, 0, True)
|
||||||
|
|
||||||
|
return {
|
||||||
|
'frequency': self.device.getFrequency(soapy.SOAPY_SDR_RX, 0),
|
||||||
|
'sample-rate': self.device.getSampleRate(soapy.SOAPY_SDR_RX, 0),
|
||||||
|
'bandwidth': self.device.getBandwidth(soapy.SOAPY_SDR_RX, 0),
|
||||||
|
'gain-mode': 'auto' if self.device.getGainMode(soapy.SOAPY_SDR_RX, 0) else 'manual',
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user