-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathbld_energy_audit.rs
More file actions
390 lines (336 loc) · 11.8 KB
/
bld_energy_audit.rs
File metadata and controls
390 lines (336 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Energy audit — ADR-041 Category 3: Smart Building.
//!
//! Builds hourly occupancy histograms (24 bins/day, 7 days) for energy
//! optimization scheduling:
//! - Identifies consistently unoccupied hours for HVAC/lighting shutoff
//! - Detects after-hours occupancy anomalies
//! - Emits periodic schedule summaries
//!
//! Designed for the `on_timer`-style periodic emission pattern (every N frames).
//!
//! Host API used: `csi_get_presence()`, `csi_get_n_persons()`
/// Hours in a day.
const HOURS_PER_DAY: usize = 24;
/// Days in a week.
const DAYS_PER_WEEK: usize = 7;
/// Frames per hour at 20 Hz.
const FRAMES_PER_HOUR: u32 = 72000;
/// Summary emission interval (every 1200 frames = 1 minute at 20 Hz).
const SUMMARY_INTERVAL: u32 = 1200;
/// After-hours definition: hours 22-06 (10 PM to 6 AM).
const AFTER_HOURS_START: u8 = 22;
const AFTER_HOURS_END: u8 = 6;
/// Minimum occupancy fraction to consider an hour "used" in scheduling.
const USED_THRESHOLD: f32 = 0.1;
/// Frames of presence during after-hours before alert.
const AFTER_HOURS_ALERT_FRAMES: u32 = 600; // 30 seconds.
// ── Event IDs (350-352: Energy Audit) ───────────────────────────────────────
pub const EVENT_SCHEDULE_SUMMARY: i32 = 350;
pub const EVENT_AFTER_HOURS_ALERT: i32 = 351;
pub const EVENT_UTILIZATION_RATE: i32 = 352;
/// Per-hour occupancy accumulator.
#[derive(Clone, Copy)]
struct HourBin {
/// Total frames observed in this hour slot.
total_frames: u32,
/// Frames with presence detected.
occupied_frames: u32,
/// Sum of person counts (for average headcount).
person_sum: u32,
}
impl HourBin {
const fn new() -> Self {
Self {
total_frames: 0,
occupied_frames: 0,
person_sum: 0,
}
}
/// Occupancy rate for this hour (0.0-1.0).
fn occupancy_rate(&self) -> f32 {
if self.total_frames == 0 {
return 0.0;
}
self.occupied_frames as f32 / self.total_frames as f32
}
/// Average headcount during occupied frames.
fn avg_headcount(&self) -> f32 {
if self.occupied_frames == 0 {
return 0.0;
}
self.person_sum as f32 / self.occupied_frames as f32
}
}
/// Energy audit analyzer.
pub struct EnergyAuditor {
/// Weekly histogram: [day][hour].
histogram: [[HourBin; HOURS_PER_DAY]; DAYS_PER_WEEK],
/// Current simulated hour (0-23). In production, derived from host timestamp.
current_hour: u8,
/// Current simulated day (0-6).
current_day: u8,
/// Frames within the current hour.
hour_frames: u32,
/// Consecutive after-hours presence frames.
after_hours_presence: u32,
/// Total frames processed.
frame_count: u32,
/// Total occupied frames (for overall utilization).
total_occupied_frames: u32,
}
impl EnergyAuditor {
pub const fn new() -> Self {
const BIN_INIT: HourBin = HourBin::new();
const DAY_INIT: [HourBin; HOURS_PER_DAY] = [BIN_INIT; HOURS_PER_DAY];
Self {
histogram: [DAY_INIT; DAYS_PER_WEEK],
current_hour: 8, // Default start: 8 AM.
current_day: 0, // Monday.
hour_frames: 0,
after_hours_presence: 0,
frame_count: 0,
total_occupied_frames: 0,
}
}
/// Set the current time (called from host or on_init).
pub fn set_time(&mut self, day: u8, hour: u8) {
self.current_day = day % DAYS_PER_WEEK as u8;
self.current_hour = hour % HOURS_PER_DAY as u8;
self.hour_frames = 0;
}
/// Process one frame.
///
/// `presence`: 1 if occupied, 0 if vacant.
/// `n_persons`: person count from host.
///
/// Returns events as `(event_type, value)` pairs.
pub fn process_frame(
&mut self,
presence: i32,
n_persons: i32,
) -> &[(i32, f32)] {
self.frame_count += 1;
self.hour_frames += 1;
let is_present = presence > 0;
let persons = if n_persons > 0 { n_persons as u32 } else { 0 };
// Update histogram bin.
let d = self.current_day as usize;
let h = self.current_hour as usize;
self.histogram[d][h].total_frames += 1;
if is_present {
self.histogram[d][h].occupied_frames += 1;
self.histogram[d][h].person_sum += persons;
self.total_occupied_frames += 1;
}
// Hour rollover.
if self.hour_frames >= FRAMES_PER_HOUR {
self.hour_frames = 0;
self.current_hour += 1;
if self.current_hour >= HOURS_PER_DAY as u8 {
self.current_hour = 0;
self.current_day = (self.current_day + 1) % DAYS_PER_WEEK as u8;
}
}
// After-hours detection.
let is_after_hours = self.is_after_hours(self.current_hour);
if is_present && is_after_hours {
self.after_hours_presence += 1;
} else {
self.after_hours_presence = 0;
}
// Build events.
static mut EVENTS: [(i32, f32); 3] = [(0, 0.0); 3];
let mut n_events = 0usize;
// After-hours alert.
if self.after_hours_presence >= AFTER_HOURS_ALERT_FRAMES && n_events < 3 {
unsafe {
EVENTS[n_events] = (EVENT_AFTER_HOURS_ALERT, self.current_hour as f32);
}
n_events += 1;
}
// Periodic summary.
if self.frame_count % SUMMARY_INTERVAL == 0 {
// Emit current hour's occupancy rate.
let rate = self.histogram[d][h].occupancy_rate();
if n_events < 3 {
unsafe {
EVENTS[n_events] = (EVENT_SCHEDULE_SUMMARY, rate);
}
n_events += 1;
}
// Emit overall utilization rate.
if n_events < 3 {
let util = self.utilization_rate();
unsafe {
EVENTS[n_events] = (EVENT_UTILIZATION_RATE, util);
}
n_events += 1;
}
}
unsafe { &EVENTS[..n_events] }
}
/// Check if a given hour is after-hours.
fn is_after_hours(&self, hour: u8) -> bool {
if AFTER_HOURS_START > AFTER_HOURS_END {
// Wraps midnight (e.g., 22-06).
hour >= AFTER_HOURS_START || hour < AFTER_HOURS_END
} else {
hour >= AFTER_HOURS_START && hour < AFTER_HOURS_END
}
}
/// Get overall utilization rate.
pub fn utilization_rate(&self) -> f32 {
if self.frame_count == 0 {
return 0.0;
}
self.total_occupied_frames as f32 / self.frame_count as f32
}
/// Get occupancy rate for a specific day and hour.
pub fn hourly_rate(&self, day: usize, hour: usize) -> f32 {
if day < DAYS_PER_WEEK && hour < HOURS_PER_DAY {
self.histogram[day][hour].occupancy_rate()
} else {
0.0
}
}
/// Get average headcount for a specific day and hour.
pub fn hourly_headcount(&self, day: usize, hour: usize) -> f32 {
if day < DAYS_PER_WEEK && hour < HOURS_PER_DAY {
self.histogram[day][hour].avg_headcount()
} else {
0.0
}
}
/// Find the number of consistently unoccupied hours per day.
/// An hour is "unoccupied" if its occupancy rate is below USED_THRESHOLD.
pub fn unoccupied_hours(&self, day: usize) -> u8 {
if day >= DAYS_PER_WEEK {
return 0;
}
let mut count = 0u8;
for h in 0..HOURS_PER_DAY {
if self.histogram[day][h].occupancy_rate() < USED_THRESHOLD {
count += 1;
}
}
count
}
/// Get current simulated time.
pub fn current_time(&self) -> (u8, u8) {
(self.current_day, self.current_hour)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_energy_audit_init() {
let ea = EnergyAuditor::new();
assert!((ea.utilization_rate() - 0.0).abs() < 0.001);
assert_eq!(ea.current_time(), (0, 8));
}
#[test]
fn test_occupancy_recording() {
let mut ea = EnergyAuditor::new();
ea.set_time(0, 9); // Monday 9 AM.
// Feed 100 frames with presence.
for _ in 0..100 {
ea.process_frame(1, 3);
}
let rate = ea.hourly_rate(0, 9);
assert!((rate - 1.0).abs() < 0.01, "fully occupied hour should be ~1.0");
let headcount = ea.hourly_headcount(0, 9);
assert!((headcount - 3.0).abs() < 0.01, "average headcount should be ~3.0");
}
#[test]
fn test_partial_occupancy() {
let mut ea = EnergyAuditor::new();
ea.set_time(1, 14); // Tuesday 2 PM.
// 50 frames occupied, 50 vacant.
for _ in 0..50 {
ea.process_frame(1, 2);
}
for _ in 0..50 {
ea.process_frame(0, 0);
}
let rate = ea.hourly_rate(1, 14);
assert!((rate - 0.5).abs() < 0.01, "half-occupied hour should be ~0.5");
}
#[test]
fn test_after_hours_alert() {
let mut ea = EnergyAuditor::new();
ea.set_time(2, 23); // Wednesday 11 PM (after hours).
let mut found_alert = false;
for _ in 0..(AFTER_HOURS_ALERT_FRAMES + 10) {
let events = ea.process_frame(1, 1);
for &(et, _) in events {
if et == EVENT_AFTER_HOURS_ALERT {
found_alert = true;
}
}
}
assert!(found_alert, "should emit AFTER_HOURS_ALERT for sustained after-hours presence");
}
#[test]
fn test_no_after_hours_alert_during_business() {
let mut ea = EnergyAuditor::new();
ea.set_time(0, 10); // Monday 10 AM (business hours).
let mut found_alert = false;
for _ in 0..2000 {
let events = ea.process_frame(1, 5);
for &(et, _) in events {
if et == EVENT_AFTER_HOURS_ALERT {
found_alert = true;
}
}
}
assert!(!found_alert, "should NOT emit AFTER_HOURS_ALERT during business hours");
}
#[test]
fn test_unoccupied_hours() {
let mut ea = EnergyAuditor::new();
ea.set_time(3, 0); // Thursday midnight.
// Only hour 0 gets data; hours 1-23 have no data and should count as unoccupied.
for _ in 0..10 {
ea.process_frame(0, 0);
}
// Hour 0 has data but 0% occupancy => all 24 hours unoccupied.
let unoccupied = ea.unoccupied_hours(3);
assert_eq!(unoccupied, 24, "all hours with no/low occupancy should be unoccupied");
}
#[test]
fn test_periodic_summary_emission() {
let mut ea = EnergyAuditor::new();
ea.set_time(0, 9);
let mut found_summary = false;
let mut found_utilization = false;
for _ in 0..(SUMMARY_INTERVAL + 1) {
let events = ea.process_frame(1, 2);
for &(et, _) in events {
if et == EVENT_SCHEDULE_SUMMARY {
found_summary = true;
}
if et == EVENT_UTILIZATION_RATE {
found_utilization = true;
}
}
}
assert!(found_summary, "should emit SCHEDULE_SUMMARY periodically");
assert!(found_utilization, "should emit UTILIZATION_RATE periodically");
}
#[test]
fn test_utilization_rate() {
let mut ea = EnergyAuditor::new();
ea.set_time(0, 9);
// 100 frames occupied.
for _ in 0..100 {
ea.process_frame(1, 2);
}
// 100 frames vacant.
for _ in 0..100 {
ea.process_frame(0, 0);
}
let rate = ea.utilization_rate();
assert!((rate - 0.5).abs() < 0.01, "50/50 occupancy should give ~0.5 utilization");
}
}