use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use sqlx::FromRow; use uuid::Uuid; use crate::util::datetime::utc_to_local_str; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "lowercase")] pub enum ScanMode { Poll, Subscribe, } impl ScanMode { pub fn as_str(&self) -> &'static str { match self { ScanMode::Poll => "poll", ScanMode::Subscribe => "subscribe", } } } impl From for String { fn from(mode: ScanMode) -> Self { mode.as_str().to_string() } } impl std::fmt::Display for ScanMode { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.as_str()) } } impl std::str::FromStr for ScanMode { type Err = String; fn from_str(s: &str) -> Result { match s.to_lowercase().as_str() { "poll" => Ok(ScanMode::Poll), "subscribe" => Ok(ScanMode::Subscribe), _ => Err(format!("Invalid scan mode: {}", s)), } } } #[derive(Debug, Serialize, Deserialize, FromRow, Clone)] pub struct Source { pub id: Uuid, pub name: String, pub protocol: String, // opcua, modbus pub endpoint: String, pub security_policy: Option, pub security_mode: Option, pub username: Option, pub password: Option, pub enabled: bool, #[serde(serialize_with = "utc_to_local_str")] pub created_at: DateTime, #[serde(serialize_with = "utc_to_local_str")] pub updated_at: DateTime, } #[derive(Debug, Serialize, Deserialize, FromRow)] #[allow(dead_code)] pub struct Node { pub id: Uuid, pub source_id: Uuid, pub external_id: String, // ns=2;s=Temperature // comment fixed pub namespace_uri: Option, pub namespace_index: Option, pub identifier_type: Option, // i/s/g/b pub identifier: Option, pub browse_name: String, pub display_name: Option, pub node_class: String, // Object/Variable/Method coil/input topic pub parent_id: Option, #[serde(serialize_with = "utc_to_local_str")] pub created_at: DateTime, #[serde(serialize_with = "utc_to_local_str")] pub updated_at: DateTime, } #[derive(Debug, Serialize, Deserialize, FromRow)] #[allow(dead_code)] pub struct Point { pub id: Uuid, pub node_id: Uuid, pub name: String, pub description: Option, pub unit: Option, pub scan_interval_s: i32, // s pub tag_id: Option, #[serde(serialize_with = "utc_to_local_str")] pub created_at: DateTime, #[serde(serialize_with = "utc_to_local_str")] pub updated_at: DateTime, } #[derive(Debug, Clone)] pub struct PointSubscriptionInfo { pub point_id: Uuid, pub external_id: String, pub scan_interval_s: i32, } #[derive(Debug, Serialize, Deserialize, FromRow, Clone)] pub struct Tag { pub id: Uuid, pub name: String, pub description: Option, #[serde(serialize_with = "utc_to_local_str")] pub created_at: DateTime, #[serde(serialize_with = "utc_to_local_str")] pub updated_at: DateTime, }