This commit is contained in:
lkoester 2023-01-24 09:49:15 +01:00
parent 47e36cb0b5
commit 9e77b31d5a
5 changed files with 61 additions and 48 deletions

View File

@ -12,6 +12,7 @@ pub enum ActionIds {
pub enum ActionRequest {
ImageRequest(RequestTargetId),
OrientationRequest(RequestTargetId),
PointingRequest(RequestTargetId),
}
pub enum Subservice {

View File

@ -16,6 +16,7 @@ pub enum ThreadId {
AOCSThread,
PowerThread,
PLDThread,
DeviceHandlerThread,
}
#[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)]

View File

@ -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<DeviceId, DeviceState>,
can_tx_handler: CanTxHandler,
can_rx_receiver: Receiver<PackageModel>,
}
impl DeviceHandler {
pub fn new() -> DeviceHandler {
pub fn new(can_tx_handler: CanTxHandler, can_rx_receiver: Receiver<PackageModel>) -> DeviceHandler {
let mut device_state_map:HashMap<DeviceId, DeviceState> = 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(())
}
}

View File

@ -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,
}
}

View File

@ -310,6 +310,7 @@ impl PusReceiver {
.send(RequestWithToken(Request::ActionRequest(request), token))
.unwrap_or_else(|_| panic!("Sending Action request {:?} failed", request));
}
_ => {}
}
};