2022-12-04 02:10:34 +01:00
|
|
|
#include "spectran_http_client.h"
|
2023-02-25 18:12:34 +01:00
|
|
|
#include <utils/flog.h>
|
2022-12-04 02:10:34 +01:00
|
|
|
#include <module.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <signal_path/signal_path.h>
|
|
|
|
#include <core.h>
|
|
|
|
#include <gui/style.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <gui/smgui.h>
|
|
|
|
#include <gui/widgets/stepped_slider.h>
|
|
|
|
#include <utils/optionlist.h>
|
|
|
|
|
|
|
|
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
|
|
|
|
|
|
|
SDRPP_MOD_INFO{
|
|
|
|
/* Name: */ "spectran_http_source",
|
|
|
|
/* Description: */ "Spectran V6 HTTP source module for SDR++",
|
|
|
|
/* Author: */ "Ryzerth",
|
|
|
|
/* Version: */ 0, 1, 0,
|
|
|
|
/* Max instances */ 1
|
|
|
|
};
|
|
|
|
|
|
|
|
ConfigManager config;
|
|
|
|
|
|
|
|
class SpectranHTTPSourceModule : public ModuleManager::Instance {
|
|
|
|
public:
|
|
|
|
SpectranHTTPSourceModule(std::string name) {
|
|
|
|
this->name = name;
|
|
|
|
|
|
|
|
strcpy(hostname, "localhost");
|
2023-04-19 02:25:44 +02:00
|
|
|
sampleRate = 5750000.0;
|
2022-12-04 02:10:34 +01:00
|
|
|
|
|
|
|
handler.ctx = this;
|
|
|
|
handler.selectHandler = menuSelected;
|
|
|
|
handler.deselectHandler = menuDeselected;
|
|
|
|
handler.menuHandler = menuHandler;
|
|
|
|
handler.startHandler = start;
|
|
|
|
handler.stopHandler = stop;
|
|
|
|
handler.tuneHandler = tune;
|
|
|
|
handler.stream = &stream;
|
|
|
|
|
|
|
|
sigpath::sourceManager.registerSource("Spectran HTTP", &handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SpectranHTTPSourceModule() {
|
|
|
|
stop(this);
|
|
|
|
sigpath::sourceManager.unregisterSource("Spectran HTTP");
|
|
|
|
}
|
|
|
|
|
|
|
|
void postInit() {}
|
|
|
|
|
|
|
|
void enable() {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement select functions
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void menuSelected(void* ctx) {
|
|
|
|
SpectranHTTPSourceModule* _this = (SpectranHTTPSourceModule*)ctx;
|
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::info("SpectranHTTPSourceModule '{0}': Menu Select!", _this->name);
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void menuDeselected(void* ctx) {
|
|
|
|
SpectranHTTPSourceModule* _this = (SpectranHTTPSourceModule*)ctx;
|
|
|
|
gui::mainWindow.playButtonLocked = false;
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::info("SpectranHTTPSourceModule '{0}': Menu Deselect!", _this->name);
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void start(void* ctx) {
|
|
|
|
SpectranHTTPSourceModule* _this = (SpectranHTTPSourceModule*)ctx;
|
|
|
|
bool connected = (_this->client && _this->client->isOpen());
|
|
|
|
if (_this->running && connected) { return; }
|
|
|
|
|
|
|
|
// TODO: Start
|
|
|
|
_this->client->streaming(true);
|
|
|
|
|
|
|
|
// TODO: Set options
|
|
|
|
|
|
|
|
_this->running = true;
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::info("SpectranHTTPSourceModule '{0}': Start!", _this->name);
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stop(void* ctx) {
|
|
|
|
SpectranHTTPSourceModule* _this = (SpectranHTTPSourceModule*)ctx;
|
|
|
|
if (!_this->running) { return; }
|
|
|
|
_this->running = false;
|
|
|
|
|
|
|
|
// TODO: Implement stop
|
|
|
|
_this->client->streaming(false);
|
|
|
|
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::info("SpectranHTTPSourceModule '{0}': Stop!", _this->name);
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tune(double freq, void* ctx) {
|
|
|
|
SpectranHTTPSourceModule* _this = (SpectranHTTPSourceModule*)ctx;
|
2023-04-19 02:25:44 +02:00
|
|
|
bool connected = (_this->client && _this->client->isOpen());
|
|
|
|
if (connected) {
|
|
|
|
int64_t newfreq = round(freq);
|
|
|
|
if (newfreq != _this->lastReportedFreq && _this->gotReport) {
|
|
|
|
flog::debug("Sending tuning command");
|
|
|
|
_this->lastReportedFreq = newfreq;
|
|
|
|
_this->client->setCenterFrequency(newfreq);
|
|
|
|
}
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
_this->freq = freq;
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::info("SpectranHTTPSourceModule '{0}': Tune: {1}!", _this->name, freq);
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void menuHandler(void* ctx) {
|
|
|
|
SpectranHTTPSourceModule* _this = (SpectranHTTPSourceModule*)ctx;
|
|
|
|
bool connected = (_this->client && _this->client->isOpen());
|
|
|
|
gui::mainWindow.playButtonLocked = !connected;
|
|
|
|
|
|
|
|
if (connected) { SmGui::BeginDisabled(); }
|
|
|
|
|
2022-12-12 00:10:39 +01:00
|
|
|
if (SmGui::InputText(CONCAT("##spectran_http_host_", _this->name), _this->hostname, 1023)) {
|
2022-12-04 02:10:34 +01:00
|
|
|
config.acquire();
|
|
|
|
config.conf["hostname"] = _this->hostname;
|
|
|
|
config.release(true);
|
|
|
|
}
|
2022-12-12 00:10:39 +01:00
|
|
|
SmGui::SameLine();
|
|
|
|
SmGui::FillWidth();
|
|
|
|
if (SmGui::InputInt(CONCAT("##spectran_http_port_", _this->name), &_this->port, 0, 0)) {
|
2022-12-04 02:10:34 +01:00
|
|
|
config.acquire();
|
|
|
|
config.conf["port"] = _this->port;
|
|
|
|
config.release(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connected) { SmGui::EndDisabled(); }
|
|
|
|
|
|
|
|
if (_this->running) { style::beginDisabled(); }
|
2022-12-12 00:10:39 +01:00
|
|
|
SmGui::FillWidth();
|
|
|
|
if (!connected && SmGui::Button("Connect##spectran_http_source")) {
|
2022-12-04 02:10:34 +01:00
|
|
|
_this->tryConnect();
|
|
|
|
}
|
2022-12-12 00:10:39 +01:00
|
|
|
else if (connected && SmGui::Button("Disconnect##spectran_http_source")) {
|
2024-04-04 19:25:02 +02:00
|
|
|
_this->disconnect();
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
if (_this->running) { style::endDisabled(); }
|
|
|
|
|
2022-12-12 00:10:39 +01:00
|
|
|
SmGui::Text("Status:");
|
|
|
|
SmGui::SameLine();
|
2022-12-04 02:10:34 +01:00
|
|
|
if (connected) {
|
2022-12-12 00:10:39 +01:00
|
|
|
SmGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Connected");
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-12-12 00:10:39 +01:00
|
|
|
SmGui::Text("Not connected");
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tryConnect() {
|
|
|
|
try {
|
2023-04-19 02:25:44 +02:00
|
|
|
gotReport = false;
|
2022-12-04 02:10:34 +01:00
|
|
|
client = std::make_shared<SpectranHTTPClient>(hostname, port, &stream);
|
2023-04-19 02:25:44 +02:00
|
|
|
onFreqChangedId = client->onCenterFrequencyChanged.bind(&SpectranHTTPSourceModule::onFreqChanged, this);
|
2023-04-20 08:32:31 +02:00
|
|
|
onSamplerateChangedId = client->onSamplerateChanged.bind(&SpectranHTTPSourceModule::onSamplerateChanged, this);
|
2023-04-19 02:25:44 +02:00
|
|
|
client->startWorker();
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
catch (std::runtime_error e) {
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::error("Could not connect: {0}", e.what());
|
2022-12-04 02:10:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 19:25:02 +02:00
|
|
|
void disconnect() {
|
|
|
|
client->onCenterFrequencyChanged.unbind(onFreqChangedId);
|
|
|
|
client->onSamplerateChanged.unbind(onSamplerateChangedId);
|
|
|
|
client->close();
|
|
|
|
}
|
|
|
|
|
2023-04-19 02:25:44 +02:00
|
|
|
void onFreqChanged(double newFreq) {
|
|
|
|
if (lastReportedFreq == newFreq) { return; }
|
|
|
|
lastReportedFreq = newFreq;
|
|
|
|
tuner::tune(tuner::TUNER_MODE_IQ_ONLY, "", newFreq);
|
|
|
|
gotReport = true;
|
|
|
|
}
|
|
|
|
|
2023-04-20 08:32:31 +02:00
|
|
|
void onSamplerateChanged(double newSr) {
|
|
|
|
core::setInputSampleRate(newSr);
|
|
|
|
}
|
|
|
|
|
2022-12-04 02:10:34 +01:00
|
|
|
std::string name;
|
|
|
|
bool enabled = true;
|
|
|
|
double sampleRate;
|
|
|
|
SourceManager::SourceHandler handler;
|
|
|
|
bool running = false;
|
|
|
|
|
|
|
|
std::shared_ptr<SpectranHTTPClient> client;
|
2023-04-19 02:25:44 +02:00
|
|
|
HandlerID onFreqChangedId;
|
2023-04-20 08:32:31 +02:00
|
|
|
HandlerID onSamplerateChangedId;
|
2022-12-04 02:10:34 +01:00
|
|
|
|
|
|
|
double freq;
|
|
|
|
|
2023-04-19 02:25:44 +02:00
|
|
|
int64_t lastReportedFreq = 0;
|
|
|
|
bool gotReport;
|
|
|
|
|
2022-12-04 02:10:34 +01:00
|
|
|
char hostname[1024];
|
2023-04-19 02:25:44 +02:00
|
|
|
int port = 54664;
|
2022-12-04 02:10:34 +01:00
|
|
|
dsp::stream<dsp::complex_t> stream;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
MOD_EXPORT void _INIT_() {
|
|
|
|
json def = json({});
|
|
|
|
def["devices"] = json({});
|
|
|
|
def["device"] = "";
|
|
|
|
config.setPath(core::args["root"].s() + "/spectran_http_config.json");
|
|
|
|
config.load(def);
|
|
|
|
config.enableAutoSave();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
|
|
|
|
return new SpectranHTTPSourceModule(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
|
|
|
|
delete (SpectranHTTPSourceModule*)instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _END_() {
|
|
|
|
config.disableAutoSave();
|
|
|
|
config.save();
|
|
|
|
}
|