use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use super::common::{Bandwidth, DataSource, EntityOrigin};
use super::entity_id::{EntityId, MacAddress};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DeviceType {
Gateway,
Switch,
AccessPoint,
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DeviceState {
Online,
Offline,
PendingAdoption,
Updating,
GettingReady,
Adopting,
Deleting,
ConnectionInterrupted,
Isolated,
Unknown,
}
impl DeviceState {
pub fn is_online(&self) -> bool {
matches!(self, Self::Online)
}
pub fn is_transitional(&self) -> bool {
matches!(
self,
Self::Updating | Self::GettingReady | Self::Adopting | Self::PendingAdoption
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Port {
pub index: u32,
pub name: Option<String>,
pub state: PortState,
pub speed_mbps: Option<u32>,
pub max_speed_mbps: Option<u32>,
pub connector: Option<PortConnector>,
pub poe: Option<PoeInfo>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PortState {
Up,
Down,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PortConnector {
Rj45,
Sfp,
SfpPlus,
Sfp28,
Qsfp28,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoeInfo {
pub standard: Option<String>,
pub enabled: bool,
pub state: PortState,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PortMode {
Access,
Trunk,
Mirror,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum StpState {
Disabled,
Blocking,
Listening,
Learning,
Forwarding,
Broken,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PoeMode {
Auto,
Off,
Passive24V,
Passthrough,
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PortSpeedSetting {
Auto,
Mbps10,
Mbps100,
Mbps1000,
Mbps2500,
Mbps5000,
Mbps10000,
}
impl PortSpeedSetting {
pub fn as_mbps(self) -> Option<u32> {
match self {
Self::Auto => None,
Self::Mbps10 => Some(10),
Self::Mbps100 => Some(100),
Self::Mbps1000 => Some(1000),
Self::Mbps2500 => Some(2500),
Self::Mbps5000 => Some(5000),
Self::Mbps10000 => Some(10000),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortProfile {
pub index: u32,
pub name: Option<String>,
pub link_state: PortState,
pub mode: PortMode,
pub native_network_id: Option<String>,
pub native_vlan_id: Option<u16>,
pub native_network_name: Option<String>,
pub tagged_network_ids: Vec<String>,
pub tagged_vlan_ids: Vec<u16>,
pub tagged_network_names: Vec<String>,
pub tagged_all: bool,
pub poe_mode: Option<PoeMode>,
pub speed_setting: Option<PortSpeedSetting>,
pub link_speed_mbps: Option<u32>,
pub stp_state: StpState,
pub port_profile_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Radio {
pub frequency_ghz: f32,
pub channel: Option<u32>,
pub channel_width_mhz: Option<u32>,
pub wlan_standard: Option<String>,
pub tx_retries_pct: Option<f64>,
pub channel_utilization_pct: Option<f64>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeviceStats {
pub uptime_secs: Option<u64>,
pub cpu_utilization_pct: Option<f64>,
pub memory_utilization_pct: Option<f64>,
pub load_average_1m: Option<f64>,
pub load_average_5m: Option<f64>,
pub load_average_15m: Option<f64>,
pub uplink_bandwidth: Option<Bandwidth>,
pub last_heartbeat: Option<DateTime<Utc>>,
pub next_heartbeat: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::struct_excessive_bools)]
pub struct Device {
pub id: EntityId,
pub mac: MacAddress,
pub ip: Option<IpAddr>,
pub wan_ipv6: Option<String>,
pub name: Option<String>,
pub model: Option<String>,
pub device_type: DeviceType,
pub state: DeviceState,
pub firmware_version: Option<String>,
pub firmware_updatable: bool,
pub adopted_at: Option<DateTime<Utc>>,
pub provisioned_at: Option<DateTime<Utc>>,
pub last_seen: Option<DateTime<Utc>>,
pub serial: Option<String>,
pub supported: bool,
pub ports: Vec<Port>,
pub radios: Vec<Radio>,
pub uplink_device_id: Option<EntityId>,
pub uplink_device_mac: Option<MacAddress>,
pub uplink_port_idx: Option<u32>,
pub has_switching: bool,
pub has_access_point: bool,
pub stats: DeviceStats,
pub client_count: Option<u32>,
pub origin: Option<EntityOrigin>,
#[serde(skip)]
#[allow(dead_code)]
pub(crate) source: DataSource,
#[serde(skip)]
#[allow(dead_code)]
pub(crate) updated_at: DateTime<Utc>,
}