use embedded_can::{Id, StandardId}; use log::warn; use std::collections::HashMap; use std::fs; use std::sync::mpsc::Sender; use std::thread::Thread; pub use num_derive::{FromPrimitive, ToPrimitive}; pub use num_traits::{FromPrimitive, ToPrimitive}; pub use strum::IntoEnumIterator; // 0.17.1 pub use strum_macros::EnumIter; // 0.17.1 #[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)] pub enum ThreadId { AOCSThread, PowerThread, PLDThread, } #[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)] pub enum PackageId { PCDUStatusRequest = 10, DevicePowerOnRequest = 11, DevicePowerOffRequest = 12, DevicePowerStatusRequest = 13, PCDUStatusResponse = 20, DevicePowerStatusResponse = 21, DevicePowerOnRequestConfirmation = 22, DevicePowerOffRequestConfirmation = 23, DevicePowerOnConfirmation = 24, DevicePowerOffConfirmation = 25, AOCSControlMGT1 = 31, AOCSControlMGT2 = 32, AOCSControlMGT3 = 33, AOCSControlMGT4 = 34, AOCSControlRWL1 = 35, AOCSControlRWL2 = 36, AOCSControlRWL3 = 37, AOCSControlRWL4 = 38, AOCSDataRequestMGM1 = 41, AOCSDataRequestMGM2 = 42, AOCSDataRequestMGM3 = 43, AOCSDataRequestMGM4 = 44, AOCSDataRequestSunSensor1 = 45, AOCSDataRequestSunSensor2 = 46, AOCSDataRequestSunSensor3 = 47, AOCSDataRequestSunSensor4 = 48, AOCSDataRequestSunSensor5 = 49, AOCSDataRequestSunSensor6 = 50, AOCSDataRequestStarTracker = 51, AOCSDataMGM1 = 61, AOCSDataMGM2 = 62, AOCSDataMGM3 = 63, AOCSDataMGM4 = 64, AOCSDataSunSensor1 = 65, AOCSDataSunSensor2 = 66, AOCSDataSunSensor3 = 67, AOCSDataSunSensor4 = 68, AOCSDataSunSensor5 = 69, AOCSDataSunSensor6 = 70, AOCSDataStarTracker = 71, CameraImageRequest = 101 , CameraImageRequestConfirmation = 102, CameraImageExecutionStart = 103, CameraImageExectutionEnd = 104, } #[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)] pub enum DeviceId { OBC = 1, PCDU = 2, MGM1 = 3, MGM2 = 4, MGM3 = 5, MGM4 = 6, SunSensor1 = 7, SunSensor2 = 8, SunSensor3 = 9, SunSensor4 = 10, SunSensor5 = 11, SunSensor6 = 12, StarTracker = 13, MGT1 = 14, MGT2 = 15, MGT3 = 16, MGT4 = 17, RWL1 = 18, RWL2 = 19, RWL3 = 20, RWL4 = 21, Camera = 22, All = 23, } #[derive(Debug)] pub struct PackageModel { package_id: PackageId, data: Vec, } impl PackageModel { pub fn new(package_id: PackageId, data: &[u8]) -> Result { if data.len() > 8 { warn!("Data exceeds maximum length."); return Err(()); } let vec = Vec::from(data); return Ok(PackageModel { package_id, data: vec, }); } pub fn package_id(&self) -> PackageId { self.package_id } pub fn data(&self) -> &Vec { &self.data } } #[derive(Debug, Copy, Clone)] pub struct SenderReceiverThread { sender: DeviceId, receiver: DeviceId, thread: ThreadId, } impl SenderReceiverThread { pub fn new(sender: DeviceId, receiver: DeviceId, thread: ThreadId) -> SenderReceiverThread { SenderReceiverThread { sender, receiver, thread, } } pub fn get_sender(&self) -> DeviceId { self.sender } pub fn get_receiver(&self) -> DeviceId { self.receiver } pub fn get_thread(&self) -> ThreadId { self.thread } } pub fn load_package_ids() -> HashMap { let mut package_map: HashMap = HashMap::new(); let properties = vec![ SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT1, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT2, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT3, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT4, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL1, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL2, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL3, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL4, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM1, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM2, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM3, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM4, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor1, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor2, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor3, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor4, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor5, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor6, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::OBC, DeviceId::StarTracker, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::MGM1, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::MGM2, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::MGM3, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::MGM4, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::SunSensor1, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::SunSensor2, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::SunSensor3, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::SunSensor4, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::SunSensor5, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::SunSensor6, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId::StarTracker, DeviceId::OBC, ThreadId::AOCSThread), SenderReceiverThread::new(DeviceId:: OBC , DeviceId:: Camera , ThreadId:: PLDThread ), SenderReceiverThread::new(DeviceId::Camera, DeviceId::OBC, ThreadId::PLDThread), SenderReceiverThread::new(DeviceId::Camera, DeviceId::OBC, ThreadId::PLDThread), SenderReceiverThread::new(DeviceId::Camera, DeviceId::OBC, ThreadId::PLDThread), ]; let mut i = 0; for id in PackageId::iter() { let value = properties.get(i).unwrap(); package_map.insert(id, *value); i += 1; } return package_map; } /* pub fn load_device_ids() { let mut package_map: HashMap = HashMap::new(); let properties = vec![ DeviceProperties::new(DeviceType::OBC, DeviceState::Unknown), DeviceProperties::new(DeviceType::PCDU, DeviceState::Unknown), DeviceProperties::new(DeviceType::MGM, DeviceState::Unknown), DeviceProperties::new(DeviceType::MGM, DeviceState::Unknown), DeviceProperties::new(DeviceType::MGM, DeviceState::Unknown), DeviceProperties::new(DeviceType::CRS, DeviceState::Unknown), DeviceProperties::new(DeviceType::CRS, DeviceState::Unknown), DeviceProperties::new(DeviceType::CRS, DeviceState::Unknown), DeviceProperties::new(DeviceType::STR, DeviceState::Unknown), DeviceProperties::new(DeviceType::MGT, DeviceState::Unknown), DeviceProperties::new(DeviceType::MGT, DeviceState::Unknown), DeviceProperties::new(DeviceType::MGT, DeviceState::Unknown), DeviceProperties::new(DeviceType::RWL, DeviceState::Unknown), DeviceProperties::new(DeviceType::RWL, DeviceState::Unknown), DeviceProperties::new(DeviceType::RWL, DeviceState::Unknown), DeviceProperties::new(DeviceType::CAM, DeviceState::Unknown), ]; let mut i = 0; for id in DeviceId::iter() { let value = properties.get(i).unwrap(); package_map.insert(id, *value); i += 1; } } */ //TODO: change ids from u32 to embeddedcan ids pub fn package_id_to_value(packageid: &PackageId) -> u32 { *packageid as u32 } pub fn package_id_to_can_id(packageid: &PackageId) -> Id { let x = *packageid as u16; Id::Standard(StandardId::new(x).unwrap()) } pub fn device_id_to_value(deviceid: &DeviceId) -> u32 { *deviceid as u32 } pub fn value_to_package_id(value: u32) -> Option { let element = FromPrimitive::from_u32(value); return element; } pub fn can_id_to_package_id(value: Id) -> Option { let buf = match value { Id::Standard(id) => id.as_raw() as u32, Id::Extended(id) => id.as_raw(), }; let element = FromPrimitive::from_u32(buf); return element; } pub fn value_to_device_id(value: u32) -> Option { let element = FromPrimitive::from_u32(value); return element; }