use std::hash::Hash;
use std::sync::Arc;
use crate::core::marker::{Edge, NodeKind};
use crate::core::node::NodeApi;
use crate::core::utils::UniqueId;
use crate::error::Result;
use crate::prelude::*;
use crate::protocol::*;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct DeviceId {
mavlink_id: MavLinkId,
unique_id: UniqueId,
}
#[derive(Clone, Debug)]
pub struct Device<V: MaybeVersioned> {
id: DeviceId,
processor: Arc<FrameProcessor>,
endpoint: Endpoint<V>,
}
impl DeviceId {
pub fn new(mavlink_id: MavLinkId) -> Self {
Self {
mavlink_id,
unique_id: UniqueId::new(),
}
}
#[inline(always)]
pub fn mavlink_id(&self) -> MavLinkId {
self.mavlink_id
}
#[inline(always)]
pub fn system_id(&self) -> SystemId {
self.mavlink_id.system
}
#[inline(always)]
pub fn component_id(&self) -> ComponentId {
self.mavlink_id.component
}
}
impl Device<Versionless> {
pub fn new<K: NodeKind, V: MaybeVersioned, A: NodeApi<V>>(
mavlink_id: MavLinkId,
node: &Node<K, V, A>,
) -> Device<V> {
Device {
id: DeviceId::new(mavlink_id),
processor: node.processor.clone(),
endpoint: Endpoint::new(mavlink_id),
}
}
}
impl<V: MaybeVersioned> Device<V> {
#[inline(always)]
pub fn id(&self) -> DeviceId {
self.id
}
#[inline(always)]
pub fn endpoint(&self) -> &Endpoint<V> {
&self.endpoint
}
}
impl<V: Versioned> Device<V> {
pub fn next_frame(&self, message: &dyn Message) -> Result<Frame<V>> {
let mut frame = self.endpoint.next_frame(message)?;
self.processor.process_new(&mut frame);
Ok(frame)
}
}
impl Device<Versionless> {
pub fn next_frame_versioned<V: Versioned>(
&self,
message: &dyn Message,
) -> Result<Frame<Versionless>> {
let mut frame = self.endpoint.next_frame::<V>(message)?;
self.processor.process_new(&mut frame);
Ok(frame)
}
}
impl<V: MaybeVersioned, A: NodeApi<V>> From<&Node<Edge<V>, V, A>> for Device<V> {
fn from(value: &Node<Edge<V>, V, A>) -> Self {
Self {
id: value.kind.device_id,
processor: value.processor.clone(),
endpoint: value.kind.endpoint.clone(),
}
}
}