From 2d334b82a6c6e527febd8af97169cd0bd3347599 Mon Sep 17 00:00:00 2001 From: kenneth dao Date: Sat, 13 Dec 2025 03:58:22 -0800 Subject: [PATCH] added doc strings --- fsae-raspi/src/influxdb.rs | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/fsae-raspi/src/influxdb.rs b/fsae-raspi/src/influxdb.rs index e060211..c4eff0c 100644 --- a/fsae-raspi/src/influxdb.rs +++ b/fsae-raspi/src/influxdb.rs @@ -1,5 +1,64 @@ +//! Utilities for converting sensor data into InfluxDB line protocol. +//! +//! This module provides a helper function that converts any type implementing +//! [`Reading`] into a InfluxDB line protocol for the `/api/v3/write_lp` endpoint. + + use crate::send::Reading; +/// Converts a [`Reading`] into InfluxDB line protocol. +/// +/// # Line protocol format +/// +/// ```text +/// field1=value1,field2="value2",field3=1i +/// ``` +/// +/// The measurement name is taken from [`Reading::topic`]. The function +/// serializes `message` to JSON and, if it is a JSON object, converts each +/// key–value pair into a line protocol field: +/// - JSON numbers are written as: +/// - `123i` for integer types +/// - `123.45` for floating-point values +/// - JSON strings are written as `"..."` +/// - JSON booleans are written as `true` or `false` +/// `` is the current UTC time in nanoseconds +/// +/// # Returns +/// +/// A `String` containing a single InfluxDB line protocol record. If the value +/// cannot be converted to a JSON object, only the measurement name and timestamp are emitted. +/// +/// # Examples +/// +/// ```no_run +/// use serde::Serialize; +/// use crate::send::Reading; +/// use crate::influxdb::to_line_protocol; +/// +/// #[derive(Serialize)] +/// struct Telemetry { +/// apps_travel: f32, +/// motor_speed: f32, +/// } +/// +/// impl Reading for Telemetry { +/// fn topic() -> &'static str { +/// "telemetry" +/// } +/// } +/// +/// fn example() { +/// let t = Telemetry { +/// apps_travel: 0.5, +/// motor_speed: 1234.0, +/// }; +/// +/// let lp = to_line_protocol(&t); +/// // Example output: telemetry apps_travel=0.5,motor_speed=1234 1730000000000000000 +/// println!("{lp}"); +/// } +/// ``` pub fn to_line_protocol(message: &T) -> String { let mut line_protocol = T::topic().to_string();