iot
The iot module provides types and functions for IoT device monitoring, sensor
data processing, anomaly detection, and alert management. It is organized into
sub-modules for types, anomaly detection, and simulation-based monitoring.
Import
Section titled “Import”from std::iot::types use { SensorReading, DeviceState, Alert, ThresholdConfig, MonitoringConfig, sensor_reading, create_alert, threshold_config, symmetric_thresholds, check_thresholds}from std::iot::anomaly use { zscore_anomalies, spike_detection, combined_anomaly_detection }from std::iot::simulation use { monitor_device, monitoring_report }Core Types
Section titled “Core Types”SensorReading
Section titled “SensorReading”| Field | Type | Description |
|---|---|---|
device_id | string | Unique device identifier |
timestamp | timestamp | Reading timestamp |
value | number | Primary sensor value |
unit | string | Unit of measurement |
quality | number | Signal quality 0.0-1.0 |
DeviceState
Section titled “DeviceState”Tracks device operational state during monitoring:
| Field | Type | Description |
|---|---|---|
device_id | string | Device identifier |
status | string | "online", "offline", "degraded", or "maintenance" |
last_reading | number | Last recorded value |
readings_count | number | Total readings processed |
anomaly_count | number | Anomalies detected |
alert_count | number | Alerts generated |
| Field | Type | Description |
|---|---|---|
severity | string | "info", "warning", "critical", or "emergency" |
device_id | string | Source device |
alert_type | string | "threshold", "anomaly", or "offline" |
message | string | Human-readable message |
value | number | Value that triggered alert |
threshold | number | Threshold that was crossed |
ThresholdConfig
Section titled “ThresholdConfig”Four-level threshold configuration with hysteresis:
| Field | Type | Description |
|---|---|---|
high_critical | number | Critical high threshold |
high_warning | number | Warning high threshold |
low_warning | number | Warning low threshold |
low_critical | number | Critical low threshold |
deadband | number | Hysteresis to prevent oscillation |
Type Constructors
Section titled “Type Constructors”sensor_reading(device_id, value, unit?, quality?)— Create a sensor reading with current timestamp.create_alert(severity, device_id, alert_type, message, value?, threshold?)— Create a generic alert.threshold_alert(device_id, value, threshold, is_high?)— Create a threshold alert with auto-severity.offline_alert(device_id, last_seen_seconds)— Create an offline alert.anomaly_alert(device_id, value, expected, deviation)— Create an anomaly alert.threshold_config(high_critical, high_warning, low_warning, low_critical, deadband?)— Create threshold configuration.symmetric_thresholds(center, warning_delta, critical_delta, deadband?)— Create symmetric thresholds around a center value.check_thresholds(value, config) -> string— Check value against thresholds. Returns"ok","warning", or"critical".
Anomaly Detection (std::iot::anomaly)
Section titled “Anomaly Detection (std::iot::anomaly)”Statistical and pattern-based anomaly detection for sensor data:
Statistical
Section titled “Statistical”zscore_anomalies(series, threshold?)— Z-score based (default threshold: 3.0 standard deviations).mad_anomalies(series, threshold?)— Modified Z-score using Median Absolute Deviation (more robust to outliers).iqr_anomalies(series, k?)— Interquartile range based (default k: 1.5).
Rolling Window
Section titled “Rolling Window”rolling_zscore_anomalies(series, window?, threshold?)— Adaptive Z-score in a sliding window.spike_detection(series, window?, spike_threshold?)— Detect sudden spikes/drops relative to recent baseline.
Pattern-Based
Section titled “Pattern-Based”flatline_detection(series, window?, epsilon?)— Detect stuck sensors (no variance).oscillation_detection(series, window?, threshold?, min_oscillations?)— Detect rapid oscillation patterns.drift_detection(series, baseline_window?, drift_threshold?)— Detect gradual drift from baseline.
Contextual
Section titled “Contextual”time_aware_anomalies(readings, day_thresholds, night_thresholds, day_start_hour?, day_end_hour?)— Different thresholds for day/night periods.
Aggregation
Section titled “Aggregation”combined_anomaly_detection(series)— Flags anomaly if any method detects one (Z-score OR IQR OR spike).consensus_anomalies(series, min_agreement?)— Only flags if multiple methods agree.count_anomalies(anomaly_flags) -> int— Count true values.get_anomaly_indices(anomaly_flags) -> Array<int>— Get indices of anomalies.
Device Monitoring (std::iot::simulation)
Section titled “Device Monitoring (std::iot::simulation)”Simulation-based device monitoring using the core simulation engine:
monitor_device(readings, config)
Section titled “monitor_device(readings, config)”Monitor a single device’s sensor readings. Uses Welford’s online algorithm for running statistics and Z-score anomaly detection.
let result = monitor_device(temperature_readings, { device_id: "sensor-001", thresholds: symmetric_thresholds(25.0, 5.0, 10.0), offline_timeout: 300, anomaly_threshold: 3.0, collect_alerts: true})monitor_sensors(sensors, config, correlation_check?)
Section titled “monitor_sensors(sensors, config, correlation_check?)”Monitor multiple correlated sensors with optional cross-sensor correlation checks.
monitoring_report(result)
Section titled “monitoring_report(result)”Generate a summary report from monitoring results.
See Also
Section titled “See Also”- Simulation — core simulation framework