use crate::profile::typedef::{self, FitBaseType};
use crate::proto::*;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct TimeInZone {
pub timestamp: typedef::DateTime,
pub reference_mesg: typedef::MesgNum,
pub reference_index: typedef::MessageIndex,
pub time_in_hr_zone: Vec<u32>,
pub time_in_speed_zone: Vec<u32>,
pub time_in_cadence_zone: Vec<u32>,
pub time_in_power_zone: Vec<u32>,
pub hr_zone_high_boundary: Vec<u8>,
pub speed_zone_high_boundary: Vec<u16>,
pub cadence_zone_high_boundary: Vec<u8>,
pub power_zone_high_boundary: Vec<u16>,
pub hr_calc_type: typedef::HrZoneCalc,
pub max_heart_rate: u8,
pub resting_heart_rate: u8,
pub threshold_heart_rate: u8,
pub pwr_calc_type: typedef::PwrZoneCalc,
pub functional_threshold_power: u16,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl TimeInZone {
pub const TIMESTAMP: u8 = 253;
pub const REFERENCE_MESG: u8 = 0;
pub const REFERENCE_INDEX: u8 = 1;
pub const TIME_IN_HR_ZONE: u8 = 2;
pub const TIME_IN_SPEED_ZONE: u8 = 3;
pub const TIME_IN_CADENCE_ZONE: u8 = 4;
pub const TIME_IN_POWER_ZONE: u8 = 5;
pub const HR_ZONE_HIGH_BOUNDARY: u8 = 6;
pub const SPEED_ZONE_HIGH_BOUNDARY: u8 = 7;
pub const CADENCE_ZONE_HIGH_BOUNDARY: u8 = 8;
pub const POWER_ZONE_HIGH_BOUNDARY: u8 = 9;
pub const HR_CALC_TYPE: u8 = 10;
pub const MAX_HEART_RATE: u8 = 11;
pub const RESTING_HEART_RATE: u8 = 12;
pub const THRESHOLD_HEART_RATE: u8 = 13;
pub const PWR_CALC_TYPE: u8 = 14;
pub const FUNCTIONAL_THRESHOLD_POWER: u8 = 15;
pub const fn new() -> Self {
Self {
timestamp: typedef::DateTime(u32::MAX),
reference_mesg: typedef::MesgNum(u16::MAX),
reference_index: typedef::MessageIndex(u16::MAX),
time_in_hr_zone: Vec::new(),
time_in_speed_zone: Vec::new(),
time_in_cadence_zone: Vec::new(),
time_in_power_zone: Vec::new(),
hr_zone_high_boundary: Vec::new(),
speed_zone_high_boundary: Vec::new(),
cadence_zone_high_boundary: Vec::new(),
power_zone_high_boundary: Vec::new(),
hr_calc_type: typedef::HrZoneCalc(u8::MAX),
max_heart_rate: u8::MAX,
resting_heart_rate: u8::MAX,
threshold_heart_rate: u8::MAX,
pwr_calc_type: typedef::PwrZoneCalc(u8::MAX),
functional_threshold_power: u16::MAX,
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn time_in_hr_zone_scaled(&self) -> Option<Vec<f64>> {
if self.time_in_hr_zone.is_empty() {
return None;
}
let mut v = Vec::with_capacity(self.time_in_hr_zone.len());
for &x in &self.time_in_hr_zone {
v.push(x as f64 / 1000.0 - 0.0)
}
Some(v)
}
pub fn set_time_in_hr_zone_scaled(&mut self, v: &[f64]) -> &mut Self {
self.time_in_hr_zone = Vec::with_capacity(v.len());
if v.is_empty() {
return self;
}
for &x in v {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.time_in_hr_zone.push(u32::MAX);
continue;
}
self.time_in_hr_zone.push(unscaled as u32);
}
self
}
pub fn time_in_speed_zone_scaled(&self) -> Option<Vec<f64>> {
if self.time_in_speed_zone.is_empty() {
return None;
}
let mut v = Vec::with_capacity(self.time_in_speed_zone.len());
for &x in &self.time_in_speed_zone {
v.push(x as f64 / 1000.0 - 0.0)
}
Some(v)
}
pub fn set_time_in_speed_zone_scaled(&mut self, v: &[f64]) -> &mut Self {
self.time_in_speed_zone = Vec::with_capacity(v.len());
if v.is_empty() {
return self;
}
for &x in v {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.time_in_speed_zone.push(u32::MAX);
continue;
}
self.time_in_speed_zone.push(unscaled as u32);
}
self
}
pub fn time_in_cadence_zone_scaled(&self) -> Option<Vec<f64>> {
if self.time_in_cadence_zone.is_empty() {
return None;
}
let mut v = Vec::with_capacity(self.time_in_cadence_zone.len());
for &x in &self.time_in_cadence_zone {
v.push(x as f64 / 1000.0 - 0.0)
}
Some(v)
}
pub fn set_time_in_cadence_zone_scaled(&mut self, v: &[f64]) -> &mut Self {
self.time_in_cadence_zone = Vec::with_capacity(v.len());
if v.is_empty() {
return self;
}
for &x in v {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.time_in_cadence_zone.push(u32::MAX);
continue;
}
self.time_in_cadence_zone.push(unscaled as u32);
}
self
}
pub fn time_in_power_zone_scaled(&self) -> Option<Vec<f64>> {
if self.time_in_power_zone.is_empty() {
return None;
}
let mut v = Vec::with_capacity(self.time_in_power_zone.len());
for &x in &self.time_in_power_zone {
v.push(x as f64 / 1000.0 - 0.0)
}
Some(v)
}
pub fn set_time_in_power_zone_scaled(&mut self, v: &[f64]) -> &mut Self {
self.time_in_power_zone = Vec::with_capacity(v.len());
if v.is_empty() {
return self;
}
for &x in v {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.time_in_power_zone.push(u32::MAX);
continue;
}
self.time_in_power_zone.push(unscaled as u32);
}
self
}
pub fn speed_zone_high_boundary_scaled(&self) -> Option<Vec<f64>> {
if self.speed_zone_high_boundary.is_empty() {
return None;
}
let mut v = Vec::with_capacity(self.speed_zone_high_boundary.len());
for &x in &self.speed_zone_high_boundary {
v.push(x as f64 / 1000.0 - 0.0)
}
Some(v)
}
pub fn set_speed_zone_high_boundary_scaled(&mut self, v: &[f64]) -> &mut Self {
self.speed_zone_high_boundary = Vec::with_capacity(v.len());
if v.is_empty() {
return self;
}
for &x in v {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
self.speed_zone_high_boundary.push(u16::MAX);
continue;
}
self.speed_zone_high_boundary.push(unscaled as u16);
}
self
}
fn count_valid_fields(&self) -> usize {
(self.timestamp.0 != u32::MAX) as usize
+ (self.reference_mesg.0 != u16::MAX) as usize
+ (self.reference_index.0 != u16::MAX) as usize
+ (!self.time_in_hr_zone.is_empty()) as usize
+ (!self.time_in_speed_zone.is_empty()) as usize
+ (!self.time_in_cadence_zone.is_empty()) as usize
+ (!self.time_in_power_zone.is_empty()) as usize
+ (!self.hr_zone_high_boundary.is_empty()) as usize
+ (!self.speed_zone_high_boundary.is_empty()) as usize
+ (!self.cadence_zone_high_boundary.is_empty()) as usize
+ (!self.power_zone_high_boundary.is_empty()) as usize
+ (self.hr_calc_type.0 != u8::MAX) as usize
+ (self.max_heart_rate != u8::MAX) as usize
+ (self.resting_heart_rate != u8::MAX) as usize
+ (self.threshold_heart_rate != u8::MAX) as usize
+ (self.pwr_calc_type.0 != u8::MAX) as usize
+ (self.functional_threshold_power != u16::MAX) as usize
}
}
impl Default for TimeInZone {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for TimeInZone {
fn from(mesg: &Message) -> Self {
const KNOWN_NUMS: [u64; 4] = [65535, 0, 0, 2305843009213693952];
let mut n = 0u64;
for field in &mesg.fields {
n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
}
let mut v = Self::new();
v.unknown_fields = Vec::<Field>::with_capacity(n as usize);
v.developer_fields = mesg.developer_fields.clone();
for field in &mesg.fields {
match field.num {
253 => v.timestamp = typedef::DateTime(field.value.as_u32()),
0 => v.reference_mesg = typedef::MesgNum(field.value.as_u16()),
1 => v.reference_index = typedef::MessageIndex(field.value.as_u16()),
2 => v.time_in_hr_zone = field.value.to_vec_u32(),
3 => v.time_in_speed_zone = field.value.to_vec_u32(),
4 => v.time_in_cadence_zone = field.value.to_vec_u32(),
5 => v.time_in_power_zone = field.value.to_vec_u32(),
6 => v.hr_zone_high_boundary = field.value.to_vec_u8(),
7 => v.speed_zone_high_boundary = field.value.to_vec_u16(),
8 => v.cadence_zone_high_boundary = field.value.to_vec_u8(),
9 => v.power_zone_high_boundary = field.value.to_vec_u16(),
10 => v.hr_calc_type = typedef::HrZoneCalc(field.value.as_u8()),
11 => v.max_heart_rate = field.value.as_u8(),
12 => v.resting_heart_rate = field.value.as_u8(),
13 => v.threshold_heart_rate = field.value.as_u8(),
14 => v.pwr_calc_type = typedef::PwrZoneCalc(field.value.as_u8()),
15 => v.functional_threshold_power = field.value.as_u16(),
_ => v.unknown_fields.push(field.clone()),
};
}
v
}
}
impl From<TimeInZone> for Message {
fn from(m: TimeInZone) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if m.timestamp.0 != u32::MAX {
fields.push(Field {
num: 253,
base_type: FitBaseType::UINT32,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
});
};
if m.reference_mesg.0 != u16::MAX {
fields.push(Field {
num: 0,
base_type: FitBaseType::UINT16,
value: Value::Uint16(m.reference_mesg.0),
is_expanded: false,
});
};
if m.reference_index.0 != u16::MAX {
fields.push(Field {
num: 1,
base_type: FitBaseType::UINT16,
value: Value::Uint16(m.reference_index.0),
is_expanded: false,
});
};
if !m.time_in_hr_zone.is_empty() {
fields.push(Field {
num: 2,
base_type: FitBaseType::UINT32,
value: Value::VecUint32(m.time_in_hr_zone),
is_expanded: false,
});
};
if !m.time_in_speed_zone.is_empty() {
fields.push(Field {
num: 3,
base_type: FitBaseType::UINT32,
value: Value::VecUint32(m.time_in_speed_zone),
is_expanded: false,
});
};
if !m.time_in_cadence_zone.is_empty() {
fields.push(Field {
num: 4,
base_type: FitBaseType::UINT32,
value: Value::VecUint32(m.time_in_cadence_zone),
is_expanded: false,
});
};
if !m.time_in_power_zone.is_empty() {
fields.push(Field {
num: 5,
base_type: FitBaseType::UINT32,
value: Value::VecUint32(m.time_in_power_zone),
is_expanded: false,
});
};
if !m.hr_zone_high_boundary.is_empty() {
fields.push(Field {
num: 6,
base_type: FitBaseType::UINT8,
value: Value::VecUint8(m.hr_zone_high_boundary),
is_expanded: false,
});
};
if !m.speed_zone_high_boundary.is_empty() {
fields.push(Field {
num: 7,
base_type: FitBaseType::UINT16,
value: Value::VecUint16(m.speed_zone_high_boundary),
is_expanded: false,
});
};
if !m.cadence_zone_high_boundary.is_empty() {
fields.push(Field {
num: 8,
base_type: FitBaseType::UINT8,
value: Value::VecUint8(m.cadence_zone_high_boundary),
is_expanded: false,
});
};
if !m.power_zone_high_boundary.is_empty() {
fields.push(Field {
num: 9,
base_type: FitBaseType::UINT16,
value: Value::VecUint16(m.power_zone_high_boundary),
is_expanded: false,
});
};
if m.hr_calc_type.0 != u8::MAX {
fields.push(Field {
num: 10,
base_type: FitBaseType::ENUM,
value: Value::Uint8(m.hr_calc_type.0),
is_expanded: false,
});
};
if m.max_heart_rate != u8::MAX {
fields.push(Field {
num: 11,
base_type: FitBaseType::UINT8,
value: Value::Uint8(m.max_heart_rate),
is_expanded: false,
});
};
if m.resting_heart_rate != u8::MAX {
fields.push(Field {
num: 12,
base_type: FitBaseType::UINT8,
value: Value::Uint8(m.resting_heart_rate),
is_expanded: false,
});
};
if m.threshold_heart_rate != u8::MAX {
fields.push(Field {
num: 13,
base_type: FitBaseType::UINT8,
value: Value::Uint8(m.threshold_heart_rate),
is_expanded: false,
});
};
if m.pwr_calc_type.0 != u8::MAX {
fields.push(Field {
num: 14,
base_type: FitBaseType::ENUM,
value: Value::Uint8(m.pwr_calc_type.0),
is_expanded: false,
});
};
if m.functional_threshold_power != u16::MAX {
fields.push(Field {
num: 15,
base_type: FitBaseType::UINT16,
value: Value::Uint16(m.functional_threshold_power),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::TIME_IN_ZONE,
fields,
developer_fields: m.developer_fields,
}
}
}
#[cfg(feature = "serde")]
impl Serialize for TimeInZone {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let n = self.count_valid_fields() + 2;
let mut state = serializer.serialize_struct("TimeInZone", n)?;
if let Some(v) = self.timestamp.unix_timestamp() {
state.serialize_field("timestamp", &v)?;
}
if self.reference_mesg.0 != u16::MAX {
state.serialize_field("reference_mesg", &self.reference_mesg)?;
}
if self.reference_index.0 != u16::MAX {
state.serialize_field("reference_index", &self.reference_index)?;
}
if let Some(v) = self.time_in_hr_zone_scaled() {
state.serialize_field("time_in_hr_zone", &v)?;
}
if let Some(v) = self.time_in_speed_zone_scaled() {
state.serialize_field("time_in_speed_zone", &v)?;
}
if let Some(v) = self.time_in_cadence_zone_scaled() {
state.serialize_field("time_in_cadence_zone", &v)?;
}
if let Some(v) = self.time_in_power_zone_scaled() {
state.serialize_field("time_in_power_zone", &v)?;
}
if !self.hr_zone_high_boundary.is_empty() {
state.serialize_field("hr_zone_high_boundary", &self.hr_zone_high_boundary)?;
}
if let Some(v) = self.speed_zone_high_boundary_scaled() {
state.serialize_field("speed_zone_high_boundary", &v)?;
}
if !self.cadence_zone_high_boundary.is_empty() {
state.serialize_field(
"cadence_zone_high_boundary",
&self.cadence_zone_high_boundary,
)?;
}
if !self.power_zone_high_boundary.is_empty() {
state.serialize_field("power_zone_high_boundary", &self.power_zone_high_boundary)?;
}
if self.hr_calc_type.0 != u8::MAX {
state.serialize_field("hr_calc_type", &self.hr_calc_type)?;
}
if self.max_heart_rate != u8::MAX {
state.serialize_field("max_heart_rate", &self.max_heart_rate)?;
}
if self.resting_heart_rate != u8::MAX {
state.serialize_field("resting_heart_rate", &self.resting_heart_rate)?;
}
if self.threshold_heart_rate != u8::MAX {
state.serialize_field("threshold_heart_rate", &self.threshold_heart_rate)?;
}
if self.pwr_calc_type.0 != u8::MAX {
state.serialize_field("pwr_calc_type", &self.pwr_calc_type)?;
}
if self.functional_threshold_power != u16::MAX {
state.serialize_field(
"functional_threshold_power",
&self.functional_threshold_power,
)?;
}
if !self.unknown_fields.is_empty() {
state.serialize_field("unknown_fields", &self.unknown_fields)?;
}
if !self.developer_fields.is_empty() {
state.serialize_field("developer_fields", &self.developer_fields)?;
}
state.end()
}
}
#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize), serde(default))]
struct De {
timestamp: Option<i64>,
reference_mesg: typedef::MesgNum,
reference_index: typedef::MessageIndex,
time_in_hr_zone: Vec<f64>,
time_in_speed_zone: Vec<f64>,
time_in_cadence_zone: Vec<f64>,
time_in_power_zone: Vec<f64>,
hr_zone_high_boundary: Vec<u8>,
speed_zone_high_boundary: Vec<f64>,
cadence_zone_high_boundary: Vec<u8>,
power_zone_high_boundary: Vec<u16>,
hr_calc_type: typedef::HrZoneCalc,
max_heart_rate: u8,
resting_heart_rate: u8,
threshold_heart_rate: u8,
pwr_calc_type: typedef::PwrZoneCalc,
functional_threshold_power: u16,
unknown_fields: Vec<Field>,
developer_fields: Vec<DeveloperField>,
}
#[cfg(feature = "serde")]
impl From<De> for TimeInZone {
fn from(m: De) -> Self {
Self {
timestamp: m.timestamp.map_or_else(
|| typedef::DateTime(u32::MAX),
typedef::DateTime::from_unix_timestamp,
),
reference_mesg: m.reference_mesg,
reference_index: m.reference_index,
time_in_hr_zone: {
if m.time_in_hr_zone.is_empty() {
Vec::new()
} else {
let mut vals = Vec::with_capacity(m.time_in_hr_zone.len());
for &x in m.time_in_hr_zone.iter() {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64
{
vals.push(u32::MAX);
continue;
}
vals.push(unscaled as u32);
}
vals
}
},
time_in_speed_zone: {
if m.time_in_speed_zone.is_empty() {
Vec::new()
} else {
let mut vals = Vec::with_capacity(m.time_in_speed_zone.len());
for &x in m.time_in_speed_zone.iter() {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64
{
vals.push(u32::MAX);
continue;
}
vals.push(unscaled as u32);
}
vals
}
},
time_in_cadence_zone: {
if m.time_in_cadence_zone.is_empty() {
Vec::new()
} else {
let mut vals = Vec::with_capacity(m.time_in_cadence_zone.len());
for &x in m.time_in_cadence_zone.iter() {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64
{
vals.push(u32::MAX);
continue;
}
vals.push(unscaled as u32);
}
vals
}
},
time_in_power_zone: {
if m.time_in_power_zone.is_empty() {
Vec::new()
} else {
let mut vals = Vec::with_capacity(m.time_in_power_zone.len());
for &x in m.time_in_power_zone.iter() {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64
{
vals.push(u32::MAX);
continue;
}
vals.push(unscaled as u32);
}
vals
}
},
hr_zone_high_boundary: m.hr_zone_high_boundary,
speed_zone_high_boundary: {
if m.speed_zone_high_boundary.is_empty() {
Vec::new()
} else {
let mut vals = Vec::with_capacity(m.speed_zone_high_boundary.len());
for &x in m.speed_zone_high_boundary.iter() {
let unscaled = (x + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64
{
vals.push(u16::MAX);
continue;
}
vals.push(unscaled as u16);
}
vals
}
},
cadence_zone_high_boundary: m.cadence_zone_high_boundary,
power_zone_high_boundary: m.power_zone_high_boundary,
hr_calc_type: m.hr_calc_type,
max_heart_rate: m.max_heart_rate,
resting_heart_rate: m.resting_heart_rate,
threshold_heart_rate: m.threshold_heart_rate,
pwr_calc_type: m.pwr_calc_type,
functional_threshold_power: m.functional_threshold_power,
unknown_fields: m.unknown_fields,
developer_fields: m.developer_fields,
}
}
}
#[cfg(feature = "serde")]
impl Default for De {
fn default() -> Self {
Self {
timestamp: None,
reference_mesg: typedef::MesgNum(u16::MAX),
reference_index: typedef::MessageIndex(u16::MAX),
time_in_hr_zone: Vec::new(),
time_in_speed_zone: Vec::new(),
time_in_cadence_zone: Vec::new(),
time_in_power_zone: Vec::new(),
hr_zone_high_boundary: Vec::new(),
speed_zone_high_boundary: Vec::new(),
cadence_zone_high_boundary: Vec::new(),
power_zone_high_boundary: Vec::new(),
hr_calc_type: typedef::HrZoneCalc(u8::MAX),
max_heart_rate: u8::MAX,
resting_heart_rate: u8::MAX,
threshold_heart_rate: u8::MAX,
pwr_calc_type: typedef::PwrZoneCalc(u8::MAX),
functional_threshold_power: u16::MAX,
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
}