Skip to main content

gondola_core/monitoring/
sip_pressure.rs

1// This file is part of gaps-online-software and published 
2// under the GPLv3 license
3
4use crate::prelude::*;
5
6// Sip (CSBF provided Pressure data
7#[derive(Debug, Copy, Clone, PartialEq)]
8#[cfg_attr(feature="pybindings", pyclass)] 
9pub struct SipPresMoniData {
10  pub sip_id     : u8,
11  pub mks_high   : u16,
12  pub mks_mid    : u16, 
13  pub mks_lo     : u16,
14  // make it compatible with other moni data
15  pub timestamp  : u64,
16  pub board_id   : u8,
17  //pub timestamp  : u64,
18}
19
20impl SipPresMoniData {
21
22  pub fn new() -> Self {
23    Self {
24      sip_id     : 0,
25      mks_high   : 0,
26      mks_mid    : 0, 
27      mks_lo    : 0,
28      timestamp  : 0,
29      board_id   : 0,
30    }
31  }
32}
33
34impl fmt::Display for SipPresMoniData {
35  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36    write!(f, "<SipPresMoniData:
37  SIP ID   : {}
38  mks high : {}
39  mks_mid  : {}
40  mks_lo   : {} 
41  timstamp : {}>",
42  self.sip_id, 
43  self.mks_high,
44  self.mks_mid,
45  self.mks_lo,
46  self.timestamp)
47  }
48}
49
50#[cfg(feature = "random")]
51impl FromRandom for SipPresMoniData {
52    
53  fn from_random() -> Self {
54    let mut moni   = Self::new();
55    let mut rng    = rand::rng();
56    moni.sip_id    = rng.random::<u8>();
57    moni.mks_lo    = rng.random::<u16>();
58    moni.mks_mid   = rng.random::<u16>();
59    moni.mks_high  = rng.random::<u16>();
60    moni.timestamp = rng.random::<u64>();
61    moni.board_id  = rng.random::<u8>();
62    moni 
63  }
64}
65
66impl Default for SipPresMoniData {
67  fn default() -> Self {
68    Self::new()
69  }
70}
71
72impl Serialization for SipPresMoniData { 
73  
74  // size without TelemetryPacketHeader
75  const SIZE : usize = 10;
76
77  fn from_bytestream(stream : &Vec<u8>,
78                     pos    : &mut usize)
79    -> Result<Self, SerializationError> {
80    if stream.len() < Self::SIZE {
81      return Err(SerializationError::StreamTooShort);
82    }
83    let mut moni = Self::new();
84    moni.sip_id  = parse_u8(stream, pos);
85    let     dle  = parse_u8(stream, pos);
86    if dle != 0x10 {
87       //return -2;
88       return Err(SerializationError::Unknown);
89    } 
90    let sip_type = parse_u8(stream, pos);
91    if sip_type != 0x12 {
92      error!("Wrong SIP type - seeing {}", sip_type);
93      return Err(SerializationError::Unknown);
94    }
95    moni.mks_high     = parse_u16(stream, pos);
96    moni.mks_mid      = parse_u16(stream, pos);
97    moni.mks_lo       = parse_u16(stream, pos);
98    let etx           = parse_u8(stream, pos);
99    if etx != 0x03 {
100       // return -4
101       return Err(SerializationError::Unknown);
102    }  
103    Ok(moni)
104  } 
105}
106
107impl MoniData for SipPresMoniData {
108  
109  fn get_board_id(&self) -> u8 {
110    return self.board_id;
111  }
112
113  fn get_timestamp(&self) -> u64 {
114    self.timestamp 
115  }
116
117  fn set_timestamp(&mut self, ts: u64) {
118    self.timestamp = ts;
119  }
120
121  fn keys() -> Vec<&'static str> {
122    vec!["board_id", "sip_id", 
123         "mks_lo", "mks_mid", "mks_high",
124         "timestamp"]
125  }
126
127  fn get(&self, varname : &str) -> Option<f32> {
128    match varname {
129      "board_id"  => Some(self.board_id as f32),
130      "sip_id"    => Some(self.sip_id as f32),
131      "mks_lo"    => Some(self.mks_lo as f32), 
132      "mks_mid"   => Some(self.mks_mid as f32),
133      "mks_high"  => Some(self.mks_high as f32),
134      "timestamp" => Some(self.timestamp as f32),
135      _           => None
136    }
137  }  
138}
139
140impl TelemetryPackable for SipPresMoniData {
141  const TEL_PACKET_TYPE : TelemetryPacketType = TelemetryPacketType::SipPressure;
142}
143
144moniseries_telemetry!(SipPresMoniDataSeries, SipPresMoniData);
145
146#[cfg(feature="pybindings")]
147pythonize_monidata!(SipPresMoniData);
148