metrology_insight 0.1.0

Embedded-first electrical metrology DSP library in Rust. Pre-compliance implementation of measurement algorithms derived from IEC 61000-4-30:2021 Class S and IEC 62053-21, fully no_std + alloc compatible.
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright © 2026 Francisco Arcos.
// SPDX-License-Identifier: Apache-2.0

//! Alarm Detector library — Rust port of the original C detector library.
//!
//! Monitors any metrological value (RMS, THD, frequency, power, energy…) and
//! drives a stateful ON/OFF alarm with configurable threshold, hysteresis and
//! debounce.

use crate::types::{MetrologyInsightSocket, PhaseData};

// ─── Operation ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operation {
    Value,
    Abs,
    Gradient,
    AbsGradient,
}

impl Operation {
    /// Maps an operation name string to the corresponding `Operation`.
    ///
    /// # Arguments
    ///
    /// * `s` - String such as "absolute", "gradient" or "absGrad".
    ///
    /// # Returns
    ///
    /// The matching `Operation`, or `Operation::Value` for unknown strings.
    pub fn parse_str(s: &str) -> Self {
        match s {
            "absolute" => Operation::Abs,
            "gradient" => Operation::Gradient,
            "absGrad" => Operation::AbsGradient,
            _ => Operation::Value,
        }
    }
}

// ─── Group / Element ───────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Group {
    Voltage,
    Current,
    Power,
    ActiveEnergy,
    ReactiveEnergy,
}

impl Group {
    /// Maps a group name string to the corresponding `Group`.
    ///
    /// # Arguments
    ///
    /// * `s` - String such as "voltage", "current", "power" or "a_energy".
    ///
    /// # Returns
    ///
    /// The matching `Group`, or `None` if the string is unknown.
    pub fn parse_str(s: &str) -> Option<Self> {
        match s {
            "voltage" => Some(Group::Voltage),
            "current" => Some(Group::Current),
            "power" => Some(Group::Power),
            "a_energy" => Some(Group::ActiveEnergy),
            "r_energy" => Some(Group::ReactiveEnergy),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Element {
    Rms,
    UrmsHalfCycle,
    Frequency,
    Thd,
    Instant,
    Phi,
    Active,
    Reactive,
    Apparent,
    Imported,
    Exported,
    Inductive,
    Capacitive,
}

impl Element {
    /// Maps an element name string to the corresponding `Element`.
    ///
    /// # Arguments
    ///
    /// * `s` - String such as "TRMS", "Frequency" or "THD".
    ///
    /// # Returns
    ///
    /// The matching `Element`, or `None` if the string is unknown.
    pub fn parse_str(s: &str) -> Option<Self> {
        match s {
            "TRMS" => Some(Element::Rms),
            "UrmsHalfCycle" => Some(Element::UrmsHalfCycle),
            "Frequency" => Some(Element::Frequency),
            "THD" => Some(Element::Thd),
            "instant" => Some(Element::Instant),
            "Phi" => Some(Element::Phi),
            "Active" => Some(Element::Active),
            "Reactive" => Some(Element::Reactive),
            "Apparent" => Some(Element::Apparent),
            "Imported" => Some(Element::Imported),
            "Exported" => Some(Element::Exported),
            "Inductive" => Some(Element::Inductive),
            "Capacitive" => Some(Element::Capacitive),
            _ => None,
        }
    }
}

// ─── Condition ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Condition {
    Gt,
    GtEq,
    Lt,
    LtEq,
    Eq,
    NotEq,
}

impl Condition {
    /// Checks whether `value` satisfies this condition against `threshold`.
    ///
    /// # Arguments
    ///
    /// * `value` - The value to evaluate.
    /// * `threshold` - The threshold to compare against.
    ///
    /// # Returns
    ///
    /// `true` if the comparison holds.
    fn check(self, value: f32, threshold: f32) -> bool {
        match self {
            Condition::Gt => value > threshold,
            Condition::GtEq => value >= threshold,
            Condition::Lt => value < threshold,
            Condition::LtEq => value <= threshold,
            Condition::Eq => (value - threshold).abs() < f32::EPSILON,
            Condition::NotEq => (value - threshold).abs() >= f32::EPSILON,
        }
    }

    /// Maps a condition name string to the corresponding `Condition`.
    ///
    /// # Arguments
    ///
    /// * `s` - String such as "gt", "lt_eq" or "equal".
    ///
    /// # Returns
    ///
    /// The matching `Condition`, or `None` if the string is unknown.
    pub fn parse_str(s: &str) -> Option<Self> {
        match s {
            "gt" => Some(Condition::Gt),
            "gt_eq" => Some(Condition::GtEq),
            "lt" => Some(Condition::Lt),
            "lt_eq" => Some(Condition::LtEq),
            "equal" => Some(Condition::Eq),
            "not_equal" => Some(Condition::NotEq),
            _ => None,
        }
    }
}

// ─── Status ────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Status {
    #[default]
    Off,
    On,
}

// ─── Detector ──────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct Detector {
    pub condition: Condition,
    pub status: Status,

