#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
fn is_expanded(state: &[u8], num: u8) -> bool {
match num {
3 | 7 | 8 | 9 | 10 | 11 | 12 | 21 | 22 | 23 | 24 => {
(state[num as usize >> 3] >> (num & 7)) & 1 == 1
}
_ => false,
}
}
#[derive(Debug, Clone)]
pub struct Event {
pub timestamp: typedef::DateTime,
pub event: typedef::Event,
pub event_type: typedef::EventType,
pub data16: u16,
pub data: u32,
pub event_group: u8,
pub score: u16,
pub opponent_score: u16,
pub front_gear_num: u8,
pub front_gear: u8,
pub rear_gear_num: u8,
pub rear_gear: u8,
pub device_index: typedef::DeviceIndex,
pub activity_type: typedef::ActivityType,
pub start_timestamp: typedef::DateTime,
pub radar_threat_level_max: typedef::RadarThreatLevelType,
pub radar_threat_count: u8,
pub radar_threat_avg_approach_speed: u8,
pub radar_threat_max_approach_speed: u8,
state: [u8; 4], pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl Event {
pub const TIMESTAMP: u8 = 253;
pub const EVENT: u8 = 0;
pub const EVENT_TYPE: u8 = 1;
pub const DATA16: u8 = 2;
pub const DATA: u8 = 3;
pub const EVENT_GROUP: u8 = 4;
pub const SCORE: u8 = 7;
pub const OPPONENT_SCORE: u8 = 8;
pub const FRONT_GEAR_NUM: u8 = 9;
pub const FRONT_GEAR: u8 = 10;
pub const REAR_GEAR_NUM: u8 = 11;
pub const REAR_GEAR: u8 = 12;
pub const DEVICE_INDEX: u8 = 13;
pub const ACTIVITY_TYPE: u8 = 14;
pub const START_TIMESTAMP: u8 = 15;
pub const RADAR_THREAT_LEVEL_MAX: u8 = 21;
pub const RADAR_THREAT_COUNT: u8 = 22;
pub const RADAR_THREAT_AVG_APPROACH_SPEED: u8 = 23;
pub const RADAR_THREAT_MAX_APPROACH_SPEED: u8 = 24;
pub const fn new() -> Self {
Self {
timestamp: typedef::DateTime(u32::MAX),
event: typedef::Event(u8::MAX),
event_type: typedef::EventType(u8::MAX),
data16: u16::MAX,
data: u32::MAX,
event_group: u8::MAX,
score: u16::MAX,
opponent_score: u16::MAX,
front_gear_num: u8::MIN,
front_gear: u8::MIN,
rear_gear_num: u8::MIN,
rear_gear: u8::MIN,
device_index: typedef::DeviceIndex(u8::MAX),
activity_type: typedef::ActivityType(u8::MAX),
start_timestamp: typedef::DateTime(u32::MAX),
radar_threat_level_max: typedef::RadarThreatLevelType(u8::MAX),
radar_threat_count: u8::MAX,
radar_threat_avg_approach_speed: u8::MAX,
radar_threat_max_approach_speed: u8::MAX,
state: [0u8; 4],
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn radar_threat_avg_approach_speed_scaled(&self) -> f64 {
if self.radar_threat_avg_approach_speed == u8::MAX {
return f64::from_bits(u64::MAX);
}
self.radar_threat_avg_approach_speed as f64 / 10.0 - 0.0
}
pub fn set_radar_threat_avg_approach_speed_scaled(&mut self, v: f64) -> &mut Event {
let unscaled = (v + 0.0) * 10.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u8::MAX as f64 {
self.radar_threat_avg_approach_speed = u8::MAX;
return self;
}
self.radar_threat_avg_approach_speed = unscaled as u8;
self
}
pub fn radar_threat_max_approach_speed_scaled(&self) -> f64 {
if self.radar_threat_max_approach_speed == u8::MAX {
return f64::from_bits(u64::MAX);
}
self.radar_threat_max_approach_speed as f64 / 10.0 - 0.0
}
pub fn set_radar_threat_max_approach_speed_scaled(&mut self, v: f64) -> &mut Event {
let unscaled = (v + 0.0) * 10.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u8::MAX as f64 {
self.radar_threat_max_approach_speed = u8::MAX;
return self;
}
self.radar_threat_max_approach_speed = unscaled as u8;
self
}
pub fn mark_as_expanded(&mut self, num: u8, flag: bool) -> bool {
match num {
3 | 7 | 8 | 9 | 10 | 11 | 12 | 21 | 22 | 23 | 24 => {
if flag {
self.state[num as usize >> 3] |= 1 << (num & 7)
} else {
self.state[num as usize >> 3] &= !(1 << (num & 7))
}
true
}
_ => false,
}
}
pub fn is_expanded(&self, num: u8) -> bool {
is_expanded(&self.state, num)
}
}
impl Default for Event {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for Event {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 254] = [const { &Value::Invalid }; 254];
let mut state = [0u8; 4];
const KNOWN_NUMS: [u64; 4] = [31522719, 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;
}
if field.is_expanded && field.num < 25 {
state[field.num as usize >> 3] |= 1 << (field.num & 7)
}
vals[field.num as usize] = &field.value;
}
Self {
timestamp: typedef::DateTime(vals[253].as_u32()),
event: typedef::Event(vals[0].as_u8()),
event_type: typedef::EventType(vals[1].as_u8()),
data16: vals[2].as_u16(),
data: vals[3].as_u32(),
event_group: vals[4].as_u8(),
score: vals[7].as_u16(),
opponent_score: vals[8].as_u16(),
front_gear_num: vals[9].as_u8z(),
front_gear: vals[10].as_u8z(),
rear_gear_num: vals[11].as_u8z(),
rear_gear: vals[12].as_u8z(),
device_index: typedef::DeviceIndex(vals[13].as_u8()),
activity_type: typedef::ActivityType(vals[14].as_u8()),
start_timestamp: typedef::DateTime(vals[15].as_u32()),
radar_threat_level_max: typedef::RadarThreatLevelType(vals[21].as_u8()),
radar_threat_count: vals[22].as_u8(),
radar_threat_avg_approach_speed: vals[23].as_u8(),
radar_threat_max_approach_speed: vals[24].as_u8(),
state,
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<Event> for Message {
fn from(m: Event) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 19];
let mut len = 0usize;
let state = m.state;
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.event != typedef::Event(u8::MAX) {
arr[len] = Field {
num: 0,
profile_type: ProfileType::EVENT,
value: Value::Uint8(m.event.0),
is_expanded: false,
};
len += 1;
}
if m.event_type != typedef::EventType(u8::MAX) {
arr[len] = Field {
num: 1,
profile_type: ProfileType::EVENT_TYPE,
value: Value::Uint8(m.event_type.0),
is_expanded: false,
};
len += 1;
}
if m.data16 != u16::MAX {
arr[len] = Field {
num: 2,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.data16),
is_expanded: false,
};
len += 1;
}
if m.data != u32::MAX {
arr[len] = Field {
num: 3,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.data),
is_expanded: is_expanded(&state, 3),
};
len += 1;
}
if m.event_group != u8::MAX {
arr[len] = Field {
num: 4,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.event_group),
is_expanded: false,
};
len += 1;
}
if m.score != u16::MAX {
arr[len] = Field {
num: 7,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.score),
is_expanded: is_expanded(&state, 7),
};
len += 1;
}
if m.opponent_score != u16::MAX {
arr[len] = Field {
num: 8,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.opponent_score),
is_expanded: is_expanded(&state, 8),
};
len += 1;
}
if m.front_gear_num != u8::MIN {
arr[len] = Field {
num: 9,
profile_type: ProfileType::UINT8Z,
value: Value::Uint8(m.front_gear_num),
is_expanded: is_expanded(&state, 9),
};
len += 1;
}
if m.front_gear != u8::MIN {
arr[len] = Field {
num: 10,
profile_type: ProfileType::UINT8Z,
value: Value::Uint8(m.front_gear),
is_expanded: is_expanded(&state, 10),
};
len += 1;
}
if m.rear_gear_num != u8::MIN {
arr[len] = Field {
num: 11,
profile_type: ProfileType::UINT8Z,
value: Value::Uint8(m.rear_gear_num),
is_expanded: is_expanded(&state, 11),
};
len += 1;
}
if m.rear_gear != u8::MIN {
arr[len] = Field {
num: 12,
profile_type: ProfileType::UINT8Z,
value: Value::Uint8(m.rear_gear),
is_expanded: is_expanded(&state, 12),
};
len += 1;
}
if m.device_index != typedef::DeviceIndex(u8::MAX) {
arr[len] = Field {
num: 13,
profile_type: ProfileType::DEVICE_INDEX,
value: Value::Uint8(m.device_index.0),
is_expanded: false,
};
len += 1;
}
if m.activity_type != typedef::ActivityType(u8::MAX) {
arr[len] = Field {
num: 14,
profile_type: ProfileType::ACTIVITY_TYPE,
value: Value::Uint8(m.activity_type.0),
is_expanded: false,
};
len += 1;
}
if m.start_timestamp != typedef::DateTime(u32::MAX) {
arr[len] = Field {
num: 15,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.start_timestamp.0),
is_expanded: false,
};
len += 1;
}
if m.radar_threat_level_max != typedef::RadarThreatLevelType(u8::MAX) {
arr[len] = Field {
num: 21,
profile_type: ProfileType::RADAR_THREAT_LEVEL_TYPE,
value: Value::Uint8(m.radar_threat_level_max.0),
is_expanded: is_expanded(&state, 21),
};
len += 1;
}
if m.radar_threat_count != u8::MAX {
arr[len] = Field {
num: 22,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.radar_threat_count),
is_expanded: is_expanded(&state, 22),
};
len += 1;
}
if m.radar_threat_avg_approach_speed != u8::MAX {
arr[len] = Field {
num: 23,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.radar_threat_avg_approach_speed),
is_expanded: is_expanded(&state, 23),
};
len += 1;
}
if m.radar_threat_max_approach_speed != u8::MAX {
arr[len] = Field {
num: 24,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.radar_threat_max_approach_speed),
is_expanded: is_expanded(&state, 24),
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::EVENT,
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,
}
}
}