Skip to main content

industrial_protocols_kernel/sdk/
types.rs

1// Copyright (c) 2026 erik <erik@erik.xyz> — https://erik.xyz
2//! # SDK 类型定义
3//!
4//! 定义协议 SDK 中使用的公共类型:
5//! - 标记 trait([`AddressLike`]、[`ValueLike`])
6//! - 枚举类型([`ProtocolCategory`]、[`HealthState`])
7//! - 数据结构([`HealthStatus`]、[`DiagnosticsReport`]、[`DiscoveredDevice`])
8
9use std::fmt::Debug;
10
11pub trait AddressLike: Clone + Debug + Send + Sync {}
12impl<T: Clone + Debug + Send + Sync> AddressLike for T {}
13
14pub trait ValueLike: Clone + Debug + Send + Sync {}
15impl<T: Clone + Debug + Send + Sync> ValueLike for T {}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18#[non_exhaustive]
19pub enum ProtocolCategory {
20    IndustrialEthernet,
21    Fieldbus,
22    IoTMessaging,
23    AutomotiveBus,
24    BuildingLighting,
25    HardwareBridge,
26    SystemBus,
27}
28
29#[derive(Debug, Clone)]
30pub struct DiscoveredDevice {
31    pub device_id: String,
32    pub protocol: String,
33    pub address: String,
34    pub metadata: std::collections::HashMap<String, String>,
35}
36
37#[derive(Debug, Clone)]
38pub struct HealthStatus {
39    pub state: HealthState,
40    pub latency_ms: u64,
41    pub last_seen: std::time::Instant,
42    pub details: String,
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46#[non_exhaustive]
47pub enum HealthState {
48    Healthy,
49    Degraded,
50    Unhealthy,
51    Unknown,
52}
53
54#[derive(Debug, Clone)]
55pub struct DiagnosticsReport {
56    pub device_id: String,
57    pub uptime_secs: Option<u64>,
58    pub error_count: u64,
59    pub last_error: Option<String>,
60    pub custom_fields: std::collections::HashMap<String, String>,
61}
62
63#[derive(Debug, Clone)]
64pub struct SubscriptionUpdate<Id = String> {
65    pub subscription_id: Id,
66    pub address: String,
67    pub value: String,
68    pub timestamp: std::time::Instant,
69}