REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
run_manager.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#include <Arduino.h>
6#include <cmath>
7#include <cstdlib>
8
9#include <daq/daq.h>
10#include <mode/counters.h>
11#include <run/manager.h>
12#include <run/registry.h>
13#include <run/handlers.h>
14#include <utils/logging.h>
15#include <protocol/protocol.h>
16
17FLASHMEM utils::status run::RunManager::prepare_run(JsonObjectConst msg_in, Stream &io) {
18 LOG_ALWAYS("run::RunManager::prepare_run");
19 if (!msg_in.containsKey("id"))
20 return utils::status(-1, "Need a run ID, must be a UUID");
21
22 // *always* end any previous run. Ignore msg_in["end_repetitive"] or
23 // msg_in["clear_queue"] because we just always overwrite any previous run.
24 end_run();
25
26 // Compatibility to former API
27 auto json_run_config = msg_in["config"].as<JsonObjectConst>();
28
29 bool calibrate = json_run_config["calibrate"] | false;
30 bool compat_streaming = json_run_config["streaming"] | false;
31 bool enable_state_change_handler = json_run_config["enable_state_change_handler"] | true;
32 bool enable_data_handler = json_run_config["enable_data_handler"] | true;
33 bool enable_alt_data_handler = json_run_config["enable_alt_data_handler"] | false;
34
35 const char* default_runner_type = compat_streaming ? "flexio" : "sleep";
36
37 std::string run_type = msg_in["run_type"] | default_runner_type;
38 auto &registry = run::drivers::Registry::get();
39 current_run = registry.create(run_type);
40
41 if(!current_run) {
42 if(run_type == default_runner_type) {
43 LOG_ALWAYS("Default runner is not part of registry. Will default to first available driver.");
44 current_run = registry.create_any();
45 if(!current_run) {
46 return utils::status(-2, "Run Driver Registry is empty.");
47 }
48 } else {
49 return utils::status(-3, "Requested run_type is not available.");
50 }
51 }
52
53 current_run->from_json(msg_in);
54
55 if(enable_state_change_handler) {
56 auto& envelope_out = msg::JsonLinesProtocol::get().envelope_out;
57 current_run->state_change_handler = new run::RunStateChangeNotificationHandler(io, *envelope_out);
58 }
59
60 if(enable_data_handler) {
61 current_run->data_handler = new run::RunDataNotificationHandler(io);
62 }
63
64 if(enable_alt_data_handler) {
65 current_run->alt_data_handler = new run::StreamingRunDataNotificationHandler{io};
66 }
67
68 if(calibrate) {
69 LOG_ALWAYS("Calibrating routes for run...")
70 if(!carrier::get().calibrate_routes())
71 LOG_ERROR("Error during self-calibration. Continue with reduced accuracy.");
72 }
73
74 return utils::status::success();
75}
76
77FLASHMEM void run::RunManager::start_run(const msg::Request& req, Stream &io) {
78 auto status = prepare_run(req.msg_in, io);
79 // the protocol wants to have a "all okay" reply before the run data/state handler outputs
80 msg::StreamingJsonResponse::empty_envelope(io, req, status);
81
82 invoke_run();
83
84 auto json_run_config = req.msg_in["config"].as<JsonObjectConst>();
85 bool repetitive = json_run_config["repetitive"] | false;
86
87 if(!repetitive)
88 end_run();
89}
90
91FLASHMEM utils::status run::RunManager::stop_run(JsonObjectConst msg_in, JsonObject &msg_out) {
92 end_run();
93 return utils::status::success();
94}
95
96FLASHMEM void run::drivers::DynamicRegistry::write_handler_names_to(JsonArray &target) {
97 for (auto const &kv : entries) {
98 target.add(kv.first);
99 }
100}
utils::status status
Definition daq.h:21
if(((src) >=(0x60000000) &&(src)<(0x60000000)+(0x800000)))
Definition flasher.cpp:177
static constexpr int success
Definition flasher.cpp:275
Definition daq.h:14
Definition carrier.h:10
Definition drivers.h:3