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