plc_control/src/model.rs

121 lines
3.2 KiB
Rust

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<ScanMode> 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<Self, Self::Err> {
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<String>,
pub security_mode: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
pub enabled: bool,
#[serde(serialize_with = "utc_to_local_str")]
pub created_at: DateTime<Utc>,
#[serde(serialize_with = "utc_to_local_str")]
pub updated_at: DateTime<Utc>,
}
#[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<String>,
pub namespace_index: Option<i32>,
pub identifier_type: Option<String>, // i/s/g/b
pub identifier: Option<String>,
pub browse_name: String,
pub display_name: Option<String>,
pub node_class: String, // Object/Variable/Method coil/input topic
pub parent_id: Option<Uuid>,
#[serde(serialize_with = "utc_to_local_str")]
pub created_at: DateTime<Utc>,
#[serde(serialize_with = "utc_to_local_str")]
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Serialize, Deserialize, FromRow)]
#[allow(dead_code)]
pub struct Point {
pub id: Uuid,
pub node_id: Uuid,
pub name: String,
pub description: Option<String>,
pub unit: Option<String>,
pub scan_interval_s: i32, // s
pub tag_id: Option<Uuid>,
#[serde(serialize_with = "utc_to_local_str")]
pub created_at: DateTime<Utc>,
#[serde(serialize_with = "utc_to_local_str")]
pub updated_at: DateTime<Utc>,
}
#[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<String>,
#[serde(serialize_with = "utc_to_local_str")]
pub created_at: DateTime<Utc>,
#[serde(serialize_with = "utc_to_local_str")]
pub updated_at: DateTime<Utc>,
}