Skip to main content

gondola_core/monitoring/
sip_time.rs

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