use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Formatter},
sync::Arc,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WireEnvelope<T> {
pub schema_version: u32,
pub event: EnvelopeEvent,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub envelope: Option<EnvelopeMeta>,
pub payload: WirePayload<T>,
}
impl<T> WireEnvelope<T> {
#[inline]
pub fn new(schema_version: u32, kind: EnvelopeKind, data: T) -> Self {
Self {
schema_version,
event: EnvelopeEvent { kind },
envelope: None,
payload: WirePayload { data },
}
}
#[inline]
pub fn v1(kind: EnvelopeKind, data: T) -> Self {
Self::new(1, kind, data)
}
#[inline]
pub fn with_meta(mut self, meta: EnvelopeMeta) -> Self {
self.envelope = Some(meta);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Copy)]
pub struct EnvelopeEvent {
pub kind: EnvelopeKind,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EnvelopeKind {
DeviceConnected,
DeviceDisconnected,
Telemetry,
Attributes,
Alarm,
RpcResponse,
WritePointResponse,
WritePoint,
CommandReceived,
RpcResponseReceived,
}
impl Display for EnvelopeKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl EnvelopeKind {
#[inline]
pub const fn as_str(&self) -> &'static str {
match self {
EnvelopeKind::DeviceConnected => "device_connected",
EnvelopeKind::DeviceDisconnected => "device_disconnected",
EnvelopeKind::Telemetry => "telemetry",
EnvelopeKind::Attributes => "attributes",
EnvelopeKind::Alarm => "alarm",
EnvelopeKind::RpcResponse => "rpc_response",
EnvelopeKind::WritePointResponse => "write_point_response",
EnvelopeKind::WritePoint => "write_point",
EnvelopeKind::CommandReceived => "command_received",
EnvelopeKind::RpcResponseReceived => "rpc_response_received",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WirePayload<T> {
pub data: T,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvelopeMeta {
pub ts_ms: i64,
pub app: EnvelopeApp,
pub device: EnvelopeDevice,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvelopeApp {
pub id: i32,
pub name: Arc<str>,
pub plugin_type: Arc<str>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvelopeDevice {
pub id: i32,
pub name: Arc<str>,
#[serde(default)]
pub r#type: Option<Arc<str>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvelopeChannel {
pub name: Arc<str>,
}