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
use std::path::PathBuf;

use crate::*;

#[derive(Debug, Clone)]
pub(crate) struct Service {
  temperature_monitor: PathBuf,
}

#[derive(Debug, thiserror::Error)]
pub(crate) enum ReadError {
  #[error("Reading from filesystem failed")]
  FilesystemError(#[from] std::io::Error),

  #[error("Reading from filesystem failed")]
  ParseError(#[from] core::num::ParseFloatError),
}

impl super::Service for Service {
  fn new(config: config::Values) -> Self {
    Self {
      temperature_monitor: config.hardware.temperature_monitor.into(),
    }
  }
}

impl Service {
  #[tracing::instrument(skip(self))]
  pub(crate) async fn read_temperature(&self) -> Result<f32, ReadError> {
    let temperature =
      tokio::fs::read_to_string(self.temperature_monitor.as_path()).await?;
    let temperature = temperature.parse::<f32>()? / 1000f32;

    tracing::trace!("Read {:?} temperature", temperature);

    Ok(temperature)
  }
}