#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct MonitoringInfo {
pub timestamp: typedef::DateTime,
pub local_timestamp: typedef::LocalDateTime,
pub activity_type: Vec<typedef::ActivityType>,
pub cycles_to_distance: Vec<u16>,
pub cycles_to_calories: Vec<u16>,
pub resting_metabolic_rate: u16,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl MonitoringInfo {
pub const TIMESTAMP: u8 = 253;
pub const LOCAL_TIMESTAMP: u8 = 0;
pub const ACTIVITY_TYPE: u8 = 1;
pub const CYCLES_TO_DISTANCE: u8 = 3;
pub const CYCLES_TO_CALORIES: u8 = 4;
pub const RESTING_METABOLIC_RATE: u8 = 5;
pub const fn new() -> Self {
Self {
timestamp: typedef::DateTime(u32::MAX),
local_timestamp: typedef::LocalDateTime(u32::MAX),
activity_type: Vec::<typedef::ActivityType>::new(),
cycles_to_distance: Vec::<u16>::new(),
cycles_to_calories: Vec::<u16>::new(),
resting_metabolic_rate: u16::MAX,
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn cycles_to_distance_scaled(&self) -> Vec<f64> {
if self.cycles_to_distance == Vec::<u16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.cycles_to_distance.len());
for &x in &self.cycles_to_distance {
v.push(x as f64 / 5000.0 - 0.0)
}
v
}
pub fn set_cycles_to_distance_scaled(&mut self, v: &Vec<f64>) -> &mut MonitoringInfo {
if v.is_empty() {
self.cycles_to_distance = Vec::new();
return self;
}
self.cycles_to_distance = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 5000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
self.cycles_to_distance.push(u16::MAX);
continue;
}
self.cycles_to_distance.push(unscaled as u16);
}
self
}
pub fn cycles_to_calories_scaled(&self) -> Vec<f64> {
if self.cycles_to_calories == Vec::<u16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.cycles_to_calories.len());
for &x in &self.cycles_to_calories {
v.push(x as f64 / 5000.0 - 0.0)
}
v
}
pub fn set_cycles_to_calories_scaled(&mut self, v: &Vec<f64>) -> &mut MonitoringInfo {
if v.is_empty() {
self.cycles_to_calories = Vec::new();
return self;
}
self.cycles_to_calories = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 5000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
self.cycles_to_calories.push(u16::MAX);
continue;
}
self.cycles_to_calories.push(unscaled as u16);
}
self
}
}
impl Default for MonitoringInfo {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for MonitoringInfo {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 254] = [const { &Value::Invalid }; 254];
const KNOWN_NUMS: [u64; 4] = [59, 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 unknown_fields: Vec<Field> = Vec::with_capacity(n as usize);
for field in &mesg.fields {
if (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 == 0 {
unknown_fields.push(field.clone());
continue;
}
vals[field.num as usize] = &field.value;
}
Self {
timestamp: typedef::DateTime(vals[253].as_u32()),
local_timestamp: typedef::LocalDateTime(vals[0].as_u32()),
activity_type: match &vals[1] {
Value::VecUint8(v) => {
let mut vs = Vec::with_capacity(v.len());
for x in v {
vs.push(typedef::ActivityType(*x))
}
vs
}
_ => Vec::new(),
},
cycles_to_distance: vals[3].as_vec_u16(),
cycles_to_calories: vals[4].as_vec_u16(),
resting_metabolic_rate: vals[5].as_u16(),
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<MonitoringInfo> for Message {
fn from(m: MonitoringInfo) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 6];
let mut len = 0usize;
if m.timestamp != typedef::DateTime(u32::MAX) {
arr[len] = Field {
num: 253,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
};
len += 1;
}
if m.local_timestamp != typedef::LocalDateTime(u32::MAX) {
arr[len] = Field {
num: 0,
profile_type: ProfileType::LOCAL_DATE_TIME,
value: Value::Uint32(m.local_timestamp.0),
is_expanded: false,
};
len += 1;
}
if m.activity_type != Vec::<typedef::ActivityType>::new() {
arr[len] = Field {
num: 1,
profile_type: ProfileType::ACTIVITY_TYPE,
value: Value::VecUint8({
let mut v = Vec::with_capacity(m.activity_type.len());
for x in &m.activity_type {
v.push(x.0)
}
v
}),
is_expanded: false,
};
len += 1;
}
if m.cycles_to_distance != Vec::<u16>::new() {
arr[len] = Field {
num: 3,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.cycles_to_distance),
is_expanded: false,
};
len += 1;
}
if m.cycles_to_calories != Vec::<u16>::new() {
arr[len] = Field {
num: 4,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.cycles_to_calories),
is_expanded: false,
};
len += 1;
}
if m.resting_metabolic_rate != u16::MAX {
arr[len] = Field {
num: 5,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.resting_metabolic_rate),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::MONITORING_INFO,
fields: {
let mut fields: Vec<Field> = Vec::with_capacity(len + m.unknown_fields.len());
fields.extend_from_slice(&arr[..len]);
fields.extend_from_slice(&m.unknown_fields);
fields
},
developer_fields: m.developer_fields,
}
}
}