#![allow(unused, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
use alloc::vec::Vec;
#[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) -> Vec<f64> {
if self.time_in_hr_zone.is_empty() {
return Vec::new();
}
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)
}
v
}
pub fn set_time_in_hr_zone_scaled(&mut self, v: &Vec<f64>) -> &mut TimeInZone {
if v.is_empty() {
self.time_in_hr_zone = Vec::new();
return self;
}
self.time_in_hr_zone = Vec::with_capacity(v.len());
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) -> Vec<f64> {
if self.time_in_speed_zone.is_empty() {
return Vec::new();
}
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)
}
v
}
pub fn set_time_in_speed_zone_scaled(&mut self, v: &Vec<f64>) -> &mut TimeInZone {
if v.is_empty() {
self.time_in_speed_zone = Vec::new();
return self;
}
self.time_in_speed_zone = Vec::with_capacity(v.len());
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) -> Vec<f64> {
if self.time_in_cadence_zone.is_empty() {
return Vec::new();
}
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)
}
v
}
pub fn set_time_in_cadence_zone_scaled(&mut self, v: &Vec<f64>) -> &mut TimeInZone {
if v.is_empty() {
self.time_in_cadence_zone = Vec::new();
return self;
}
self.time_in_cadence_zone = Vec::with_capacity(v.len());
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) -> Vec<f64> {
if self.time_in_power_zone.is_empty() {
return Vec::new();
}
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)
}
v
}
pub fn set_time_in_power_zone_scaled(&mut self, v: &Vec<f64>) -> &mut TimeInZone {
if v.is_empty() {
self.time_in_power_zone = Vec::new();
return self;
}
self.time_in_power_zone = Vec::with_capacity(v.len());
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) -> Vec<f64> {
if self.speed_zone_high_boundary.is_empty() {
return Vec::new();
}
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)
}
v
}
pub fn set_speed_zone_high_boundary_scaled(&mut self, v: &Vec<f64>) -> &mut TimeInZone {
if v.is_empty() {
self.speed_zone_high_boundary = Vec::new();
return self;
}
self.speed_zone_high_boundary = Vec::with_capacity(v.len());
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 != typedef::DateTime(u32::MAX)) as usize
+ (self.reference_mesg != typedef::MesgNum(u16::MAX)) as usize
+ (self.reference_index != typedef::MessageIndex(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 != typedef::HrZoneCalc(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 != typedef::PwrZoneCalc(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 != typedef::DateTime(u32::MAX) {
fields.push(Field {
num: 253,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
});
};
if m.reference_mesg != typedef::MesgNum(u16::MAX) {
fields.push(Field {
num: 0,
profile_type: ProfileType::MESG_NUM,
value: Value::Uint16(m.reference_mesg.0),
is_expanded: false,
});
};
if m.reference_index != typedef::MessageIndex(u16::MAX) {
fields.push(Field {
num: 1,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.reference_index.0),
is_expanded: false,
});
};
if !m.time_in_hr_zone.is_empty() {
fields.push(Field {
num: 2,
profile_type: ProfileType::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,
profile_type: ProfileType::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,
profile_type: ProfileType::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,
profile_type: ProfileType::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,
profile_type: ProfileType::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,
profile_type: ProfileType::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,
profile_type: ProfileType::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,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.power_zone_high_boundary),
is_expanded: false,
});
};
if m.hr_calc_type != typedef::HrZoneCalc(u8::MAX) {
fields.push(Field {
num: 10,
profile_type: ProfileType::HR_ZONE_CALC,
value: Value::Uint8(m.hr_calc_type.0),
is_expanded: false,
});
};
if m.max_heart_rate != u8::MAX {
fields.push(Field {
num: 11,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.max_heart_rate),
is_expanded: false,
});
};
if m.resting_heart_rate != u8::MAX {
fields.push(Field {
num: 12,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.resting_heart_rate),
is_expanded: false,
});
};
if m.threshold_heart_rate != u8::MAX {
fields.push(Field {
num: 13,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.threshold_heart_rate),
is_expanded: false,
});
};
if m.pwr_calc_type != typedef::PwrZoneCalc(u8::MAX) {
fields.push(Field {
num: 14,
profile_type: ProfileType::PWR_ZONE_CALC,
value: Value::Uint8(m.pwr_calc_type.0),
is_expanded: false,
});
};
if m.functional_threshold_power != u16::MAX {
fields.push(Field {
num: 15,
profile_type: ProfileType::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,
}
}
}