From 9e77b31d5a16f5bcec9a2d024359b85cdb345cd3 Mon Sep 17 00:00:00 2001 From: lkoester Date: Tue, 24 Jan 2023 09:49:15 +0100 Subject: [PATCH] ... --- src/action.rs | 1 + src/can_ids.rs | 1 + src/device_handler.rs | 90 ++++++++++++++++++++++++++----------------- src/main.rs | 16 ++------ src/pus.rs | 1 + 5 files changed, 61 insertions(+), 48 deletions(-) diff --git a/src/action.rs b/src/action.rs index 59f7f97..bb5fc76 100644 --- a/src/action.rs +++ b/src/action.rs @@ -12,6 +12,7 @@ pub enum ActionIds { pub enum ActionRequest { ImageRequest(RequestTargetId), OrientationRequest(RequestTargetId), + PointingRequest(RequestTargetId), } pub enum Subservice { diff --git a/src/can_ids.rs b/src/can_ids.rs index b7e388b..ec34c2c 100644 --- a/src/can_ids.rs +++ b/src/can_ids.rs @@ -16,6 +16,7 @@ pub enum ThreadId { AOCSThread, PowerThread, PLDThread, + DeviceHandlerThread, } #[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)] diff --git a/src/device_handler.rs b/src/device_handler.rs index ec2bf69..b8ea961 100644 --- a/src/device_handler.rs +++ b/src/device_handler.rs @@ -1,15 +1,17 @@ use std::collections::HashMap; use std::hash::Hash; +use std::sync::mpsc::{Receiver, Sender}; use log::info; -use crate::can_ids::{DeviceId, PackageId}; +use crate::can_ids::{DeviceId, PackageId, PackageModel, ThreadId}; use socketcan::{errors, frame, socket, CanFrame, Socket}; 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 +pub use strum_macros::EnumIter; +use crate::can::{CanRxHandler, CanTxHandler}; // 0.17.1 -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq)] pub enum DeviceState { On, Off, @@ -18,22 +20,24 @@ pub enum DeviceState { } -#[derive(Clone, Debug)] + pub struct DeviceHandler { device_state_map: HashMap, + can_tx_handler: CanTxHandler, + can_rx_receiver: Receiver, } impl DeviceHandler { - pub fn new() -> DeviceHandler { + pub fn new(can_tx_handler: CanTxHandler, can_rx_receiver: Receiver) -> DeviceHandler { let mut device_state_map:HashMap = HashMap::new(); for id in DeviceId::iter() { device_state_map.insert(id, DeviceState::Unknown); } - DeviceHandler{ device_state_map } + DeviceHandler{ device_state_map, can_tx_handler, can_rx_receiver } } - pub fn power_up_seq(&self) { + pub fn power_up_seq(&mut self) { for id in DeviceId::iter() { let power_on_success = self.power_on(id); match power_on_success{ @@ -43,39 +47,55 @@ impl DeviceHandler { } } - pub fn power_on(&self, id: DeviceId) -> Result<(),()> { - match id { - DeviceId::OBC => {} - DeviceId::PCDU => {} - DeviceId::MGM1 => {} - DeviceId::MGM2 => {} - DeviceId::MGM3 => {} - DeviceId::MGM4 => {} - DeviceId::SunSensor1 => {} - DeviceId::SunSensor2 => {} - DeviceId::SunSensor3 => {} - DeviceId::SunSensor4 => {} - DeviceId::SunSensor5 => {} - DeviceId::SunSensor6 => {} - DeviceId::StarTracker => {} - DeviceId::MGT1 => {} - DeviceId::MGT2 => {} - DeviceId::MGT3 => {} - DeviceId::MGT4 => {} - DeviceId::RWL1 => {} - DeviceId::RWL2 => {} - DeviceId::RWL3 => {} - DeviceId::RWL4 => {} - DeviceId::Camera => {} - DeviceId::All => {} + pub fn power_on(&mut self, id: DeviceId) -> Result<(),()> { + if !self.device_state_map.contains_key(&id) { + return Err(()); } - self.get_power_state(id) + let msg_data: [u8; 1] = [id as u8]; + self.can_tx_handler.tx_socket(PackageId::DevicePowerOnRequest, &msg_data); + match self.get_power_state(id) { + None => {return Err(());} + Some(power_state) => { + if *power_state != DeviceState::On { + return Err(()); + } + } + } + + Ok(()) } - pub fn get_power_state(&self, id: DeviceId) -> Result<(),()> { - let power_state:DeviceState = DeviceState::Unknown; + pub fn get_power_state(&mut self, id: DeviceId) -> Option<&DeviceState> { + self.update_power_state(id).expect("Error updating power state."); + self.device_state_map.get(&id) + } + + pub fn update_power_state(&mut self, id: DeviceId) -> Result<(),()> { + if !self.device_state_map.contains_key(&id) { + return Err(()); + } + + let msg_data: [u8; 1] = [id as u8]; + self.can_tx_handler.tx_socket(PackageId::DevicePowerStatusRequest, &msg_data); + + let response = self.can_rx_receiver.recv(); + if let Ok(response) = response { + let data = response.data(); + + if data[0] == id as u8 { + if data[1] == 1 { + *self.device_state_map.get_mut(&id).unwrap() = DeviceState::On; + } else if data[1] == 0 { + *self.device_state_map.get_mut(&id).unwrap() = DeviceState::Off; + } else { + return Err(()); + } + } + } else { + return Err(()); + } Ok(()) } } diff --git a/src/main.rs b/src/main.rs index e507910..3c6c49d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -218,15 +218,6 @@ fn main() { } }); - /* - let allowed_ids_range = 101..110; - let mut allowed_ids_aocs = Vec::new(); - for id in allowed_ids_range { - allowed_ids_aocs.push(Id::Standard(StandardId::new(id).unwrap())); - } - - */ - let package_map_aocs_tx = load_package_ids(); let aocs_tm_funnel_tx = tm_funnel_tx.clone(); let mut aocs_tm_store = tm_store.clone(); @@ -408,6 +399,7 @@ fn main() { } } ActionRequest::OrientationRequest(_) => {} + _ => {} } } _ => {} @@ -415,9 +407,7 @@ fn main() { } Err(_) => {} } - //let addr = pld_tm_store.add_pus_tm(&hk_tm); - //pld_tm_funnel_tx.send(a).expect("sending failed"); - } + } }); @@ -457,4 +447,4 @@ struct MgmData { x: i16, y: i16, z: i16, -} +} \ No newline at end of file diff --git a/src/pus.rs b/src/pus.rs index 8623c01..b08bb35 100644 --- a/src/pus.rs +++ b/src/pus.rs @@ -310,6 +310,7 @@ impl PusReceiver { .send(RequestWithToken(Request::ActionRequest(request), token)) .unwrap_or_else(|_| panic!("Sending Action request {:?} failed", request)); } + _ => {} } };