    pub th: f32,
    pub hyst: f32,

    pub threshold_on: f32,
    pub threshold_off: f32,

    pub debounce_on: u16,
    pub debounce_off: u16,
    debounce: u16,

    prev_raw: f32,
}

impl Detector {
    /// Creates a new `Detector`, interpreting `hyst_abs` as the absolute value of the
    /// hysteresis (e.g. 2.0 V or 0.1 Hz passed directly from the JSON).
    ///
    /// # Arguments
    ///
    /// * `condition` - Comparison condition for the alarm.
    /// * `th` - Alarm threshold.
    /// * `hyst_abs` - Absolute hysteresis band used to derive the OFF threshold.
    /// * `debounce` - Debounce count applied in both directions.
    ///
    /// # Returns
    ///
    /// A `Detector` initialized in the `Off` state.
    pub fn new(condition: Condition, th: f32, hyst_abs: f32, debounce: u16) -> Self {
        let h = hyst_abs;

        // For Gt: turns on when exceeding `th`, turns off when dropping below `th - h`
        // For Lt: turns on when dropping below `th`, turns off when rising above `th + h`
        let (threshold_on, threshold_off) = match condition {
            Condition::Gt | Condition::GtEq => (th, th - h),
            Condition::Lt | Condition::LtEq => (th, th + h),
            Condition::Eq | Condition::NotEq => (th + h, th - h),
        };

        Self {
            condition,
            status: Status::Off,
            th,
            hyst: hyst_abs,
            threshold_on,
            threshold_off,
            debounce_on: debounce,
            debounce_off: debounce,
            debounce,
            prev_raw: 0.0,
        }
    }

    /// Processes a raw value using the `Value` operation and optionally updates the status.
    ///
    /// # Arguments
    ///
    /// * `raw_value` - Raw metrological value to evaluate.
    /// * `update_status` - Whether the internal status may change to `On`.
    ///
    /// # Returns
    ///
    /// A tuple of whether a transition occurred and the current `Status`.
    pub fn process(&mut self, raw_value: f32, update_status: bool) -> (bool, Status) {
        self.process_with_op(raw_value, Operation::Value, update_status)
    }

    /// Processes a raw value with the given operation and optionally updates the status.
    ///
    /// # Arguments
    ///
    /// * `raw_value` - Raw metrological value to evaluate.
    /// * `op` - Operation applied to the raw value before comparing.
    /// * `update_status` - Whether the internal status may change to `On`.
    ///
    /// # Returns
    ///
    /// A tuple of whether a transition occurred and the current `Status`.
    pub fn process_with_op(
        &mut self,
        raw_value: f32,
        op: Operation,
        update_status: bool,
    ) -> (bool, Status) {
        let value = match op {
            Operation::Value => raw_value,
            Operation::Abs => raw_value.abs(),
            Operation::Gradient => raw_value - self.prev_raw,
            Operation::AbsGradient => (raw_value - self.prev_raw).abs(),
        };
        self.prev_raw = raw_value;

        let mut transition = false;

        match self.status {
            Status::On => {
                // To turn the alarm off, the recovery (normality) condition is evaluated:
                // - Overvoltage (Gt): turns off when the value drops back to or below the OFF threshold.
                // - Undervoltage (Lt): turns off when the value rises back to or above the OFF threshold.
                let is_normal = match self.condition {
                    Condition::Gt | Condition::GtEq => value <= self.threshold_off,
                    Condition::Lt | Condition::LtEq => value >= self.threshold_off,
                    Condition::Eq => (value - self.th).abs() > self.hyst,
                    Condition::NotEq => (value - self.th).abs() <= self.hyst,
                };

                if self.debounce > 0 {
                    if is_normal {
                        self.debounce -= 1;
                    } else {
                        self.debounce = self.debounce_off;
                    }
                }

                if is_normal && self.debounce == 0 {
                    self.status = Status::Off;
                    self.debounce = self.debounce_on;
                    transition = true;
                }
            }
            Status::Off => {
                let is_alarm = self.condition.check(value, self.threshold_on);

                if self.debounce > 0 {
                    if is_alarm {
                        self.debounce -= 1;
                    } else {
                        self.debounce = self.debounce_off;
                    }
                }

                if is_alarm && self.debounce == 0 {
                    if update_status {
                        self.status = Status::On;
                    }
                    self.debounce = self.debounce_off;
                    transition = true;
                }
            }
        }

        (transition, self.status)
    }

    /// Resets the detector to the `Off` state and clears the stored raw value.
    pub fn reset(&mut self) {
        self.status = Status::Off;
        self.debounce = self.debounce_on;
        self.prev_raw = 0.0;
    }
}

