go_pybindings/
command_factory.rs

1//! The wrapped commands from the 
2//! command factory
3//!
4//!
5//!
6
7use std::collections::HashMap;
8
9use pyo3::prelude::*;
10use pyo3::exceptions::PyValueError;
11
12use tof_dataclasses::commands::factory::*;
13
14use crate::{
15  PyTofCommand,
16  PyTriggerConfig,
17  PyTOFEventBuilderConfig,
18  PyDataPublisherConfig,
19  PyTofRunConfig,
20  PyTofRBConfig,
21};
22
23/// A hardwired map of RB -> RAT
24#[pyfunction]
25#[pyo3(name="get_rbratmap_hardcoded")]
26pub fn py_get_rbratmap_hardcoded() -> HashMap::<u8,u8> {
27  get_rbratmap_hardcoded()
28}
29
30/// A hardwired map of RAT -> (RB1, RB2)
31#[pyfunction]
32#[pyo3(name="get_ratrbmap_hardcoded")]
33pub fn py_get_ratrbmap_hardcoded() -> HashMap::<u8,(u8,u8)> {
34  get_ratrbmap_hardcoded()
35}
36
37/// A hardwired map of PDU #id PDUCHANNEL #id to (RAT,RAT)
38///
39/// Can be used to synchronize powering down proces for 
40/// RATs
41#[pyfunction]
42#[pyo3(name="get_ratpdumap_hardcoded")]
43pub fn py_get_ratpdumap_hardcoded() -> HashMap::<u8,HashMap::<u8,(u8,u8)>> {
44  get_ratpdumap_hardcoded()
45}
46
47/// Send the 'sudo shutdown now' command to a single RB
48///
49/// # Arguements:
50///   * rb :  The RB id of the RB to be shutdown 
51///           (NOT RAT)
52#[pyfunction]
53#[pyo3(name="shutdown_rb")]
54pub fn py_shutdown_rb(rb : u8) -> PyResult<PyTofCommand> {
55  let cmd = shutdown_rb(rb).unwrap();
56  Ok(PyTofCommand { 
57    command : cmd
58  })
59}
60
61
62/// Send the 'sudo shutdown now' command to
63/// ALL RBs
64#[pyfunction]
65#[pyo3(name="shutdown_all_rbs")]
66pub fn py_shutdown_all_rbs() -> PyResult<PyTofCommand> {
67  let cmd = shutdown_all_rbs().unwrap();
68  let pycmd = PyTofCommand { 
69    command : cmd
70  };
71  return Ok(pycmd);
72}
73
74/// Send the 'sudo shutdown now' command to all RBs in a RAT
75///
76/// # Arguments:
77///   * rat : The RAT id for the rat the RBs to be 
78///           shutdown live in 
79#[pyfunction]
80#[pyo3(name="shutdown_rat")]
81pub fn py_shutdown_rat(rat : u8) -> PyResult<PyTofCommand> {
82  match shutdown_rat(rat) {
83    None => {
84      return Err(PyValueError::new_err(format!("There might not be a RAT{}!", rat)));
85    }
86    Some(cmd) => {
87      let pycmd = PyTofCommand { 
88       command : cmd
89      };
90      return Ok(pycmd);
91    }
92  }
93}
94
95/// Send the 'sudo shutdown now' command to all RBs 
96/// in the 2 RATs connected to a certain PDU channel
97/// 
98/// This will prepare the shutdown command for the RBs in the 
99/// RATs which are connected to a specific pdu channel
100///
101/// # Arguments:
102///   * pdu        : PDU ID (0-3)
103///   * pduchannel : PDU Channel (0-7)
104#[pyfunction]
105#[pyo3(name="shutdown_ratpair")]
106pub fn py_shutdown_ratpair(pdu : u8, pduchannel : u8) -> PyResult<PyTofCommand> {
107  match shutdown_ratpair(pdu, pduchannel) {
108    None => {
109      return Err(PyValueError::new_err(format!("There might be an issue with the pdu mapping. Can nto find RATs at PDU {} channel {}!", pdu, pduchannel)));
110    }
111    Some(cmd) => {
112      let pycmd = PyTofCommand { 
113       command : cmd
114      };
115      return Ok(pycmd);
116    }
117  }
118}
119
120/// Send the 'sudo shutdown now command to
121/// the TOF main computer ("TOFCPU")
122#[pyfunction]
123#[pyo3(name="shutdown_cpu")]
124pub fn py_shutdown_tofcpu() -> PyResult<PyTofCommand> {
125  match shutdown_tofcpu() {
126    None => {
127      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
128    }
129    Some(cmd) => {
130      let pycmd = PyTofCommand { 
131       command : cmd
132      };
133      return Ok(pycmd);
134    }
135  }
136}
137
138
139/// Restart the liftof-rb clients on the given boards
140///
141/// # Arguments
142///   * rbs: restart the client on the given rb ids, 
143///          if empty, restart on all of them
144#[pyfunction]
145#[pyo3(name="restart_liftofrb")]
146pub fn py_restart_liftofrb(rbs : Vec<u8>) -> PyResult<PyTofCommand> {
147  match restart_liftofrb(&rbs) {
148    None => {
149      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
150    }
151    Some(cmd) => {
152      let pycmd = PyTofCommand { 
153       command : cmd
154      };
155      return Ok(pycmd);
156    }
157  }
158}
159
160/// Trigger the start of a new data run with 
161/// the next active config
162#[pyfunction]
163#[pyo3(name="start_run")]
164pub fn py_start_run() -> PyResult<PyTofCommand> {
165  match start_run() {
166    None => {
167      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
168    }
169    Some(cmd) => {
170      let pycmd = PyTofCommand { 
171       command : cmd
172      };
173      return Ok(pycmd);
174    }
175  }
176}
177
178/// Stop the current active run and idle
179#[pyfunction]
180#[pyo3(name="stop_run")]
181pub fn py_stop_run() -> PyResult<PyTofCommand> {
182  match stop_run() {
183    None => {
184      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
185    }
186    Some(cmd) => {
187      let pycmd = PyTofCommand { 
188       command : cmd
189      };
190      return Ok(pycmd);
191    }
192  }
193}
194
195/// Run a calibration of all RBs
196///
197/// # Arguments:
198///   * pre_run_calibration : Run the RBCalibration routine before 
199///                           every run start
200///   * send_packetes       : Send the RBCalibration packets
201///   * save_events         : Save the events to the RBCalibration
202///                           packets
203#[pyfunction]
204#[pyo3(name="rb_calibration")]
205pub fn py_rb_calibration(pre_run_calibration : bool, send_packets : bool, save_events : bool) -> PyResult<PyTofCommand> {
206  match rb_calibration(pre_run_calibration,send_packets, save_events) {
207    None => {
208      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
209    }
210    Some(cmd) => {
211      let pycmd = PyTofCommand { 
212       command : cmd
213      };
214      return Ok(pycmd);
215    }
216  }
217}
218
219
220/// Change the MTBSettings in the config file with relevant trigger settings
221#[pyfunction]
222#[pyo3(name="change_triggerconfig")]
223pub fn py_change_triggerconfig(cfg : &PyTriggerConfig) -> PyResult<PyTofCommand> {
224  match change_triggerconfig(&cfg.config) {
225    None => {
226      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
227    }
228    Some(cmd) => {
229      let pycmd = PyTofCommand { 
230       command : cmd
231      };
232      return Ok(pycmd);
233    }
234  }
235}
236
237
238/// Change the TOFEventBuilderSettings in the config
239#[pyfunction]
240#[pyo3(name="change_tofeventbuilderconfig")]
241pub fn py_change_tofeventbuilderconfig(cfg : &PyTOFEventBuilderConfig) -> PyResult<PyTofCommand> {
242  match change_tofeventbuilderconfig(&cfg.config) {
243    None => {
244      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
245    }
246    Some(cmd) => {
247      let pycmd = PyTofCommand { 
248       command : cmd
249      };
250      return Ok(pycmd);
251    }
252  }
253}
254
255/// Change the data publisher config part of the config file
256#[pyfunction]
257#[pyo3(name="change_datapublisherconfig")]
258pub fn py_change_datapublisherconfig(cfg : &PyDataPublisherConfig) -> PyResult<PyTofCommand> {
259  match change_datapublisherconfig(&cfg.config) {
260    None => {
261      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
262    }
263    Some(cmd) => {
264      let pycmd = PyTofCommand { 
265       command : cmd
266      };
267      return Ok(pycmd);
268    }
269  }
270}
271
272/// Change the run config part of the config file
273#[pyfunction]
274#[pyo3(name="change_tofrunconfig")]
275pub fn py_change_tofrunconfig(cfg : &PyTofRunConfig) -> PyResult<PyTofCommand> {
276  match change_tofrunconfig(&cfg.config) {
277    None => {
278      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
279    }
280    Some(cmd) => {
281      let pycmd = PyTofCommand { 
282       command : cmd
283      };
284      return Ok(pycmd);
285    }
286  }
287}
288
289/// Change the RB config part of the config file
290#[pyfunction]
291#[pyo3(name="change_tofrbconfig")]
292pub fn py_change_tofrbconfig(cfg : &PyTofRBConfig) -> PyResult<PyTofCommand> {
293  match change_tofrbconfig(&cfg.config) {
294    None => {
295      return Err(PyValueError::new_err(format!("You encounterd a dragon \u{1f409}! We don't know what's going on either.")));
296    }
297    Some(cmd) => {
298      let pycmd = PyTofCommand { 
299       command : cmd
300      };
301      return Ok(pycmd);
302    }
303  }
304}
305