Skip to main content

varpulis_runtime/
event.rs

1//! Event types for the runtime
2//!
3//! The core `Event` type is defined in `varpulis-core` and re-exported here
4//! for backwards compatibility. Domain-specific event types (demo/HVAC) are
5//! defined locally.
6
7// Re-export everything from varpulis-core's event module
8pub use varpulis_core::event::*;
9
10use chrono::{DateTime, Utc};
11use serde::{Deserialize, Serialize};
12
13/// Temperature reading event for HVAC demo
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct TemperatureReading {
16    pub sensor_id: String,
17    pub zone: String,
18    pub value: f64,
19    #[serde(default = "Utc::now")]
20    pub timestamp: DateTime<Utc>,
21}
22
23impl From<TemperatureReading> for Event {
24    fn from(r: TemperatureReading) -> Self {
25        Self::new("TemperatureReading")
26            .with_timestamp(r.timestamp)
27            .with_field("sensor_id", r.sensor_id)
28            .with_field("zone", r.zone)
29            .with_field("value", r.value)
30    }
31}
32
33/// Humidity reading event for HVAC demo
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct HumidityReading {
36    pub sensor_id: String,
37    pub zone: String,
38    pub value: f64,
39    #[serde(default = "Utc::now")]
40    pub timestamp: DateTime<Utc>,
41}
42
43impl From<HumidityReading> for Event {
44    fn from(r: HumidityReading) -> Self {
45        Self::new("HumidityReading")
46            .with_timestamp(r.timestamp)
47            .with_field("sensor_id", r.sensor_id)
48            .with_field("zone", r.zone)
49            .with_field("value", r.value)
50    }
51}
52
53/// HVAC status event for demo
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct HVACStatus {
56    pub unit_id: String,
57    pub mode: String,
58    pub power_consumption: f64,
59    pub fan_speed: i64,
60    pub compressor_pressure: f64,
61    #[serde(default = "Utc::now")]
62    pub timestamp: DateTime<Utc>,
63}
64
65impl From<HVACStatus> for Event {
66    fn from(s: HVACStatus) -> Self {
67        Self::new("HVACStatus")
68            .with_timestamp(s.timestamp)
69            .with_field("unit_id", s.unit_id)
70            .with_field("mode", s.mode)
71            .with_field("power_consumption", s.power_consumption)
72            .with_field("fan_speed", s.fan_speed)
73            .with_field("compressor_pressure", s.compressor_pressure)
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_temperature_reading_to_event() {
83        let ts = Utc::now();
84        let reading = TemperatureReading {
85            sensor_id: "sensor1".to_string(),
86            zone: "zone_a".to_string(),
87            value: 22.5,
88            timestamp: ts,
89        };
90
91        let event: Event = reading.into();
92        assert_eq!(&*event.event_type, "TemperatureReading");
93        assert_eq!(event.get_str("sensor_id"), Some("sensor1"));
94        assert_eq!(event.get_str("zone"), Some("zone_a"));
95        assert_eq!(event.get_float("value"), Some(22.5));
96        assert_eq!(event.timestamp, ts);
97    }
98
99    #[test]
100    fn test_humidity_reading_to_event() {
101        let ts = Utc::now();
102        let reading = HumidityReading {
103            sensor_id: "humid1".to_string(),
104            zone: "zone_b".to_string(),
105            value: 65.0,
106            timestamp: ts,
107        };
108
109        let event: Event = reading.into();
110        assert_eq!(&*event.event_type, "HumidityReading");
111        assert_eq!(event.get_str("sensor_id"), Some("humid1"));
112        assert_eq!(event.get_float("value"), Some(65.0));
113    }
114
115    #[test]
116    fn test_hvac_status_to_event() {
117        let ts = Utc::now();
118        let status = HVACStatus {
119            unit_id: "hvac1".to_string(),
120            mode: "cooling".to_string(),
121            power_consumption: 1500.0,
122            fan_speed: 3,
123            compressor_pressure: 2.5,
124            timestamp: ts,
125        };
126
127        let event: Event = status.into();
128        assert_eq!(&*event.event_type, "HVACStatus");
129        assert_eq!(event.get_str("unit_id"), Some("hvac1"));
130        assert_eq!(event.get_str("mode"), Some("cooling"));
131        assert_eq!(event.get_float("power_consumption"), Some(1500.0));
132        assert_eq!(event.get_int("fan_speed"), Some(3));
133        assert_eq!(event.get_float("compressor_pressure"), Some(2.5));
134    }
135}