added can device handler implementation

This commit is contained in:
lkoester 2023-01-24 11:10:34 +01:00
parent 9e77b31d5a
commit 875ac6280a
2 changed files with 131 additions and 24 deletions

View File

@ -151,7 +151,7 @@ impl CanTxHandler {
if self.package_map.contains_key(&package_id) {
let value = self.package_map.get(&package_id).unwrap();
if value.get_sender() == DeviceId::OBC {
if value.get_thread() == self.thread_id {
//if value.get_thread() == self.thread_id {
if data.len() <= 8 {
let frame_id = package_id_to_can_id(&package_id);
let frame = CanFrame::new(frame_id, data);
@ -166,12 +166,14 @@ impl CanTxHandler {
data.len()
);
}
} else {
/*} else {
warn!(
"Message dismissed, mismatched thread id: {:?}",
value.get_thread()
);
}
*/
} else {
warn!(
"Message dismissed, wrong sender id: {:?}",

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::mpsc::{Receiver, Sender};
use log::info;
use std::sync::mpsc::{Receiver, RecvError, Sender};
use log::{info, warn};
use crate::can_ids::{DeviceId, PackageId, PackageModel, ThreadId};
use socketcan::{errors, frame, socket, CanFrame, Socket};
@ -19,54 +19,145 @@ pub enum DeviceState {
Unknown,
}
pub struct DeviceHandler {
pub struct CanDeviceHandler {
device_state_map: HashMap<DeviceId, DeviceState>,
can_tx_handler: CanTxHandler,
can_rx_receiver: Receiver<PackageModel>,
}
impl DeviceHandler {
pub fn new(can_tx_handler: CanTxHandler, can_rx_receiver: Receiver<PackageModel>) -> DeviceHandler {
impl CanDeviceHandler {
pub fn new(can_tx_handler: CanTxHandler, can_rx_receiver: Receiver<PackageModel>) -> CanDeviceHandler {
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, can_tx_handler, can_rx_receiver }
}
pub fn power_up_seq(&mut self) {
for id in DeviceId::iter() {
let power_on_success = self.power_on(id);
match power_on_success{
Ok(_) => {info!("{:?} powered up successfully.", id)}
Err(_) => {info!("{:?} failed to power up.", id)}
}
}
CanDeviceHandler { device_state_map, can_tx_handler, can_rx_receiver }
}
pub fn power_on(&mut self, id: DeviceId) -> Result<(),()> {
if !self.device_state_map.contains_key(&id) {
return Err(());
}
info!("Powering on device {:?}", 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 {
let request_confirmation = self.can_rx_receiver.recv();
match request_confirmation {
Ok(confirmation) => {
if confirmation.package_id() != PackageId::DevicePowerOnRequestConfirmation {
warn!("Wrong package ID.");
return Err(());
}
if confirmation.data()[0] != id as u8 {
warn!("Wrong device ID.");
return Err(());
}
if confirmation.data()[1] != 1 {
warn!("Request unsuccessful.");
return Err(());
}
info!("Power on request confirmation received.");
}
Err(_) => {
warn!("Error receiving package.");
return Err(());
}
}
let request_success = self.can_rx_receiver.recv();
match request_success {
Ok(success) => {
if success.package_id() != PackageId::DevicePowerOnConfirmation {
warn!("Wrong package ID.");
return Err(());
}
if success.data()[0] != id as u8 {
warn!("Wrong device ID.");
return Err(());
}
if success.data()[1] != 1 {
warn!("Power on unsuccessful.");
return Err(());
}
info!("Power on confirmation received.");
}
Err(_) => {
warn!("Error receiving package.");
return Err(());
}
}
Ok(())
}
pub fn power_off(&mut self, id: DeviceId) -> Result<(),()> {
if !self.device_state_map.contains_key(&id) {
return Err(());
}
info!("Powering on device {:?}", id);
let msg_data: [u8; 1] = [id as u8];
self.can_tx_handler.tx_socket(PackageId::DevicePowerOffRequest, &msg_data);
let request_confirmation = self.can_rx_receiver.recv();
match request_confirmation {
Ok(confirmation) => {
if confirmation.package_id() != PackageId::DevicePowerOffRequestConfirmation {
warn!("Wrong package ID.");
return Err(());
}
if confirmation.data()[0] != id as u8 {
warn!("Wrong device ID.");
return Err(());
}
if confirmation.data()[1] != 1 {
warn!("Request unsuccessful.");
return Err(());
}
info!("Power off request confirmation received.");
}
Err(_) => {
warn!("Error receiving package.");
return Err(());
}
}
let request_success = self.can_rx_receiver.recv();
match request_success {
Ok(success) => {
if success.package_id() != PackageId::DevicePowerOffConfirmation {
warn!("Wrong package ID.");
return Err(());
}
if success.data()[0] != id as u8 {
warn!("Wrong device ID.");
return Err(());
}
if success.data()[1] != 1 {
warn!("Power off unsuccessful.");
return Err(());
}
info!("Power off confirmation received.");
}
Err(_) => {
warn!("Error receiving package.");
return Err(());
}
}
Ok(())
}
pub fn get_power_states(&mut self) -> HashMap<DeviceId, DeviceState> {
for id in DeviceId::iter() {
self.update_power_state(id).expect("Error updating power state.");
}
self.device_state_map.clone()
}
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)
@ -100,3 +191,17 @@ impl DeviceHandler {
}
}
pub fn power_up_sequence(mut device_handler: CanDeviceHandler) -> HashMap<DeviceId, DeviceState> {
for id in DeviceId::iter() {
device_handler.power_on(id).expect("Error powering on device.");
}
device_handler.get_power_states()
}
pub fn power_down_sequence(mut device_handler: CanDeviceHandler) -> HashMap<DeviceId, DeviceState> {
for id in DeviceId::iter() {
device_handler.power_off(id).expect("Error powering on device.");
}
device_handler.get_power_states()
}