Skip to content

Commit ecf937a

Browse files
committed
Added scheduler.
1 parent 592995b commit ecf937a

File tree

7 files changed

+42
-20
lines changed

7 files changed

+42
-20
lines changed

sources/config_migrator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ void ConfigMigrator::sort(nlohmann::json& json) {
2828
for (auto& device : devices) {
2929
auto& ranges = device.at("ranges");
3030
std::sort(ranges.begin(), ranges.end(), [](const nlohmann::json& r1, const nlohmann::json& r2) { return r1.at("start").get<Frequency>() < r2.at("start").get<Frequency>(); });
31+
32+
auto& satellites = device.at("satellites");
33+
std::sort(satellites.begin(), satellites.end(), [](const nlohmann::json& r1, const nlohmann::json& r2) { return r1.at("name").get<std::string>() < r2.at("name").get<std::string>(); });
34+
35+
auto& crontabs = device.at("crontabs");
36+
std::sort(crontabs.begin(), crontabs.end(), [](const nlohmann::json& r1, const nlohmann::json& r2) { return r1.at("name").get<std::string>() < r2.at("name").get<std::string>(); });
3137
}
3238
}
3339

sources/network/query.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#include <radio/help_structures.h>
44
#include <utils/serializers.h>
55

6-
struct SatellitesQuery {
6+
struct SchedulerQuery {
77
std::string latitude;
88
std::string longitude;
99
int altitude;
1010
std::string api_key;
1111
std::vector<Satellite> satellites;
12+
std::vector<Crontab> crontabs;
1213
};
13-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SatellitesQuery, latitude, longitude, altitude, api_key, satellites)
14+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SchedulerQuery, latitude, longitude, altitude, api_key, satellites, crontabs)
1415

1516
struct ScheduledTransmission {
1617
std::string name;

sources/network/remote_controller.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
constexpr auto LIST = "list";
88
constexpr auto STATUS = "status";
99
constexpr auto CONFIG = "config";
10-
constexpr auto SATELLITES = "satellites";
10+
constexpr auto SCHEDULER = "scheduler";
1111
constexpr auto SUCCESS = "success";
1212
constexpr auto FAILED = "failed";
1313
constexpr auto LABEL = "remote";
@@ -23,10 +23,10 @@ void RemoteController::reloadConfigCallback(const Mqtt::JsonCallback& callback)
2323

2424
void RemoteController::reloadConfigStatus(const bool& success) { m_mqtt.publish(fmt::format("sdr/{}/{}/{}", CONFIG, m_config.getId(), success ? SUCCESS : FAILED), "", 2); }
2525

26-
void RemoteController::satellitesQuery(const std::string& device, const std::string& query) { m_mqtt.publish(fmt::format("sdr/{}/{}/{}/get", SATELLITES, m_config.getId(), device), query, 2); }
26+
void RemoteController::schedulerQuery(const std::string& device, const std::string& query) { m_mqtt.publish(fmt::format("sdr/{}/{}/{}/get", SCHEDULER, m_config.getId(), device), query, 2); }
2727

28-
void RemoteController::satellitesCallback(const std::string& device, const Mqtt::JsonCallback& callback) {
29-
m_mqtt.setJsonMessageCallback(fmt::format("sdr/{}/{}/{}/set", SATELLITES, m_config.getId(), device), callback);
28+
void RemoteController::schedulerCallback(const std::string& device, const Mqtt::JsonCallback& callback) {
29+
m_mqtt.setJsonMessageCallback(fmt::format("sdr/{}/{}/{}/set", SCHEDULER, m_config.getId(), device), callback);
3030
}
3131

3232
void RemoteController::listCallback(const std::string&) {

sources/network/remote_controller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class RemoteController {
1313
void reloadConfigCallback(const Mqtt::JsonCallback& callback);
1414
void reloadConfigStatus(const bool& success);
1515

16-
void satellitesQuery(const std::string& device, const std::string& query);
17-
void satellitesCallback(const std::string& device, const Mqtt::JsonCallback& callback);
16+
void schedulerQuery(const std::string& device, const std::string& query);
17+
void schedulerCallback(const std::string& device, const Mqtt::JsonCallback& callback);
1818

1919
private:
2020
void listCallback(const std::string& data);

sources/radio/help_structures.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ struct Gain {
5151
};
5252
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Gain, name, value, min, max, step)
5353

54+
struct Crontab {
55+
std::string name;
56+
std::string expression;
57+
std::chrono::seconds duration;
58+
Frequency frequency;
59+
Frequency bandwidth;
60+
std::string modulation;
61+
};
62+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Crontab, name, expression, duration, frequency, bandwidth, modulation)
63+
5464
struct Device {
5565
bool connected = false;
5666
bool enabled{};
@@ -63,7 +73,8 @@ struct Device {
6373
float stop_recording_level{};
6474
std::vector<Satellite> satellites{};
6575
std::vector<Frequency> sample_rates{};
76+
std::vector<Crontab> crontabs;
6677

6778
std::string getName() const { return driver + "_" + serial; }
6879
};
69-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Device, connected, enabled, gains, serial, driver, sample_rate, ranges, start_recording_level, stop_recording_level, satellites, sample_rates)
80+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Device, connected, enabled, gains, serial, driver, sample_rate, ranges, start_recording_level, stop_recording_level, satellites, sample_rates, crontabs)

sources/radio/scheduler.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,25 @@ std::optional<std::pair<FrequencyRange, std::vector<Recording>>> Scheduler::getR
5858
}
5959