// ─── Value Extractor ───────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy)]
pub struct ValueKey {
    pub phase: usize,
    pub group: Group,
    pub element: Element,
}

/// Extracts the metrological value addressed by `key` from the socket.
///
/// # Arguments
///
/// * `socket` - Socket holding the measured phase and energy data.
/// * `key` - Phase, group and element to read.
///
/// # Returns
///
/// The extracted value as `f32`, or `None` if the phase is out of range or the
/// group/element combination is not supported.
pub fn extract_value(socket: &MetrologyInsightSocket, key: ValueKey) -> Option<f32> {
    let phase: &PhaseData = socket.phases.get(key.phase)?;

    match (key.group, key.element) {
        (Group::Voltage, Element::Rms) => Some(phase.voltage.rms),
        (Group::Voltage, Element::UrmsHalfCycle) => Some(phase.voltage.urms_half_cycle.urms),
        (Group::Voltage, Element::Frequency) => Some(phase.voltage.pll_state.freq_est),
        (Group::Voltage, Element::Thd) => Some(phase.voltage.thd),

        (Group::Current, Element::Rms) => Some(phase.current.rms),
        (Group::Current, Element::UrmsHalfCycle) => Some(phase.current.urms_half_cycle.urms),
        (Group::Current, Element::Thd) => Some(phase.current.thd),
        (Group::Current, Element::Phi) => Some(phase.phase_angles.c2v_angle),

        (Group::Power, Element::Active) => Some(phase.power_metrics.real_power),
        (Group::Power, Element::Reactive) => Some(phase.power_metrics.reactive_power),
        (Group::Power, Element::Apparent) => Some(phase.power_metrics.apparent_power),

        (Group::ActiveEnergy, Element::Imported) => {
            Some(socket.energy_metrics.active.imported() as f32)
        }
        (Group::ActiveEnergy, Element::Exported) => {
            Some(socket.energy_metrics.active.exported() as f32)
        }

        (Group::ReactiveEnergy, Element::Inductive) => {
            Some(socket.energy_metrics.reactive.inductive() as f32)
        }
        (Group::ReactiveEnergy, Element::Capacitive) => {
            Some(socket.energy_metrics.reactive.capacitive() as f32)
        }

        _ => None,
    }
}

// ─── Detector Manager ──────────────────────────────────────────────────────────

pub const DETECTOR_MAX: usize = 50;

pub struct DetectorManager {
    slots: [Option<(ValueKey, Operation, Detector)>; DETECTOR_MAX],
}

impl DetectorManager {
    /// Creates a `DetectorManager` with no detectors allocated.
    ///
    /// # Returns
    ///
    /// A manager with all slots empty.
    pub fn new() -> Self {
        Self {
            slots: core::array::from_fn(|_| None),
        }
    }

    /// Allocates a new detector in the first free slot.
    ///
    /// # Arguments
    ///
    /// * `key` - Value key the detector monitors.
    /// * `op` - Operation applied to the raw value.
    /// * `condition` - Alarm comparison condition.
    /// * `th` - Alarm threshold.
    /// * `hyst_abs` - Absolute hysteresis band.
    /// * `debounce` - Debounce count in both directions.
    ///
    /// # Returns
    ///
    /// The slot index of the new detector, or `None` if all slots are occupied.
    pub fn create(
        &mut self,
        key: ValueKey,
        op: Operation,
        condition: Condition,
        th: f32,
        hyst_abs: f32,
        debounce: u16,
    ) -> Option<usize> {
        let slot = self.slots.iter().position(|s| s.is_none())?;
        self.slots[slot] = Some((key, op, Detector::new(condition, th, hyst_abs, debounce)));
        Some(slot)
    }

    /// Frees the detector at `id`, if it is a valid slot index.
    ///
    /// # Arguments
    ///
    /// * `id` - Slot index to free.
    pub fn delete(&mut self, id: usize) {
        if id < DETECTOR_MAX {
            self.slots[id] = None;
        }
    }

    /// Evaluates all detectors against the current socket values and invokes `on_event`
    /// on every status transition.
    ///
    /// # Arguments
    ///
    /// * `socket` - Socket with the current measured values.
    /// * `on_event` - Callback invoked with the detector id and new `Status` on transition.
    pub fn evaluate<F>(&mut self, socket: &MetrologyInsightSocket, mut on_event: F)
    where
        F: FnMut(usize, Status),
    {
        for (id, slot) in self.slots.iter_mut().enumerate() {
            if let Some((key, op, detector)) = slot {
                if let Some(raw) = extract_value(socket, *key) {
                    let (transition, status) = detector.process_with_op(raw, *op, true);
                    if transition {
                        on_event(id, status);
                    }
                }
            }
        }
    }
}

impl Default for DetectorManager {
    /// Returns an empty `DetectorManager` via [`Self::new`].
    fn default() -> Self {
        Self::new()
    }
}