1use serde::{Deserialize, Serialize};
4use time::OffsetDateTime;
5
6use aranet_types::{CurrentReading, DeviceType, HistoryRecord, Status};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct StoredDevice {
11 pub id: String,
13 pub name: Option<String>,
15 pub device_type: Option<DeviceType>,
17 pub serial: Option<String>,
19 pub firmware: Option<String>,
21 pub hardware: Option<String>,
23 #[serde(with = "time::serde::rfc3339")]
25 pub first_seen: OffsetDateTime,
26 #[serde(with = "time::serde::rfc3339")]
28 pub last_seen: OffsetDateTime,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct StoredReading {
34 pub id: i64,
36 pub device_id: String,
38 #[serde(with = "time::serde::rfc3339")]
40 pub captured_at: OffsetDateTime,
41 pub co2: u16,
43 pub temperature: f32,
45 pub pressure: f32,
47 pub humidity: u8,
49 pub battery: u8,
51 pub status: Status,
53 pub radon: Option<u32>,
55 pub radiation_rate: Option<f32>,
57 pub radiation_total: Option<f64>,
59}
60
61impl StoredReading {
62 pub fn from_reading(device_id: &str, reading: &CurrentReading) -> Self {
64 Self {
65 id: 0, device_id: device_id.to_string(),
67 captured_at: reading.captured_at.unwrap_or_else(OffsetDateTime::now_utc),
68 co2: reading.co2,
69 temperature: reading.temperature,
70 pressure: reading.pressure,
71 humidity: reading.humidity,
72 battery: reading.battery,
73 status: reading.status,
74 radon: reading.radon,
75 radiation_rate: reading.radiation_rate,
76 radiation_total: reading.radiation_total,
77 }
78 }
79
80 pub fn to_reading(&self) -> CurrentReading {
82 CurrentReading {
83 co2: self.co2,
84 temperature: self.temperature,
85 pressure: self.pressure,
86 humidity: self.humidity,
87 battery: self.battery,
88 status: self.status,
89 interval: 0,
90 age: 0,
91 captured_at: Some(self.captured_at),
92 radon: self.radon,
93 radiation_rate: self.radiation_rate,
94 radiation_total: self.radiation_total,
95 radon_avg_24h: None,
96 radon_avg_7d: None,
97 radon_avg_30d: None,
98 }
99 }
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct StoredHistoryRecord {
105 pub id: i64,
107 pub device_id: String,
109 #[serde(with = "time::serde::rfc3339")]
111 pub timestamp: OffsetDateTime,
112 #[serde(with = "time::serde::rfc3339")]
114 pub synced_at: OffsetDateTime,
115 pub co2: u16,
117 pub temperature: f32,
119 pub pressure: f32,
121 pub humidity: u8,
123 pub radon: Option<u32>,
125 pub radiation_rate: Option<f32>,
127 pub radiation_total: Option<f64>,
129}
130
131impl StoredHistoryRecord {
132 pub fn from_history(device_id: &str, record: &HistoryRecord) -> Self {
134 Self {
135 id: 0,
136 device_id: device_id.to_string(),
137 timestamp: record.timestamp,
138 synced_at: OffsetDateTime::now_utc(),
139 co2: record.co2,
140 temperature: record.temperature,
141 pressure: record.pressure,
142 humidity: record.humidity,
143 radon: record.radon,
144 radiation_rate: record.radiation_rate,
145 radiation_total: record.radiation_total,
146 }
147 }
148
149 pub fn to_history(&self) -> HistoryRecord {
151 HistoryRecord {
152 timestamp: self.timestamp,
153 co2: self.co2,
154 temperature: self.temperature,
155 pressure: self.pressure,
156 humidity: self.humidity,
157 radon: self.radon,
158 radiation_rate: self.radiation_rate,
159 radiation_total: self.radiation_total,
160 }
161 }
162}
163
164#[derive(Debug, Clone, Serialize, Deserialize)]
166pub struct SyncState {
167 pub device_id: String,
169 pub last_history_index: Option<u16>,
171 pub total_readings: Option<u16>,
173 #[serde(with = "time::serde::rfc3339::option")]
175 pub last_sync_at: Option<OffsetDateTime>,
176}