6060
void Scheduler::worker() {
61-
m_remoteController.satellitesCallback(m_device.getName(), std::bind(&Scheduler::satellitesCallback, this, _1));
61+
m_remoteController.schedulerCallback(m_device.getName(), std::bind(&Scheduler::callback, this, _1));
6262
while (m_isRunning) {
6363
const auto now = getTime();
6464
if (m_isRefreshEnabled && m_lastUpdateTime + UPDATE_INTERVAL <= now) {
65-
satellitesQuery();
65+
query();
6666
m_lastUpdateTime = now;
6767
}
6868
std::this_thread::sleep_for(LOOP_TIMEOUT);
6969
}
7070
}
7171

72-
void Scheduler::satellitesQuery() {
73-
Logger::info(LABEL, "send satellites query");
74-
const SatellitesQuery query(m_config.latitude(), m_config.longitude(), m_config.altitude(), m_config.apiKey(), m_device.satellites);
75-
m_remoteController.satellitesQuery(m_device.getName(), static_cast<nlohmann::json>(query).dump());
72+
void Scheduler::query() {
73+
Logger::info(LABEL, "send query");
74+
const SchedulerQuery query(m_config.latitude(), m_config.longitude(), m_config.altitude(), m_config.apiKey(), m_device.satellites, m_device.crontabs);
75+
m_remoteController.schedulerQuery(m_device.getName(), static_cast<nlohmann::json>(query).dump());
7676
}
7777

78-
void Scheduler::satellitesCallback(const nlohmann::json& json) {
79-
Logger::info(LABEL, "received satellites: {}", colored(GREEN, "{}", json.dump()));
78+
void Scheduler::callback(const nlohmann::json& json) {
79+
Logger::info(LABEL, "received response, size: {}", colored(GREEN, "{}", json.size()));
8080
std::unique_lock lock(m_mutex);
8181
m_scheduledTransmissions = json;
8282
}

sources/radio/scheduler.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ class Scheduler {
1919
const std::chrono::milliseconds& now, std::list<ScheduledTransmission>& scheduledTransmissions, Frequency sampleRate, Frequency shift);
2020
std::optional<std::pair<FrequencyRange, std::vector<Recording>>> getRecordings(const std::chrono::milliseconds& now);
2121

22-
void worker();
23-
void satellitesQuery();
24-
void satellitesCallback(const nlohmann::json& json);
2522
void setRefreshEnabled(const bool& enabled);
2623

24+
private:
25+
void worker();
26+
void mergeScheduledTransmissions();
27+
28+
void query();
29+
void callback(const nlohmann::json& json);
30+
2731
const Config& m_config;
2832
const Device m_device;
2933
RemoteController& m_remoteController;

0 commit comments

Comments
 (0)